0
Fork 0
mirror of https://github.com/project-zot/zot.git synced 2024-12-23 22:27:35 -05:00
zot/pkg/compliance/v1_0_0/check_test.go

113 lines
2.2 KiB
Go
Raw Normal View History

2019-10-09 13:50:10 -05:00
package v1_0_0_test
import (
"context"
"net/http"
"os"
2019-10-09 13:50:10 -05:00
"testing"
"time"
"gopkg.in/resty.v1"
"zotregistry.io/zot/pkg/api"
"zotregistry.io/zot/pkg/api/config"
"zotregistry.io/zot/pkg/compliance"
"zotregistry.io/zot/pkg/compliance/v1_0_0"
. "zotregistry.io/zot/pkg/test"
2019-10-09 13:50:10 -05:00
)
//nolint:gochecknoglobals
var (
listenAddress = "127.0.0.1"
defaultDir = ""
firstDir = ""
secondDir = ""
2019-10-09 13:50:10 -05:00
)
func TestWorkflows(t *testing.T) {
ctrl, randomPort := startServer(t)
defer stopServer(ctrl)
storageInfo := []string{defaultDir, firstDir, secondDir}
v1_0_0.CheckWorkflows(t, &compliance.Config{
Address: listenAddress,
Port: randomPort,
StorageInfo: storageInfo,
})
2019-10-09 13:50:10 -05:00
}
func TestWorkflowsOutputJSON(t *testing.T) {
ctrl, randomPort := startServer(t)
defer stopServer(ctrl)
storageInfo := []string{defaultDir, firstDir, secondDir}
v1_0_0.CheckWorkflows(t, &compliance.Config{
Address: listenAddress,
Port: randomPort,
OutputJSON: true,
StorageInfo: storageInfo,
})
}
// start local server on random open port.
func startServer(t *testing.T) (*api.Controller, string) {
t.Helper()
port := GetFreePort()
baseURL := GetBaseURL(port)
conf := config.New()
conf.HTTP.Address = listenAddress
conf.HTTP.Port = port
ctrl := api.NewController(conf)
dir := t.TempDir()
defaultDir = dir
firstSubDir := t.TempDir()
firstDir = firstSubDir
secondSubDir := t.TempDir()
secondDir = secondSubDir
subPaths := make(map[string]config.StorageConfig)
subPaths["/firsttest"] = config.StorageConfig{RootDirectory: firstSubDir}
subPaths["/secondtest"] = config.StorageConfig{RootDirectory: secondSubDir}
ctrl.Config.Storage.RootDirectory = dir
ctrl.Config.Storage.SubPaths = subPaths
2019-10-09 13:50:10 -05:00
go func() {
// this blocks
if err := ctrl.Run(context.Background()); err != nil {
2019-10-09 13:50:10 -05:00
return
}
}()
for {
// poll until ready
resp, _ := resty.R().Get(baseURL)
if resp.StatusCode() == http.StatusNotFound {
2019-10-09 13:50:10 -05:00
break
}
2019-10-09 13:50:10 -05:00
time.Sleep(100 * time.Millisecond)
}
return ctrl, port
}
func stopServer(ctrl *api.Controller) {
err := ctrl.Server.Shutdown(context.Background())
if err != nil {
panic(err)
}
err = os.RemoveAll(ctrl.Config.Storage.RootDirectory)
if err != nil {
panic(err)
}
2019-10-09 13:50:10 -05:00
}