diff --git a/pkg/api/controller_test.go b/pkg/api/controller_test.go index 64457b0b..442ca8af 100644 --- a/pkg/api/controller_test.go +++ b/pkg/api/controller_test.go @@ -62,6 +62,7 @@ import ( storageConstants "zotregistry.io/zot/pkg/storage/constants" "zotregistry.io/zot/pkg/test" testc "zotregistry.io/zot/pkg/test/common" + . "zotregistry.io/zot/pkg/test/image-utils" "zotregistry.io/zot/pkg/test/inject" "zotregistry.io/zot/pkg/test/mocks" ) @@ -925,9 +926,9 @@ func TestBlobReferenced(t *testing.T) { repoName := "repo" - img := test.CreateRandomImage() + img := CreateRandomImage() - err = test.UploadImage(img, baseURL, repoName, "1.0") + err = UploadImage(img, baseURL, repoName, "1.0") So(err, ShouldBeNil) manifestDigest := img.ManifestDescriptor.Digest @@ -2110,9 +2111,9 @@ func TestGroupsPermissionsForLDAP(t *testing.T) { cm.StartAndWait(port) defer cm.StopServer() - img := test.CreateDefaultImage() + img := CreateDefaultImage() - err = test.UploadImageWithBasicAuth( + err = UploadImageWithBasicAuth( img, baseURL, repo, img.DigestStr(), username, passphrase) So(err, ShouldBeNil) @@ -3607,7 +3608,7 @@ func TestAuthorization(t *testing.T) { ctlr := api.NewController(conf) ctlr.Config.Storage.RootDirectory = t.TempDir() - err = test.WriteImageToFileSystem(test.CreateDefaultImage(), "zot-test", "0.0.1", + err = test.WriteImageToFileSystem(CreateDefaultImage(), "zot-test", "0.0.1", test.GetDefaultStoreController(ctlr.Config.Storage.RootDirectory, ctlr.Log)) So(err, ShouldBeNil) @@ -3643,7 +3644,7 @@ func TestAuthorization(t *testing.T) { ctlr := api.NewController(conf) ctlr.Config.Storage.RootDirectory = t.TempDir() - err := test.WriteImageToFileSystem(test.CreateDefaultImage(), "zot-test", "0.0.1", + err := test.WriteImageToFileSystem(CreateDefaultImage(), "zot-test", "0.0.1", test.GetDefaultStoreController(ctlr.Config.Storage.RootDirectory, ctlr.Log)) So(err, ShouldBeNil) @@ -3920,7 +3921,7 @@ func TestAuthorizationWithOnlyAnonymousPolicy(t *testing.T) { err = os.Mkdir(path.Join(dir, "zot-test"), storageConstants.DefaultDirPerms) So(err, ShouldBeNil) - err = test.WriteImageToFileSystem(test.CreateDefaultImage(), "zot-test", "tag", ctlr.StoreController) + err = test.WriteImageToFileSystem(CreateDefaultImage(), "zot-test", "tag", ctlr.StoreController) So(err, ShouldBeNil) // should not have read rights on zot-test @@ -3966,7 +3967,7 @@ func TestAuthorizationWithAnonymousPolicyBasicAuthAndSessionHeader(t *testing.T) htpasswdPath := test.MakeHtpasswdFileFromString(htpasswdContent) defer os.Remove(htpasswdPath) - img := test.CreateRandomImage() + img := CreateRandomImage() tagAnonymous := "1.0-anon" tagAuth := "1.0-auth" tagUnauth := "1.0-unauth" @@ -4070,14 +4071,14 @@ func TestAuthorizationWithAnonymousPolicyBasicAuthAndSessionHeader(t *testing.T) // upload capability // should get 403 without create - err = test.UploadImage(img, baseURL, TestRepo, tagAnonymous) + err = UploadImage(img, baseURL, TestRepo, tagAnonymous) So(err, ShouldNotBeNil) - err = test.UploadImageWithBasicAuth(img, baseURL, + err = UploadImageWithBasicAuth(img, baseURL, TestRepo, tagAuth, htpasswdUsername, passphrase) So(err, ShouldNotBeNil) - err = test.UploadImageWithBasicAuth(img, baseURL, + err = UploadImageWithBasicAuth(img, baseURL, TestRepo, tagUnauth, htpasswdUsername, badpassphrase) So(err, ShouldNotBeNil) @@ -4091,14 +4092,14 @@ func TestAuthorizationWithAnonymousPolicyBasicAuthAndSessionHeader(t *testing.T) } // now it should succeed for valid users - err = test.UploadImage(img, baseURL, TestRepo, tagAnonymous) + err = UploadImage(img, baseURL, TestRepo, tagAnonymous) So(err, ShouldBeNil) - err = test.UploadImageWithBasicAuth(img, baseURL, + err = UploadImageWithBasicAuth(img, baseURL, TestRepo, tagAuth, htpasswdUsername, passphrase) So(err, ShouldBeNil) - err = test.UploadImageWithBasicAuth(img, baseURL, + err = UploadImageWithBasicAuth(img, baseURL, TestRepo, tagUnauth, htpasswdUsername, badpassphrase) So(err, ShouldNotBeNil) @@ -4228,7 +4229,7 @@ func TestAuthorizationWithMultiplePolicies(t *testing.T) { ctlr := api.NewController(conf) ctlr.Config.Storage.RootDirectory = dir - err = test.WriteImageToFileSystem(test.CreateDefaultImage(), "zot-test", "0.0.1", + err = test.WriteImageToFileSystem(CreateDefaultImage(), "zot-test", "0.0.1", test.GetDefaultStoreController(ctlr.Config.Storage.RootDirectory, ctlr.Log)) So(err, ShouldBeNil) @@ -4287,7 +4288,7 @@ func TestAuthorizationWithMultiplePolicies(t *testing.T) { ctlr := api.NewController(conf) ctlr.Config.Storage.RootDirectory = dir - err := test.WriteImageToFileSystem(test.CreateDefaultImage(), "zot-test", "0.0.1", + err := test.WriteImageToFileSystem(CreateDefaultImage(), "zot-test", "0.0.1", test.GetDefaultStoreController(ctlr.Config.Storage.RootDirectory, ctlr.Log)) So(err, ShouldBeNil) @@ -4449,7 +4450,7 @@ func TestCrossRepoMount(t *testing.T) { ctlr.Config.Storage.RemoteCache = false ctlr.Config.Storage.Dedupe = false - err := test.WriteImageToFileSystem(test.CreateDefaultImage(), "zot-cve-test", "test", storage.StoreController{ + err := test.WriteImageToFileSystem(CreateDefaultImage(), "zot-cve-test", "test", storage.StoreController{ DefaultStore: test.GetDefaultImageStore(dir, ctlr.Log), }) So(err, ShouldBeNil) @@ -4660,7 +4661,7 @@ func TestCrossRepoMount(t *testing.T) { ctlr.Config.Storage.Dedupe = false ctlr.Config.Storage.GC = false - image := test.CreateImageWith().RandomLayers(1, 10).DefaultConfig().Build() + image := CreateImageWith().RandomLayers(1, 10).DefaultConfig().Build() err := test.WriteImageToFileSystem(image, "zot-cve-test", "0.0.1", test.GetDefaultStoreController(dir, ctlr.Log)) @@ -4807,10 +4808,10 @@ func TestParallelRequests(t *testing.T) { testImagesDir := t.TempDir() testImagesController := test.GetDefaultStoreController(testImagesDir, ctlr.Log) - err := test.WriteImageToFileSystem(test.CreateRandomImage(), "zot-test", "0.0.1", testImagesController) + err := test.WriteImageToFileSystem(CreateRandomImage(), "zot-test", "0.0.1", testImagesController) assert.Equal(t, err, nil, "Error should be nil") - err = test.WriteImageToFileSystem(test.CreateRandomImage(), "zot-cve-test", "0.0.1", testImagesController) + err = test.WriteImageToFileSystem(CreateRandomImage(), "zot-cve-test", "0.0.1", testImagesController) assert.Equal(t, err, nil, "Error should be nil") cm := test.NewControllerManager(ctlr) @@ -5065,11 +5066,11 @@ func TestImageSignatures(t *testing.T) { defer cm.StopServer() repoName := "signed-repo" - img := test.CreateRandomImage() + img := CreateRandomImage() content := img.ManifestDescriptor.Data digest := img.ManifestDescriptor.Digest - err := test.UploadImage(img, baseURL, repoName, "1.0") + err := UploadImage(img, baseURL, repoName, "1.0") So(err, ShouldBeNil) Convey("Validate cosign signatures", func() { @@ -5303,13 +5304,13 @@ func TestManifestValidation(t *testing.T) { blobDigest := godigest.FromBytes(blobContent) So(blobDigest, ShouldNotBeNil) - img := test.CreateRandomImage() + img := CreateRandomImage() content := img.ManifestDescriptor.Data digest := img.ManifestDescriptor.Digest configDigest := img.ConfigDescriptor.Digest configBlob := img.ConfigDescriptor.Data - err := test.UploadImage(img, baseURL, repoName, "1.0") + err := UploadImage(img, baseURL, repoName, "1.0") So(err, ShouldBeNil) Convey("empty layers should pass validation", func() { @@ -5526,8 +5527,8 @@ func TestArtifactReferences(t *testing.T) { cfg, layers, manifest, err := test.GetImageComponents(2) //nolint:staticcheck So(err, ShouldBeNil) - err = test.UploadImage( - test.Image{ + err = UploadImage( + Image{ Config: cfg, Layers: layers, Manifest: manifest, @@ -5558,7 +5559,7 @@ func TestArtifactReferences(t *testing.T) { So(err, ShouldBeNil) So(resp.StatusCode(), ShouldEqual, http.StatusAccepted) loc := testc.Location(baseURL, resp) - cblob, cdigest := test.GetEmptyImageConfig() + cblob, cdigest := getEmptyImageConfig() resp, err = resty.R(). SetContentLength(true). @@ -6388,7 +6389,7 @@ func TestListingTags(t *testing.T) { rthdlr := api.NewRouteHandler(ctlr) - img := test.CreateRandomImage() + img := CreateRandomImage() sigTag := fmt.Sprintf("sha256-%s.sig", img.Digest().Encoded()) repoName := "test-tags" @@ -6398,7 +6399,7 @@ func TestListingTags(t *testing.T) { } for _, tag := range tagsList { - err := test.UploadImage(img, baseURL, repoName, tag) + err := UploadImage(img, baseURL, repoName, tag) if err != nil { panic(err) } @@ -6571,8 +6572,8 @@ func TestStorageCommit(t *testing.T) { So(resp.StatusCode(), ShouldEqual, http.StatusNotFound) repoName := "repo7" - err = test.UploadImage( - test.Image{ + err = UploadImage( + Image{ Config: cfg, Layers: layers, Manifest: manifest, @@ -6605,8 +6606,8 @@ func TestStorageCommit(t *testing.T) { content, err = json.Marshal(manifest) So(err, ShouldBeNil) - err = test.UploadImage( - test.Image{ + err = UploadImage( + Image{ Config: cfg, Layers: layers, Manifest: manifest, @@ -6616,8 +6617,8 @@ func TestStorageCommit(t *testing.T) { cfg, layers, manifest, err = test.GetImageComponents(1) //nolint:staticcheck So(err, ShouldBeNil) - err = test.UploadImage( - test.Image{ + err = UploadImage( + Image{ Config: cfg, Layers: layers, Manifest: manifest, @@ -6725,8 +6726,8 @@ func TestManifestImageIndex(t *testing.T) { So(resp.StatusCode(), ShouldEqual, http.StatusNotFound) repoName := "index" - err = test.UploadImage( - test.Image{ + err = UploadImage( + Image{ Config: cfg, Layers: layers, Manifest: manifest, @@ -6756,9 +6757,9 @@ func TestManifestImageIndex(t *testing.T) { So(err, ShouldBeNil) So(resp.StatusCode(), ShouldEqual, http.StatusAccepted) - img := test.CreateRandomImage() + img := CreateRandomImage() - err = test.UploadImage(img, baseURL, repoName, img.DigestStr()) + err = UploadImage(img, baseURL, repoName, img.DigestStr()) So(err, ShouldBeNil) content = img.ManifestDescriptor.Data @@ -6775,9 +6776,9 @@ func TestManifestImageIndex(t *testing.T) { So(digestHdr, ShouldEqual, digest.String()) Convey("Image index", func() { - img := test.CreateRandomImage() + img := CreateRandomImage() - err = test.UploadImage(img, baseURL, repoName, img.DigestStr()) + err = UploadImage(img, baseURL, repoName, img.DigestStr()) So(err, ShouldBeNil) content := img.ManifestDescriptor.Data @@ -6825,9 +6826,9 @@ func TestManifestImageIndex(t *testing.T) { So(resp.Body(), ShouldNotBeEmpty) So(resp.Header().Get("Content-Type"), ShouldNotBeEmpty) - img = test.CreateRandomImage() + img = CreateRandomImage() - err = test.UploadImage(img, baseURL, repoName, img.DigestStr()) + err = UploadImage(img, baseURL, repoName, img.DigestStr()) So(err, ShouldBeNil) content = img.ManifestDescriptor.Data @@ -6999,9 +7000,9 @@ func TestManifestImageIndex(t *testing.T) { }) Convey("Update an index tag with different manifest", func() { - img := test.CreateRandomImage() + img := CreateRandomImage() - err = test.UploadImage(img, baseURL, repoName, img.DigestStr()) + err = UploadImage(img, baseURL, repoName, img.DigestStr()) So(err, ShouldBeNil) content = img.ManifestDescriptor.Data @@ -7141,8 +7142,8 @@ func TestManifestCollision(t *testing.T) { cfg, layers, manifest, err := test.GetImageComponents(2) //nolint:staticcheck So(err, ShouldBeNil) - err = test.UploadImage( - test.Image{ + err = UploadImage( + Image{ Config: cfg, Layers: layers, Manifest: manifest, @@ -7167,8 +7168,8 @@ func TestManifestCollision(t *testing.T) { digest = godigest.FromBytes(content) So(digest, ShouldNotBeNil) - err = test.UploadImage( - test.Image{ + err = UploadImage( + Image{ Config: cfg, Layers: layers, Manifest: manifest, @@ -7685,7 +7686,7 @@ func TestGCSignaturesAndUntaggedManifests(t *testing.T) { ctlr.Config.Storage.Dedupe = false - err := test.WriteImageToFileSystem(test.CreateDefaultImage(), repoName, tag, + err := test.WriteImageToFileSystem(CreateDefaultImage(), repoName, tag, test.GetDefaultStoreController(dir, ctlr.Log)) So(err, ShouldBeNil) @@ -7767,9 +7768,9 @@ func TestGCSignaturesAndUntaggedManifests(t *testing.T) { So(err, ShouldBeNil) // trigger gc - img := test.CreateRandomImage() + img := CreateRandomImage() - err = test.UploadImage(img, baseURL, repoName, img.DigestStr()) + err = UploadImage(img, baseURL, repoName, img.DigestStr()) So(err, ShouldBeNil) err = ctlr.StoreController.DefaultStore.RunGCRepo(repoName) @@ -7783,7 +7784,7 @@ func TestGCSignaturesAndUntaggedManifests(t *testing.T) { err = os.WriteFile(path.Join(dir, repoName, "blobs", "sha256", refs.Manifests[0].Digest.Encoded()), []byte("corrupt"), 0o600) //nolint:lll So(err, ShouldBeNil) - err = test.UploadImage(img, baseURL, repoName, tag) + err = UploadImage(img, baseURL, repoName, tag) So(err, ShouldBeNil) err = ctlr.StoreController.DefaultStore.RunGCRepo(repoName) @@ -7802,8 +7803,8 @@ func TestGCSignaturesAndUntaggedManifests(t *testing.T) { So(err, ShouldBeNil) untaggedManifestDigest := godigest.FromBytes(manifestBuf) - err = test.UploadImage( - test.Image{ + err = UploadImage( + Image{ Config: cfg, Layers: layers, Manifest: manifest, @@ -7814,8 +7815,8 @@ func TestGCSignaturesAndUntaggedManifests(t *testing.T) { cfg, layers, manifest, err = test.GetImageComponents(3) //nolint:staticcheck So(err, ShouldBeNil) - err = test.UploadImage( - test.Image{ + err = UploadImage( + Image{ Config: cfg, Layers: layers, Manifest: manifest, @@ -7876,7 +7877,7 @@ func TestGCSignaturesAndUntaggedManifests(t *testing.T) { ctlr.Config.Storage.GCDelay = 1 * time.Second ctlr.Config.Storage.UntaggedImageRetentionDelay = 1 * time.Second - err := test.WriteImageToFileSystem(test.CreateDefaultImage(), repoName, tag, + err := test.WriteImageToFileSystem(CreateDefaultImage(), repoName, tag, test.GetDefaultStoreController(dir, ctlr.Log)) So(err, ShouldBeNil) @@ -7906,8 +7907,8 @@ func TestGCSignaturesAndUntaggedManifests(t *testing.T) { manifestDigest := godigest.FromBytes(manifestContent) - err = test.UploadImage( - test.Image{ + err = UploadImage( + Image{ Manifest: manifest, Config: config, Layers: layers, @@ -7975,7 +7976,7 @@ func TestPeriodicGC(t *testing.T) { ctlr.Config.Storage.GCInterval = 1 * time.Hour ctlr.Config.Storage.GCDelay = 1 * time.Second - err = test.WriteImageToFileSystem(test.CreateDefaultImage(), repoName, "0.0.1", + err = test.WriteImageToFileSystem(CreateDefaultImage(), repoName, "0.0.1", test.GetDefaultStoreController(dir, ctlr.Log)) So(err, ShouldBeNil) @@ -8054,7 +8055,7 @@ func TestPeriodicGC(t *testing.T) { ctlr.Config.Storage.GCInterval = 1 * time.Hour ctlr.Config.Storage.GCDelay = 1 * time.Second - err = test.WriteImageToFileSystem(test.CreateDefaultImage(), repoName, "0.0.1", + err = test.WriteImageToFileSystem(CreateDefaultImage(), repoName, "0.0.1", test.GetDefaultStoreController(dir, ctlr.Log)) So(err, ShouldBeNil) @@ -8098,8 +8099,8 @@ func TestSearchRoutes(t *testing.T) { cfg, layers, manifest, err := test.GetImageComponents(10000) //nolint:staticcheck So(err, ShouldBeNil) - err = test.UploadImage( - test.Image{ + err = UploadImage( + Image{ Config: cfg, Layers: layers, Manifest: manifest, @@ -8111,8 +8112,8 @@ func TestSearchRoutes(t *testing.T) { cfg, layers, manifest, err = test.GetImageComponents(10000) //nolint:staticcheck So(err, ShouldBeNil) - err = test.UploadImage( - test.Image{ + err = UploadImage( + Image{ Config: cfg, Layers: layers, Manifest: manifest, @@ -8184,8 +8185,8 @@ func TestSearchRoutes(t *testing.T) { cfg, layers, manifest, err := test.GetImageComponents(10000) //nolint:staticcheck So(err, ShouldBeNil) - err = test.UploadImageWithBasicAuth( - test.Image{ + err = UploadImageWithBasicAuth( + Image{ Config: cfg, Layers: layers, Manifest: manifest, @@ -8197,8 +8198,8 @@ func TestSearchRoutes(t *testing.T) { cfg, layers, manifest, err = test.GetImageComponents(10000) //nolint:staticcheck So(err, ShouldBeNil) - err = test.UploadImageWithBasicAuth( - test.Image{ + err = UploadImageWithBasicAuth( + Image{ Config: cfg, Layers: layers, Manifest: manifest, @@ -8326,9 +8327,9 @@ func TestSearchRoutes(t *testing.T) { cm.StartAndWait(port) defer cm.StopServer() - img := test.CreateRandomImage() + img := CreateRandomImage() - err = test.UploadImageWithBasicAuth(img, baseURL, repoName, img.DigestStr(), user1, password1) + err = UploadImageWithBasicAuth(img, baseURL, repoName, img.DigestStr(), user1, password1) So(err, ShouldBeNil) query := ` @@ -8413,9 +8414,9 @@ func TestSearchRoutes(t *testing.T) { cm.StartAndWait(port) defer cm.StopServer() - img := test.CreateRandomImage() + img := CreateRandomImage() - err = test.UploadImageWithBasicAuth(img, baseURL, repoName, img.DigestStr(), user1, password1) + err = UploadImageWithBasicAuth(img, baseURL, repoName, img.DigestStr(), user1, password1) So(err, ShouldNotBeNil) }) @@ -8481,9 +8482,9 @@ func TestSearchRoutes(t *testing.T) { cm.StartAndWait(port) defer cm.StopServer() - img := test.CreateRandomImage() + img := CreateRandomImage() - err = test.UploadImageWithBasicAuth(img, baseURL, repoName, img.DigestStr(), user1, password1) + err = UploadImageWithBasicAuth(img, baseURL, repoName, img.DigestStr(), user1, password1) So(err, ShouldBeNil) }) @@ -8549,9 +8550,9 @@ func TestSearchRoutes(t *testing.T) { cm.StartAndWait(port) defer cm.StopServer() - img := test.CreateRandomImage() + img := CreateRandomImage() - err = test.UploadImageWithBasicAuth(img, baseURL, repoName, img.DigestStr(), user1, password1) + err = UploadImageWithBasicAuth(img, baseURL, repoName, img.DigestStr(), user1, password1) So(err, ShouldBeNil) }) @@ -8603,9 +8604,9 @@ func TestSearchRoutes(t *testing.T) { cm.StartAndWait(port) defer cm.StopServer() - img := test.CreateRandomImage() + img := CreateRandomImage() - err = test.UploadImageWithBasicAuth(img, baseURL, repoName, img.DigestStr(), user1, password1) + err = UploadImageWithBasicAuth(img, baseURL, repoName, img.DigestStr(), user1, password1) So(err, ShouldBeNil) }) @@ -8667,9 +8668,9 @@ func TestSearchRoutes(t *testing.T) { cm.StartAndWait(port) defer cm.StopServer() - img := test.CreateRandomImage() + img := CreateRandomImage() - err = test.UploadImageWithBasicAuth(img, baseURL, repoName, img.DigestStr(), "", "") + err = UploadImageWithBasicAuth(img, baseURL, repoName, img.DigestStr(), "", "") So(err, ShouldBeNil) }) }) @@ -9692,3 +9693,16 @@ func RunAuthorizationTests(t *testing.T, client *resty.Client, baseURL string, c So(resp.StatusCode(), ShouldEqual, http.StatusForbidden) }) } + +func getEmptyImageConfig() ([]byte, godigest.Digest) { + config := ispec.Image{} + + configBlobContent, err := json.MarshalIndent(&config, "", "\t") + if err != nil { + return nil, "" + } + + configBlobDigestRaw := godigest.FromBytes(configBlobContent) + + return configBlobContent, configBlobDigestRaw +} diff --git a/pkg/cli/cve_cmd_test.go b/pkg/cli/cve_cmd_test.go index 8de5b5e2..95b4a281 100644 --- a/pkg/cli/cve_cmd_test.go +++ b/pkg/cli/cve_cmd_test.go @@ -37,6 +37,7 @@ import ( "zotregistry.io/zot/pkg/storage" "zotregistry.io/zot/pkg/storage/local" "zotregistry.io/zot/pkg/test" + . "zotregistry.io/zot/pkg/test/image-utils" "zotregistry.io/zot/pkg/test/mocks" ) @@ -404,7 +405,7 @@ func TestNegativeServerResponse(t *testing.T) { dir := t.TempDir() srcStorageCtlr := test.GetDefaultStoreController(dir, log.NewLogger("debug", "")) - err := test.WriteImageToFileSystem(test.CreateDefaultVulnerableImage(), "zot-cve-test", "0.0.1", srcStorageCtlr) + err := test.WriteImageToFileSystem(CreateDefaultVulnerableImage(), "zot-cve-test", "0.0.1", srcStorageCtlr) So(err, ShouldBeNil) conf.Storage.RootDirectory = dir @@ -481,7 +482,7 @@ func TestNegativeServerResponse(t *testing.T) { So(err, ShouldBeNil) err = test.WriteImageToFileSystem( - test.Image{ + Image{ Manifest: manifest, Layers: layers, Config: config, @@ -622,8 +623,7 @@ func TestServerCVEResponse(t *testing.T) { panic(err) } - err = test.PushTestImage("zot-cve-test", "0.0.1", url, - manifest, config, layers) + err = UploadImage(Image{Manifest: manifest, Config: config, Layers: layers}, url, "zot-cve-test", "0.0.1") if err != nil { panic(err) } @@ -932,7 +932,7 @@ func TestCVESort(t *testing.T) { ctlr := api.NewController(conf) ctlr.Config.Storage.RootDirectory = rootDir - image1 := test.CreateRandomImage() + image1 := CreateRandomImage() storeController := test.GetDefaultStoreController(rootDir, ctlr.Log) diff --git a/pkg/cli/image_cmd_test.go b/pkg/cli/image_cmd_test.go index feb57cc7..e43a88a7 100644 --- a/pkg/cli/image_cmd_test.go +++ b/pkg/cli/image_cmd_test.go @@ -34,6 +34,7 @@ import ( zlog "zotregistry.io/zot/pkg/log" "zotregistry.io/zot/pkg/test" testc "zotregistry.io/zot/pkg/test/common" + . "zotregistry.io/zot/pkg/test/image-utils" ) func TestSearchImageCmd(t *testing.T) { @@ -251,8 +252,8 @@ func TestSignature(t *testing.T) { defer cm.StopServer() repoName := "repo7" - image := test.CreateDefaultImage() - err = test.UploadImage(image, url, repoName, "1.0") + image := CreateDefaultImage() + err = UploadImage(image, url, repoName, "1.0") So(err, ShouldBeNil) // generate a keypair @@ -327,7 +328,7 @@ func TestSignature(t *testing.T) { defer cm.StopServer() repoName := "repo7" - err = test.UploadImage(test.CreateDefaultImage(), url, repoName, "0.0.1") + err = UploadImage(CreateDefaultImage(), url, repoName, "0.0.1") So(err, ShouldBeNil) err = test.SignImageUsingNotary("repo7:0.0.1", port) @@ -1233,7 +1234,7 @@ func TestServerResponseGQLWithoutPermissions(t *testing.T) { dir := t.TempDir() srcStorageCtlr := test.GetDefaultStoreController(dir, zlog.NewLogger("debug", "")) - err := test.WriteImageToFileSystem(test.CreateDefaultImage(), "zot-test", "0.0.1", srcStorageCtlr) + err := test.WriteImageToFileSystem(CreateDefaultImage(), "zot-test", "0.0.1", srcStorageCtlr) So(err, ShouldBeNil) err = os.Chmod(path.Join(dir, "zot-test", "blobs"), 0o000) @@ -1374,11 +1375,11 @@ func TestImagesSortFlag(t *testing.T) { ctlr := api.NewController(conf) ctlr.Config.Storage.RootDirectory = rootDir - image1 := test.CreateImageWith().DefaultLayers(). - ImageConfig(ispec.Image{Created: test.DateRef(2010, 1, 1, 1, 1, 1, 0, time.UTC)}).Build() + image1 := CreateImageWith().DefaultLayers(). + ImageConfig(ispec.Image{Created: DateRef(2010, 1, 1, 1, 1, 1, 0, time.UTC)}).Build() - image2 := test.CreateImageWith().DefaultLayers(). - ImageConfig(ispec.Image{Created: test.DateRef(2020, 1, 1, 1, 1, 1, 0, time.UTC)}).Build() + image2 := CreateImageWith().DefaultLayers(). + ImageConfig(ispec.Image{Created: DateRef(2020, 1, 1, 1, 1, 1, 0, time.UTC)}).Build() storeController := test.GetDefaultStoreController(rootDir, ctlr.Log) @@ -1455,18 +1456,18 @@ func TestImagesCommandGQL(t *testing.T) { So(err, ShouldBeNil) Convey("base and derived command", func() { - baseImage := test.CreateImageWith().LayerBlobs( + baseImage := CreateImageWith().LayerBlobs( [][]byte{{1, 2, 3}, {11, 22, 33}}, ).DefaultConfig().Build() - derivedImage := test.CreateImageWith().LayerBlobs( + derivedImage := CreateImageWith().LayerBlobs( [][]byte{{1, 2, 3}, {11, 22, 33}, {44, 55, 66}}, ).DefaultConfig().Build() - err := test.UploadImage(baseImage, baseURL, "repo", "base") + err := UploadImage(baseImage, baseURL, "repo", "base") So(err, ShouldBeNil) - err = test.UploadImage(derivedImage, baseURL, "repo", "derived") + err = UploadImage(derivedImage, baseURL, "repo", "derived") So(err, ShouldBeNil) configPath := makeConfigFile(fmt.Sprintf(`{"configs":[{"_name":"imagetest","url":"%s","showspinner":false}]}`, @@ -1553,9 +1554,9 @@ func TestImagesCommandGQL(t *testing.T) { }) Convey("digest command", func() { - image := test.CreateImageWith().RandomLayers(1, 10).DefaultConfig().Build() + image := CreateImageWith().RandomLayers(1, 10).DefaultConfig().Build() - err := test.UploadImage(image, baseURL, "repo", "img") + err := UploadImage(image, baseURL, "repo", "img") So(err, ShouldBeNil) configPath := makeConfigFile(fmt.Sprintf(`{"configs":[{"_name":"imagetest","url":"%s","showspinner":false}]}`, @@ -1610,9 +1611,9 @@ func TestImagesCommandGQL(t *testing.T) { }) Convey("list command", func() { - image := test.CreateImageWith().RandomLayers(1, 10).DefaultConfig().Build() + image := CreateImageWith().RandomLayers(1, 10).DefaultConfig().Build() - err := test.UploadImage(image, baseURL, "repo", "img") + err := UploadImage(image, baseURL, "repo", "img") So(err, ShouldBeNil) configPath := makeConfigFile(fmt.Sprintf(`{"configs":[{"_name":"imagetest","url":"%s","showspinner":false}]}`, @@ -1659,12 +1660,12 @@ func TestImagesCommandGQL(t *testing.T) { }) Convey("name command", func() { - image := test.CreateImageWith().RandomLayers(1, 10).DefaultConfig().Build() + image := CreateImageWith().RandomLayers(1, 10).DefaultConfig().Build() - err := test.UploadImage(image, baseURL, "repo", "img") + err := UploadImage(image, baseURL, "repo", "img") So(err, ShouldBeNil) - err = test.UploadImage(test.CreateRandomImage(), baseURL, "repo", "img2") + err = UploadImage(CreateRandomImage(), baseURL, "repo", "img2") So(err, ShouldBeNil) configPath := makeConfigFile(fmt.Sprintf(`{"configs":[{"_name":"imagetest","url":"%s","showspinner":false}]}`, @@ -1721,8 +1722,8 @@ func TestImagesCommandGQL(t *testing.T) { }) Convey("CVE", func() { - vulnImage := test.CreateDefaultVulnerableImage() - err := test.UploadImage(vulnImage, baseURL, "repo", "vuln") + vulnImage := CreateDefaultVulnerableImage() + err := UploadImage(vulnImage, baseURL, "repo", "vuln") So(err, ShouldBeNil) configPath := makeConfigFile(fmt.Sprintf(`{"configs":[{"_name":"imagetest","url":"%s","showspinner":false}]}`, @@ -1852,18 +1853,18 @@ func TestImageCommandREST(t *testing.T) { So(err, ShouldBeNil) Convey("base and derived command", func() { - baseImage := test.CreateImageWith().LayerBlobs( + baseImage := CreateImageWith().LayerBlobs( [][]byte{{1, 2, 3}, {11, 22, 33}}, ).DefaultConfig().Build() - derivedImage := test.CreateImageWith().LayerBlobs( + derivedImage := CreateImageWith().LayerBlobs( [][]byte{{1, 2, 3}, {11, 22, 33}, {44, 55, 66}}, ).DefaultConfig().Build() - err := test.UploadImage(baseImage, baseURL, "repo", "base") + err := UploadImage(baseImage, baseURL, "repo", "base") So(err, ShouldBeNil) - err = test.UploadImage(derivedImage, baseURL, "repo", "derived") + err = UploadImage(derivedImage, baseURL, "repo", "derived") So(err, ShouldBeNil) configPath := makeConfigFile(fmt.Sprintf(`{"configs":[{"_name":"imagetest","url":"%s","showspinner":false}]}`, @@ -1890,9 +1891,9 @@ func TestImageCommandREST(t *testing.T) { }) Convey("digest command", func() { - image := test.CreateRandomImage() + image := CreateRandomImage() - err := test.UploadImage(image, baseURL, "repo", "img") + err := UploadImage(image, baseURL, "repo", "img") So(err, ShouldBeNil) configPath := makeConfigFile(fmt.Sprintf(`{"configs":[{"_name":"imagetest","url":"%s","showspinner":false}]}`, @@ -1910,9 +1911,9 @@ func TestImageCommandREST(t *testing.T) { }) Convey("list command", func() { - image := test.CreateRandomImage() + image := CreateRandomImage() - err := test.UploadImage(image, baseURL, "repo", "img") + err := UploadImage(image, baseURL, "repo", "img") So(err, ShouldBeNil) configPath := makeConfigFile(fmt.Sprintf(`{"configs":[{"_name":"imagetest","url":"%s","showspinner":false}]}`, @@ -1932,12 +1933,12 @@ func TestImageCommandREST(t *testing.T) { }) Convey("name command", func() { - image := test.CreateRandomImage() + image := CreateRandomImage() - err := test.UploadImage(image, baseURL, "repo", "img") + err := UploadImage(image, baseURL, "repo", "img") So(err, ShouldBeNil) - err = test.UploadImage(test.CreateRandomImage(), baseURL, "repo", "img2") + err = UploadImage(CreateRandomImage(), baseURL, "repo", "img2") So(err, ShouldBeNil) configPath := makeConfigFile(fmt.Sprintf(`{"configs":[{"_name":"imagetest","url":"%s","showspinner":false}]}`, @@ -1957,8 +1958,8 @@ func TestImageCommandREST(t *testing.T) { }) Convey("CVE", func() { - vulnImage := test.CreateDefaultVulnerableImage() - err := test.UploadImage(vulnImage, baseURL, "repo", "vuln") + vulnImage := CreateDefaultVulnerableImage() + err := UploadImage(vulnImage, baseURL, "repo", "vuln") So(err, ShouldBeNil) configPath := makeConfigFile(fmt.Sprintf(`{"configs":[{"_name":"imagetest","url":"%s","showspinner":false}]}`, @@ -1981,7 +1982,7 @@ func uploadTestMultiarch(baseURL string) { layer11 := []byte{11, 12, 13, 14} layer12 := []byte{16, 17, 18, 19} - image1 := test.CreateImageWith(). + image1 := CreateImageWith(). LayerBlobs([][]byte{ layer11, layer12, @@ -1995,7 +1996,7 @@ func uploadTestMultiarch(baseURL string) { // ------ Define Image2 layer21 := []byte{21, 22, 23, 24} - image2 := test.CreateImageWith(). + image2 := CreateImageWith(). LayerBlobs([][]byte{ layer21, }). @@ -2007,9 +2008,9 @@ func uploadTestMultiarch(baseURL string) { // ------- Upload The multiarch image - multiarch := test.GetMultiarchImageForImages([]test.Image{image1, image2}) //nolint:staticcheck + multiarch := test.GetMultiarchImageForImages([]Image{image1, image2}) //nolint:staticcheck - err := test.UploadMultiarchImage(multiarch, baseURL, "repo", "multi-arch") + err := UploadMultiarchImage(multiarch, baseURL, "repo", "multi-arch") So(err, ShouldBeNil) } diff --git a/pkg/cli/repo_test.go b/pkg/cli/repo_test.go index f63f243f..f585c42f 100644 --- a/pkg/cli/repo_test.go +++ b/pkg/cli/repo_test.go @@ -16,6 +16,7 @@ import ( "zotregistry.io/zot/pkg/api" "zotregistry.io/zot/pkg/api/config" "zotregistry.io/zot/pkg/test" + . "zotregistry.io/zot/pkg/test/image-utils" ) func TestReposCommand(t *testing.T) { @@ -32,9 +33,9 @@ func TestReposCommand(t *testing.T) { cm.StartAndWait(conf.HTTP.Port) defer cm.StopServer() - err := test.UploadImage(test.CreateRandomImage(), baseURL, "repo1", "tag1") + err := UploadImage(CreateRandomImage(), baseURL, "repo1", "tag1") So(err, ShouldBeNil) - err = test.UploadImage(test.CreateRandomImage(), baseURL, "repo2", "tag2") + err = UploadImage(CreateRandomImage(), baseURL, "repo2", "tag2") So(err, ShouldBeNil) configPath := makeConfigFile(fmt.Sprintf(`{"configs":[{"_name":"repostest","url":"%s","showspinner":false}]}`, diff --git a/pkg/cli/search_cmd_test.go b/pkg/cli/search_cmd_test.go index 7081d20f..db79cd41 100644 --- a/pkg/cli/search_cmd_test.go +++ b/pkg/cli/search_cmd_test.go @@ -19,6 +19,7 @@ import ( "zotregistry.io/zot/pkg/api/config" extconf "zotregistry.io/zot/pkg/extensions/config" "zotregistry.io/zot/pkg/test" + . "zotregistry.io/zot/pkg/test/image-utils" ) const ( @@ -47,34 +48,34 @@ func TestReferrerCLI(t *testing.T) { defer cm.StopServer() repo := repoName - image := test.CreateRandomImage() + image := CreateRandomImage() - err := test.UploadImage(image, baseURL, repo, "tag") + err := UploadImage(image, baseURL, repo, "tag") So(err, ShouldBeNil) - ref1 := test.CreateImageWith(). + ref1 := CreateImageWith(). RandomLayers(1, 10). RandomConfig(). Subject(image.DescriptorRef()).Build() - ref2 := test.CreateImageWith(). + ref2 := CreateImageWith(). RandomLayers(1, 10). ArtifactConfig(customArtTypeV1). Subject(image.DescriptorRef()).Build() - ref3 := test.CreateImageWith(). + ref3 := CreateImageWith(). RandomLayers(1, 10). RandomConfig(). ArtifactType(customArtTypeV2). Subject(image.DescriptorRef()).Build() - err = test.UploadImage(ref1, baseURL, repo, ref1.DigestStr()) + err = UploadImage(ref1, baseURL, repo, ref1.DigestStr()) So(err, ShouldBeNil) - err = test.UploadImage(ref2, baseURL, repo, ref2.DigestStr()) + err = UploadImage(ref2, baseURL, repo, ref2.DigestStr()) So(err, ShouldBeNil) - err = test.UploadImage(ref3, baseURL, repo, ref3.DigestStr()) + err = UploadImage(ref3, baseURL, repo, ref3.DigestStr()) So(err, ShouldBeNil) args := []string{"subject", repo + "@" + image.DigestStr(), "--config", "reftest"} @@ -144,34 +145,34 @@ func TestReferrerCLI(t *testing.T) { defer cm.StopServer() repo := repoName - image := test.CreateRandomImage() + image := CreateRandomImage() - err := test.UploadImage(image, baseURL, repo, "tag") + err := UploadImage(image, baseURL, repo, "tag") So(err, ShouldBeNil) - ref1 := test.CreateImageWith(). + ref1 := CreateImageWith(). RandomLayers(1, 10). RandomConfig(). Subject(image.DescriptorRef()).Build() - ref2 := test.CreateImageWith(). + ref2 := CreateImageWith(). RandomLayers(1, 10). ArtifactConfig(customArtTypeV1). Subject(image.DescriptorRef()).Build() - ref3 := test.CreateImageWith(). + ref3 := CreateImageWith(). RandomLayers(1, 10). RandomConfig(). ArtifactType(customArtTypeV2). Subject(image.DescriptorRef()).Build() - err = test.UploadImage(ref1, baseURL, repo, ref1.DigestStr()) + err = UploadImage(ref1, baseURL, repo, ref1.DigestStr()) So(err, ShouldBeNil) - err = test.UploadImage(ref2, baseURL, repo, ref2.DigestStr()) + err = UploadImage(ref2, baseURL, repo, ref2.DigestStr()) So(err, ShouldBeNil) - err = test.UploadImage(ref3, baseURL, repo, ref3.DigestStr()) + err = UploadImage(ref3, baseURL, repo, ref3.DigestStr()) So(err, ShouldBeNil) // get referrers by digest @@ -239,35 +240,35 @@ func TestFormatsReferrersCLI(t *testing.T) { defer cm.StopServer() repo := repoName - image := test.CreateRandomImage() + image := CreateRandomImage() - err := test.UploadImage(image, baseURL, repo, "tag") + err := UploadImage(image, baseURL, repo, "tag") So(err, ShouldBeNil) // add referrers - ref1 := test.CreateImageWith(). + ref1 := CreateImageWith(). RandomLayers(1, 10). RandomConfig(). Subject(image.DescriptorRef()).Build() - ref2 := test.CreateImageWith(). + ref2 := CreateImageWith(). RandomLayers(1, 10). ArtifactConfig(customArtTypeV1). Subject(image.DescriptorRef()).Build() - ref3 := test.CreateImageWith(). + ref3 := CreateImageWith(). RandomLayers(1, 10). RandomConfig(). ArtifactType(customArtTypeV2). Subject(image.DescriptorRef()).Build() - err = test.UploadImage(ref1, baseURL, repo, ref1.DigestStr()) + err = UploadImage(ref1, baseURL, repo, ref1.DigestStr()) So(err, ShouldBeNil) - err = test.UploadImage(ref2, baseURL, repo, ref2.DigestStr()) + err = UploadImage(ref2, baseURL, repo, ref2.DigestStr()) So(err, ShouldBeNil) - err = test.UploadImage(ref3, baseURL, repo, ref3.DigestStr()) + err = UploadImage(ref3, baseURL, repo, ref3.DigestStr()) So(err, ShouldBeNil) Convey("JSON format", func() { @@ -456,34 +457,34 @@ func TestSearchCLI(t *testing.T) { r3tag2 = "repo3tag2" ) - image1 := test.CreateImageWith(). + image1 := CreateImageWith(). RandomLayers(1, 10). ImageConfig(ispec.Image{ - Created: test.DefaultTimeRef(), + Created: DefaultTimeRef(), Platform: ispec.Platform{OS: "Os", Architecture: "Arch"}, }). Build() formatterDigest1 := image1.Digest().Encoded()[:8] - image2 := test.CreateImageWith(). + image2 := CreateImageWith(). RandomLayers(1, 10). DefaultConfig(). Build() formatterDigest2 := image2.Digest().Encoded()[:8] - err := test.UploadImage(image1, baseURL, repo1, r1tag1) + err := UploadImage(image1, baseURL, repo1, r1tag1) So(err, ShouldBeNil) - err = test.UploadImage(image2, baseURL, repo1, r1tag2) + err = UploadImage(image2, baseURL, repo1, r1tag2) So(err, ShouldBeNil) - err = test.UploadImage(image1, baseURL, repo2, r2tag1) + err = UploadImage(image1, baseURL, repo2, r2tag1) So(err, ShouldBeNil) - err = test.UploadImage(image2, baseURL, repo2, r2tag2) + err = UploadImage(image2, baseURL, repo2, r2tag2) So(err, ShouldBeNil) - err = test.UploadImage(image1, baseURL, repo3, r3tag1) + err = UploadImage(image1, baseURL, repo3, r3tag1) So(err, ShouldBeNil) - err = test.UploadImage(image2, baseURL, repo3, r3tag2) + err = UploadImage(image2, baseURL, repo3, r3tag2) So(err, ShouldBeNil) // search by repos @@ -570,22 +571,22 @@ func TestFormatsSearchCLI(t *testing.T) { r3tag2 = "repo3tag2" ) - image1 := test.CreateImageWith().RandomLayers(1, 10).DefaultConfig().Build() - image2 := test.CreateImageWith().RandomLayers(1, 10).DefaultConfig().Build() + image1 := CreateImageWith().RandomLayers(1, 10).DefaultConfig().Build() + image2 := CreateImageWith().RandomLayers(1, 10).DefaultConfig().Build() - err := test.UploadImage(image1, baseURL, repo1, r1tag1) + err := UploadImage(image1, baseURL, repo1, r1tag1) So(err, ShouldBeNil) - err = test.UploadImage(image2, baseURL, repo1, r1tag2) + err = UploadImage(image2, baseURL, repo1, r1tag2) So(err, ShouldBeNil) - err = test.UploadImage(image1, baseURL, repo2, r2tag1) + err = UploadImage(image1, baseURL, repo2, r2tag1) So(err, ShouldBeNil) - err = test.UploadImage(image2, baseURL, repo2, r2tag2) + err = UploadImage(image2, baseURL, repo2, r2tag2) So(err, ShouldBeNil) - err = test.UploadImage(image1, baseURL, repo3, r3tag1) + err = UploadImage(image1, baseURL, repo3, r3tag1) So(err, ShouldBeNil) - err = test.UploadImage(image2, baseURL, repo3, r3tag2) + err = UploadImage(image2, baseURL, repo3, r3tag2) So(err, ShouldBeNil) cmd := NewSearchCommand(new(searchService)) @@ -780,7 +781,7 @@ func TestSearchCommandGQL(t *testing.T) { }) Convey("subject", func() { - err := test.UploadImage(test.CreateRandomImage(), baseURL, "repo", "tag") + err := UploadImage(CreateRandomImage(), baseURL, "repo", "tag") So(err, ShouldBeNil) args := []string{"subject", "repo:tag", "--config", "searchtest"} @@ -843,7 +844,7 @@ func TestSearchCommandREST(t *testing.T) { }) Convey("subject", func() { - err := test.UploadImage(test.CreateRandomImage(), baseURL, "repo", "tag") + err := UploadImage(CreateRandomImage(), baseURL, "repo", "tag") So(err, ShouldBeNil) args := []string{"subject", "repo:tag", "--config", "searchtest"} @@ -881,12 +882,12 @@ func TestSearchSort(t *testing.T) { ctlr := api.NewController(conf) ctlr.Config.Storage.RootDirectory = rootDir - image1 := test.CreateImageWith().DefaultLayers(). - ImageConfig(ispec.Image{Created: test.DateRef(2010, 1, 1, 1, 1, 1, 0, time.UTC)}). + image1 := CreateImageWith().DefaultLayers(). + ImageConfig(ispec.Image{Created: DateRef(2010, 1, 1, 1, 1, 1, 0, time.UTC)}). Build() - image2 := test.CreateImageWith().DefaultLayers(). - ImageConfig(ispec.Image{Created: test.DateRef(2020, 1, 1, 1, 1, 1, 0, time.UTC)}). + image2 := CreateImageWith().DefaultLayers(). + ImageConfig(ispec.Image{Created: DateRef(2020, 1, 1, 1, 1, 1, 0, time.UTC)}). Build() storeController := test.GetDefaultStoreController(rootDir, ctlr.Log) diff --git a/pkg/compliance/v1_0_0/check.go b/pkg/compliance/v1_0_0/check.go index a2d25af6..5aad4e1d 100644 --- a/pkg/compliance/v1_0_0/check.go +++ b/pkg/compliance/v1_0_0/check.go @@ -24,6 +24,7 @@ import ( "zotregistry.io/zot/pkg/compliance" "zotregistry.io/zot/pkg/test" testc "zotregistry.io/zot/pkg/test/common" + "zotregistry.io/zot/pkg/test/image-utils" ) func CheckWorkflows(t *testing.T, config *compliance.Config) { @@ -489,8 +490,8 @@ func CheckWorkflows(t *testing.T, config *compliance.Config) { So(err, ShouldBeNil) repoName := "repo7" - err = test.UploadImage( - test.Image{ + err = image.UploadImage( + image.Image{ Config: cfg, Layers: layers, Manifest: manifest, @@ -502,8 +503,8 @@ func CheckWorkflows(t *testing.T, config *compliance.Config) { digest = godigest.FromBytes(content) So(digest, ShouldNotBeNil) - err = test.UploadImage( - test.Image{ + err = image.UploadImage( + image.Image{ Config: cfg, Layers: layers, Manifest: manifest, @@ -519,8 +520,8 @@ func CheckWorkflows(t *testing.T, config *compliance.Config) { digest = godigest.FromBytes(content) So(digest, ShouldNotBeNil) - err = test.UploadImage( - test.Image{ + err = image.UploadImage( + image.Image{ Config: cfg, Layers: layers, Manifest: manifest, @@ -597,8 +598,8 @@ func CheckWorkflows(t *testing.T, config *compliance.Config) { So(err, ShouldBeNil) repoName := "page0" - err = test.UploadImage( - test.Image{ + err = image.UploadImage( + image.Image{ Config: cfg, Layers: layers, Manifest: manifest, @@ -737,8 +738,8 @@ func CheckWorkflows(t *testing.T, config *compliance.Config) { So(err, ShouldBeNil) // subpath firsttest - err = test.UploadImage( - test.Image{ + err = image.UploadImage( + image.Image{ Config: cfg, Layers: layers, Manifest: manifest, @@ -751,8 +752,8 @@ func CheckWorkflows(t *testing.T, config *compliance.Config) { So(digest, ShouldNotBeNil) // subpath secondtest - err = test.UploadImage( - test.Image{ + err = image.UploadImage( + image.Image{ Config: cfg, Layers: layers, Manifest: manifest, @@ -769,8 +770,8 @@ func CheckWorkflows(t *testing.T, config *compliance.Config) { So(digest, ShouldNotBeNil) // subpath firsttest - err = test.UploadImage( - test.Image{ + err = image.UploadImage( + image.Image{ Config: cfg, Layers: layers, Manifest: manifest, @@ -783,8 +784,8 @@ func CheckWorkflows(t *testing.T, config *compliance.Config) { So(digest, ShouldNotBeNil) // subpath secondtest - err = test.UploadImage( - test.Image{ + err = image.UploadImage( + image.Image{ Config: cfg, Layers: layers, Manifest: manifest, diff --git a/pkg/extensions/extension_image_trust_test.go b/pkg/extensions/extension_image_trust_test.go index f1951fdf..e04bcd1b 100644 --- a/pkg/extensions/extension_image_trust_test.go +++ b/pkg/extensions/extension_image_trust_test.go @@ -34,6 +34,7 @@ import ( "zotregistry.io/zot/pkg/storage" "zotregistry.io/zot/pkg/storage/local" "zotregistry.io/zot/pkg/test" + . "zotregistry.io/zot/pkg/test/image-utils" ) type errReader int @@ -201,7 +202,7 @@ func RunSignatureUploadAndVerificationTests(t *testing.T, cacheDriverParams map[ DefaultStore: imageStore, } - image := test.CreateRandomImage() + image := CreateRandomImage() err = test.WriteImageToFileSystem(image, repo, tag, storeController) So(err, ShouldBeNil) @@ -321,7 +322,7 @@ func RunSignatureUploadAndVerificationTests(t *testing.T, cacheDriverParams map[ DefaultStore: imageStore, } - image := test.CreateRandomImage() + image := CreateRandomImage() err = test.WriteImageToFileSystem(image, repo, tag, storeController) So(err, ShouldBeNil) @@ -428,7 +429,7 @@ func RunSignatureUploadAndVerificationTests(t *testing.T, cacheDriverParams map[ DefaultStore: imageStore, } - image := test.CreateRandomImage() + image := CreateRandomImage() err = test.WriteImageToFileSystem(image, repo, tag, storeController) So(err, ShouldBeNil) @@ -590,7 +591,7 @@ func RunSignatureUploadAndVerificationTests(t *testing.T, cacheDriverParams map[ DefaultStore: imageStore, } - image := test.CreateRandomImage() + image := CreateRandomImage() err = test.WriteImageToFileSystem(image, repo, tag, storeController) So(err, ShouldBeNil) @@ -851,12 +852,12 @@ func RunSignatureUploadAndVerificationTests(t *testing.T, cacheDriverParams map[ } // Write image - image := test.CreateRandomImage() + image := CreateRandomImage() err = test.WriteImageToFileSystem(image, repo, tag, storeController) So(err, ShouldBeNil) // Write signature - signature := test.CreateImageWith().RandomLayers(1, 2).RandomConfig().Build() + signature := CreateImageWith().RandomLayers(1, 2).RandomConfig().Build() So(err, ShouldBeNil) ref, err := test.GetCosignSignatureTagForManifest(image.Manifest) So(err, ShouldBeNil) diff --git a/pkg/extensions/extension_ui_test.go b/pkg/extensions/extension_ui_test.go index 2cc5886e..70a3b433 100644 --- a/pkg/extensions/extension_ui_test.go +++ b/pkg/extensions/extension_ui_test.go @@ -17,6 +17,7 @@ import ( "zotregistry.io/zot/pkg/api/config" extconf "zotregistry.io/zot/pkg/extensions/config" "zotregistry.io/zot/pkg/test" + . "zotregistry.io/zot/pkg/test/image-utils" ) func TestUIExtension(t *testing.T) { @@ -64,8 +65,8 @@ func TestUIExtension(t *testing.T) { tagName := "test-tag" // Upload a test image - err = test.UploadImage( - test.Image{ + err = UploadImage( + Image{ Config: cfg, Layers: layers, Manifest: manifest, diff --git a/pkg/extensions/imagetrust/image_trust_test.go b/pkg/extensions/imagetrust/image_trust_test.go index d6cb2009..207e5490 100644 --- a/pkg/extensions/imagetrust/image_trust_test.go +++ b/pkg/extensions/imagetrust/image_trust_test.go @@ -38,6 +38,7 @@ import ( extconf "zotregistry.io/zot/pkg/extensions/config" "zotregistry.io/zot/pkg/extensions/imagetrust" "zotregistry.io/zot/pkg/test" + . "zotregistry.io/zot/pkg/test/image-utils" "zotregistry.io/zot/pkg/test/mocks" ) @@ -257,7 +258,7 @@ func TestVerifySignatures(t *testing.T) { cm.StartAndWait(conf.HTTP.Port) defer cm.StopServer() - err := test.UploadImage(image, baseURL, repo, tag) + err := UploadImage(image, baseURL, repo, tag) So(err, ShouldBeNil) pubKeyStorage, err := imagetrust.NewPublicKeyLocalStorage(rootDir) @@ -427,7 +428,7 @@ func TestVerifySignatures(t *testing.T) { cm.StartAndWait(conf.HTTP.Port) defer cm.StopServer() - err := test.UploadImage(image, baseURL, repo, tag) + err := UploadImage(image, baseURL, repo, tag) So(err, ShouldBeNil) certStorage, err := imagetrust.NewCertificateLocalStorage(rootDir) @@ -915,7 +916,7 @@ func TestAWSTrustStore(t *testing.T) { Convey("VerifySignature - trustpolicy.json does not exist", t, func() { repo := "repo" - image := test.CreateRandomImage() + image := CreateRandomImage() manifestContent, err := json.Marshal(image.Manifest) So(err, ShouldBeNil) @@ -950,7 +951,7 @@ func TestAWSTrustStore(t *testing.T) { Convey("VerifySignature - trustpolicy.json has invalid content", t, func() { repo := "repo" - image := test.CreateRandomImage() + image := CreateRandomImage() manifestContent, err := json.Marshal(image.Manifest) So(err, ShouldBeNil) @@ -1188,7 +1189,7 @@ func RunVerificationTests(t *testing.T, dbDriverParams map[string]interface{}) { manifestDigest := image.Digest() - err = test.UploadImage(image, baseURL, repo, tag) + err = UploadImage(image, baseURL, repo, tag) So(err, ShouldBeNil) cwd, err := os.Getwd() @@ -1272,7 +1273,7 @@ func RunVerificationTests(t *testing.T, dbDriverParams map[string]interface{}) { manifestDigest := image.Digest() - err = test.UploadImage(image, baseURL, repo, tag) + err = UploadImage(image, baseURL, repo, tag) So(err, ShouldBeNil) notationDir := t.TempDir() diff --git a/pkg/extensions/lint/lint_test.go b/pkg/extensions/lint/lint_test.go index 2e361f21..d355a8df 100644 --- a/pkg/extensions/lint/lint_test.go +++ b/pkg/extensions/lint/lint_test.go @@ -25,6 +25,7 @@ import ( "zotregistry.io/zot/pkg/storage/local" "zotregistry.io/zot/pkg/test" testc "zotregistry.io/zot/pkg/test/common" + . "zotregistry.io/zot/pkg/test/image-utils" ) const ( @@ -57,7 +58,7 @@ func TestVerifyMandatoryAnnotations(t *testing.T) { dir := t.TempDir() testStoreCtlr := test.GetDefaultStoreController(dir, ctlr.Log) - err := test.WriteImageToFileSystem(test.CreateRandomImage(), "zot-test", "0.0.1", testStoreCtlr) + err := test.WriteImageToFileSystem(CreateRandomImage(), "zot-test", "0.0.1", testStoreCtlr) So(err, ShouldBeNil) ctlr.Config.Storage.RootDirectory = dir @@ -104,7 +105,7 @@ func TestVerifyMandatoryAnnotations(t *testing.T) { dir := t.TempDir() testStoreCtlr := test.GetDefaultStoreController(dir, ctlr.Log) - err := test.WriteImageToFileSystem(test.CreateRandomImage(), "zot-test", "0.0.1", testStoreCtlr) + err := test.WriteImageToFileSystem(CreateRandomImage(), "zot-test", "0.0.1", testStoreCtlr) So(err, ShouldBeNil) ctlr.Config.Storage.RootDirectory = dir @@ -151,7 +152,7 @@ func TestVerifyMandatoryAnnotations(t *testing.T) { dir := t.TempDir() testStoreCtlr := test.GetDefaultStoreController(dir, ctlr.Log) - err := test.WriteImageToFileSystem(test.CreateRandomImage(), "zot-test", "0.0.1", testStoreCtlr) + err := test.WriteImageToFileSystem(CreateRandomImage(), "zot-test", "0.0.1", testStoreCtlr) So(err, ShouldBeNil) ctlr.Config.Storage.RootDirectory = dir @@ -204,7 +205,7 @@ func TestVerifyMandatoryAnnotations(t *testing.T) { dir := t.TempDir() testStoreCtlr := test.GetDefaultStoreController(dir, ctlr.Log) - err := test.WriteImageToFileSystem(test.CreateRandomImage(), "zot-test", "0.0.1", testStoreCtlr) + err := test.WriteImageToFileSystem(CreateRandomImage(), "zot-test", "0.0.1", testStoreCtlr) So(err, ShouldBeNil) ctlr.Config.Storage.RootDirectory = dir @@ -292,7 +293,7 @@ func TestVerifyMandatoryAnnotations(t *testing.T) { dir := t.TempDir() testStoreCtlr := test.GetDefaultStoreController(dir, ctlr.Log) - err := test.WriteImageToFileSystem(test.CreateRandomImage(), "zot-test", "0.0.1", testStoreCtlr) + err := test.WriteImageToFileSystem(CreateRandomImage(), "zot-test", "0.0.1", testStoreCtlr) So(err, ShouldBeNil) ctlr.Config.Storage.RootDirectory = dir @@ -379,7 +380,7 @@ func TestVerifyMandatoryAnnotations(t *testing.T) { dir := t.TempDir() testStoreCtlr := test.GetDefaultStoreController(dir, ctlr.Log) - err := test.WriteImageToFileSystem(test.CreateRandomImage(), "zot-test", "0.0.1", testStoreCtlr) + err := test.WriteImageToFileSystem(CreateRandomImage(), "zot-test", "0.0.1", testStoreCtlr) So(err, ShouldBeNil) ctlr.Config.Storage.RootDirectory = dir @@ -430,7 +431,7 @@ func TestVerifyMandatoryAnnotations(t *testing.T) { dir := t.TempDir() testStoreCtlr := test.GetDefaultStoreController(dir, ctlr.Log) - err := test.WriteImageToFileSystem(test.CreateRandomImage(), "zot-test", "0.0.1", testStoreCtlr) + err := test.WriteImageToFileSystem(CreateRandomImage(), "zot-test", "0.0.1", testStoreCtlr) So(err, ShouldBeNil) files, err := os.ReadDir(dir) @@ -485,7 +486,7 @@ func TestVerifyMandatoryAnnotationsFunction(t *testing.T) { dir := t.TempDir() testStoreCtlr := test.GetDefaultStoreController(dir, log.NewLogger("debug", "")) - err := test.WriteImageToFileSystem(test.CreateRandomImage(), "zot-test", "0.0.1", testStoreCtlr) + err := test.WriteImageToFileSystem(CreateRandomImage(), "zot-test", "0.0.1", testStoreCtlr) So(err, ShouldBeNil) var index ispec.Index @@ -517,7 +518,7 @@ func TestVerifyMandatoryAnnotationsFunction(t *testing.T) { dir := t.TempDir() testStoreCtlr := test.GetDefaultStoreController(dir, log.NewLogger("debug", "")) - err := test.WriteImageToFileSystem(test.CreateRandomImage(), "zot-test", "0.0.1", testStoreCtlr) + err := test.WriteImageToFileSystem(CreateRandomImage(), "zot-test", "0.0.1", testStoreCtlr) So(err, ShouldBeNil) var index ispec.Index @@ -549,7 +550,7 @@ func TestVerifyMandatoryAnnotationsFunction(t *testing.T) { dir := t.TempDir() testStoreCtlr := test.GetDefaultStoreController(dir, log.NewLogger("debug", "")) - err := test.WriteImageToFileSystem(test.CreateRandomImage(), "zot-test", "0.0.1", testStoreCtlr) + err := test.WriteImageToFileSystem(CreateRandomImage(), "zot-test", "0.0.1", testStoreCtlr) So(err, ShouldBeNil) var index ispec.Index @@ -612,7 +613,7 @@ func TestVerifyMandatoryAnnotationsFunction(t *testing.T) { dir := t.TempDir() testStoreCtlr := test.GetDefaultStoreController(dir, log.NewLogger("debug", "")) - err := test.WriteImageToFileSystem(test.CreateRandomImage(), "zot-test", "0.0.1", testStoreCtlr) + err := test.WriteImageToFileSystem(CreateRandomImage(), "zot-test", "0.0.1", testStoreCtlr) So(err, ShouldBeNil) var index ispec.Index @@ -674,7 +675,7 @@ func TestVerifyMandatoryAnnotationsFunction(t *testing.T) { dir := t.TempDir() testStoreCtlr := test.GetDefaultStoreController(dir, log.NewLogger("debug", "")) - err := test.WriteImageToFileSystem(test.CreateRandomImage(), "zot-test", "0.0.1", testStoreCtlr) + err := test.WriteImageToFileSystem(CreateRandomImage(), "zot-test", "0.0.1", testStoreCtlr) So(err, ShouldBeNil) var index ispec.Index @@ -738,7 +739,7 @@ func TestVerifyMandatoryAnnotationsFunction(t *testing.T) { dir := t.TempDir() testStoreCtlr := test.GetDefaultStoreController(dir, log.NewLogger("debug", "")) - err := test.WriteImageToFileSystem(test.CreateRandomImage(), "zot-test", "0.0.1", testStoreCtlr) + err := test.WriteImageToFileSystem(CreateRandomImage(), "zot-test", "0.0.1", testStoreCtlr) So(err, ShouldBeNil) var index ispec.Index @@ -811,7 +812,7 @@ func TestVerifyMandatoryAnnotationsFunction(t *testing.T) { dir := t.TempDir() testStoreCtlr := test.GetDefaultStoreController(dir, log.NewLogger("debug", "")) - err := test.WriteImageToFileSystem(test.CreateRandomImage(), "zot-test", "0.0.1", testStoreCtlr) + err := test.WriteImageToFileSystem(CreateRandomImage(), "zot-test", "0.0.1", testStoreCtlr) So(err, ShouldBeNil) var index ispec.Index diff --git a/pkg/extensions/monitoring/monitoring_test.go b/pkg/extensions/monitoring/monitoring_test.go index bb6f1599..0cef5edb 100644 --- a/pkg/extensions/monitoring/monitoring_test.go +++ b/pkg/extensions/monitoring/monitoring_test.go @@ -16,6 +16,7 @@ import ( extconf "zotregistry.io/zot/pkg/extensions/config" "zotregistry.io/zot/pkg/extensions/monitoring" "zotregistry.io/zot/pkg/test" + . "zotregistry.io/zot/pkg/test/image-utils" ) func TestExtensionMetrics(t *testing.T) { @@ -55,7 +56,7 @@ func TestExtensionMetrics(t *testing.T) { monitoring.IncUploadCounter(ctlr.Metrics, "alpine") srcStorageCtlr := test.GetDefaultStoreController(rootDir, ctlr.Log) - err := test.WriteImageToFileSystem(test.CreateDefaultImage(), "alpine", "0.0.1", srcStorageCtlr) + err := test.WriteImageToFileSystem(CreateDefaultImage(), "alpine", "0.0.1", srcStorageCtlr) So(err, ShouldBeNil) monitoring.SetStorageUsage(ctlr.Metrics, rootDir, "alpine") diff --git a/pkg/extensions/scrub/scrub_test.go b/pkg/extensions/scrub/scrub_test.go index c03c6fe6..f1c320be 100644 --- a/pkg/extensions/scrub/scrub_test.go +++ b/pkg/extensions/scrub/scrub_test.go @@ -23,6 +23,7 @@ import ( "zotregistry.io/zot/pkg/storage/cache" "zotregistry.io/zot/pkg/storage/local" "zotregistry.io/zot/pkg/test" + . "zotregistry.io/zot/pkg/test/image-utils" ) const ( @@ -63,7 +64,7 @@ func TestScrubExtension(t *testing.T) { ctlr := api.NewController(conf) srcStorageCtlr := test.GetDefaultStoreController(dir, log.NewLogger("debug", "")) - err = test.WriteImageToFileSystem(test.CreateDefaultVulnerableImage(), repoName, "0.0.1", srcStorageCtlr) + err = test.WriteImageToFileSystem(CreateDefaultVulnerableImage(), repoName, "0.0.1", srcStorageCtlr) So(err, ShouldBeNil) cm := test.NewControllerManager(ctlr) @@ -107,7 +108,7 @@ func TestScrubExtension(t *testing.T) { ctlr := api.NewController(conf) srcStorageCtlr := test.GetDefaultStoreController(dir, log.NewLogger("debug", "")) - image := test.CreateDefaultVulnerableImage() + image := CreateDefaultVulnerableImage() err = test.WriteImageToFileSystem(image, repoName, "0.0.1", srcStorageCtlr) So(err, ShouldBeNil) @@ -159,7 +160,7 @@ func TestScrubExtension(t *testing.T) { ctlr := api.NewController(conf) srcStorageCtlr := test.GetDefaultStoreController(dir, log.NewLogger("debug", "")) - image := test.CreateDefaultVulnerableImage() + image := CreateDefaultVulnerableImage() err = test.WriteImageToFileSystem(image, repoName, "0.0.1", srcStorageCtlr) So(err, ShouldBeNil) @@ -203,7 +204,7 @@ func TestRunScrubRepo(t *testing.T) { true, log, metrics, nil, cacheDriver) srcStorageCtlr := test.GetDefaultStoreController(dir, log) - image := test.CreateDefaultVulnerableImage() + image := CreateDefaultVulnerableImage() err = test.WriteImageToFileSystem(image, repoName, "0.0.1", srcStorageCtlr) So(err, ShouldBeNil) @@ -239,7 +240,7 @@ func TestRunScrubRepo(t *testing.T) { true, log, metrics, nil, cacheDriver) srcStorageCtlr := test.GetDefaultStoreController(dir, log) - image := test.CreateDefaultVulnerableImage() + image := CreateDefaultVulnerableImage() err = test.WriteImageToFileSystem(image, repoName, "0.0.1", srcStorageCtlr) So(err, ShouldBeNil) @@ -282,7 +283,7 @@ func TestRunScrubRepo(t *testing.T) { ) srcStorageCtlr := test.GetDefaultStoreController(dir, log) - image := test.CreateDefaultVulnerableImage() + image := CreateDefaultVulnerableImage() err = test.WriteImageToFileSystem(image, repoName, "0.0.1", srcStorageCtlr) So(err, ShouldBeNil) diff --git a/pkg/extensions/search/convert/convert_test.go b/pkg/extensions/search/convert/convert_test.go index 93b2fc55..1ceea2e0 100644 --- a/pkg/extensions/search/convert/convert_test.go +++ b/pkg/extensions/search/convert/convert_test.go @@ -20,6 +20,7 @@ import ( "zotregistry.io/zot/pkg/meta/boltdb" mTypes "zotregistry.io/zot/pkg/meta/types" "zotregistry.io/zot/pkg/test" + . "zotregistry.io/zot/pkg/test/image-utils" "zotregistry.io/zot/pkg/test/mocks" ) @@ -501,22 +502,22 @@ func TestPaginatedConvert(t *testing.T) { ctx := context.Background() var ( - badBothImage = test.CreateImageWith().DefaultLayers().ImageConfig( + badBothImage = CreateImageWith().DefaultLayers().ImageConfig( ispec.Image{Platform: ispec.Platform{OS: "bad-os", Architecture: "bad-arch"}}).Build() - badOsImage = test.CreateImageWith().DefaultLayers().ImageConfig( + badOsImage = CreateImageWith().DefaultLayers().ImageConfig( ispec.Image{Platform: ispec.Platform{OS: "bad-os", Architecture: "good-arch"}}).Build() - badArchImage = test.CreateImageWith().DefaultLayers().ImageConfig( + badArchImage = CreateImageWith().DefaultLayers().ImageConfig( ispec.Image{Platform: ispec.Platform{OS: "good-os", Architecture: "bad-arch"}}).Build() - goodImage = test.CreateImageWith().DefaultLayers().ImageConfig( + goodImage = CreateImageWith().DefaultLayers().ImageConfig( ispec.Image{Platform: ispec.Platform{OS: "good-os", Architecture: "good-arch"}}).Build() - randomImage1 = test.CreateRandomImage() - randomImage2 = test.CreateRandomImage() + randomImage1 = CreateRandomImage() + randomImage2 = CreateRandomImage() - badMultiArch = test.CreateMultiarchWith().Images( - []test.Image{badBothImage, badOsImage, badArchImage, randomImage1}).Build() - goodMultiArch = test.CreateMultiarchWith().Images( - []test.Image{badOsImage, badArchImage, randomImage2, goodImage}).Build() + badMultiArch = CreateMultiarchWith().Images( + []Image{badBothImage, badOsImage, badArchImage, randomImage1}).Build() + goodMultiArch = CreateMultiarchWith().Images( + []Image{badOsImage, badArchImage, randomImage2, goodImage}).Build() ) reposMeta, manifestMetaMap, indexDataMap := test.GetMetadataForRepos( @@ -563,7 +564,7 @@ func TestPaginatedConvert(t *testing.T) { Name: "repo5-signed", Images: []test.RepoImage{ {Image: goodImage, Tag: "goodImage"}, // is fake signed by the image below - {Image: test.CreateFakeTestSignature(goodImage.DescriptorRef())}, + {Image: CreateFakeTestSignature(goodImage.DescriptorRef())}, }, }, ) @@ -835,14 +836,14 @@ func TestGetOneManifestAnnotations(t *testing.T) { ispec.AnnotationSource: "IndexSource", } - imageWithConfigAnnotations := test.CreateImageWith().DefaultLayers(). + imageWithConfigAnnotations := CreateImageWith().DefaultLayers(). ImageConfig(ispec.Image{ Config: ispec.ImageConfig{ Labels: configLabels, }, }).Build() - imageWithManifestAndConfigAnnotations := test.CreateImageWith().DefaultLayers(). + imageWithManifestAndConfigAnnotations := CreateImageWith().DefaultLayers(). ImageConfig(ispec.Image{ Config: ispec.ImageConfig{ Labels: configLabels, @@ -850,8 +851,8 @@ func TestGetOneManifestAnnotations(t *testing.T) { }).Annotations(manifestAnnotations).Build() // -------------------------------------------------------- - indexWithAnnotations := test.CreateMultiarchWith().Images( - []test.Image{imageWithManifestAndConfigAnnotations}, + indexWithAnnotations := CreateMultiarchWith().Images( + []Image{imageWithManifestAndConfigAnnotations}, ).Annotations(indexAnnotations).Build() repoMeta, manifestMetadata, indexData := test.GetMetadataForRepos(test.Repo{ @@ -875,8 +876,8 @@ func TestGetOneManifestAnnotations(t *testing.T) { So(*imageSummary.Authors, ShouldResemble, "IndexAuthors") // -------------------------------------------------------- - indexWithManifestAndConfigAnnotations := test.CreateMultiarchWith().Images( - []test.Image{imageWithManifestAndConfigAnnotations, test.CreateRandomImage(), test.CreateRandomImage()}, + indexWithManifestAndConfigAnnotations := CreateMultiarchWith().Images( + []Image{imageWithManifestAndConfigAnnotations, CreateRandomImage(), CreateRandomImage()}, ).Build() repoMeta, manifestMetadata, indexData = test.GetMetadataForRepos(test.Repo{ @@ -896,8 +897,8 @@ func TestGetOneManifestAnnotations(t *testing.T) { So(*imageSummary.Vendor, ShouldResemble, "ManifestVendor") So(*imageSummary.Authors, ShouldResemble, "ManifestAuthors") // -------------------------------------------------------- - indexWithConfigAnnotations := test.CreateMultiarchWith().Images( - []test.Image{imageWithConfigAnnotations, test.CreateRandomImage(), test.CreateRandomImage()}, + indexWithConfigAnnotations := CreateMultiarchWith().Images( + []Image{imageWithConfigAnnotations, CreateRandomImage(), CreateRandomImage()}, ).Build() repoMeta, manifestMetadata, indexData = test.GetMetadataForRepos(test.Repo{ @@ -918,9 +919,9 @@ func TestGetOneManifestAnnotations(t *testing.T) { So(*imageSummary.Authors, ShouldResemble, "ConfigAuthors") //-------------------------------------------------------- - indexWithMixAnnotations := test.CreateMultiarchWith().Images( - []test.Image{ - test.CreateImageWith().DefaultLayers().ImageConfig(ispec.Image{ + indexWithMixAnnotations := CreateMultiarchWith().Images( + []Image{ + CreateImageWith().DefaultLayers().ImageConfig(ispec.Image{ Config: ispec.ImageConfig{ Labels: map[string]string{ ispec.AnnotationDescription: "ConfigDescription", @@ -931,8 +932,8 @@ func TestGetOneManifestAnnotations(t *testing.T) { ispec.AnnotationVendor: "ManifestVendor", ispec.AnnotationAuthors: "ManifestAuthors", }).Build(), - test.CreateRandomImage(), - test.CreateRandomImage(), + CreateRandomImage(), + CreateRandomImage(), }, ).Annotations( map[string]string{ @@ -960,7 +961,7 @@ func TestGetOneManifestAnnotations(t *testing.T) { So(*imageSummary.Source, ShouldResemble, "IndexSource") //-------------------------------------------------------- - indexWithNoAnnotations := test.CreateRandomMultiarch() + indexWithNoAnnotations := CreateRandomMultiarch() repoMeta, manifestMetadata, indexData = test.GetMetadataForRepos(test.Repo{ Name: "repo", @@ -988,7 +989,7 @@ func TestDownloadCount(t *testing.T) { Name: "repo", Images: []test.RepoImage{ { - Image: test.CreateRandomImage(), + Image: CreateRandomImage(), Tag: "10-downloads", Statistics: mTypes.DescriptorStatistics{ DownloadCount: 10, @@ -1005,8 +1006,8 @@ func TestDownloadCount(t *testing.T) { }) Convey("index", t, func() { - img1, img2, img3 := test.CreateRandomImage(), test.CreateRandomImage(), test.CreateRandomImage() - multiArch := test.CreateMultiarchWith().Images([]test.Image{img1, img2, img3}).Build() + img1, img2, img3 := CreateRandomImage(), CreateRandomImage(), CreateRandomImage() + multiArch := CreateMultiarchWith().Images([]Image{img1, img2, img3}).Build() repoMeta, manifestMetaMap, indexDataMap := test.GetMetadataForRepos( test.Repo{ @@ -1033,20 +1034,20 @@ func TestDownloadCount(t *testing.T) { }) Convey("index + manifest mixed", t, func() { - img1 := test.CreateRandomImage() - img2 := test.CreateRandomImage() - img3 := test.CreateImageWith().DefaultLayers().ImageConfig( - ispec.Image{Created: test.DateRef(2020, 1, 1, 1, 1, 1, 0, time.UTC)}, + img1 := CreateRandomImage() + img2 := CreateRandomImage() + img3 := CreateImageWith().DefaultLayers().ImageConfig( + ispec.Image{Created: DateRef(2020, 1, 1, 1, 1, 1, 0, time.UTC)}, ).Build() - multiArch := test.CreateMultiarchWith().Images([]test.Image{img1, img2, img3}).Build() + multiArch := CreateMultiarchWith().Images([]Image{img1, img2, img3}).Build() repoMeta, manifestMetaMap, indexDataMap := test.GetMetadataForRepos( test.Repo{ Name: "repo", Images: []test.RepoImage{ { - Image: test.CreateRandomImage(), + Image: CreateRandomImage(), Tag: "5-downloads", Statistics: mTypes.DescriptorStatistics{DownloadCount: 5}, }, diff --git a/pkg/extensions/search/cve/cve_test.go b/pkg/extensions/search/cve/cve_test.go index b93f56b4..38a01975 100644 --- a/pkg/extensions/search/cve/cve_test.go +++ b/pkg/extensions/search/cve/cve_test.go @@ -40,6 +40,7 @@ import ( storageConstants "zotregistry.io/zot/pkg/storage/constants" "zotregistry.io/zot/pkg/storage/local" . "zotregistry.io/zot/pkg/test" + . "zotregistry.io/zot/pkg/test/image-utils" "zotregistry.io/zot/pkg/test/mocks" ) diff --git a/pkg/extensions/search/cve/trivy/scanner_internal_test.go b/pkg/extensions/search/cve/trivy/scanner_internal_test.go index 6f791a55..090c7230 100644 --- a/pkg/extensions/search/cve/trivy/scanner_internal_test.go +++ b/pkg/extensions/search/cve/trivy/scanner_internal_test.go @@ -29,6 +29,7 @@ import ( "zotregistry.io/zot/pkg/storage/local" storageTypes "zotregistry.io/zot/pkg/storage/types" "zotregistry.io/zot/pkg/test" + . "zotregistry.io/zot/pkg/test/image-utils" "zotregistry.io/zot/pkg/test/mocks" ) @@ -182,7 +183,7 @@ func TestTrivyLibraryErrors(t *testing.T) { rootDir := t.TempDir() storageCtlr := test.GetDefaultStoreController(rootDir, log.NewLogger("debug", "")) - err := test.WriteImageToFileSystem(test.CreateDefaultVulnerableImage(), "zot-test", "0.0.1", storageCtlr) + err := test.WriteImageToFileSystem(CreateDefaultVulnerableImage(), "zot-test", "0.0.1", storageCtlr) So(err, ShouldBeNil) log := log.NewLogger("debug", "") diff --git a/pkg/extensions/search/cve/trivy/scanner_test.go b/pkg/extensions/search/cve/trivy/scanner_test.go index 1ff033f8..28043fc5 100644 --- a/pkg/extensions/search/cve/trivy/scanner_test.go +++ b/pkg/extensions/search/cve/trivy/scanner_test.go @@ -22,12 +22,14 @@ import ( "zotregistry.io/zot/pkg/storage" "zotregistry.io/zot/pkg/storage/local" "zotregistry.io/zot/pkg/test" + testc "zotregistry.io/zot/pkg/test/common" + . "zotregistry.io/zot/pkg/test/image-utils" "zotregistry.io/zot/pkg/test/mocks" ) func TestScanBigTestFile(t *testing.T) { Convey("Scan zot-test", t, func() { - projRootDir, err := test.GetProjectRootDir() + projRootDir, err := testc.GetProjectRootDir() So(err, ShouldBeNil) testImage := filepath.Join(projRootDir, "test/data/zot-test") @@ -86,13 +88,13 @@ func TestScanningByDigest(t *testing.T) { cm.StartAndWait(port) defer cm.StopServer() // push index with 2 manifests: one with vulns and one without - vulnImage := test.CreateDefaultVulnerableImage() + vulnImage := CreateDefaultVulnerableImage() - simpleImage := test.CreateRandomImage() + simpleImage := CreateRandomImage() - multiArch := test.GetMultiarchImageForImages([]test.Image{simpleImage, vulnImage}) //nolint:staticcheck + multiArch := test.GetMultiarchImageForImages([]Image{simpleImage, vulnImage}) //nolint:staticcheck - err := test.UploadMultiarchImage(multiArch, baseURL, "multi-arch", "multi-arch-tag") + err := UploadMultiarchImage(multiArch, baseURL, "multi-arch", "multi-arch-tag") So(err, ShouldBeNil) // scan @@ -103,9 +105,9 @@ func TestScanningByDigest(t *testing.T) { cveMap, err := scanner.ScanImage("multi-arch@" + vulnImage.DigestStr()) So(err, ShouldBeNil) - So(cveMap, ShouldContainKey, test.Vulnerability1ID) - So(cveMap, ShouldContainKey, test.Vulnerability2ID) - So(cveMap, ShouldContainKey, test.Vulnerability3ID) + So(cveMap, ShouldContainKey, Vulnerability1ID) + So(cveMap, ShouldContainKey, Vulnerability2ID) + So(cveMap, ShouldContainKey, Vulnerability3ID) cveMap, err = scanner.ScanImage("multi-arch@" + simpleImage.DigestStr()) So(err, ShouldBeNil) @@ -113,15 +115,15 @@ func TestScanningByDigest(t *testing.T) { cveMap, err = scanner.ScanImage("multi-arch@" + multiArch.DigestStr()) So(err, ShouldBeNil) - So(cveMap, ShouldContainKey, test.Vulnerability1ID) - So(cveMap, ShouldContainKey, test.Vulnerability2ID) - So(cveMap, ShouldContainKey, test.Vulnerability3ID) + So(cveMap, ShouldContainKey, Vulnerability1ID) + So(cveMap, ShouldContainKey, Vulnerability2ID) + So(cveMap, ShouldContainKey, Vulnerability3ID) cveMap, err = scanner.ScanImage("multi-arch:multi-arch-tag") So(err, ShouldBeNil) - So(cveMap, ShouldContainKey, test.Vulnerability1ID) - So(cveMap, ShouldContainKey, test.Vulnerability2ID) - So(cveMap, ShouldContainKey, test.Vulnerability3ID) + So(cveMap, ShouldContainKey, Vulnerability1ID) + So(cveMap, ShouldContainKey, Vulnerability2ID) + So(cveMap, ShouldContainKey, Vulnerability3ID) }) } @@ -152,7 +154,7 @@ func TestScannerErrors(t *testing.T) { func TestVulnerableLayer(t *testing.T) { Convey("Vulnerable layer", t, func() { - vulnerableLayer, err := test.GetLayerWithVulnerability() + vulnerableLayer, err := GetLayerWithVulnerability() So(err, ShouldBeNil) created, err := time.Parse(time.RFC3339, "2023-03-29T18:19:24Z") @@ -174,7 +176,7 @@ func TestVulnerableLayer(t *testing.T) { }, } - img := test.CreateImageWith(). + img := CreateImageWith(). LayerBlobs([][]byte{vulnerableLayer}). ImageConfig(config). Build() diff --git a/pkg/extensions/search/digest_test.go b/pkg/extensions/search/digest_test.go index b8fd5a33..ede66ca3 100644 --- a/pkg/extensions/search/digest_test.go +++ b/pkg/extensions/search/digest_test.go @@ -21,6 +21,7 @@ import ( "zotregistry.io/zot/pkg/common" extconf "zotregistry.io/zot/pkg/extensions/config" . "zotregistry.io/zot/pkg/test" + . "zotregistry.io/zot/pkg/test/image-utils" ) type ImgResponseForDigest struct { @@ -294,12 +295,10 @@ func TestDigestSearchHTTPSubPaths(t *testing.T) { config, layers, manifest, err := GetImageComponents(100) So(err, ShouldBeNil) - err = PushTestImage("a/zot-cve-test", "0.0.1", baseURL, - manifest, config, layers) + err = UploadImage(Image{Manifest: manifest, Config: config, Layers: layers}, baseURL, "a/zot-cve-test", "0.0.1") So(err, ShouldBeNil) - err = PushTestImage("a/zot-test", "0.0.1", baseURL, - manifest, config, layers) + err = UploadImage(Image{Manifest: manifest, Config: config, Layers: layers}, baseURL, "a/zot-test", "0.0.1") So(err, ShouldBeNil) resp, err := resty.R().Get(baseURL + "/v2/") diff --git a/pkg/extensions/search/search_test.go b/pkg/extensions/search/search_test.go index 2bdf1b3b..aa92ee5a 100644 --- a/pkg/extensions/search/search_test.go +++ b/pkg/extensions/search/search_test.go @@ -44,6 +44,7 @@ import ( "zotregistry.io/zot/pkg/storage/local" storageTypes "zotregistry.io/zot/pkg/storage/types" . "zotregistry.io/zot/pkg/test" + . "zotregistry.io/zot/pkg/test/image-utils" "zotregistry.io/zot/pkg/test/mocks" ocilayout "zotregistry.io/zot/pkg/test/oci-layout" ) @@ -402,20 +403,16 @@ func TestRepoListWithNewestImage(t *testing.T) { config, layers, manifest, err := GetImageComponents(100) So(err, ShouldBeNil) - err = PushTestImage("zot-cve-test", "0.0.1", baseURL, - manifest, config, layers) + err = UploadImage(Image{Manifest: manifest, Config: config, Layers: layers}, baseURL, "zot-cve-test", "0.0.1") So(err, ShouldBeNil) - err = PushTestImage("a/zot-cve-test", "0.0.1", baseURL, - manifest, config, layers) + err = UploadImage(Image{Manifest: manifest, Config: config, Layers: layers}, baseURL, "a/zot-cve-test", "0.0.1") So(err, ShouldBeNil) - err = PushTestImage("zot-test", "0.0.1", baseURL, - manifest, config, layers) + err = UploadImage(Image{Manifest: manifest, Config: config, Layers: layers}, baseURL, "zot-test", "0.0.1") So(err, ShouldBeNil) - err = PushTestImage("a/zot-test", "0.0.1", baseURL, - manifest, config, layers) + err = UploadImage(Image{Manifest: manifest, Config: config, Layers: layers}, baseURL, "a/zot-test", "0.0.1") So(err, ShouldBeNil) resp, err := resty.R().Get(baseURL + "/v2/") @@ -746,20 +743,16 @@ func TestRepoListWithNewestImage(t *testing.T) { config, layers, manifest, err := GetImageComponents(100) So(err, ShouldBeNil) - err = PushTestImage("zot-cve-test", "0.0.1", baseURL, - manifest, config, layers) + err = UploadImage(Image{Manifest: manifest, Config: config, Layers: layers}, baseURL, "zot-cve-test", "0.0.1") So(err, ShouldBeNil) - err = PushTestImage("a/zot-cve-test", "0.0.1", baseURL, - manifest, config, layers) + err = UploadImage(Image{Manifest: manifest, Config: config, Layers: layers}, baseURL, "a/zot-cve-test", "0.0.1") So(err, ShouldBeNil) - err = PushTestImage("zot-test", "0.0.1", baseURL, - manifest, config, layers) + err = UploadImage(Image{Manifest: manifest, Config: config, Layers: layers}, baseURL, "zot-test", "0.0.1") So(err, ShouldBeNil) - err = PushTestImage("a/zot-test", "0.0.1", baseURL, - manifest, config, layers) + err = UploadImage(Image{Manifest: manifest, Config: config, Layers: layers}, baseURL, "a/zot-test", "0.0.1") So(err, ShouldBeNil) query := `{ @@ -1307,20 +1300,16 @@ func TestExpandedRepoInfo(t *testing.T) { manifest.Annotations = make(map[string]string) manifest.Annotations["org.opencontainers.image.vendor"] = "zot" - err = PushTestImage("zot-cve-test", "0.0.1", baseURL, - manifest, config, layers) + err = UploadImage(Image{Manifest: manifest, Config: config, Layers: layers}, baseURL, "zot-cve-test", "0.0.1") So(err, ShouldBeNil) - err = PushTestImage("a/zot-cve-test", "0.0.1", baseURL, - manifest, config, layers) + err = UploadImage(Image{Manifest: manifest, Config: config, Layers: layers}, baseURL, "a/zot-cve-test", "0.0.1") So(err, ShouldBeNil) - err = PushTestImage("zot-test", "0.0.1", baseURL, - manifest, config, layers) + err = UploadImage(Image{Manifest: manifest, Config: config, Layers: layers}, baseURL, "zot-test", "0.0.1") So(err, ShouldBeNil) - err = PushTestImage("a/zot-test", "0.0.1", baseURL, - manifest, config, layers) + err = UploadImage(Image{Manifest: manifest, Config: config, Layers: layers}, baseURL, "a/zot-test", "0.0.1") So(err, ShouldBeNil) log := log.NewLogger("debug", "") @@ -3960,20 +3949,16 @@ func TestImageList(t *testing.T) { manifest, err = updateManifestConfig(manifest, config) So(err, ShouldBeNil) - err = PushTestImage("zot-cve-test", "0.0.1", baseURL, - manifest, config, layers) + err = UploadImage(Image{Manifest: manifest, Config: config, Layers: layers}, baseURL, "zot-cve-test", "0.0.1") So(err, ShouldBeNil) - err = PushTestImage("a/zot-cve-test", "0.0.1", baseURL, - manifest, config, layers) + err = UploadImage(Image{Manifest: manifest, Config: config, Layers: layers}, baseURL, "a/zot-cve-test", "0.0.1") So(err, ShouldBeNil) - err = PushTestImage("zot-test", "0.0.1", baseURL, - manifest, config, layers) + err = UploadImage(Image{Manifest: manifest, Config: config, Layers: layers}, baseURL, "zot-test", "0.0.1") So(err, ShouldBeNil) - err = PushTestImage("a/zot-test", "0.0.1", baseURL, - manifest, config, layers) + err = UploadImage(Image{Manifest: manifest, Config: config, Layers: layers}, baseURL, "a/zot-test", "0.0.1") So(err, ShouldBeNil) imageStore := ctlr.StoreController.DefaultStore diff --git a/pkg/extensions/search/userprefs_test.go b/pkg/extensions/search/userprefs_test.go index 4e7315f6..a0f5dfba 100644 --- a/pkg/extensions/search/userprefs_test.go +++ b/pkg/extensions/search/userprefs_test.go @@ -24,6 +24,7 @@ import ( "zotregistry.io/zot/pkg/storage" "zotregistry.io/zot/pkg/storage/local" . "zotregistry.io/zot/pkg/test" + . "zotregistry.io/zot/pkg/test/image-utils" ) //nolint:dupl diff --git a/pkg/extensions/sync/sync_internal_test.go b/pkg/extensions/sync/sync_internal_test.go index fae4e02e..73391c4f 100644 --- a/pkg/extensions/sync/sync_internal_test.go +++ b/pkg/extensions/sync/sync_internal_test.go @@ -32,6 +32,7 @@ import ( storageConstants "zotregistry.io/zot/pkg/storage/constants" "zotregistry.io/zot/pkg/storage/local" "zotregistry.io/zot/pkg/test" + . "zotregistry.io/zot/pkg/test/image-utils" "zotregistry.io/zot/pkg/test/inject" "zotregistry.io/zot/pkg/test/mocks" ) @@ -450,7 +451,7 @@ func TestConvertDockerToOCI(t *testing.T) { srcStorageCtlr := test.GetDefaultStoreController(dir, log.NewLogger("debug", "")) - err := test.WriteImageToFileSystem(test.CreateDefaultImage(), "zot-test", "0.0.1", srcStorageCtlr) + err := test.WriteImageToFileSystem(CreateDefaultImage(), "zot-test", "0.0.1", srcStorageCtlr) So(err, ShouldBeNil) imageRef, err := layout.NewReference(path.Join(dir, "zot-test"), "0.0.1") diff --git a/pkg/extensions/sync/sync_test.go b/pkg/extensions/sync/sync_test.go index f07ac9db..5cdd1648 100644 --- a/pkg/extensions/sync/sync_test.go +++ b/pkg/extensions/sync/sync_test.go @@ -49,6 +49,7 @@ import ( storageConstants "zotregistry.io/zot/pkg/storage/constants" "zotregistry.io/zot/pkg/test" testc "zotregistry.io/zot/pkg/test/common" + . "zotregistry.io/zot/pkg/test/image-utils" "zotregistry.io/zot/pkg/test/mocks" ) @@ -139,12 +140,12 @@ func makeUpstreamServer( srcDir := t.TempDir() srcStorageCtrl := test.GetDefaultStoreController(srcDir, log.NewLogger("debug", "")) - err := test.WriteImageToFileSystem(test.CreateDefaultImage(), "zot-test", "0.0.1", srcStorageCtrl) + err := test.WriteImageToFileSystem(CreateDefaultImage(), "zot-test", "0.0.1", srcStorageCtrl) if err != nil { panic(err) } - err = test.WriteImageToFileSystem(test.CreateDefaultVulnerableImage(), "zot-cve-test", "0.0.1", srcStorageCtrl) + err = test.WriteImageToFileSystem(CreateDefaultVulnerableImage(), "zot-cve-test", "0.0.1", srcStorageCtrl) if err != nil { panic(err) } @@ -742,8 +743,8 @@ func TestOnDemand(t *testing.T) { manifestDigest := godigest.FromBytes(manifestBlob) - err = test.UploadImage( - test.Image{Config: imageConfig, Layers: layers, Manifest: manifest}, + err = UploadImage( + Image{Config: imageConfig, Layers: layers, Manifest: manifest}, srcBaseURL, "remote-repo", "test", ) So(err, ShouldBeNil) @@ -1104,8 +1105,8 @@ func TestSyncWithNonDistributableBlob(t *testing.T) { manifest.Layers = append(manifest.Layers, nonDistributableLayerDesc) - err = test.UploadImage( - test.Image{Config: imageConfig, Layers: layers, Manifest: manifest}, + err = UploadImage( + Image{Config: imageConfig, Layers: layers, Manifest: manifest}, srcBaseURL, repoName, tag, ) @@ -1285,8 +1286,8 @@ func TestDockerImagesAreSkipped(t *testing.T) { manifestDigest := godigest.FromBytes(manifestContent) - err = test.UploadImage( - test.Image{ + err = UploadImage( + Image{ Manifest: manifest, Config: config, Layers: layers, @@ -3114,10 +3115,10 @@ func TestSubPaths(t *testing.T) { subpath := "/subpath" srcStorageCtlr := test.GetDefaultStoreController(path.Join(srcDir, subpath), log.NewLogger("debug", "")) - err := test.WriteImageToFileSystem(test.CreateDefaultImage(), "zot-test", "0.0.1", srcStorageCtlr) + err := test.WriteImageToFileSystem(CreateDefaultImage(), "zot-test", "0.0.1", srcStorageCtlr) So(err, ShouldBeNil) - err = test.WriteImageToFileSystem(test.CreateDefaultVulnerableImage(), "zot-cve-test", "0.0.1", srcStorageCtlr) + err = test.WriteImageToFileSystem(CreateDefaultVulnerableImage(), "zot-cve-test", "0.0.1", srcStorageCtlr) So(err, ShouldBeNil) srcConfig.Storage.RootDirectory = srcDir @@ -4513,7 +4514,7 @@ func TestSyncedSignaturesMetaDB(t *testing.T) { signedImage, err := test.GetRandomImage() //nolint:staticcheck So(err, ShouldBeNil) - err = test.UploadImage(signedImage, srcBaseURL, repoName, tag) + err = UploadImage(signedImage, srcBaseURL, repoName, tag) So(err, ShouldBeNil) err = test.SignImageUsingNotary(repoName+":"+tag, srcPort) @@ -4588,10 +4589,10 @@ func TestOnDemandRetryGoroutine(t *testing.T) { srcStorageCtlr := test.GetDefaultStoreController(srcDir, log.NewLogger("debug", "")) - err := test.WriteImageToFileSystem(test.CreateDefaultImage(), "zot-test", "0.0.1", srcStorageCtlr) + err := test.WriteImageToFileSystem(CreateDefaultImage(), "zot-test", "0.0.1", srcStorageCtlr) So(err, ShouldBeNil) - err = test.WriteImageToFileSystem(test.CreateDefaultVulnerableImage(), "zot-cve-test", "0.0.1", srcStorageCtlr) + err = test.WriteImageToFileSystem(CreateDefaultVulnerableImage(), "zot-cve-test", "0.0.1", srcStorageCtlr) So(err, ShouldBeNil) srcConfig.Storage.RootDirectory = srcDir @@ -4801,10 +4802,10 @@ func TestOnDemandMultipleImage(t *testing.T) { srcStorageCtlr := test.GetDefaultStoreController(srcDir, log.NewLogger("debug", "")) - err := test.WriteImageToFileSystem(test.CreateDefaultImage(), "zot-test", "0.0.1", srcStorageCtlr) + err := test.WriteImageToFileSystem(CreateDefaultImage(), "zot-test", "0.0.1", srcStorageCtlr) So(err, ShouldBeNil) - err = test.WriteImageToFileSystem(test.CreateDefaultVulnerableImage(), "zot-cve-test", "0.0.1", srcStorageCtlr) + err = test.WriteImageToFileSystem(CreateDefaultVulnerableImage(), "zot-cve-test", "0.0.1", srcStorageCtlr) So(err, ShouldBeNil) srcConfig.Storage.RootDirectory = srcDir @@ -5449,10 +5450,10 @@ func TestSyncOnlyDiff(t *testing.T) { // copy images so we have them before syncing, sync should not pull them again destStorageCtrl := test.GetDefaultStoreController(destDir, log.NewLogger("debug", "")) - err := test.WriteImageToFileSystem(test.CreateDefaultImage(), "zot-test", "0.0.1", destStorageCtrl) + err := test.WriteImageToFileSystem(CreateDefaultImage(), "zot-test", "0.0.1", destStorageCtrl) So(err, ShouldBeNil) - err = test.WriteImageToFileSystem(test.CreateDefaultVulnerableImage(), "zot-cve-test", "0.0.1", destStorageCtrl) + err = test.WriteImageToFileSystem(CreateDefaultVulnerableImage(), "zot-cve-test", "0.0.1", destStorageCtrl) So(err, ShouldBeNil) destConfig.Storage.RootDirectory = destDir @@ -5535,10 +5536,10 @@ func TestSyncWithDiffDigest(t *testing.T) { // copy images so we have them before syncing, sync should not pull them again srcStorageCtlr := test.GetDefaultStoreController(destDir, log.NewLogger("debug", "")) - err := test.WriteImageToFileSystem(test.CreateDefaultImage(), "zot-test", "0.0.1", srcStorageCtlr) + err := test.WriteImageToFileSystem(CreateDefaultImage(), "zot-test", "0.0.1", srcStorageCtlr) So(err, ShouldBeNil) - err = test.WriteImageToFileSystem(test.CreateDefaultVulnerableImage(), "zot-cve-test", "0.0.1", srcStorageCtlr) + err = test.WriteImageToFileSystem(CreateDefaultVulnerableImage(), "zot-cve-test", "0.0.1", srcStorageCtlr) So(err, ShouldBeNil) destConfig.Storage.RootDirectory = destDir @@ -6209,8 +6210,8 @@ func TestSyncImageIndex(t *testing.T) { manifestDigest := godigest.FromBytes(manifestContent) - err = test.UploadImage( - test.Image{ + err = UploadImage( + Image{ Manifest: manifest, Config: config, Layers: layers, diff --git a/pkg/meta/hooks_test.go b/pkg/meta/hooks_test.go index f52a25e5..4589203a 100644 --- a/pkg/meta/hooks_test.go +++ b/pkg/meta/hooks_test.go @@ -20,6 +20,7 @@ import ( "zotregistry.io/zot/pkg/storage" "zotregistry.io/zot/pkg/storage/local" "zotregistry.io/zot/pkg/test" + . "zotregistry.io/zot/pkg/test/image-utils" "zotregistry.io/zot/pkg/test/mocks" ) @@ -48,7 +49,7 @@ func TestOnUpdateManifest(t *testing.T) { So(err, ShouldBeNil) err = test.WriteImageToFileSystem( - test.Image{ + Image{ Config: config, Manifest: manifest, Layers: layers, }, "repo", "tag1", storeController) So(err, ShouldBeNil) diff --git a/pkg/meta/parse_test.go b/pkg/meta/parse_test.go index 67a351b0..802afc0a 100644 --- a/pkg/meta/parse_test.go +++ b/pkg/meta/parse_test.go @@ -25,6 +25,7 @@ import ( "zotregistry.io/zot/pkg/storage/local" storageTypes "zotregistry.io/zot/pkg/storage/types" "zotregistry.io/zot/pkg/test" + . "zotregistry.io/zot/pkg/test/image-utils" "zotregistry.io/zot/pkg/test/mocks" ) @@ -411,7 +412,7 @@ func RunParseStorageTests(rootDir string, metaDB mTypes.MetaDB) { manifests = append(manifests, manifest) err = test.WriteImageToFileSystem( - test.Image{ + Image{ Config: config, Layers: layers, Manifest: manifest, @@ -432,7 +433,7 @@ func RunParseStorageTests(rootDir string, metaDB mTypes.MetaDB) { So(err, ShouldBeNil) err = test.WriteImageToFileSystem( - test.Image{ + Image{ Config: config, Layers: layers, Manifest: manifest, @@ -494,7 +495,7 @@ func RunParseStorageTests(rootDir string, metaDB mTypes.MetaDB) { So(err, ShouldBeNil) err = test.WriteImageToFileSystem( - test.Image{ + Image{ Config: config, Layers: layers, Manifest: manifest, @@ -515,7 +516,7 @@ func RunParseStorageTests(rootDir string, metaDB mTypes.MetaDB) { So(err, ShouldBeNil) err = test.WriteImageToFileSystem( - test.Image{ + Image{ Config: config, Layers: layers, Manifest: manifest, diff --git a/pkg/storage/common/common_test.go b/pkg/storage/common/common_test.go index 782bde5f..98d80bce 100644 --- a/pkg/storage/common/common_test.go +++ b/pkg/storage/common/common_test.go @@ -23,6 +23,7 @@ import ( storageConstants "zotregistry.io/zot/pkg/storage/constants" "zotregistry.io/zot/pkg/storage/local" "zotregistry.io/zot/pkg/test" + . "zotregistry.io/zot/pkg/test/image-utils" "zotregistry.io/zot/pkg/test/mocks" ) @@ -183,7 +184,7 @@ func TestGetReferrersErrors(t *testing.T) { }) storageCtlr := storage.StoreController{DefaultStore: imgStore} - err := test.WriteImageToFileSystem(test.CreateDefaultImage(), "zot-test", "0.0.1", storageCtlr) + err := test.WriteImageToFileSystem(CreateDefaultImage(), "zot-test", "0.0.1", storageCtlr) So(err, ShouldBeNil) digest := godigest.FromBytes([]byte("{}")) diff --git a/pkg/storage/local/local_test.go b/pkg/storage/local/local_test.go index 53f4331b..d3ff5d1e 100644 --- a/pkg/storage/local/local_test.go +++ b/pkg/storage/local/local_test.go @@ -37,6 +37,7 @@ import ( "zotregistry.io/zot/pkg/storage/local" storageTypes "zotregistry.io/zot/pkg/storage/types" "zotregistry.io/zot/pkg/test" + . "zotregistry.io/zot/pkg/test/image-utils" "zotregistry.io/zot/pkg/test/mocks" ) @@ -210,7 +211,7 @@ func TestGetOrasReferrers(t *testing.T) { storageConstants.DefaultUntaggedImgeRetentionDelay, true, true, log, metrics, nil, cacheDriver) Convey("Get referrers", t, func(c C) { - err := test.WriteImageToFileSystem(test.CreateDefaultVulnerableImage(), "zot-test", "0.0.1", storage.StoreController{ + err := test.WriteImageToFileSystem(CreateDefaultVulnerableImage(), "zot-test", "0.0.1", storage.StoreController{ DefaultStore: imgStore, }) So(err, ShouldBeNil) @@ -1094,7 +1095,7 @@ func FuzzGetOrasReferrers(f *testing.F) { storageConstants.DefaultUntaggedImgeRetentionDelay, true, true, *log, metrics, nil, cacheDriver) storageCtlr := storage.StoreController{DefaultStore: imgStore} - err := test.WriteImageToFileSystem(test.CreateDefaultVulnerableImage(), "zot-test", "0.0.1", storageCtlr) + err := test.WriteImageToFileSystem(CreateDefaultVulnerableImage(), "zot-test", "0.0.1", storageCtlr) if err != nil { t.Error(err) } @@ -2036,7 +2037,7 @@ func TestGarbageCollectForImageStore(t *testing.T) { true, true, log, metrics, nil, cacheDriver) repoName := "gc-all-repos-short" - image := test.CreateDefaultVulnerableImage() + image := CreateDefaultVulnerableImage() err := test.WriteImageToFileSystem(image, repoName, "0.0.1", storage.StoreController{ DefaultStore: imgStore, }) @@ -2076,7 +2077,7 @@ func TestGarbageCollectForImageStore(t *testing.T) { true, true, log, metrics, nil, cacheDriver) repoName := "gc-all-repos-short" - image := test.CreateDefaultVulnerableImage() + image := CreateDefaultVulnerableImage() err := test.WriteImageToFileSystem(image, repoName, "0.0.1", storage.StoreController{ DefaultStore: imgStore, }) @@ -2113,7 +2114,7 @@ func TestGarbageCollectForImageStore(t *testing.T) { repoName := "gc-sig" storeController := storage.StoreController{DefaultStore: imgStore} - img := test.CreateRandomImage() + img := CreateRandomImage() err := test.WriteImageToFileSystem(img, repoName, "tag1", storeController) So(err, ShouldBeNil) @@ -2122,7 +2123,7 @@ func TestGarbageCollectForImageStore(t *testing.T) { cosignTag, err := test.GetCosignSignatureTagForManifest(img.Manifest) So(err, ShouldBeNil) - cosignSig := test.CreateRandomImage() + cosignSig := CreateRandomImage() So(err, ShouldBeNil) err = test.WriteImageToFileSystem(cosignSig, repoName, cosignTag, storeController) @@ -2136,14 +2137,14 @@ func TestGarbageCollectForImageStore(t *testing.T) { sbomTag := fmt.Sprintf("sha256-%s.%s", manifestDigest.Encoded(), "sbom") So(err, ShouldBeNil) - sbomImg := test.CreateRandomImage() + sbomImg := CreateRandomImage() So(err, ShouldBeNil) err = test.WriteImageToFileSystem(sbomImg, repoName, sbomTag, storeController) So(err, ShouldBeNil) // add fake signature for tag1 - notationSig := test.CreateImageWith(). + notationSig := CreateImageWith(). RandomLayers(1, 10). ArtifactConfig("application/vnd.cncf.notary.signature"). Subject(img.DescriptorRef()).Build() @@ -2178,20 +2179,20 @@ func TestGarbageCollectImageUnknownManifest(t *testing.T) { unsupportedMediaType := "application/vnd.oci.artifact.manifest.v1+json" - img := test.CreateRandomImage() + img := CreateRandomImage() err := test.WriteImageToFileSystem(img, repoName, "v1", storeController) So(err, ShouldBeNil) // add image with unsupported media type - artifact := test.CreateRandomImage() + artifact := CreateRandomImage() err = test.WriteImageToFileSystem(artifact, repoName, "artifact", storeController) So(err, ShouldBeNil) // add referrer with unsupported media type subjectDesc := img.Descriptor() - referrer := test.CreateRandomImageWith().Subject(&subjectDesc).Build() + referrer := CreateRandomImageWith().Subject(&subjectDesc).Build() err = test.WriteImageToFileSystem(referrer, repoName, referrer.Digest().String(), storeController) So(err, ShouldBeNil) @@ -2866,7 +2867,7 @@ func TestGetNextRepository(t *testing.T) { secondRepoName := "repo2" srcStorageCtlr := storage.StoreController{DefaultStore: imgStore} - image := test.CreateDefaultImage() + image := CreateDefaultImage() err := test.WriteImageToFileSystem(image, firstRepoName, "0.0.1", srcStorageCtlr) if err != nil { diff --git a/pkg/test/common.go b/pkg/test/common.go index 34571dde..1bacaa73 100644 --- a/pkg/test/common.go +++ b/pkg/test/common.go @@ -15,7 +15,6 @@ import ( "log" "math" "math/big" - mathRand "math/rand" "net" "net/http" "os" @@ -51,10 +50,10 @@ import ( "zotregistry.io/zot/pkg/extensions/monitoring" zLog "zotregistry.io/zot/pkg/log" "zotregistry.io/zot/pkg/storage" - storageCommon "zotregistry.io/zot/pkg/storage/common" "zotregistry.io/zot/pkg/storage/local" stypes "zotregistry.io/zot/pkg/storage/types" testc "zotregistry.io/zot/pkg/test/common" + "zotregistry.io/zot/pkg/test/image-utils" "zotregistry.io/zot/pkg/test/inject" "zotregistry.io/zot/pkg/test/mocks" ) @@ -65,9 +64,11 @@ const ( SleepTime = 100 * time.Millisecond ) -var ErrNoGoModFileFound = errors.New("test: no go.mod file found in parent directories") - -var vulnerableLayer []byte //nolint: gochecknoglobals +var ( + ErrSignatureVerification = errors.New("signature verification failed") + ErrAlreadyExists = errors.New("already exists") + ErrKeyNotFound = errors.New("key not found") +) var NotationPathLock = new(sync.Mutex) //nolint: gochecknoglobals @@ -94,20 +95,6 @@ func GetTestBlobDigest(image, which string) godigest.Digest { return "" } -var ( - ErrPostBlob = errors.New("can't post blob") - ErrPutBlob = errors.New("can't put blob") - ErrAlreadyExists = errors.New("already exists") - ErrKeyNotFound = errors.New("key not found") - ErrSignatureVerification = errors.New("signature verification failed") - ErrPutIndex = errors.New("can't put index") -) - -type ArtifactBlobs struct { - Blob []byte - MediaType string -} - func GetFreePort() string { port, err := freeport.GetFreePort() if err != nil { @@ -221,7 +208,7 @@ func CopyTestKeysAndCerts(destDir string) error { "client.key", "server.cert", "server.csr", "server.key", } - rootPath, err := GetProjectRootDir() + rootPath, err := testc.GetProjectRootDir() if err != nil { return err } @@ -309,7 +296,7 @@ func NewControllerManager(controller Controller) ControllerManager { return cm } -func WriteImageToFileSystem(image Image, repoName, ref string, storeController storage.StoreController) error { +func WriteImageToFileSystem(image image.Image, repoName, ref string, storeController storage.StoreController) error { store := storeController.GetImageStore(repoName) err := store.InitRepo(repoName) @@ -353,7 +340,7 @@ func WriteImageToFileSystem(image Image, repoName, ref string, storeController s return nil } -func WriteMultiArchImageToFileSystem(multiarchImage MultiarchImage, repoName, ref string, +func WriteMultiArchImageToFileSystem(multiarchImage image.MultiarchImage, repoName, ref string, storeController storage.StoreController, ) error { store := storeController.GetImageStore(repoName) @@ -447,42 +434,6 @@ func GetRandomImageConfig() ([]byte, godigest.Digest) { return configBlobContent, configBlobDigestRaw } -func GetEmptyImageConfig() ([]byte, godigest.Digest) { - config := ispec.Image{} - - configBlobContent, err := json.MarshalIndent(&config, "", "\t") - if err != nil { - log.Fatal(err) - } - - configBlobDigestRaw := godigest.FromBytes(configBlobContent) - - return configBlobContent, configBlobDigestRaw -} - -func GetImageConfig() ([]byte, godigest.Digest) { - config := ispec.Image{ - Platform: ispec.Platform{ - Architecture: "amd64", - OS: "linux", - }, - RootFS: ispec.RootFS{ - Type: "layers", - DiffIDs: []godigest.Digest{}, - }, - Author: "some author", - } - - configBlobContent, err := json.MarshalIndent(&config, "", "\t") - if err != nil { - log.Fatal(err) - } - - configBlobDigestRaw := godigest.FromBytes(configBlobContent) - - return configBlobContent, configBlobDigestRaw -} - func GetOciLayoutDigests(imagePath string) (godigest.Digest, godigest.Digest, godigest.Digest) { var ( manifestDigest godigest.Digest @@ -632,18 +583,11 @@ func GetRandomImageComponents(layerSize int) (ispec.Image, [][]byte, ispec.Manif return config, layers, manifest, nil } -// These are the 3 vulnerabilities found for the returned image by the GetVulnImage function. -const ( - Vulnerability1ID = "CVE-2023-2650" - Vulnerability2ID = "CVE-2023-1255" - Vulnerability3ID = "CVE-2023-2975" -) - // Deprecated: Should use the new functions starting with "Create". -func GetVulnImageWithConfig(config ispec.Image) (Image, error) { - vulnerableLayer, err := GetLayerWithVulnerability() +func GetVulnImageWithConfig(config ispec.Image) (image.Image, error) { + vulnerableLayer, err := image.GetLayerWithVulnerability() if err != nil { - return Image{}, err + return image.Image{}, err } vulnerableConfig := ispec.Image{ @@ -663,57 +607,12 @@ func GetVulnImageWithConfig(config ispec.Image) (Image, error) { vulnerableLayer, }) if err != nil { - return Image{}, err + return image.Image{}, err } return img, err } -func GetLayerWithVulnerability() ([]byte, error) { - if vulnerableLayer != nil { - return vulnerableLayer, nil - } - - projectRootDir, err := GetProjectRootDir() - if err != nil { - return nil, err - } - - // this is the path of the blob relative to the root of the zot folder - vulnBlobPath := "test/data/alpine/blobs/sha256/f56be85fc22e46face30e2c3de3f7fe7c15f8fd7c4e5add29d7f64b87abdaa09" - - absoluteVulnBlobPath, _ := filepath.Abs(filepath.Join(projectRootDir, vulnBlobPath)) - - vulnerableLayer, err := os.ReadFile(absoluteVulnBlobPath) //nolint: lll - if err != nil { - return nil, err - } - - return vulnerableLayer, nil -} - -func GetProjectRootDir() (string, error) { - workDir, err := os.Getwd() - if err != nil { - return "", err - } - - for { - goModPath := filepath.Join(workDir, "go.mod") - - _, err := os.Stat(goModPath) - if err == nil { - return workDir, nil - } - - if workDir == filepath.Dir(workDir) { - return "", ErrNoGoModFileFound - } - - workDir = filepath.Dir(workDir) - } -} - func GetRandomLayer(size int) []byte { layer := make([]byte, size) @@ -726,15 +625,15 @@ func GetRandomLayer(size int) []byte { } // Deprecated: Should use the new functions starting with "Create". -func GetRandomImage() (Image, error) { +func GetRandomImage() (image.Image, error) { const layerSize = 20 config, layers, manifest, err := GetRandomImageComponents(layerSize) if err != nil { - return Image{}, err + return image.Image{}, err } - return Image{ + return image.Image{ Manifest: manifest, Layers: layers, Config: config, @@ -787,13 +686,13 @@ func GetImageComponentsWithConfig(conf ispec.Image) (ispec.Image, [][]byte, ispe } // Deprecated: Should use the new functions starting with "Create". -func GetImageWithConfig(conf ispec.Image) (Image, error) { +func GetImageWithConfig(conf ispec.Image) (image.Image, error) { config, layers, manifest, err := GetImageComponentsWithConfig(conf) if err != nil { - return Image{}, err + return image.Image{}, err } - return Image{ + return image.Image{ Manifest: manifest, Config: config, Layers: layers, @@ -801,10 +700,10 @@ func GetImageWithConfig(conf ispec.Image) (Image, error) { } // Deprecated: Should use the new functions starting with "Create". -func GetImageWithComponents(config ispec.Image, layers [][]byte) (Image, error) { +func GetImageWithComponents(config ispec.Image, layers [][]byte) (image.Image, error) { configBlob, err := json.Marshal(config) if err != nil { - return Image{}, err + return image.Image{}, err } manifestLayers := make([]ispec.Descriptor, 0, len(layers)) @@ -832,7 +731,7 @@ func GetImageWithComponents(config ispec.Image, layers [][]byte) (Image, error) Layers: manifestLayers, } - return Image{ + return image.Image{ Manifest: manifest, Config: config, Layers: layers, @@ -855,12 +754,12 @@ func GetCosignSignatureTagForDigest(manifestDigest godigest.Digest) string { } // Deprecated: Should use the new functions starting with "Create". -func GetImageWithSubject(subjectDigest godigest.Digest, mediaType string) (Image, error) { +func GetImageWithSubject(subjectDigest godigest.Digest, mediaType string) (image.Image, error) { num := 100 conf, layers, manifest, err := GetRandomImageComponents(num) if err != nil { - return Image{}, err + return image.Image{}, err } manifest.Subject = &ispec.Descriptor{ @@ -868,183 +767,13 @@ func GetImageWithSubject(subjectDigest godigest.Digest, mediaType string) (Image MediaType: mediaType, } - return Image{ + return image.Image{ Manifest: manifest, Config: conf, Layers: layers, }, nil } -func UploadImage(img Image, baseURL, repo, ref string) error { - for _, blob := range img.Layers { - resp, err := resty.R().Post(baseURL + "/v2/" + repo + "/blobs/uploads/") - if err != nil { - return err - } - - if resp.StatusCode() != http.StatusAccepted { - return ErrPostBlob - } - - loc := resp.Header().Get("Location") - - digest := godigest.FromBytes(blob).String() - - resp, err = resty.R(). - SetHeader("Content-Length", fmt.Sprintf("%d", len(blob))). - SetHeader("Content-Type", "application/octet-stream"). - SetQueryParam("digest", digest). - SetBody(blob). - Put(baseURL + loc) - - if err != nil { - return err - } - - if resp.StatusCode() != http.StatusCreated { - return ErrPutBlob - } - } - - var err error - - cblob := img.ConfigDescriptor.Data - - // we'll remove this check once we make the full transition to the new way of generating test images - if len(cblob) == 0 { - cblob, err = json.Marshal(img.Config) - if err = inject.Error(err); err != nil { - return err - } - } - - cdigest := godigest.FromBytes(cblob) - - if img.Manifest.Config.MediaType == ispec.MediaTypeEmptyJSON || - img.Manifest.Config.Digest == ispec.DescriptorEmptyJSON.Digest { - cblob = ispec.DescriptorEmptyJSON.Data - cdigest = ispec.DescriptorEmptyJSON.Digest - } - - resp, err := resty.R(). - Post(baseURL + "/v2/" + repo + "/blobs/uploads/") - if err = inject.Error(err); err != nil { - return err - } - - if inject.ErrStatusCode(resp.StatusCode()) != http.StatusAccepted || inject.ErrStatusCode(resp.StatusCode()) == -1 { - return ErrPostBlob - } - - loc := testc.Location(baseURL, resp) - - // uploading blob should get 201 - resp, err = resty.R(). - SetHeader("Content-Length", fmt.Sprintf("%d", len(cblob))). - SetHeader("Content-Type", "application/octet-stream"). - SetQueryParam("digest", cdigest.String()). - SetBody(cblob). - Put(loc) - if err = inject.Error(err); err != nil { - return err - } - - if inject.ErrStatusCode(resp.StatusCode()) != http.StatusCreated || inject.ErrStatusCode(resp.StatusCode()) == -1 { - return ErrPostBlob - } - - manifestBlob := img.ManifestDescriptor.Data - - // we'll remove this check once we make the full transition to the new way of generating test images - if len(manifestBlob) == 0 { - manifestBlob, err = json.Marshal(img.Manifest) - if err = inject.Error(err); err != nil { - return err - } - } - - // validate manifest - if err := storageCommon.ValidateManifestSchema(manifestBlob); err != nil { - return err - } - - resp, err = resty.R(). - SetHeader("Content-type", ispec.MediaTypeImageManifest). - SetBody(manifestBlob). - Put(baseURL + "/v2/" + repo + "/manifests/" + ref) - - if inject.ErrStatusCode(resp.StatusCode()) != http.StatusCreated { - return ErrPutBlob - } - - if inject.ErrStatusCode(resp.StatusCode()) != http.StatusCreated { - return ErrPutBlob - } - - return err -} - -func DeleteImage(repo, reference, baseURL string) (int, error) { - resp, err := resty.R().Delete( - fmt.Sprintf(baseURL+"/v2/%s/manifests/%s", repo, reference), - ) - if err != nil { - return -1, err - } - - return resp.StatusCode(), err -} - -func UploadBlob(baseURL, repo string, blob []byte, artifactBlobMediaType string) error { - resp, err := resty.R().Post(baseURL + "/v2/" + repo + "/blobs/uploads/") - if err != nil { - return err - } - - if resp.StatusCode() != http.StatusAccepted { - return ErrPostBlob - } - - loc := resp.Header().Get("Location") - - blobDigest := godigest.FromBytes(blob).String() - - resp, err = resty.R(). - SetHeader("Content-Length", fmt.Sprintf("%d", len(blob))). - SetHeader("Content-Type", artifactBlobMediaType). - SetQueryParam("digest", blobDigest). - SetBody(blob). - Put(baseURL + loc) - - if err != nil { - return err - } - - if resp.StatusCode() != http.StatusCreated { - return ErrPutBlob - } - - return nil -} - -func PushTestImage(repoName string, tag string, //nolint:unparam - baseURL string, manifest ispec.Manifest, - config ispec.Image, layers [][]byte, -) error { - err := UploadImage( - Image{ - Manifest: manifest, - Config: config, - Layers: layers, - }, - baseURL, - repoName, - tag, - ) - - return err -} - func ReadLogFileAndSearchString(logPath string, stringToMatch string, timeout time.Duration) (bool, error) { ctx, cancelFunc := context.WithTimeout(context.Background(), timeout) defer cancelFunc() @@ -1558,96 +1287,6 @@ func Contains[E isser](s []E, name string) bool { return Index(s, name) >= 0 } -func UploadImageWithBasicAuth(img Image, baseURL, repo, ref, user, password string) error { - for _, blob := range img.Layers { - resp, err := resty.R(). - SetBasicAuth(user, password). - Post(baseURL + "/v2/" + repo + "/blobs/uploads/") - if err != nil { - return err - } - - if resp.StatusCode() != http.StatusAccepted { - return ErrPostBlob - } - - loc := resp.Header().Get("Location") - - digest := godigest.FromBytes(blob).String() - - resp, err = resty.R(). - SetBasicAuth(user, password). - SetHeader("Content-Length", fmt.Sprintf("%d", len(blob))). - SetHeader("Content-Type", "application/octet-stream"). - SetQueryParam("digest", digest). - SetBody(blob). - Put(baseURL + loc) - - if err != nil { - return err - } - - if resp.StatusCode() != http.StatusCreated { - return ErrPutBlob - } - } - // upload config - cblob, err := json.Marshal(img.Config) - if err = inject.Error(err); err != nil { - return err - } - - cdigest := godigest.FromBytes(cblob) - - if img.Manifest.Config.MediaType == ispec.MediaTypeEmptyJSON { - cblob = ispec.DescriptorEmptyJSON.Data - cdigest = ispec.DescriptorEmptyJSON.Digest - } - - resp, err := resty.R(). - SetBasicAuth(user, password). - Post(baseURL + "/v2/" + repo + "/blobs/uploads/") - if err = inject.Error(err); err != nil { - return err - } - - if inject.ErrStatusCode(resp.StatusCode()) != http.StatusAccepted || inject.ErrStatusCode(resp.StatusCode()) == -1 { - return ErrPostBlob - } - - loc := testc.Location(baseURL, resp) - - // uploading blob should get 201 - resp, err = resty.R(). - SetBasicAuth(user, password). - SetHeader("Content-Length", fmt.Sprintf("%d", len(cblob))). - SetHeader("Content-Type", "application/octet-stream"). - SetQueryParam("digest", cdigest.String()). - SetBody(cblob). - Put(loc) - if err = inject.Error(err); err != nil { - return err - } - - if inject.ErrStatusCode(resp.StatusCode()) != http.StatusCreated || inject.ErrStatusCode(resp.StatusCode()) == -1 { - return ErrPostBlob - } - - // put manifest - manifestBlob, err := json.Marshal(img.Manifest) - if err = inject.Error(err); err != nil { - return err - } - - _, err = resty.R(). - SetBasicAuth(user, password). - SetHeader("Content-type", "application/vnd.oci.image.manifest.v1+json"). - SetBody(manifestBlob). - Put(baseURL + "/v2/" + repo + "/manifests/" + ref) - - return err -} - func SignImageUsingCosign(repoTag, port string) error { cwd, err := os.Getwd() if err != nil { @@ -1725,14 +1364,14 @@ func SignImageUsingNotary(repoTag, port string) error { } // Deprecated: Should use the new functions starting with "Create". -func GetRandomMultiarchImageComponents() (ispec.Index, []Image, error) { +func GetRandomMultiarchImageComponents() (ispec.Index, []image.Image, error) { const layerSize = 100 randomLayer1 := make([]byte, layerSize) _, err := rand.Read(randomLayer1) if err != nil { - return ispec.Index{}, []Image{}, err + return ispec.Index{}, []image.Image{}, err } image1, err := GetImageWithComponents( @@ -1746,14 +1385,14 @@ func GetRandomMultiarchImageComponents() (ispec.Index, []Image, error) { randomLayer1, }) if err != nil { - return ispec.Index{}, []Image{}, err + return ispec.Index{}, []image.Image{}, err } randomLayer2 := make([]byte, layerSize) _, err = rand.Read(randomLayer2) if err != nil { - return ispec.Index{}, []Image{}, err + return ispec.Index{}, []image.Image{}, err } image2, err := GetImageWithComponents( @@ -1767,14 +1406,14 @@ func GetRandomMultiarchImageComponents() (ispec.Index, []Image, error) { randomLayer2, }) if err != nil { - return ispec.Index{}, []Image{}, err + return ispec.Index{}, []image.Image{}, err } randomLayer3 := make([]byte, layerSize) _, err = rand.Read(randomLayer3) if err != nil { - return ispec.Index{}, []Image{}, err + return ispec.Index{}, []image.Image{}, err } image3, err := GetImageWithComponents( @@ -1788,7 +1427,7 @@ func GetRandomMultiarchImageComponents() (ispec.Index, []Image, error) { randomLayer3, }) if err != nil { - return ispec.Index{}, []Image{}, err + return ispec.Index{}, []image.Image{}, err } index := ispec.Index{ @@ -1812,25 +1451,25 @@ func GetRandomMultiarchImageComponents() (ispec.Index, []Image, error) { }, } - return index, []Image{image1, image2, image3}, nil + return index, []image.Image{image1, image2, image3}, nil } // Deprecated: Should use the new functions starting with "Create". -func GetRandomMultiarchImage(reference string) (MultiarchImage, error) { +func GetRandomMultiarchImage(reference string) (image.MultiarchImage, error) { index, images, err := GetRandomMultiarchImageComponents() if err != nil { - return MultiarchImage{}, err + return image.MultiarchImage{}, err } index.SchemaVersion = 2 - return MultiarchImage{ + return image.MultiarchImage{ Index: index, Images: images, Reference: reference, }, err } // Deprecated: Should use the new functions starting with "Create". -func GetMultiarchImageForImages(images []Image) MultiarchImage { +func GetMultiarchImageForImages(images []image.Image) image.MultiarchImage { var index ispec.Index for _, image := range images { @@ -1843,7 +1482,7 @@ func GetMultiarchImageForImages(images []Image) MultiarchImage { index.SchemaVersion = 2 - return MultiarchImage{Index: index, Images: images} + return image.MultiarchImage{Index: index, Images: images} } func getManifestSize(manifest ispec.Manifest) int64 { @@ -1864,37 +1503,6 @@ func getManifestDigest(manifest ispec.Manifest) godigest.Digest { return godigest.FromBytes(manifestBlob) } -func UploadMultiarchImage(multiImage MultiarchImage, baseURL string, repo, ref string) error { - for _, image := range multiImage.Images { - err := UploadImage(image, baseURL, repo, image.DigestStr()) - if err != nil { - return err - } - } - - // put manifest - indexBlob, err := json.Marshal(multiImage.Index) - if err = inject.Error(err); err != nil { - return err - } - - // validate manifest - if err := storageCommon.ValidateImageIndexSchema(indexBlob); err != nil { - return err - } - - resp, err := resty.R(). - SetHeader("Content-type", ispec.MediaTypeImageIndex). - SetBody(indexBlob). - Put(baseURL + "/v2/" + repo + "/manifests/" + ref) - - if resp.StatusCode() != http.StatusCreated { - return ErrPutIndex - } - - return err -} - func GetIndexBlobWithManifests(manifestDigests []godigest.Digest) ([]byte, error) { manifests := make([]ispec.Descriptor, 0, len(manifestDigests)) @@ -1965,90 +1573,6 @@ func CustomRedirectPolicy(noOfRedirect int) resty.RedirectPolicy { }) } -func DateRef(year int, month time.Month, day, hour, min, sec, nsec int, loc *time.Location) *time.Time { - date := time.Date(year, month, day, hour, min, sec, nsec, loc) - - return &date -} - -func RandomDateRef(loc *time.Location) *time.Time { - var ( - year = 1990 + mathRand.Intn(30) //nolint: gosec,gomnd - month = time.Month(1 + mathRand.Intn(10)) //nolint: gosec,gomnd - day = 1 + mathRand.Intn(5) //nolint: gosec,gomnd - hour = 1 + mathRand.Intn(22) //nolint: gosec,gomnd - min = 1 + mathRand.Intn(58) //nolint: gosec,gomnd - sec = 1 + mathRand.Intn(58) //nolint: gosec,gomnd - nsec = 1 - ) - - return DateRef(year, month, day, hour, min, sec, nsec, time.UTC) -} - -func GetDefaultConfig() ispec.Image { - return ispec.Image{ - Created: DefaultTimeRef(), - Author: "ZotUser", - Platform: ispec.Platform{ - OS: "linux", - Architecture: "amd64", - }, - RootFS: ispec.RootFS{ - Type: "layers", - DiffIDs: []godigest.Digest{}, - }, - } -} - -func GetDefaultVulnConfig() ispec.Image { - return ispec.Image{ - Created: DefaultTimeRef(), - Author: "ZotUser", - Platform: ispec.Platform{ - Architecture: "amd64", - OS: "linux", - }, - Config: ispec.ImageConfig{ - Env: []string{"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"}, - Cmd: []string{"/bin/sh"}, - }, - RootFS: ispec.RootFS{ - Type: "layers", - DiffIDs: []godigest.Digest{"sha256:f1417ff83b319fbdae6dd9cd6d8c9c88002dcd75ecf6ec201c8c6894681cf2b5"}, - }, - } -} - -func DefaultTimeRef() *time.Time { - var ( - year = 2010 - month = time.Month(1) - day = 1 - hour = 1 - min = 1 - sec = 1 - nsec = 0 - ) - - return DateRef(year, month, day, hour, min, sec, nsec, time.UTC) -} - -func GetDefaultLayers() []Layer { - return []Layer{ - {Blob: []byte("abc"), Digest: godigest.FromBytes([]byte("abc")), MediaType: ispec.MediaTypeImageLayerGzip}, - {Blob: []byte("123"), Digest: godigest.FromBytes([]byte("123")), MediaType: ispec.MediaTypeImageLayerGzip}, - {Blob: []byte("xyz"), Digest: godigest.FromBytes([]byte("xyz")), MediaType: ispec.MediaTypeImageLayerGzip}, - } -} - -func GetDefaultLayersBlobs() [][]byte { - return [][]byte{ - []byte("abc"), - []byte("123"), - []byte("xyz"), - } -} - func GetDefaultImageStore(rootDir string, log zLog.Logger) stypes.ImageStore { return local.NewImageStore(rootDir, false, false, time.Hour, time.Hour, false, false, log, monitoring.NewMetricsServer(false, log), diff --git a/pkg/test/common/utils.go b/pkg/test/common/utils.go index 86f97cf3..863ada5a 100644 --- a/pkg/test/common/utils.go +++ b/pkg/test/common/utils.go @@ -1,11 +1,16 @@ package common import ( + "errors" "net/url" + "os" + "path/filepath" "gopkg.in/resty.v1" ) +var ErrNoGoModFileFound = errors.New("test: no go.mod file found in parent directories") + 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 @@ -23,3 +28,25 @@ func Location(baseURL string, resp *resty.Response) string { return baseURL + path } + +func GetProjectRootDir() (string, error) { + workDir, err := os.Getwd() + if err != nil { + return "", err + } + + for { + goModPath := filepath.Join(workDir, "go.mod") + + _, err := os.Stat(goModPath) + if err == nil { + return workDir, nil + } + + if workDir == filepath.Dir(workDir) { + return "", ErrNoGoModFileFound + } + + workDir = filepath.Dir(workDir) + } +} diff --git a/pkg/test/common_test.go b/pkg/test/common_test.go index 5d31040f..988af020 100644 --- a/pkg/test/common_test.go +++ b/pkg/test/common_test.go @@ -17,7 +17,6 @@ import ( notconfig "github.com/notaryproject/notation-go/config" godigest "github.com/opencontainers/go-digest" - "github.com/opencontainers/image-spec/specs-go" ispec "github.com/opencontainers/image-spec/specs-go/v1" . "github.com/smartystreets/goconvey/convey" @@ -26,6 +25,7 @@ import ( "zotregistry.io/zot/pkg/log" "zotregistry.io/zot/pkg/storage" "zotregistry.io/zot/pkg/test" + . "zotregistry.io/zot/pkg/test/image-utils" "zotregistry.io/zot/pkg/test/inject" "zotregistry.io/zot/pkg/test/mocks" ) @@ -134,7 +134,7 @@ func TestGetOciLayoutDigests(t *testing.T) { Convey("no permissions when getting index", t, func() { storageCtlr := test.GetDefaultStoreController(dir, log.NewLogger("debug", "")) - image := test.CreateDefaultImage() + image := CreateDefaultImage() err := test.WriteImageToFileSystem(image, "test-index", "0.0.1", storageCtlr) So(err, ShouldBeNil) @@ -154,7 +154,7 @@ func TestGetOciLayoutDigests(t *testing.T) { Convey("can't access manifest digest", t, func() { storageCtlr := test.GetDefaultStoreController(dir, log.NewLogger("debug", "")) - image := test.CreateDefaultImage() + image := CreateDefaultImage() err := test.WriteImageToFileSystem(image, "test-manifest", "0.0.1", storageCtlr) So(err, ShouldBeNil) @@ -265,582 +265,6 @@ func TestControllerManager(t *testing.T) { }) } -func TestUploadBlob(t *testing.T) { - Convey("Post request results in an error", t, func() { - port := test.GetFreePort() - baseURL := test.GetBaseURL(port) - - err := test.UploadBlob(baseURL, "test", []byte("test"), "zot.com.test") - So(err, ShouldNotBeNil) - }) - - Convey("Post request status differs from accepted", t, func() { - port := test.GetFreePort() - baseURL := test.GetBaseURL(port) - - tempDir := t.TempDir() - conf := config.New() - conf.HTTP.Port = port - conf.Storage.RootDirectory = tempDir - - err := os.Chmod(tempDir, 0o400) - if err != nil { - t.Fatal(err) - } - - ctlr := api.NewController(conf) - - ctlrManager := test.NewControllerManager(ctlr) - ctlrManager.StartAndWait(port) - defer ctlrManager.StopServer() - - err = test.UploadBlob(baseURL, "test", []byte("test"), "zot.com.test") - So(err, ShouldEqual, test.ErrPostBlob) - }) - - Convey("Put request results in an error", t, func() { - port := test.GetFreePort() - baseURL := test.GetBaseURL(port) - - tempDir := t.TempDir() - conf := config.New() - conf.HTTP.Port = port - conf.Storage.RootDirectory = tempDir - - ctlr := api.NewController(conf) - ctlrManager := test.NewControllerManager(ctlr) - ctlrManager.StartAndWait(port) - defer ctlrManager.StopServer() - - blob := new([]byte) - - err := test.UploadBlob(baseURL, "test", *blob, "zot.com.test") - So(err, ShouldNotBeNil) - }) - - Convey("Put request status differs from accepted", t, func() { - port := test.GetFreePort() - baseURL := test.GetBaseURL(port) - - tempDir := t.TempDir() - conf := config.New() - conf.HTTP.Port = port - conf.Storage.RootDirectory = tempDir - - ctlr := api.NewController(conf) - ctlrManager := test.NewControllerManager(ctlr) - ctlrManager.StartAndWait(port) - defer ctlrManager.StopServer() - - blob := []byte("test") - blobDigest := godigest.FromBytes(blob) - layerPath := path.Join(tempDir, "test", "blobs", "sha256") - blobPath := path.Join(layerPath, blobDigest.String()) - if _, err := os.Stat(layerPath); os.IsNotExist(err) { - err = os.MkdirAll(layerPath, 0o700) - if err != nil { - t.Fatal(err) - } - - file, err := os.Create(blobPath) - if err != nil { - t.Fatal(err) - } - - err = os.Chmod(layerPath, 0o000) - if err != nil { - t.Fatal(err) - } - defer func() { - err = os.Chmod(layerPath, 0o700) - if err != nil { - t.Fatal(err) - } - - os.RemoveAll(file.Name()) - }() - } - - err := test.UploadBlob(baseURL, "test", blob, "zot.com.test") - So(err, ShouldEqual, test.ErrPutBlob) - }) - - Convey("Put request successful", t, func() { - port := test.GetFreePort() - baseURL := test.GetBaseURL(port) - - tempDir := t.TempDir() - conf := config.New() - conf.HTTP.Port = port - conf.Storage.RootDirectory = tempDir - - ctlr := api.NewController(conf) - - ctlrManager := test.NewControllerManager(ctlr) - ctlrManager.StartAndWait(port) - defer ctlrManager.StopServer() - - blob := []byte("test") - - err := test.UploadBlob(baseURL, "test", blob, "zot.com.test") - So(err, ShouldEqual, nil) - }) -} - -func TestUploadMultiarchImage(t *testing.T) { - Convey("make controller", t, func() { - port := test.GetFreePort() - baseURL := test.GetBaseURL(port) - - conf := config.New() - conf.HTTP.Port = port - conf.Storage.RootDirectory = t.TempDir() - - ctlr := api.NewController(conf) - - ctlrManager := test.NewControllerManager(ctlr) - ctlrManager.StartAndWait(port) - defer ctlrManager.StopServer() - - layerBlob := []byte("test") - - img := test.Image{ - Layers: [][]byte{ - layerBlob, - }, - Manifest: ispec.Manifest{ - Versioned: specs.Versioned{ - SchemaVersion: 2, - }, - Layers: []ispec.Descriptor{ - { - Digest: godigest.FromBytes(layerBlob), - Size: int64(len(layerBlob)), - MediaType: ispec.MediaTypeImageLayerGzip, - }, - }, - Config: ispec.DescriptorEmptyJSON, - }, - Config: ispec.Image{}, - } - - manifestBuf, err := json.Marshal(img.Manifest) - So(err, ShouldBeNil) - - Convey("Multiarch image uploaded successfully", func() { - err = test.UploadMultiarchImage(test.MultiarchImage{ - Index: ispec.Index{ - Versioned: specs.Versioned{ - SchemaVersion: 2, - }, - MediaType: ispec.MediaTypeImageIndex, - Manifests: []ispec.Descriptor{ - { - MediaType: ispec.MediaTypeImageManifest, - Digest: godigest.FromBytes(manifestBuf), - Size: int64(len(manifestBuf)), - }, - }, - }, - Images: []test.Image{img}, - }, baseURL, "test", "index") - So(err, ShouldBeNil) - }) - - Convey("Multiarch image without schemaVersion should fail validation", func() { - err = test.UploadMultiarchImage(test.MultiarchImage{ - Index: ispec.Index{ - MediaType: ispec.MediaTypeImageIndex, - Manifests: []ispec.Descriptor{ - { - MediaType: ispec.MediaTypeImageManifest, - Digest: godigest.FromBytes(manifestBuf), - Size: int64(len(manifestBuf)), - }, - }, - }, - Images: []test.Image{img}, - }, baseURL, "test", "index") - So(err, ShouldNotBeNil) - }) - }) -} - -func TestUploadImage(t *testing.T) { - Convey("Manifest without schemaVersion should fail validation", t, func() { - port := test.GetFreePort() - baseURL := test.GetBaseURL(port) - - conf := config.New() - conf.HTTP.Port = port - conf.Storage.RootDirectory = t.TempDir() - - ctlr := api.NewController(conf) - - ctlrManager := test.NewControllerManager(ctlr) - ctlrManager.StartAndWait(port) - defer ctlrManager.StopServer() - - layerBlob := []byte("test") - - img := test.Image{ - Layers: [][]byte{ - layerBlob, - }, - Manifest: ispec.Manifest{ - Layers: []ispec.Descriptor{ - { - Digest: godigest.FromBytes(layerBlob), - Size: int64(len(layerBlob)), - MediaType: ispec.MediaTypeImageLayerGzip, - }, - }, - Config: ispec.DescriptorEmptyJSON, - }, - Config: ispec.Image{}, - } - - err := test.UploadImage(img, baseURL, "test", img.DigestStr()) - So(err, ShouldNotBeNil) - }) - - Convey("Post request results in an error", t, func() { - port := test.GetFreePort() - baseURL := test.GetBaseURL(port) - - conf := config.New() - conf.HTTP.Port = port - conf.Storage.RootDirectory = t.TempDir() - - img := test.Image{ - Layers: make([][]byte, 10), - } - - err := test.UploadImage(img, baseURL, "test", "") - So(err, ShouldNotBeNil) - }) - - Convey("Post request status differs from accepted", t, func() { - port := test.GetFreePort() - baseURL := test.GetBaseURL(port) - - tempDir := t.TempDir() - conf := config.New() - conf.HTTP.Port = port - conf.Storage.RootDirectory = tempDir - - err := os.Chmod(tempDir, 0o400) - if err != nil { - t.Fatal(err) - } - - ctlr := api.NewController(conf) - - ctlrManager := test.NewControllerManager(ctlr) - ctlrManager.StartAndWait(port) - defer ctlrManager.StopServer() - - img := test.Image{ - Layers: make([][]byte, 10), - } - - err = test.UploadImage(img, baseURL, "test", "") - So(err, ShouldNotBeNil) - }) - - Convey("Put request results in an error", t, func() { - port := test.GetFreePort() - baseURL := test.GetBaseURL(port) - - conf := config.New() - conf.HTTP.Port = port - conf.Storage.RootDirectory = t.TempDir() - - ctlr := api.NewController(conf) - - ctlrManager := test.NewControllerManager(ctlr) - ctlrManager.StartAndWait(port) - defer ctlrManager.StopServer() - - img := test.Image{ - Layers: make([][]byte, 10), // invalid format that will result in an error - Config: ispec.Image{}, - } - - err := test.UploadImage(img, baseURL, "test", "") - So(err, ShouldNotBeNil) - }) - - Convey("Image uploaded successfully", t, func() { - port := test.GetFreePort() - baseURL := test.GetBaseURL(port) - - conf := config.New() - conf.HTTP.Port = port - conf.Storage.RootDirectory = t.TempDir() - - ctlr := api.NewController(conf) - - ctlrManager := test.NewControllerManager(ctlr) - ctlrManager.StartAndWait(port) - defer ctlrManager.StopServer() - - layerBlob := []byte("test") - - img := test.Image{ - Layers: [][]byte{ - layerBlob, - }, - Manifest: ispec.Manifest{ - Versioned: specs.Versioned{ - SchemaVersion: 2, - }, - Layers: []ispec.Descriptor{ - { - Digest: godigest.FromBytes(layerBlob), - Size: int64(len(layerBlob)), - MediaType: ispec.MediaTypeImageLayerGzip, - }, - }, - Config: ispec.DescriptorEmptyJSON, - }, - Config: ispec.Image{}, - } - - err := test.UploadImage(img, baseURL, "test", img.DigestStr()) - So(err, ShouldBeNil) - }) - - Convey("Upload image with authentification", t, func() { - tempDir := t.TempDir() - conf := config.New() - port := test.GetFreePort() - baseURL := test.GetBaseURL(port) - - user1 := "test" - password1 := "test" - testString1 := test.GetCredString(user1, password1) - htpasswdPath := test.MakeHtpasswdFileFromString(testString1) - defer os.Remove(htpasswdPath) - conf.HTTP.Auth = &config.AuthConfig{ - HTPasswd: config.AuthHTPasswd{ - Path: htpasswdPath, - }, - } - - conf.HTTP.Port = port - - conf.HTTP.AccessControl = &config.AccessControlConfig{ - Repositories: config.Repositories{ - "repo": config.PolicyGroup{ - Policies: []config.Policy{ - { - Users: []string{user1}, - Actions: []string{"read", "create"}, - }, - }, - DefaultPolicy: []string{}, - }, - "inaccessibleRepo": config.PolicyGroup{ - Policies: []config.Policy{ - { - Users: []string{user1}, - Actions: []string{"create"}, - }, - }, - DefaultPolicy: []string{}, - }, - }, - AdminPolicy: config.Policy{ - Users: []string{}, - Actions: []string{}, - }, - } - - ctlr := api.NewController(conf) - - ctlr.Config.Storage.RootDirectory = tempDir - - ctlrManager := test.NewControllerManager(ctlr) - ctlrManager.StartAndWait(port) - defer ctlrManager.StopServer() - - Convey("Request fail while pushing layer", func() { - err := test.UploadImageWithBasicAuth(test.Image{Layers: [][]byte{{1, 2, 3}}}, "badURL", "", "", "", "") - So(err, ShouldNotBeNil) - }) - Convey("Request status is not StatusOk while pushing layer", func() { - err := test.UploadImageWithBasicAuth(test.Image{Layers: [][]byte{{1, 2, 3}}}, baseURL, "", "repo", "", "") - So(err, ShouldNotBeNil) - }) - Convey("Request fail while pushing config", func() { - err := test.UploadImageWithBasicAuth(test.Image{}, "badURL", "", "", "", "") - So(err, ShouldNotBeNil) - }) - Convey("Request status is not StatusOk while pushing config", func() { - err := test.UploadImageWithBasicAuth(test.Image{}, baseURL, "", "repo", "", "") - So(err, ShouldNotBeNil) - }) - }) - - Convey("Blob upload wrong response status code", t, func() { - port := test.GetFreePort() - baseURL := test.GetBaseURL(port) - - tempDir := t.TempDir() - conf := config.New() - conf.HTTP.Port = port - conf.Storage.RootDirectory = tempDir - - ctlr := api.NewController(conf) - - ctlrManager := test.NewControllerManager(ctlr) - ctlrManager.StartAndWait(port) - defer ctlrManager.StopServer() - - layerBlob := []byte("test") - layerBlobDigest := godigest.FromBytes(layerBlob) - layerPath := path.Join(tempDir, "test", "blobs", "sha256") - - if _, err := os.Stat(layerPath); os.IsNotExist(err) { - err = os.MkdirAll(layerPath, 0o700) - if err != nil { - t.Fatal(err) - } - file, err := os.Create(path.Join(layerPath, layerBlobDigest.Encoded())) - if err != nil { - t.Fatal(err) - } - - err = os.Chmod(layerPath, 0o000) - if err != nil { - t.Fatal(err) - } - defer func() { - err = os.Chmod(layerPath, 0o700) - if err != nil { - t.Fatal(err) - } - os.RemoveAll(file.Name()) - }() - } - - img := test.Image{ - Layers: [][]byte{ - layerBlob, - }, // invalid format that will result in an error - Config: ispec.Image{}, - } - - err := test.UploadImage(img, baseURL, "test", "") - So(err, ShouldNotBeNil) - }) - - Convey("CreateBlobUpload wrong response status code", t, func() { - port := test.GetFreePort() - baseURL := test.GetBaseURL(port) - - tempDir := t.TempDir() - conf := config.New() - conf.HTTP.Port = port - conf.Storage.RootDirectory = tempDir - - ctlr := api.NewController(conf) - - ctlrManager := test.NewControllerManager(ctlr) - ctlrManager.StartAndWait(port) - defer ctlrManager.StopServer() - - layerBlob := []byte("test") - - img := test.Image{ - Layers: [][]byte{ - layerBlob, - }, // invalid format that will result in an error - Config: ispec.Image{}, - } - - Convey("CreateBlobUpload", func() { - injected := inject.InjectFailure(2) - if injected { - err := test.UploadImage(img, baseURL, "test", img.DigestStr()) - So(err, ShouldNotBeNil) - } - }) - Convey("UpdateBlobUpload", func() { - injected := inject.InjectFailure(4) - if injected { - err := test.UploadImage(img, baseURL, "test", img.DigestStr()) - So(err, ShouldNotBeNil) - } - }) - }) -} - -func TestInjectUploadImage(t *testing.T) { - Convey("Inject failures for unreachable lines", t, func() { - port := test.GetFreePort() - baseURL := test.GetBaseURL(port) - - tempDir := t.TempDir() - conf := config.New() - conf.HTTP.Port = port - conf.Storage.RootDirectory = tempDir - - ctlr := api.NewController(conf) - - ctlrManager := test.NewControllerManager(ctlr) - ctlrManager.StartAndWait(port) - defer ctlrManager.StopServer() - - layerBlob := []byte("test") - layerPath := path.Join(tempDir, "test", ".uploads") - - if _, err := os.Stat(layerPath); os.IsNotExist(err) { - err = os.MkdirAll(layerPath, 0o700) - if err != nil { - t.Fatal(err) - } - } - - img := test.Image{ - Layers: [][]byte{ - layerBlob, - }, // invalid format that will result in an error - Config: ispec.Image{}, - } - - Convey("first marshal", func() { - injected := inject.InjectFailure(0) - if injected { - err := test.UploadImage(img, baseURL, "test", img.DigestStr()) - So(err, ShouldNotBeNil) - } - }) - Convey("CreateBlobUpload POST call", func() { - injected := inject.InjectFailure(1) - if injected { - err := test.UploadImage(img, baseURL, "test", img.DigestStr()) - So(err, ShouldNotBeNil) - } - }) - Convey("UpdateBlobUpload PUT call", func() { - injected := inject.InjectFailure(3) - if injected { - err := test.UploadImage(img, baseURL, "test", img.DigestStr()) - So(err, ShouldNotBeNil) - } - }) - Convey("second marshal", func() { - injected := inject.InjectFailure(5) - if injected { - err := test.UploadImage(img, baseURL, "test", img.DigestStr()) - So(err, ShouldNotBeNil) - } - }) - }) -} - func TestReadLogFileAndSearchString(t *testing.T) { logFile, err := os.CreateTemp(t.TempDir(), "zot-log*.txt") if err != nil { @@ -895,81 +319,6 @@ func TestReadLogFileAndCountStringOccurence(t *testing.T) { }) } -func TestInjectUploadImageWithBasicAuth(t *testing.T) { - Convey("Inject failures for unreachable lines", t, func() { - port := test.GetFreePort() - baseURL := test.GetBaseURL(port) - - tempDir := t.TempDir() - conf := config.New() - conf.HTTP.Port = port - conf.Storage.RootDirectory = tempDir - - user := "user" - password := "password" - testString := test.GetCredString(user, password) - htpasswdPath := test.MakeHtpasswdFileFromString(testString) - defer os.Remove(htpasswdPath) - conf.HTTP.Auth = &config.AuthConfig{ - HTPasswd: config.AuthHTPasswd{ - Path: htpasswdPath, - }, - } - - ctlr := api.NewController(conf) - - ctlrManager := test.NewControllerManager(ctlr) - ctlrManager.StartAndWait(port) - defer ctlrManager.StopServer() - - layerBlob := []byte("test") - layerPath := path.Join(tempDir, "test", ".uploads") - - if _, err := os.Stat(layerPath); os.IsNotExist(err) { - err = os.MkdirAll(layerPath, 0o700) - if err != nil { - t.Fatal(err) - } - } - - img := test.Image{ - Layers: [][]byte{ - layerBlob, - }, // invalid format that will result in an error - Config: ispec.Image{}, - } - - Convey("first marshal", func() { - injected := inject.InjectFailure(0) - if injected { - err := test.UploadImageWithBasicAuth(img, baseURL, "test", img.DigestStr(), "user", "password") - So(err, ShouldNotBeNil) - } - }) - Convey("CreateBlobUpload POST call", func() { - injected := inject.InjectFailure(1) - if injected { - err := test.UploadImageWithBasicAuth(img, baseURL, "test", img.DigestStr(), "user", "password") - So(err, ShouldNotBeNil) - } - }) - Convey("UpdateBlobUpload PUT call", func() { - injected := inject.InjectFailure(3) - if injected { - err := test.UploadImageWithBasicAuth(img, baseURL, "test", img.DigestStr(), "user", "password") - So(err, ShouldNotBeNil) - } - }) - Convey("second marshal", func() { - injected := inject.InjectFailure(5) - if injected { - err := test.UploadImageWithBasicAuth(img, baseURL, "test", img.DigestStr(), "user", "password") - So(err, ShouldNotBeNil) - } - }) - }) -} - func TestCopyFile(t *testing.T) { Convey("destFilePath does not exist", t, func() { err := test.CopyFile("/path/to/srcFile", "~/path/to/some/unexisting/destDir/file") @@ -1235,8 +584,8 @@ func TestVerifyWithNotation(t *testing.T) { cfg, layers, manifest, err := test.GetImageComponents(2) So(err, ShouldBeNil) - err = test.UploadImage( - test.Image{ + err = UploadImage( + Image{ Config: cfg, Layers: layers, Manifest: manifest, @@ -1449,7 +798,7 @@ func TestGenerateNotationCerts(t *testing.T) { func TestWriteImageToFileSystem(t *testing.T) { Convey("WriteImageToFileSystem errors", t, func() { - err := test.WriteImageToFileSystem(test.Image{}, "repo", "dig", storage.StoreController{ + err := test.WriteImageToFileSystem(Image{}, "repo", "dig", storage.StoreController{ DefaultStore: mocks.MockedImageStore{ InitRepoFn: func(name string) error { return ErrTestError @@ -1459,7 +808,7 @@ func TestWriteImageToFileSystem(t *testing.T) { So(err, ShouldNotBeNil) err = test.WriteImageToFileSystem( - test.Image{Layers: [][]byte{[]byte("testLayer")}}, + Image{Layers: [][]byte{[]byte("testLayer")}}, "repo", "tag", storage.StoreController{ @@ -1474,7 +823,7 @@ func TestWriteImageToFileSystem(t *testing.T) { count := 0 err = test.WriteImageToFileSystem( - test.Image{Layers: [][]byte{[]byte("testLayer")}}, + Image{Layers: [][]byte{[]byte("testLayer")}}, "repo", "tag", storage.StoreController{ @@ -1494,7 +843,7 @@ func TestWriteImageToFileSystem(t *testing.T) { So(err, ShouldNotBeNil) err = test.WriteImageToFileSystem( - test.Image{Layers: [][]byte{[]byte("testLayer")}}, + Image{Layers: [][]byte{[]byte("testLayer")}}, "repo", "tag", storage.StoreController{ diff --git a/pkg/test/images.go b/pkg/test/image-utils/images.go similarity index 99% rename from pkg/test/images.go rename to pkg/test/image-utils/images.go index ab56ec21..38587f07 100644 --- a/pkg/test/images.go +++ b/pkg/test/image-utils/images.go @@ -1,4 +1,4 @@ -package test +package image import ( "crypto/rand" diff --git a/pkg/test/images_test.go b/pkg/test/image-utils/images_test.go similarity index 74% rename from pkg/test/images_test.go rename to pkg/test/image-utils/images_test.go index 15d22330..90b9692c 100644 --- a/pkg/test/images_test.go +++ b/pkg/test/image-utils/images_test.go @@ -1,4 +1,4 @@ -package test_test +package image_test import ( "encoding/json" @@ -8,17 +8,17 @@ import ( ispec "github.com/opencontainers/image-spec/specs-go/v1" . "github.com/smartystreets/goconvey/convey" - "zotregistry.io/zot/pkg/test" + . "zotregistry.io/zot/pkg/test/image-utils" ) func TestImageBuilder(t *testing.T) { - vulnLayer, err := test.GetLayerWithVulnerability() + vulnLayer, err := GetLayerWithVulnerability() if err != nil { t.FailNow() } Convey("Test Layer Builders", t, func() { - layerBuilder := test.CreateImageWith() + layerBuilder := CreateImageWith() Convey("LayerBlobs", func() { layerBlobs := [][]byte{{11, 11, 11}, {22, 22, 22}} @@ -29,7 +29,7 @@ func TestImageBuilder(t *testing.T) { Build() So(image.Layers, ShouldResemble, layerBlobs) - So(image.Config, ShouldResemble, test.GetDefaultConfig()) + So(image.Config, ShouldResemble, GetDefaultConfig()) }) Convey("DefaultLayers", func() { @@ -38,14 +38,14 @@ func TestImageBuilder(t *testing.T) { DefaultConfig(). Build() - So(image.Layers, ShouldResemble, test.GetDefaultLayersBlobs()) - So(image.Config, ShouldResemble, test.GetDefaultConfig()) + So(image.Layers, ShouldResemble, GetDefaultLayersBlobs()) + So(image.Config, ShouldResemble, GetDefaultConfig()) }) Convey("Layers", func() { blob1, blob2 := []byte{10, 10, 10}, []byte{20, 20, 20} - layers := []test.Layer{ + layers := []Layer{ { Blob: blob1, MediaType: ispec.MediaTypeImageLayerGzip, @@ -63,7 +63,7 @@ func TestImageBuilder(t *testing.T) { Build() So(image.Layers, ShouldResemble, [][]byte{blob1, blob2}) - So(image.Config, ShouldResemble, test.GetDefaultConfig()) + So(image.Config, ShouldResemble, GetDefaultConfig()) }) Convey("Empty Layer", func() { @@ -77,7 +77,7 @@ func TestImageBuilder(t *testing.T) { }) Convey("Config builder", t, func() { - configBuilder := test.CreateImageWith().DefaultLayers() + configBuilder := CreateImageWith().DefaultLayers() Convey("Empty Config", func() { img := configBuilder.EmptyConfig().Build() @@ -87,7 +87,7 @@ func TestImageBuilder(t *testing.T) { }) Convey("Vulnerable config builder", t, func() { - configBuilder := test.CreateImageWith().VulnerableLayers() + configBuilder := CreateImageWith().VulnerableLayers() Convey("VulnerableConfig", func() { platform := ispec.Platform{OS: "os", Architecture: "arch"} @@ -107,8 +107,8 @@ func TestImageBuilder(t *testing.T) { }) }) - Convey("Manifes builder", t, func() { - manifestBuilder := test.CreateImageWith().DefaultLayers().DefaultConfig() + Convey("Manifest builder", t, func() { + manifestBuilder := CreateImageWith().DefaultLayers().DefaultConfig() subject := ispec.Descriptor{ Digest: godigest.FromString("digest"), @@ -121,8 +121,8 @@ func TestImageBuilder(t *testing.T) { Annotations(map[string]string{"key": "val"}). Build() - So(image.Layers, ShouldResemble, test.GetDefaultLayersBlobs()) - So(image.Config, ShouldResemble, test.GetDefaultConfig()) + So(image.Layers, ShouldResemble, GetDefaultLayersBlobs()) + So(image.Config, ShouldResemble, GetDefaultConfig()) So(image.Manifest.Subject, ShouldResemble, &subject) So(image.Manifest.ArtifactType, ShouldResemble, "art.type") So(image.Manifest.Annotations, ShouldResemble, map[string]string{"key": "val"}) @@ -131,10 +131,10 @@ func TestImageBuilder(t *testing.T) { func TestMultiarchImageBuilder(t *testing.T) { Convey("Multiarch", t, func() { - multiArch := test.CreateMultiarchWith(). - Images([]test.Image{ - test.CreateRandomImage(), - test.CreateRandomImage(), + multiArch := CreateMultiarchWith(). + Images([]Image{ + CreateRandomImage(), + CreateRandomImage(), }). Annotations(map[string]string{"a": "b"}). ArtifactType("art.type"). @@ -151,35 +151,35 @@ func TestMultiarchImageBuilder(t *testing.T) { func TestPredefinedImages(t *testing.T) { Convey("Predefined Images", t, func() { - img := test.CreateDefaultImage() - So(img.Layers, ShouldResemble, test.GetDefaultLayersBlobs()) + img := CreateDefaultImage() + So(img.Layers, ShouldResemble, GetDefaultLayersBlobs()) - img = test.CreateDefaultImageWith().ArtifactType("art.type").Build() + img = CreateDefaultImageWith().ArtifactType("art.type").Build() So(img.Manifest.ArtifactType, ShouldEqual, "art.type") - img = test.CreateRandomImageWith().ArtifactType("art.type").Build() + img = CreateRandomImageWith().ArtifactType("art.type").Build() So(img.Manifest.ArtifactType, ShouldEqual, "art.type") - img = test.CreateRandomVulnerableImage() - So(img.Layers, ShouldNotResemble, test.GetDefaultLayersBlobs()) + img = CreateRandomVulnerableImage() + So(img.Layers, ShouldNotResemble, GetDefaultLayersBlobs()) - img = test.CreateRandomVulnerableImageWith().ArtifactType("art.type").Build() + img = CreateRandomVulnerableImageWith().ArtifactType("art.type").Build() So(img.Manifest.ArtifactType, ShouldEqual, "art.type") }) Convey("Predefined Multiarch-Images", t, func() { - multiArch := test.CreateRandomMultiarch() + multiArch := CreateRandomMultiarch() So(len(multiArch.Images), ShouldEqual, 3) So(multiArch.Reference, ShouldResemble, multiArch.Digest().String()) - multiArch = test.CreateVulnerableMultiarch() + multiArch = CreateVulnerableMultiarch() So(len(multiArch.Images), ShouldEqual, 3) So(multiArch.Reference, ShouldResemble, multiArch.Digest().String()) }) } func TestImageMethods(t *testing.T) { - img := test.CreateDefaultImage() + img := CreateDefaultImage() Convey("Image", t, func() { manifestBlob, err := json.Marshal(img.Manifest) diff --git a/pkg/test/multiarch.go b/pkg/test/image-utils/multiarch.go similarity index 97% rename from pkg/test/multiarch.go rename to pkg/test/image-utils/multiarch.go index f7b876aa..37b3eaf1 100644 --- a/pkg/test/multiarch.go +++ b/pkg/test/image-utils/multiarch.go @@ -1,4 +1,4 @@ -package test +package image import ( "encoding/json" @@ -15,7 +15,7 @@ type MultiarchImage struct { Images []Image Reference string - indexDescriptor ispec.Descriptor + IndexDescriptor ispec.Descriptor } func (mi *MultiarchImage) Digest() godigest.Digest { @@ -141,7 +141,7 @@ func (mb *BaseMultiarchBuilder) Build() MultiarchImage { Images: mb.images, Reference: ref, - indexDescriptor: ispec.Descriptor{ + IndexDescriptor: ispec.Descriptor{ MediaType: ispec.MediaTypeImageIndex, Size: int64(len(indexBlob)), Digest: indexDigest, diff --git a/pkg/test/image-utils/upload.go b/pkg/test/image-utils/upload.go new file mode 100644 index 00000000..bd4b88e4 --- /dev/null +++ b/pkg/test/image-utils/upload.go @@ -0,0 +1,252 @@ +package image + +import ( + "encoding/json" + "errors" + "fmt" + "net/http" + + godigest "github.com/opencontainers/go-digest" + ispec "github.com/opencontainers/image-spec/specs-go/v1" + "gopkg.in/resty.v1" + + testc "zotregistry.io/zot/pkg/test/common" + "zotregistry.io/zot/pkg/test/inject" +) + +var ( + ErrPostBlob = errors.New("can't post blob") + ErrPutBlob = errors.New("can't put blob") + ErrPutIndex = errors.New("can't put index") +) + +func UploadImage(img Image, baseURL, repo, ref string) error { + for _, blob := range img.Layers { + resp, err := resty.R().Post(baseURL + "/v2/" + repo + "/blobs/uploads/") + if err != nil { + return err + } + + if resp.StatusCode() != http.StatusAccepted { + return ErrPostBlob + } + + loc := resp.Header().Get("Location") + + digest := godigest.FromBytes(blob).String() + + resp, err = resty.R(). + SetHeader("Content-Length", fmt.Sprintf("%d", len(blob))). + SetHeader("Content-Type", "application/octet-stream"). + SetQueryParam("digest", digest). + SetBody(blob). + Put(baseURL + loc) + + if err != nil { + return err + } + + if resp.StatusCode() != http.StatusCreated { + return ErrPutBlob + } + } + + var err error + + cblob := img.ConfigDescriptor.Data + + // we'll remove this check once we make the full transition to the new way of generating test images + if len(cblob) == 0 { + cblob, err = json.Marshal(img.Config) + if err = inject.Error(err); err != nil { + return err + } + } + + cdigest := godigest.FromBytes(cblob) + + if img.Manifest.Config.MediaType == ispec.MediaTypeEmptyJSON || + img.Manifest.Config.Digest == ispec.DescriptorEmptyJSON.Digest { + cblob = ispec.DescriptorEmptyJSON.Data + cdigest = ispec.DescriptorEmptyJSON.Digest + } + + resp, err := resty.R(). + Post(baseURL + "/v2/" + repo + "/blobs/uploads/") + if err = inject.Error(err); err != nil { + return err + } + + if inject.ErrStatusCode(resp.StatusCode()) != http.StatusAccepted || inject.ErrStatusCode(resp.StatusCode()) == -1 { + return ErrPostBlob + } + + loc := testc.Location(baseURL, resp) + + // uploading blob should get 201 + resp, err = resty.R(). + SetHeader("Content-Length", fmt.Sprintf("%d", len(cblob))). + SetHeader("Content-Type", "application/octet-stream"). + SetQueryParam("digest", cdigest.String()). + SetBody(cblob). + Put(loc) + if err = inject.Error(err); err != nil { + return err + } + + if inject.ErrStatusCode(resp.StatusCode()) != http.StatusCreated || inject.ErrStatusCode(resp.StatusCode()) == -1 { + return ErrPostBlob + } + + manifestBlob := img.ManifestDescriptor.Data + + // we'll remove this check once we make the full transition to the new way of generating test images + if len(manifestBlob) == 0 { + manifestBlob, err = json.Marshal(img.Manifest) + if err = inject.Error(err); err != nil { + return err + } + } + + resp, err = resty.R(). + SetHeader("Content-type", ispec.MediaTypeImageManifest). + SetBody(manifestBlob). + Put(baseURL + "/v2/" + repo + "/manifests/" + ref) + + if inject.ErrStatusCode(resp.StatusCode()) != http.StatusCreated { + return ErrPutBlob + } + + if inject.ErrStatusCode(resp.StatusCode()) != http.StatusCreated { + return ErrPutBlob + } + + return err +} + +func UploadImageWithBasicAuth(img Image, baseURL, repo, ref, user, password string) error { + for _, blob := range img.Layers { + resp, err := resty.R(). + SetBasicAuth(user, password). + Post(baseURL + "/v2/" + repo + "/blobs/uploads/") + if err != nil { + return err + } + + if resp.StatusCode() != http.StatusAccepted { + return ErrPostBlob + } + + loc := resp.Header().Get("Location") + + digest := godigest.FromBytes(blob).String() + + resp, err = resty.R(). + SetBasicAuth(user, password). + SetHeader("Content-Length", fmt.Sprintf("%d", len(blob))). + SetHeader("Content-Type", "application/octet-stream"). + SetQueryParam("digest", digest). + SetBody(blob). + Put(baseURL + loc) + + if err != nil { + return err + } + + if resp.StatusCode() != http.StatusCreated { + return ErrPutBlob + } + } + // upload config + cblob, err := json.Marshal(img.Config) + if err = inject.Error(err); err != nil { + return err + } + + cdigest := godigest.FromBytes(cblob) + + if img.Manifest.Config.MediaType == ispec.MediaTypeEmptyJSON { + cblob = ispec.DescriptorEmptyJSON.Data + cdigest = ispec.DescriptorEmptyJSON.Digest + } + + resp, err := resty.R(). + SetBasicAuth(user, password). + Post(baseURL + "/v2/" + repo + "/blobs/uploads/") + if err = inject.Error(err); err != nil { + return err + } + + if inject.ErrStatusCode(resp.StatusCode()) != http.StatusAccepted || inject.ErrStatusCode(resp.StatusCode()) == -1 { + return ErrPostBlob + } + + loc := testc.Location(baseURL, resp) + + // uploading blob should get 201 + resp, err = resty.R(). + SetBasicAuth(user, password). + SetHeader("Content-Length", fmt.Sprintf("%d", len(cblob))). + SetHeader("Content-Type", "application/octet-stream"). + SetQueryParam("digest", cdigest.String()). + SetBody(cblob). + Put(loc) + if err = inject.Error(err); err != nil { + return err + } + + if inject.ErrStatusCode(resp.StatusCode()) != http.StatusCreated || inject.ErrStatusCode(resp.StatusCode()) == -1 { + return ErrPostBlob + } + + // put manifest + manifestBlob, err := json.Marshal(img.Manifest) + if err = inject.Error(err); err != nil { + return err + } + + _, err = resty.R(). + SetBasicAuth(user, password). + SetHeader("Content-type", "application/vnd.oci.image.manifest.v1+json"). + SetBody(manifestBlob). + Put(baseURL + "/v2/" + repo + "/manifests/" + ref) + + return err +} + +func UploadMultiarchImage(multiImage MultiarchImage, baseURL string, repo, ref string) error { + for _, image := range multiImage.Images { + err := UploadImage(image, baseURL, repo, image.DigestStr()) + if err != nil { + return err + } + } + + // put manifest + indexBlob, err := json.Marshal(multiImage.Index) + if err = inject.Error(err); err != nil { + return err + } + + resp, err := resty.R(). + SetHeader("Content-type", ispec.MediaTypeImageIndex). + SetBody(indexBlob). + Put(baseURL + "/v2/" + repo + "/manifests/" + ref) + + if resp.StatusCode() != http.StatusCreated { + return ErrPutIndex + } + + return err +} + +func DeleteImage(repo, reference, baseURL string) (int, error) { + resp, err := resty.R().Delete( + fmt.Sprintf(baseURL+"/v2/%s/manifests/%s", repo, reference), + ) + if err != nil { + return -1, err + } + + return resp.StatusCode(), err +} diff --git a/pkg/test/image-utils/upload_test.go b/pkg/test/image-utils/upload_test.go new file mode 100644 index 00000000..8b27ba1c --- /dev/null +++ b/pkg/test/image-utils/upload_test.go @@ -0,0 +1,548 @@ +package image_test + +import ( + "encoding/json" + "os" + "path" + "testing" + + godigest "github.com/opencontainers/go-digest" + "github.com/opencontainers/image-spec/specs-go" + ispec "github.com/opencontainers/image-spec/specs-go/v1" + . "github.com/smartystreets/goconvey/convey" + + "zotregistry.io/zot/pkg/api" + "zotregistry.io/zot/pkg/api/config" + . "zotregistry.io/zot/pkg/test" + . "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) + + conf := config.New() + conf.HTTP.Port = port + conf.Storage.RootDirectory = t.TempDir() + + ctlr := api.NewController(conf) + + ctlrManager := NewControllerManager(ctlr) + ctlrManager.StartAndWait(port) + defer ctlrManager.StopServer() + + layerBlob := []byte("test") + + img := Image{ + Layers: [][]byte{ + layerBlob, + }, + Manifest: ispec.Manifest{ + Layers: []ispec.Descriptor{ + { + Digest: godigest.FromBytes(layerBlob), + Size: int64(len(layerBlob)), + MediaType: ispec.MediaTypeImageLayerGzip, + }, + }, + Config: ispec.DescriptorEmptyJSON, + }, + Config: ispec.Image{}, + } + + err := UploadImage(img, baseURL, "test", img.DigestStr()) + So(err, ShouldNotBeNil) + }) + + Convey("Post request results in an error", t, func() { + port := GetFreePort() + baseURL := GetBaseURL(port) + + conf := config.New() + conf.HTTP.Port = port + conf.Storage.RootDirectory = t.TempDir() + + img := Image{ + Layers: make([][]byte, 10), + } + + err := UploadImage(img, baseURL, "test", "") + So(err, ShouldNotBeNil) + }) + + Convey("Post request status differs from accepted", t, func() { + port := GetFreePort() + baseURL := GetBaseURL(port) + + tempDir := t.TempDir() + conf := config.New() + conf.HTTP.Port = port + conf.Storage.RootDirectory = tempDir + + err := os.Chmod(tempDir, 0o400) + if err != nil { + t.Fatal(err) + } + + ctlr := api.NewController(conf) + + ctlrManager := NewControllerManager(ctlr) + ctlrManager.StartAndWait(port) + defer ctlrManager.StopServer() + + img := Image{ + Layers: make([][]byte, 10), + } + + err = UploadImage(img, baseURL, "test", "") + So(err, ShouldNotBeNil) + }) + + Convey("Put request results in an error", t, func() { + port := GetFreePort() + baseURL := GetBaseURL(port) + + conf := config.New() + conf.HTTP.Port = port + conf.Storage.RootDirectory = t.TempDir() + + ctlr := api.NewController(conf) + + ctlrManager := NewControllerManager(ctlr) + ctlrManager.StartAndWait(port) + defer ctlrManager.StopServer() + + img := Image{ + Layers: make([][]byte, 10), // invalid format that will result in an error + Config: ispec.Image{}, + } + + err := UploadImage(img, baseURL, "test", "") + So(err, ShouldNotBeNil) + }) + + Convey("Image uploaded successfully", t, func() { + port := GetFreePort() + baseURL := GetBaseURL(port) + + conf := config.New() + conf.HTTP.Port = port + conf.Storage.RootDirectory = t.TempDir() + + ctlr := api.NewController(conf) + + ctlrManager := NewControllerManager(ctlr) + ctlrManager.StartAndWait(port) + defer ctlrManager.StopServer() + + layerBlob := []byte("test") + + img := Image{ + Layers: [][]byte{ + layerBlob, + }, + Manifest: ispec.Manifest{ + Versioned: specs.Versioned{ + SchemaVersion: 2, + }, + Layers: []ispec.Descriptor{ + { + Digest: godigest.FromBytes(layerBlob), + Size: int64(len(layerBlob)), + MediaType: ispec.MediaTypeImageLayerGzip, + }, + }, + Config: ispec.DescriptorEmptyJSON, + }, + Config: ispec.Image{}, + } + + err := UploadImage(img, baseURL, "test", img.DigestStr()) + So(err, ShouldBeNil) + }) + + Convey("Upload image with authentification", t, func() { + tempDir := t.TempDir() + conf := config.New() + port := GetFreePort() + baseURL := GetBaseURL(port) + + user1 := "test" + password1 := "test" + testString1 := GetCredString(user1, password1) + htpasswdPath := MakeHtpasswdFileFromString(testString1) + defer os.Remove(htpasswdPath) + conf.HTTP.Auth = &config.AuthConfig{ + HTPasswd: config.AuthHTPasswd{ + Path: htpasswdPath, + }, + } + + conf.HTTP.Port = port + + conf.HTTP.AccessControl = &config.AccessControlConfig{ + Repositories: config.Repositories{ + "repo": config.PolicyGroup{ + Policies: []config.Policy{ + { + Users: []string{user1}, + Actions: []string{"read", "create"}, + }, + }, + DefaultPolicy: []string{}, + }, + "inaccessibleRepo": config.PolicyGroup{ + Policies: []config.Policy{ + { + Users: []string{user1}, + Actions: []string{"create"}, + }, + }, + DefaultPolicy: []string{}, + }, + }, + AdminPolicy: config.Policy{ + Users: []string{}, + Actions: []string{}, + }, + } + + ctlr := api.NewController(conf) + + ctlr.Config.Storage.RootDirectory = tempDir + + ctlrManager := NewControllerManager(ctlr) + ctlrManager.StartAndWait(port) + defer ctlrManager.StopServer() + + Convey("Request fail while pushing layer", func() { + err := UploadImageWithBasicAuth(Image{Layers: [][]byte{{1, 2, 3}}}, "badURL", "", "", "", "") + So(err, ShouldNotBeNil) + }) + Convey("Request status is not StatusOk while pushing layer", func() { + err := UploadImageWithBasicAuth(Image{Layers: [][]byte{{1, 2, 3}}}, baseURL, "", "repo", "", "") + So(err, ShouldNotBeNil) + }) + Convey("Request fail while pushing config", func() { + err := UploadImageWithBasicAuth(Image{}, "badURL", "", "", "", "") + So(err, ShouldNotBeNil) + }) + Convey("Request status is not StatusOk while pushing config", func() { + err := UploadImageWithBasicAuth(Image{}, baseURL, "", "repo", "", "") + So(err, ShouldNotBeNil) + }) + }) + + Convey("Blob upload wrong response status code", t, func() { + port := GetFreePort() + baseURL := GetBaseURL(port) + + tempDir := t.TempDir() + conf := config.New() + conf.HTTP.Port = port + conf.Storage.RootDirectory = tempDir + + ctlr := api.NewController(conf) + + ctlrManager := NewControllerManager(ctlr) + ctlrManager.StartAndWait(port) + defer ctlrManager.StopServer() + + layerBlob := []byte("test") + layerBlobDigest := godigest.FromBytes(layerBlob) + layerPath := path.Join(tempDir, "test", "blobs", "sha256") + + if _, err := os.Stat(layerPath); os.IsNotExist(err) { + err = os.MkdirAll(layerPath, 0o700) + if err != nil { + t.Fatal(err) + } + file, err := os.Create(path.Join(layerPath, layerBlobDigest.Encoded())) + if err != nil { + t.Fatal(err) + } + + err = os.Chmod(layerPath, 0o000) + if err != nil { + t.Fatal(err) + } + defer func() { + err = os.Chmod(layerPath, 0o700) + if err != nil { + t.Fatal(err) + } + os.RemoveAll(file.Name()) + }() + } + + img := Image{ + Layers: [][]byte{ + layerBlob, + }, // invalid format that will result in an error + Config: ispec.Image{}, + } + + err := UploadImage(img, baseURL, "test", "") + So(err, ShouldNotBeNil) + }) + + Convey("CreateBlobUpload wrong response status code", t, func() { + port := GetFreePort() + baseURL := GetBaseURL(port) + + tempDir := t.TempDir() + conf := config.New() + conf.HTTP.Port = port + conf.Storage.RootDirectory = tempDir + + ctlr := api.NewController(conf) + + ctlrManager := NewControllerManager(ctlr) + ctlrManager.StartAndWait(port) + defer ctlrManager.StopServer() + + layerBlob := []byte("test") + + img := Image{ + Layers: [][]byte{ + layerBlob, + }, // invalid format that will result in an error + Config: ispec.Image{}, + } + + Convey("CreateBlobUpload", func() { + injected := inject.InjectFailure(2) + if injected { + err := UploadImage(img, baseURL, "test", img.DigestStr()) + So(err, ShouldNotBeNil) + } + }) + Convey("UpdateBlobUpload", func() { + injected := inject.InjectFailure(4) + if injected { + err := UploadImage(img, baseURL, "test", img.DigestStr()) + So(err, ShouldNotBeNil) + } + }) + }) +} + +func TestInjectUploadImage(t *testing.T) { + Convey("Inject failures for unreachable lines", t, func() { + port := GetFreePort() + baseURL := GetBaseURL(port) + + tempDir := t.TempDir() + conf := config.New() + conf.HTTP.Port = port + conf.Storage.RootDirectory = tempDir + + ctlr := api.NewController(conf) + + ctlrManager := NewControllerManager(ctlr) + ctlrManager.StartAndWait(port) + defer ctlrManager.StopServer() + + layerBlob := []byte("test") + layerPath := path.Join(tempDir, "test", ".uploads") + + if _, err := os.Stat(layerPath); os.IsNotExist(err) { + err = os.MkdirAll(layerPath, 0o700) + if err != nil { + t.Fatal(err) + } + } + + img := Image{ + Layers: [][]byte{ + layerBlob, + }, // invalid format that will result in an error + Config: ispec.Image{}, + } + + Convey("first marshal", func() { + injected := inject.InjectFailure(0) + if injected { + err := UploadImage(img, baseURL, "test", img.DigestStr()) + So(err, ShouldNotBeNil) + } + }) + Convey("CreateBlobUpload POST call", func() { + injected := inject.InjectFailure(1) + if injected { + err := UploadImage(img, baseURL, "test", img.DigestStr()) + So(err, ShouldNotBeNil) + } + }) + Convey("UpdateBlobUpload PUT call", func() { + injected := inject.InjectFailure(3) + if injected { + err := UploadImage(img, baseURL, "test", img.DigestStr()) + So(err, ShouldNotBeNil) + } + }) + Convey("second marshal", func() { + injected := inject.InjectFailure(5) + if injected { + err := UploadImage(img, baseURL, "test", img.DigestStr()) + So(err, ShouldNotBeNil) + } + }) + }) +} + +func TestUploadMultiarchImage(t *testing.T) { + Convey("make controller", t, func() { + port := GetFreePort() + baseURL := GetBaseURL(port) + + conf := config.New() + conf.HTTP.Port = port + conf.Storage.RootDirectory = t.TempDir() + + ctlr := api.NewController(conf) + + ctlrManager := NewControllerManager(ctlr) + ctlrManager.StartAndWait(port) + defer ctlrManager.StopServer() + + layerBlob := []byte("test") + + img := Image{ + Layers: [][]byte{ + layerBlob, + }, + Manifest: ispec.Manifest{ + Versioned: specs.Versioned{ + SchemaVersion: 2, + }, + Layers: []ispec.Descriptor{ + { + Digest: godigest.FromBytes(layerBlob), + Size: int64(len(layerBlob)), + MediaType: ispec.MediaTypeImageLayerGzip, + }, + }, + Config: ispec.DescriptorEmptyJSON, + }, + Config: ispec.Image{}, + } + + manifestBuf, err := json.Marshal(img.Manifest) + So(err, ShouldBeNil) + + Convey("Multiarch image uploaded successfully", func() { + err = UploadMultiarchImage(MultiarchImage{ + Index: ispec.Index{ + Versioned: specs.Versioned{ + SchemaVersion: 2, + }, + MediaType: ispec.MediaTypeImageIndex, + Manifests: []ispec.Descriptor{ + { + MediaType: ispec.MediaTypeImageManifest, + Digest: godigest.FromBytes(manifestBuf), + Size: int64(len(manifestBuf)), + }, + }, + }, + Images: []Image{img}, + }, baseURL, "test", "index") + So(err, ShouldBeNil) + }) + + Convey("Multiarch image without schemaVersion should fail validation", func() { + err = UploadMultiarchImage(MultiarchImage{ + Index: ispec.Index{ + MediaType: ispec.MediaTypeImageIndex, + Manifests: []ispec.Descriptor{ + { + MediaType: ispec.MediaTypeImageManifest, + Digest: godigest.FromBytes(manifestBuf), + Size: int64(len(manifestBuf)), + }, + }, + }, + Images: []Image{img}, + }, baseURL, "test", "index") + So(err, ShouldNotBeNil) + }) + }) +} + +func TestInjectUploadImageWithBasicAuth(t *testing.T) { + Convey("Inject failures for unreachable lines", t, func() { + port := GetFreePort() + baseURL := GetBaseURL(port) + + tempDir := t.TempDir() + conf := config.New() + conf.HTTP.Port = port + conf.Storage.RootDirectory = tempDir + + user := "user" + password := "password" + testString := GetCredString(user, password) + htpasswdPath := MakeHtpasswdFileFromString(testString) + defer os.Remove(htpasswdPath) + conf.HTTP.Auth = &config.AuthConfig{ + HTPasswd: config.AuthHTPasswd{ + Path: htpasswdPath, + }, + } + + ctlr := api.NewController(conf) + + ctlrManager := NewControllerManager(ctlr) + ctlrManager.StartAndWait(port) + defer ctlrManager.StopServer() + + layerBlob := []byte("test") + layerPath := path.Join(tempDir, "test", ".uploads") + + if _, err := os.Stat(layerPath); os.IsNotExist(err) { + err = os.MkdirAll(layerPath, 0o700) + if err != nil { + t.Fatal(err) + } + } + + img := Image{ + Layers: [][]byte{ + layerBlob, + }, // invalid format that will result in an error + Config: ispec.Image{}, + } + + Convey("first marshal", func() { + injected := inject.InjectFailure(0) + if injected { + err := UploadImageWithBasicAuth(img, baseURL, "test", img.DigestStr(), "user", "password") + So(err, ShouldNotBeNil) + } + }) + Convey("CreateBlobUpload POST call", func() { + injected := inject.InjectFailure(1) + if injected { + err := UploadImageWithBasicAuth(img, baseURL, "test", img.DigestStr(), "user", "password") + So(err, ShouldNotBeNil) + } + }) + Convey("UpdateBlobUpload PUT call", func() { + injected := inject.InjectFailure(3) + if injected { + err := UploadImageWithBasicAuth(img, baseURL, "test", img.DigestStr(), "user", "password") + So(err, ShouldNotBeNil) + } + }) + Convey("second marshal", func() { + injected := inject.InjectFailure(5) + if injected { + err := UploadImageWithBasicAuth(img, baseURL, "test", img.DigestStr(), "user", "password") + So(err, ShouldNotBeNil) + } + }) + }) +} diff --git a/pkg/test/image-utils/utils.go b/pkg/test/image-utils/utils.go new file mode 100644 index 00000000..d7c73c85 --- /dev/null +++ b/pkg/test/image-utils/utils.go @@ -0,0 +1,129 @@ +package image + +import ( + mathRand "math/rand" + "os" + "path/filepath" + "time" + + godigest "github.com/opencontainers/go-digest" + ispec "github.com/opencontainers/image-spec/specs-go/v1" + + testc "zotregistry.io/zot/pkg/test/common" +) + +var vulnerableLayer []byte //nolint: gochecknoglobals + +// These are the 3 vulnerabilities found for the returned image by the GetVulnImage function. +const ( + Vulnerability1ID = "CVE-2023-2650" + Vulnerability2ID = "CVE-2023-1255" + Vulnerability3ID = "CVE-2023-2975" +) + +func GetLayerWithVulnerability() ([]byte, error) { + if vulnerableLayer != nil { + return vulnerableLayer, nil + } + + projectRootDir, err := testc.GetProjectRootDir() + if err != nil { + return nil, err + } + + // this is the path of the blob relative to the root of the zot folder + vulnBlobPath := "test/data/alpine/blobs/sha256/f56be85fc22e46face30e2c3de3f7fe7c15f8fd7c4e5add29d7f64b87abdaa09" + + absoluteVulnBlobPath, _ := filepath.Abs(filepath.Join(projectRootDir, vulnBlobPath)) + + vulnerableLayer, err := os.ReadFile(absoluteVulnBlobPath) //nolint: lll + if err != nil { + return nil, err + } + + return vulnerableLayer, nil +} + +func GetDefaultLayers() []Layer { + return []Layer{ + {Blob: []byte("abc"), Digest: godigest.FromBytes([]byte("abc")), MediaType: ispec.MediaTypeImageLayerGzip}, + {Blob: []byte("123"), Digest: godigest.FromBytes([]byte("123")), MediaType: ispec.MediaTypeImageLayerGzip}, + {Blob: []byte("xyz"), Digest: godigest.FromBytes([]byte("xyz")), MediaType: ispec.MediaTypeImageLayerGzip}, + } +} + +func GetDefaultLayersBlobs() [][]byte { + return [][]byte{ + []byte("abc"), + []byte("123"), + []byte("xyz"), + } +} + +func GetDefaultConfig() ispec.Image { + return ispec.Image{ + Created: DefaultTimeRef(), + Author: "ZotUser", + Platform: ispec.Platform{ + OS: "linux", + Architecture: "amd64", + }, + RootFS: ispec.RootFS{ + Type: "layers", + DiffIDs: []godigest.Digest{}, + }, + } +} + +func DefaultTimeRef() *time.Time { + var ( + year = 2010 + month = time.Month(1) + day = 1 + hour = 1 + min = 1 + sec = 1 + nsec = 0 + ) + + return DateRef(year, month, day, hour, min, sec, nsec, time.UTC) +} + +func DateRef(year int, month time.Month, day, hour, min, sec, nsec int, loc *time.Location) *time.Time { + date := time.Date(year, month, day, hour, min, sec, nsec, loc) + + return &date +} + +func RandomDateRef(loc *time.Location) *time.Time { + var ( + year = 1990 + mathRand.Intn(30) //nolint: gosec,gomnd + month = time.Month(1 + mathRand.Intn(10)) //nolint: gosec,gomnd + day = 1 + mathRand.Intn(5) //nolint: gosec,gomnd + hour = 1 + mathRand.Intn(22) //nolint: gosec,gomnd + min = 1 + mathRand.Intn(58) //nolint: gosec,gomnd + sec = 1 + mathRand.Intn(58) //nolint: gosec,gomnd + nsec = 1 + ) + + return DateRef(year, month, day, hour, min, sec, nsec, time.UTC) +} + +func GetDefaultVulnConfig() ispec.Image { + return ispec.Image{ + Created: DefaultTimeRef(), + Author: "ZotUser", + Platform: ispec.Platform{ + Architecture: "amd64", + OS: "linux", + }, + Config: ispec.ImageConfig{ + Env: []string{"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"}, + Cmd: []string{"/bin/sh"}, + }, + RootFS: ispec.RootFS{ + Type: "layers", + DiffIDs: []godigest.Digest{"sha256:f1417ff83b319fbdae6dd9cd6d8c9c88002dcd75ecf6ec201c8c6894681cf2b5"}, + }, + } +} diff --git a/pkg/test/oci-layout/oci_layout_test.go b/pkg/test/oci-layout/oci_layout_test.go index 14c63fe1..9b5f3838 100644 --- a/pkg/test/oci-layout/oci_layout_test.go +++ b/pkg/test/oci-layout/oci_layout_test.go @@ -25,6 +25,7 @@ import ( "zotregistry.io/zot/pkg/storage" "zotregistry.io/zot/pkg/storage/local" . "zotregistry.io/zot/pkg/test" + . "zotregistry.io/zot/pkg/test/image-utils" "zotregistry.io/zot/pkg/test/mocks" ocilayout "zotregistry.io/zot/pkg/test/oci-layout" ) diff --git a/pkg/test/repo.go b/pkg/test/repo.go index 6c1f4007..b73d77e8 100644 --- a/pkg/test/repo.go +++ b/pkg/test/repo.go @@ -4,16 +4,17 @@ import ( ispec "github.com/opencontainers/image-spec/specs-go/v1" mTypes "zotregistry.io/zot/pkg/meta/types" + imageUtil "zotregistry.io/zot/pkg/test/image-utils" ) type RepoImage struct { - Image + imageUtil.Image Tag string Statistics mTypes.DescriptorStatistics } type RepoMultiArchImage struct { - MultiarchImage + imageUtil.MultiarchImage ImageStatistics map[string]mTypes.DescriptorStatistics Tag string } @@ -68,8 +69,8 @@ func GetMetadataForRepos(repos ...Repo) ([]mTypes.RepoMetadata, map[string]mType }, repoMeta, manifestMetadataMap) } - indexDataMap[multiArch.indexDescriptor.Digest.String()] = mTypes.IndexData{ - IndexBlob: multiArch.indexDescriptor.Data, + indexDataMap[multiArch.IndexDescriptor.Digest.String()] = mTypes.IndexData{ + IndexBlob: multiArch.IndexDescriptor.Data, } } @@ -92,7 +93,7 @@ func addImageMetaToMetaDB(image RepoImage, repoMeta mTypes.RepoMetadata, // I need just the tags for now and the fake signature. // This is done just to mark a manifest as signed in the resulted RepoMeta - if image.Manifest.ArtifactType == TestFakeSignatureArtType && image.Manifest.Subject != nil { + if image.Manifest.ArtifactType == imageUtil.TestFakeSignatureArtType && image.Manifest.Subject != nil { signedManifestDig := image.Manifest.Subject.Digest.String() repoMeta.Signatures[signedManifestDig] = mTypes.ManifestSignatures{ "fakeSignature": []mTypes.SignatureInfo{{SignatureManifestDigest: image.ManifestDescriptor.Digest.String()}},