|
| 1 | +package variables |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "sync" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/linuxboot/contest/pkg/event/testevent" |
| 10 | + "github.com/linuxboot/contest/pkg/storage" |
| 11 | + "github.com/linuxboot/contest/pkg/target" |
| 12 | + "github.com/linuxboot/contest/pkg/test" |
| 13 | + "github.com/linuxboot/contest/pkg/xcontext" |
| 14 | + "github.com/linuxboot/contest/plugins/storage/memory" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | +) |
| 17 | + |
| 18 | +func TestCreation(t *testing.T) { |
| 19 | + obj := New() |
| 20 | + require.NotNil(t, obj) |
| 21 | + require.Equal(t, Name, obj.Name()) |
| 22 | +} |
| 23 | + |
| 24 | +func TestValidateParameters(t *testing.T) { |
| 25 | + obj := New() |
| 26 | + require.NotNil(t, obj) |
| 27 | + |
| 28 | + require.NoError(t, obj.ValidateParameters(xcontext.Background(), nil)) |
| 29 | + require.NoError(t, obj.ValidateParameters(xcontext.Background(), test.TestStepParameters{ |
| 30 | + "var1": []test.Param{ |
| 31 | + { |
| 32 | + RawMessage: json.RawMessage("123"), |
| 33 | + }, |
| 34 | + }, |
| 35 | + })) |
| 36 | + // invalid variable name |
| 37 | + require.Error(t, obj.ValidateParameters(xcontext.Background(), test.TestStepParameters{ |
| 38 | + "var var": []test.Param{ |
| 39 | + { |
| 40 | + RawMessage: json.RawMessage("123"), |
| 41 | + }, |
| 42 | + }, |
| 43 | + })) |
| 44 | + // invalid value |
| 45 | + require.Error(t, obj.ValidateParameters(xcontext.Background(), test.TestStepParameters{ |
| 46 | + "var1": []test.Param{ |
| 47 | + { |
| 48 | + RawMessage: json.RawMessage("ALALALALA[}"), |
| 49 | + }, |
| 50 | + }, |
| 51 | + })) |
| 52 | +} |
| 53 | + |
| 54 | +func TestVariablesEmission(t *testing.T) { |
| 55 | + ctx, cancel := xcontext.WithCancel(xcontext.Background()) |
| 56 | + defer cancel() |
| 57 | + |
| 58 | + obj := New() |
| 59 | + require.NotNil(t, obj) |
| 60 | + |
| 61 | + in := make(chan *target.Target, 1) |
| 62 | + out := make(chan test.TestStepResult, 1) |
| 63 | + |
| 64 | + m, err := memory.New() |
| 65 | + if err != nil { |
| 66 | + t.Fatalf("could not initialize memory storage: '%v'", err) |
| 67 | + } |
| 68 | + storageEngineVault := storage.NewSimpleEngineVault() |
| 69 | + if err := storageEngineVault.StoreEngine(m, storage.SyncEngine); err != nil { |
| 70 | + t.Fatalf("Failed to set memory storage: '%v'", err) |
| 71 | + } |
| 72 | + ev := storage.NewTestEventEmitterFetcher(storageEngineVault, testevent.Header{ |
| 73 | + JobID: 12345, |
| 74 | + TestName: "variables_tests", |
| 75 | + TestStepLabel: "variables", |
| 76 | + }) |
| 77 | + |
| 78 | + svm := newStepsVariablesMock() |
| 79 | + |
| 80 | + tgt := target.Target{ID: "id1"} |
| 81 | + in <- &tgt |
| 82 | + close(in) |
| 83 | + |
| 84 | + state, err := obj.Run(ctx, test.TestStepChannels{In: in, Out: out}, ev, svm, test.TestStepParameters{ |
| 85 | + "str_variable": []test.Param{ |
| 86 | + { |
| 87 | + RawMessage: json.RawMessage("\"dummy\""), |
| 88 | + }, |
| 89 | + }, |
| 90 | + "int_variable": []test.Param{ |
| 91 | + { |
| 92 | + RawMessage: json.RawMessage("123"), |
| 93 | + }, |
| 94 | + }, |
| 95 | + "complex_variable": []test.Param{ |
| 96 | + { |
| 97 | + RawMessage: json.RawMessage("{\"name\":\"value\"}"), |
| 98 | + }, |
| 99 | + }, |
| 100 | + }, nil) |
| 101 | + require.NoError(t, err) |
| 102 | + require.Empty(t, state) |
| 103 | + |
| 104 | + stepResult := <-out |
| 105 | + require.Equal(t, tgt, *stepResult.Target) |
| 106 | + require.NoError(t, stepResult.Err) |
| 107 | + |
| 108 | + var strVar string |
| 109 | + require.NoError(t, svm.get(tgt.ID, "str_variable", &strVar)) |
| 110 | + require.Equal(t, "dummy", strVar) |
| 111 | + |
| 112 | + var intVar int |
| 113 | + require.NoError(t, svm.get(tgt.ID, "int_variable", &intVar)) |
| 114 | + require.Equal(t, 123, intVar) |
| 115 | + |
| 116 | + var complexVar dummyStruct |
| 117 | + require.NoError(t, svm.get(tgt.ID, "complex_variable", &complexVar)) |
| 118 | + require.Equal(t, dummyStruct{Name: "value"}, complexVar) |
| 119 | +} |
| 120 | + |
| 121 | +type dummyStruct struct { |
| 122 | + Name string `json:"name"` |
| 123 | +} |
| 124 | + |
| 125 | +type stepsVariablesMock struct { |
| 126 | + mu sync.Mutex |
| 127 | + variables map[string]map[string]json.RawMessage |
| 128 | +} |
| 129 | + |
| 130 | +func newStepsVariablesMock() *stepsVariablesMock { |
| 131 | + return &stepsVariablesMock{ |
| 132 | + variables: make(map[string]map[string]json.RawMessage), |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +func (svm *stepsVariablesMock) AddRaw(tgtID string, name string, b json.RawMessage) error { |
| 137 | + svm.mu.Lock() |
| 138 | + defer svm.mu.Unlock() |
| 139 | + |
| 140 | + targetVars := svm.variables[tgtID] |
| 141 | + if targetVars == nil { |
| 142 | + targetVars = make(map[string]json.RawMessage) |
| 143 | + svm.variables[tgtID] = targetVars |
| 144 | + } |
| 145 | + targetVars[name] = b |
| 146 | + return nil |
| 147 | +} |
| 148 | + |
| 149 | +func (svm *stepsVariablesMock) Add(tgtID string, name string, v interface{}) error { |
| 150 | + b, err := json.Marshal(v) |
| 151 | + if err != nil { |
| 152 | + return err |
| 153 | + } |
| 154 | + |
| 155 | + return svm.AddRaw(tgtID, name, b) |
| 156 | +} |
| 157 | + |
| 158 | +func (svm *stepsVariablesMock) Get(tgtID string, stepLabel, name string, out interface{}) error { |
| 159 | + panic("not implemented") |
| 160 | +} |
| 161 | + |
| 162 | +func (svm *stepsVariablesMock) get(tgtID string, name string, out interface{}) error { |
| 163 | + svm.mu.Lock() |
| 164 | + defer svm.mu.Unlock() |
| 165 | + |
| 166 | + targetVars := svm.variables[tgtID] |
| 167 | + if targetVars == nil { |
| 168 | + return fmt.Errorf("no target: %s", tgtID) |
| 169 | + } |
| 170 | + b, found := targetVars[name] |
| 171 | + if !found { |
| 172 | + return fmt.Errorf("no variable: %s", name) |
| 173 | + } |
| 174 | + return json.Unmarshal(b, out) |
| 175 | +} |
0 commit comments