2022-04-27 01:00:20 -05:00
|
|
|
//go:build sync || metrics
|
|
|
|
// +build sync metrics
|
|
|
|
|
|
|
|
package extensions_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"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()
|
|
|
|
baseURL := test.GetBaseURL(port)
|
|
|
|
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)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
ctx := context.Background()
|
|
|
|
_ = ctlr.Server.Shutdown(ctx)
|
|
|
|
}()
|
|
|
|
|
|
|
|
ctlr.Config.Storage.RootDirectory = globalDir
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err := ctlr.Run(context.Background()); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
test.WaitTillServerReady(baseURL)
|
|
|
|
|
|
|
|
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
|
|
|
|
baseURL := test.GetBaseURL(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{
|
|
|
|
Enable: &defaultValue,
|
|
|
|
Prometheus: &extconf.PrometheusConfig{},
|
|
|
|
}
|
|
|
|
conf.Log.Level = "info"
|
|
|
|
conf.Log.Output = logFile.Name()
|
|
|
|
defer os.Remove(logFile.Name()) // cleanup
|
|
|
|
|
|
|
|
ctlr := api.NewController(conf)
|
|
|
|
|
|
|
|
subPaths := make(map[string]config.StorageConfig)
|
|
|
|
subPaths["/a"] = config.StorageConfig{}
|
|
|
|
|
|
|
|
ctlr.Config.Storage.RootDirectory = globalDir
|
|
|
|
ctlr.Config.Storage.SubPaths = subPaths
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err := ctlr.Run(context.Background()); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
test.WaitTillServerReady(baseURL)
|
|
|
|
|
|
|
|
data, _ := os.ReadFile(logFile.Name())
|
|
|
|
|
|
|
|
So(string(data), ShouldContainSubstring,
|
|
|
|
"Prometheus instrumentation Path not set, changing to '/metrics'.")
|
|
|
|
})
|
|
|
|
}
|