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-"