Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: TestPopulateStorageMetrics fails occasionally in CI #2042

Merged
merged 1 commit into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions pkg/extensions/monitoring/monitoring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package monitoring_test
import (
"context"
"fmt"
"io"
"math/rand"
"net/http"
"os"
Expand Down Expand Up @@ -435,16 +436,27 @@ func TestPopulateStorageMetrics(t *testing.T) {
Prometheus: &extconf.PrometheusConfig{Path: "/metrics"},
}

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)
So(ctlr, ShouldNotBeNil)
ctlr.Log.Logger = ctlr.Log.Output(writers)

cm := test.NewControllerManager(ctlr)
cm.StartAndWait(port)
defer cm.StopServer()

// write a couple of images
srcStorageCtlr := ociutils.GetDefaultStoreController(rootDir, ctlr.Log)
err := WriteImageToFileSystem(CreateDefaultImage(), "alpine", "0.0.1", srcStorageCtlr)
err = WriteImageToFileSystem(CreateDefaultImage(), "alpine", "0.0.1", srcStorageCtlr)
So(err, ShouldBeNil)
err = WriteImageToFileSystem(CreateDefaultImage(), "busybox", "0.0.1", srcStorageCtlr)
So(err, ShouldBeNil)
Expand All @@ -462,7 +474,16 @@ func TestPopulateStorageMetrics(t *testing.T) {

sch.SubmitGenerator(generator, time.Duration(0), scheduler.LowPriority)

time.Sleep(5 * time.Second)
// Wait for storage metrics to update
found, err := test.ReadLogFileAndSearchString(logPath,
"monitoring: computed storage usage for repo alpine", time.Minute)
So(err, ShouldBeNil)
So(found, ShouldBeTrue)
found, err = test.ReadLogFileAndSearchString(logPath,
"monitoring: computed storage usage for repo busybox", time.Minute)
So(err, ShouldBeNil)
So(found, ShouldBeTrue)

cancel()
alpineSize, err := monitoring.GetDirSize(path.Join(rootDir, "alpine"))
So(err, ShouldBeNil)
Expand Down
7 changes: 5 additions & 2 deletions pkg/storage/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -1095,7 +1095,7 @@ func (gen *StorageMetricsInitGenerator) Next() (scheduler.Task, error) {
}
gen.lastRepo = repo

return NewStorageMetricsTask(gen.ImgStore, gen.Metrics, repo), nil
return NewStorageMetricsTask(gen.ImgStore, gen.Metrics, repo, gen.Log), nil
}

func (gen *StorageMetricsInitGenerator) IsDone() bool {
Expand All @@ -1116,16 +1116,19 @@ type smTask struct {
imgStore storageTypes.ImageStore
metrics monitoring.MetricServer
repo string
log zlog.Logger
}

func NewStorageMetricsTask(imgStore storageTypes.ImageStore, metrics monitoring.MetricServer, repo string,
log zlog.Logger,
) *smTask {
return &smTask{imgStore, metrics, repo}
return &smTask{imgStore, metrics, repo, log}
}

func (smt *smTask) DoWork(ctx context.Context) error {
// run task
monitoring.SetStorageUsage(smt.metrics, smt.imgStore.RootDir(), smt.repo)
smt.log.Debug().Msg("monitoring: computed storage usage for repo " + smt.repo)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.Str("repo", smt.repo).Msg("...")

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, but for testing purposes where we wait for some specific string messages in the logs before proceeding further and asserting, we need the message in that form unless we will start using some json go libraries for better log parsing


return nil
}