Skip to content
quarry Artifact [0b6b464298]
Login

Artifact [0b6b464298]

Artifact 0b6b46429839a5fcc19ce24f8b5a9c87f59036e3e145d5621fe5b2a69d5f8aa6:


package ci

import (
	"bytes"
	"encoding/json"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"
	"time"
)

func TestQuarryPoolIsStrippedAndNamesTheNamespace(t *testing.T) {
	var gotNS, gotSpec []string
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		var body struct {
			Name, Spec    string
			TaskNamespace string `json:"task_namespace"`
		}
		json.NewDecoder(r.Body).Decode(&body)
		gotNS = append(gotNS, body.TaskNamespace)
		gotSpec = append(gotSpec, body.Spec)
		w.Write([]byte(`{"workflow":{"name":"x"},"registered":true,"diagnostics":[]}`))
	}))
	defer srv.Close()
	e := New(srv.URL)

	pooled := []byte("quarry_pool: linux   # the Linux box\njobs:\n  a:\n    steps: []\n")
	plain := []byte("jobs:\n  a:\n    steps: []\n")
	if _, _, err := e.RegisterYAML("n", pooled, "pic"); err != nil {
		t.Fatalf("FAIL: register pooled: %v", err)
	}
	if _, _, err := e.RegisterYAML("n", plain, "pic"); err != nil {
		t.Fatalf("FAIL: register plain: %v", err)
	}
	if _, _, err := e.ValidateYAML("n", pooled, "pic"); err != nil {
		t.Fatalf("FAIL: validate pooled: %v", err)
	}
	want := []string{"pic-linux", "pic", "pic-linux"}
	for i, w := range want {
		if i >= len(gotNS) || gotNS[i] != w {
			t.Errorf("FAIL: call %d namespace = %v, want %q", i, gotNS, w)
		}
	}
	if len(gotSpec) > 0 {
		if strings.Contains(gotSpec[0], "quarry_pool") {
			t.Errorf("FAIL: the engine was sent quarry_pool: %q", gotSpec[0])
		}
		if bytes.Count([]byte(gotSpec[0]), []byte("\n")) != bytes.Count(pooled, []byte("\n")) {
			t.Errorf("FAIL: line count changed: %q", gotSpec[0])
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

func TestABadQuarryPoolIsRefused(t *testing.T) {
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Path == "/v1/ci/compile" {
			t.Errorf("FAIL: the engine was asked to register a refused pipeline")
		}
		w.Write([]byte(`{"diagnostics":[]}`))
	}))
	defer srv.Close()
	e := New(srv.URL)
	for _, bad := range []string{"Linux", "a_b", "", strings.Repeat("a", 33), "a.b", "-"} {
		spec := []byte("quarry_pool: \"" + bad + "\"\njobs: {}\n")
		if _, _, err := e.RegisterYAML("n", spec, "pic"); err == nil || !strings.Contains(err.Error(), "quarry_pool") {
			t.Errorf("FAIL: register accepted pool %q: %v", bad, err)
		}
		if diags, _, _ := e.ValidateYAML("n", spec, "pic"); len(diags) == 0 {
			t.Errorf("FAIL: validate accepted pool %q", bad)
		}
	}
	for _, good := range []string{"linux", "a", "mac-mini-2", strings.Repeat("a", 32)} {
		if got, err := poolOf([]byte("quarry_pool: " + good + "\n")); err != nil || got != good {
			t.Errorf("FAIL: pool %q = %q, %v", good, got, err)
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

func TestAPoolSharingARepositorysGroupIsRefused(t *testing.T) {
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		t.Errorf("FAIL: the engine was asked about a refused pipeline")
	}))
	defer srv.Close()
	e := New(srv.URL)
	e.SetGroupTaken(func(g string) bool { return g == "pic-linux" })
	_, _, err := e.RegisterYAML("n", []byte("quarry_pool: linux\njobs: {}\n"), "pic")
	if err == nil || !strings.Contains(err.Error(), "quarry_pool") {
		t.Errorf("FAIL: a pool that is another repository's group was accepted: %v", err)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

func TestPoolTaskTypeIsGroupDashPoolJob(t *testing.T) {
	if got := PoolTaskType("pic", "linux"); got != "pic-linux.job" {
		t.Errorf("FAIL: pool task type = %q", got)
	}
	if got := PoolTaskType("pic", ""); got != "pic.job" {
		t.Errorf("FAIL: default task type = %q", got)
	}
	if pool, ok := PoolOfTaskType("pic", "pic-linux.job"); !ok || pool != "linux" {
		t.Errorf("FAIL: PoolOfTaskType = %q %v", pool, ok)
	}
	for _, no := range []string{"pic.job", "pic-Linux.job", "other-linux.job", "pic-linux.test", "pic-.job"} {
		if pool, ok := PoolOfTaskType("pic", no); ok {
			t.Errorf("FAIL: %q read as pool %q", no, pool)
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

func TestAPoolRunnerCountsAsARunnerForItsRepository(t *testing.T) {
	now := time.Now()
	seen := now.Format(time.RFC3339)
	pooled := []Worker{{WorkerGroups: []string{"pic-linux"}, LastSeen: seen}}
	if !RunnerFor(pooled, "pic", now) {
		t.Errorf("FAIL: a pic-linux runner is not a runner for pic")
	}
	other := []Worker{{WorkerGroups: []string{"pid-linux"}, LastSeen: seen}}
	if RunnerFor(other, "pic", now) {
		t.Errorf("FAIL: another repository's pool counted")
	}
	if !t.Failed() {
		t.Log("ok")
	}
}