From d3c90a0d0ffc3f8a23f580378ab446f9fda3f072 Mon Sep 17 00:00:00 2001 From: Ilya Date: Wed, 15 Dec 2021 19:43:02 +0000 Subject: [PATCH] Improve BaseTestSuite: move global variable ctx into suite Use BaseTestSuite in TestRunner tests --- pkg/runner/base_test_suite_test.go | 58 ++++++++-- pkg/runner/job_runner_test.go | 24 ++-- pkg/runner/step_runner_test.go | 10 +- pkg/runner/test_runner_test.go | 178 ++++++++++------------------- 4 files changed, 127 insertions(+), 143 deletions(-) diff --git a/pkg/runner/base_test_suite_test.go b/pkg/runner/base_test_suite_test.go index b0569b6f..8b510a00 100644 --- a/pkg/runner/base_test_suite_test.go +++ b/pkg/runner/base_test_suite_test.go @@ -1,38 +1,82 @@ package runner import ( + "context" "encoding/json" "github.com/benbjohnson/clock" "github.com/linuxboot/contest/pkg/event/testevent" "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/pkg/xcontext/bundles/logrusctx" + "github.com/linuxboot/contest/pkg/xcontext/logger" + "github.com/linuxboot/contest/plugins/storage/memory" "github.com/linuxboot/contest/plugins/targetlocker/inmemory" + "github.com/linuxboot/contest/tests/common" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" ) +type MemoryStorageEngine struct { + Ctx xcontext.Context + Storage storage.ResettableStorage + StorageEngineVault *storage.SimpleEngineVault +} + +func NewMemoryStorageEngine(ctx xcontext.Context) (*MemoryStorageEngine, error) { + ms, err := memory.New() + if err != nil { + return nil, err + } + + storageEngineVault := storage.NewSimpleEngineVault() + if err := storageEngineVault.StoreEngine(ms, storage.SyncEngine); err != nil { + return nil, err + } + + return &MemoryStorageEngine{ + Ctx: ctx, + Storage: ms, + StorageEngineVault: storageEngineVault, + }, nil +} + +func (mse *MemoryStorageEngine) GetStepEvents(testName string, stepLabel string) string { + return common.GetTestEventsAsString(mse.Ctx, mse.Storage, testName, nil, &stepLabel) +} + +func (mse *MemoryStorageEngine) GetTargetEvents(testName string, targetID string) string { + return common.GetTestEventsAsString(mse.Ctx, mse.Storage, testName, &targetID, nil) +} + type BaseTestSuite struct { suite.Suite - pluginRegistry *pluginregistry.PluginRegistry - internalStorage *MemoryStorageEngine + Ctx xcontext.Context + Cancel context.CancelFunc + + PluginRegistry *pluginregistry.PluginRegistry + MemoryStorage *MemoryStorageEngine } func (s *BaseTestSuite) SetupTest() { - storageEngine, err := NewMemoryStorageEngine() + s.Ctx, s.Cancel = xcontext.WithCancel(logrusctx.NewContext(logger.LevelDebug)) + + storageEngine, err := NewMemoryStorageEngine(s.Ctx) require.NoError(s.T(), err) - s.internalStorage = storageEngine + s.MemoryStorage = storageEngine target.SetLocker(inmemory.New(clock.New())) - s.pluginRegistry = pluginregistry.NewPluginRegistry(xcontext.Background()) + s.PluginRegistry = pluginregistry.NewPluginRegistry(s.Ctx) } func (s *BaseTestSuite) TearDownTest() { target.SetLocker(nil) + s.Cancel() } func (s *BaseTestSuite) RegisterStateFullStep( @@ -41,7 +85,7 @@ func (s *BaseTestSuite) RegisterStateFullStep( 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 s.PluginRegistry.RegisterTestStep(stateFullStepName, func() test.TestStep { return &stateFullStep{ runFunction: runFunction, validateFunction: validateFunction, @@ -55,7 +99,7 @@ func (s *BaseTestSuite) NewStep(label, name string, params test.TestStepParamete Label: label, Parameters: params, } - sb, err := s.pluginRegistry.NewTestStepBundle(ctx, td) + sb, err := s.PluginRegistry.NewTestStepBundle(s.Ctx, td) require.NoError(s.T(), err) return *sb } diff --git a/pkg/runner/job_runner_test.go b/pkg/runner/job_runner_test.go index 15ecbfac..eef81fc4 100644 --- a/pkg/runner/job_runner_test.go +++ b/pkg/runner/job_runner_test.go @@ -99,7 +99,7 @@ func (s *JobRunnerSuite) SetupTest() { }{ {echo.Name, echo.New, echo.Events}, } { - require.NoError(s.T(), s.pluginRegistry.RegisterTestStep(e.name, e.factory, e.events)) + require.NoError(s.T(), s.PluginRegistry.RegisterTestStep(e.name, e.factory, e.events)) } } @@ -147,11 +147,11 @@ func (s *JobRunnerSuite) TestSimpleJobStartFinish() { }, } - jsm := storage.NewJobStorageManager(s.internalStorage.StorageEngineVault) - jr := NewJobRunner(jsm, s.internalStorage.StorageEngineVault, clock.New(), time.Second) + jsm := storage.NewJobStorageManager(s.MemoryStorage.StorageEngineVault) + jr := NewJobRunner(jsm, s.MemoryStorage.StorageEngineVault, clock.New(), time.Second) require.NotNil(s.T(), jr) - resumeState, err := jr.Run(ctx, &j, nil) + resumeState, err := jr.Run(s.Ctx, &j, nil) require.NoError(s.T(), err) require.Nil(s.T(), resumeState) @@ -162,7 +162,7 @@ func (s *JobRunnerSuite) TestSimpleJobStartFinish() { {[1 1 SimpleTest 0 test_step_label][Target{ID: "T1"} TargetIn]} {[1 1 SimpleTest 0 test_step_label][Target{ID: "T1"} TargetOut]} {[1 1 SimpleTest 0 ][Target{ID: "T1"} TargetReleased]} -`, s.internalStorage.GetTargetEvents(testName, "T1")) +`, s.MemoryStorage.GetTargetEvents(testName, "T1")) } func (s *JobRunnerSuite) TestJobWithTestRetry() { @@ -232,11 +232,11 @@ func (s *JobRunnerSuite) TestJobWithTestRetry() { }, } - jsm := storage.NewJobStorageManager(s.internalStorage.StorageEngineVault) - jr := NewJobRunner(jsm, s.internalStorage.StorageEngineVault, clock.New(), time.Second) + jsm := storage.NewJobStorageManager(s.MemoryStorage.StorageEngineVault) + jr := NewJobRunner(jsm, s.MemoryStorage.StorageEngineVault, clock.New(), time.Second) require.NotNil(s.T(), jr) - resumeState, err := jr.Run(ctx, &j, nil) + resumeState, err := jr.Run(s.Ctx, &j, nil) require.NoError(s.T(), err) require.Nil(s.T(), resumeState) @@ -255,7 +255,7 @@ func (s *JobRunnerSuite) TestJobWithTestRetry() { {[1 1 SimpleTest 1 echo2_step_label][Target{ID: "T1"} TargetIn]} {[1 1 SimpleTest 1 echo2_step_label][Target{ID: "T1"} TargetOut]} {[1 1 SimpleTest 1 ][Target{ID: "T1"} TargetReleased]} -`, s.internalStorage.GetTargetEvents(testName, "T1")) +`, s.MemoryStorage.GetTargetEvents(testName, "T1")) require.Len(s.T(), reporter.runStatuses, 1) require.Len(s.T(), reporter.runStatuses[0].TestStatuses, 1) @@ -311,8 +311,8 @@ func (s *JobRunnerSuite) TestResumeStateBadJobId() { }, } - jsm := storage.NewJobStorageManager(s.internalStorage.StorageEngineVault) - jr := NewJobRunner(jsm, s.internalStorage.StorageEngineVault, clock.New(), time.Second) + jsm := storage.NewJobStorageManager(s.MemoryStorage.StorageEngineVault) + jr := NewJobRunner(jsm, s.MemoryStorage.StorageEngineVault, clock.New(), time.Second) require.NotNil(s.T(), jr) inputResumeState := job.PauseEventPayload{ @@ -322,7 +322,7 @@ func (s *JobRunnerSuite) TestResumeStateBadJobId() { TestID: 1, } - resumeState, err := jr.Run(ctx, &j, &inputResumeState) + resumeState, err := jr.Run(s.Ctx, &j, &inputResumeState) require.Error(s.T(), err) require.Nil(s.T(), resumeState) } diff --git a/pkg/runner/step_runner_test.go b/pkg/runner/step_runner_test.go index 70a5e46b..bad0880f 100644 --- a/pkg/runner/step_runner_test.go +++ b/pkg/runner/step_runner_test.go @@ -56,21 +56,21 @@ func (s *StepRunnerSuite) TestRunningStep() { stepRunner := NewStepRunner() require.NotNil(s.T(), stepRunner) - emitterFactory := NewTestStepEventsEmitterFactory(s.internalStorage.StorageEngineVault, 1, 1, testName, 0) + emitterFactory := NewTestStepEventsEmitterFactory(s.MemoryStorage.StorageEngineVault, 1, 1, testName, 0) emitter := emitterFactory.New("test_step_label") inputResumeState := json.RawMessage("{\"some_input\": 42}") - resultChan, err := stepRunner.Run(ctx, s.NewStep("test_step_label", stateFullStepName, nil), emitter, inputResumeState) + resultChan, err := stepRunner.Run(s.Ctx, s.NewStep("test_step_label", stateFullStepName, nil), emitter, inputResumeState) require.NoError(s.T(), err) require.NotNil(s.T(), resultChan) - require.NoError(s.T(), stepRunner.AddTarget(ctx, tgt("TSucc"))) + require.NoError(s.T(), stepRunner.AddTarget(s.Ctx, tgt("TSucc"))) ev, ok := <-resultChan require.True(s.T(), ok) require.Equal(s.T(), tgt("TSucc"), ev.Target) require.NoError(s.T(), ev.Err) - require.NoError(s.T(), stepRunner.AddTarget(ctx, tgt("TFail"))) + require.NoError(s.T(), stepRunner.AddTarget(s.Ctx, tgt("TFail"))) ev, ok = <-resultChan require.True(s.T(), ok) require.Equal(s.T(), tgt("TFail"), ev.Target) @@ -86,7 +86,7 @@ func (s *StepRunnerSuite) TestRunningStep() { ev, ok = <-resultChan require.False(s.T(), ok) - closedCtx, cancel := xcontext.WithCancel(ctx) + closedCtx, cancel := xcontext.WithCancel(s.Ctx) cancel() // if step runner has results, it should return them even if input context is closed diff --git a/pkg/runner/test_runner_test.go b/pkg/runner/test_runner_test.go index 8f1962cc..9f82964d 100644 --- a/pkg/runner/test_runner_test.go +++ b/pkg/runner/test_runner_test.go @@ -18,15 +18,10 @@ import ( "github.com/linuxboot/contest/pkg/cerrors" "github.com/linuxboot/contest/pkg/event" - "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/types" "github.com/linuxboot/contest/pkg/xcontext" - "github.com/linuxboot/contest/pkg/xcontext/bundles/logrusctx" - "github.com/linuxboot/contest/pkg/xcontext/logger" - "github.com/linuxboot/contest/plugins/storage/memory" "github.com/linuxboot/contest/tests/common" "github.com/linuxboot/contest/tests/common/goroutine_leak_check" "github.com/linuxboot/contest/tests/plugins/teststeps/badtargets" @@ -42,10 +37,6 @@ const ( shutdownTimeout = 3 * time.Second ) -var ( - ctx = logrusctx.NewContext(logger.LevelDebug) -) - func TestMain(m *testing.M) { flag.Parse() goroutine_leak_check.LeakCheckingTestMain(m, @@ -72,41 +63,8 @@ type runRes struct { err error } -type MemoryStorageEngine struct { - Storage storage.ResettableStorage - StorageEngineVault *storage.SimpleEngineVault -} - -func NewMemoryStorageEngine() (*MemoryStorageEngine, error) { - ms, err := memory.New() - if err != nil { - return nil, err - } - - storageEngineVault := storage.NewSimpleEngineVault() - if err := storageEngineVault.StoreEngine(ms, storage.SyncEngine); err != nil { - return nil, err - } - - return &MemoryStorageEngine{ - Storage: ms, - StorageEngineVault: storageEngineVault, - }, nil -} - -func (mse *MemoryStorageEngine) GetStepEvents(testName string, stepLabel string) string { - return common.GetTestEventsAsString(ctx, mse.Storage, testName, nil, &stepLabel) -} - -func (mse *MemoryStorageEngine) GetTargetEvents(testName string, targetID string) string { - return common.GetTestEventsAsString(ctx, mse.Storage, testName, &targetID, nil) -} - type TestRunnerSuite struct { - suite.Suite - - pluginRegistry *pluginregistry.PluginRegistry - internalStorage *MemoryStorageEngine + BaseTestSuite } func TestTestRunnerSuite(t *testing.T) { @@ -114,11 +72,8 @@ func TestTestRunnerSuite(t *testing.T) { } func (s *TestRunnerSuite) SetupTest() { - storageEngine, err := NewMemoryStorageEngine() - require.NoError(s.T(), err) - s.internalStorage = storageEngine + s.BaseTestSuite.SetupTest() - s.pluginRegistry = pluginregistry.NewPluginRegistry(ctx) for _, e := range []struct { name string factory test.TestStepFactory @@ -131,29 +86,14 @@ func (s *TestRunnerSuite) SetupTest() { {panicstep.Name, panicstep.New, panicstep.Events}, {teststep.Name, teststep.New, teststep.Events}, } { - if err := s.pluginRegistry.RegisterTestStep(e.name, e.factory, e.events); err != nil { + if err := s.PluginRegistry.RegisterTestStep(e.name, e.factory, e.events); err != nil { panic(fmt.Sprintf("could not register TestStep: %v", err)) } } } -func (s *TestRunnerSuite) newStep(label, name string, params *test.TestStepParameters) test.TestStepBundle { - td := test.TestStepDescriptor{ - Name: name, - Label: label, - } - if params != nil { - td.Parameters = *params - } - sb, err := s.pluginRegistry.NewTestStepBundle(ctx, td) - if err != nil { - panic(fmt.Sprintf("failed to create test step bundle: %v", err)) - } - return *sb -} - func (s *TestRunnerSuite) newTestStep(label string, failPct int, failTargets string, delayTargets string) test.TestStepBundle { - return s.newStep(label, teststep.Name, &test.TestStepParameters{ + return s.NewStep(label, teststep.Name, test.TestStepParameters{ teststep.FailPctParam: []test.Param{*test.NewParam(fmt.Sprintf("%d", failPct))}, teststep.FailTargetsParam: []test.Param{*test.NewParam(failTargets)}, teststep.DelayTargetsParam: []test.Param{*test.NewParam(delayTargets)}, @@ -173,7 +113,7 @@ func (s *TestRunnerSuite) runWithTimeout(ctx xcontext.Context, Name: testName, TestStepsBundles: bundles, } - emitterFactory := NewTestStepEventsEmitterFactory(s.internalStorage.StorageEngineVault, 1, runID, test.Name, 0) + emitterFactory := NewTestStepEventsEmitterFactory(s.MemoryStorage.StorageEngineVault, 1, runID, test.Name, 0) resCh := make(chan runRes) go func() { res, targetsResults, err := tr.Run(newCtx, test, targets, emitterFactory, resumeState) @@ -192,7 +132,7 @@ func (s *TestRunnerSuite) runWithTimeout(ctx xcontext.Context, // Simple case: one target, one step, success. func (s *TestRunnerSuite) Test1Step1Success() { tr := newTestRunner() - _, targetsResults, err := s.runWithTimeout(ctx, tr, nil, 1, 2*time.Second, + _, targetsResults, err := s.runWithTimeout(s.Ctx, tr, nil, 1, 2*time.Second, []*target.Target{tgt("T1")}, []test.TestStepBundle{ s.newTestStep("Step 1", 0, "", ""), @@ -206,20 +146,20 @@ func (s *TestRunnerSuite) Test1Step1Success() { require.Equal(s.T(), ` {[1 1 SimpleTest 0 Step 1][(*Target)(nil) TestStepRunningEvent]} {[1 1 SimpleTest 0 Step 1][(*Target)(nil) TestStepFinishedEvent]} -`, s.internalStorage.GetStepEvents(testName, "")) +`, s.MemoryStorage.GetStepEvents(testName, "")) require.Equal(s.T(), ` {[1 1 SimpleTest 0 Step 1][Target{ID: "T1"} TargetIn]} {[1 1 SimpleTest 0 Step 1][Target{ID: "T1"} TestStartedEvent]} {[1 1 SimpleTest 0 Step 1][Target{ID: "T1"} TestFinishedEvent]} {[1 1 SimpleTest 0 Step 1][Target{ID: "T1"} TargetOut]} -`, s.internalStorage.GetTargetEvents(testName, "T1")) +`, s.MemoryStorage.GetTargetEvents(testName, "T1")) } // Simple case: one target, one step that blocks for a bit, success. // We block for longer than the shutdown timeout of the test runner. func (s *TestRunnerSuite) Test1StepLongerThanShutdown1Success() { tr := NewTestRunnerWithTimeouts(100 * time.Millisecond) - _, targetsResults, err := s.runWithTimeout(ctx, tr, nil, 1, 2*time.Second, + _, targetsResults, err := s.runWithTimeout(s.Ctx, tr, nil, 1, 2*time.Second, []*target.Target{tgt("T1")}, []test.TestStepBundle{ s.newTestStep("Step 1", 0, "", "T1=500"), @@ -233,19 +173,19 @@ func (s *TestRunnerSuite) Test1StepLongerThanShutdown1Success() { require.Equal(s.T(), ` {[1 1 SimpleTest 0 Step 1][(*Target)(nil) TestStepRunningEvent]} {[1 1 SimpleTest 0 Step 1][(*Target)(nil) TestStepFinishedEvent]} -`, s.internalStorage.GetStepEvents(testName, "")) +`, s.MemoryStorage.GetStepEvents(testName, "")) require.Equal(s.T(), ` {[1 1 SimpleTest 0 Step 1][Target{ID: "T1"} TargetIn]} {[1 1 SimpleTest 0 Step 1][Target{ID: "T1"} TestStartedEvent]} {[1 1 SimpleTest 0 Step 1][Target{ID: "T1"} TestFinishedEvent]} {[1 1 SimpleTest 0 Step 1][Target{ID: "T1"} TargetOut]} -`, s.internalStorage.GetTargetEvents(testName, "T1")) +`, s.MemoryStorage.GetTargetEvents(testName, "T1")) } // Simple case: one target, one step, failure. func (s *TestRunnerSuite) Test1Step1Fail() { tr := newTestRunner() - _, targetsResults, err := s.runWithTimeout(ctx, tr, nil, 1, 2*time.Second, + _, targetsResults, err := s.runWithTimeout(s.Ctx, tr, nil, 1, 2*time.Second, []*target.Target{tgt("T1")}, []test.TestStepBundle{ s.newTestStep("Step 1", 100, "", ""), @@ -258,19 +198,19 @@ func (s *TestRunnerSuite) Test1Step1Fail() { require.Equal(s.T(), ` {[1 1 SimpleTest 0 Step 1][(*Target)(nil) TestStepRunningEvent]} {[1 1 SimpleTest 0 Step 1][(*Target)(nil) TestStepFinishedEvent]} -`, s.internalStorage.GetStepEvents(testName, "Step 1")) +`, s.MemoryStorage.GetStepEvents(testName, "Step 1")) require.Equal(s.T(), ` {[1 1 SimpleTest 0 Step 1][Target{ID: "T1"} TargetIn]} {[1 1 SimpleTest 0 Step 1][Target{ID: "T1"} TestStartedEvent]} {[1 1 SimpleTest 0 Step 1][Target{ID: "T1"} TestFailedEvent]} {[1 1 SimpleTest 0 Step 1][Target{ID: "T1"} TargetErr &"{\"Error\":\"target failed\"}"]} -`, s.internalStorage.GetTargetEvents(testName, "T1")) +`, s.MemoryStorage.GetTargetEvents(testName, "T1")) } // One step pipeline with two targets - one fails, one succeeds. func (s *TestRunnerSuite) Test1Step1Success1Fail() { tr := newTestRunner() - _, _, err := s.runWithTimeout(ctx, tr, nil, 1, 2*time.Second, + _, _, err := s.runWithTimeout(s.Ctx, tr, nil, 1, 2*time.Second, []*target.Target{tgt("T1"), tgt("T2")}, []test.TestStepBundle{ s.newTestStep("Step 1", 0, "T1", "T2=100"), @@ -280,26 +220,26 @@ func (s *TestRunnerSuite) Test1Step1Success1Fail() { require.Equal(s.T(), ` {[1 1 SimpleTest 0 Step 1][(*Target)(nil) TestStepRunningEvent]} {[1 1 SimpleTest 0 Step 1][(*Target)(nil) TestStepFinishedEvent]} -`, s.internalStorage.GetStepEvents(testName, "")) +`, s.MemoryStorage.GetStepEvents(testName, "")) require.Equal(s.T(), ` {[1 1 SimpleTest 0 Step 1][Target{ID: "T1"} TargetIn]} {[1 1 SimpleTest 0 Step 1][Target{ID: "T1"} TestStartedEvent]} {[1 1 SimpleTest 0 Step 1][Target{ID: "T1"} TestFailedEvent]} {[1 1 SimpleTest 0 Step 1][Target{ID: "T1"} TargetErr &"{\"Error\":\"target failed\"}"]} -`, s.internalStorage.GetTargetEvents(testName, "T1")) +`, s.MemoryStorage.GetTargetEvents(testName, "T1")) require.Equal(s.T(), ` {[1 1 SimpleTest 0 Step 1][Target{ID: "T2"} TargetIn]} {[1 1 SimpleTest 0 Step 1][Target{ID: "T2"} TestStartedEvent]} {[1 1 SimpleTest 0 Step 1][Target{ID: "T2"} TestFinishedEvent]} {[1 1 SimpleTest 0 Step 1][Target{ID: "T2"} TargetOut]} -`, s.internalStorage.GetTargetEvents(testName, "T2")) +`, s.MemoryStorage.GetTargetEvents(testName, "T2")) } // Three-step pipeline, two targets: T1 fails at step 1, T2 fails at step 2, // step 3 is not reached and not even run. func (s *TestRunnerSuite) Test3StepsNotReachedStepNotRun() { tr := newTestRunner() - _, _, err := s.runWithTimeout(ctx, tr, nil, 1, 2*time.Second, + _, _, err := s.runWithTimeout(s.Ctx, tr, nil, 1, 2*time.Second, []*target.Target{tgt("T1"), tgt("T2")}, []test.TestStepBundle{ s.newTestStep("Step 1", 0, "T1", ""), @@ -311,18 +251,18 @@ func (s *TestRunnerSuite) Test3StepsNotReachedStepNotRun() { require.Equal(s.T(), ` {[1 1 SimpleTest 0 Step 1][(*Target)(nil) TestStepRunningEvent]} {[1 1 SimpleTest 0 Step 1][(*Target)(nil) TestStepFinishedEvent]} -`, s.internalStorage.GetStepEvents(testName, "Step 1")) +`, s.MemoryStorage.GetStepEvents(testName, "Step 1")) require.Equal(s.T(), ` {[1 1 SimpleTest 0 Step 2][(*Target)(nil) TestStepRunningEvent]} {[1 1 SimpleTest 0 Step 2][(*Target)(nil) TestStepFinishedEvent]} -`, s.internalStorage.GetStepEvents(testName, "Step 2")) - require.Equal(s.T(), "\n\n", s.internalStorage.GetStepEvents(testName, "Step 3")) +`, s.MemoryStorage.GetStepEvents(testName, "Step 2")) + require.Equal(s.T(), "\n\n", s.MemoryStorage.GetStepEvents(testName, "Step 3")) require.Equal(s.T(), ` {[1 1 SimpleTest 0 Step 1][Target{ID: "T1"} TargetIn]} {[1 1 SimpleTest 0 Step 1][Target{ID: "T1"} TestStartedEvent]} {[1 1 SimpleTest 0 Step 1][Target{ID: "T1"} TestFailedEvent]} {[1 1 SimpleTest 0 Step 1][Target{ID: "T1"} TargetErr &"{\"Error\":\"target failed\"}"]} -`, s.internalStorage.GetTargetEvents(testName, "T1")) +`, s.MemoryStorage.GetTargetEvents(testName, "T1")) require.Equal(s.T(), ` {[1 1 SimpleTest 0 Step 1][Target{ID: "T2"} TargetIn]} {[1 1 SimpleTest 0 Step 1][Target{ID: "T2"} TestStartedEvent]} @@ -332,48 +272,48 @@ func (s *TestRunnerSuite) Test3StepsNotReachedStepNotRun() { {[1 1 SimpleTest 0 Step 2][Target{ID: "T2"} TestStartedEvent]} {[1 1 SimpleTest 0 Step 2][Target{ID: "T2"} TestFailedEvent]} {[1 1 SimpleTest 0 Step 2][Target{ID: "T2"} TargetErr &"{\"Error\":\"target failed\"}"]} -`, s.internalStorage.GetTargetEvents(testName, "T2")) +`, s.MemoryStorage.GetTargetEvents(testName, "T2")) } // A misbehaving step that fails to shut down properly after processing targets // and does not return. func (s *TestRunnerSuite) TestNoReturnStepWithCorrectTargetForwarding() { tr := NewTestRunnerWithTimeouts(200 * time.Millisecond) - ctx, cancel := xcontext.WithCancel(ctx) + ctx, cancel := xcontext.WithCancel(s.Ctx) defer cancel() _, _, err := s.runWithTimeout(ctx, tr, nil, 1, 2*time.Second, []*target.Target{tgt("T1")}, []test.TestStepBundle{ - s.newStep("Step 1", noreturn.Name, nil), + s.NewStep("Step 1", noreturn.Name, nil), }, ) require.Error(s.T(), err) require.IsType(s.T(), &cerrors.ErrTestStepsNeverReturned{}, err) - require.Contains(s.T(), s.internalStorage.GetStepEvents(testName, "Step 1"), "step [Step 1] did not return") + require.Contains(s.T(), s.MemoryStorage.GetStepEvents(testName, "Step 1"), "step [Step 1] did not return") } // A misbehaving step that panics. func (s *TestRunnerSuite) TestStepPanics() { tr := newTestRunner() - _, _, err := s.runWithTimeout(ctx, tr, nil, 1, 2*time.Second, + _, _, err := s.runWithTimeout(s.Ctx, tr, nil, 1, 2*time.Second, []*target.Target{tgt("T1")}, []test.TestStepBundle{ - s.newStep("Step 1", panicstep.Name, nil), + s.NewStep("Step 1", panicstep.Name, nil), }, ) require.Error(s.T(), err) require.IsType(s.T(), &cerrors.ErrTestStepPaniced{}, err) - require.Equal(s.T(), "\n\n", s.internalStorage.GetTargetEvents(testName, "T1")) - require.Contains(s.T(), s.internalStorage.GetStepEvents(testName, "Step 1"), "step Step 1 paniced") + require.Equal(s.T(), "\n\n", s.MemoryStorage.GetTargetEvents(testName, "T1")) + require.Contains(s.T(), s.MemoryStorage.GetStepEvents(testName, "Step 1"), "step Step 1 paniced") } // A misbehaving step that closes its output channel. func (s *TestRunnerSuite) TestStepClosesChannels() { tr := newTestRunner() - _, _, err := s.runWithTimeout(ctx, tr, nil, 1, 2*time.Second, + _, _, err := s.runWithTimeout(s.Ctx, tr, nil, 1, 2*time.Second, []*target.Target{tgt("T1")}, []test.TestStepBundle{ - s.newStep("Step 1", channels.Name, nil), + s.NewStep("Step 1", channels.Name, nil), }, ) require.Error(s.T(), err) @@ -381,38 +321,38 @@ func (s *TestRunnerSuite) TestStepClosesChannels() { require.Equal(s.T(), ` {[1 1 SimpleTest 0 Step 1][Target{ID: "T1"} TargetIn]} {[1 1 SimpleTest 0 Step 1][Target{ID: "T1"} TargetOut]} -`, s.internalStorage.GetTargetEvents(testName, "T1")) +`, s.MemoryStorage.GetTargetEvents(testName, "T1")) require.Equal(s.T(), ` {[1 1 SimpleTest 0 Step 1][(*Target)(nil) TestError &"\"test step Step 1 closed output channels (api violation)\""]} -`, s.internalStorage.GetStepEvents(testName, "Step 1")) +`, s.MemoryStorage.GetStepEvents(testName, "Step 1")) } // A misbehaving step that yields a result for a target that does not exist. func (s *TestRunnerSuite) TestStepYieldsResultForNonexistentTarget() { tr := newTestRunner() - _, _, err := s.runWithTimeout(ctx, tr, nil, 1, 2*time.Second, + _, _, err := s.runWithTimeout(s.Ctx, tr, nil, 1, 2*time.Second, []*target.Target{tgt("TExtra")}, []test.TestStepBundle{ - s.newStep("Step 1", badtargets.Name, nil), + s.NewStep("Step 1", badtargets.Name, nil), }, ) require.Error(s.T(), err) require.IsType(s.T(), &cerrors.ErrTestStepReturnedUnexpectedResult{}, err) - require.Equal(s.T(), "\n\n", s.internalStorage.GetTargetEvents(testName, "TExtra2")) + require.Equal(s.T(), "\n\n", s.MemoryStorage.GetTargetEvents(testName, "TExtra2")) require.Equal(s.T(), ` {[1 1 SimpleTest 0 Step 1][(*Target)(nil) TestError &"\"test step Step 1 returned unexpected result for TExtra2\""]} -`, s.internalStorage.GetStepEvents(testName, "Step 1")) +`, s.MemoryStorage.GetStepEvents(testName, "Step 1")) } // A misbehaving step that yields a duplicate result for a target. func (s *TestRunnerSuite) TestStepYieldsDuplicateResult() { tr := newTestRunner() - _, _, err := s.runWithTimeout(ctx, tr, nil, 1, 2*time.Second, + _, _, err := s.runWithTimeout(s.Ctx, tr, nil, 1, 2*time.Second, []*target.Target{tgt("TGood"), tgt("TDup")}, []test.TestStepBundle{ // TGood makes it past here unscathed and gets delayed in Step 2, // TDup also emerges fine at first but is then returned again, and that's bad. - s.newStep("Step 1", badtargets.Name, nil), + s.NewStep("Step 1", badtargets.Name, nil), s.newTestStep("Step 2", 0, "", "TGood=100"), }, ) @@ -423,10 +363,10 @@ func (s *TestRunnerSuite) TestStepYieldsDuplicateResult() { // A misbehaving step that loses targets. func (s *TestRunnerSuite) TestStepLosesTargets() { tr := newTestRunner() - _, _, err := s.runWithTimeout(ctx, tr, nil, 1, 2*time.Second, + _, _, err := s.runWithTimeout(s.Ctx, tr, nil, 1, 2*time.Second, []*target.Target{tgt("TGood"), tgt("TDrop")}, []test.TestStepBundle{ - s.newStep("Step 1", badtargets.Name, nil), + s.NewStep("Step 1", badtargets.Name, nil), }, ) require.Error(s.T(), err) @@ -438,13 +378,13 @@ func (s *TestRunnerSuite) TestStepLosesTargets() { // but is not currently waiting for it. func (s *TestRunnerSuite) TestStepYieldsResultForUnexpectedTarget() { tr := newTestRunner() - _, _, err := s.runWithTimeout(ctx, tr, nil, 1, 2*time.Second, + _, _, err := s.runWithTimeout(s.Ctx, tr, nil, 1, 2*time.Second, []*target.Target{tgt("TExtra"), tgt("TExtra2")}, []test.TestStepBundle{ // TExtra2 fails here. s.newTestStep("Step 1", 0, "TExtra2", ""), // Yet, a result for it is returned here, which we did not expect. - s.newStep("Step 2", badtargets.Name, nil), + s.NewStep("Step 2", badtargets.Name, nil), }, ) require.Error(s.T(), err) @@ -458,7 +398,7 @@ func (s *TestRunnerSuite) TestRandomizedMultiStep() { for i := 1; i <= 100; i++ { targets = append(targets, tgt(fmt.Sprintf("T%d", i))) } - _, _, err := s.runWithTimeout(ctx, tr, nil, 1, 2*time.Second, + _, _, err := s.runWithTimeout(s.Ctx, tr, nil, 1, 2*time.Second, targets, []test.TestStepBundle{ s.newTestStep("Step 1", 0, "", "*=10"), // All targets pass the first step, with a slight delay @@ -477,9 +417,9 @@ func (s *TestRunnerSuite) TestRandomizedMultiStep() { {[1 1 SimpleTest 0 Step 1][Target{ID: "%s"} TestFinishedEvent]} {[1 1 SimpleTest 0 Step 1][Target{ID: "%s"} TargetOut]} `, tgt.ID, tgt.ID, tgt.ID, tgt.ID), - common.GetTestEventsAsString(ctx, s.internalStorage.Storage, testName, &tgt.ID, &s1n)) + common.GetTestEventsAsString(s.Ctx, s.MemoryStorage.Storage, testName, &tgt.ID, &s1n)) s3n := "Step 3" - if strings.Contains(common.GetTestEventsAsString(ctx, s.internalStorage.Storage, testName, &tgt.ID, &s3n), "TestFinishedEvent") { + if strings.Contains(common.GetTestEventsAsString(s.Ctx, s.MemoryStorage.Storage, testName, &tgt.ID, &s3n), "TestFinishedEvent") { numFinished++ } } @@ -501,12 +441,12 @@ func (s *TestRunnerSuite) TestPauseResumeSimple() { } { tr1 := newTestRunner() - ctx1, pause := xcontext.WithNotify(ctx, xcontext.ErrPaused) + ctx1, pause := xcontext.WithNotify(s.Ctx, xcontext.ErrPaused) ctx1, cancel := xcontext.WithCancel(ctx1) defer cancel() go func() { time.Sleep(100 * time.Millisecond) - ctx.Infof("TestPauseResumeNaive: pausing") + s.Ctx.Infof("TestPauseResumeNaive: pausing") pause() }() resumeState, _, err = s.runWithTimeout(ctx1, tr1, nil, 1, 2*time.Second, targets, steps) @@ -514,11 +454,11 @@ func (s *TestRunnerSuite) TestPauseResumeSimple() { require.IsType(s.T(), xcontext.ErrPaused, err) require.NotNil(s.T(), resumeState) } - ctx.Debugf("Resume state: %s", string(resumeState)) + s.Ctx.Debugf("Resume state: %s", string(resumeState)) // Make sure that resume state is validated. { tr := newTestRunner() - ctx, cancel := xcontext.WithCancel(ctx) + ctx, cancel := xcontext.WithCancel(s.Ctx) defer cancel() resumeState2, _, err := s.runWithTimeout( ctx, tr, []byte("FOO"), 2, 2*time.Second, targets, steps) @@ -528,7 +468,7 @@ func (s *TestRunnerSuite) TestPauseResumeSimple() { } { tr := newTestRunner() - ctx, cancel := xcontext.WithCancel(ctx) + ctx, cancel := xcontext.WithCancel(s.Ctx) defer cancel() resumeState2 := strings.Replace(string(resumeState), `"V"`, `"XV"`, 1) _, _, err := s.runWithTimeout( @@ -540,7 +480,7 @@ func (s *TestRunnerSuite) TestPauseResumeSimple() { // Finally, resume and finish the job. { tr2 := newTestRunner() - ctx2, cancel := xcontext.WithCancel(ctx) + ctx2, cancel := xcontext.WithCancel(s.Ctx) defer cancel() _, _, err := s.runWithTimeout(ctx2, tr2, resumeState, 5, 2*time.Second, // Pass exactly the same targets and pipeline to resume properly. @@ -560,23 +500,23 @@ func (s *TestRunnerSuite) TestPauseResumeSimple() { require.Equal(s.T(), ` {[1 1 SimpleTest 0 Step 1][(*Target)(nil) TestStepRunningEvent]} {[1 1 SimpleTest 0 Step 1][(*Target)(nil) TestStepFinishedEvent]} -`, s.internalStorage.GetStepEvents(testName, "Step 1")) +`, s.MemoryStorage.GetStepEvents(testName, "Step 1")) require.Equal(s.T(), ` {[1 1 SimpleTest 0 Step 2][(*Target)(nil) TestStepRunningEvent]} {[1 1 SimpleTest 0 Step 2][(*Target)(nil) TestStepFinishedEvent]} -`, s.internalStorage.GetStepEvents(testName, "Step 2")) +`, s.MemoryStorage.GetStepEvents(testName, "Step 2")) // Step 3 did not get to start in the first instance and ran in the second. require.Equal(s.T(), ` {[1 5 SimpleTest 0 Step 3][(*Target)(nil) TestStepRunningEvent]} {[1 5 SimpleTest 0 Step 3][(*Target)(nil) TestStepFinishedEvent]} -`, s.internalStorage.GetStepEvents(testName, "Step 3")) +`, s.MemoryStorage.GetStepEvents(testName, "Step 3")) // T1 failed entirely within the first run. require.Equal(s.T(), ` {[1 1 SimpleTest 0 Step 1][Target{ID: "T1"} TargetIn]} {[1 1 SimpleTest 0 Step 1][Target{ID: "T1"} TestStartedEvent]} {[1 1 SimpleTest 0 Step 1][Target{ID: "T1"} TestFailedEvent]} {[1 1 SimpleTest 0 Step 1][Target{ID: "T1"} TargetErr &"{\"Error\":\"target failed\"}"]} -`, s.internalStorage.GetTargetEvents(testName, "T1")) +`, s.MemoryStorage.GetTargetEvents(testName, "T1")) // T2 and T3 ran in both. require.Equal(s.T(), ` {[1 1 SimpleTest 0 Step 1][Target{ID: "T2"} TargetIn]} @@ -591,5 +531,5 @@ func (s *TestRunnerSuite) TestPauseResumeSimple() { {[1 5 SimpleTest 0 Step 3][Target{ID: "T2"} TestStartedEvent]} {[1 5 SimpleTest 0 Step 3][Target{ID: "T2"} TestFinishedEvent]} {[1 5 SimpleTest 0 Step 3][Target{ID: "T2"} TargetOut]} -`, s.internalStorage.GetTargetEvents(testName, "T2")) +`, s.MemoryStorage.GetTargetEvents(testName, "T2")) }