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

Add dummy backend #3820

Merged
merged 38 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
6ddcf67
init mock backend
6543 Jun 22, 2024
1f21033
extend
6543 Jun 22, 2024
353ab68
emulate services in mock backend
6543 Jun 22, 2024
f2c38fc
check against dubble start of steps
6543 Jun 22, 2024
2b7ee22
test the mock more
6543 Jun 22, 2024
5e24efa
docu mock backend
6543 Jun 22, 2024
3e02a08
call it dummy
6543 Jun 22, 2024
6972c1c
nit
6543 Jun 22, 2024
b5351fb
better dummy log output
6543 Jun 22, 2024
3373780
prettier
6543 Jun 22, 2024
e09de76
exclude it in release binary
6543 Jun 23, 2024
66cb79e
Merge branch 'main' into pipeline-backend-mock
6543 Jun 23, 2024
455ca10
pass tags flag on compile
6543 Jun 23, 2024
0f5d2f1
jup
6543 Jun 24, 2024
d0ccd5a
Merge branch 'main' into pipeline-backend-mock
6543 Jun 24, 2024
f71109d
also dont compie mockerly generated into release
6543 Jun 24, 2024
7efddcb
all mockerly only in test
6543 Jun 24, 2024
a9cd876
fix lint
6543 Jun 24, 2024
bc821eb
Merge branch 'main' into pipeline-backend-mock
6543 Jun 24, 2024
424a803
Merge branch 'main' into pipeline-backend-mock
6543 Jun 25, 2024
d67ae93
Update docs/docs/92-development/09-testing.md
6543 Jun 25, 2024
82032a3
docs++
6543 Jun 30, 2024
015a15c
Merge branch 'main' into pipeline-backend-mock
6543 Jun 30, 2024
1a6f2a7
Update pipeline/backend/dummy/dummy.go
6543 Jun 30, 2024
7808ce1
Update pipeline/backend/dummy/dummy.go
6543 Jun 30, 2024
b981870
workflow setup error dummy
6543 Jun 30, 2024
9e6a707
Apply suggestions from code review
anbraten Jun 30, 2024
35a9f43
reuse "test" tag
6543 Jun 30, 2024
d36168a
vscode alow go language server to inspect test tagged files too
6543 Jun 30, 2024
f3a43db
use environment vars if possible to control dummy engine
6543 Jun 30, 2024
a6e4791
extend
6543 Jun 30, 2024
d98022d
extend
6543 Jun 30, 2024
77ebdaa
Merge branch 'main' into pipeline-backend-mock
6543 Jun 30, 2024
56b8264
add prfided example test usage
6543 Jun 30, 2024
6e9c57b
lint
6543 Jun 30, 2024
24acd3f
clean
6543 Jun 30, 2024
670a0a2
Apply suggestions from code review
anbraten Jul 1, 2024
98ae630
Update .vscode/settings.json
anbraten Jul 1, 2024
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
2 changes: 2 additions & 0 deletions cli/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/docker"
"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/kubernetes"
"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/local"
"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/mock"
backendTypes "go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/types"
"go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/yaml"
"go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/yaml/compiler"
Expand Down Expand Up @@ -228,6 +229,7 @@ func execWithAxis(c *cli.Context, file, repoPath string, axis matrix.Axis) error
kubernetes.New(),
docker.New(),
local.New(),
mock.New(),
}
backendEngine, err := backend.FindBackend(backendCtx, backends, c.String("backend-engine"))
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions cmd/agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/docker"
"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/kubernetes"
"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/local"
"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/mock"
backendTypes "go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/types"
)

Expand All @@ -27,5 +28,6 @@ func main() {
kubernetes.New(),
docker.New(),
local.New(),
mock.New(),
})
}
218 changes: 218 additions & 0 deletions pipeline/backend/mock/mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
// Copyright 2024 Woodpecker Authors
6543 marked this conversation as resolved.
Show resolved Hide resolved
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package mock

import (
"context"
"fmt"
"io"
"strings"
"sync"
"time"

"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"

backend "go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/types"
)

type mock struct {
kv sync.Map
}

const (
// Step names to control mock behavior.
StepStartFail = "step_start_fail"
StepExecError = "step_exec_error"
EnvKeyStepSleep = "SLEEP"
EnvKeyStepType = "EXPECT_TYPE"

// Internal const.
stepStateStarted = "started"
stepStateDone = "done"
testServiceTimeout = 1 * time.Second
)

// New returns a new Docker Backend.
func New() backend.Backend {
return &mock{
kv: sync.Map{},
}
}

func (e *mock) Name() string {
return "mock"
}

func (e *mock) IsAvailable(_ context.Context) bool {
return true
}

func (e *mock) Flags() []cli.Flag {
return nil
}

// Load new client for Docker Backend using environment variables.
func (e *mock) Load(_ context.Context) (*backend.BackendInfo, error) {
return &backend.BackendInfo{
Platform: "mock",
}, nil
}

func (e *mock) SetupWorkflow(_ context.Context, _ *backend.Config, taskUUID string) error {
log.Trace().Str("taskUUID", taskUUID).Msg("create workflow environment")
e.kv.Store("task_"+taskUUID, "setup")
return nil
}

