0
Fork 0
mirror of https://github.com/project-zot/zot.git synced 2024-12-30 22:34:13 -05:00

refactor: update tests to use the newer API for creating test images (#2168)

- update cve tests
- update scrub tests
- update tests for parsing storage and loading into meta DB
- update controller tests

Signed-off-by: Andrei Aaron <aaaron@luxoft.com>
This commit is contained in:
Andrei Aaron 2024-01-17 20:20:07 +02:00 committed by GitHub
parent 029f01ac6e
commit 8467a80a50
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 144 additions and 307 deletions

View file

@ -64,7 +64,6 @@ import (
storageTypes "zotregistry.io/zot/pkg/storage/types" storageTypes "zotregistry.io/zot/pkg/storage/types"
authutils "zotregistry.io/zot/pkg/test/auth" authutils "zotregistry.io/zot/pkg/test/auth"
test "zotregistry.io/zot/pkg/test/common" test "zotregistry.io/zot/pkg/test/common"
"zotregistry.io/zot/pkg/test/deprecated"
. "zotregistry.io/zot/pkg/test/image-utils" . "zotregistry.io/zot/pkg/test/image-utils"
"zotregistry.io/zot/pkg/test/inject" "zotregistry.io/zot/pkg/test/inject"
"zotregistry.io/zot/pkg/test/mocks" "zotregistry.io/zot/pkg/test/mocks"
@ -6327,26 +6326,13 @@ func TestArtifactReferences(t *testing.T) {
defer cm.StopServer() defer cm.StopServer()
repoName := "artifact-repo" repoName := "artifact-repo"
content := []byte("this is a blob")
digest := godigest.FromBytes(content)
So(digest, ShouldNotBeNil)
cfg, layers, manifest, err := deprecated.GetImageComponents(2) //nolint:staticcheck image := CreateImageWith().RandomLayers(1, 2).DefaultConfig().Build()
So(err, ShouldBeNil) digest := image.ManifestDescriptor.Digest
err = UploadImage( err := UploadImage(image, baseURL, repoName, "1.0")
Image{
Config: cfg,
Layers: layers,
Manifest: manifest,
}, baseURL, repoName, "1.0")
So(err, ShouldBeNil) So(err, ShouldBeNil)
content, err = json.Marshal(manifest)
So(err, ShouldBeNil)
digest = godigest.FromBytes(content)
So(digest, ShouldNotBeNil)
artifactType := "application/vnd.example.icecream.v1" artifactType := "application/vnd.example.icecream.v1"
Convey("Validate Image Manifest Reference", func() { Convey("Validate Image Manifest Reference", func() {
@ -6389,13 +6375,13 @@ func TestArtifactReferences(t *testing.T) {
{ {
MediaType: "application/vnd.oci.image.layer.v1.tar", MediaType: "application/vnd.oci.image.layer.v1.tar",
Digest: digest, Digest: digest,
Size: int64(len(content)), Size: image.ManifestDescriptor.Size,
}, },
}, },
Subject: &ispec.Descriptor{ Subject: &ispec.Descriptor{
MediaType: ispec.MediaTypeImageManifest, MediaType: ispec.MediaTypeImageManifest,
Digest: digest, Digest: digest,
Size: int64(len(content)), Size: image.ManifestDescriptor.Size,
}, },
Annotations: map[string]string{ Annotations: map[string]string{
"key": "val", "key": "val",
@ -6530,7 +6516,7 @@ func TestArtifactReferences(t *testing.T) {
}) })
Convey("Using valid content", func() { Convey("Using valid content", func() {
content, err = json.Marshal(manifest) content, err := json.Marshal(manifest)
So(err, ShouldBeNil) So(err, ShouldBeNil)
resp, err = resty.R().SetHeader("Content-Type", ispec.MediaTypeImageManifest). resp, err = resty.R().SetHeader("Content-Type", ispec.MediaTypeImageManifest).
SetBody(content).Put(baseURL + fmt.Sprintf("/v2/%s/manifests/1.0", repoName)) SetBody(content).Put(baseURL + fmt.Sprintf("/v2/%s/manifests/1.0", repoName))
@ -7366,33 +7352,23 @@ func TestStorageCommit(t *testing.T) {
Convey("Manifests", func() { Convey("Manifests", func() {
_, _ = Print("\nManifests") _, _ = Print("\nManifests")
cfg, layers, manifest, err := deprecated.GetImageComponents(2) //nolint:staticcheck
So(err, ShouldBeNil)
content := []byte("this is a blob5")
digest := godigest.FromBytes(content)
So(digest, ShouldNotBeNil)
// check a non-existent manifest // check a non-existent manifest
resp, err := resty.R().SetHeader("Content-Type", "application/vnd.oci.image.manifest.v1+json"). resp, err := resty.R().SetHeader("Content-Type", "application/vnd.oci.image.manifest.v1+json").
SetBody(content).Head(baseURL + "/v2/unknown/manifests/test:1.0") Head(baseURL + "/v2/unknown/manifests/test:1.0")
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, http.StatusNotFound) So(resp.StatusCode(), ShouldEqual, http.StatusNotFound)
image := CreateImageWith().RandomLayers(1, 2).DefaultConfig().Build()
repoName := "repo7" repoName := "repo7"
err = UploadImage( err = UploadImage(image, baseURL, repoName, "test:1.0")
Image{
Config: cfg,
Layers: layers,
Manifest: manifest,
}, baseURL, repoName, "test:1.0")
So(err, ShouldBeNil) So(err, ShouldBeNil)
_, err = os.Stat(path.Join(dir, "repo7")) _, err = os.Stat(path.Join(dir, "repo7"))
So(err, ShouldBeNil) So(err, ShouldBeNil)
content, err = json.Marshal(manifest) content := image.ManifestDescriptor.Data
So(err, ShouldBeNil) digest := image.ManifestDescriptor.Digest
digest = godigest.FromBytes(content)
So(digest, ShouldNotBeNil) So(digest, ShouldNotBeNil)
resp, err = resty.R().SetHeader("Content-Type", "application/vnd.oci.image.manifest.v1+json"). resp, err = resty.R().SetHeader("Content-Type", "application/vnd.oci.image.manifest.v1+json").
SetBody(content).Put(baseURL + "/v2/repo7/manifests/test:1.0") SetBody(content).Put(baseURL + "/v2/repo7/manifests/test:1.0")
@ -7410,26 +7386,12 @@ func TestStorageCommit(t *testing.T) {
So(digestHdr, ShouldNotBeEmpty) So(digestHdr, ShouldNotBeEmpty)
So(digestHdr, ShouldEqual, digest.String()) So(digestHdr, ShouldEqual, digest.String())
content, err = json.Marshal(manifest) err = UploadImage(image, baseURL, repoName, "test:1.0.1")
So(err, ShouldBeNil) So(err, ShouldBeNil)
err = UploadImage( image = CreateImageWith().RandomLayers(1, 1).DefaultConfig().Build()
Image{
Config: cfg,
Layers: layers,
Manifest: manifest,
}, baseURL, repoName, "test:1.0.1")
So(err, ShouldBeNil)
cfg, layers, manifest, err = deprecated.GetImageComponents(1) //nolint:staticcheck err = UploadImage(image, baseURL, repoName, "test:2.0")
So(err, ShouldBeNil)
err = UploadImage(
Image{
Config: cfg,
Layers: layers,
Manifest: manifest,
}, baseURL, repoName, "test:2.0")
So(err, ShouldBeNil) So(err, ShouldBeNil)
resp, err = resty.R().SetHeader("Content-Type", "application/vnd.oci.image.manifest.v1+json"). resp, err = resty.R().SetHeader("Content-Type", "application/vnd.oci.image.manifest.v1+json").
@ -7519,34 +7481,23 @@ func TestManifestImageIndex(t *testing.T) {
rthdlr := api.NewRouteHandler(ctlr) rthdlr := api.NewRouteHandler(ctlr)
cfg, layers, manifest, err := deprecated.GetImageComponents(2) //nolint:staticcheck img := CreateImageWith().RandomLayers(1, 2).DefaultConfig().Build()
So(err, ShouldBeNil)
content := []byte("this is a blob1")
digest := godigest.FromBytes(content)
So(digest, ShouldNotBeNil)
// check a non-existent manifest // check a non-existent manifest
resp, err := resty.R().SetHeader("Content-Type", ispec.MediaTypeImageManifest). resp, err := resty.R().SetHeader("Content-Type", ispec.MediaTypeImageManifest).
SetBody(content).Head(baseURL + "/v2/unknown/manifests/test:1.0") Head(baseURL + "/v2/unknown/manifests/test:1.0")
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, http.StatusNotFound) So(resp.StatusCode(), ShouldEqual, http.StatusNotFound)
repoName := "index" repoName := "index"
err = UploadImage( err = UploadImage(img, baseURL, repoName, "test:1.0")
Image{
Config: cfg,
Layers: layers,
Manifest: manifest,
}, baseURL, repoName, "test:1.0")
So(err, ShouldBeNil) So(err, ShouldBeNil)
_, err = os.Stat(path.Join(dir, "index")) _, err = os.Stat(path.Join(dir, "index"))
So(err, ShouldBeNil) So(err, ShouldBeNil)
content, err = json.Marshal(manifest) content := img.ManifestDescriptor.Data
So(err, ShouldBeNil) digest := img.ManifestDescriptor.Digest
digest = godigest.FromBytes(content)
So(digest, ShouldNotBeNil) So(digest, ShouldNotBeNil)
m1content := content m1content := content
resp, err = resty.R().SetHeader("Content-Type", ispec.MediaTypeImageManifest). resp, err = resty.R().SetHeader("Content-Type", ispec.MediaTypeImageManifest).
@ -7564,7 +7515,7 @@ func TestManifestImageIndex(t *testing.T) {
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, http.StatusAccepted) So(resp.StatusCode(), ShouldEqual, http.StatusAccepted)
img := CreateRandomImage() img = CreateRandomImage()
err = UploadImage(img, baseURL, repoName, img.DigestStr()) err = UploadImage(img, baseURL, repoName, img.DigestStr())
So(err, ShouldBeNil) So(err, ShouldBeNil)
@ -7946,41 +7897,24 @@ func TestManifestCollision(t *testing.T) {
cm.StartAndWait(port) cm.StartAndWait(port)
defer cm.StopServer() defer cm.StopServer()
cfg, layers, manifest, err := deprecated.GetImageComponents(2) //nolint:staticcheck img := CreateImageWith().RandomLayers(1, 2).DefaultConfig().Build()
So(err, ShouldBeNil)
err = UploadImage( err := UploadImage(img, baseURL, "index", "test:1.0")
Image{
Config: cfg,
Layers: layers,
Manifest: manifest,
}, baseURL, "index", "test:1.0")
So(err, ShouldBeNil) So(err, ShouldBeNil)
_, err = os.Stat(path.Join(dir, "index")) _, err = os.Stat(path.Join(dir, "index"))
So(err, ShouldBeNil) So(err, ShouldBeNil)
content := []byte("this is a blob1")
digest := godigest.FromBytes(content)
So(digest, ShouldNotBeNil)
// check a non-existent manifest // check a non-existent manifest
resp, err := resty.R().SetHeader("Content-Type", ispec.MediaTypeImageManifest). resp, err := resty.R().SetHeader("Content-Type", ispec.MediaTypeImageManifest).
SetBody(content).Head(baseURL + "/v2/unknown/manifests/test:1.0") Head(baseURL + "/v2/unknown/manifests/test:1.0")
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, http.StatusNotFound) So(resp.StatusCode(), ShouldEqual, http.StatusNotFound)
content, err = json.Marshal(manifest) digest := img.ManifestDescriptor.Digest
So(err, ShouldBeNil)
digest = godigest.FromBytes(content)
So(digest, ShouldNotBeNil) So(digest, ShouldNotBeNil)
err = UploadImage( err = UploadImage(img, baseURL, "index", "test:2.0")
Image{
Config: cfg,
Layers: layers,
Manifest: manifest,
}, baseURL, "index", "test:2.0")
So(err, ShouldBeNil) So(err, ShouldBeNil)
// Deletion should fail if using digest // Deletion should fail if using digest
@ -8660,19 +8594,11 @@ func TestGCSignaturesAndUntaggedManifestsWithMetaDB(t *testing.T) {
Convey("Overwrite original image, signatures should be garbage-collected", func() { Convey("Overwrite original image, signatures should be garbage-collected", func() {
// push an image without tag // push an image without tag
cfg, layers, manifest, err := deprecated.GetImageComponents(2) //nolint:staticcheck img := CreateImageWith().RandomLayers(1, 2).DefaultConfig().Build()
So(err, ShouldBeNil)
manifestBuf, err := json.Marshal(manifest) untaggedManifestDigest := img.ManifestDescriptor.Digest
So(err, ShouldBeNil)
untaggedManifestDigest := godigest.FromBytes(manifestBuf)
err = UploadImage( err = UploadImage(img, baseURL, repoName, untaggedManifestDigest.String())
Image{
Config: cfg,
Layers: layers,
Manifest: manifest,
}, baseURL, repoName, untaggedManifestDigest.String())
So(err, ShouldBeNil) So(err, ShouldBeNil)
// make sure repoDB reference was added // make sure repoDB reference was added
@ -8687,20 +8613,12 @@ func TestGCSignaturesAndUntaggedManifestsWithMetaDB(t *testing.T) {
So(ok, ShouldBeTrue) So(ok, ShouldBeTrue)
// overwrite image so that signatures will get invalidated and gc'ed // overwrite image so that signatures will get invalidated and gc'ed
cfg, layers, manifest, err = deprecated.GetImageComponents(3) //nolint:staticcheck img = CreateImageWith().RandomLayers(1, 3).DefaultConfig().Build()
err = UploadImage(img, baseURL, repoName, tag)
So(err, ShouldBeNil) So(err, ShouldBeNil)
err = UploadImage( newManifestDigest := img.ManifestDescriptor.Digest
Image{
Config: cfg,
Layers: layers,
Manifest: manifest,
}, baseURL, repoName, tag)
So(err, ShouldBeNil)
manifestBuf, err = json.Marshal(manifest)
So(err, ShouldBeNil)
newManifestDigest := godigest.FromBytes(manifestBuf)
err = gc.CleanRepo(ctx, repoName) //nolint: contextcheck err = gc.CleanRepo(ctx, repoName) //nolint: contextcheck
So(err, ShouldBeNil) So(err, ShouldBeNil)
@ -8804,26 +8722,17 @@ func TestGCSignaturesAndUntaggedManifestsWithMetaDB(t *testing.T) {
// upload multiple manifests // upload multiple manifests
for i := 0; i < 4; i++ { for i := 0; i < 4; i++ {
config, layers, manifest, err := deprecated.GetImageComponents(1000 + i) //nolint:staticcheck img := CreateImageWith().RandomLayers(1, 1000+i).DefaultConfig().Build()
So(err, ShouldBeNil)
manifestContent, err := json.Marshal(manifest) manifestDigest := img.ManifestDescriptor.Digest
So(err, ShouldBeNil)
manifestDigest := godigest.FromBytes(manifestContent) err = UploadImage(img, baseURL, repoName, manifestDigest.String())
err = UploadImage(
Image{
Manifest: manifest,
Config: config,
Layers: layers,
}, baseURL, repoName, manifestDigest.String())
So(err, ShouldBeNil) So(err, ShouldBeNil)
index.Manifests = append(index.Manifests, ispec.Descriptor{ index.Manifests = append(index.Manifests, ispec.Descriptor{
Digest: manifestDigest, Digest: manifestDigest,
MediaType: ispec.MediaTypeImageManifest, MediaType: ispec.MediaTypeImageManifest,
Size: int64(len(manifestContent)), Size: img.ManifestDescriptor.Size,
}) })
} }
@ -9055,28 +8964,18 @@ func TestSearchRoutes(t *testing.T) {
cm.StartAndWait(port) cm.StartAndWait(port)
defer cm.StopServer() defer cm.StopServer()
cfg, layers, manifest, err := deprecated.GetImageComponents(10000) //nolint:staticcheck img := CreateImageWith().RandomLayers(1, 10000).DefaultConfig().Build()
So(err, ShouldBeNil)
err = UploadImageWithBasicAuth( err := UploadImageWithBasicAuth(
Image{ img, baseURL, repoName, "latest",
Config: cfg,
Layers: layers,
Manifest: manifest,
}, baseURL, repoName, "latest",
user1, password1) user1, password1)
So(err, ShouldBeNil) So(err, ShouldBeNil)
// data for the inaccessible repo // data for the inaccessible repo
cfg, layers, manifest, err = deprecated.GetImageComponents(10000) //nolint:staticcheck img = CreateImageWith().RandomLayers(1, 10000).DefaultConfig().Build()
So(err, ShouldBeNil)
err = UploadImageWithBasicAuth( err = UploadImageWithBasicAuth(
Image{ img, baseURL, inaccessibleRepo, "latest",
Config: cfg,
Layers: layers,
Manifest: manifest,
}, baseURL, inaccessibleRepo, "latest",
user1, password1) user1, password1)
So(err, ShouldBeNil) So(err, ShouldBeNil)

View file

@ -40,7 +40,6 @@ import (
"zotregistry.io/zot/pkg/storage" "zotregistry.io/zot/pkg/storage"
"zotregistry.io/zot/pkg/storage/local" "zotregistry.io/zot/pkg/storage/local"
test "zotregistry.io/zot/pkg/test/common" test "zotregistry.io/zot/pkg/test/common"
"zotregistry.io/zot/pkg/test/deprecated"
. "zotregistry.io/zot/pkg/test/image-utils" . "zotregistry.io/zot/pkg/test/image-utils"
"zotregistry.io/zot/pkg/test/mocks" "zotregistry.io/zot/pkg/test/mocks"
ociutils "zotregistry.io/zot/pkg/test/oci-utils" ociutils "zotregistry.io/zot/pkg/test/oci-utils"
@ -1729,35 +1728,30 @@ func TestFixedTagsWithIndex(t *testing.T) {
defer cm.StopServer() defer cm.StopServer()
// push index with 2 manifests: one with vulns and one without // push index with 2 manifests: one with vulns and one without
vulnManifestCreated := time.Date(2010, 1, 1, 1, 1, 1, 1, time.UTC) vulnManifestCreated := time.Date(2010, 1, 1, 1, 1, 1, 1, time.UTC)
vulnManifest, err := deprecated.GetVulnImageWithConfig(ispec.Image{ //nolint:staticcheck vulnImageConfig := GetDefaultConfig()
Created: &vulnManifestCreated, vulnImageConfig.Created = &vulnManifestCreated
Platform: ispec.Platform{OS: "linux", Architecture: "amd64"}, vulnImageConfig.Platform = ispec.Platform{OS: "linux", Architecture: "amd64"}
}) vulnSingleArchImage := CreateImageWith().VulnerableLayers().VulnerableConfig(vulnImageConfig).Build()
So(err, ShouldBeNil)
fixedManifestCreated := time.Date(2010, 1, 1, 1, 1, 1, 1, time.UTC) fixedManifestCreated := time.Date(2010, 1, 1, 1, 1, 1, 1, time.UTC)
fixedManifest, err := deprecated.GetImageWithConfig(ispec.Image{ //nolint:staticcheck fixedImageConfig := GetDefaultConfig()
Created: &fixedManifestCreated, fixedImageConfig.Created = &fixedManifestCreated
Platform: ispec.Platform{OS: "windows", Architecture: "amd64"}, fixedImageConfig.Platform = ispec.Platform{OS: "windows", Architecture: "amd64"}
}) fixedSingleArchImage := CreateImageWith().DefaultLayers().ImageConfig(fixedImageConfig).Build()
So(err, ShouldBeNil)
fixedDigest := fixedManifest.Digest()
multiArch := deprecated.GetMultiarchImageForImages([]Image{fixedManifest, //nolint:staticcheck multiArchImage := CreateMultiarchWith().Images([]Image{vulnSingleArchImage, fixedSingleArchImage}).Build()
vulnManifest})
err = UploadMultiarchImage(multiArch, baseURL, "repo", "multi-arch-tag") err = UploadMultiarchImage(multiArchImage, baseURL, "repo", "multi-arch-tag")
So(err, ShouldBeNil) So(err, ShouldBeNil)
// oldest vulnerability // oldest vulnerability
simpleVulnCreated := time.Date(2005, 1, 1, 1, 1, 1, 1, time.UTC) simpleVulnCreated := time.Date(2005, 1, 1, 1, 1, 1, 1, time.UTC)
simpleVulnImg, err := deprecated.GetVulnImageWithConfig(ispec.Image{ //nolint:staticcheck singleVulnImageConfig := GetDefaultConfig()
Created: &simpleVulnCreated, singleVulnImageConfig.Created = &simpleVulnCreated
Platform: ispec.Platform{OS: "windows", Architecture: "amd64"}, singleVulnImageConfig.Platform = ispec.Platform{OS: "windows", Architecture: "amd64"}
}) simpleVulnImage := CreateImageWith().VulnerableLayers().VulnerableConfig(singleVulnImageConfig).Build()
So(err, ShouldBeNil)
err = UploadImage(simpleVulnImg, baseURL, "repo", "vuln-img") err = UploadImage(simpleVulnImage, baseURL, "repo", "vuln-img")
So(err, ShouldBeNil) So(err, ShouldBeNil)
// Wait for trivy db to download // Wait for trivy db to download
@ -1771,7 +1765,7 @@ func TestFixedTagsWithIndex(t *testing.T) {
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(len(tagsInfo), ShouldEqual, 1) So(len(tagsInfo), ShouldEqual, 1)
So(len(tagsInfo[0].Manifests), ShouldEqual, 1) So(len(tagsInfo[0].Manifests), ShouldEqual, 1)
So(tagsInfo[0].Manifests[0].Digest, ShouldResemble, fixedDigest) So(tagsInfo[0].Manifests[0].Digest, ShouldResemble, fixedSingleArchImage.ManifestDescriptor.Digest)
const query = ` const query = `
{ {
@ -1794,7 +1788,7 @@ func TestFixedTagsWithIndex(t *testing.T) {
So(len(responseStruct.Results), ShouldEqual, 1) So(len(responseStruct.Results), ShouldEqual, 1)
So(len(responseStruct.Results[0].Manifests), ShouldEqual, 1) So(len(responseStruct.Results[0].Manifests), ShouldEqual, 1)
fixedManifestResp := responseStruct.Results[0].Manifests[0] fixedManifestResp := responseStruct.Results[0].Manifests[0]
So(fixedManifestResp.Digest, ShouldResemble, fixedDigest.String()) So(fixedManifestResp.Digest, ShouldResemble, fixedSingleArchImage.ManifestDescriptor.Digest.String())
}) })
} }

View file

@ -4,9 +4,7 @@
package trivy package trivy
import ( import (
"bytes"
"context" "context"
"encoding/json"
"os" "os"
"path" "path"
"testing" "testing"
@ -29,38 +27,17 @@ import (
"zotregistry.io/zot/pkg/storage/local" "zotregistry.io/zot/pkg/storage/local"
storageTypes "zotregistry.io/zot/pkg/storage/types" storageTypes "zotregistry.io/zot/pkg/storage/types"
test "zotregistry.io/zot/pkg/test/common" test "zotregistry.io/zot/pkg/test/common"
"zotregistry.io/zot/pkg/test/deprecated"
. "zotregistry.io/zot/pkg/test/image-utils" . "zotregistry.io/zot/pkg/test/image-utils"
"zotregistry.io/zot/pkg/test/mocks" "zotregistry.io/zot/pkg/test/mocks"
) )
func generateTestImage(storeController storage.StoreController, image string) { func generateTestImage(storeController storage.StoreController, imageName string) {
repoName, tag := common.GetImageDirAndTag(image) repoName, tag := common.GetImageDirAndTag(imageName)
config, layers, manifest, err := deprecated.GetImageComponents(10) //nolint:staticcheck image := CreateRandomImage()
So(err, ShouldBeNil)
store := storeController.GetImageStore(repoName) err := WriteImageToFileSystem(
err = store.InitRepo(repoName) image, repoName, tag, storeController)
So(err, ShouldBeNil)
for _, layerBlob := range layers {
layerReader := bytes.NewReader(layerBlob)
layerDigest := godigest.FromBytes(layerBlob)
_, _, err = store.FullBlobUpload(repoName, layerReader, layerDigest)
So(err, ShouldBeNil)
}
configBlob, err := json.Marshal(config)
So(err, ShouldBeNil)
configReader := bytes.NewReader(configBlob)
configDigest := godigest.FromBytes(configBlob)
_, _, err = store.FullBlobUpload(repoName, configReader, configDigest)
So(err, ShouldBeNil)
manifestBlob, err := json.Marshal(manifest)
So(err, ShouldBeNil)
_, _, err = store.PutImageManifest(repoName, tag, ispec.MediaTypeImageManifest, manifestBlob)
So(err, ShouldBeNil) So(err, ShouldBeNil)
} }

View file

@ -25,7 +25,6 @@ import (
"zotregistry.io/zot/pkg/storage" "zotregistry.io/zot/pkg/storage"
"zotregistry.io/zot/pkg/storage/local" "zotregistry.io/zot/pkg/storage/local"
. "zotregistry.io/zot/pkg/test/common" . "zotregistry.io/zot/pkg/test/common"
"zotregistry.io/zot/pkg/test/deprecated"
. "zotregistry.io/zot/pkg/test/image-utils" . "zotregistry.io/zot/pkg/test/image-utils"
"zotregistry.io/zot/pkg/test/mocks" "zotregistry.io/zot/pkg/test/mocks"
) )
@ -97,8 +96,8 @@ func TestScanningByDigest(t *testing.T) {
simpleImage := CreateRandomImage() simpleImage := CreateRandomImage()
multiArch := deprecated.GetMultiarchImageForImages([]Image{simpleImage, //nolint:staticcheck multiArch := CreateMultiarchWith().Images([]Image{simpleImage, //nolint:staticcheck
vulnImage}) vulnImage}).Build()
err := UploadMultiarchImage(multiArch, baseURL, "multi-arch", "multi-arch-tag") err := UploadMultiarchImage(multiArch, baseURL, "multi-arch", "multi-arch-tag")
So(err, ShouldBeNil) So(err, ShouldBeNil)

View file

@ -26,7 +26,6 @@ import (
"zotregistry.io/zot/pkg/storage/local" "zotregistry.io/zot/pkg/storage/local"
storageTypes "zotregistry.io/zot/pkg/storage/types" storageTypes "zotregistry.io/zot/pkg/storage/types"
tcommon "zotregistry.io/zot/pkg/test/common" tcommon "zotregistry.io/zot/pkg/test/common"
"zotregistry.io/zot/pkg/test/deprecated"
. "zotregistry.io/zot/pkg/test/image-utils" . "zotregistry.io/zot/pkg/test/image-utils"
"zotregistry.io/zot/pkg/test/mocks" "zotregistry.io/zot/pkg/test/mocks"
ociutils "zotregistry.io/zot/pkg/test/oci-utils" ociutils "zotregistry.io/zot/pkg/test/oci-utils"
@ -368,15 +367,9 @@ func RunParseStorageTests(rootDir string, metaDB mTypes.MetaDB, log log.Logger)
signedManifestDigest := godigest.FromBytes(manifestBlob) signedManifestDigest := godigest.FromBytes(manifestBlob)
config, layers, manifest, err := deprecated.GetRandomImageComponents(100) //nolint:staticcheck image := CreateRandomImage()
So(err, ShouldBeNil)
err = WriteImageToFileSystem( err = WriteImageToFileSystem(image, repo, signatureTag, storeController)
Image{
Config: config,
Layers: layers,
Manifest: manifest,
}, repo, signatureTag, storeController)
So(err, ShouldBeNil) So(err, ShouldBeNil)
// remove tag2 from index.json // remove tag2 from index.json
@ -427,37 +420,26 @@ func RunParseStorageTests(rootDir string, metaDB mTypes.MetaDB, log log.Logger)
log, monitoring.NewMetricsServer(false, log), nil, nil) log, monitoring.NewMetricsServer(false, log), nil, nil)
storeController := storage.StoreController{DefaultStore: imageStore} storeController := storage.StoreController{DefaultStore: imageStore}
// add an image
config, layers, manifest, err := deprecated.GetRandomImageComponents(100) //nolint:staticcheck
So(err, ShouldBeNil)
err = WriteImageToFileSystem( // add an image
Image{ image := CreateRandomImage()
Config: config,
Layers: layers, err := WriteImageToFileSystem(image, repo, "tag1", storeController)
Manifest: manifest,
}, repo, "tag1", storeController)
So(err, ShouldBeNil) So(err, ShouldBeNil)
// add mock cosign signature without pushing the signed image // add mock cosign signature without pushing the signed image
image, err := deprecated.GetRandomImage() //nolint:staticcheck image = CreateRandomImage()
So(err, ShouldBeNil) So(err, ShouldBeNil)
signatureTag, err := signature.GetCosignSignatureTagForManifest(image.Manifest) signatureTag, err := signature.GetCosignSignatureTagForManifest(image.Manifest)
So(err, ShouldBeNil) So(err, ShouldBeNil)
missingImageDigest := image.Digest() missingImageDigest := image.ManifestDescriptor.Digest
// get the body of the signature // get the body of the signature
config, layers, manifest, err = deprecated.GetRandomImageComponents(100) //nolint:staticcheck signature := CreateRandomImage()
So(err, ShouldBeNil)
err = WriteImageToFileSystem( err = WriteImageToFileSystem(signature, repo, signatureTag, storeController)
Image{
Config: config,
Layers: layers,
Manifest: manifest,
}, repo, signatureTag, storeController)
So(err, ShouldBeNil) So(err, ShouldBeNil)
err = meta.ParseStorage(metaDB, storeController, log) //nolint: contextcheck err = meta.ParseStorage(metaDB, storeController, log) //nolint: contextcheck

View file

@ -36,7 +36,6 @@ import (
storageConstants "zotregistry.io/zot/pkg/storage/constants" storageConstants "zotregistry.io/zot/pkg/storage/constants"
"zotregistry.io/zot/pkg/storage/s3" "zotregistry.io/zot/pkg/storage/s3"
storageTypes "zotregistry.io/zot/pkg/storage/types" storageTypes "zotregistry.io/zot/pkg/storage/types"
"zotregistry.io/zot/pkg/test/deprecated"
. "zotregistry.io/zot/pkg/test/image-utils" . "zotregistry.io/zot/pkg/test/image-utils"
"zotregistry.io/zot/pkg/test/inject" "zotregistry.io/zot/pkg/test/inject"
"zotregistry.io/zot/pkg/test/mocks" "zotregistry.io/zot/pkg/test/mocks"
@ -467,8 +466,11 @@ func TestGetOrasAndOCIReferrers(t *testing.T) {
_, imgStore, _ := createObjectsStore(testDir, tdir, true) _, imgStore, _ := createObjectsStore(testDir, tdir, true)
Convey("Upload test image", t, func(c C) { Convey("Upload test image", t, func(c C) {
cfg, layers, manifest, err := deprecated.GetImageComponents(100) //nolint:staticcheck image := CreateDefaultImage()
So(err, ShouldBeNil)
manifest := image.Manifest
cfg := image.Config
layers := image.Layers
for _, content := range layers { for _, content := range layers {
upload, err := imgStore.NewBlobUpload(repo) upload, err := imgStore.NewBlobUpload(repo)

View file

@ -26,7 +26,6 @@ import (
"zotregistry.io/zot/pkg/storage/local" "zotregistry.io/zot/pkg/storage/local"
"zotregistry.io/zot/pkg/storage/s3" "zotregistry.io/zot/pkg/storage/s3"
storageTypes "zotregistry.io/zot/pkg/storage/types" storageTypes "zotregistry.io/zot/pkg/storage/types"
"zotregistry.io/zot/pkg/test/deprecated"
. "zotregistry.io/zot/pkg/test/image-utils" . "zotregistry.io/zot/pkg/test/image-utils"
"zotregistry.io/zot/pkg/test/mocks" "zotregistry.io/zot/pkg/test/mocks"
tskip "zotregistry.io/zot/pkg/test/skip" tskip "zotregistry.io/zot/pkg/test/skip"
@ -92,24 +91,9 @@ func RunCheckAllBlobsIntegrityTests( //nolint: thelper
storeCtlr.DefaultStore = imgStore storeCtlr.DefaultStore = imgStore
So(storeCtlr.GetImageStore(repoName), ShouldResemble, imgStore) So(storeCtlr.GetImageStore(repoName), ShouldResemble, imgStore)
config, layers, manifest, err := deprecated.GetImageComponents(1000) //nolint:staticcheck image := CreateRandomImage()
So(err, ShouldBeNil)
layerReader := bytes.NewReader(layers[0]) err = WriteImageToFileSystem(image, repoName, tag, storeCtlr)
layerDigest := godigest.FromBytes(layers[0])
_, _, err = imgStore.FullBlobUpload(repoName, layerReader, layerDigest)
So(err, ShouldBeNil)
configBlob, err := json.Marshal(config)
So(err, ShouldBeNil)
configReader := bytes.NewReader(configBlob)
configDigest := godigest.FromBytes(configBlob)
_, _, err = imgStore.FullBlobUpload(repoName, configReader, configDigest)
So(err, ShouldBeNil)
manifestBlob, err := json.Marshal(manifest)
So(err, ShouldBeNil)
manifestDigest, _, err := imgStore.PutImageManifest(repoName, tag, ispec.MediaTypeImageManifest, manifestBlob)
So(err, ShouldBeNil) So(err, ShouldBeNil)
Convey("Blobs integrity not affected", func() { Convey("Blobs integrity not affected", func() {
@ -144,11 +128,11 @@ func RunCheckAllBlobsIntegrityTests( //nolint: thelper
Convey("Manifest integrity affected", func() { Convey("Manifest integrity affected", func() {
// get content of manifest file // get content of manifest file
content, _, _, err := imgStore.GetImageManifest(repoName, manifestDigest.String()) content, _, _, err := imgStore.GetImageManifest(repoName, image.ManifestDescriptor.Digest.String())
So(err, ShouldBeNil) So(err, ShouldBeNil)
// delete content of manifest file // delete content of manifest file
manifestDig := manifestDigest.Encoded() manifestDig := image.ManifestDescriptor.Digest.Encoded()
manifestFile := path.Join(imgStore.RootDir(), repoName, "/blobs/sha256", manifestDig) manifestFile := path.Join(imgStore.RootDir(), repoName, "/blobs/sha256", manifestDig)
err = driver.Delete(manifestFile) err = driver.Delete(manifestFile)
So(err, ShouldBeNil) So(err, ShouldBeNil)
@ -210,11 +194,11 @@ func RunCheckAllBlobsIntegrityTests( //nolint: thelper
Convey("Config integrity affected", func() { Convey("Config integrity affected", func() {
// get content of config file // get content of config file
content, err := imgStore.GetBlobContent(repoName, configDigest) content, err := imgStore.GetBlobContent(repoName, image.ConfigDescriptor.Digest)
So(err, ShouldBeNil) So(err, ShouldBeNil)
// delete content of config file // delete content of config file
configDig := configDigest.Encoded() configDig := image.ConfigDescriptor.Digest.Encoded()
configFile := path.Join(imgStore.RootDir(), repoName, "/blobs/sha256", configDig) configFile := path.Join(imgStore.RootDir(), repoName, "/blobs/sha256", configDig)
err = driver.Delete(configFile) err = driver.Delete(configFile)
So(err, ShouldBeNil) So(err, ShouldBeNil)
@ -254,11 +238,11 @@ func RunCheckAllBlobsIntegrityTests( //nolint: thelper
Convey("Layers integrity affected", func() { Convey("Layers integrity affected", func() {
// get content of layer // get content of layer
content, err := imgStore.GetBlobContent(repoName, layerDigest) content, err := imgStore.GetBlobContent(repoName, image.Manifest.Layers[0].Digest)
So(err, ShouldBeNil) So(err, ShouldBeNil)
// delete content of layer file // delete content of layer file
layerDig := layerDigest.Encoded() layerDig := image.Manifest.Layers[0].Digest.Encoded()
layerFile := path.Join(imgStore.RootDir(), repoName, "/blobs/sha256", layerDig) layerFile := path.Join(imgStore.RootDir(), repoName, "/blobs/sha256", layerDig)
_, err = driver.WriteFile(layerFile, []byte(" ")) _, err = driver.WriteFile(layerFile, []byte(" "))
So(err, ShouldBeNil) So(err, ShouldBeNil)
@ -284,11 +268,11 @@ func RunCheckAllBlobsIntegrityTests( //nolint: thelper
Convey("Layer not found", func() { Convey("Layer not found", func() {
// get content of layer // get content of layer
content, err := imgStore.GetBlobContent(repoName, layerDigest) content, err := imgStore.GetBlobContent(repoName, image.Manifest.Layers[0].Digest)
So(err, ShouldBeNil) So(err, ShouldBeNil)
// change layer file permissions // change layer file permissions
layerDig := layerDigest.Encoded() layerDig := image.Manifest.Layers[0].Digest.Encoded()
repoDir := path.Join(imgStore.RootDir(), repoName) repoDir := path.Join(imgStore.RootDir(), repoName)
layerFile := path.Join(repoDir, "/blobs/sha256", layerDig) layerFile := path.Join(repoDir, "/blobs/sha256", layerDig)
err = driver.Delete(layerFile) err = driver.Delete(layerFile)
@ -323,32 +307,16 @@ func RunCheckAllBlobsIntegrityTests( //nolint: thelper
}) })
Convey("Scrub index", func() { Convey("Scrub index", func() {
newConfig, newLayers, newManifest, err := deprecated.GetImageComponents(10) //nolint:staticcheck newImage := CreateRandomImage()
So(err, ShouldBeNil) newManifestDigest := newImage.ManifestDescriptor.Digest
newLayerReader := bytes.NewReader(newLayers[0]) err = WriteImageToFileSystem(newImage, repoName, "2.0", storeCtlr)
newLayerDigest := godigest.FromBytes(newLayers[0])
_, _, err = imgStore.FullBlobUpload(repoName, newLayerReader, newLayerDigest)
So(err, ShouldBeNil)
newConfigBlob, err := json.Marshal(newConfig)
So(err, ShouldBeNil)
newConfigReader := bytes.NewReader(newConfigBlob)
newConfigDigest := godigest.FromBytes(newConfigBlob)
_, _, err = imgStore.FullBlobUpload(repoName, newConfigReader, newConfigDigest)
So(err, ShouldBeNil)
newManifestBlob, err := json.Marshal(newManifest)
So(err, ShouldBeNil)
newManifestReader := bytes.NewReader(newManifestBlob)
newManifestDigest := godigest.FromBytes(newManifestBlob)
_, _, err = imgStore.FullBlobUpload(repoName, newManifestReader, newManifestDigest)
So(err, ShouldBeNil) So(err, ShouldBeNil)
idx, err := common.GetIndex(imgStore, repoName, log) idx, err := common.GetIndex(imgStore, repoName, log)
So(err, ShouldBeNil) So(err, ShouldBeNil)
manifestDescriptor, ok := common.GetManifestDescByReference(idx, manifestDigest.String()) manifestDescriptor, ok := common.GetManifestDescByReference(idx, image.ManifestDescriptor.Digest.String())
So(ok, ShouldBeTrue) So(ok, ShouldBeTrue)
var index ispec.Index var index ispec.Index
@ -358,7 +326,7 @@ func RunCheckAllBlobsIntegrityTests( //nolint: thelper
{ {
MediaType: ispec.MediaTypeImageManifest, MediaType: ispec.MediaTypeImageManifest,
Digest: newManifestDigest, Digest: newManifestDigest,
Size: int64(len(newManifestBlob)), Size: newImage.ManifestDescriptor.Size,
}, },
} }
@ -472,7 +440,7 @@ func RunCheckAllBlobsIntegrityTests( //nolint: thelper
Convey("Manifest not found", func() { Convey("Manifest not found", func() {
// delete manifest file // delete manifest file
manifestDig := manifestDigest.Encoded() manifestDig := image.ManifestDescriptor.Digest.Encoded()
manifestFile := path.Join(imgStore.RootDir(), repoName, "/blobs/sha256", manifestDig) manifestFile := path.Join(imgStore.RootDir(), repoName, "/blobs/sha256", manifestDig)
err = driver.Delete(manifestFile) err = driver.Delete(manifestFile)
So(err, ShouldBeNil) So(err, ShouldBeNil)
@ -504,7 +472,7 @@ func RunCheckAllBlobsIntegrityTests( //nolint: thelper
index, err := common.GetIndex(imgStore, repoName, log) index, err := common.GetIndex(imgStore, repoName, log)
So(err, ShouldBeNil) So(err, ShouldBeNil)
manifestDescriptor, ok := common.GetManifestDescByReference(index, manifestDigest.String()) manifestDescriptor, ok := common.GetManifestDescByReference(index, image.ManifestDescriptor.Digest.String())
So(ok, ShouldBeTrue) So(ok, ShouldBeTrue)
err = WriteImageToFileSystem(CreateDefaultImageWith().Subject(&manifestDescriptor).Build(), err = WriteImageToFileSystem(CreateDefaultImageWith().Subject(&manifestDescriptor).Build(),
@ -557,5 +525,42 @@ func RunCheckAllBlobsIntegrityTests( //nolint: thelper
_, err = storeController.CheckAllBlobsIntegrity(context.Background()) _, err = storeController.CheckAllBlobsIntegrity(context.Background())
So(err, ShouldNotBeNil) So(err, ShouldNotBeNil)
So(err, ShouldEqual, errUnexpectedError) So(err, ShouldEqual, errUnexpectedError)
manifestDigest := godigest.FromString("abcd")
mockedImgStore = mocks.MockedImageStore{
GetRepositoriesFn: func() ([]string, error) {
return []string{repoName}, nil
},
GetIndexContentFn: func(repo string) ([]byte, error) {
var index ispec.Index
index.SchemaVersion = 2
index.Manifests = []ispec.Descriptor{
{
MediaType: "InvalidMediaType",
Digest: manifestDigest,
Size: int64(100),
Annotations: map[string]string{ispec.AnnotationRefName: "1.0"},
},
}
return json.Marshal(index)
},
}
storeController.DefaultStore = mockedImgStore
res, err := storeController.CheckAllBlobsIntegrity(context.Background())
So(err, ShouldBeNil)
buff := bytes.NewBufferString("")
res.PrintScrubResults(buff)
space := regexp.MustCompile(`\s+`)
str := space.ReplaceAllString(buff.String(), " ")
actual := strings.TrimSpace(str)
So(actual, ShouldContainSubstring, "REPOSITORY TAG STATUS AFFECTED BLOB ERROR")
So(actual, ShouldContainSubstring, fmt.Sprintf("%s 1.0 affected %s invalid manifest content",
repoName, manifestDigest.Encoded()))
}) })
} }

View file

@ -171,6 +171,15 @@ func TestStorageAPIs(t *testing.T) {
Convey("Repo layout", t, func(c C) { Convey("Repo layout", t, func(c C) {
repoName := "test" repoName := "test"
Convey("Get all blobs from repo without initialization", func() {
allBlobs, err := imgStore.GetAllBlobs(repoName)
So(err, ShouldBeNil)
So(allBlobs, ShouldBeEmpty)
ok := imgStore.DirExists(path.Join(imgStore.RootDir(), repoName))
So(ok, ShouldBeFalse)
})
Convey("Validate repo without initialization", func() { Convey("Validate repo without initialization", func() {
v, err := imgStore.ValidateRepo(repoName) v, err := imgStore.ValidateRepo(repoName)
So(v, ShouldEqual, false) So(v, ShouldEqual, false)

View file

@ -121,36 +121,6 @@ func GetRandomLayer(size int) []byte {
return layer return layer
} }
// Deprecated: Should use the new functions starting with "Create".
func GetVulnImageWithConfig(config ispec.Image) (image.Image, error) {
vulnerableLayer, err := image.GetLayerWithVulnerability()
if err != nil {
return image.Image{}, err
}
vulnerableConfig := ispec.Image{
Platform: config.Platform,
Config: config.Config,
RootFS: ispec.RootFS{
Type: "layers",
DiffIDs: []godigest.Digest{"sha256:f1417ff83b319fbdae6dd9cd6d8c9c88002dcd75ecf6ec201c8c6894681cf2b5"},
},
Created: config.Created,
History: config.History,
}
img, err := GetImageWithComponents(
vulnerableConfig,
[][]byte{
vulnerableLayer,
})
if err != nil {
return image.Image{}, err
}
return img, err
}
// Deprecated: Should use the new functions starting with "Create". // Deprecated: Should use the new functions starting with "Create".
func GetRandomImage() (image.Image, error) { func GetRandomImage() (image.Image, error) {
const layerSize = 20 const layerSize = 20