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 <peusebiu@cisco.com>
This commit is contained in:
Petu Eusebiu 2022-07-14 13:03:25 +03:00 committed by Ramkumar Chinchani
parent 003de3a80a
commit 2496fef3c2
2 changed files with 22 additions and 3 deletions

View File

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

View File

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