2019-10-09 13:50:10 -05:00
|
|
|
package v1_0_0_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2020-03-09 16:08:31 -05:00
|
|
|
"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"
|
2019-12-13 15:57:51 -05:00
|
|
|
"github.com/phayes/freeport"
|
2019-10-09 13:50:10 -05:00
|
|
|
"gopkg.in/resty.v1"
|
|
|
|
)
|
|
|
|
|
2020-05-11 17:13:24 -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) {
|
2019-12-13 15:57:51 -05:00
|
|
|
ctrl, randomPort := startServer()
|
|
|
|
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) {
|
|
|
|
ctrl, randomPort := startServer()
|
|
|
|
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.
|
2019-12-13 15:57:51 -05:00
|
|
|
func startServer() (*api.Controller, string) {
|
|
|
|
portInt, err := freeport.GetFreePort()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-05-11 17:13:24 -05:00
|
|
|
|
2019-12-13 15:57:51 -05:00
|
|
|
randomPort := fmt.Sprintf("%d", portInt)
|
|
|
|
fmt.Println(randomPort)
|
|
|
|
|
2019-10-09 13:50:10 -05:00
|
|
|
config := api.NewConfig()
|
2019-12-13 15:57:51 -05:00
|
|
|
config.HTTP.Address = listenAddress
|
|
|
|
config.HTTP.Port = randomPort
|
|
|
|
ctrl := api.NewController(config)
|
2020-05-11 17:13:24 -05:00
|
|
|
|
2019-10-09 13:50:10 -05:00
|
|
|
dir, err := ioutil.TempDir("", "oci-repo-test")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2021-04-05 19:40:33 -05:00
|
|
|
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}
|
|
|
|
|
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
|
2019-12-13 15:57:51 -05:00
|
|
|
if err := ctrl.Run(); err != nil {
|
2019-10-09 13:50:10 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-12-13 15:57:51 -05:00
|
|
|
baseURL := fmt.Sprintf("http://%s:%s", listenAddress, randomPort)
|
2020-05-11 17:13:24 -05:00
|
|
|
|
2019-10-09 13:50:10 -05:00
|
|
|
for {
|
|
|
|
// poll until ready
|
2019-12-13 15:57:51 -05:00
|
|
|
resp, _ := resty.R().Get(baseURL)
|
2019-10-09 13:50:10 -05:00
|
|
|
if resp.StatusCode() == 404 {
|
|
|
|
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
|
|
|
|
2019-12-13 15:57:51 -05:00
|
|
|
return ctrl, randomPort
|
|
|
|
}
|
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
|
|
|
}
|