Skip to content

Commit

Permalink
Refactor implement step runner (#48)
Browse files Browse the repository at this point in the history
Implement StepRunner
Implement simple unit test for step runner
Increase successTimeout for integration unit tests
  • Loading branch information
rihter007 authored Dec 15, 2021
1 parent 750bbda commit 0f755b4
Show file tree
Hide file tree
Showing 8 changed files with 653 additions and 282 deletions.
18 changes: 18 additions & 0 deletions pkg/cerrors/cerrors.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ import (
"strings"
)

// ErrAlreadyDone indicates that action already happened
type ErrAlreadyDone struct {
}

func (e *ErrAlreadyDone) Error() string {
return "already done"
}

// ErrTestStepsNeverReturned indicates that one or multiple TestSteps
// did not complete when the test terminated or when the pipeline
// received a cancellation or pause signal
Expand Down Expand Up @@ -44,6 +52,16 @@ func (e *ErrTestStepPaniced) Error() string {
return fmt.Sprintf("test step %s paniced, trace: %q", e.StepName, e.StackTrace)
}

// ErrTestStepReturnedNoTarget indicates that a test step returned nil Target
type ErrTestStepReturnedNoTarget struct {
StepName string
}

// Error returns the error string associated with the error
func (e *ErrTestStepReturnedNoTarget) Error() string {
return fmt.Sprintf("test step %s returned nil result", e.StepName)
}

// ErrTestStepReturnedDuplicateResult indicates that a test step returned result
// twice for the same target.
type ErrTestStepReturnedDuplicateResult struct {
Expand Down
61 changes: 61 additions & 0 deletions pkg/runner/base_test_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package runner

import (
"encoding/json"

"github.com/benbjohnson/clock"
"github.com/linuxboot/contest/pkg/event/testevent"
"github.com/linuxboot/contest/pkg/pluginregistry"
"github.com/linuxboot/contest/pkg/target"
"github.com/linuxboot/contest/pkg/test"
"github.com/linuxboot/contest/pkg/xcontext"
"github.com/linuxboot/contest/plugins/targetlocker/inmemory"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)

type BaseTestSuite struct {
suite.Suite

pluginRegistry *pluginregistry.PluginRegistry
internalStorage *MemoryStorageEngine
}

func (s *BaseTestSuite) SetupTest() {
storageEngine, err := NewMemoryStorageEngine()
require.NoError(s.T(), err)
s.internalStorage = storageEngine

target.SetLocker(inmemory.New(clock.New()))

s.pluginRegistry = pluginregistry.NewPluginRegistry(xcontext.Background())
}

func (s *BaseTestSuite) TearDownTest() {
target.SetLocker(nil)
}

func (s *BaseTestSuite) RegisterStateFullStep(
runFunction func(
ctx xcontext.Context, ch test.TestStepChannels, params test.TestStepParameters,
ev testevent.Emitter, resumeState json.RawMessage) (json.RawMessage, error),
validateFunction func(ctx xcontext.Context, params test.TestStepParameters) error) error {

return s.pluginRegistry.RegisterTestStep(stateFullStepName, func() test.TestStep {
return &stateFullStep{
runFunction: runFunction,
validateFunction: validateFunction,
}
}, nil)
}

func (s *BaseTestSuite) NewStep(label, name string, params test.TestStepParameters) test.TestStepBundle {
td := test.TestStepDescriptor{
Name: name,
Label: label,
Parameters: params,
}
sb, err := s.pluginRegistry.NewTestStepBundle(ctx, td)
require.NoError(s.T(), err)
return *sb
}
57 changes: 9 additions & 48 deletions pkg/runner/job_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ import (
"github.com/linuxboot/contest/pkg/event"
"github.com/linuxboot/contest/pkg/event/testevent"
"github.com/linuxboot/contest/pkg/job"
"github.com/linuxboot/contest/pkg/pluginregistry"
"github.com/linuxboot/contest/pkg/storage"
"github.com/linuxboot/contest/pkg/target"
"github.com/linuxboot/contest/pkg/test"
"github.com/linuxboot/contest/pkg/xcontext"
"github.com/linuxboot/contest/plugins/targetlocker/inmemory"
"github.com/linuxboot/contest/plugins/targetmanagers/targetlist"
"github.com/linuxboot/contest/plugins/teststeps"
"github.com/linuxboot/contest/plugins/teststeps/echo"
Expand Down Expand Up @@ -84,24 +82,16 @@ func (r *collectingReporter) FinalReport(ctx xcontext.Context, parameters interf
}

type JobRunnerSuite struct {
suite.Suite

pluginRegistry *pluginregistry.PluginRegistry
internalStorage *MemoryStorageEngine
BaseTestSuite
}

func TestTestStepSuite(t *testing.T) {
suite.Run(t, &JobRunnerSuite{})
}

func (s *JobRunnerSuite) SetupTest() {
storageEngine, err := NewMemoryStorageEngine()
require.NoError(s.T(), err)
s.internalStorage = storageEngine
s.BaseTestSuite.SetupTest()

target.SetLocker(inmemory.New(clock.New()))

s.pluginRegistry = pluginregistry.NewPluginRegistry(xcontext.Background())
for _, e := range []struct {
name string
factory test.TestStepFactory
Expand All @@ -113,40 +103,11 @@ func (s *JobRunnerSuite) SetupTest() {
}
}

func (s *JobRunnerSuite) TearDownTest() {
target.SetLocker(nil)
}

func (s *JobRunnerSuite) registerStateFullStep(
runFunction func(
ctx xcontext.Context, ch test.TestStepChannels, params test.TestStepParameters,
ev testevent.Emitter, resumeState json.RawMessage) (json.RawMessage, error),
validateFunction func(ctx xcontext.Context, params test.TestStepParameters) error) error {

return s.pluginRegistry.RegisterTestStep(stateFullStepName, func() test.TestStep {
return &stateFullStep{
runFunction: runFunction,
validateFunction: validateFunction,
}
}, nil)
}

func (s *JobRunnerSuite) newStep(label, name string, params test.TestStepParameters) test.TestStepBundle {
td := test.TestStepDescriptor{
Name: name,
Label: label,
Parameters: params,
}
sb, err := s.pluginRegistry.NewTestStepBundle(ctx, td)
require.NoError(s.T(), err)
return *sb
}

func (s *JobRunnerSuite) TestSimpleJobStartFinish() {
var mu sync.Mutex
var resultTargets []*target.Target

require.NoError(s.T(), s.registerStateFullStep(
require.NoError(s.T(), s.RegisterStateFullStep(
func(ctx xcontext.Context, ch test.TestStepChannels, params test.TestStepParameters, ev testevent.Emitter, resumeState json.RawMessage) (json.RawMessage, error) {
return teststeps.ForEachTarget(stateFullStepName, ctx, ch, func(ctx xcontext.Context, target *target.Target) error {
assert.NotNil(s.T(), target)
Expand Down Expand Up @@ -180,7 +141,7 @@ func (s *JobRunnerSuite) TestSimpleJobStartFinish() {
TargetManager: targetlist.New(),
},
TestStepsBundles: []test.TestStepBundle{
s.newStep("test_step_label", stateFullStepName, nil),
s.NewStep("test_step_label", stateFullStepName, nil),
},
},
},
Expand Down Expand Up @@ -209,7 +170,7 @@ func (s *JobRunnerSuite) TestJobWithTestRetry() {
var resultTargets []*target.Target
var callsCount int

require.NoError(s.T(), s.registerStateFullStep(
require.NoError(s.T(), s.RegisterStateFullStep(
func(ctx xcontext.Context, ch test.TestStepChannels, params test.TestStepParameters, ev testevent.Emitter, resumeState json.RawMessage) (json.RawMessage, error) {
return teststeps.ForEachTarget(stateFullStepName, ctx, ch, func(ctx xcontext.Context, target *target.Target) error {
assert.NotNil(s.T(), target)
Expand Down Expand Up @@ -259,11 +220,11 @@ func (s *JobRunnerSuite) TestJobWithTestRetry() {
TargetManager: targetlist.New(),
},
TestStepsBundles: []test.TestStepBundle{
s.newStep("echo1_step_label", echo.Name, map[string][]test.Param{
s.NewStep("echo1_step_label", echo.Name, map[string][]test.Param{
"text": {*test.NewParam("hello")},
}),
s.newStep("test_step_label", stateFullStepName, nil),
s.newStep("echo2_step_label", echo.Name, map[string][]test.Param{
s.NewStep("test_step_label", stateFullStepName, nil),
s.NewStep("echo2_step_label", echo.Name, map[string][]test.Param{
"text": {*test.NewParam("world")},
}),
},
Expand Down Expand Up @@ -342,7 +303,7 @@ func (s *JobRunnerSuite) TestResumeStateBadJobId() {
TargetManager: targetlist.New(),
},
TestStepsBundles: []test.TestStepBundle{
s.newStep("echo1_step_label", echo.Name, map[string][]test.Param{
s.NewStep("echo1_step_label", echo.Name, map[string][]test.Param{
"text": {*test.NewParam("hello")},
}),
},
Expand Down
Loading

0 comments on commit 0f755b4

Please sign in to comment.