github.com/anuvu/zot@v1.3.4/pkg/compliance/v1_0_0/check_test.go (about) 1 package v1_0_0_test 2 3 import ( 4 "context" 5 "io/ioutil" 6 "net/http" 7 "os" 8 "testing" 9 "time" 10 11 "github.com/anuvu/zot/pkg/api" 12 "github.com/anuvu/zot/pkg/api/config" 13 "github.com/anuvu/zot/pkg/compliance" 14 "github.com/anuvu/zot/pkg/compliance/v1_0_0" 15 . "github.com/anuvu/zot/test" 16 "gopkg.in/resty.v1" 17 ) 18 19 // nolint: gochecknoglobals 20 var ( 21 listenAddress = "127.0.0.1" 22 defaultDir = "" 23 firstDir = "" 24 secondDir = "" 25 ) 26 27 func TestWorkflows(t *testing.T) { 28 ctrl, randomPort := startServer() 29 defer stopServer(ctrl) 30 31 storageInfo := []string{defaultDir, firstDir, secondDir} 32 33 v1_0_0.CheckWorkflows(t, &compliance.Config{ 34 Address: listenAddress, 35 Port: randomPort, 36 StorageInfo: storageInfo, 37 }) 38 } 39 40 func TestWorkflowsOutputJSON(t *testing.T) { 41 ctrl, randomPort := startServer() 42 defer stopServer(ctrl) 43 44 storageInfo := []string{defaultDir, firstDir, secondDir} 45 46 v1_0_0.CheckWorkflows(t, &compliance.Config{ 47 Address: listenAddress, 48 Port: randomPort, 49 OutputJSON: true, 50 StorageInfo: storageInfo, 51 }) 52 } 53 54 // start local server on random open port. 55 func startServer() (*api.Controller, string) { 56 port := GetFreePort() 57 baseURL := GetBaseURL(port) 58 conf := config.New() 59 conf.HTTP.Address = listenAddress 60 conf.HTTP.Port = port 61 ctrl := api.NewController(conf) 62 63 dir, err := ioutil.TempDir("", "oci-repo-test") 64 if err != nil { 65 panic(err) 66 } 67 68 defaultDir = dir 69 70 firstSubDir, err := ioutil.TempDir("", "oci-repo-test") 71 if err != nil { 72 panic(err) 73 } 74 75 firstDir = firstSubDir 76 77 secondSubDir, err := ioutil.TempDir("", "oci-repo-test") 78 if err != nil { 79 panic(err) 80 } 81 82 secondDir = secondSubDir 83 84 subPaths := make(map[string]config.StorageConfig) 85 86 subPaths["/firsttest"] = config.StorageConfig{RootDirectory: firstSubDir} 87 subPaths["/secondtest"] = config.StorageConfig{RootDirectory: secondSubDir} 88 89 ctrl.Config.Storage.RootDirectory = dir 90 91 ctrl.Config.Storage.SubPaths = subPaths 92 93 go func() { 94 // this blocks 95 if err := ctrl.Run(); err != nil { 96 return 97 } 98 }() 99 100 for { 101 // poll until ready 102 resp, _ := resty.R().Get(baseURL) 103 if resp.StatusCode() == http.StatusNotFound { 104 break 105 } 106 107 time.Sleep(100 * time.Millisecond) 108 } 109 110 return ctrl, port 111 } 112 113 func stopServer(ctrl *api.Controller) { 114 err := ctrl.Server.Shutdown(context.Background()) 115 if err != nil { 116 panic(err) 117 } 118 119 err = os.RemoveAll(ctrl.Config.Storage.RootDirectory) 120 if err != nil { 121 panic(err) 122 } 123 }