refactor(pkg/test): split logic in pkg/test/common.go into multiple packages (#1861)

Which could be imported independently. See more details:
1. "zotregistry.io/zot/pkg/test/common" - currently used as
   tcommon "zotregistry.io/zot/pkg/test/common" - inside pkg/test
   test "zotregistry.io/zot/pkg/test/common" - in tests
   . "zotregistry.io/zot/pkg/test/common" - in tests
Decouple zb from code in test/pkg in order to keep the size small.

2. "zotregistry.io/zot/pkg/test/image-utils" - curently used as
   . "zotregistry.io/zot/pkg/test/image-utils"

3. "zotregistry.io/zot/pkg/test/deprecated" -  curently used as
   "zotregistry.io/zot/pkg/test/deprecated"
This one will bre replaced gradually by image-utils in the future.

4. "zotregistry.io/zot/pkg/test/signature" - (cosign + notation) use as
   "zotregistry.io/zot/pkg/test/signature"

5. "zotregistry.io/zot/pkg/test/auth" - (bearer + oidc)  curently used as
   authutils "zotregistry.io/zot/pkg/test/auth"

 6. "zotregistry.io/zot/pkg/test/oci-utils" -  curently used as
   ociutils "zotregistry.io/zot/pkg/test/oci-utils"

Some unused functions were removed, some were replaced, and in
a few cases specific funtions were moved to the files they were used in.

Added an interface for the StoreController, this reduces the number of imports
of the entire image store, decreasing binary size for tests.
If the zb code was still coupled with pkg/test, this would have reflected in zb size.

Signed-off-by: Andrei Aaron <aaaron@luxoft.com>
This commit is contained in:
Andrei Aaron
2023-09-27 21:34:48 +03:00
committed by GitHub
parent c3801dc3d3
commit ba6f347d8d
82 changed files with 3362 additions and 3243 deletions
+9 -6
View File
@@ -38,9 +38,11 @@ import (
mTypes "zotregistry.io/zot/pkg/meta/types"
"zotregistry.io/zot/pkg/storage"
"zotregistry.io/zot/pkg/storage/local"
. "zotregistry.io/zot/pkg/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/mocks"
ociutils "zotregistry.io/zot/pkg/test/oci-utils"
)
const (
@@ -88,7 +90,7 @@ func testSetup(t *testing.T) (string, error) {
return "", err
}
testStorageCtrl := GetDefaultStoreController(dir, log.NewLogger("debug", ""))
testStorageCtrl := ociutils.GetDefaultStoreController(dir, log.NewLogger("debug", ""))
err = WriteImageToFileSystem(CreateRandomVulnerableImage(), "zot-test", "0.0.1", testStorageCtrl)
if err != nil {
@@ -1647,28 +1649,29 @@ func TestFixedTagsWithIndex(t *testing.T) {
defer cm.StopServer()
// push index with 2 manifests: one with vulns and one without
vulnManifestCreated := time.Date(2010, 1, 1, 1, 1, 1, 1, time.UTC)
vulnManifest, err := GetVulnImageWithConfig(ispec.Image{
vulnManifest, err := deprecated.GetVulnImageWithConfig(ispec.Image{ //nolint:staticcheck
Created: &vulnManifestCreated,
Platform: ispec.Platform{OS: "linux", Architecture: "amd64"},
})
So(err, ShouldBeNil)
fixedManifestCreated := time.Date(2010, 1, 1, 1, 1, 1, 1, time.UTC)
fixedManifest, err := GetImageWithConfig(ispec.Image{
fixedManifest, err := deprecated.GetImageWithConfig(ispec.Image{ //nolint:staticcheck
Created: &fixedManifestCreated,
Platform: ispec.Platform{OS: "windows", Architecture: "amd64"},
})
So(err, ShouldBeNil)
fixedDigest := fixedManifest.Digest()
multiArch := GetMultiarchImageForImages([]Image{fixedManifest, vulnManifest})
multiArch := deprecated.GetMultiarchImageForImages([]Image{fixedManifest, //nolint:staticcheck
vulnManifest})
err = UploadMultiarchImage(multiArch, baseURL, "repo", "multi-arch-tag")
So(err, ShouldBeNil)
// oldest vulnerability
simpleVulnCreated := time.Date(2005, 1, 1, 1, 1, 1, 1, time.UTC)
simpleVulnImg, err := GetVulnImageWithConfig(ispec.Image{
simpleVulnImg, err := deprecated.GetVulnImageWithConfig(ispec.Image{ //nolint:staticcheck
Created: &simpleVulnCreated,
Platform: ispec.Platform{OS: "windows", Architecture: "amd64"},
})
+8 -8
View File
@@ -31,7 +31,7 @@ import (
"zotregistry.io/zot/pkg/scheduler"
"zotregistry.io/zot/pkg/storage"
"zotregistry.io/zot/pkg/storage/local"
. "zotregistry.io/zot/pkg/test"
test "zotregistry.io/zot/pkg/test/common"
. "zotregistry.io/zot/pkg/test/image-utils"
"zotregistry.io/zot/pkg/test/mocks"
)
@@ -530,7 +530,7 @@ func TestScanGeneratorWithMockedData(t *testing.T) { //nolint: gocyclo
defer cancel()
// Make sure the scanner generator has completed despite errors
found, err := ReadLogFileAndSearchString(logPath,
found, err := test.ReadLogFileAndSearchString(logPath,
"Scheduled CVE scan: finished for available images", 20*time.Second)
So(err, ShouldBeNil)
So(found, ShouldBeTrue)
@@ -553,19 +553,19 @@ func TestScanGeneratorWithMockedData(t *testing.T) { //nolint: gocyclo
}
// Make sure the scanner generator is catching the metadb error for repo5:nonexitent-manifest
found, err = ReadLogFileAndSearchString(logPath,
found, err = test.ReadLogFileAndSearchString(logPath,
"Scheduled CVE scan: error while obtaining repo metadata", 20*time.Second)
So(err, ShouldBeNil)
So(found, ShouldBeTrue)
// Make sure the scanner generator is catching the scanning error for repo7
found, err = ReadLogFileAndSearchString(logPath,
found, err = test.ReadLogFileAndSearchString(logPath,
"Scheduled CVE scan errored for image", 20*time.Second)
So(err, ShouldBeNil)
So(found, ShouldBeTrue)
// Make sure the scanner generator is triggered at least twice
found, err = ReadLogFileAndCountStringOccurence(logPath,
found, err = test.ReadLogFileAndCountStringOccurence(logPath,
"Scheduled CVE scan: finished for available images", 30*time.Second, 2)
So(err, ShouldBeNil)
So(found, ShouldBeTrue)
@@ -627,17 +627,17 @@ func TestScanGeneratorWithRealData(t *testing.T) {
defer cancel()
// Make sure the scanner generator has completed
found, err := ReadLogFileAndSearchString(logPath,
found, err := test.ReadLogFileAndSearchString(logPath,
"Scheduled CVE scan: finished for available images", 120*time.Second)
So(err, ShouldBeNil)
So(found, ShouldBeTrue)
found, err = ReadLogFileAndSearchString(logPath,
found, err = test.ReadLogFileAndSearchString(logPath,
image.ManifestDescriptor.Digest.String(), 120*time.Second)
So(err, ShouldBeNil)
So(found, ShouldBeTrue)
found, err = ReadLogFileAndSearchString(logPath,
found, err = test.ReadLogFileAndSearchString(logPath,
"Scheduled CVE scan completed successfully for image", 120*time.Second)
So(err, ShouldBeNil)
So(found, ShouldBeTrue)
@@ -27,7 +27,8 @@ import (
"zotregistry.io/zot/pkg/storage/imagestore"
"zotregistry.io/zot/pkg/storage/local"
storageTypes "zotregistry.io/zot/pkg/storage/types"
"zotregistry.io/zot/pkg/test"
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/mocks"
)
@@ -35,7 +36,7 @@ import (
func generateTestImage(storeController storage.StoreController, image string) {
repoName, tag := common.GetImageDirAndTag(image)
config, layers, manifest, err := test.GetImageComponents(10) //nolint:staticcheck
config, layers, manifest, err := deprecated.GetImageComponents(10) //nolint:staticcheck
So(err, ShouldBeNil)
store := storeController.GetImageStore(repoName)
@@ -178,10 +179,6 @@ func TestTrivyLibraryErrors(t *testing.T) {
// Create temporary directory
rootDir := t.TempDir()
storageCtlr := test.GetDefaultStoreController(rootDir, log.NewLogger("debug", ""))
err := test.WriteImageToFileSystem(CreateDefaultVulnerableImage(), "zot-test", "0.0.1", storageCtlr)
So(err, ShouldBeNil)
log := log.NewLogger("debug", "")
metrics := monitoring.NewMetricsServer(false, log)
@@ -191,6 +188,9 @@ func TestTrivyLibraryErrors(t *testing.T) {
storeController := storage.StoreController{}
storeController.DefaultStore = store
err := WriteImageToFileSystem(CreateDefaultVulnerableImage(), "zot-test", "0.0.1", storeController)
So(err, ShouldBeNil)
params := boltdb.DBParameters{
RootDir: rootDir,
}
+12 -11
View File
@@ -23,21 +23,21 @@ import (
mTypes "zotregistry.io/zot/pkg/meta/types"
"zotregistry.io/zot/pkg/storage"
"zotregistry.io/zot/pkg/storage/local"
"zotregistry.io/zot/pkg/test"
testc "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/mocks"
)
func TestScanBigTestFile(t *testing.T) {
Convey("Scan zot-test", t, func() {
projRootDir, err := testc.GetProjectRootDir()
projRootDir, err := GetProjectRootDir()
So(err, ShouldBeNil)
testImage := filepath.Join(projRootDir, "test/data/zot-test")
tempDir := t.TempDir()
port := test.GetFreePort()
port := GetFreePort()
conf := config.New()
conf.HTTP.Port = port
defaultVal := true
@@ -50,10 +50,10 @@ func TestScanBigTestFile(t *testing.T) {
ctlr := api.NewController(conf)
So(ctlr, ShouldNotBeNil)
err = test.CopyFiles(testImage, filepath.Join(tempDir, "zot-test"))
err = CopyFiles(testImage, filepath.Join(tempDir, "zot-test"))
So(err, ShouldBeNil)
cm := test.NewControllerManager(ctlr)
cm := NewControllerManager(ctlr)
cm.StartAndWait(port)
defer cm.StopServer()
// scan
@@ -72,8 +72,8 @@ func TestScanningByDigest(t *testing.T) {
Convey("Scan the individual manifests inside an index", t, func() {
// start server
tempDir := t.TempDir()
port := test.GetFreePort()
baseURL := test.GetBaseURL(port)
port := GetFreePort()
baseURL := GetBaseURL(port)
conf := config.New()
conf.HTTP.Port = port
defaultVal := true
@@ -86,7 +86,7 @@ func TestScanningByDigest(t *testing.T) {
ctlr := api.NewController(conf)
So(ctlr, ShouldNotBeNil)
cm := test.NewControllerManager(ctlr)
cm := NewControllerManager(ctlr)
cm.StartAndWait(port)
defer cm.StopServer()
// push index with 2 manifests: one with vulns and one without
@@ -94,7 +94,8 @@ func TestScanningByDigest(t *testing.T) {
simpleImage := CreateRandomImage()
multiArch := test.GetMultiarchImageForImages([]Image{simpleImage, vulnImage}) //nolint:staticcheck
multiArch := deprecated.GetMultiarchImageForImages([]Image{simpleImage, //nolint:staticcheck
vulnImage})
err := UploadMultiarchImage(multiArch, baseURL, "multi-arch", "multi-arch-tag")
So(err, ShouldBeNil)
@@ -193,7 +194,7 @@ func TestVulnerableLayer(t *testing.T) {
DefaultStore: imageStore,
}
err = test.WriteImageToFileSystem(img, "repo", img.DigestStr(), storeController)
err = WriteImageToFileSystem(img, "repo", img.DigestStr(), storeController)
So(err, ShouldBeNil)
params := boltdb.DBParameters{
+2 -2
View File
@@ -19,7 +19,7 @@ import (
mTypes "zotregistry.io/zot/pkg/meta/types"
"zotregistry.io/zot/pkg/scheduler"
"zotregistry.io/zot/pkg/storage"
. "zotregistry.io/zot/pkg/test"
test "zotregistry.io/zot/pkg/test/common"
"zotregistry.io/zot/pkg/test/mocks"
)
@@ -68,7 +68,7 @@ func TestCVEDBGenerator(t *testing.T) {
defer cancel()
// Wait for trivy db to download
found, err := ReadLogFileAndCountStringOccurence(logPath,
found, err := test.ReadLogFileAndCountStringOccurence(logPath,
"DB update completed, next update scheduled", 140*time.Second, 2)
So(err, ShouldBeNil)
So(found, ShouldBeTrue)