diff --git a/pkg/cli/cve_cmd_test.go b/pkg/cli/cve_cmd_test.go index b6fc970d..b55d9b44 100644 --- a/pkg/cli/cve_cmd_test.go +++ b/pkg/cli/cve_cmd_test.go @@ -7,6 +7,7 @@ import ( "bytes" "context" "fmt" + "io" "os" "path" "regexp" @@ -369,7 +370,18 @@ func TestServerCVEResponseGQL(t *testing.T) { Search: searchConfig, } + logFile, err := os.CreateTemp(t.TempDir(), "zot-log*.txt") + if err != nil { + panic(err) + } + + logPath := logFile.Name() + defer os.Remove(logPath) + + writers := io.MultiWriter(os.Stdout, logFile) + ctlr := api.NewController(conf) + ctlr.Log.Logger = ctlr.Log.Output(writers) go func(controller *api.Controller) { // this blocks @@ -386,7 +398,11 @@ func TestServerCVEResponseGQL(t *testing.T) { time.Sleep(100 * time.Millisecond) } - time.Sleep(90 * time.Second) + + _, err = test.ReadLogFileAndSearchString(logPath, "DB update completed, next update scheduled", 90*time.Second) + if err != nil { + panic(err) + } defer func(controller *api.Controller) { ctx := context.Background() @@ -641,7 +657,18 @@ func TestNegativeServerResponse(t *testing.T) { Search: searchConfig, } + logFile, err := os.CreateTemp(t.TempDir(), "zot-log*.txt") + if err != nil { + panic(err) + } + + logPath := logFile.Name() + defer os.Remove(logPath) + + writers := io.MultiWriter(os.Stdout, logFile) + ctlr := api.NewController(conf) + ctlr.Log.Logger = ctlr.Log.Output(writers) go func(controller *api.Controller) { // this blocks @@ -658,7 +685,10 @@ func TestNegativeServerResponse(t *testing.T) { time.Sleep(100 * time.Millisecond) } - time.Sleep(90 * time.Second) + _, err = test.ReadLogFileAndSearchString(logPath, "CVE config not provided, skipping CVE update", 90*time.Second) + if err != nil { + panic(err) + } defer func(controller *api.Controller) { ctx := context.Background() @@ -714,7 +744,18 @@ func TestNegativeServerResponse(t *testing.T) { Search: searchConfig, } + logFile, err := os.CreateTemp(t.TempDir(), "zot-log*.txt") + if err != nil { + panic(err) + } + + logPath := logFile.Name() + defer os.Remove(logPath) + + writers := io.MultiWriter(os.Stdout, logFile) + ctlr := api.NewController(conf) + ctlr.Log.Logger = ctlr.Log.Output(writers) go func(controller *api.Controller) { // this blocks @@ -731,7 +772,10 @@ func TestNegativeServerResponse(t *testing.T) { time.Sleep(100 * time.Millisecond) } - time.Sleep(90 * time.Second) + _, err = test.ReadLogFileAndSearchString(logPath, "DB update completed, next update scheduled", 90*time.Second) + if err != nil { + panic(err) + } defer func(controller *api.Controller) { ctx := context.Background() @@ -778,7 +822,18 @@ func TestServerCVEResponse(t *testing.T) { Search: searchConfig, } + logFile, err := os.CreateTemp(t.TempDir(), "zot-log*.txt") + if err != nil { + panic(err) + } + + logPath := logFile.Name() + defer os.Remove(logPath) + + writers := io.MultiWriter(os.Stdout, logFile) + ctlr := api.NewController(conf) + ctlr.Log.Logger = ctlr.Log.Output(writers) go func(controller *api.Controller) { // this blocks @@ -795,7 +850,11 @@ func TestServerCVEResponse(t *testing.T) { time.Sleep(100 * time.Millisecond) } - time.Sleep(90 * time.Second) + + _, err = test.ReadLogFileAndSearchString(logPath, "DB update completed, next update scheduled", 90*time.Second) + if err != nil { + panic(err) + } defer func(controller *api.Controller) { ctx := context.Background() diff --git a/pkg/cli/image_cmd_test.go b/pkg/cli/image_cmd_test.go index 06296e4a..2ec1e310 100644 --- a/pkg/cli/image_cmd_test.go +++ b/pkg/cli/image_cmd_test.go @@ -8,6 +8,7 @@ import ( "context" "encoding/json" "fmt" + "io" "log" "os" "path" @@ -1269,7 +1270,18 @@ func TestServerResponseGQLWithoutPermissions(t *testing.T) { Search: searchConfig, } + logFile, err := os.CreateTemp(t.TempDir(), "zot-log*.txt") + if err != nil { + panic(err) + } + + logPath := logFile.Name() + defer os.Remove(logPath) + + writers := io.MultiWriter(os.Stdout, logFile) + ctlr := api.NewController(conf) + ctlr.Log.Logger = ctlr.Log.Output(writers) go func(controller *api.Controller) { // this blocks @@ -1286,7 +1298,11 @@ func TestServerResponseGQLWithoutPermissions(t *testing.T) { time.Sleep(100 * time.Millisecond) } - time.Sleep(90 * time.Second) + + _, err = test.ReadLogFileAndSearchString(logPath, "DB update completed, next update scheduled", 90*time.Second) + if err != nil { + panic(err) + } defer func(controller *api.Controller) { err = os.Chmod(path.Join(dir, "zot-test", "blobs"), 0o777) diff --git a/pkg/extensions/search/cve/cve_test.go b/pkg/extensions/search/cve/cve_test.go index 750a45d7..b473b5c0 100644 --- a/pkg/extensions/search/cve/cve_test.go +++ b/pkg/extensions/search/cve/cve_test.go @@ -8,6 +8,7 @@ import ( "context" "encoding/json" "fmt" + "io" "net/http" "os" "path" @@ -419,7 +420,18 @@ func TestCVESearch(t *testing.T) { Search: searchConfig, } + logFile, err := os.CreateTemp(t.TempDir(), "zot-log*.txt") + if err != nil { + panic(err) + } + + logPath := logFile.Name() + defer os.Remove(logPath) + + writers := io.MultiWriter(os.Stdout, logFile) + ctlr := api.NewController(conf) + ctlr.Log.Logger = ctlr.Log.Output(writers) go func() { // this blocks @@ -438,7 +450,10 @@ func TestCVESearch(t *testing.T) { } // Wait for trivy db to download - time.Sleep(90 * time.Second) + _, err = ReadLogFileAndSearchString(logPath, "DB update completed, next update scheduled", 90*time.Second) + if err != nil { + panic(err) + } defer func() { ctx := context.Background() diff --git a/pkg/test/common.go b/pkg/test/common.go index c203126c..5d938838 100644 --- a/pkg/test/common.go +++ b/pkg/test/common.go @@ -13,6 +13,7 @@ import ( "net/url" "os" "path" + "strings" "time" godigest "github.com/opencontainers/go-digest" @@ -425,3 +426,24 @@ func UploadImage(img Image, baseURL, repo string) error { return err } + +func ReadLogFileAndSearchString(logPath string, stringToMatch string, timeout time.Duration) (bool, error) { + ctx, cancelFunc := context.WithTimeout(context.Background(), timeout) + defer cancelFunc() + + for { + select { + case <-ctx.Done(): + return false, nil + default: + content, err := os.ReadFile(logPath) + if err != nil { + return false, err + } + + if strings.Contains(string(content), stringToMatch) { + return true, nil + } + } + } +} diff --git a/pkg/test/common_test.go b/pkg/test/common_test.go index 843009e3..6d762fab 100644 --- a/pkg/test/common_test.go +++ b/pkg/test/common_test.go @@ -409,6 +409,27 @@ func TestInjectUploadImage(t *testing.T) { }) } +func TestReadLogFileAndSearchString(t *testing.T) { + logFile, err := os.CreateTemp(t.TempDir(), "zot-log*.txt") + if err != nil { + panic(err) + } + + logPath := logFile.Name() + defer os.Remove(logPath) + + Convey("Invalid path", t, func() { + _, err = test.ReadLogFileAndSearchString("invalidPath", "DB update completed, next update scheduled", 90*time.Second) + So(err, ShouldNotBeNil) + }) + + Convey("Time too short", t, func() { + ok, err := test.ReadLogFileAndSearchString(logPath, "invalid string", time.Microsecond) + So(err, ShouldBeNil) + So(ok, ShouldBeFalse) + }) +} + func startServer(c *api.Controller) { // this blocks ctx := context.Background()