Skip to content

Commit

Permalink
Read log path and verify content separately to avoid failed tests
Browse files Browse the repository at this point in the history
Signed-off-by: Nicol Draghici <idraghic@cisco.com>
  • Loading branch information
nicoldr authored and rchincha committed Sep 5, 2022
1 parent 8ffb053 commit 6471add
Showing 1 changed file with 114 additions and 45 deletions.
159 changes: 114 additions & 45 deletions pkg/cli/extensions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@
package cli_test

import (
"context"
"fmt"
"net/http"
"os"
"strings"
"testing"
"time"

. "github.com/smartystreets/goconvey/convey"
"gopkg.in/resty.v1"
"zotregistry.io/zot/pkg/cli"
. "zotregistry.io/zot/pkg/test"
)

const readLogFileTimeout = 5 * time.Second

func TestServeExtensions(t *testing.T) {
oldArgs := os.Args

Expand Down Expand Up @@ -311,9 +316,12 @@ func TestServeSyncExtension(t *testing.T) {
}
}`

data, err := runCLIWithConfig(t.TempDir(), content)
logPath, err := runCLIWithConfig(t.TempDir(), content)
So(err, ShouldBeNil)
data, err := os.ReadFile(logPath)
So(err, ShouldBeNil)
So(data, ShouldContainSubstring,
defer os.Remove(logPath) // clean up
So(string(data), ShouldContainSubstring,
"\"Extensions\":{\"Search\":null,\"Sync\":{\"Enable\":true")
})

Expand Down Expand Up @@ -354,9 +362,12 @@ func TestServeSyncExtension(t *testing.T) {
}
}`

data, err := runCLIWithConfig(t.TempDir(), content)
logPath, err := runCLIWithConfig(t.TempDir(), content)
So(err, ShouldBeNil)
So(data, ShouldContainSubstring,
data, err := os.ReadFile(logPath)
So(err, ShouldBeNil)
defer os.Remove(logPath) // clean up
So(string(data), ShouldContainSubstring,
"\"Extensions\":{\"Search\":null,\"Sync\":{\"Enable\":true")
})

Expand Down Expand Up @@ -387,9 +398,12 @@ func TestServeSyncExtension(t *testing.T) {
}
}`

data, err := runCLIWithConfig(t.TempDir(), content)
logPath, err := runCLIWithConfig(t.TempDir(), content)
So(err, ShouldBeNil)
data, err := os.ReadFile(logPath)
So(err, ShouldBeNil)
So(data, ShouldContainSubstring,
defer os.Remove(logPath) // clean up
So(string(data), ShouldContainSubstring,
"\"Extensions\":{\"Search\":null,\"Sync\":{\"Enable\":false")
})
}
Expand Down Expand Up @@ -419,15 +433,19 @@ func TestServeScrubExtension(t *testing.T) {
}
}`

data, err := runCLIWithConfig(t.TempDir(), content)
logPath, err := runCLIWithConfig(t.TempDir(), content)
So(err, ShouldBeNil)
data, err := os.ReadFile(logPath)
So(err, ShouldBeNil)
defer os.Remove(logPath) // clean up
// Even if in config we specified scrub interval=1h, the minimum interval is 2h
So(data, ShouldContainSubstring,
dataStr := string(data)
So(dataStr, ShouldContainSubstring,
"\"Extensions\":{\"Search\":null,\"Sync\":null,\"Metrics\":null,\"Scrub\":{\"Interval\":3600000000000},\"Lint\":null") //nolint:lll // gofumpt conflicts with lll
So(data, ShouldContainSubstring,
So(dataStr, ShouldContainSubstring,
"Scrub interval set to too-short interval < 2h, changing scrub duration to 2 hours and continuing.")
So(data, ShouldContainSubstring, "Starting periodic background tasks for")
So(data, ShouldContainSubstring, "Finishing periodic background tasks for")
So(dataStr, ShouldContainSubstring, "Starting periodic background tasks for")
So(dataStr, ShouldContainSubstring, "Finishing periodic background tasks for")
})

