2023-09-13 02:28:14 -05:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2023-09-15 11:53:15 -05:00
|
|
|
"errors"
|
2023-09-27 13:34:48 -05:00
|
|
|
"fmt"
|
2023-10-26 03:20:39 -05:00
|
|
|
"math/rand"
|
2023-09-27 13:34:48 -05:00
|
|
|
"net/http"
|
2023-09-13 02:28:14 -05:00
|
|
|
"net/url"
|
2023-09-15 11:53:15 -05:00
|
|
|
"os"
|
2023-09-27 13:34:48 -05:00
|
|
|
"path"
|
|
|
|
"time"
|
2023-09-13 02:28:14 -05:00
|
|
|
|
2023-09-27 13:34:48 -05:00
|
|
|
"github.com/phayes/freeport"
|
2023-09-13 02:28:14 -05:00
|
|
|
"gopkg.in/resty.v1"
|
|
|
|
)
|
|
|
|
|
2023-09-27 13:34:48 -05:00
|
|
|
const (
|
2023-10-26 03:20:39 -05:00
|
|
|
BaseURL = "http://127.0.0.1:%s"
|
|
|
|
BaseSecureURL = "https://127.0.0.1:%s"
|
|
|
|
SleepTime = 100 * time.Millisecond
|
|
|
|
AuthorizationAllRepos = "**"
|
2023-09-27 13:34:48 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type isser interface {
|
|
|
|
Is(string) bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Index returns the index of the first occurrence of name in s,
|
|
|
|
// or -1 if not present.
|
|
|
|
func Index[E isser](s []E, name string) int {
|
|
|
|
for i, v := range s {
|
|
|
|
if v.Is(name) {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Contains reports whether name is present in s.
|
|
|
|
func Contains[E isser](s []E, name string) bool {
|
|
|
|
return Index(s, name) >= 0
|
|
|
|
}
|
2023-09-15 11:53:15 -05:00
|
|
|
|
2023-09-13 02:28:14 -05:00
|
|
|
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")
|
|
|
|
|
|
|
|
uloc, err := url.Parse(loc)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
path := uloc.Path
|
|
|
|
|
|
|
|
return baseURL + path
|
|
|
|
}
|
2023-09-15 11:53:15 -05:00
|
|
|
|
2023-09-27 13:34:48 -05:00
|
|
|
type Controller interface {
|
2023-12-11 13:00:34 -05:00
|
|
|
Init() error
|
|
|
|
Run() error
|
2023-09-27 13:34:48 -05:00
|
|
|
Shutdown()
|
|
|
|
GetPort() int
|
|
|
|
}
|
|
|
|
|
|
|
|
type ControllerManager struct {
|
|
|
|
controller Controller
|
|
|
|
}
|
|
|
|
|
2023-12-11 13:00:34 -05:00
|
|
|
func (cm *ControllerManager) RunServer() {
|
2023-09-27 13:34:48 -05:00
|
|
|
// Useful to be able to call in the same goroutine for testing purposes
|
2023-12-11 13:00:34 -05:00
|
|
|
if err := cm.controller.Run(); !errors.Is(err, http.ErrServerClosed) {
|
2023-09-27 13:34:48 -05:00
|
|
|
panic(err)
|
2023-09-15 11:53:15 -05:00
|
|
|
}
|
2023-09-27 13:34:48 -05:00
|
|
|
}
|
2023-09-15 11:53:15 -05:00
|
|
|
|
2023-09-27 13:34:48 -05:00
|
|
|
func (cm *ControllerManager) StartServer() {
|
2023-12-11 13:00:34 -05:00
|
|
|
if err := cm.controller.Init(); err != nil {
|
2023-09-27 13:34:48 -05:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2023-12-11 13:00:34 -05:00
|
|
|
cm.RunServer()
|
2023-09-27 13:34:48 -05:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cm *ControllerManager) StopServer() {
|
|
|
|
cm.controller.Shutdown()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cm *ControllerManager) WaitServerToBeReady(port string) {
|
|
|
|
url := GetBaseURL(port)
|
|
|
|
WaitTillServerReady(url)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cm *ControllerManager) StartAndWait(port string) {
|
|
|
|
cm.StartServer()
|
2023-09-15 11:53:15 -05:00
|
|
|
|
2023-09-27 13:34:48 -05:00
|
|
|
url := GetBaseURL(port)
|
|
|
|
WaitTillServerReady(url)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewControllerManager(controller Controller) ControllerManager {
|
|
|
|
cm := ControllerManager{
|
|
|
|
controller: controller,
|
|
|
|
}
|
|
|
|
|
|
|
|
return cm
|
|
|
|
}
|
|
|
|
|
|
|
|
func WaitTillServerReady(url string) {
|
|
|
|
for {
|
|
|
|
_, err := resty.R().Get(url)
|
2023-09-15 11:53:15 -05:00
|
|
|
if err == nil {
|
2023-09-27 13:34:48 -05:00
|
|
|
break
|
2023-09-15 11:53:15 -05:00
|
|
|
}
|
|
|
|
|
2023-09-27 13:34:48 -05:00
|
|
|
time.Sleep(SleepTime)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WaitTillTrivyDBDownloadStarted(rootDir string) {
|
|
|
|
for {
|
|
|
|
if _, err := os.Stat(path.Join(rootDir, "_trivy", "db", "trivy.db")); err == nil {
|
|
|
|
break
|
2023-09-15 11:53:15 -05:00
|
|
|
}
|
|
|
|
|
2023-09-27 13:34:48 -05:00
|
|
|
time.Sleep(SleepTime)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetFreePort() string {
|
|
|
|
port, err := freeport.GetFreePort()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2023-09-15 11:53:15 -05:00
|
|
|
}
|
2023-09-27 13:34:48 -05:00
|
|
|
|
|
|
|
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 CustomRedirectPolicy(noOfRedirect int) resty.RedirectPolicy {
|
|
|
|
return resty.RedirectPolicyFunc(func(req *http.Request, via []*http.Request) error {
|
|
|
|
if len(via) >= noOfRedirect {
|
|
|
|
return fmt.Errorf("stopped after %d redirects", noOfRedirect) //nolint: goerr113
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, val := range via[len(via)-1].Header {
|
|
|
|
req.Header[key] = val
|
|
|
|
}
|
|
|
|
|
|
|
|
respCookies := req.Response.Cookies()
|
|
|
|
for _, cookie := range respCookies {
|
|
|
|
req.AddCookie(cookie)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
2023-09-15 11:53:15 -05:00
|
|
|
}
|
2023-10-26 03:20:39 -05:00
|
|
|
|
|
|
|
// Generates a random string with length 10 from lower case & upper case characters and
|
|
|
|
// a seed that can be logged in tests (if test fails, you can reconstruct random string).
|
|
|
|
func GenerateRandomString() (string, int64) {
|
|
|
|
seed := time.Now().UnixNano()
|
|
|
|
//nolint: gosec
|
|
|
|
seededRand := rand.New(rand.NewSource(seed))
|
|
|
|
charset := "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
|
|
|
|
randomBytes := make([]byte, 10)
|
|
|
|
for i := range randomBytes {
|
|
|
|
randomBytes[i] = charset[seededRand.Intn(len(charset))]
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(randomBytes), seed
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generates a random string with length 10 from lower case characters and digits and
|
|
|
|
// a seed that can be logged in tests (if test fails, you can reconstruct random string).
|
|
|
|
func GenerateRandomName() (string, int64) {
|
|
|
|
seed := time.Now().UnixNano()
|
|
|
|
//nolint: gosec
|
|
|
|
seededRand := rand.New(rand.NewSource(seed))
|
|
|
|
charset := "abcdefghijklmnopqrstuvwxyz" + "0123456789"
|
|
|
|
|
|
|
|
randomBytes := make([]byte, 10)
|
|
|
|
for i := range randomBytes {
|
|
|
|
randomBytes[i] = charset[seededRand.Intn(len(charset))]
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(randomBytes), seed
|
|
|
|
}
|
2023-10-30 15:06:04 -05:00
|
|
|
|
2023-11-16 13:39:27 -05:00
|
|
|
func AccumulateField[T any, R any](list []T, accFunc func(T) R) []R {
|
2023-10-30 15:06:04 -05:00
|
|
|
result := make([]R, 0, len(list))
|
|
|
|
|
|
|
|
for i := range list {
|
|
|
|
result = append(result, accFunc(list[i]))
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func ContainSameElements[T comparable](list1, list2 []T) bool {
|
|
|
|
if len(list1) != len(list2) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
count1 := map[T]int{}
|
|
|
|
count2 := map[T]int{}
|
|
|
|
|
|
|
|
for i := range list1 {
|
|
|
|
count1[list1[i]]++
|
|
|
|
count2[list2[i]]++
|
|
|
|
}
|
|
|
|
|
|
|
|
for key := range count1 {
|
|
|
|
if count1[key] != count2[key] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|