Skip to content

Commit

Permalink
refactor(test): move image utils for tests in a separate module (#1789)
Browse files Browse the repository at this point in the history
Signed-off-by: Laurentiu Niculae <[email protected]>
  • Loading branch information
laurentiuNiculae authored Sep 15, 2023
1 parent 14206dd commit 8e18917
Show file tree
Hide file tree
Showing 36 changed files with 1,415 additions and 1,564 deletions.
178 changes: 96 additions & 82 deletions pkg/api/controller_test.go

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions pkg/cli/cve_cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -481,7 +482,7 @@ func TestNegativeServerResponse(t *testing.T) {
So(err, ShouldBeNil)

err = test.WriteImageToFileSystem(
test.Image{
Image{
Manifest: manifest,
Layers: layers,
Config: config,
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)

Expand Down
77 changes: 39 additions & 38 deletions pkg/cli/image_cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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}]}`,
Expand Down Expand Up @@ -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}]}`,
Expand Down Expand Up @@ -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}]}`,
Expand Down Expand Up @@ -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}]}`,
Expand Down Expand Up @@ -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}]}`,
Expand Down Expand Up @@ -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}]}`,
Expand All @@ -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}]}`,
Expand All @@ -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}]}`,
Expand All @@ -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}]}`,
Expand All @@ -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}]}`,
Expand All @@ -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,
Expand All @@ -1995,7 +1996,7 @@ func uploadTestMultiarch(baseURL string) {
// ------ Define Image2
layer21 := []byte{21, 22, 23, 24}

image2 := test.CreateImageWith().
image2 := CreateImageWith().
LayerBlobs([][]byte{
layer21,
}).
Expand All @@ -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)
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/cli/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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}]}`,
Expand Down
Loading

0 comments on commit 8e18917

Please sign in to comment.