zotregistry.dev/zot@v1.4.4-0.20240314164342-eec277e14d20/pkg/test/common/utils_test.go (about)

     1  package common_test
     2  
     3  import (
     4  	"os"
     5  	"path"
     6  	"testing"
     7  	"time"
     8  
     9  	. "github.com/smartystreets/goconvey/convey"
    10  
    11  	"zotregistry.dev/zot/pkg/api"
    12  	"zotregistry.dev/zot/pkg/api/config"
    13  	tcommon "zotregistry.dev/zot/pkg/test/common"
    14  )
    15  
    16  func TestWaitTillTrivyDBDownloadStarted(t *testing.T) {
    17  	Convey("finishes successfully", t, func() {
    18  		tempDir := t.TempDir()
    19  		go func() {
    20  			tcommon.WaitTillTrivyDBDownloadStarted(tempDir)
    21  		}()
    22  
    23  		time.Sleep(tcommon.SleepTime)
    24  
    25  		_, err := os.Create(path.Join(tempDir, "trivy.db"))
    26  		So(err, ShouldBeNil)
    27  	})
    28  }
    29  
    30  func TestControllerManager(t *testing.T) {
    31  	Convey("Test StartServer Init() panic", t, func() {
    32  		port := tcommon.GetFreePort()
    33  
    34  		conf := config.New()
    35  		conf.HTTP.Port = port
    36  
    37  		ctlr := api.NewController(conf)
    38  		ctlrManager := tcommon.NewControllerManager(ctlr)
    39  
    40  		// No storage configured
    41  		So(func() { ctlrManager.StartServer() }, ShouldPanic)
    42  	})
    43  
    44  	Convey("Test RunServer panic", t, func() {
    45  		tempDir := t.TempDir()
    46  
    47  		// Invalid port
    48  		conf := config.New()
    49  		conf.HTTP.Port = "999999"
    50  		conf.Storage.RootDirectory = tempDir
    51  
    52  		ctlr := api.NewController(conf)
    53  		ctlrManager := tcommon.NewControllerManager(ctlr)
    54  
    55  		err := ctlr.Init()
    56  		So(err, ShouldBeNil)
    57  
    58  		So(func() { ctlrManager.RunServer() }, ShouldPanic)
    59  	})
    60  }