0
Fork 0
mirror of https://github.com/project-zot/zot.git synced 2024-12-16 21:56:37 -05:00
zot/pkg/compliance/v1_0_0/check_test.go
Andrei Aaron 38b00e3507
chore(lint): gci to separate zot from other imports (#870)
Signed-off-by: Andrei Aaron <andaaron@cisco.com>
2022-10-20 09:39:20 -07:00

113 lines
2.2 KiB
Go

package v1_0_0_test
import (
"context"
"net/http"
"os"
"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"
)
//nolint:gochecknoglobals
var (
listenAddress = "127.0.0.1"
defaultDir = ""
firstDir = ""
secondDir = ""
)
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,
})
}
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
go func() {
// this blocks
if err := ctrl.Run(context.Background()); err != nil {
return
}
}()
for {
// poll until ready
resp, _ := resty.R().Get(baseURL)
if resp.StatusCode() == http.StatusNotFound {
break
}
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)
}
}