refactor: separate workload class from placement driver
This commit is contained in:
@@ -21,29 +21,37 @@ func task(t *testing.T, labels string) *runnerv1.Task {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewSelectsBackendFromRunsOn(t *testing.T) {
|
||||
func TestNewSelectsPlacementFromRunsOn(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
labels string
|
||||
backend Backend
|
||||
labels string
|
||||
placement Placement
|
||||
}{
|
||||
{"[self-hosted, pod]", BackendPod},
|
||||
{"[self-hosted, vm]", BackendVM},
|
||||
{"[self-hosted, vm-dev]", BackendVM},
|
||||
{"[self-hosted, pod]", KubernetesContainer},
|
||||
{"[self-hosted, container]", KubernetesContainer},
|
||||
{"[self-hosted, container, kubernetes]", KubernetesContainer},
|
||||
{"[self-hosted, vm]", OpenSandboxVM},
|
||||
{"[self-hosted, vm-dev]", OpenSandboxVM},
|
||||
{"[self-hosted, vm, opensandbox]", OpenSandboxVM},
|
||||
} {
|
||||
assignment, err := New(task(t, test.labels), "ddupan.top")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if assignment.Backend != test.backend || assignment.ID != "gitea-task-42" {
|
||||
if assignment.Placement != test.placement || assignment.ID != "gitea-task-42" {
|
||||
t.Fatalf("assignment = %#v", assignment)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewRejectsAmbiguousBackend(t *testing.T) {
|
||||
func TestNewRejectsInvalidPlacement(t *testing.T) {
|
||||
for _, labels := range []string{
|
||||
"[self-hosted]",
|
||||
"[self-hosted, pod, vm]",
|
||||
"[self-hosted, pod, container]",
|
||||
"[self-hosted, pod, kubernetes]",
|
||||
"[self-hosted, container, opensandbox]",
|
||||
"[self-hosted, vm, kubernetes]",
|
||||
"[self-hosted, container, kubernetes, opensandbox]",
|
||||
"[pod]",
|
||||
} {
|
||||
if _, err := New(task(t, labels), "ddupan.top"); err == nil {
|
||||
@@ -65,27 +73,28 @@ func TestAssignmentWireRoundTripAndValidation(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.ID != want.ID || got.Backend != want.Backend || got.Identity != want.Identity || !bytes.Equal(got.Task.WorkflowPayload, want.Task.WorkflowPayload) {
|
||||
if got.ID != want.ID || got.Placement != want.Placement || got.Identity != want.Identity || !bytes.Equal(got.Task.WorkflowPayload, want.Task.WorkflowPayload) {
|
||||
t.Fatalf("round trip = %#v, want %#v", got, want)
|
||||
}
|
||||
|
||||
tampered := bytes.Replace(data, []byte(`"backend":"pod"`), []byte(`"backend":"vm"`), 1)
|
||||
tampered := bytes.Replace(data, []byte(`"driver":"kubernetes"`), []byte(`"driver":"opensandbox"`), 1)
|
||||
if _, err := Unmarshal(tampered, "ddupan.top"); err == nil {
|
||||
t.Fatal("expected tampered backend to fail")
|
||||
t.Fatal("expected tampered placement to fail")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFromMetadataRecoversMinimalAssignment(t *testing.T) {
|
||||
assignment, err := FromMetadata(map[string]string{
|
||||
"ci.ddupan.top/assignment-id": "gitea-task-42",
|
||||
"ci.ddupan.top/task-id": "42",
|
||||
"ci.ddupan.top/backend": "vm",
|
||||
"ci.ddupan.top/assignment-id": "gitea-task-42",
|
||||
"ci.ddupan.top/task-id": "42",
|
||||
"ci.ddupan.top/workload-class": "vm",
|
||||
"ci.ddupan.top/driver": "opensandbox",
|
||||
}, map[string]string{
|
||||
"ci.ddupan.top/repository": "owner/repo",
|
||||
"ci.ddupan.top/job-key": "publish",
|
||||
"ci.ddupan.top/spiffe-id": "spiffe://ddupan.top/ci/owner/repo/publish",
|
||||
}, "ddupan.top")
|
||||
if err != nil || assignment.Task.GetId() != 42 || assignment.Backend != BackendVM {
|
||||
if err != nil || assignment.Task.GetId() != 42 || assignment.Placement != OpenSandboxVM {
|
||||
t.Fatalf("assignment=%#v err=%v", assignment, err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user