2022-04-27 01:00:20 -05:00
|
|
|
//go:build sync || metrics
|
|
|
|
// +build sync metrics
|
|
|
|
|
|
|
|
package extensions_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
2022-10-20 11:39:20 -05:00
|
|
|
|
2022-04-27 01:00:20 -05:00
|
|
|
"zotregistry.io/zot/pkg/api"
|
|
|
|
"zotregistry.io/zot/pkg/api/config"
|
|
|
|
extconf "zotregistry.io/zot/pkg/extensions/config"
|
|
|
|
"zotregistry.io/zot/pkg/extensions/sync"
|
|
|
|
"zotregistry.io/zot/pkg/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestEnableExtension(t *testing.T) {
|
|
|
|
Convey("Verify log if sync disabled in config", t, func() {
|
|
|
|
globalDir := t.TempDir()
|
|
|
|
port := test.GetFreePort()
|
|
|
|
conf := config.New()
|
|
|
|
falseValue := false
|
|
|
|
|
|
|
|
syncConfig := &sync.Config{
|
|
|
|
Enable: &falseValue,
|
|
|
|
Registries: []sync.RegistryConfig{},
|
|
|
|
}
|
|
|
|
|
|
|
|
// conf.Extensions.Sync.Enable = &falseValue
|
|
|
|
conf.Extensions = &extconf.ExtensionConfig{}
|
|
|
|
conf.Extensions.Sync = syncConfig
|
|
|
|
conf.HTTP.Port = port
|
|
|
|
|
2022-09-02 07:56:02 -05:00
|
|
|
logFile, err := os.CreateTemp(globalDir, "zot-log*.txt")
|
2022-04-27 01:00:20 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
conf.Log.Level = "info"
|
|
|
|
conf.Log.Output = logFile.Name()
|
|
|
|
defer os.Remove(logFile.Name()) // cleanup
|
|
|
|
|
|
|
|
ctlr := api.NewController(conf)
|
2023-01-19 11:54:05 -05:00
|
|
|
ctlrManager := test.NewControllerManager(ctlr)
|
2022-04-27 01:00:20 -05:00
|
|
|
|
2023-01-19 11:54:05 -05:00
|
|
|
defer ctlrManager.StopServer()
|
2022-04-27 01:00:20 -05:00
|
|
|
|
|
|
|
ctlr.Config.Storage.RootDirectory = globalDir
|
|
|
|
|
2023-01-19 11:54:05 -05:00
|
|
|
ctlrManager.StartAndWait(port)
|
2022-04-27 01:00:20 -05:00
|
|
|
|
|
|
|
data, err := os.ReadFile(logFile.Name())
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(string(data), ShouldContainSubstring,
|
|
|
|
"Sync registries config not provided or disabled, skipping sync")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMetricsExtension(t *testing.T) {
|
|
|
|
Convey("Verify Metrics enabled for storage subpaths", t, func() {
|
|
|
|
globalDir := t.TempDir()
|
|
|
|
conf := config.New()
|
|
|
|
port := test.GetFreePort()
|
|
|
|
conf.HTTP.Port = port
|
|
|
|
|
2022-09-02 07:56:02 -05:00
|
|
|
logFile, err := os.CreateTemp(globalDir, "zot-log*.txt")
|
2022-04-27 01:00:20 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
defaultValue := true
|
|
|
|
|
|
|
|
conf.Extensions = &extconf.ExtensionConfig{}
|
|
|
|
conf.Extensions.Metrics = &extconf.MetricsConfig{
|
2022-10-21 07:33:54 -05:00
|
|
|
BaseConfig: extconf.BaseConfig{Enable: &defaultValue},
|
2022-04-27 01:00:20 -05:00
|
|
|
Prometheus: &extconf.PrometheusConfig{},
|
|
|
|
}
|
|
|
|
conf.Log.Level = "info"
|
|
|
|
conf.Log.Output = logFile.Name()
|
|
|
|
defer os.Remove(logFile.Name()) // cleanup
|
|
|
|
|
|
|
|
ctlr := api.NewController(conf)
|
2023-01-19 11:54:05 -05:00
|
|
|
ctlrManager := test.NewControllerManager(ctlr)
|
2022-04-27 01:00:20 -05:00
|
|
|
|
|
|
|
subPaths := make(map[string]config.StorageConfig)
|
|
|
|
subPaths["/a"] = config.StorageConfig{}
|
|
|
|
|
|
|
|
ctlr.Config.Storage.RootDirectory = globalDir
|
|
|
|
ctlr.Config.Storage.SubPaths = subPaths
|
|
|
|
|
2023-01-19 11:54:05 -05:00
|
|
|
ctlrManager.StartAndWait(port)
|
2022-04-27 01:00:20 -05:00
|
|
|
|
|
|
|
data, _ := os.ReadFile(logFile.Name())
|
|
|
|
|
|
|
|
So(string(data), ShouldContainSubstring,
|
|
|
|
"Prometheus instrumentation Path not set, changing to '/metrics'.")
|
|
|
|
})
|
|
|
|
}
|