Convey("scrub not enabled - scrub interval param not set", t, func(c C) {
Expand All @@ -449,12 +467,16 @@ func TestServeScrubExtension(t *testing.T) {
}
}`

data, err := runCLIWithConfig(t.TempDir(), content)
logPath, err := runCLIWithConfig(t.TempDir(), content)
So(err, ShouldBeNil)
data, err := os.ReadFile(logPath)
So(err, ShouldBeNil)
So(data, ShouldContainSubstring,
defer os.Remove(logPath) // clean up
dataStr := string(data)
So(dataStr, ShouldContainSubstring,
"\"Extensions\":{\"Search\":null,\"Sync\":null,\"Metrics\":null,\"Scrub\":null,\"Lint\":null}")
So(data, ShouldContainSubstring, "Scrub config not provided, skipping scrub")
So(data, ShouldNotContainSubstring,
So(dataStr, ShouldContainSubstring, "Scrub config not provided, skipping scrub")
So(dataStr, ShouldNotContainSubstring,
"Scrub interval set to too-short interval < 2h, changing scrub duration to 2 hours and continuing.")
})
}
Expand Down Expand Up @@ -485,9 +507,12 @@ func TestServeLintExtension(t *testing.T) {
}
}`

data, err := runCLIWithConfig(t.TempDir(), content)
logPath, err := runCLIWithConfig(t.TempDir(), content)
So(err, ShouldBeNil)
So(data, ShouldContainSubstring,
data, err := os.ReadFile(logPath)
So(err, ShouldBeNil)
defer os.Remove(logPath) // clean up
So(string(data), ShouldContainSubstring,
"\"Extensions\":{\"Search\":null,\"Sync\":null,\"Metrics\":null,\"Scrub\":null,\"Lint\":{\"Enabled\":true,\"MandatoryAnnotations\":") //nolint:lll // gofumpt conflicts with lll
})

Expand All @@ -511,9 +536,12 @@ func TestServeLintExtension(t *testing.T) {
}
}`

data, err := runCLIWithConfig(t.TempDir(), content)
logPath, err := runCLIWithConfig(t.TempDir(), content)
So(err, ShouldBeNil)
So(data, ShouldContainSubstring,
data, err := os.ReadFile(logPath)
So(err, ShouldBeNil)
defer os.Remove(logPath) // clean up
So(string(data), ShouldContainSubstring,
"\"Extensions\":{\"Search\":null,\"Sync\":null,\"Metrics\":null,\"Scrub\":null,\"Lint\":{\"Enabled\":false,\"MandatoryAnnotations\":null}") //nolint:lll // gofumpt conflicts with lll
})
}
Expand Down Expand Up @@ -543,13 +571,20 @@ func TestServeSearchEnabled(t *testing.T) {
}`

tempDir := t.TempDir()
data, err := runCLIWithConfig(tempDir, content)
logPath, err := runCLIWithConfig(tempDir, content)
So(err, ShouldBeNil)
// to avoid data race when multiple go routines write to trivy DB instance.
WaitTillTrivyDBDownloadStarted(tempDir)
defer os.Remove(logPath) // clean up

substring := "\"Extensions\":{\"Search\":{\"CVE\":{\"UpdateInterval\":86400000000000},\"Enable\":true},\"Sync\":null,\"Metrics\":null,\"Scrub\":null,\"Lint\":null}" //nolint:lll // gofumpt conflicts with lll
found, err := readLogFileAndSearchString(logPath, substring, readLogFileTimeout)
So(found, ShouldBeTrue)
So(err, ShouldBeNil)

found, err = readLogFileAndSearchString(logPath, "updating the CVE database", readLogFileTimeout)
So(found, ShouldBeTrue)
So(err, ShouldBeNil)
So(data, ShouldContainSubstring,
"\"Extensions\":{\"Search\":{\"CVE\":{\"UpdateInterval\":86400000000000},\"Enable\":true},\"Sync\":null,\"Metrics\":null,\"Scrub\":null,\"Lint\":null}") //nolint:lll // gofumpt conflicts with lll
So(data, ShouldContainSubstring, "updating the CVE database")
})
}

