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

131 lines
2.5 KiB
Go
Raw Normal View History

2019-10-09 13:50:10 -05:00
package v1_0_0_test
import (
"context"
"fmt"
"io/ioutil"
"os"
2019-10-09 13:50:10 -05:00
"testing"
"time"
"github.com/anuvu/zot/pkg/api"
"github.com/anuvu/zot/pkg/compliance"
"github.com/anuvu/zot/pkg/compliance/v1_0_0"
"github.com/phayes/freeport"
2019-10-09 13:50:10 -05:00
"gopkg.in/resty.v1"
)
// 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()
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()
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() (*api.Controller, string) {
portInt, err := freeport.GetFreePort()
if err != nil {
panic(err)
}
randomPort := fmt.Sprintf("%d", portInt)
fmt.Println(randomPort)
2019-10-09 13:50:10 -05:00
config := api.NewConfig()
config.HTTP.Address = listenAddress
config.HTTP.Port = randomPort
ctrl := api.NewController(config)
2019-10-09 13:50:10 -05:00
dir, err := ioutil.TempDir("", "oci-repo-test")
if err != nil {
panic(err)
}
defaultDir = dir
firstSubDir, err := ioutil.TempDir("", "oci-repo-test")
if err != nil {
panic(err)
}
firstDir = firstSubDir
secondSubDir, err := ioutil.TempDir("", "oci-repo-test")
if err != nil {
panic(err)
}
secondDir = secondSubDir
subPaths := make(map[string]api.StorageConfig)
subPaths["/firsttest"] = api.StorageConfig{RootDirectory: firstSubDir}
subPaths["/secondtest"] = api.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(); err != nil {
2019-10-09 13:50:10 -05:00
return
}
}()
baseURL := fmt.Sprintf("http://%s:%s", listenAddress, randomPort)
2019-10-09 13:50:10 -05:00
for {
// poll until ready
resp, _ := resty.R().Get(baseURL)
2019-10-09 13:50:10 -05:00
if resp.StatusCode() == 404 {
break
}
2019-10-09 13:50:10 -05:00
time.Sleep(100 * time.Millisecond)
}
return ctrl, randomPort
}
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
}