fix(trivy): consistent coverage for reset method + longer wait time between retries (#1272)

Signed-off-by: Ana-Roberta Lisca <ana.kagome@yahoo.com>
This commit is contained in:
Lisca Ana-Roberta
2023-03-22 18:52:48 +02:00
committed by GitHub
parent a2c34808a5
commit 5f026d2e80
4 changed files with 140 additions and 8 deletions
+23
View File
@@ -1049,6 +1049,29 @@ func ReadLogFileAndSearchString(logPath string, stringToMatch string, timeout ti
}
}
func ReadLogFileAndCountStringOccurence(logPath string, stringToMatch string,
timeout time.Duration, count int,
) (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.Count(string(content), stringToMatch) >= count {
return true, nil
}
}
}
}
func CopyFile(sourceFilePath, destFilePath string) error {
destFile, err := os.Create(destFilePath)
if err != nil {
+33
View File
@@ -725,6 +725,39 @@ func TestReadLogFileAndSearchString(t *testing.T) {
})
}
func TestReadLogFileAndCountStringOccurence(t *testing.T) {
logFile, err := os.CreateTemp(t.TempDir(), "zot-log*.txt")
if err != nil {
panic(err)
}
_, err = logFile.Write([]byte("line1\n line2\n line3 line1 line2\n line1"))
if err != nil {
panic(err)
}
logPath := logFile.Name()
defer os.Remove(logPath)
Convey("Invalid path", t, func() {
_, err = test.ReadLogFileAndCountStringOccurence("invalidPath",
"DB update completed, next update scheduled", 90*time.Second, 1)
So(err, ShouldNotBeNil)
})
Convey("Time too short", t, func() {
ok, err := test.ReadLogFileAndCountStringOccurence(logPath, "invalid string", time.Microsecond, 1)
So(err, ShouldBeNil)
So(ok, ShouldBeFalse)
})
Convey("Count occurrence working", t, func() {
ok, err := test.ReadLogFileAndCountStringOccurence(logPath, "line1", 90*time.Second, 3)
So(err, ShouldBeNil)
So(ok, ShouldBeTrue)
})
}
func TestInjectUploadImageWithBasicAuth(t *testing.T) {
Convey("Inject failures for unreachable lines", t, func() {
port := test.GetFreePort()