mirror of
https://github.com/project-zot/zot.git
synced 2026-06-16 04:17:55 +08:00
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:
@@ -10,7 +10,7 @@ import (
|
||||
ispec "github.com/opencontainers/image-spec/specs-go/v1"
|
||||
"gopkg.in/resty.v1"
|
||||
|
||||
testc "zotregistry.io/zot/pkg/test/common"
|
||||
tcommon "zotregistry.io/zot/pkg/test/common"
|
||||
"zotregistry.io/zot/pkg/test/inject"
|
||||
)
|
||||
|
||||
@@ -81,7 +81,7 @@ func UploadImage(img Image, baseURL, repo, ref string) error {
|
||||
return ErrPostBlob
|
||||
}
|
||||
|
||||
loc := testc.Location(baseURL, resp)
|
||||
loc := tcommon.Location(baseURL, resp)
|
||||
|
||||
// uploading blob should get 201
|
||||
resp, err = resty.R().
|
||||
@@ -181,7 +181,7 @@ func UploadImageWithBasicAuth(img Image, baseURL, repo, ref, user, password stri
|
||||
return ErrPostBlob
|
||||
}
|
||||
|
||||
loc := testc.Location(baseURL, resp)
|
||||
loc := tcommon.Location(baseURL, resp)
|
||||
|
||||
// uploading blob should get 201
|
||||
resp, err = resty.R().
|
||||
|
||||
@@ -13,15 +13,15 @@ import (
|
||||
|
||||
"zotregistry.io/zot/pkg/api"
|
||||
"zotregistry.io/zot/pkg/api/config"
|
||||
. "zotregistry.io/zot/pkg/test"
|
||||
tcommon "zotregistry.io/zot/pkg/test/common"
|
||||
. "zotregistry.io/zot/pkg/test/image-utils"
|
||||
"zotregistry.io/zot/pkg/test/inject"
|
||||
)
|
||||
|
||||
func TestUploadImage(t *testing.T) {
|
||||
Convey("Manifest without schemaVersion should fail validation", t, func() {
|
||||
port := GetFreePort()
|
||||
baseURL := GetBaseURL(port)
|
||||
port := tcommon.GetFreePort()
|
||||
baseURL := tcommon.GetBaseURL(port)
|
||||
|
||||
conf := config.New()
|
||||
conf.HTTP.Port = port
|
||||
@@ -29,7 +29,7 @@ func TestUploadImage(t *testing.T) {
|
||||
|
||||
ctlr := api.NewController(conf)
|
||||
|
||||
ctlrManager := NewControllerManager(ctlr)
|
||||
ctlrManager := tcommon.NewControllerManager(ctlr)
|
||||
ctlrManager.StartAndWait(port)
|
||||
defer ctlrManager.StopServer()
|
||||
|
||||
@@ -57,8 +57,8 @@ func TestUploadImage(t *testing.T) {
|
||||
})
|
||||
|
||||
Convey("Post request results in an error", t, func() {
|
||||
port := GetFreePort()
|
||||
baseURL := GetBaseURL(port)
|
||||
port := tcommon.GetFreePort()
|
||||
baseURL := tcommon.GetBaseURL(port)
|
||||
|
||||
conf := config.New()
|
||||
conf.HTTP.Port = port
|
||||
@@ -73,8 +73,8 @@ func TestUploadImage(t *testing.T) {
|
||||
})
|
||||
|
||||
Convey("Post request status differs from accepted", t, func() {
|
||||
port := GetFreePort()
|
||||
baseURL := GetBaseURL(port)
|
||||
port := tcommon.GetFreePort()
|
||||
baseURL := tcommon.GetBaseURL(port)
|
||||
|
||||
tempDir := t.TempDir()
|
||||
conf := config.New()
|
||||
@@ -88,7 +88,7 @@ func TestUploadImage(t *testing.T) {
|
||||
|
||||
ctlr := api.NewController(conf)
|
||||
|
||||
ctlrManager := NewControllerManager(ctlr)
|
||||
ctlrManager := tcommon.NewControllerManager(ctlr)
|
||||
ctlrManager.StartAndWait(port)
|
||||
defer ctlrManager.StopServer()
|
||||
|
||||
@@ -101,8 +101,8 @@ func TestUploadImage(t *testing.T) {
|
||||
})
|
||||
|
||||
Convey("Put request results in an error", t, func() {
|
||||
port := GetFreePort()
|
||||
baseURL := GetBaseURL(port)
|
||||
port := tcommon.GetFreePort()
|
||||
baseURL := tcommon.GetBaseURL(port)
|
||||
|
||||
conf := config.New()
|
||||
conf.HTTP.Port = port
|
||||
@@ -110,7 +110,7 @@ func TestUploadImage(t *testing.T) {
|
||||
|
||||
ctlr := api.NewController(conf)
|
||||
|
||||
ctlrManager := NewControllerManager(ctlr)
|
||||
ctlrManager := tcommon.NewControllerManager(ctlr)
|
||||
ctlrManager.StartAndWait(port)
|
||||
defer ctlrManager.StopServer()
|
||||
|
||||
@@ -124,8 +124,8 @@ func TestUploadImage(t *testing.T) {
|
||||
})
|
||||
|
||||
Convey("Image uploaded successfully", t, func() {
|
||||
port := GetFreePort()
|
||||
baseURL := GetBaseURL(port)
|
||||
port := tcommon.GetFreePort()
|
||||
baseURL := tcommon.GetBaseURL(port)
|
||||
|
||||
conf := config.New()
|
||||
conf.HTTP.Port = port
|
||||
@@ -133,7 +133,7 @@ func TestUploadImage(t *testing.T) {
|
||||
|
||||
ctlr := api.NewController(conf)
|
||||
|
||||
ctlrManager := NewControllerManager(ctlr)
|
||||
ctlrManager := tcommon.NewControllerManager(ctlr)
|
||||
ctlrManager.StartAndWait(port)
|
||||
defer ctlrManager.StopServer()
|
||||
|
||||
@@ -166,13 +166,13 @@ func TestUploadImage(t *testing.T) {
|
||||
Convey("Upload image with authentification", t, func() {
|
||||
tempDir := t.TempDir()
|
||||
conf := config.New()
|
||||
port := GetFreePort()
|
||||
baseURL := GetBaseURL(port)
|
||||
port := tcommon.GetFreePort()
|
||||
baseURL := tcommon.GetBaseURL(port)
|
||||
|
||||
user1 := "test"
|
||||
password1 := "test"
|
||||
testString1 := GetCredString(user1, password1)
|
||||
htpasswdPath := MakeHtpasswdFileFromString(testString1)
|
||||
testString1 := tcommon.GetCredString(user1, password1)
|
||||
htpasswdPath := tcommon.MakeHtpasswdFileFromString(testString1)
|
||||
defer os.Remove(htpasswdPath)
|
||||
conf.HTTP.Auth = &config.AuthConfig{
|
||||
HTPasswd: config.AuthHTPasswd{
|
||||
@@ -213,7 +213,7 @@ func TestUploadImage(t *testing.T) {
|
||||
|
||||
ctlr.Config.Storage.RootDirectory = tempDir
|
||||
|
||||
ctlrManager := NewControllerManager(ctlr)
|
||||
ctlrManager := tcommon.NewControllerManager(ctlr)
|
||||
ctlrManager.StartAndWait(port)
|
||||
defer ctlrManager.StopServer()
|
||||
|
||||
@@ -236,8 +236,8 @@ func TestUploadImage(t *testing.T) {
|
||||
})
|
||||
|
||||
Convey("Blob upload wrong response status code", t, func() {
|
||||
port := GetFreePort()
|
||||
baseURL := GetBaseURL(port)
|
||||
port := tcommon.GetFreePort()
|
||||
baseURL := tcommon.GetBaseURL(port)
|
||||
|
||||
tempDir := t.TempDir()
|
||||
conf := config.New()
|
||||
@@ -246,7 +246,7 @@ func TestUploadImage(t *testing.T) {
|
||||
|
||||
ctlr := api.NewController(conf)
|
||||
|
||||
ctlrManager := NewControllerManager(ctlr)
|
||||
ctlrManager := tcommon.NewControllerManager(ctlr)
|
||||
ctlrManager.StartAndWait(port)
|
||||
defer ctlrManager.StopServer()
|
||||
|
||||
@@ -289,8 +289,8 @@ func TestUploadImage(t *testing.T) {
|
||||
})
|
||||
|
||||
Convey("CreateBlobUpload wrong response status code", t, func() {
|
||||
port := GetFreePort()
|
||||
baseURL := GetBaseURL(port)
|
||||
port := tcommon.GetFreePort()
|
||||
baseURL := tcommon.GetBaseURL(port)
|
||||
|
||||
tempDir := t.TempDir()
|
||||
conf := config.New()
|
||||
@@ -299,7 +299,7 @@ func TestUploadImage(t *testing.T) {
|
||||
|
||||
ctlr := api.NewController(conf)
|
||||
|
||||
ctlrManager := NewControllerManager(ctlr)
|
||||
ctlrManager := tcommon.NewControllerManager(ctlr)
|
||||
ctlrManager.StartAndWait(port)
|
||||
defer ctlrManager.StopServer()
|
||||
|
||||
@@ -331,8 +331,8 @@ func TestUploadImage(t *testing.T) {
|
||||
|
||||
func TestInjectUploadImage(t *testing.T) {
|
||||
Convey("Inject failures for unreachable lines", t, func() {
|
||||
port := GetFreePort()
|
||||
baseURL := GetBaseURL(port)
|
||||
port := tcommon.GetFreePort()
|
||||
baseURL := tcommon.GetBaseURL(port)
|
||||
|
||||
tempDir := t.TempDir()
|
||||
conf := config.New()
|
||||
@@ -341,7 +341,7 @@ func TestInjectUploadImage(t *testing.T) {
|
||||
|
||||
ctlr := api.NewController(conf)
|
||||
|
||||
ctlrManager := NewControllerManager(ctlr)
|
||||
ctlrManager := tcommon.NewControllerManager(ctlr)
|
||||
ctlrManager.StartAndWait(port)
|
||||
defer ctlrManager.StopServer()
|
||||
|
||||
@@ -395,8 +395,8 @@ func TestInjectUploadImage(t *testing.T) {
|
||||
|
||||
func TestUploadMultiarchImage(t *testing.T) {
|
||||
Convey("make controller", t, func() {
|
||||
port := GetFreePort()
|
||||
baseURL := GetBaseURL(port)
|
||||
port := tcommon.GetFreePort()
|
||||
baseURL := tcommon.GetBaseURL(port)
|
||||
|
||||
conf := config.New()
|
||||
conf.HTTP.Port = port
|
||||
@@ -404,7 +404,7 @@ func TestUploadMultiarchImage(t *testing.T) {
|
||||
|
||||
ctlr := api.NewController(conf)
|
||||
|
||||
ctlrManager := NewControllerManager(ctlr)
|
||||
ctlrManager := tcommon.NewControllerManager(ctlr)
|
||||
ctlrManager.StartAndWait(port)
|
||||
defer ctlrManager.StopServer()
|
||||
|
||||
@@ -474,8 +474,8 @@ func TestUploadMultiarchImage(t *testing.T) {
|
||||
|
||||
func TestInjectUploadImageWithBasicAuth(t *testing.T) {
|
||||
Convey("Inject failures for unreachable lines", t, func() {
|
||||
port := GetFreePort()
|
||||
baseURL := GetBaseURL(port)
|
||||
port := tcommon.GetFreePort()
|
||||
baseURL := tcommon.GetBaseURL(port)
|
||||
|
||||
tempDir := t.TempDir()
|
||||
conf := config.New()
|
||||
@@ -484,8 +484,8 @@ func TestInjectUploadImageWithBasicAuth(t *testing.T) {
|
||||
|
||||
user := "user"
|
||||
password := "password"
|
||||
testString := GetCredString(user, password)
|
||||
htpasswdPath := MakeHtpasswdFileFromString(testString)
|
||||
testString := tcommon.GetCredString(user, password)
|
||||
htpasswdPath := tcommon.MakeHtpasswdFileFromString(testString)
|
||||
defer os.Remove(htpasswdPath)
|
||||
conf.HTTP.Auth = &config.AuthConfig{
|
||||
HTPasswd: config.AuthHTPasswd{
|
||||
@@ -495,7 +495,7 @@ func TestInjectUploadImageWithBasicAuth(t *testing.T) {
|
||||
|
||||
ctlr := api.NewController(conf)
|
||||
|
||||
ctlrManager := NewControllerManager(ctlr)
|
||||
ctlrManager := tcommon.NewControllerManager(ctlr)
|
||||
ctlrManager.StartAndWait(port)
|
||||
defer ctlrManager.StopServer()
|
||||
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package image
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"math/big"
|
||||
mathRand "math/rand"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -9,7 +13,7 @@ import (
|
||||
godigest "github.com/opencontainers/go-digest"
|
||||
ispec "github.com/opencontainers/image-spec/specs-go/v1"
|
||||
|
||||
testc "zotregistry.io/zot/pkg/test/common"
|
||||
tcommon "zotregistry.io/zot/pkg/test/common"
|
||||
)
|
||||
|
||||
var vulnerableLayer []byte //nolint: gochecknoglobals
|
||||
@@ -26,7 +30,7 @@ func GetLayerWithVulnerability() ([]byte, error) {
|
||||
return vulnerableLayer, nil
|
||||
}
|
||||
|
||||
projectRootDir, err := testc.GetProjectRootDir()
|
||||
projectRootDir, err := tcommon.GetProjectRootDir()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -127,3 +131,66 @@ func GetDefaultVulnConfig() ispec.Image {
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Adapted from https://gist.github.com/dopey/c69559607800d2f2f90b1b1ed4e550fb
|
||||
func RandomString(n int) string {
|
||||
const letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-"
|
||||
|
||||
ret := make([]byte, n)
|
||||
|
||||
for count := 0; count < n; count++ {
|
||||
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(letters))))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
ret[count] = letters[num.Int64()]
|
||||
}
|
||||
|
||||
return string(ret)
|
||||
}
|
||||
|
||||
func GetRandomImageConfig() ([]byte, godigest.Digest) {
|
||||
const maxLen = 16
|
||||
|
||||
randomAuthor := RandomString(maxLen)
|
||||
|
||||
config := ispec.Image{
|
||||
Platform: ispec.Platform{
|
||||
Architecture: "amd64",
|
||||
OS: "linux",
|
||||
},
|
||||
RootFS: ispec.RootFS{
|
||||
Type: "layers",
|
||||
DiffIDs: []godigest.Digest{},
|
||||
},
|
||||
Author: randomAuthor,
|
||||
}
|
||||
|
||||
configBlobContent, err := json.MarshalIndent(&config, "", "\t")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
configBlobDigestRaw := godigest.FromBytes(configBlobContent)
|
||||
|
||||
return configBlobContent, configBlobDigestRaw
|
||||
}
|
||||
|
||||
func GetIndexBlobWithManifests(manifestDigests []godigest.Digest) ([]byte, error) {
|
||||
manifests := make([]ispec.Descriptor, 0, len(manifestDigests))
|
||||
|
||||
for _, manifestDigest := range manifestDigests {
|
||||
manifests = append(manifests, ispec.Descriptor{
|
||||
Digest: manifestDigest,
|
||||
MediaType: ispec.MediaTypeImageManifest,
|
||||
})
|
||||
}
|
||||
|
||||
indexContent := ispec.Index{
|
||||
MediaType: ispec.MediaTypeImageIndex,
|
||||
Manifests: manifests,
|
||||
}
|
||||
|
||||
return json.Marshal(indexContent)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
package image
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
|
||||
godigest "github.com/opencontainers/go-digest"
|
||||
ispec "github.com/opencontainers/image-spec/specs-go/v1"
|
||||
|
||||
stypes "zotregistry.io/zot/pkg/storage/types"
|
||||
)
|
||||
|
||||
func WriteImageToFileSystem(image Image, repoName, ref string, storeController stypes.StoreController) error {
|
||||
store := storeController.GetImageStore(repoName)
|
||||
|
||||
err := store.InitRepo(repoName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, layerBlob := range image.Layers {
|
||||
layerReader := bytes.NewReader(layerBlob)
|
||||
layerDigest := godigest.FromBytes(layerBlob)
|
||||
|
||||
_, _, err = store.FullBlobUpload(repoName, layerReader, layerDigest)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
configBlob, err := json.Marshal(image.Config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
configReader := bytes.NewReader(configBlob)
|
||||
configDigest := godigest.FromBytes(configBlob)
|
||||
|
||||
_, _, err = store.FullBlobUpload(repoName, configReader, configDigest)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
manifestBlob, err := json.Marshal(image.Manifest)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, _, err = store.PutImageManifest(repoName, ref, ispec.MediaTypeImageManifest, manifestBlob)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func WriteMultiArchImageToFileSystem(multiarchImage MultiarchImage, repoName, ref string,
|
||||
storeController stypes.StoreController,
|
||||
) error {
|
||||
store := storeController.GetImageStore(repoName)
|
||||
|
||||
err := store.InitRepo(repoName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, image := range multiarchImage.Images {
|
||||
err := WriteImageToFileSystem(image, repoName, image.DigestStr(), storeController)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
indexBlob, err := json.Marshal(multiarchImage.Index)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, _, err = store.PutImageManifest(repoName, ref, ispec.MediaTypeImageIndex,
|
||||
indexBlob)
|
||||
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package image_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"testing"
|
||||
|
||||
godigest "github.com/opencontainers/go-digest"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
|
||||
"zotregistry.io/zot/pkg/storage"
|
||||
. "zotregistry.io/zot/pkg/test/image-utils"
|
||||
"zotregistry.io/zot/pkg/test/mocks"
|
||||
)
|
||||
|
||||
var ErrTestError = errors.New("ErrTestError")
|
||||
|
||||
func TestWriteImageToFileSystem(t *testing.T) {
|
||||
Convey("WriteImageToFileSystem errors", t, func() {
|
||||
err := WriteImageToFileSystem(Image{}, "repo", "dig", storage.StoreController{
|
||||
DefaultStore: mocks.MockedImageStore{
|
||||
InitRepoFn: func(name string) error {
|
||||
return ErrTestError
|
||||
},
|
||||
},
|
||||
})
|
||||
So(err, ShouldNotBeNil)
|
||||
|
||||
err = WriteImageToFileSystem(
|
||||
Image{Layers: [][]byte{[]byte("testLayer")}},
|
||||
"repo",
|
||||
"tag",
|
||||
storage.StoreController{
|
||||
DefaultStore: mocks.MockedImageStore{
|
||||
FullBlobUploadFn: func(repo string, body io.Reader, digest godigest.Digest,
|
||||
) (string, int64, error) {
|
||||
return "", 0, ErrTestError
|
||||
},
|
||||
},
|
||||
})
|
||||
So(err, ShouldNotBeNil)
|
||||
|
||||
count := 0
|
||||
err = WriteImageToFileSystem(
|
||||
Image{Layers: [][]byte{[]byte("testLayer")}},
|
||||
"repo",
|
||||
"tag",
|
||||
storage.StoreController{
|
||||
DefaultStore: mocks.MockedImageStore{
|
||||
FullBlobUploadFn: func(repo string, body io.Reader, digest godigest.Digest,
|
||||
) (string, int64, error) {
|
||||
if count == 0 {
|
||||
count++
|
||||
|
||||
return "", 0, nil
|
||||
}
|
||||
|
||||
return "", 0, ErrTestError
|
||||
},
|
||||
},
|
||||
})
|
||||
So(err, ShouldNotBeNil)
|
||||
|
||||
err = WriteImageToFileSystem(
|
||||
Image{Layers: [][]byte{[]byte("testLayer")}},
|
||||
"repo",
|
||||
"tag",
|
||||
storage.StoreController{
|
||||
DefaultStore: mocks.MockedImageStore{
|
||||
PutImageManifestFn: func(repo, reference, mediaType string, body []byte,
|
||||
) (godigest.Digest, godigest.Digest, error) {
|
||||
return "", "", ErrTestError
|
||||
},
|
||||
},
|
||||
})
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user