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 Service tests #442

Merged
merged 8 commits into from
Mar 20, 2023
210 changes: 210 additions & 0 deletions executor/linux/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,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"
)

func TestLinux_CreateService(t *testing.T) {
Expand All @@ -39,6 +40,11 @@ func TestLinux_CreateService(t *testing.T) {
t.Errorf("unable to create docker runtime engine: %v", err)
}

_kubernetes, err := kubernetes.NewMock(testPod(false))
if err != nil {
t.Errorf("unable to create kubernetes runtime engine: %v", err)
}

// setup tests
tests := []struct {
name string
Expand All @@ -62,6 +68,22 @@ func TestLinux_CreateService(t *testing.T) {
Pull: "not_present",
},
},
{
name: "kubernetes-basic service container",
failure: false,
runtime: _kubernetes,
container: &pipeline.Container{
ID: "service-github-octocat-1-postgres",
Detach: true,
Directory: "/vela/src/github.com/github/octocat",
Environment: map[string]string{"FOO": "bar"},
Image: "postgres:12-alpine",
Name: "postgres",
Number: 1,
Ports: []string{"5432:5432"},
Pull: "not_present",
},
},
{
name: "docker-service container with image not found",
failure: true,
Expand All @@ -78,12 +100,34 @@ func TestLinux_CreateService(t *testing.T) {
Pull: "not_present",
},
},
// {
// name: "kubernetes-service container with image not found",
// failure: true, // FIXME: make Kubernetes mock simulate failure similar to Docker mock
// runtime: _kubernetes,
// container: &pipeline.Container{
// ID: "service-github-octocat-1-postgres",
// Detach: true,
// Directory: "/vela/src/github.com/github/octocat",
// Environment: map[string]string{"FOO": "bar"},
// Image: "postgres:notfound",
// Name: "postgres",
// Number: 1,
// Ports: []string{"5432:5432"},
// Pull: "not_present",
// },
// },
{
name: "docker-empty service container",
failure: true,
runtime: _docker,
container: new(pipeline.Container),
},
{
name: "kubernetes-empty service container",
failure: true,
runtime: _kubernetes,
container: new(pipeline.Container),
},
}

// run tests
Expand Down Expand Up @@ -138,6 +182,11 @@ func TestLinux_PlanService(t *testing.T) {
t.Errorf("unable to create docker runtime engine: %v", err)
}

_kubernetes, err := kubernetes.NewMock(testPod(false))
if err != nil {
t.Errorf("unable to create kubernetes runtime engine: %v", err)
}

// setup tests
tests := []struct {
name string
Expand All @@ -161,6 +210,22 @@ func TestLinux_PlanService(t *testing.T) {
Pull: "not_present",
},
},
{
name: "kubernetes-basic service container",
failure: false,
runtime: _kubernetes,
container: &pipeline.Container{
ID: "service-github-octocat-1-postgres",
Detach: true,
Directory: "/vela/src/github.com/github/octocat",
Environment: map[string]string{"FOO": "bar"},
Image: "postgres:12-alpine",
Name: "postgres",
Number: 1,
Ports: []string{"5432:5432"},
Pull: "not_present",
},
},
{
name: "docker-service container with nil environment",
failure: true,
Expand All @@ -177,12 +242,34 @@ func TestLinux_PlanService(t *testing.T) {
Pull: "not_present",
},
},
{
name: "kubernetes-service container with nil environment",
failure: true,
runtime: _kubernetes,
container: &pipeline.Container{
ID: "service-github-octocat-1-postgres",
Detach: true,
Directory: "/vela/src/github.com/github/octocat",
Environment: nil,
Image: "postgres:12-alpine",
Name: "postgres",
Number: 1,
Ports: []string{"5432:5432"},
Pull: "not_present",
},
},
{
name: "docker-empty service container",
failure: true,
runtime: _docker,
container: new(pipeline.Container),
},
{
name: "kubernetes-empty service container",
failure: true,
runtime: _kubernetes,
container: new(pipeline.Container),
},
}

// run tests
Expand Down Expand Up @@ -237,6 +324,11 @@ func TestLinux_ExecService(t *testing.T) {
t.Errorf("unable to create docker runtime engine: %v", err)
}

_kubernetes, err := kubernetes.NewMock(testPod(false))
if err != nil {
t.Errorf("unable to create kubernetes runtime engine: %v", err)
}

streamRequests, done := message.MockStreamRequestsWithCancel(context.Background())
defer done()

