From 2496fef3c20f5d7e86545d1c8d219fc8e32d6d20 Mon Sep 17 00:00:00 2001 From: Petu Eusebiu Date: Thu, 14 Jul 2022 13:03:25 +0300 Subject: [PATCH] Fix data race on trivydb download in tests. Multiple go routines downloading trivy db triggers data race on trivy internal db.Path(). In each go routine wait for db download to start. closes #636 Signed-off-by: Petu Eusebiu --- pkg/cli/extensions_test.go | 15 ++++++++++++--- pkg/test/common.go | 10 ++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/pkg/cli/extensions_test.go b/pkg/cli/extensions_test.go index 161c6cc1..33256cc6 100644 --- a/pkg/cli/extensions_test.go +++ b/pkg/cli/extensions_test.go @@ -484,7 +484,10 @@ func TestServeSearchEnabled(t *testing.T) { } }` - data, err := runCLIWithConfig(t.TempDir(), content) + tempDir := t.TempDir() + data, err := runCLIWithConfig(tempDir, content) + // to avoid data race when multiple go routines write to trivy DB instance. + WaitTillTrivyDBDownloadStarted(tempDir) So(err, ShouldBeNil) So(data, ShouldContainSubstring, "\"Extensions\":{\"Search\":{\"CVE\":{\"UpdateInterval\":86400000000000},\"Enable\":true},\"Sync\":null,\"Metrics\":null,\"Scrub\":null}") //nolint:lll // gofumpt conflicts with lll @@ -519,7 +522,10 @@ func TestServeSearchEnabledCVE(t *testing.T) { } }` - data, err := runCLIWithConfig(t.TempDir(), content) + tempDir := t.TempDir() + data, err := runCLIWithConfig(tempDir, content) + // to avoid data race when multiple go routines write to trivy DB instance. + WaitTillTrivyDBDownloadStarted(tempDir) So(err, ShouldBeNil) // Even if in config we specified updateInterval=1h, the minimum interval is 2h So(data, ShouldContainSubstring, @@ -555,7 +561,10 @@ func TestServeSearchEnabledNoCVE(t *testing.T) { } }` - data, err := runCLIWithConfig(t.TempDir(), content) + tempDir := t.TempDir() + data, err := runCLIWithConfig(tempDir, content) + // to avoid data race when multiple go routines write to trivy DB instance. + WaitTillTrivyDBDownloadStarted(tempDir) So(err, ShouldBeNil) So(data, ShouldContainSubstring, "\"Extensions\":{\"Search\":{\"CVE\":{\"UpdateInterval\":86400000000000},\"Enable\":true},\"Sync\":null,\"Metrics\":null,\"Scrub\":null}") //nolint:lll // gofumpt conflicts with lll diff --git a/pkg/test/common.go b/pkg/test/common.go index ac8b29d8..955d40e5 100644 --- a/pkg/test/common.go +++ b/pkg/test/common.go @@ -140,6 +140,16 @@ func WaitTillServerReady(url string) { } } +func WaitTillTrivyDBDownloadStarted(rootDir string) { + for { + if _, err := os.Stat(path.Join(rootDir, "trivy.db")); err == nil { + break + } + + time.Sleep(SleepTime) + } +} + // Adapted from https://gist.github.com/dopey/c69559607800d2f2f90b1b1ed4e550fb func randomString(n int) string { const letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-"