refactor(pkg/test): split logic in pkg/test/common.go into multiple packages (#1861)

Which could be imported independently. See more details:
1. "zotregistry.io/zot/pkg/test/common" - currently used as
   tcommon "zotregistry.io/zot/pkg/test/common" - inside pkg/test
   test "zotregistry.io/zot/pkg/test/common" - in tests
   . "zotregistry.io/zot/pkg/test/common" - in tests
Decouple zb from code in test/pkg in order to keep the size small.

2. "zotregistry.io/zot/pkg/test/image-utils" - curently used as
   . "zotregistry.io/zot/pkg/test/image-utils"

3. "zotregistry.io/zot/pkg/test/deprecated" -  curently used as
   "zotregistry.io/zot/pkg/test/deprecated"
This one will bre replaced gradually by image-utils in the future.

4. "zotregistry.io/zot/pkg/test/signature" - (cosign + notation) use as
   "zotregistry.io/zot/pkg/test/signature"

5. "zotregistry.io/zot/pkg/test/auth" - (bearer + oidc)  curently used as
   authutils "zotregistry.io/zot/pkg/test/auth"

 6. "zotregistry.io/zot/pkg/test/oci-utils" -  curently used as
   ociutils "zotregistry.io/zot/pkg/test/oci-utils"

Some unused functions were removed, some were replaced, and in
a few cases specific funtions were moved to the files they were used in.

Added an interface for the StoreController, this reduces the number of imports
of the entire image store, decreasing binary size for tests.
If the zb code was still coupled with pkg/test, this would have reflected in zb size.

Signed-off-by: Andrei Aaron <aaaron@luxoft.com>
This commit is contained in:
Andrei Aaron
2023-09-27 21:34:48 +03:00
committed by GitHub
parent c3801dc3d3
commit ba6f347d8d
82 changed files with 3362 additions and 3243 deletions
+10 -8
View File
@@ -36,9 +36,11 @@ import (
mTypes "zotregistry.io/zot/pkg/meta/types"
"zotregistry.io/zot/pkg/storage"
"zotregistry.io/zot/pkg/storage/local"
"zotregistry.io/zot/pkg/test"
test "zotregistry.io/zot/pkg/test/common"
"zotregistry.io/zot/pkg/test/deprecated"
. "zotregistry.io/zot/pkg/test/image-utils"
"zotregistry.io/zot/pkg/test/mocks"
ociutils "zotregistry.io/zot/pkg/test/oci-utils"
)
func TestSearchCVECmd(t *testing.T) {
@@ -404,8 +406,8 @@ func TestNegativeServerResponse(t *testing.T) {
dir := t.TempDir()
srcStorageCtlr := test.GetDefaultStoreController(dir, log.NewLogger("debug", ""))
err := test.WriteImageToFileSystem(CreateDefaultVulnerableImage(), "zot-cve-test", "0.0.1", srcStorageCtlr)
srcStorageCtlr := ociutils.GetDefaultStoreController(dir, log.NewLogger("debug", ""))
err := WriteImageToFileSystem(CreateDefaultVulnerableImage(), "zot-cve-test", "0.0.1", srcStorageCtlr)
So(err, ShouldBeNil)
conf.Storage.RootDirectory = dir
@@ -478,10 +480,10 @@ func TestNegativeServerResponse(t *testing.T) {
}
num := 10
config, layers, manifest, err := test.GetRandomImageComponents(num) //nolint:staticcheck
config, layers, manifest, err := deprecated.GetRandomImageComponents(num) //nolint:staticcheck
So(err, ShouldBeNil)
err = test.WriteImageToFileSystem(
err = WriteImageToFileSystem(
Image{
Manifest: manifest,
Layers: layers,
@@ -618,7 +620,7 @@ func TestServerCVEResponse(t *testing.T) {
test.WaitTillServerReady(url)
config, layers, manifest, err := test.GetImageComponents(100) //nolint:staticcheck
config, layers, manifest, err := deprecated.GetImageComponents(100) //nolint:staticcheck
if err != nil {
panic(err)
}
@@ -934,9 +936,9 @@ func TestCVESort(t *testing.T) {
image1 := CreateRandomImage()
storeController := test.GetDefaultStoreController(rootDir, ctlr.Log)
storeController := ociutils.GetDefaultStoreController(rootDir, ctlr.Log)
err := test.WriteImageToFileSystem(image1, "repo", "tag", storeController)
err := WriteImageToFileSystem(image1, "repo", "tag", storeController)
if err != nil {
t.FailNow()
}
+1 -1
View File
@@ -19,7 +19,7 @@ import (
"zotregistry.io/zot/pkg/api"
"zotregistry.io/zot/pkg/api/config"
"zotregistry.io/zot/pkg/api/constants"
"zotregistry.io/zot/pkg/test"
test "zotregistry.io/zot/pkg/test/common"
)
func TestElevatedPrivilegesTLSNewControllerPrivilegedCert(t *testing.T) {
+1 -1
View File
@@ -12,7 +12,7 @@ import (
"zotregistry.io/zot/pkg/api"
"zotregistry.io/zot/pkg/api/config"
extconf "zotregistry.io/zot/pkg/extensions/config"
"zotregistry.io/zot/pkg/test"
test "zotregistry.io/zot/pkg/test/common"
)
func TestGQLQueries(t *testing.T) {
+35 -14
View File
@@ -12,6 +12,7 @@ import (
"log"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"sync"
@@ -32,9 +33,12 @@ import (
"zotregistry.io/zot/pkg/common"
extconf "zotregistry.io/zot/pkg/extensions/config"
zlog "zotregistry.io/zot/pkg/log"
"zotregistry.io/zot/pkg/test"
testc "zotregistry.io/zot/pkg/test/common"
stypes "zotregistry.io/zot/pkg/storage/types"
test "zotregistry.io/zot/pkg/test/common"
"zotregistry.io/zot/pkg/test/deprecated"
. "zotregistry.io/zot/pkg/test/image-utils"
ociutils "zotregistry.io/zot/pkg/test/oci-utils"
"zotregistry.io/zot/pkg/test/signature"
)
func TestSearchImageCmd(t *testing.T) {
@@ -331,7 +335,7 @@ func TestSignature(t *testing.T) {
err = UploadImage(CreateDefaultImage(), url, repoName, "0.0.1")
So(err, ShouldBeNil)
err = test.SignImageUsingNotary("repo7:0.0.1", port)
err = signature.SignImageUsingNotary("repo7:0.0.1", port)
So(err, ShouldBeNil)
searchConfig := getTestSearchConfig(url, new(searchService))
@@ -1233,8 +1237,8 @@ func TestServerResponseGQLWithoutPermissions(t *testing.T) {
dir := t.TempDir()
srcStorageCtlr := test.GetDefaultStoreController(dir, zlog.NewLogger("debug", ""))
err := test.WriteImageToFileSystem(CreateDefaultImage(), "zot-test", "0.0.1", srcStorageCtlr)
srcStorageCtlr := ociutils.GetDefaultStoreController(dir, zlog.NewLogger("debug", ""))
err := WriteImageToFileSystem(CreateDefaultImage(), "zot-test", "0.0.1", srcStorageCtlr)
So(err, ShouldBeNil)
err = os.Chmod(path.Join(dir, "zot-test", "blobs"), 0o000)
@@ -1381,14 +1385,14 @@ func TestImagesSortFlag(t *testing.T) {
image2 := CreateImageWith().DefaultLayers().
ImageConfig(ispec.Image{Created: DateRef(2020, 1, 1, 1, 1, 1, 0, time.UTC)}).Build()
storeController := test.GetDefaultStoreController(rootDir, ctlr.Log)
storeController := ociutils.GetDefaultStoreController(rootDir, ctlr.Log)
err := test.WriteImageToFileSystem(image1, "a-repo", "tag1", storeController)
err := WriteImageToFileSystem(image1, "a-repo", "tag1", storeController)
if err != nil {
t.FailNow()
}
err = test.WriteImageToFileSystem(image2, "b-repo", "tag2", storeController)
err = WriteImageToFileSystem(image2, "b-repo", "tag2", storeController)
if err != nil {
t.FailNow()
}
@@ -1452,7 +1456,7 @@ func TestImagesCommandGQL(t *testing.T) {
defer cm.StopServer()
Convey("commands with gql", t, func() {
err := test.RemoveLocalStorageContents(ctlr.StoreController.DefaultStore)
err := removeLocalStorageContents(ctlr.StoreController.DefaultStore)
So(err, ShouldBeNil)
Convey("base and derived command", func() {
@@ -1849,7 +1853,7 @@ func TestImageCommandREST(t *testing.T) {
defer cm.StopServer()
Convey("commands without gql", t, func() {
err := test.RemoveLocalStorageContents(ctlr.StoreController.DefaultStore)
err := removeLocalStorageContents(ctlr.StoreController.DefaultStore)
So(err, ShouldBeNil)
Convey("base and derived command", func() {
@@ -2008,7 +2012,7 @@ func uploadTestMultiarch(baseURL string) {
// ------- Upload The multiarch image
multiarch := test.GetMultiarchImageForImages([]Image{image1, image2}) //nolint:staticcheck
multiarch := deprecated.GetMultiarchImageForImages([]Image{image1, image2}) //nolint:staticcheck
err := UploadMultiarchImage(multiarch, baseURL, "repo", "multi-arch")
So(err, ShouldBeNil)
@@ -2017,7 +2021,7 @@ func uploadTestMultiarch(baseURL string) {
func uploadManifest(url string) error {
// create and upload a blob/layer
resp, _ := resty.R().Post(url + "/v2/repo7/blobs/uploads/")
loc := testc.Location(url, resp)
loc := test.Location(url, resp)
content := []byte("this is a blob5")
digest := godigest.FromBytes(content)
@@ -2049,7 +2053,7 @@ func uploadManifest(url string) error {
// upload image config blob
resp, _ = resty.R().Post(url + "/v2/repo7/blobs/uploads/")
loc = testc.Location(url, resp)
loc = test.Location(url, resp)
_, _ = resty.R().
SetContentLength(true).
@@ -2155,7 +2159,7 @@ func uploadManifestDerivedBase(url string) error {
// upload image config blob
resp, _ := resty.R().Post(url + "/v2/repo7/blobs/uploads/")
loc := testc.Location(url, resp)
loc := test.Location(url, resp)
_, _ = resty.R().
SetContentLength(true).
@@ -2705,3 +2709,20 @@ func getTestSearchConfig(url string, searchService SearchService) searchConfig {
resultWriter: nil,
}
}
func removeLocalStorageContents(imageStore stypes.ImageStore) error {
repos, err := imageStore.GetRepositories()
if err != nil {
return err
}
for _, repo := range repos {
// take just the first path
err = os.RemoveAll(filepath.Join(imageStore.RootDir(), filepath.SplitList(repo)[0]))
if err != nil {
return err
}
}
return nil
}
+6 -3
View File
@@ -19,7 +19,7 @@ import (
"zotregistry.io/zot/pkg/api/config"
"zotregistry.io/zot/pkg/api/constants"
extConf "zotregistry.io/zot/pkg/extensions/config"
"zotregistry.io/zot/pkg/test"
test "zotregistry.io/zot/pkg/test/common"
)
const (
@@ -167,7 +167,10 @@ func TestTLSWithoutAuth(t *testing.T) {
home := os.Getenv("HOME")
destCertsDir := filepath.Join(home, certsDir1)
test.CopyTestFiles(sourceCertsDir, destCertsDir)
err := test.CopyFiles(sourceCertsDir, destCertsDir)
So(err, ShouldBeNil)
defer os.RemoveAll(destCertsDir)
args := []string{"list", "--config", "imagetest"}
@@ -176,7 +179,7 @@ func TestTLSWithoutAuth(t *testing.T) {
imageCmd.SetOut(imageBuff)
imageCmd.SetErr(imageBuff)
imageCmd.SetArgs(args)
err := imageCmd.Execute()
err = imageCmd.Execute()
So(err, ShouldBeNil)
})
})
+1 -1
View File
@@ -15,7 +15,7 @@ import (
"zotregistry.io/zot/pkg/api"
"zotregistry.io/zot/pkg/api/config"
"zotregistry.io/zot/pkg/test"
test "zotregistry.io/zot/pkg/test/common"
. "zotregistry.io/zot/pkg/test/image-utils"
)
+5 -4
View File
@@ -18,8 +18,9 @@ import (
"zotregistry.io/zot/pkg/api"
"zotregistry.io/zot/pkg/api/config"
extconf "zotregistry.io/zot/pkg/extensions/config"
"zotregistry.io/zot/pkg/test"
test "zotregistry.io/zot/pkg/test/common"
. "zotregistry.io/zot/pkg/test/image-utils"
ociutils "zotregistry.io/zot/pkg/test/oci-utils"
)
const (
@@ -890,14 +891,14 @@ func TestSearchSort(t *testing.T) {
ImageConfig(ispec.Image{Created: DateRef(2020, 1, 1, 1, 1, 1, 0, time.UTC)}).
Build()
storeController := test.GetDefaultStoreController(rootDir, ctlr.Log)
storeController := ociutils.GetDefaultStoreController(rootDir, ctlr.Log)
err := test.WriteImageToFileSystem(image1, "b-repo", "tag2", storeController)
err := WriteImageToFileSystem(image1, "b-repo", "tag2", storeController)
if err != nil {
t.FailNow()
}
err = test.WriteImageToFileSystem(image2, "a-test-repo", "tag2", storeController)
err = WriteImageToFileSystem(image2, "a-test-repo", "tag2", storeController)
if err != nil {
t.FailNow()
}
+62 -19
View File
@@ -5,6 +5,7 @@ package client
import (
"context"
"errors"
"fmt"
"io"
"net/http"
@@ -16,7 +17,7 @@ import (
ispec "github.com/opencontainers/image-spec/specs-go/v1"
. "github.com/smartystreets/goconvey/convey"
"zotregistry.io/zot/pkg/test"
test "zotregistry.io/zot/pkg/test/common"
)
func getDefaultSearchConf(baseURL string) searchConfig {
@@ -35,10 +36,52 @@ func getDefaultSearchConf(baseURL string) searchConfig {
}
}
type RouteHandler struct {
Route string
// HandlerFunc is the HTTP handler function that receives a writer for output and an HTTP request as input.
HandlerFunc http.HandlerFunc
// AllowedMethods specifies the HTTP methods allowed for the current route.
AllowedMethods []string
}
// Routes is a map that associates HTTP paths to their corresponding HTTP handlers.
type HTTPRoutes []RouteHandler
func StartTestHTTPServer(routes HTTPRoutes, port string) *http.Server {
baseURL := test.GetBaseURL(port)
mux := mux.NewRouter()
mux.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte("{}"))
if err != nil {
return
}
}).Methods(http.MethodGet)
for _, routeHandler := range routes {
mux.HandleFunc(routeHandler.Route, routeHandler.HandlerFunc).Methods(routeHandler.AllowedMethods...)
}
server := &http.Server{ //nolint:gosec
Addr: fmt.Sprintf(":%s", port),
Handler: mux,
}
go func() {
if err := server.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
return
}
}()
test.WaitTillServerReady(baseURL + "/test")
return server
}
func TestDoHTTPRequest(t *testing.T) {
Convey("doHTTPRequest nil result pointer", t, func() {
port := test.GetFreePort()
server := test.StartTestHTTPServer(nil, port)
server := StartTestHTTPServer(nil, port)
defer server.Close()
url := fmt.Sprintf("http://127.0.0.1:%s/asd", port)
@@ -50,7 +93,7 @@ func TestDoHTTPRequest(t *testing.T) {
Convey("doHTTPRequest bad return json", t, func() {
port := test.GetFreePort()
server := test.StartTestHTTPServer(test.HTTPRoutes{
server := StartTestHTTPServer(HTTPRoutes{
{
Route: "/test",
HandlerFunc: func(w http.ResponseWriter, r *http.Request) {
@@ -92,7 +135,7 @@ func TestDoHTTPRequest(t *testing.T) {
searchConf := getDefaultSearchConf(baseURL)
// 404 erorr will appear
server := test.StartTestHTTPServer(test.HTTPRoutes{}, port)
server := StartTestHTTPServer(HTTPRoutes{}, port)
defer server.Close()
URL := baseURL + "/v2/repo/manifests/tag"
@@ -115,7 +158,7 @@ func TestDoHTTPRequest(t *testing.T) {
searchConf := getDefaultSearchConf(baseURL)
Convey("makeGETRequest manifest error, context is done", func() {
server := test.StartTestHTTPServer(test.HTTPRoutes{}, port)
server := StartTestHTTPServer(HTTPRoutes{}, port)
defer server.Close()
ctx, cancel := context.WithCancel(context.Background())
@@ -129,7 +172,7 @@ func TestDoHTTPRequest(t *testing.T) {
})
Convey("makeGETRequest manifest error, context is not done", func() {
server := test.StartTestHTTPServer(test.HTTPRoutes{}, port)
server := StartTestHTTPServer(HTTPRoutes{}, port)
defer server.Close()
_, err := fetchManifestStruct(context.Background(), "repo", "tag", searchConf,
@@ -139,7 +182,7 @@ func TestDoHTTPRequest(t *testing.T) {
})
Convey("makeGETRequest config error, context is not done", func() {
server := test.StartTestHTTPServer(test.HTTPRoutes{
server := StartTestHTTPServer(HTTPRoutes{
{
Route: "/v2/{name}/manifests/{reference}",
HandlerFunc: func(w http.ResponseWriter, r *http.Request) {
@@ -160,7 +203,7 @@ func TestDoHTTPRequest(t *testing.T) {
})
Convey("Platforms on config", func() {
server := test.StartTestHTTPServer(test.HTTPRoutes{
server := StartTestHTTPServer(HTTPRoutes{
{
Route: "/v2/{name}/manifests/{reference}",
HandlerFunc: func(w http.ResponseWriter, r *http.Request) {
@@ -215,7 +258,7 @@ func TestDoHTTPRequest(t *testing.T) {
})
Convey("fetchImageIndexStruct no errors", func() {
server := test.StartTestHTTPServer(test.HTTPRoutes{
server := StartTestHTTPServer(HTTPRoutes{
{
Route: "/v2/{name}/manifests/{reference}",
HandlerFunc: func(writer http.ResponseWriter, req *http.Request) {
@@ -284,7 +327,7 @@ func TestDoHTTPRequest(t *testing.T) {
})
Convey("fetchImageIndexStruct makeGETRequest errors context done", func() {
server := test.StartTestHTTPServer(test.HTTPRoutes{}, port)
server := StartTestHTTPServer(HTTPRoutes{}, port)
defer server.Close()
ctx, cancel := context.WithCancel(context.Background())
@@ -306,7 +349,7 @@ func TestDoHTTPRequest(t *testing.T) {
})
Convey("fetchImageIndexStruct makeGETRequest errors context not done", func() {
server := test.StartTestHTTPServer(test.HTTPRoutes{}, port)
server := StartTestHTTPServer(HTTPRoutes{}, port)
defer server.Close()
URL := baseURL + "/v2/repo/manifests/indexRef"
@@ -341,7 +384,7 @@ func TestDoJobErrors(t *testing.T) {
reqPool.wtgrp.Add(1)
Convey("Do Job makeHEADRequest error context done", func() {
server := test.StartTestHTTPServer(test.HTTPRoutes{}, port)
server := StartTestHTTPServer(HTTPRoutes{}, port)
defer server.Close()
URL := baseURL + "/v2/repo/manifests/manifestRef"
@@ -361,7 +404,7 @@ func TestDoJobErrors(t *testing.T) {
})
Convey("Do Job makeHEADRequest error context not done", func() {
server := test.StartTestHTTPServer(test.HTTPRoutes{}, port)
server := StartTestHTTPServer(HTTPRoutes{}, port)
defer server.Close()
URL := baseURL + "/v2/repo/manifests/manifestRef"
@@ -383,7 +426,7 @@ func TestDoJobErrors(t *testing.T) {
})
Convey("Do Job fetchManifestStruct errors context canceled", func() {
server := test.StartTestHTTPServer(test.HTTPRoutes{
server := StartTestHTTPServer(HTTPRoutes{
{
Route: "/v2/{name}/manifests/{reference}",
HandlerFunc: func(w http.ResponseWriter, r *http.Request) {
@@ -416,7 +459,7 @@ func TestDoJobErrors(t *testing.T) {
})
Convey("Do Job fetchManifestStruct errors context not canceled", func() {
server := test.StartTestHTTPServer(test.HTTPRoutes{
server := StartTestHTTPServer(HTTPRoutes{
{
Route: "/v2/{name}/manifests/{reference}",
HandlerFunc: func(w http.ResponseWriter, r *http.Request) {
@@ -450,7 +493,7 @@ func TestDoJobErrors(t *testing.T) {
})
Convey("Do Job fetchIndexStruct errors context canceled", func() {
server := test.StartTestHTTPServer(test.HTTPRoutes{
server := StartTestHTTPServer(HTTPRoutes{
{
Route: "/v2/{name}/manifests/{reference}",
HandlerFunc: func(w http.ResponseWriter, r *http.Request) {
@@ -483,7 +526,7 @@ func TestDoJobErrors(t *testing.T) {
})
Convey("Do Job fetchIndexStruct errors context not canceled", func() {
server := test.StartTestHTTPServer(test.HTTPRoutes{
server := StartTestHTTPServer(HTTPRoutes{
{
Route: "/v2/{name}/manifests/{reference}",
HandlerFunc: func(w http.ResponseWriter, r *http.Request) {
@@ -516,7 +559,7 @@ func TestDoJobErrors(t *testing.T) {
So(result.StrValue, ShouldResemble, "")
})
Convey("Do Job fetchIndexStruct not supported content type", func() {
server := test.StartTestHTTPServer(test.HTTPRoutes{
server := StartTestHTTPServer(HTTPRoutes{
{
Route: "/v2/{name}/manifests/{reference}",
HandlerFunc: func(w http.ResponseWriter, r *http.Request) {
@@ -546,7 +589,7 @@ func TestDoJobErrors(t *testing.T) {
})
Convey("Media type is MediaTypeImageIndex image.string erorrs", func() {
server := test.StartTestHTTPServer(test.HTTPRoutes{
server := StartTestHTTPServer(HTTPRoutes{
{
Route: "/v2/{name}/manifests/{reference}",
HandlerFunc: func(w http.ResponseWriter, r *http.Request) {