From 49e4d93f422f5e39c6698ce94fd4be6b80481d60 Mon Sep 17 00:00:00 2001 From: peusebiu Date: Thu, 6 Jul 2023 14:17:49 +0300 Subject: [PATCH] fix(test): TestConfigReloader, wait for trivy db download (#1543) Signed-off-by: Petu Eusebiu --- pkg/api/controller.go | 2 ++ pkg/cli/config_reloader_test.go | 21 +++++++++++++++++---- pkg/extensions/search/cve/trivy/scanner.go | 8 ++++++-- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/pkg/api/controller.go b/pkg/api/controller.go index 1ce0969b..7d995487 100644 --- a/pkg/api/controller.go +++ b/pkg/api/controller.go @@ -300,6 +300,8 @@ func (c *Controller) LoadNewConfig(reloadCtx context.Context, config *config.Con c.Config.Extensions = nil } + c.InitCVEInfo() + c.StartBackgroundTasks(reloadCtx) c.Log.Info().Interface("reloaded params", c.Config.Sanitize()). diff --git a/pkg/cli/config_reloader_test.go b/pkg/cli/config_reloader_test.go index 3c6d030b..a1b1ee05 100644 --- a/pkg/cli/config_reloader_test.go +++ b/pkg/cli/config_reloader_test.go @@ -1,3 +1,6 @@ +//go:build search +// +build search + package cli_test import ( @@ -322,7 +325,7 @@ func TestConfigReloader(t *testing.T) { "cve": { "updateInterval": "24h", "trivy": { - "DBRepository": "ghcr.io/aquasecurity/trivy-db" + "DBRepository": "unreachable/trivy/url1" } } }, @@ -367,7 +370,7 @@ func TestConfigReloader(t *testing.T) { "cve": { "updateInterval": "5h", "trivy": { - "DBRepository": "ghcr.io/project-zot/trivy-db" + "DBRepository": "another/unreachable/trivy/url2" } } } @@ -387,7 +390,12 @@ func TestConfigReloader(t *testing.T) { So(err, ShouldBeNil) // wait for config reload - time.Sleep(2 * time.Second) + time.Sleep(5 * time.Second) + + found, err := test.ReadLogFileAndSearchString(logFile.Name(), + "Error downloading Trivy DB to destination dir", 30*time.Second) + So(err, ShouldBeNil) + So(found, ShouldBeTrue) data, err := os.ReadFile(logFile.Name()) So(err, ShouldBeNil) @@ -397,7 +405,12 @@ func TestConfigReloader(t *testing.T) { So(string(data), ShouldContainSubstring, "loaded new configuration settings") So(string(data), ShouldContainSubstring, "\"UpdateInterval\":18000000000000") So(string(data), ShouldContainSubstring, "\"Scrub\":null") - So(string(data), ShouldContainSubstring, "\"DBRepository\":\"ghcr.io/project-zot/trivy-db\"") + So(string(data), ShouldContainSubstring, "\"DBRepository\":\"another/unreachable/trivy/url2\"") + // matching log message when it errors out, test that indeed the download will try the second url + found, err = test.ReadLogFileAndSearchString(logFile.Name(), + "\"dbRepository\":\"another/unreachable/trivy/url2\",\"goroutine", 1*time.Minute) + So(err, ShouldBeNil) + So(found, ShouldBeTrue) }) Convey("reload bad config", t, func(c C) { diff --git a/pkg/extensions/search/cve/trivy/scanner.go b/pkg/extensions/search/cve/trivy/scanner.go index fc47a704..1e006e0f 100644 --- a/pkg/extensions/search/cve/trivy/scanner.go +++ b/pkg/extensions/search/cve/trivy/scanner.go @@ -497,9 +497,12 @@ func (scanner Scanner) updateDB(dbDir string) error { registryOpts := fanalTypes.RegistryOptions{Insecure: false} + scanner.log.Debug().Str("dbDir", dbDir).Msg("Started downloading Trivy DB to destination dir") + err := operation.DownloadDB(ctx, "dev", dbDir, scanner.dbRepository, false, false, registryOpts) if err != nil { - scanner.log.Error().Err(err).Str("dbDir", dbDir).Msg("Error downloading Trivy DB to destination dir") + scanner.log.Error().Err(err).Str("dbDir", dbDir). + Str("dbRepository", scanner.dbRepository).Msg("Error downloading Trivy DB to destination dir") return err } @@ -508,7 +511,8 @@ func (scanner Scanner) updateDB(dbDir string) error { javadb.Init(dbDir, scanner.javaDBRepository, false, false, registryOpts.Insecure) if err := javadb.Update(); err != nil { - scanner.log.Error().Err(err).Str("dbDir", dbDir).Msg("Error downloading Trivy Java DB to destination dir") + scanner.log.Error().Err(err).Str("dbDir", dbDir). + Str("javaDBRepository", scanner.javaDBRepository).Msg("Error downloading Trivy Java DB to destination dir") return err }