func (e *mock) StartStep(_ context.Context, step *backend.Step, taskUUID string) error {
log.Trace().Str("taskUUID", taskUUID).Msgf("start step %s", step.Name)

// internal state checks
_, exist := e.kv.Load("task_" + taskUUID)
if !exist {
return fmt.Errorf("expect env of workflow %s to exist but found none to destroy", taskUUID)
}
stepState, stepExist := e.kv.Load(fmt.Sprintf("task_%s_step_%s", taskUUID, step.UUID))
if stepExist {
// Detect issues like https://github.com/woodpecker-ci/woodpecker/issues/3494
return fmt.Errorf("StartStep detect already started step '%s' (%s) in state: %s", step.Name, step.UUID, stepState)
}

if step.Name == StepStartFail {
return fmt.Errorf("expected fail to start step")
}

expectStepType, testStepType := step.Environment[EnvKeyStepType]
if testStepType && string(step.Type) != expectStepType {
return fmt.Errorf("expected step type '%s' but got '%s'", expectStepType, step.Type)
}

e.kv.Store(fmt.Sprintf("task_%s_step_%s", taskUUID, step.UUID), stepStateStarted)
return nil
}

func (e *mock) WaitStep(ctx context.Context, step *backend.Step, taskUUID string) (*backend.State, error) {
log.Trace().Str("taskUUID", taskUUID).Msgf("wait for step %s", step.Name)

_, exist := e.kv.Load("task_" + taskUUID)
if !exist {
err := fmt.Errorf("expect env of workflow %s to exist but found none to destroy", taskUUID)
return &backend.State{Error: err}, err
}

// check state
stepState, stepExist := e.kv.Load(fmt.Sprintf("task_%s_step_%s", taskUUID, step.UUID))
if !stepExist {
err := fmt.Errorf("WaitStep expect step '%s' (%s) to be created but found none", step.Name, step.UUID)
return &backend.State{Error: err}, err
}
if stepState != stepStateStarted {
err := fmt.Errorf("WaitStep expect step '%s' (%s) to be '%s' but it is: %s", step.Name, step.UUID, stepStateStarted, stepState)
return &backend.State{Error: err}, err
}

// extend wait time logic
if sleep, sleepExist := step.Environment[EnvKeyStepSleep]; sleepExist {
toSleep, err := time.ParseDuration(sleep)
if err != nil {
err = fmt.Errorf("WaitStep fail to parse sleep duration: %w", err)
return &backend.State{Error: err}, err
}
time.Sleep(toSleep)
} else {
if step.Type == backend.StepTypeService {
select {
case <-time.NewTimer(testServiceTimeout).C:
err := fmt.Errorf("WaitStep fail due to timeout of service after 1 second")
return &backend.State{Error: err}, err
case <-ctx.Done():
// context for service closed ... we can move forward
}
} else {
time.Sleep(time.Nanosecond)
}
}

e.kv.Store(fmt.Sprintf("task_%s_step_%s", taskUUID, step.UUID), stepStateDone)

if step.Name == StepExecError {
return &backend.State{
ExitCode: 1,
Exited: true,
OOMKilled: false,
}, nil
}

return &backend.State{
ExitCode: 0,
Exited: true,
OOMKilled: false,
}, nil
}

func (e *mock) TailStep(_ context.Context, step *backend.Step, taskUUID string) (io.ReadCloser, error) {
log.Trace().Str("taskUUID", taskUUID).Msgf("tail logs of step %s", step.Name)

_, exist := e.kv.Load("task_" + taskUUID)
if !exist {
return nil, fmt.Errorf("expect env of workflow %s to exist but found none to destroy", taskUUID)
}

// check state
stepState, stepExist := e.kv.Load(fmt.Sprintf("task_%s_step_%s", taskUUID, step.UUID))
if !stepExist {
return nil, fmt.Errorf("WaitStep expect step '%s' (%s) to be created but found none", step.Name, step.UUID)
}
if stepState != stepStateStarted {
return nil, fmt.Errorf("WaitStep expect step '%s' (%s) to be '%s' but it is: %s", step.Name, step.UUID, stepStateStarted, stepState)
}

return io.NopCloser(strings.NewReader(
fmt.Sprintf("StepName: %s\nStepType: %s\nStepUUID: %sStepCommands:\n\n%s\n", step.Name, step.Type, step.UUID, strings.Join(step.Commands, "\n")),
)), nil
}

func (e *mock) DestroyStep(_ context.Context, step *backend.Step, taskUUID string) error {
log.Trace().Str("taskUUID", taskUUID).Msgf("stop step %s", step.Name)

_, exist := e.kv.Load("task_" + taskUUID)
if !exist {
return fmt.Errorf("expect env of workflow %s to exist but found none to destroy", taskUUID)
}

// check state
stepState, stepExist := e.kv.Load(fmt.Sprintf("task_%s_step_%s", taskUUID, step.UUID))
if !stepExist {
return fmt.Errorf("WaitStep expect step '%s' (%s) to be created but found none", step.Name, step.UUID)
}
if stepState != stepStateDone {
return fmt.Errorf("WaitStep expect step '%s' (%s) to be '%s' but it is: %s", step.Name, step.UUID, stepStateDone, stepState)
}

e.kv.Delete(fmt.Sprintf("task_%s_step_%s", taskUUID, step.UUID))
return nil
}

