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 TestAPoolOnAGroupHoldingTheSeparatorIsRefused(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)
if _, _, err := e.RegisterYAML("n", []byte("quarry_pool: linux\njobs: {}\n"), "a__b"); err == nil {
t.Errorf("FAIL: a group holding the separator took a pool")
}
if !t.Failed() {
t.Log("ok")
}
}
// Two repositories' pools can never make one group: a pool name holds no
// underscore and a repository name no double underscore, so the first
// separator in a pool group splits it one way only.
func TestNoTwoRepositoriesPoolsMakeTheSameGroup(t *testing.T) {
names := []string{"pi", "pi-c", "pi_c", "pi-c-linux", "a", "a-b", "a_b", "c", "linux", "c-linux"}
pools := []string{"", "c", "linux", "c-linux", "b", "a"}
seen := map[string]string{}
for _, repo := range names {
for _, pool := range pools {
g, err := PoolGroup(GroupFor(repo), pool)
if err != nil {
t.Errorf("FAIL: %s/%s: %v", repo, pool, err)
continue
}
who := repo + "/" + pool
if prev, dup := seen[g]; dup {
t.Errorf("FAIL: %s and %s are both group %s", prev, who, g)
}
seen[g] = who
}
}
for _, bad := range []string{"a_b", "-a", "a-", "a--b", "A", "a_b", "a.b"} {
if ValidPool(bad) {
t.Errorf("FAIL: pool %q accepted", bad)
}
}
if !t.Failed() {
t.Log("ok")
}
}
func TestPoolTaskTypeIsGroupDashPoolJob(t *testing.T) {
if got, err := PoolTaskType("pic", "linux"); err != nil || got != "pic__linux.job" {
t.Errorf("FAIL: pool task type = %q, %v", got, err)
}
if got, err := PoolTaskType("pic", ""); err != nil || got != "pic.job" {
t.Errorf("FAIL: default task type = %q, %v", got, err)
}
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.job", "pic__.job", "pic__a_b.job", "pic__a__b.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")
}
}