2019-10-09 13:50:10 -05:00
|
|
|
package v1_0_0_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-11-10 09:31:03 -05:00
|
|
|
"net/http"
|
2020-03-09 16:08:31 -05:00
|
|
|
"os"
|
2019-10-09 13:50:10 -05:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"gopkg.in/resty.v1"
|
2022-10-20 11:39:20 -05:00
|
|
|
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/pkg/api"
|
|
|
|
"zotregistry.io/zot/pkg/api/config"
|
|
|
|
"zotregistry.io/zot/pkg/compliance"
|
|
|
|
"zotregistry.io/zot/pkg/compliance/v1_0_0"
|
2022-01-19 14:54:17 -05:00
|
|
|
. "zotregistry.io/zot/pkg/test"
|
2019-10-09 13:50:10 -05:00
|
|
|
)
|
|
|
|
|
2022-10-05 05:21:14 -05:00
|
|
|
//nolint:gochecknoglobals
|
2019-12-13 15:57:51 -05:00
|
|
|
var (
|
|
|
|
listenAddress = "127.0.0.1"
|
2021-04-05 19:40:33 -05:00
|
|
|
defaultDir = ""
|
|
|
|
firstDir = ""
|
|
|
|
secondDir = ""
|
2019-10-09 13:50:10 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestWorkflows(t *testing.T) {
|
2022-03-07 03:55:12 -05:00
|
|
|
ctrl, randomPort := startServer(t)
|
2019-12-13 15:57:51 -05:00
|
|
|
defer stopServer(ctrl)
|
2021-04-05 19:40:33 -05:00
|
|
|
|
|
|
|
storageInfo := []string{defaultDir, firstDir, secondDir}
|
|
|
|
|
2019-12-13 15:57:51 -05:00
|
|
|
v1_0_0.CheckWorkflows(t, &compliance.Config{
|
2021-04-05 19:40:33 -05:00
|
|
|
Address: listenAddress,
|
|
|
|
Port: randomPort,
|
|
|
|
StorageInfo: storageInfo,
|
2019-12-13 15:57:51 -05:00
|
|
|
})
|
2019-10-09 13:50:10 -05:00
|
|
|
}
|
|
|
|
|
2019-12-13 15:57:51 -05:00
|
|
|
func TestWorkflowsOutputJSON(t *testing.T) {
|
2022-03-07 03:55:12 -05:00
|
|
|
ctrl, randomPort := startServer(t)
|
2019-12-13 15:57:51 -05:00
|
|
|
defer stopServer(ctrl)
|
2021-04-05 19:40:33 -05:00
|
|
|
|
|
|
|
storageInfo := []string{defaultDir, firstDir, secondDir}
|
|
|
|
|
2019-12-13 15:57:51 -05:00
|
|
|
v1_0_0.CheckWorkflows(t, &compliance.Config{
|
2021-04-05 19:40:33 -05:00
|
|
|
Address: listenAddress,
|
|
|
|
Port: randomPort,
|
|
|
|
OutputJSON: true,
|
|
|
|
StorageInfo: storageInfo,
|
2019-12-13 15:57:51 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-11 17:13:24 -05:00
|
|
|
// start local server on random open port.
|
2022-03-07 03:55:12 -05:00
|
|
|
func startServer(t *testing.T) (*api.Controller, string) {
|
|
|
|
t.Helper()
|
|
|
|
|
2021-11-10 09:31:03 -05:00
|
|
|
port := GetFreePort()
|
|
|
|
baseURL := GetBaseURL(port)
|
2021-06-08 15:11:18 -05:00
|
|
|
conf := config.New()
|
|
|
|
conf.HTTP.Address = listenAddress
|
2021-11-10 09:31:03 -05:00
|
|
|
conf.HTTP.Port = port
|
2021-06-08 15:11:18 -05:00
|
|
|
ctrl := api.NewController(conf)
|
2020-05-11 17:13:24 -05:00
|
|
|
|
2022-03-07 03:55:12 -05:00
|
|
|
dir := t.TempDir()
|
2021-04-05 19:40:33 -05:00
|
|
|
defaultDir = dir
|
|
|
|
|
2022-03-07 03:55:12 -05:00
|
|
|
firstSubDir := t.TempDir()
|
2021-04-05 19:40:33 -05:00
|
|
|
firstDir = firstSubDir
|
|
|
|
|
2022-03-07 03:55:12 -05:00
|
|
|
secondSubDir := t.TempDir()
|
2021-04-05 19:40:33 -05:00
|
|
|
secondDir = secondSubDir
|
|
|
|
|
2021-06-08 15:11:18 -05:00
|
|
|
subPaths := make(map[string]config.StorageConfig)
|
2021-04-05 19:40:33 -05:00
|
|
|
|
2021-06-08 15:11:18 -05:00
|
|
|
subPaths["/firsttest"] = config.StorageConfig{RootDirectory: firstSubDir}
|
|
|
|
subPaths["/secondtest"] = config.StorageConfig{RootDirectory: secondSubDir}
|
2021-04-05 19:40:33 -05:00
|
|
|
|
2019-12-13 15:57:51 -05:00
|
|
|
ctrl.Config.Storage.RootDirectory = dir
|
2020-05-11 17:13:24 -05:00
|
|
|
|
2021-04-05 19:40:33 -05:00
|
|
|
ctrl.Config.Storage.SubPaths = subPaths
|
|
|
|
|
2019-10-09 13:50:10 -05:00
|
|
|
go func() {
|
|
|
|
// this blocks
|
2022-03-24 07:49:51 -05:00
|
|
|
if err := ctrl.Run(context.Background()); err != nil {
|
2019-10-09 13:50:10 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
// poll until ready
|
2019-12-13 15:57:51 -05:00
|
|
|
resp, _ := resty.R().Get(baseURL)
|
2021-11-10 09:31:03 -05:00
|
|
|
if resp.StatusCode() == http.StatusNotFound {
|
2019-10-09 13:50:10 -05:00
|
|
|
break
|
|
|
|
}
|
2020-05-11 17:13:24 -05:00
|
|
|
|
2019-10-09 13:50:10 -05:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2021-11-10 09:31:03 -05:00
|
|
|
return ctrl, port
|
2019-12-13 15:57:51 -05:00
|
|
|
}
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-12-13 15:57:51 -05:00
|
|
|
func stopServer(ctrl *api.Controller) {
|
2020-05-11 17:13:24 -05:00
|
|
|
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
|
|
|
}
|