Skip to content

Commit

Permalink
Try to stabilize TestIncludesExcludes (#22514)
Browse files Browse the repository at this point in the history
- Setup files/environment before running the test
  - reduce chance of races, especially with very slow/overloaded CI workers
  - fix t.Fatal being called from spawned go-routine (big NO NO)
- collect all found paths into a dictionary and assert wanted vs. found
  paths after
  - now checks for expected files that have not been found (semantic change)
  - assert will now print a 'diff' with missing/unknown paths on error
  • Loading branch information
Steffen Siering authored Nov 10, 2020
1 parent 118e649 commit 9841601
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions auditbeat/module/file_integrity/metricset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,13 @@ func TestIncludedExcludedFiles(t *testing.T) {
config["recursive"] = true
ms := mbtest.NewPushMetricSetV2(t, config)

go func() {
for _, f := range []string{"FILE.TXT", ".ssh/known_hosts", ".ssh/known_hosts.swp"} {
file := filepath.Join(dir, f)
err := ioutil.WriteFile(file, []byte("hello world"), 0600)
if err != nil {
t.Fatal(err)
}
for _, f := range []string{"FILE.TXT", ".ssh/known_hosts", ".ssh/known_hosts.swp"} {
file := filepath.Join(dir, f)
err := ioutil.WriteFile(file, []byte("hello world"), 0600)
if err != nil {
t.Fatal(err)
}
}()
}

events := mbtest.RunPushMetricSetV2(10*time.Second, 3, ms)
for _, e := range events {
Expand All @@ -278,14 +276,16 @@ func TestIncludedExcludedFiles(t *testing.T) {
if !assert.Len(t, events, len(wanted)) {
return
}

got := map[string]bool{}
for _, e := range events {
event := e.MetricSetFields
path, err := event.GetValue("file.path")
if assert.NoError(t, err) {
_, ok := wanted[path.(string)]
assert.True(t, ok)
if assert.NoError(t, err, "Failed to read file.path field") {
got[path.(string)] = true
}
}
assert.Equal(t, wanted, got)
}

func getConfig(path ...string) map[string]interface{} {
Expand Down

0 comments on commit 9841601

Please sign in to comment.