0
Fork 0
mirror of https://github.com/project-zot/zot.git synced 2024-12-16 21:56:37 -05:00

refactor: replace deprecated APIs for creating images in the search tests (#2173)

Signed-off-by: Andrei Aaron <aaaron@luxoft.com>
This commit is contained in:
Andrei Aaron 2024-01-22 19:10:34 +02:00 committed by GitHub
parent 7dd1ca353f
commit ed6be0580e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 495 additions and 1159 deletions

View file

@ -21,7 +21,6 @@ import (
"zotregistry.io/zot/pkg/common"
extconf "zotregistry.io/zot/pkg/extensions/config"
. "zotregistry.io/zot/pkg/test/common"
"zotregistry.io/zot/pkg/test/deprecated"
. "zotregistry.io/zot/pkg/test/image-utils"
)
@ -75,38 +74,34 @@ func TestDigestSearchHTTP(t *testing.T) {
layers1 := [][]byte{
{3, 2, 2},
}
image1, err := deprecated.GetImageWithComponents( //nolint: staticcheck
ispec.Image{
image1 := CreateImageWith().
LayerBlobs(layers1).
ImageConfig(ispec.Image{
Created: &createdTime1,
History: []ispec.History{
{
Created: &createdTime1,
},
},
},
layers1,
)
So(err, ShouldBeNil)
}).Build()
const ver001 = "0.0.1"
err = UploadImage(image1, baseURL, "zot-cve-test", ver001)
err := UploadImage(image1, baseURL, "zot-cve-test", ver001)
So(err, ShouldBeNil)
createdTime2 := time.Date(2010, 1, 1, 12, 0, 0, 0, time.UTC)
image2, err := deprecated.GetImageWithComponents( //nolint: staticcheck
ispec.Image{
image2 := CreateImageWith().
LayerBlobs([][]byte{{0, 0, 2}}).
ImageConfig(ispec.Image{
History: []ispec.History{{Created: &createdTime2}},
Platform: ispec.Platform{
Architecture: "amd64",
OS: "linux",
},
},
[][]byte{
{0, 0, 2},
},
)
So(err, ShouldBeNil)
}).Build()
manifestDigest := image2.Digest()
@ -293,13 +288,12 @@ func TestDigestSearchHTTPSubPaths(t *testing.T) {
// shut down server
defer ctrlManager.StopServer()
config, layers, manifest, err := deprecated.GetImageComponents(100) //nolint: staticcheck
image := CreateDefaultImage()
err := UploadImage(image, baseURL, "a/zot-cve-test", "0.0.1")
So(err, ShouldBeNil)
err = UploadImage(Image{Manifest: manifest, Config: config, Layers: layers}, baseURL, "a/zot-cve-test", "0.0.1")
So(err, ShouldBeNil)
err = UploadImage(Image{Manifest: manifest, Config: config, Layers: layers}, baseURL, "a/zot-test", "0.0.1")
err = UploadImage(image, baseURL, "a/zot-test", "0.0.1")
So(err, ShouldBeNil)
resp, err := resty.R().Get(baseURL + "/v2/")

File diff suppressed because it is too large Load diff

View file

@ -20,7 +20,6 @@ import (
extconf "zotregistry.io/zot/pkg/extensions/config"
"zotregistry.io/zot/pkg/log"
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/oci-utils"
)
@ -94,25 +93,16 @@ func TestUserData(t *testing.T) {
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
config, layers, manifest, err := deprecated.GetImageComponents(100) //nolint:staticcheck
So(err, ShouldBeNil)
image := CreateDefaultImage()
err = UploadImageWithBasicAuth(
Image{
Config: config,
Layers: layers,
Manifest: manifest,
}, baseURL, accessibleRepo, tag,
err := UploadImageWithBasicAuth(
image, baseURL, accessibleRepo, tag,
adminUser, adminPassword,
)
So(err, ShouldBeNil)
err = UploadImageWithBasicAuth(
Image{
Config: config,
Layers: layers,
Manifest: manifest,
}, baseURL, forbiddenRepo, tag,
image, baseURL, forbiddenRepo, tag,
adminUser, adminPassword,
)
So(err, ShouldBeNil)
@ -655,15 +645,15 @@ func TestGlobalSearchWithUserPrefFiltering(t *testing.T) {
// ------ Add simple repo
repo := "repo"
img, err := deprecated.GetRandomImage() //nolint:staticcheck
So(err, ShouldBeNil)
err = UploadImageWithBasicAuth(img, baseURL, repo, "tag", simpleUser, simpleUserPassword)
img := CreateRandomImage()
err := UploadImageWithBasicAuth(img, baseURL, repo, "tag", simpleUser, simpleUserPassword)
So(err, ShouldBeNil)
// ------ Add repo and star it
sRepo := "starred-repo"
img, err = deprecated.GetRandomImage() //nolint:staticcheck
So(err, ShouldBeNil)
img = CreateRandomImage()
err = UploadImageWithBasicAuth(img, baseURL, sRepo, "tag", simpleUser, simpleUserPassword)
So(err, ShouldBeNil)
@ -673,8 +663,8 @@ func TestGlobalSearchWithUserPrefFiltering(t *testing.T) {
// ------ Add repo and bookmark it
bRepo := "bookmarked-repo"
img, err = deprecated.GetRandomImage() //nolint:staticcheck
So(err, ShouldBeNil)
img = CreateRandomImage()
err = UploadImageWithBasicAuth(img, baseURL, bRepo, "tag", simpleUser, simpleUserPassword)
So(err, ShouldBeNil)
@ -684,8 +674,8 @@ func TestGlobalSearchWithUserPrefFiltering(t *testing.T) {
// ------ Add repo, star and bookmark it
sbRepo := "starred-bookmarked-repo"
img, err = deprecated.GetRandomImage() //nolint:staticcheck
So(err, ShouldBeNil)
img = CreateRandomImage()
err = UploadImageWithBasicAuth(img, baseURL, sbRepo, "tag", simpleUser, simpleUserPassword)
So(err, ShouldBeNil)
@ -849,9 +839,9 @@ func TestExpandedRepoInfoWithUserPrefs(t *testing.T) {
// ------ Add sbrepo and star/bookmark it
sbrepo := "sbrepo"
img, err := deprecated.GetRandomImage() //nolint:staticcheck
So(err, ShouldBeNil)
err = UploadImageWithBasicAuth(img, baseURL, sbrepo, "tag", simpleUser, simpleUserPassword)
img := CreateRandomImage()
err := UploadImageWithBasicAuth(img, baseURL, sbrepo, "tag", simpleUser, simpleUserPassword)
So(err, ShouldBeNil)
resp, err := simpleUserClient.Put(preferencesBaseURL + PutRepoStarURL(sbrepo))
@ -889,8 +879,8 @@ func TestExpandedRepoInfoWithUserPrefs(t *testing.T) {
// ------ Add srepo and star it
srepo := "srepo"
img, err = deprecated.GetRandomImage() //nolint:staticcheck
So(err, ShouldBeNil)
img = CreateRandomImage()
err = UploadImageWithBasicAuth(img, baseURL, srepo, "tag", simpleUser, simpleUserPassword)
So(err, ShouldBeNil)
@ -924,8 +914,8 @@ func TestExpandedRepoInfoWithUserPrefs(t *testing.T) {
// ------ Add brepo and bookmark it
brepo := "brepo"
img, err = deprecated.GetRandomImage() //nolint:staticcheck
So(err, ShouldBeNil)
img = CreateRandomImage()
err = UploadImageWithBasicAuth(img, baseURL, brepo, "tag", simpleUser, simpleUserPassword)
So(err, ShouldBeNil)
@ -959,8 +949,8 @@ func TestExpandedRepoInfoWithUserPrefs(t *testing.T) {
// ------ Add repo without star/bookmark
repo := "repo"
img, err = deprecated.GetRandomImage() //nolint:staticcheck
So(err, ShouldBeNil)
img = CreateRandomImage()
err = UploadImageWithBasicAuth(img, baseURL, repo, "tag", simpleUser, simpleUserPassword)
So(err, ShouldBeNil)

View file

@ -1,397 +0,0 @@
package deprecated
import (
"crypto/rand"
"encoding/json"
godigest "github.com/opencontainers/go-digest"
"github.com/opencontainers/image-spec/specs-go"
ispec "github.com/opencontainers/image-spec/specs-go/v1"
"zotregistry.io/zot/pkg/test/image-utils"
"zotregistry.io/zot/pkg/test/inject"
)
// Deprecated: Should use the new functions starting with "Create".
func GetImageComponents(layerSize int) (ispec.Image, [][]byte, ispec.Manifest, error) {
config := ispec.Image{
Platform: ispec.Platform{
Architecture: "amd64",
OS: "linux",
},
RootFS: ispec.RootFS{
Type: "layers",
DiffIDs: []godigest.Digest{},
},
Author: "ZotUser",
}
configBlob, err := json.Marshal(config)
if err = inject.Error(err); err != nil {
return ispec.Image{}, [][]byte{}, ispec.Manifest{}, err
}
configDigest := godigest.FromBytes(configBlob)
layers := [][]byte{
make([]byte, layerSize),
}
schemaVersion := 2
manifest := ispec.Manifest{
MediaType: ispec.MediaTypeImageManifest,
Versioned: specs.Versioned{
SchemaVersion: schemaVersion,
},
Config: ispec.Descriptor{
MediaType: "application/vnd.oci.image.config.v1+json",
Digest: configDigest,
Size: int64(len(configBlob)),
},
Layers: []ispec.Descriptor{
{
MediaType: "application/vnd.oci.image.layer.v1.tar",
Digest: godigest.FromBytes(layers[0]),
Size: int64(len(layers[0])),
},
},
}
return config, layers, manifest, nil
}
// Deprecated: Should use the new functions starting with "Create".
func GetRandomImageComponents(layerSize int) (ispec.Image, [][]byte, ispec.Manifest, error) {
config := ispec.Image{
Platform: ispec.Platform{
Architecture: "amd64",
OS: "linux",
},
RootFS: ispec.RootFS{
Type: "layers",
DiffIDs: []godigest.Digest{},
},
Author: "ZotUser",
}
configBlob, err := json.Marshal(config)
if err = inject.Error(err); err != nil {
return ispec.Image{}, [][]byte{}, ispec.Manifest{}, err
}
configDigest := godigest.FromBytes(configBlob)
layers := [][]byte{
GetRandomLayer(layerSize),
}
schemaVersion := 2
manifest := ispec.Manifest{
MediaType: ispec.MediaTypeImageManifest,
Versioned: specs.Versioned{
SchemaVersion: schemaVersion,
},
Config: ispec.Descriptor{
MediaType: "application/vnd.oci.image.config.v1+json",
Digest: configDigest,
Size: int64(len(configBlob)),
},
Layers: []ispec.Descriptor{
{
MediaType: "application/vnd.oci.image.layer.v1.tar",
Digest: godigest.FromBytes(layers[0]),
Size: int64(len(layers[0])),
},
},
}
return config, layers, manifest, nil
}
func GetRandomLayer(size int) []byte {
layer := make([]byte, size)
_, err := rand.Read(layer)
if err != nil {
return layer
}
return layer
}
// Deprecated: Should use the new functions starting with "Create".
func GetRandomImage() (image.Image, error) {
const layerSize = 20
config, layers, manifest, err := GetRandomImageComponents(layerSize)
if err != nil {
return image.Image{}, err
}
return image.Image{
Manifest: manifest,
Layers: layers,
Config: config,
}, nil
}
// Deprecated: Should use the new functions starting with "Create".
func GetImageComponentsWithConfig(conf ispec.Image) (ispec.Image, [][]byte, ispec.Manifest, error) {
configBlob, err := json.Marshal(conf)
if err = inject.Error(err); err != nil {
return ispec.Image{}, [][]byte{}, ispec.Manifest{}, err
}
configDigest := godigest.FromBytes(configBlob)
layerSize := 100
layer := make([]byte, layerSize)
_, err = rand.Read(layer)
if err != nil {
return ispec.Image{}, [][]byte{}, ispec.Manifest{}, err
}
layers := [][]byte{
layer,
}
schemaVersion := 2
manifest := ispec.Manifest{
MediaType: ispec.MediaTypeImageManifest,
Versioned: specs.Versioned{
SchemaVersion: schemaVersion,
},
Config: ispec.Descriptor{
MediaType: "application/vnd.oci.image.config.v1+json",
Digest: configDigest,
Size: int64(len(configBlob)),
},
Layers: []ispec.Descriptor{
{
MediaType: "application/vnd.oci.image.layer.v1.tar",
Digest: godigest.FromBytes(layers[0]),
Size: int64(len(layers[0])),
},
},
}
return conf, layers, manifest, nil
}
// Deprecated: Should use the new functions starting with "Create".
func GetImageWithConfig(conf ispec.Image) (image.Image, error) {
config, layers, manifest, err := GetImageComponentsWithConfig(conf)
if err != nil {
return image.Image{}, err
}
return image.Image{
Manifest: manifest,
Config: config,
Layers: layers,
}, nil
}
// Deprecated: Should use the new functions starting with "Create".
func GetImageWithComponents(config ispec.Image, layers [][]byte) (image.Image, error) {
configBlob, err := json.Marshal(config)
if err != nil {
return image.Image{}, err
}
manifestLayers := make([]ispec.Descriptor, 0, len(layers))
for _, layer := range layers {
manifestLayers = append(manifestLayers, ispec.Descriptor{
MediaType: "application/vnd.oci.image.layer.v1.tar",
Digest: godigest.FromBytes(layer),
Size: int64(len(layer)),
})
}
const schemaVersion = 2
manifest := ispec.Manifest{
MediaType: ispec.MediaTypeImageManifest,
Versioned: specs.Versioned{
SchemaVersion: schemaVersion,
},
Config: ispec.Descriptor{
MediaType: "application/vnd.oci.image.config.v1+json",
Digest: godigest.FromBytes(configBlob),
Size: int64(len(configBlob)),
},
Layers: manifestLayers,
}
return image.Image{
Manifest: manifest,
Config: config,
Layers: layers,
}, nil
}
// Deprecated: Should use the new functions starting with "Create".
func GetImageWithSubject(subjectDigest godigest.Digest, mediaType string) (image.Image, error) {
num := 100
conf, layers, manifest, err := GetRandomImageComponents(num)
if err != nil {
return image.Image{}, err
}
manifest.Subject = &ispec.Descriptor{
Digest: subjectDigest,
MediaType: mediaType,
}
return image.Image{
Manifest: manifest,
Config: conf,
Layers: layers,
}, nil
}
// Deprecated: Should use the new functions starting with "Create".
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.Image{}, err
}
image1, err := GetImageWithComponents(
ispec.Image{
Platform: ispec.Platform{
OS: "linux",
Architecture: "amd64",
},
},
[][]byte{
randomLayer1,
})
if err != nil {
return ispec.Index{}, []image.Image{}, err
}
randomLayer2 := make([]byte, layerSize)
_, err = rand.Read(randomLayer2)
if err != nil {
return ispec.Index{}, []image.Image{}, err
}
image2, err := GetImageWithComponents(
ispec.Image{
Platform: ispec.Platform{
OS: "linux",
Architecture: "386",
},
},
[][]byte{
randomLayer2,
})
if err != nil {
return ispec.Index{}, []image.Image{}, err
}
randomLayer3 := make([]byte, layerSize)
_, err = rand.Read(randomLayer3)
if err != nil {
return ispec.Index{}, []image.Image{}, err
}
image3, err := GetImageWithComponents(
ispec.Image{
Platform: ispec.Platform{
OS: "windows",
Architecture: "amd64",
},
},
[][]byte{
randomLayer3,
})
if err != nil {
return ispec.Index{}, []image.Image{}, err
}
index := ispec.Index{
MediaType: ispec.MediaTypeImageIndex,
Manifests: []ispec.Descriptor{
{
MediaType: ispec.MediaTypeImageManifest,
Digest: getManifestDigest(image1.Manifest),
Size: getManifestSize(image1.Manifest),
},
{
MediaType: ispec.MediaTypeImageManifest,
Digest: getManifestDigest(image2.Manifest),
Size: getManifestSize(image2.Manifest),
},
{
MediaType: ispec.MediaTypeImageManifest,
Digest: getManifestDigest(image3.Manifest),
Size: getManifestSize(image3.Manifest),
},
},
}
return index, []image.Image{image1, image2, image3}, nil
}
// Deprecated: Should use the new functions starting with "Create".
func GetRandomMultiarchImage(reference string) (image.MultiarchImage, error) {
index, images, err := GetRandomMultiarchImageComponents()
if err != nil {
return image.MultiarchImage{}, err
}
index.SchemaVersion = 2
return image.MultiarchImage{
Index: index, Images: images,
}, err
}
// Deprecated: Should use the new functions starting with "Create".
func GetMultiarchImageForImages(images []image.Image) image.MultiarchImage {
var index ispec.Index
for _, image := range images {
index.Manifests = append(index.Manifests, ispec.Descriptor{
MediaType: ispec.MediaTypeImageManifest,
Digest: getManifestDigest(image.Manifest),
Size: getManifestSize(image.Manifest),
})
}
index.SchemaVersion = 2
return image.MultiarchImage{Index: index, Images: images}
}
func getManifestSize(manifest ispec.Manifest) int64 {
manifestBlob, err := json.Marshal(manifest)
if err != nil {
return 0
}
return int64(len(manifestBlob))
}
func getManifestDigest(manifest ispec.Manifest) godigest.Digest {
manifestBlob, err := json.Marshal(manifest)
if err != nil {
return ""
}
return godigest.FromBytes(manifestBlob)
}

View file

@ -1,45 +0,0 @@
package deprecated_test
import (
"testing"
ispec "github.com/opencontainers/image-spec/specs-go/v1"
. "github.com/smartystreets/goconvey/convey"
"zotregistry.io/zot/pkg/test/deprecated"
"zotregistry.io/zot/pkg/test/inject"
)
func TestGetImageComponents(t *testing.T) {
Convey("Inject failures for unreachable lines", t, func() {
injected := inject.InjectFailure(0)
if injected {
_, _, _, err := deprecated.GetImageComponents(100) //nolint:staticcheck
So(err, ShouldNotBeNil)
}
})
Convey("finishes successfully", t, func() {
_, _, _, err := deprecated.GetImageComponents(100) //nolint:staticcheck
So(err, ShouldBeNil)
})
}
func TestGetRandomImageComponents(t *testing.T) {
Convey("Inject failures for unreachable lines", t, func() {
injected := inject.InjectFailure(0)
if injected {
_, _, _, err := deprecated.GetRandomImageComponents(100) //nolint:staticcheck
So(err, ShouldNotBeNil)
}
})
}
func TestGetImageComponentsWithConfig(t *testing.T) {
Convey("Inject failures for unreachable lines", t, func() {
injected := inject.InjectFailure(0)
if injected {
_, _, _, err := deprecated.GetImageComponentsWithConfig(ispec.Image{}) //nolint:staticcheck
So(err, ShouldNotBeNil)
}
})
}