Expand Down Expand Up @@ -581,16 +616,25 @@ func TestServeSearchEnabledCVE(t *testing.T) {
}`

tempDir := t.TempDir()
data, err := runCLIWithConfig(tempDir, content)
logPath, err := runCLIWithConfig(tempDir, content)
So(err, ShouldBeNil)
defer os.Remove(logPath) // clean up
// to avoid data race when multiple go routines write to trivy DB instance.
WaitTillTrivyDBDownloadStarted(tempDir)

substring := "\"Extensions\":{\"Search\":{\"CVE\":{\"UpdateInterval\":3600000000000},\"Enable\":true},\"Sync\":null,\"Metrics\":null,\"Scrub\":null,\"Lint\":null}" //nolint:lll // gofumpt conflicts with lll
found, err := readLogFileAndSearchString(logPath, substring, readLogFileTimeout)
So(found, ShouldBeTrue)
So(err, ShouldBeNil)

found, err = readLogFileAndSearchString(logPath, "updating the CVE database", readLogFileTimeout)
So(found, ShouldBeTrue)
So(err, ShouldBeNil)

substring = "CVE update interval set to too-short interval < 2h, changing update duration to 2 hours and continuing." //nolint:lll // gofumpt conflicts with lll
found, err = readLogFileAndSearchString(logPath, substring, readLogFileTimeout)
So(found, ShouldBeTrue)
So(err, ShouldBeNil)
// Even if in config we specified updateInterval=1h, the minimum interval is 2h
So(data, ShouldContainSubstring,
"\"Extensions\":{\"Search\":{\"CVE\":{\"UpdateInterval\":3600000000000},\"Enable\":true},\"Sync\":null,\"Metrics\":null,\"Scrub\":null,\"Lint\":null}") //nolint:lll // gofumpt conflicts with lll
So(data, ShouldContainSubstring, "updating the CVE database")
So(data, ShouldContainSubstring,
"CVE update interval set to too-short interval < 2h, changing update duration to 2 hours and continuing.")
})
}

Expand Down Expand Up @@ -620,13 +664,20 @@ func TestServeSearchEnabledNoCVE(t *testing.T) {
}`

tempDir := t.TempDir()
data, err := runCLIWithConfig(tempDir, content)
logPath, err := runCLIWithConfig(tempDir, content)
So(err, ShouldBeNil)
defer os.Remove(logPath) // clean up
// to avoid data race when multiple go routines write to trivy DB instance.
WaitTillTrivyDBDownloadStarted(tempDir)

substring := "\"Extensions\":{\"Search\":{\"CVE\":{\"UpdateInterval\":86400000000000},\"Enable\":true},\"Sync\":null,\"Metrics\":null,\"Scrub\":null,\"Lint\":null}" //nolint:lll // gofumpt conflicts with lll
found, err := readLogFileAndSearchString(logPath, substring, readLogFileTimeout)
So(found, ShouldBeTrue)
So(err, ShouldBeNil)

found, err = readLogFileAndSearchString(logPath, "updating the CVE database", readLogFileTimeout)
So(found, ShouldBeTrue)
So(err, ShouldBeNil)
So(data, ShouldContainSubstring,
"\"Extensions\":{\"Search\":{\"CVE\":{\"UpdateInterval\":86400000000000},\"Enable\":true},\"Sync\":null,\"Metrics\":null,\"Scrub\":null,\"Lint\":null}") //nolint:lll // gofumpt conflicts with lll
So(data, ShouldContainSubstring, "updating the CVE database")
})
}

Expand Down Expand Up @@ -658,18 +709,41 @@ func TestServeSearchDisabled(t *testing.T) {
}
}`

data, err := runCLIWithConfig(t.TempDir(), content)
logPath, err := runCLIWithConfig(t.TempDir(), content)
So(err, ShouldBeNil)

data, err := os.ReadFile(logPath)
So(err, ShouldBeNil)
So(data, ShouldContainSubstring,
defer os.Remove(logPath) // clean up
dataStr := string(data)
So(dataStr, ShouldContainSubstring,
"\"Extensions\":{\"Search\":{\"CVE\":{\"UpdateInterval\":10800000000000},\"Enable\":false},\"Sync\":null,\"Metrics\":null,\"Scrub\":null,\"Lint\":null}") //nolint:lll // gofumpt conflicts with lll
So(data, ShouldContainSubstring, "CVE config not provided, skipping CVE update")
So(data, ShouldNotContainSubstring,
So(dataStr, ShouldContainSubstring, "CVE config not provided, skipping CVE update")
So(dataStr, ShouldNotContainSubstring,
"CVE update interval set to too-short interval < 2h, changing update duration to 2 hours and continuing.")
})
}

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

// run cli and return output.
func runCLIWithConfig(tempDir string, config string) (string, error) {
port := GetFreePort()
Expand All @@ -680,8 +754,6 @@ func runCLIWithConfig(tempDir string, config string) (string, error) {
return "", err
}

defer os.Remove(logFile.Name()) // clean up

cfgfile, err := os.CreateTemp(tempDir, "zot-test*.json")
if err != nil {
return "", err
Expand Down Expand Up @@ -710,8 +782,5 @@ func runCLIWithConfig(tempDir string, config string) (string, error) {

WaitTillServerReady(baseURL)

data, err := os.ReadFile(logFile.Name())
So(err, ShouldBeNil)

return string(data), nil
return logFile.Name(), nil
}

0 comments on commit 6471add

Please sign in to comment.