mirror of
https://github.com/project-zot/zot.git
synced 2026-06-20 06:37:56 +08:00
refactor(cve): improve CVE test time by mocking trivy (#1184)
- refactor(cve): remove the global of type cveinfo.CveInfo from the extensions package Replace it with an attribute on controller level - refactor(controller): extract initialization logic from controller.Run() - test(cve): mock cve scanner in cli tests Signed-off-by: Andrei Aaron <aaaron@luxoft.com>
This commit is contained in:
+14
-12
@@ -187,6 +187,7 @@ func CopyTestFiles(sourceDir, destDir string) {
|
||||
}
|
||||
|
||||
type Controller interface {
|
||||
Init(ctx context.Context) error
|
||||
Run(ctx context.Context) error
|
||||
Shutdown()
|
||||
GetPort() int
|
||||
@@ -196,14 +197,22 @@ type ControllerManager struct {
|
||||
controller Controller
|
||||
}
|
||||
|
||||
func (cm *ControllerManager) RunServer(ctx context.Context) {
|
||||
// Useful to be able to call in the same goroutine for testing purposes
|
||||
if err := cm.controller.Run(ctx); !errors.Is(err, http.ErrServerClosed) {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (cm *ControllerManager) StartServer() {
|
||||
// this blocks
|
||||
ctx := context.Background()
|
||||
|
||||
if err := cm.controller.Init(ctx); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
go func() {
|
||||
if err := cm.controller.Run(ctx); err != nil {
|
||||
return
|
||||
}
|
||||
cm.RunServer(ctx)
|
||||
}()
|
||||
}
|
||||
|
||||
@@ -217,14 +226,7 @@ func (cm *ControllerManager) WaitServerToBeReady(port string) {
|
||||
}
|
||||
|
||||
func (cm *ControllerManager) StartAndWait(port string) {
|
||||
// this blocks
|
||||
ctx := context.Background()
|
||||
|
||||
go func() {
|
||||
if err := cm.controller.Run(ctx); err != nil {
|
||||
return
|
||||
}
|
||||
}()
|
||||
cm.StartServer()
|
||||
|
||||
url := GetBaseURL(port)
|
||||
WaitTillServerReady(url)
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
package test_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
@@ -192,6 +193,40 @@ func TestWaitTillTrivyDBDownloadStarted(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestControllerManager(t *testing.T) {
|
||||
Convey("Test StartServer Init() panic", t, func() {
|
||||
port := test.GetFreePort()
|
||||
|
||||
conf := config.New()
|
||||
conf.HTTP.Port = port
|
||||
|
||||
ctlr := api.NewController(conf)
|
||||
ctlrManager := test.NewControllerManager(ctlr)
|
||||
|
||||
// No storage configured
|
||||
So(func() { ctlrManager.StartServer() }, ShouldPanic)
|
||||
})
|
||||
|
||||
Convey("Test RunServer panic", t, func() {
|
||||
tempDir := t.TempDir()
|
||||
|
||||
// Invalid port
|
||||
conf := config.New()
|
||||
conf.HTTP.Port = "999999"
|
||||
conf.Storage.RootDirectory = tempDir
|
||||
|
||||
ctlr := api.NewController(conf)
|
||||
ctlrManager := test.NewControllerManager(ctlr)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
err := ctlr.Init(ctx)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(func() { ctlrManager.RunServer(ctx) }, ShouldPanic)
|
||||
})
|
||||
}
|
||||
|
||||
func TestUploadArtifact(t *testing.T) {
|
||||
Convey("Put request results in an error", t, func() {
|
||||
port := test.GetFreePort()
|
||||
|
||||
Reference in New Issue
Block a user