fix: replace time.sleep() with checking logs (#899)

Signed-off-by: Lisca Ana-Roberta <ana.kagome@yahoo.com>
This commit is contained in:
Lisca Ana-Roberta
2022-10-21 21:17:06 +03:00
committed by GitHub
parent 00e65bd32b
commit 26d982becb
5 changed files with 139 additions and 6 deletions
+22
View File
@@ -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
}
}
}
}
+21
View File
@@ -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()