|
8 | 8 | "os" |
9 | 9 | "path/filepath" |
10 | 10 | "reflect" |
| 11 | + "slices" |
11 | 12 | "testing" |
12 | 13 | "time" |
13 | 14 |
|
@@ -125,12 +126,36 @@ func testOneBucket(t *testing.T, hub *cwhub.Hub, dir string) { |
125 | 126 | holders, response, err := LoadBuckets(cscfg, hub, scenarios, bucketStore, false) |
126 | 127 | require.NoError(t, err) |
127 | 128 |
|
128 | | - if !testFile(t, filepath.Join(dir, "test.json"), holders, response, bucketStore) { |
129 | | - t.Fatalf("tests from %s failed", dir) |
| 129 | + testFile(t, filepath.Join(dir, "test.json"), holders, response, bucketStore) |
| 130 | +} |
| 131 | + |
| 132 | +func matchOverflow(got, expected pipeline.RuntimeAlert) bool { |
| 133 | + // both empty |
| 134 | + if got.Alert == nil && expected.Alert == nil { |
| 135 | + return true |
| 136 | + } |
| 137 | + |
| 138 | + // one empty, one not |
| 139 | + if got.Alert == nil || expected.Alert == nil { |
| 140 | + return false |
| 141 | + } |
| 142 | + |
| 143 | + if *got.Alert.Scenario != *expected.Alert.Scenario { |
| 144 | + return false |
130 | 145 | } |
| 146 | + |
| 147 | + if *got.Alert.EventsCount != *expected.Alert.EventsCount { |
| 148 | + return false |
| 149 | + } |
| 150 | + |
| 151 | + if !reflect.DeepEqual(got.Sources, expected.Sources) { |
| 152 | + return false |
| 153 | + } |
| 154 | + |
| 155 | + return true |
131 | 156 | } |
132 | 157 |
|
133 | | -func testFile(t *testing.T, file string, holders []BucketFactory, response chan pipeline.Event, bucketStore *BucketStore) bool { |
| 158 | +func testFile(t *testing.T, file string, holders []BucketFactory, response chan pipeline.Event, bucketStore *BucketStore) { |
134 | 159 | var results []pipeline.Event |
135 | 160 |
|
136 | 161 | yamlFile, err := os.Open(file) |
@@ -211,75 +236,39 @@ POLL_AGAIN: |
211 | 236 | */ |
212 | 237 | for { |
213 | 238 | if len(tf.Results) == 0 && len(results) == 0 { |
214 | | - log.Warning("Test is successful") |
215 | | - return true |
| 239 | + return |
216 | 240 | } |
217 | | - log.Warningf("%d results to check against %d expected results", len(results), len(tf.Results)) |
218 | | - if len(tf.Results) != len(results) { |
219 | | - log.Errorf("results / expected count doesn't match results = %d / expected = %d", len(results), len(tf.Results)) |
220 | | - return false |
221 | | - } |
222 | | - checkresultsloop: |
223 | | - for eidx, out := range results { |
224 | | - for ridx, expected := range tf.Results { |
225 | | - log.Tracef("Checking next expected result.") |
226 | | - |
227 | | - // empty overflow |
228 | | - if out.Overflow.Alert == nil && expected.Overflow.Alert == nil { |
229 | | - // match stuff |
230 | | - } else { |
231 | | - if out.Overflow.Alert == nil || expected.Overflow.Alert == nil { |
232 | | - log.Info("Here ?") |
233 | | - continue |
234 | | - } |
235 | | - |
236 | | - // Scenario |
237 | | - if *out.Overflow.Alert.Scenario != *expected.Overflow.Alert.Scenario { |
238 | | - log.Errorf("(scenario) %v != %v", *out.Overflow.Alert.Scenario, *expected.Overflow.Alert.Scenario) |
239 | | - continue |
240 | | - } |
241 | | - log.Infof("(scenario) %v == %v", *out.Overflow.Alert.Scenario, *expected.Overflow.Alert.Scenario) |
242 | | - |
243 | | - // EventsCount |
244 | | - if *out.Overflow.Alert.EventsCount != *expected.Overflow.Alert.EventsCount { |
245 | | - log.Errorf("(EventsCount) %d != %d", *out.Overflow.Alert.EventsCount, *expected.Overflow.Alert.EventsCount) |
246 | | - continue |
247 | | - } |
248 | | - log.Infof("(EventsCount) %d == %d", *out.Overflow.Alert.EventsCount, *expected.Overflow.Alert.EventsCount) |
249 | | - |
250 | | - // Sources |
251 | | - if !reflect.DeepEqual(out.Overflow.Sources, expected.Overflow.Sources) { |
252 | | - log.Errorf("(Sources %s != %s)", spew.Sdump(out.Overflow.Sources), spew.Sdump(expected.Overflow.Sources)) |
253 | | - continue |
254 | | - } |
255 | | - log.Infof("(Sources: %s == %s)", spew.Sdump(out.Overflow.Sources), spew.Sdump(expected.Overflow.Sources)) |
| 241 | + |
| 242 | + require.Len(t, results, len(tf.Results)) |
| 243 | + |
| 244 | + matched := false |
| 245 | + |
| 246 | + for i := range results { |
| 247 | + out := results[i] |
| 248 | + |
| 249 | + matchIdx := -1 |
| 250 | + |
| 251 | + for j := range tf.Results { |
| 252 | + if matchOverflow(out.Overflow, tf.Results[j].Overflow) { |
| 253 | + matchIdx = j |
| 254 | + break |
256 | 255 | } |
257 | | - // Events |
258 | | - // if !reflect.DeepEqual(out.Overflow.Alert.Events, expected.Overflow.Alert.Events) { |
259 | | - // log.Errorf("(Events %s != %s)", spew.Sdump(out.Overflow.Alert.Events), spew.Sdump(expected.Overflow.Alert.Events)) |
260 | | - // valid = false |
261 | | - // continue |
262 | | - // } else { |
263 | | - // log.Infof("(Events: %s == %s)", spew.Sdump(out.Overflow.Alert.Events), spew.Sdump(expected.Overflow.Alert.Events)) |
264 | | - // } |
265 | | - |
266 | | - // CheckFailed: |
267 | | - |
268 | | - log.Warningf("The test is valid, remove entry %d from expects, and %d from t.Results", eidx, ridx) |
269 | | - // don't do this at home : delete current element from list and redo |
270 | | - results[eidx] = results[len(results)-1] |
271 | | - results = results[:len(results)-1] |
272 | | - tf.Results[ridx] = tf.Results[len(tf.Results)-1] |
273 | | - tf.Results = tf.Results[:len(tf.Results)-1] |
274 | | - goto checkresultsloop |
275 | 256 | } |
| 257 | + |
| 258 | + if matchIdx < 0 { |
| 259 | + continue |
| 260 | + } |
| 261 | + |
| 262 | + results = slices.Delete(results, i, i+1) |
| 263 | + tf.Results = slices.Delete(tf.Results, matchIdx, matchIdx+1) |
| 264 | + |
| 265 | + matched = true |
| 266 | + break // return to outer loop because the slices have changed |
276 | 267 | } |
277 | | - if len(results) != 0 && len(tf.Results) != 0 { |
278 | | - log.Errorf("mismatching entries left") |
279 | | - log.Errorf("we got: %s", spew.Sdump(results)) |
280 | | - log.Errorf("we expected: %s", spew.Sdump(tf.Results)) |
281 | | - return false |
| 268 | + |
| 269 | + if !matched { |
| 270 | + require.Failf(t, "no matching pairs remain", |
| 271 | + "leftover results: %s\nleftover expected: %s", spew.Sdump(results), spew.Sdump(tf.Results)) |
282 | 272 | } |
283 | | - log.Warning("entry valid at end of loop") |
284 | 273 | } |
285 | 274 | } |
0 commit comments