From 47546025421fa6cfb3945799968313b18ad82783 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?No=C3=A9mi=20V=C3=A1nyi?= Date: Thu, 8 Oct 2020 11:51:10 +0200 Subject: [PATCH] Fix flaky FSWatch/FSScanner tests (#21625) ## What does this PR do? Unit tests in for `fswatch.go` do not depend on the order of the returned events anymore. Closes #21489 --- filebeat/input/filestream/fswatch_test.go | 32 ++++--------------- .../filestream/fswatch_test_non_windows.go | 2 +- 2 files changed, 8 insertions(+), 26 deletions(-) diff --git a/filebeat/input/filestream/fswatch_test.go b/filebeat/input/filestream/fswatch_test.go index d6286a273eb..2435fad1500 100644 --- a/filebeat/input/filestream/fswatch_test.go +++ b/filebeat/input/filestream/fswatch_test.go @@ -38,8 +38,6 @@ var ( ) func TestFileScanner(t *testing.T) { - t.Skip("Flaky test: https://github.com/elastic/beats/issues/21489") - testCases := map[string]struct { paths []string excludedFiles []match.Matcher @@ -89,7 +87,7 @@ func TestFileScanner(t *testing.T) { for p, _ := range files { paths = append(paths, p) } - assert.True(t, checkIfSameContents(test.expectedFiles, paths)) + assert.ElementsMatch(t, paths, test.expectedFiles) }) } } @@ -116,26 +114,7 @@ func removeFilesOfScannerTest(t *testing.T) { } } -// only handles sets -func checkIfSameContents(one, other []string) bool { - if len(one) != len(other) { - return false - } - - mustFind := len(one) - for _, oneElem := range one { - for _, otherElem := range other { - if oneElem == otherElem { - mustFind-- - } - } - } - return mustFind == 0 -} - func TestFileWatchNewDeleteModified(t *testing.T) { - t.Skip("Flaky test: https://github.com/elastic/beats/issues/21489") - oldTs := time.Now() newTs := oldTs.Add(5 * time.Second) testCases := map[string]struct { @@ -226,10 +205,13 @@ func TestFileWatchNewDeleteModified(t *testing.T) { go w.watch(context.Background()) - for _, expectedEvent := range test.expectedEvents { - evt := w.Event() - assert.Equal(t, expectedEvent, evt) + count := len(test.expectedEvents) + actual := make([]loginp.FSEvent, count) + for i := 0; i < count; i++ { + actual[i] = w.Event() } + + assert.ElementsMatch(t, actual, test.expectedEvents) }) } } diff --git a/filebeat/input/filestream/fswatch_test_non_windows.go b/filebeat/input/filestream/fswatch_test_non_windows.go index eecfeddf930..3c316efdfc9 100644 --- a/filebeat/input/filestream/fswatch_test_non_windows.go +++ b/filebeat/input/filestream/fswatch_test_non_windows.go @@ -88,7 +88,7 @@ func TestFileScannerSymlinks(t *testing.T) { for p, _ := range files { paths = append(paths, p) } - assert.Equal(t, test.expectedFiles, paths) + assert.ElementsMatch(t, test.expectedFiles, paths) }) } }