func (e *mock) DestroyWorkflow(_ context.Context, _ *backend.Config, taskUUID string) error {
log.Trace().Str("taskUUID", taskUUID).Msgf("delete workflow environment")

_, exist := e.kv.Load("task_" + taskUUID)
if !exist {
return fmt.Errorf("expect env of workflow %s to exist but found none to destroy", taskUUID)
}
e.kv.Delete("task_" + taskUUID)
return nil
}
134 changes: 134 additions & 0 deletions pipeline/backend/mock/mock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// Copyright 2024 Woodpecker Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package mock_test

import (
"context"
"io"
"testing"

"github.com/stretchr/testify/assert"

"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/mock"
"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/types"
)

func TestSmalPipelineMockRun(t *testing.T) {
mockEngine := mock.New()
ctx := context.Background()

assert.True(t, mockEngine.IsAvailable(ctx))
assert.EqualValues(t, "mock", mockEngine.Name())
_, err := mockEngine.Load(ctx)
assert.NoError(t, err)

t.Run("expect fail of step func with non setup workflow", func(t *testing.T) {
step := &types.Step{Name: "step1", UUID: "SID_1"}
nonExistWorkflowID := "WID_NONE"

err := mockEngine.StartStep(ctx, step, nonExistWorkflowID)
assert.Error(t, err)

_, err = mockEngine.TailStep(ctx, step, nonExistWorkflowID)
assert.Error(t, err)

_, err = mockEngine.WaitStep(ctx, step, nonExistWorkflowID)
assert.Error(t, err)

err = mockEngine.DestroyStep(ctx, step, nonExistWorkflowID)
assert.Error(t, err)
})

t.Run("step exec successfully", func(t *testing.T) {
step := &types.Step{
Name: "step1",
UUID: "SID_1",
Environment: map[string]string{},
Commands: []string{"echo ja", "echo nein"},
}
workflowUUID := "WID_1"

assert.NoError(t, mockEngine.SetupWorkflow(ctx, nil, workflowUUID))

assert.NoError(t, mockEngine.StartStep(ctx, step, workflowUUID))

reader, err := mockEngine.TailStep(ctx, step, workflowUUID)
assert.NoError(t, err)
log, err := io.ReadAll(reader)
assert.NoError(t, err)
assert.EqualValues(t, "StepName: step1\nStepType: \nStepUUID: SID_1StepCommands:\n\necho ja\necho nein\n", string(log))

state, err := mockEngine.WaitStep(ctx, step, workflowUUID)
assert.NoError(t, err)
assert.NoError(t, state.Error)
assert.EqualValues(t, 0, state.ExitCode)

assert.NoError(t, mockEngine.DestroyStep(ctx, step, workflowUUID))

assert.NoError(t, mockEngine.DestroyWorkflow(ctx, nil, workflowUUID))
})

t.Run("step exec error", func(t *testing.T) {
step := &types.Step{
Name: mock.StepExecError,
UUID: "SID_2",
Type: types.StepTypePlugin,
Environment: map[string]string{mock.EnvKeyStepType: "plugin"},
}
workflowUUID := "WID_1"

assert.NoError(t, mockEngine.SetupWorkflow(ctx, nil, workflowUUID))

assert.NoError(t, mockEngine.StartStep(ctx, step, workflowUUID))

_, err := mockEngine.TailStep(ctx, step, workflowUUID)
assert.NoError(t, err)

state, err := mockEngine.WaitStep(ctx, step, workflowUUID)
assert.NoError(t, err)
assert.NoError(t, state.Error)
assert.EqualValues(t, 1, state.ExitCode)

assert.NoError(t, mockEngine.DestroyStep(ctx, step, workflowUUID))

assert.NoError(t, mockEngine.DestroyWorkflow(ctx, nil, workflowUUID))
})

t.Run("step start fail", func(t *testing.T) {
step := &types.Step{
Name: mock.StepStartFail,
UUID: "SID_2",
Type: types.StepTypeService,
Environment: map[string]string{mock.EnvKeyStepType: "service"},
}
workflowUUID := "WID_1"

assert.NoError(t, mockEngine.SetupWorkflow(ctx, nil, workflowUUID))

assert.Error(t, mockEngine.StartStep(ctx, step, workflowUUID))

_, err := mockEngine.TailStep(ctx, step, workflowUUID)
assert.Error(t, err)

state, err := mockEngine.WaitStep(ctx, step, workflowUUID)
assert.Error(t, err)
assert.Error(t, state.Error)
assert.EqualValues(t, 0, state.ExitCode)

assert.Error(t, mockEngine.DestroyStep(ctx, step, workflowUUID))

assert.NoError(t, mockEngine.DestroyWorkflow(ctx, nil, workflowUUID))
})
}