Skip to content

Commit

Permalink
tests(executor): add streamRequests mock to build/exec tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cognifloyd committed Apr 20, 2022
1 parent 9be71cf commit 5b5e902
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 11 deletions.
19 changes: 19 additions & 0 deletions executor/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,22 @@ type StreamRequest struct {
// Container is the container for the service or step to stream logs for.
Container *pipeline.Container
}

// MockStreamRequestsWithCancel discards all requests until you call the cancel function.
func MockStreamRequestsWithCancel(ctx context.Context) (chan<- StreamRequest, context.CancelFunc) {
cancelCtx, done := context.WithCancel(ctx)
streamRequests := make(chan StreamRequest)

// discard all stream requests
go func() {
for {
select {
case <-streamRequests:
case <-cancelCtx.Done():
return
}
}
}()

return streamRequests, done
}
11 changes: 9 additions & 2 deletions executor/linux/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/go-vela/server/mock/server"
"github.com/urfave/cli/v2"

"github.com/go-vela/worker/executor"
"github.com/go-vela/worker/runtime/docker"

"github.com/go-vela/sdk-go/vela"
Expand Down Expand Up @@ -232,6 +233,9 @@ func TestLinux_AssembleBuild(t *testing.T) {
t.Errorf("unable to create runtime engine: %v", err)
}

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

