0
Fork 0
mirror of https://github.com/project-zot/zot.git synced 2024-12-16 21:56:37 -05:00

fix: TestPopulateStorageMetrics fails occasionally in CI (#2042)

Signed-off-by: Alexei Dodon <adodon@cisco.com>
This commit is contained in:
Alexei Dodon 2023-11-15 02:22:24 +02:00 committed by GitHub
parent 272eb7cc43
commit dd079bf9a3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 4 deletions

View file

@ -6,6 +6,7 @@ package monitoring_test
import ( import (
"context" "context"
"fmt" "fmt"
"io"
"math/rand" "math/rand"
"net/http" "net/http"
"os" "os"
@ -435,8 +436,19 @@ func TestPopulateStorageMetrics(t *testing.T) {
Prometheus: &extconf.PrometheusConfig{Path: "/metrics"}, 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) ctlr := api.NewController(conf)
So(ctlr, ShouldNotBeNil) So(ctlr, ShouldNotBeNil)
ctlr.Log.Logger = ctlr.Log.Output(writers)
cm := test.NewControllerManager(ctlr) cm := test.NewControllerManager(ctlr)
cm.StartAndWait(port) cm.StartAndWait(port)
@ -444,7 +456,7 @@ func TestPopulateStorageMetrics(t *testing.T) {
// write a couple of images // write a couple of images
srcStorageCtlr := ociutils.GetDefaultStoreController(rootDir, ctlr.Log) 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) So(err, ShouldBeNil)
err = WriteImageToFileSystem(CreateDefaultImage(), "busybox", "0.0.1", srcStorageCtlr) err = WriteImageToFileSystem(CreateDefaultImage(), "busybox", "0.0.1", srcStorageCtlr)
So(err, ShouldBeNil) So(err, ShouldBeNil)
@ -462,7 +474,16 @@ func TestPopulateStorageMetrics(t *testing.T) {
sch.SubmitGenerator(generator, time.Duration(0), scheduler.LowPriority) 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() cancel()
alpineSize, err := monitoring.GetDirSize(path.Join(rootDir, "alpine")) alpineSize, err := monitoring.GetDirSize(path.Join(rootDir, "alpine"))
So(err, ShouldBeNil) So(err, ShouldBeNil)

View file

@ -1095,7 +1095,7 @@ func (gen *StorageMetricsInitGenerator) Next() (scheduler.Task, error) {
} }
gen.lastRepo = repo 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 { func (gen *StorageMetricsInitGenerator) IsDone() bool {
@ -1116,16 +1116,19 @@ type smTask struct {
imgStore storageTypes.ImageStore imgStore storageTypes.ImageStore
metrics monitoring.MetricServer metrics monitoring.MetricServer
repo string repo string
log zlog.Logger
} }
func NewStorageMetricsTask(imgStore storageTypes.ImageStore, metrics monitoring.MetricServer, repo string, func NewStorageMetricsTask(imgStore storageTypes.ImageStore, metrics monitoring.MetricServer, repo string,
log zlog.Logger,
) *smTask { ) *smTask {
return &smTask{imgStore, metrics, repo} return &smTask{imgStore, metrics, repo, log}
} }
func (smt *smTask) DoWork(ctx context.Context) error { func (smt *smTask) DoWork(ctx context.Context) error {
// run task // run task
monitoring.SetStorageUsage(smt.metrics, smt.imgStore.RootDir(), smt.repo) monitoring.SetStorageUsage(smt.metrics, smt.imgStore.RootDir(), smt.repo)
smt.log.Debug().Msg("monitoring: computed storage usage for repo " + smt.repo)
return nil return nil
} }