-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathbase_test_suite_test.go
More file actions
102 lines (84 loc) · 2.96 KB
/
base_test_suite_test.go
File metadata and controls
102 lines (84 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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/storage"
"github.com/linuxboot/contest/pkg/target"
"github.com/linuxboot/contest/pkg/test"
"github.com/linuxboot/contest/pkg/xcontext"
"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 {
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(ctx xcontext.Context, testName string, stepLabel string) string {
return common.GetTestEventsAsString(ctx, mse.Storage, testName, nil, &stepLabel)
}
func (mse *MemoryStorageEngine) GetTargetEvents(ctx xcontext.Context, testName string, targetID string) string {
return common.GetTestEventsAsString(ctx, mse.Storage, testName, &targetID, nil)
}
func (mse *MemoryStorageEngine) GetTestEvents(ctx xcontext.Context, testName string) string {
return common.GetTestEventsAsString(ctx, mse.Storage, testName, nil, nil)
}
type BaseTestSuite struct {
suite.Suite
PluginRegistry *pluginregistry.PluginRegistry
MemoryStorage *MemoryStorageEngine
}
func (s *BaseTestSuite) SetupTest() {
storageEngine, err := NewMemoryStorageEngine()
require.NoError(s.T(), err)
s.MemoryStorage = 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, io test.TestStepInputOutput, ev testevent.Emitter,
stepsVars test.StepsVariables, params test.TestStepParameters,
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(
ctx xcontext.Context,
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
}