tests := []struct {
failure bool
pipeline string
Expand Down Expand Up @@ -316,7 +320,7 @@ func TestLinux_AssembleBuild(t *testing.T) {
t.Errorf("unable to create build: %v", err)
}

err = _engine.AssembleBuild(context.Background())
err = _engine.AssembleBuild(context.Background(), streamRequests)

if test.failure {
if err == nil {
Expand Down Expand Up @@ -355,6 +359,9 @@ func TestLinux_ExecBuild(t *testing.T) {
t.Errorf("unable to create runtime engine: %v", err)
}

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

tests := []struct {
failure bool
pipeline string
Expand Down Expand Up @@ -449,7 +456,7 @@ func TestLinux_ExecBuild(t *testing.T) {
// go-vela/server has logic to set it to an expected state.
_engine.build.SetStatus("running")

err = _engine.ExecBuild(context.Background())
err = _engine.ExecBuild(context.Background(), streamRequests)

if test.failure {
if err == nil {
Expand Down
6 changes: 5 additions & 1 deletion executor/linux/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/go-vela/server/compiler/native"
"github.com/go-vela/server/mock/server"

"github.com/go-vela/worker/executor"
"github.com/go-vela/worker/runtime/docker"

"github.com/go-vela/sdk-go/vela"
Expand Down Expand Up @@ -253,6 +254,9 @@ func TestLinux_Secret_exec(t *testing.T) {
t.Errorf("unable to create runtime engine: %v", err)
}

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

// setup tests
tests := []struct {
failure bool
Expand Down Expand Up @@ -296,7 +300,7 @@ func TestLinux_Secret_exec(t *testing.T) {
// add init container info to client
_ = _engine.CreateBuild(context.Background())

err = _engine.secret.exec(context.Background(), &p.Secrets)
err = _engine.secret.exec(context.Background(), &p.Secrets, streamRequests)

if test.failure {
if err == nil {
Expand Down
6 changes: 5 additions & 1 deletion executor/linux/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/go-vela/server/mock/server"

"github.com/go-vela/worker/executor"
"github.com/go-vela/worker/runtime/docker"

"github.com/go-vela/sdk-go/vela"
Expand Down Expand Up @@ -219,6 +220,9 @@ func TestLinux_ExecService(t *testing.T) {
t.Errorf("unable to create runtime engine: %v", err)
}

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

// setup tests
tests := []struct {
failure bool
Expand Down Expand Up @@ -277,7 +281,7 @@ func TestLinux_ExecService(t *testing.T) {
_engine.serviceLogs.Store(test.container.ID, new(library.Log))
}

err = _engine.ExecService(context.Background(), test.container)
err = _engine.ExecService(context.Background(), test.container, streamRequests)

if test.failure {
if err == nil {
Expand Down
6 changes: 5 additions & 1 deletion executor/linux/stage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/go-vela/server/compiler/native"
"github.com/go-vela/server/mock/server"

"github.com/go-vela/worker/executor"
"github.com/go-vela/worker/runtime/docker"

"github.com/go-vela/sdk-go/vela"
Expand Down Expand Up @@ -290,6 +291,9 @@ func TestLinux_ExecStage(t *testing.T) {
t.Errorf("unable to create runtime engine: %v", err)
}

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

// setup tests
tests := []struct {
failure bool
Expand Down Expand Up @@ -365,7 +369,7 @@ func TestLinux_ExecStage(t *testing.T) {
t.Errorf("unable to create executor engine: %v", err)
}

err = _engine.ExecStage(context.Background(), test.stage, stageMap)
err = _engine.ExecStage(context.Background(), test.stage, stageMap, streamRequests)

if test.failure {
if err == nil {
Expand Down
6 changes: 5 additions & 1 deletion executor/linux/step_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/go-vela/server/mock/server"

"github.com/go-vela/worker/executor"
"github.com/go-vela/worker/runtime/docker"

"github.com/go-vela/sdk-go/vela"
Expand Down Expand Up @@ -225,6 +226,9 @@ func TestLinux_ExecStep(t *testing.T) {
t.Errorf("unable to create runtime engine: %v", err)
}

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

// setup tests
tests := []struct {
failure bool
Expand Down Expand Up @@ -304,7 +308,7 @@ func TestLinux_ExecStep(t *testing.T) {
_engine.stepLogs.Store(test.container.ID, new(library.Log))
}

err = _engine.ExecStep(context.Background(), test.container)
err = _engine.ExecStep(context.Background(), test.container, streamRequests)

if test.failure {
if err == nil {
Expand Down
11 changes: 9 additions & 2 deletions executor/local/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/go-vela/server/compiler/native"
"github.com/urfave/cli/v2"

"github.com/go-vela/worker/executor"
"github.com/go-vela/worker/runtime/docker"
)

Expand Down Expand Up @@ -174,6 +175,9 @@ func TestLocal_AssembleBuild(t *testing.T) {
t.Errorf("unable to create runtime engine: %v", err)
}

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

tests := []struct {
failure bool
pipeline string
Expand Down Expand Up @@ -245,7 +249,7 @@ func TestLocal_AssembleBuild(t *testing.T) {
t.Errorf("unable to create build: %v", err)
}

err = _engine.AssembleBuild(context.Background())
err = _engine.AssembleBuild(context.Background(), streamRequests)

if test.failure {
if err == nil {
Expand Down Expand Up @@ -274,6 +278,9 @@ func TestLocal_ExecBuild(t *testing.T) {
t.Errorf("unable to create runtime engine: %v", err)
}

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

tests := []struct {
failure bool
pipeline string
Expand Down Expand Up @@ -333,7 +340,7 @@ func TestLocal_ExecBuild(t *testing.T) {
t.Errorf("unable to create build: %v", err)
}

err = _engine.ExecBuild(context.Background())
err = _engine.ExecBuild(context.Background(), streamRequests)

if test.failure {
if err == nil {
Expand Down
6 changes: 5 additions & 1 deletion executor/local/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"testing"

"github.com/go-vela/worker/executor"
"github.com/go-vela/worker/runtime/docker"

"github.com/go-vela/types/library"
Expand Down Expand Up @@ -169,6 +170,9 @@ func TestLocal_ExecService(t *testing.T) {
t.Errorf("unable to create runtime engine: %v", err)
}

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

// setup tests
tests := []struct {
failure bool
Expand Down Expand Up @@ -225,7 +229,7 @@ func TestLocal_ExecService(t *testing.T) {
_engine.services.Store(test.container.ID, new(library.Service))
}

err = _engine.ExecService(context.Background(), test.container)
err = _engine.ExecService(context.Background(), test.container, streamRequests)

if test.failure {
if err == nil {
Expand Down
6 changes: 5 additions & 1 deletion executor/local/stage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/go-vela/server/compiler/native"

"github.com/go-vela/worker/executor"
"github.com/go-vela/worker/runtime/docker"

"github.com/go-vela/types/pipeline"
Expand Down Expand Up @@ -249,6 +250,9 @@ func TestLocal_ExecStage(t *testing.T) {
t.Errorf("unable to create runtime engine: %v", err)
}

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

// setup tests
tests := []struct {
failure bool
Expand Down Expand Up @@ -306,7 +310,7 @@ func TestLocal_ExecStage(t *testing.T) {
t.Errorf("unable to create executor engine: %v", err)
}

err = _engine.ExecStage(context.Background(), test.stage, stageMap)
err = _engine.ExecStage(context.Background(), test.stage, stageMap, streamRequests)

if test.failure {
if err == nil {
Expand Down
6 changes: 5 additions & 1 deletion executor/local/step_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"testing"

"github.com/go-vela/worker/executor"
"github.com/go-vela/worker/runtime/docker"

"github.com/go-vela/types/library"
Expand Down Expand Up @@ -175,6 +176,9 @@ func TestLocal_ExecStep(t *testing.T) {
t.Errorf("unable to create runtime engine: %v", err)
}

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

// setup tests
tests := []struct {
failure bool
Expand Down Expand Up @@ -252,7 +256,7 @@ func TestLocal_ExecStep(t *testing.T) {
_engine.steps.Store(test.container.ID, new(library.Step))
}

err = _engine.ExecStep(context.Background(), test.container)
err = _engine.ExecStep(context.Background(), test.container, streamRequests)

if test.failure {
if err == nil {
Expand Down

0 comments on commit 5b5e902

Please sign in to comment.