2023-03-22 11:52:48 -05:00
|
|
|
//go:build search
|
|
|
|
// +build search
|
|
|
|
|
2023-09-22 13:49:17 -05:00
|
|
|
package cveinfo_test
|
2023-03-22 11:52:48 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-07-04 03:03:29 -05:00
|
|
|
"io"
|
2023-03-22 11:52:48 -05:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
ispec "github.com/opencontainers/image-spec/specs-go/v1"
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
|
2023-07-04 03:03:29 -05:00
|
|
|
"zotregistry.io/zot/pkg/api/config"
|
2023-03-22 11:52:48 -05:00
|
|
|
cveinfo "zotregistry.io/zot/pkg/extensions/search/cve"
|
|
|
|
"zotregistry.io/zot/pkg/log"
|
2023-07-18 12:27:26 -05:00
|
|
|
mTypes "zotregistry.io/zot/pkg/meta/types"
|
2023-03-22 11:52:48 -05:00
|
|
|
"zotregistry.io/zot/pkg/scheduler"
|
|
|
|
"zotregistry.io/zot/pkg/storage"
|
2023-09-27 13:34:48 -05:00
|
|
|
test "zotregistry.io/zot/pkg/test/common"
|
2023-03-22 11:52:48 -05:00
|
|
|
"zotregistry.io/zot/pkg/test/mocks"
|
|
|
|
)
|
|
|
|
|
2023-09-22 13:49:17 -05:00
|
|
|
func TestCVEDBGenerator(t *testing.T) {
|
|
|
|
Convey("Test CVE DB task scheduler reset", t, func() {
|
2023-03-22 11:52:48 -05:00
|
|
|
logFile, err := os.CreateTemp(t.TempDir(), "zot-log*.txt")
|
|
|
|
logPath := logFile.Name()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
defer os.Remove(logFile.Name()) // clean up
|
|
|
|
|
2023-07-04 03:03:29 -05:00
|
|
|
logger := log.NewLogger("debug", logPath)
|
|
|
|
writers := io.MultiWriter(os.Stdout, logFile)
|
|
|
|
logger.Logger = logger.Output(writers)
|
|
|
|
|
|
|
|
cfg := config.New()
|
|
|
|
cfg.Scheduler = &config.SchedulerConfig{NumWorkers: 3}
|
|
|
|
sch := scheduler.NewScheduler(cfg, logger)
|
2023-03-22 11:52:48 -05:00
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
metaDB := &mocks.MetaDBMock{
|
2023-10-30 15:06:04 -05:00
|
|
|
GetRepoMetaFn: func(ctx context.Context, repo string) (mTypes.RepoMeta, error) {
|
|
|
|
return mTypes.RepoMeta{
|
2023-07-18 12:27:26 -05:00
|
|
|
Tags: map[string]mTypes.Descriptor{
|
2023-03-22 11:52:48 -05:00
|
|
|
"tag": {MediaType: ispec.MediaTypeImageIndex},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
storeController := storage.StoreController{
|
2023-06-15 17:07:28 -05:00
|
|
|
DefaultStore: mocks.MockedImageStore{
|
|
|
|
RootDirFn: func() string {
|
|
|
|
return t.TempDir()
|
|
|
|
},
|
|
|
|
},
|
2023-03-22 11:52:48 -05:00
|
|
|
}
|
|
|
|
|
2023-09-22 13:49:17 -05:00
|
|
|
cveScanner := cveinfo.NewScanner(storeController, metaDB, "ghcr.io/project-zot/trivy-db", "", logger)
|
|
|
|
generator := cveinfo.NewDBUpdateTaskGenerator(time.Minute, cveScanner, logger)
|
2023-03-22 11:52:48 -05:00
|
|
|
|
|
|
|
sch.SubmitGenerator(generator, 12000*time.Millisecond, scheduler.HighPriority)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2023-07-04 03:03:29 -05:00
|
|
|
|
2023-03-22 11:52:48 -05:00
|
|
|
sch.RunScheduler(ctx)
|
|
|
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// Wait for trivy db to download
|
2023-09-27 13:34:48 -05:00
|
|
|
found, err := test.ReadLogFileAndCountStringOccurence(logPath,
|
2023-09-17 17:12:20 -05:00
|
|
|
"DB update completed, next update scheduled", 140*time.Second, 2)
|
2023-03-22 11:52:48 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(found, ShouldBeTrue)
|
|
|
|
})
|
|
|
|
}
|