Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance(executor tests): Add kubernetes runtime test cases for Stage tests #441

Merged
merged 4 commits into from
Mar 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
219 changes: 219 additions & 0 deletions executor/linux/stage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -173,20 +223,39 @@ 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))

dtm, _ := dockerTestMap.Load("foo")
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))

dem, _ := dockerErrMap.Load("foo")
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
Expand Down Expand Up @@ -215,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,
Expand All @@ -236,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,
Expand All @@ -257,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
Expand Down Expand Up @@ -311,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()

Expand Down Expand Up @@ -341,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,
Expand All @@ -361,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,
Expand All @@ -381,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
Expand Down Expand Up @@ -439,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
Expand All @@ -465,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
Expand Down