From e8caac073168835a6f56760600b2edb23dd9c1ff Mon Sep 17 00:00:00 2001 From: JordanBrockopp Date: Tue, 4 Oct 2022 13:38:20 -0500 Subject: [PATCH 1/3] test(executor): add kubernetes runtime for CreateStage tests --- executor/linux/stage_test.go | 50 ++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/executor/linux/stage_test.go b/executor/linux/stage_test.go index c70b94ce..a35afd71 100644 --- a/executor/linux/stage_test.go +++ b/executor/linux/stage_test.go @@ -20,6 +20,7 @@ import ( "github.com/go-vela/worker/internal/message" "github.com/go-vela/worker/runtime" "github.com/go-vela/worker/runtime/docker" + "github.com/go-vela/worker/runtime/kubernetes" "github.com/urfave/cli/v2" ) @@ -60,6 +61,11 @@ func TestLinux_CreateStage(t *testing.T) { t.Errorf("unable to create docker runtime engine: %v", err) } + _kubernetes, err := kubernetes.NewMock(testPod(true)) + if err != nil { + t.Errorf("unable to create kubernetes runtime engine: %v", err) + } + // setup tests tests := []struct { name string @@ -86,6 +92,25 @@ func TestLinux_CreateStage(t *testing.T) { }, }, }, + { + name: "kubernetes-basic stage", + failure: false, + runtime: _kubernetes, + stage: &pipeline.Stage{ + Name: "echo", + Steps: pipeline.ContainerSlice{ + { + ID: "github_octocat_1_echo_echo", + Directory: "/vela/src/github.com/github/octocat", + Environment: map[string]string{"FOO": "bar"}, + Image: "alpine:latest", + Name: "echo", + Number: 1, + Pull: "not_present", + }, + }, + }, + }, { name: "docker-stage with step container with image not found", failure: true, @@ -105,12 +130,37 @@ func TestLinux_CreateStage(t *testing.T) { }, }, }, + //{ + // name: "kubernetes-stage with step container with image not found", + // failure: true, // FIXME: make Kubernetes mock simulate failure similar to Docker mock + // runtime: _kubernetes, + // stage: &pipeline.Stage{ + // Name: "echo", + // Steps: pipeline.ContainerSlice{ + // { + // ID: "github_octocat_1_echo_echo", + // Directory: "/vela/src/github.com/github/octocat", + // Environment: map[string]string{"FOO": "bar"}, + // Image: "alpine:notfound", + // Name: "echo", + // Number: 1, + // Pull: "not_present", + // }, + // }, + // }, + //}, { name: "docker-empty stage", failure: true, runtime: _docker, stage: new(pipeline.Stage), }, + { + name: "kubernetes-empty stage", + failure: true, + runtime: _kubernetes, + stage: new(pipeline.Stage), + }, } // run tests From 873e1fd35b720b0dd8ba5d4470ceda16c3a7ef03 Mon Sep 17 00:00:00 2001 From: JordanBrockopp Date: Tue, 4 Oct 2022 14:25:12 -0500 Subject: [PATCH 2/3] test(executor): add k8s runtime for stage tests --- executor/linux/stage_test.go | 169 +++++++++++++++++++++++++++++++++++ 1 file changed, 169 insertions(+) diff --git a/executor/linux/stage_test.go b/executor/linux/stage_test.go index a35afd71..81106128 100644 --- a/executor/linux/stage_test.go +++ b/executor/linux/stage_test.go @@ -223,6 +223,11 @@ func TestLinux_PlanStage(t *testing.T) { t.Errorf("unable to create docker runtime engine: %v", err) } + _kubernetes, err := kubernetes.NewMock(testPod(true)) + if err != nil { + t.Errorf("unable to create kubernetes runtime engine: %v", err) + } + dockerTestMap := new(sync.Map) dockerTestMap.Store("foo", make(chan error, 1)) @@ -230,6 +235,13 @@ func TestLinux_PlanStage(t *testing.T) { dtm.(chan error) <- nil close(dtm.(chan error)) + kubernetesTestMap := new(sync.Map) + kubernetesTestMap.Store("foo", make(chan error, 1)) + + ktm, _ := kubernetesTestMap.Load("foo") + ktm.(chan error) <- nil + close(ktm.(chan error)) + dockerErrMap := new(sync.Map) dockerErrMap.Store("foo", make(chan error, 1)) @@ -237,6 +249,13 @@ func TestLinux_PlanStage(t *testing.T) { dem.(chan error) <- errors.New("bar") close(dem.(chan error)) + kubernetesErrMap := new(sync.Map) + kubernetesErrMap.Store("foo", make(chan error, 1)) + + kem, _ := kubernetesErrMap.Load("foo") + kem.(chan error) <- errors.New("bar") + close(kem.(chan error)) + // setup tests tests := []struct { name string @@ -265,6 +284,26 @@ func TestLinux_PlanStage(t *testing.T) { }, stageMap: new(sync.Map), }, + { + name: "kubernetes-basic stage", + failure: false, + runtime: _kubernetes, + stage: &pipeline.Stage{ + Name: "echo", + Steps: pipeline.ContainerSlice{ + { + ID: "github_octocat_1_echo_echo", + Directory: "/vela/src/github.com/github/octocat", + Environment: map[string]string{"FOO": "bar"}, + Image: "alpine:latest", + Name: "echo", + Number: 1, + Pull: "not_present", + }, + }, + }, + stageMap: new(sync.Map), + }, { name: "docker-basic stage with nil stage map", failure: false, @@ -286,6 +325,27 @@ func TestLinux_PlanStage(t *testing.T) { }, stageMap: dockerTestMap, }, + { + name: "kubernetes-basic stage with nil stage map", + failure: false, + runtime: _kubernetes, + stage: &pipeline.Stage{ + Name: "echo", + Needs: []string{"foo"}, + Steps: pipeline.ContainerSlice{ + { + ID: "github_octocat_1_echo_echo", + Directory: "/vela/src/github.com/github/octocat", + Environment: map[string]string{"FOO": "bar"}, + Image: "alpine:latest", + Name: "echo", + Number: 1, + Pull: "not_present", + }, + }, + }, + stageMap: kubernetesTestMap, + }, { name: "docker-basic stage with error stage map", failure: true, @@ -307,6 +367,27 @@ func TestLinux_PlanStage(t *testing.T) { }, stageMap: dockerErrMap, }, + { + name: "kubernetes-basic stage with error stage map", + failure: true, + runtime: _kubernetes, + stage: &pipeline.Stage{ + Name: "echo", + Needs: []string{"foo"}, + Steps: pipeline.ContainerSlice{ + { + ID: "github_octocat_1_echo_echo", + Directory: "/vela/src/github.com/github/octocat", + Environment: map[string]string{"FOO": "bar"}, + Image: "alpine:latest", + Name: "echo", + Number: 1, + Pull: "not_present", + }, + }, + }, + stageMap: kubernetesErrMap, + }, } // run tests @@ -361,6 +442,13 @@ func TestLinux_ExecStage(t *testing.T) { t.Errorf("unable to create docker runtime engine: %v", err) } + _kubernetes, err := kubernetes.NewMock(testPod(true)) + if err != nil { + t.Errorf("unable to create kubernetes runtime engine: %v", err) + } + + _kubernetes.PodTracker.Start(context.Background()) + streamRequests, done := message.MockStreamRequestsWithCancel(context.Background()) defer done() @@ -391,6 +479,25 @@ func TestLinux_ExecStage(t *testing.T) { }, }, }, + { + name: "kubernetes-basic stage", + failure: false, + runtime: _kubernetes, + stage: &pipeline.Stage{ + Name: "echo", + Steps: pipeline.ContainerSlice{ + { + ID: "github-octocat-1-echo-echo", + Directory: "/vela/src/github.com/github/octocat", + Environment: map[string]string{"FOO": "bar"}, + Image: "alpine:latest", + Name: "echo", + Number: 1, + Pull: "not_present", + }, + }, + }, + }, { name: "docker-stage with step container with image not found", failure: true, @@ -411,6 +518,25 @@ func TestLinux_ExecStage(t *testing.T) { }, }, }, + //{ + // name: "kubernetes-stage with step container with image not found", + // failure: true, // FIXME: make Kubernetes mock simulate failure similar to Docker mock + // runtime: _kubernetes, + // stage: &pipeline.Stage{ + // Name: "echo", + // Steps: pipeline.ContainerSlice{ + // { + // ID: "github-octocat-1-echo-echo", + // Directory: "/vela/src/github.com/github/octocat", + // Environment: map[string]string{"FOO": "bar"}, + // Image: "alpine:notfound", + // Name: "echo", + // Number: 1, + // Pull: "not_present", + // }, + // }, + // }, + //}, { name: "docker-stage with step container with bad number", failure: true, @@ -431,6 +557,25 @@ func TestLinux_ExecStage(t *testing.T) { }, }, }, + { + name: "kubernetes-stage with step container with bad number", + failure: true, + runtime: _kubernetes, + stage: &pipeline.Stage{ + Name: "echo", + Steps: pipeline.ContainerSlice{ + { + ID: "github-octocat-1-echo-echo", + Directory: "/vela/src/github.com/github/octocat", + Environment: map[string]string{"FOO": "bar"}, + Image: "alpine:latest", + Name: "echo", + Number: 0, + Pull: "not_present", + }, + }, + }, + }, } // run tests @@ -489,6 +634,11 @@ func TestLinux_DestroyStage(t *testing.T) { t.Errorf("unable to create docker runtime engine: %v", err) } + _kubernetes, err := kubernetes.NewMock(testPod(true)) + if err != nil { + t.Errorf("unable to create kubernetes runtime engine: %v", err) + } + // setup tests tests := []struct { name string @@ -515,6 +665,25 @@ func TestLinux_DestroyStage(t *testing.T) { }, }, }, + { + name: "kubernetes-basic stage", + failure: false, + runtime: _kubernetes, + stage: &pipeline.Stage{ + Name: "echo", + Steps: pipeline.ContainerSlice{ + { + ID: "github_octocat_1_echo_echo", + Directory: "/vela/src/github.com/github/octocat", + Environment: map[string]string{"FOO": "bar"}, + Image: "alpine:latest", + Name: "echo", + Number: 1, + Pull: "not_present", + }, + }, + }, + }, } // run tests From 3e0492be9e26420b709ee39042f5b8aca804f79d Mon Sep 17 00:00:00 2001 From: Jacob Floyd Date: Tue, 28 Feb 2023 11:55:03 -0600 Subject: [PATCH 3/3] sanitize container names for k8s runtime --- executor/linux/stage_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/executor/linux/stage_test.go b/executor/linux/stage_test.go index 81106128..3bd2e684 100644 --- a/executor/linux/stage_test.go +++ b/executor/linux/stage_test.go @@ -100,7 +100,7 @@ func TestLinux_CreateStage(t *testing.T) { Name: "echo", Steps: pipeline.ContainerSlice{ { - ID: "github_octocat_1_echo_echo", + ID: "github-octocat-1-echo-echo", Directory: "/vela/src/github.com/github/octocat", Environment: map[string]string{"FOO": "bar"}, Image: "alpine:latest", @@ -138,7 +138,7 @@ func TestLinux_CreateStage(t *testing.T) { // Name: "echo", // Steps: pipeline.ContainerSlice{ // { - // ID: "github_octocat_1_echo_echo", + // ID: "github-octocat-1-echo-echo", // Directory: "/vela/src/github.com/github/octocat", // Environment: map[string]string{"FOO": "bar"}, // Image: "alpine:notfound", @@ -292,7 +292,7 @@ func TestLinux_PlanStage(t *testing.T) { Name: "echo", Steps: pipeline.ContainerSlice{ { - ID: "github_octocat_1_echo_echo", + ID: "github-octocat-1-echo-echo", Directory: "/vela/src/github.com/github/octocat", Environment: map[string]string{"FOO": "bar"}, Image: "alpine:latest", @@ -334,7 +334,7 @@ func TestLinux_PlanStage(t *testing.T) { Needs: []string{"foo"}, Steps: pipeline.ContainerSlice{ { - ID: "github_octocat_1_echo_echo", + ID: "github-octocat-1-echo-echo", Directory: "/vela/src/github.com/github/octocat", Environment: map[string]string{"FOO": "bar"}, Image: "alpine:latest", @@ -376,7 +376,7 @@ func TestLinux_PlanStage(t *testing.T) { Needs: []string{"foo"}, Steps: pipeline.ContainerSlice{ { - ID: "github_octocat_1_echo_echo", + ID: "github-octocat-1-echo-echo", Directory: "/vela/src/github.com/github/octocat", Environment: map[string]string{"FOO": "bar"}, Image: "alpine:latest", @@ -673,7 +673,7 @@ func TestLinux_DestroyStage(t *testing.T) { Name: "echo", Steps: pipeline.ContainerSlice{ { - ID: "github_octocat_1_echo_echo", + ID: "github-octocat-1-echo-echo", Directory: "/vela/src/github.com/github/octocat", Environment: map[string]string{"FOO": "bar"}, Image: "alpine:latest",