Skip to content

Commit

Permalink
refactor(umoci): remove test code reling on the umoci library
Browse files Browse the repository at this point in the history
Since we started relying on images in test/data, and we create
our own images using go code we can obtain digests by other means.

Signed-off-by: Andrei Aaron <[email protected]>
  • Loading branch information
andaaron committed Sep 26, 2023
1 parent 9096031 commit dee67d1
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 158 deletions.
10 changes: 5 additions & 5 deletions pkg/api/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4350,7 +4350,7 @@ func TestInvalidCases(t *testing.T) {
panic(err)
}

digest := test.GetTestBlobDigest("zot-cve-test", "config").String()
digest := godigest.FromString("dummy").String()
name := "zot-c-test"

client := resty.New()
Expand Down Expand Up @@ -4449,7 +4449,8 @@ func TestCrossRepoMount(t *testing.T) {
ctlr.Config.Storage.RemoteCache = false
ctlr.Config.Storage.Dedupe = false

err := test.WriteImageToFileSystem(CreateDefaultImage(), "zot-cve-test", "test", storage.StoreController{
image := CreateDefaultImage()
err := test.WriteImageToFileSystem(image, "zot-cve-test", "test", storage.StoreController{
DefaultStore: test.GetDefaultImageStore(dir, ctlr.Log),
})
So(err, ShouldBeNil)
Expand All @@ -4459,8 +4460,7 @@ func TestCrossRepoMount(t *testing.T) {

params := make(map[string]string)

var manifestDigest godigest.Digest
manifestDigest, _, _ = test.GetOciLayoutDigests(path.Join(dir, "zot-cve-test"))
manifestDigest := image.ManifestDescriptor.Digest

dgst := manifestDigest
name := "zot-cve-test"
Expand All @@ -4487,7 +4487,7 @@ func TestCrossRepoMount(t *testing.T) {
baseURL, constants.RoutePrefix, constants.Blobs, constants.Uploads))

incorrectParams := make(map[string]string)
incorrectParams["mount"] = test.GetTestBlobDigest("zot-cve-test", "manifest").String()
incorrectParams["mount"] = godigest.FromString("dummy").String()
incorrectParams["from"] = "zot-x-test"

postResponse, err = client.R().
Expand Down
36 changes: 20 additions & 16 deletions pkg/extensions/search/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,19 +381,21 @@ func TestRepoListWithNewestImage(t *testing.T) {
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()

config, layers, manifest, err := GetImageComponents(100)
config, layers, _, err := GetImageComponents(100)
So(err, ShouldBeNil)

err = UploadImage(Image{Manifest: manifest, Config: config, Layers: layers}, baseURL, "zot-cve-test", "0.0.1")
uploadedImage := CreateImageWith().LayerBlobs(layers).ImageConfig(config).Build()

err = UploadImage(uploadedImage, baseURL, "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")
err = UploadImage(uploadedImage, baseURL, "a/zot-cve-test", "0.0.1")
So(err, ShouldBeNil)

err = UploadImage(Image{Manifest: manifest, Config: config, Layers: layers}, baseURL, "zot-test", "0.0.1")
err = UploadImage(uploadedImage, baseURL, "zot-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(uploadedImage, baseURL, "a/zot-test", "0.0.1")
So(err, ShouldBeNil)

resp, err := resty.R().Get(baseURL + "/v2/")
Expand Down Expand Up @@ -590,9 +592,8 @@ func TestRepoListWithNewestImage(t *testing.T) {
panic(err)
}

var manifestDigest godigest.Digest
var configDigest godigest.Digest
manifestDigest, configDigest, _ = GetOciLayoutDigests(path.Join(subRootDir, "a/zot-test"))
manifestDigest := uploadedImage.ManifestDescriptor.Digest
configDigest := uploadedImage.ConfigDescriptor.Digest

// Delete config blob and try.
err = os.Remove(path.Join(subRootDir, "a/zot-test/blobs/sha256", configDigest.Encoded()))
Expand Down Expand Up @@ -1269,22 +1270,25 @@ func TestExpandedRepoInfo(t *testing.T) {
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()

config, layers, manifest, err := GetImageComponents(100)
config, layers, _, err := GetImageComponents(100)
So(err, ShouldBeNil)

manifest.Annotations = make(map[string]string)
manifest.Annotations["org.opencontainers.image.vendor"] = "zot"
annotations := make(map[string]string)
annotations["org.opencontainers.image.vendor"] = "zot"

err = UploadImage(Image{Manifest: manifest, Config: config, Layers: layers}, baseURL, "zot-cve-test", "0.0.1")
uploadedImage := CreateImageWith().LayerBlobs(layers).ImageConfig(config).
Annotations(annotations).Build()

err = UploadImage(uploadedImage, baseURL, "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")
err = UploadImage(uploadedImage, baseURL, "a/zot-cve-test", "0.0.1")
So(err, ShouldBeNil)

err = UploadImage(Image{Manifest: manifest, Config: config, Layers: layers}, baseURL, "zot-test", "0.0.1")
err = UploadImage(uploadedImage, baseURL, "zot-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(uploadedImage, baseURL, "a/zot-test", "0.0.1")
So(err, ShouldBeNil)

log := log.NewLogger("debug", "")
Expand Down Expand Up @@ -1456,7 +1460,7 @@ func TestExpandedRepoInfo(t *testing.T) {
So(found, ShouldEqual, true)

var manifestDigest godigest.Digest
manifestDigest, _, _ = GetOciLayoutDigests(path.Join(rootDir, "zot-test"))
manifestDigest = uploadedImage.ManifestDescriptor.Digest

err = os.Remove(path.Join(rootDir, "zot-test/blobs/sha256", manifestDigest.Encoded()))
So(err, ShouldBeNil)
Expand Down
75 changes: 0 additions & 75 deletions pkg/test/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (
godigest "github.com/opencontainers/go-digest"
"github.com/opencontainers/image-spec/specs-go"
ispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/opencontainers/umoci"
"github.com/phayes/freeport"
"github.com/project-zot/mockoidc"
"github.com/sigstore/cosign/v2/cmd/cosign/cli/generate"
Expand Down Expand Up @@ -72,29 +71,6 @@ var (

var NotationPathLock = new(sync.Mutex) //nolint: gochecknoglobals

// which: manifest, config, layer
func GetTestBlobDigest(image, which string) godigest.Digest {
prePath := "../test/data"

for _, err := os.Stat(prePath); err != nil; _, err = os.Stat(prePath) {
prePath = "../" + prePath
}

imgPath := path.Join(prePath, image)
manifest, config, layer := GetOciLayoutDigests(imgPath)

switch which {
case "manifest":
return manifest
case "config":
return config
case "layer":
return layer
}

return ""
}

func GetFreePort() string {
port, err := freeport.GetFreePort()
if err != nil {
Expand Down Expand Up @@ -434,57 +410,6 @@ func GetRandomImageConfig() ([]byte, godigest.Digest) {
return configBlobContent, configBlobDigestRaw
}

func GetOciLayoutDigests(imagePath string) (godigest.Digest, godigest.Digest, godigest.Digest) {
var (
manifestDigest godigest.Digest
configDigest godigest.Digest
layerDigest godigest.Digest
)

oci, err := umoci.OpenLayout(imagePath)
if err != nil {
panic(fmt.Errorf("error opening layout at '%s' : %w", imagePath, err))
}

defer oci.Close()

ctxUmoci := context.Background()

index, err := oci.GetIndex(ctxUmoci)
if err != nil {
panic(err)
}

for _, manifest := range index.Manifests {
manifestDigest = manifest.Digest

manifestBlob, err := oci.GetBlob(ctxUmoci, manifest.Digest)
if err != nil {
panic(err)
}

manifestBuf, err := io.ReadAll(manifestBlob)
if err != nil {
panic(err)
}

var manifest ispec.Manifest

err = json.Unmarshal(manifestBuf, &manifest)
if err != nil {
panic(err)
}

configDigest = manifest.Config.Digest

for _, layer := range manifest.Layers {
layerDigest = layer.Digest
}
}

return manifestDigest, configDigest, layerDigest
}

// Deprecated: Should use the new functions starting with "Create".
func GetImageComponents(layerSize int) (ispec.Image, [][]byte, ispec.Manifest, error) {
config := ispec.Image{
Expand Down
59 changes: 0 additions & 59 deletions pkg/test/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (

"zotregistry.io/zot/pkg/api"
"zotregistry.io/zot/pkg/api/config"
"zotregistry.io/zot/pkg/log"
"zotregistry.io/zot/pkg/storage"
"zotregistry.io/zot/pkg/test"
. "zotregistry.io/zot/pkg/test/image-utils"
Expand Down Expand Up @@ -125,64 +124,6 @@ func TestCopyFiles(t *testing.T) {
})
}

func TestGetOciLayoutDigests(t *testing.T) {
dir := t.TempDir()

Convey("image path is wrong", t, func() {
So(func() { _, _, _ = test.GetOciLayoutDigests("inexistent-image") }, ShouldPanic)
})

Convey("no permissions when getting index", t, func() {
storageCtlr := test.GetDefaultStoreController(dir, log.NewLogger("debug", ""))
image := CreateDefaultImage()

err := test.WriteImageToFileSystem(image, "test-index", "0.0.1", storageCtlr)
So(err, ShouldBeNil)

err = os.Chmod(path.Join(dir, "test-index", "index.json"), 0o000)
if err != nil {
panic(err)
}

So(func() { _, _, _ = test.GetOciLayoutDigests(path.Join(dir, "test-index")) }, ShouldPanic)

err = os.Chmod(path.Join(dir, "test-index", "index.json"), 0o755)
if err != nil {
panic(err)
}
})

Convey("can't access manifest digest", t, func() {
storageCtlr := test.GetDefaultStoreController(dir, log.NewLogger("debug", ""))
image := CreateDefaultImage()

err := test.WriteImageToFileSystem(image, "test-manifest", "0.0.1", storageCtlr)
So(err, ShouldBeNil)

buf, err := os.ReadFile(path.Join(dir, "test-manifest", "index.json"))
if err != nil {
panic(err)
}

var index ispec.Index
if err := json.Unmarshal(buf, &index); err != nil {
panic(err)
}

err = os.Chmod(path.Join(dir, "test-manifest", "blobs/sha256", index.Manifests[0].Digest.Encoded()), 0o000)
if err != nil {
panic(err)
}

So(func() { _, _, _ = test.GetOciLayoutDigests(path.Join(dir, "test-manifest")) }, ShouldPanic)

err = os.Chmod(path.Join(dir, "test-manifest", "blobs/sha256", index.Manifests[0].Digest.Encoded()), 0o755)
if err != nil {
panic(err)
}
})
}

func TestGetImageComponents(t *testing.T) {
Convey("Inject failures for unreachable lines", t, func() {
injected := inject.InjectFailure(0)
Expand Down
7 changes: 4 additions & 3 deletions pkg/test/oci-layout/oci_layout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ import (
var ErrTestError = fmt.Errorf("testError")

func TestBaseOciLayoutUtils(t *testing.T) {
manifestDigest := GetTestBlobDigest("zot-test", "config").String()

Convey("GetImageManifestSize fail", t, func() {
mockStoreController := mocks.MockedImageStore{
GetBlobContentFn: func(repo string, digest godigest.Digest) ([]byte, error) {
Expand Down Expand Up @@ -64,6 +62,9 @@ func TestBaseOciLayoutUtils(t *testing.T) {
})

Convey("GetImageConfigSize: config GetBlobContent fail", t, func() {
image := CreateRandomImage()
manifestDigest := image.ConfigDescriptor.Digest.String()

mockStoreController := mocks.MockedImageStore{
GetBlobContentFn: func(repo string, digest godigest.Digest) ([]byte, error) {
if digest.String() == manifestDigest {
Expand All @@ -83,7 +84,7 @@ func TestBaseOciLayoutUtils(t *testing.T) {
"layers": [
{
"mediaType": "application/vnd.oci.image.layer.v1.tar+gzip",
"digest": "` + GetTestBlobDigest("zot-test", "layer").String() + `",
"digest": "` + image.Manifest.Layers[0].Digest.String() + `",
"size": 76097157
}
]
Expand Down

0 comments on commit dee67d1

Please sign in to comment.