From c6ffbce6cf771e12783c7fc3b39993905a6b180d Mon Sep 17 00:00:00 2001 From: Catalin-George Hofnar Date: Wed, 26 Oct 2022 19:14:16 +0300 Subject: [PATCH] refactor(tests): removed globals from digest test, removed some more hardcoded digests (#923) Signed-off-by: Catalin Hofnar --- pkg/api/controller_test.go | 2 +- pkg/cli/cve_cmd_test.go | 12 ++-- pkg/cli/image_cmd_test.go | 2 +- pkg/extensions/search/common/common_test.go | 2 +- pkg/extensions/search/digest/digest_test.go | 76 ++++++++------------- 5 files changed, 39 insertions(+), 55 deletions(-) diff --git a/pkg/api/controller_test.go b/pkg/api/controller_test.go index 1ed78d54..1b90a7e9 100644 --- a/pkg/api/controller_test.go +++ b/pkg/api/controller_test.go @@ -3215,7 +3215,7 @@ func TestCrossRepoMount(t *testing.T) { baseURL, constants.RoutePrefix, constants.Blobs, constants.Uploads)) incorrectParams := make(map[string]string) - incorrectParams["mount"] = "sha256:63a795ca90aa6e7dda60941e826810a4cd0a2e73ea02bf458241df2a5c973e29" + incorrectParams["mount"] = test.GetTestBlobDigest("zot-cve-test", "manifest").String() incorrectParams["from"] = "zot-x-test" postResponse, err = client.R(). diff --git a/pkg/cli/cve_cmd_test.go b/pkg/cli/cve_cmd_test.go index eaad2a95..f478f9f8 100644 --- a/pkg/cli/cve_cmd_test.go +++ b/pkg/cli/cve_cmd_test.go @@ -481,7 +481,8 @@ func TestServerCVEResponseGQL(t *testing.T) { str := space.ReplaceAllString(buff.String(), " ") str = strings.TrimSpace(str) So(err, ShouldBeNil) - So(str, ShouldEqual, "IMAGE NAME TAG DIGEST SIGNED SIZE zot-cve-test 0.0.1 63a795ca false 75MB") + So(str, ShouldEqual, "IMAGE NAME TAG DIGEST SIGNED SIZE zot-cve-test 0.0.1 "+ + test.GetTestBlobDigest("zot-cve-test", "manifest").Encoded()[:8]+" false 75MB") Convey("invalid CVE ID", func() { args := []string{"cvetest", "--cve-id", "invalid"} @@ -596,7 +597,8 @@ func TestServerCVEResponseGQL(t *testing.T) { space := regexp.MustCompile(`\s+`) str := space.ReplaceAllString(buff.String(), " ") So(err, ShouldBeNil) - So(strings.TrimSpace(str), ShouldEqual, "IMAGE NAME TAG DIGEST SIGNED SIZE zot-cve-test 0.0.1 63a795ca false 75MB") + So(strings.TrimSpace(str), ShouldEqual, "IMAGE NAME TAG DIGEST SIGNED SIZE zot-cve-test 0.0.1 "+ + test.GetTestBlobDigest("zot-cve-test", "manifest").Encoded()[:8]+" false 75MB") Convey("invalid name and CVE ID", func() { args := []string{"cvetest", "--image", "test", "--cve-id", "CVE-20807"} @@ -905,7 +907,8 @@ func TestServerCVEResponse(t *testing.T) { str := space.ReplaceAllString(buff.String(), " ") str = strings.TrimSpace(str) So(err, ShouldBeNil) - So(str, ShouldEqual, "IMAGE NAME TAG DIGEST SIGNED SIZE zot-cve-test 0.0.1 63a795ca false 75MB") + So(str, ShouldEqual, "IMAGE NAME TAG DIGEST SIGNED SIZE zot-cve-test 0.0.1 "+ + test.GetTestBlobDigest("zot-cve-test", "manifest").Encoded()[:8]+" false 75MB") Convey("invalid CVE ID", func() { args := []string{"cvetest", "--cve-id", "invalid"} configPath := makeConfigFile(fmt.Sprintf(`{"configs":[{"_name":"cvetest","url":"%s","showspinner":false}]}`, url)) @@ -987,7 +990,8 @@ func TestServerCVEResponse(t *testing.T) { space := regexp.MustCompile(`\s+`) str := space.ReplaceAllString(buff.String(), " ") So(err, ShouldBeNil) - So(strings.TrimSpace(str), ShouldEqual, "IMAGE NAME TAG DIGEST SIGNED SIZE zot-cve-test 0.0.1 63a795ca false 75MB") + So(strings.TrimSpace(str), ShouldEqual, "IMAGE NAME TAG DIGEST SIGNED SIZE zot-cve-test 0.0.1 "+ + test.GetTestBlobDigest("zot-cve-test", "manifest").Encoded()[:8]+" false 75MB") Convey("invalid name and CVE ID", func() { args := []string{"cvetest", "--image", "test", "--cve-id", "CVE-20807"} configPath := makeConfigFile(fmt.Sprintf(`{"configs":[{"_name":"cvetest","url":"%s","showspinner":false}]}`, url)) diff --git a/pkg/cli/image_cmd_test.go b/pkg/cli/image_cmd_test.go index 7c9b3635..d20ca648 100644 --- a/pkg/cli/image_cmd_test.go +++ b/pkg/cli/image_cmd_test.go @@ -1377,7 +1377,7 @@ func TestServerResponseGQLWithoutPermissions(t *testing.T) { }) Convey("Test image by digest", t, func() { - args := []string{"imagetest", "--digest", "2bacca16"} + args := []string{"imagetest", "--digest", test.GetTestBlobDigest("zot-test", "manifest").Encoded()} configPath := makeConfigFile(fmt.Sprintf(`{"configs":[{"_name":"imagetest","url":"%s","showspinner":false}]}`, url)) defer os.Remove(configPath) cmd := NewImageCommand(new(searchService)) diff --git a/pkg/extensions/search/common/common_test.go b/pkg/extensions/search/common/common_test.go index 2c31b2d8..49629321 100644 --- a/pkg/extensions/search/common/common_test.go +++ b/pkg/extensions/search/common/common_test.go @@ -2750,7 +2750,7 @@ func TestBaseOciLayoutUtils(t *testing.T) { "layers": [ { "mediaType": "application/vnd.oci.image.layer.v1.tar+gzip", - "digest": "sha256:2d473b07cdd5f0912cd6f1a703352c82b512407db6b05b43f2553732b55df3bc", + "digest": "` + GetTestBlobDigest("zot-test", "layer").String() + `", "size": 76097157 } ] diff --git a/pkg/extensions/search/digest/digest_test.go b/pkg/extensions/search/digest/digest_test.go index 437cd82c..fcce1392 100644 --- a/pkg/extensions/search/digest/digest_test.go +++ b/pkg/extensions/search/digest/digest_test.go @@ -27,13 +27,6 @@ import ( . "zotregistry.io/zot/pkg/test" ) -//nolint:gochecknoglobals -var ( - digestInfo *digestinfo.DigestInfo - rootDir string - subRootDir string -) - type ImgResponseForDigest struct { ImgListForDigest ImgListForDigest `json:"data"` Errors []ErrorGQL `json:"errors"` @@ -58,26 +51,14 @@ type ErrorGQL struct { Path []string `json:"path"` } -func init() { - if err := testSetup(); err != nil { - panic(err) - } -} +func testSetup(t *testing.T) (string, string, *digestinfo.DigestInfo) { + t.Helper() + dir := t.TempDir() + subDir := t.TempDir() -func testSetup() error { - dir, err := os.MkdirTemp("", "digest_test") - if err != nil { - return err - } + rootDir := dir - subDir, err := os.MkdirTemp("", "sub_digest_test") - if err != nil { - return err - } - - rootDir = dir - - subRootDir = subDir + subRootDir := subDir // Test images used/copied: // IMAGE NAME TAG DIGEST CONFIG LAYERS SIZE @@ -86,19 +67,19 @@ func testSetup() error { // zot-cve-test 0.0.1 63a795ca 8dd57e17 75MB // 7a0437f0 75MB - err = os.Mkdir(subDir+"/a", 0o700) + err := os.Mkdir(subDir+"/a", 0o700) if err != nil { - return err + panic(err) } err = CopyFiles("../../../../test/data", rootDir) if err != nil { - return err + panic(err) } err = CopyFiles("../../../../test/data", subDir+"/a/") if err != nil { - return err + panic(err) } log := log.NewLogger("debug", "") @@ -107,41 +88,36 @@ func testSetup() error { DefaultStore: local.NewImageStore(rootDir, false, storage.DefaultGCDelay, false, false, log, metrics, nil), } - digestInfo = digestinfo.NewDigestInfo(storeController, log) + digestInfo := digestinfo.NewDigestInfo(storeController, log) - return nil + return rootDir, subRootDir, digestInfo } func TestDigestInfo(t *testing.T) { Convey("Test image tag", t, func() { - log := log.NewLogger("debug", "") - metrics := monitoring.NewMetricsServer(false, log) - storeController := storage.StoreController{ - DefaultStore: local.NewImageStore(rootDir, false, storage.DefaultGCDelay, false, false, log, metrics, nil), - } - - digestInfo = digestinfo.NewDigestInfo(storeController, log) + _, _, digestInfo := testSetup(t) // Search by manifest digest - imageTags, err := digestInfo.GetImageTagsByDigest("zot-cve-test", "63a795ca") + imageTags, err := digestInfo.GetImageTagsByDigest("zot-cve-test", + GetTestBlobDigest("zot-cve-test", "manifest").Encoded()) So(err, ShouldBeNil) So(len(imageTags), ShouldEqual, 1) So(imageTags[0].Tag, ShouldEqual, "0.0.1") // Search by config digest - imageTags, err = digestInfo.GetImageTagsByDigest("zot-test", "adf3bb6c") + imageTags, err = digestInfo.GetImageTagsByDigest("zot-test", GetTestBlobDigest("zot-test", "config").Encoded()) So(err, ShouldBeNil) So(len(imageTags), ShouldEqual, 1) So(imageTags[0].Tag, ShouldEqual, "0.0.1") // Search by layer digest - imageTags, err = digestInfo.GetImageTagsByDigest("zot-cve-test", "7a0437f0") + imageTags, err = digestInfo.GetImageTagsByDigest("zot-cve-test", GetTestBlobDigest("zot-cve-test", "layer").Encoded()) So(err, ShouldBeNil) So(len(imageTags), ShouldEqual, 1) So(imageTags[0].Tag, ShouldEqual, "0.0.1") // Search by non-existent image - imageTags, err = digestInfo.GetImageTagsByDigest("zot-tes", "63a795ca") + imageTags, err = digestInfo.GetImageTagsByDigest("zot-tes", GetTestBlobDigest("zot-test", "manifest").Encoded()) So(err, ShouldNotBeNil) So(len(imageTags), ShouldEqual, 0) @@ -154,6 +130,8 @@ func TestDigestInfo(t *testing.T) { func TestDigestSearchHTTP(t *testing.T) { Convey("Test image search by digest scanning", t, func() { + rootDir, _, _ := testSetup(t) + port := GetFreePort() baseURL := GetBaseURL(port) conf := config.New() @@ -215,9 +193,9 @@ func TestDigestSearchHTTP(t *testing.T) { So(responseStruct.ImgListForDigest.Images[0].Tag, ShouldEqual, "0.0.1") // Call should return {"data":{"ImageListForDigest":[{"Name":"zot-test","Tags":["0.0.1"]}]}} - // "2bacca16" should match the manifest of 1 image + // GetTestBlobDigest("zot-test", "manifest").Encoded() should match the manifest of 1 image - gqlQuery := url.QueryEscape(`{ImageListForDigest(id:"2bacca16") + gqlQuery := url.QueryEscape(`{ImageListForDigest(id:"` + GetTestBlobDigest("zot-test", "manifest").Encoded() + `") {RepoName Tag Digest ConfigDigest Size Layers { Digest }}}`) targetURL := baseURL + constants.FullSearchPrefix + `?query=` + gqlQuery @@ -234,8 +212,8 @@ func TestDigestSearchHTTP(t *testing.T) { So(responseStruct.ImgListForDigest.Images[0].RepoName, ShouldEqual, "zot-test") So(responseStruct.ImgListForDigest.Images[0].Tag, ShouldEqual, "0.0.1") - // "adf3bb6c" should match the config of 1 image. - gqlQuery = url.QueryEscape(`{ImageListForDigest(id:"adf3bb6c") + // GetTestBlobDigest("zot-test", "config").Encoded() should match the config of 1 image. + gqlQuery = url.QueryEscape(`{ImageListForDigest(id:"` + GetTestBlobDigest("zot-test", "config").Encoded() + `") {RepoName Tag Digest ConfigDigest Size Layers { Digest }}}`) targetURL = baseURL + constants.FullSearchPrefix + `?query=` + gqlQuery @@ -253,8 +231,8 @@ func TestDigestSearchHTTP(t *testing.T) { So(responseStruct.ImgListForDigest.Images[0].Tag, ShouldEqual, "0.0.1") // Call should return {"data":{"ImageListForDigest":[{"Name":"zot-cve-test","Tags":["0.0.1"]}]}} - // "7a0437f0" should match the layer of 1 image - gqlQuery = url.QueryEscape(`{ImageListForDigest(id:"7a0437f0") + // GetTestBlobDigest("zot-cve-test", "layer").Encoded() should match the layer of 1 image + gqlQuery = url.QueryEscape(`{ImageListForDigest(id:"` + GetTestBlobDigest("zot-cve-test", "layer").Encoded() + `") {RepoName Tag Digest ConfigDigest Size Layers { Digest }}}`) targetURL = baseURL + constants.FullSearchPrefix + `?query=` + gqlQuery @@ -306,6 +284,8 @@ func TestDigestSearchHTTP(t *testing.T) { func TestDigestSearchHTTPSubPaths(t *testing.T) { Convey("Test image search by digest scanning using storage subpaths", t, func() { + _, subRootDir, _ := testSetup(t) + port := GetFreePort() baseURL := GetBaseURL(port) conf := config.New()