2021-11-10 09:31:03 -05:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
2021-11-22 03:12:01 -05:00
|
|
|
"time"
|
2021-11-10 09:31:03 -05:00
|
|
|
|
|
|
|
"github.com/phayes/freeport"
|
|
|
|
"gopkg.in/resty.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
BaseURL = "http://127.0.0.1:%s"
|
|
|
|
BaseSecureURL = "https://127.0.0.1:%s"
|
2021-11-22 03:12:01 -05:00
|
|
|
SleepTime = 100 * time.Millisecond
|
2021-11-10 09:31:03 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func GetFreePort() string {
|
|
|
|
port, err := freeport.GetFreePort()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprint(port)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetBaseURL(port string) string {
|
|
|
|
return fmt.Sprintf(BaseURL, port)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetSecureBaseURL(port string) string {
|
|
|
|
return fmt.Sprintf(BaseSecureURL, port)
|
|
|
|
}
|
|
|
|
|
|
|
|
func MakeHtpasswdFile() string {
|
|
|
|
// bcrypt(username="test", passwd="test")
|
|
|
|
content := "test:$2y$05$hlbSXDp6hzDLu6VwACS39ORvVRpr3OMR4RlJ31jtlaOEGnPjKZI1m\n"
|
|
|
|
return MakeHtpasswdFileFromString(content)
|
|
|
|
}
|
|
|
|
|
|
|
|
func MakeHtpasswdFileFromString(fileContent string) string {
|
|
|
|
f, err := ioutil.TempFile("", "htpasswd-")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// bcrypt(username="test", passwd="test")
|
|
|
|
content := []byte(fileContent)
|
|
|
|
if err := ioutil.WriteFile(f.Name(), content, 0600); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return f.Name()
|
|
|
|
}
|
|
|
|
|
|
|
|
func Location(baseURL string, resp *resty.Response) string {
|
|
|
|
// For some API responses, the Location header is set and is supposed to
|
|
|
|
// indicate an opaque value. However, it is not clear if this value is an
|
|
|
|
// absolute URL (https://server:port/v2/...) or just a path (/v2/...)
|
|
|
|
// zot implements the latter as per the spec, but some registries appear to
|
|
|
|
// return the former - this needs to be clarified
|
|
|
|
loc := resp.Header().Get("Location")
|
|
|
|
return baseURL + loc
|
|
|
|
}
|
|
|
|
|
|
|
|
func CopyFiles(sourceDir string, destDir string) error {
|
|
|
|
sourceMeta, err := os.Stat(sourceDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.MkdirAll(destDir, sourceMeta.Mode()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
files, err := ioutil.ReadDir(sourceDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
sourceFilePath := path.Join(sourceDir, file.Name())
|
|
|
|
destFilePath := path.Join(destDir, file.Name())
|
|
|
|
|
|
|
|
if file.IsDir() {
|
|
|
|
if err = CopyFiles(sourceFilePath, destFilePath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sourceFile, err := os.Open(sourceFilePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer sourceFile.Close()
|
|
|
|
|
|
|
|
destFile, err := os.Create(destFilePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer destFile.Close()
|
|
|
|
|
|
|
|
if _, err = io.Copy(destFile, sourceFile); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-11-22 03:12:01 -05:00
|
|
|
|
|
|
|
func WaitTillServerReady(url string) {
|
|
|
|
for {
|
|
|
|
_, err := resty.R().Get(url)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(SleepTime)
|
|
|
|
}
|
|
|
|
}
|