Expand All @@ -263,6 +355,22 @@ func TestLinux_ExecService(t *testing.T) {
Pull: "not_present",
},
},
{
name: "kubernetes-basic service container",
failure: false,
runtime: _kubernetes,
container: &pipeline.Container{
ID: "service-github-octocat-1-postgres",
Detach: true,
Directory: "/vela/src/github.com/github/octocat",
Environment: map[string]string{"FOO": "bar"},
Image: "postgres:12-alpine",
Name: "postgres",
Number: 1,
Ports: []string{"5432:5432"},
Pull: "not_present",
},
},
{
name: "docker-service container with image not found",
failure: true,
Expand All @@ -279,12 +387,34 @@ func TestLinux_ExecService(t *testing.T) {
Pull: "not_present",
},
},
{
name: "kubernetes-service container with image not found",
failure: false,
runtime: _kubernetes,
container: &pipeline.Container{
ID: "service-github-octocat-1-postgres",
Detach: true,
Directory: "/vela/src/github.com/github/octocat",
Environment: map[string]string{"FOO": "bar"},
Image: "postgres:notfound",
Name: "postgres",
Number: 1,
Ports: []string{"5432:5432"},
Pull: "not_present",
},
},
{
name: "docker-empty service container",
failure: true,
runtime: _docker,
container: new(pipeline.Container),
},
{
name: "kubernetes-empty service container",
failure: true,
runtime: _kubernetes,
container: new(pipeline.Container),
},
}

// run tests
Expand Down Expand Up @@ -345,6 +475,11 @@ func TestLinux_StreamService(t *testing.T) {
t.Errorf("unable to create docker runtime engine: %v", err)
}

_kubernetes, err := kubernetes.NewMock(testPod(false))
if err != nil {
t.Errorf("unable to create kubernetes runtime engine: %v", err)
}

// setup tests
tests := []struct {
name string
Expand All @@ -368,6 +503,22 @@ func TestLinux_StreamService(t *testing.T) {
Pull: "not_present",
},
},
{
name: "kubernetes-basic service container",
failure: false,
runtime: _kubernetes,
container: &pipeline.Container{
ID: "service-github-octocat-1-postgres",
Detach: true,
Directory: "/vela/src/github.com/github/octocat",
Environment: map[string]string{"FOO": "bar"},
Image: "postgres:12-alpine",
Name: "postgres",
Number: 1,
Ports: []string{"5432:5432"},
Pull: "not_present",
},
},
{
name: "docker-service container with name not found",
failure: true,
Expand All @@ -384,12 +535,34 @@ func TestLinux_StreamService(t *testing.T) {
Pull: "not_present",
},
},
//{
// name: "kubernetes-service container with name not found",
// failure: true, // FIXME: make Kubernetes mock simulate failure similar to Docker mock
// runtime: _kubernetes,
// container: &pipeline.Container{
// ID: "service-github-octocat-1-notfound",
// Detach: true,
// Directory: "/vela/src/github.com/github/octocat",
// Environment: map[string]string{"FOO": "bar"},
// Image: "postgres:12-alpine",
// Name: "notfound",
// Number: 1,
// Ports: []string{"5432:5432"},
// Pull: "not_present",
// },
//},
{
name: "docker-empty service container",
failure: true,
runtime: _docker,
container: new(pipeline.Container),
},
{
name: "kubernetes-empty service container",
failure: true,
runtime: _kubernetes,
container: new(pipeline.Container),
},
}

// run tests
Expand Down Expand Up @@ -449,6 +622,11 @@ func TestLinux_DestroyService(t *testing.T) {
t.Errorf("unable to create docker runtime engine: %v", err)
}

_kubernetes, err := kubernetes.NewMock(testPod(false))
if err != nil {
t.Errorf("unable to create kubernetes runtime engine: %v", err)
}

// setup tests
tests := []struct {
name string
Expand All @@ -472,6 +650,22 @@ func TestLinux_DestroyService(t *testing.T) {
Pull: "not_present",
},
},
{
name: "kubernetes-basic service container",
failure: false,
runtime: _kubernetes,
container: &pipeline.Container{
ID: "service-github-octocat-1-postgres",
Detach: true,
Directory: "/vela/src/github.com/github/octocat",
Environment: map[string]string{"FOO": "bar"},
Image: "postgres:12-alpine",
Name: "postgres",
Number: 1,
Ports: []string{"5432:5432"},
Pull: "not_present",
},
},
{
name: "docker-service container with ignoring name not found",
failure: true,
Expand All @@ -488,6 +682,22 @@ func TestLinux_DestroyService(t *testing.T) {
Pull: "not_present",
},
},
//{
// name: "kubernetes-service container with ignoring name not found",
// failure: true, // FIXME: make Kubernetes mock simulate failure similar to Docker mock
// runtime: _kubernetes,
// container: &pipeline.Container{
// ID: "service-github-octocat-1-ignorenotfound",
// Detach: true,
// Directory: "/vela/src/github.com/github/octocat",
// Environment: map[string]string{"FOO": "bar"},
// Image: "postgres:12-alpine",
// Name: "ignorenotfound",
// Number: 1,
// Ports: []string{"5432:5432"},
// Pull: "not_present",
// },
//},
}

// run tests
Expand Down