test(refactor): refactor tests that use zot-test to use smaller images (#1690)

Signed-off-by: Laurentiu Niculae <niculae.laurentiu1@gmail.com>
This commit is contained in:
LaurentiuNiculae
2023-08-18 11:46:11 +03:00
committed by GitHub
parent e510df7c22
commit 0731fd3828
24 changed files with 536 additions and 194 deletions
+55
View File
@@ -49,9 +49,14 @@ import (
"oras.land/oras-go/v2/registry/remote"
"oras.land/oras-go/v2/registry/remote/auth"
"zotregistry.io/zot/pkg/extensions/monitoring"
zLog "zotregistry.io/zot/pkg/log"
"zotregistry.io/zot/pkg/storage"
storageCommon "zotregistry.io/zot/pkg/storage/common"
"zotregistry.io/zot/pkg/storage/local"
"zotregistry.io/zot/pkg/storage/types"
"zotregistry.io/zot/pkg/test/inject"
"zotregistry.io/zot/pkg/test/mocks"
)
const (
@@ -228,6 +233,38 @@ func CopyTestFiles(sourceDir, destDir string) {
}
}
func CopyTestKeysAndCerts(destDir string) error {
files := []string{
"ca.crt", "ca.key", "client.cert", "client.csr",
"client.key", "server.cert", "server.csr", "server.key",
}
rootPath, err := GetProjectRootDir()
if err != nil {
return err
}
sourceDir := filepath.Join(rootPath, "test/data")
sourceMeta, err := os.Stat(sourceDir)
if err != nil {
return fmt.Errorf("CopyFiles os.Stat failed: %w", err)
}
if err := os.MkdirAll(destDir, sourceMeta.Mode()); err != nil {
return err
}
for _, file := range files {
err = CopyFile(filepath.Join(sourceDir, file), filepath.Join(destDir, file))
if err != nil {
return err
}
}
return nil
}
type Controller interface {
Init(ctx context.Context) error
Run(ctx context.Context) error
@@ -2029,3 +2066,21 @@ func GetDefaultLayersBlobs() [][]byte {
[]byte("xyz"),
}
}
func GetDefaultImageStore(rootDir string, log zLog.Logger) types.ImageStore {
return local.NewImageStore(rootDir, false, time.Hour, false, false, log,
monitoring.NewMetricsServer(false, log),
mocks.MockedLint{
LintFn: func(repo string, manifestDigest godigest.Digest, imageStore types.ImageStore) (bool, error) {
return true, nil
},
},
mocks.CacheMock{},
)
}
func GetDefaultStoreController(rootDir string, log zLog.Logger) storage.StoreController {
return storage.StoreController{
DefaultStore: GetDefaultImageStore(rootDir, log),
}
}
+56 -3
View File
@@ -11,6 +11,7 @@ import (
"io"
"os"
"path"
"path/filepath"
"testing"
"time"
@@ -22,6 +23,7 @@ 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/inject"
@@ -131,9 +133,13 @@ func TestGetOciLayoutDigests(t *testing.T) {
})
Convey("no permissions when getting index", t, func() {
test.CopyTestFiles("../../test/data/zot-test", path.Join(dir, "test-index"))
storageCtlr := test.GetDefaultStoreController(dir, log.NewLogger("debug", ""))
image := test.CreateDefaultImage()
err := os.Chmod(path.Join(dir, "test-index", "index.json"), 0o000)
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)
}
@@ -147,7 +153,11 @@ func TestGetOciLayoutDigests(t *testing.T) {
})
Convey("can't access manifest digest", t, func() {
test.CopyTestFiles("../../test/data/zot-test", path.Join(dir, "test-manifest"))
storageCtlr := test.GetDefaultStoreController(dir, log.NewLogger("debug", ""))
image := test.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 {
@@ -1504,3 +1514,46 @@ func TestBearerServer(t *testing.T) {
So(func() { test.MakeAuthTestServer("", "") }, ShouldPanic)
})
}
func TestCopyTestKeysAndCerts(t *testing.T) {
Convey("CopyTestKeysAndCerts", t, func() {
// ------- Make test files unreadable -------
dir := t.TempDir()
file := filepath.Join(dir, "ca.crt")
_, err := os.Create(file)
So(err, ShouldBeNil)
err = os.Chmod(file, 0o000)
So(err, ShouldBeNil)
err = test.CopyTestKeysAndCerts(dir)
So(err, ShouldNotBeNil)
err = os.Chmod(file, 0o777)
So(err, ShouldBeNil)
// ------- Copy fails -------
err = os.Chmod(dir, 0o000)
So(err, ShouldBeNil)
err = test.CopyTestKeysAndCerts(file)
So(err, ShouldNotBeNil)
err = os.Chmod(dir, 0o777)
So(err, ShouldBeNil)
// ------- Folder creation fails -------
file = filepath.Join(dir, "a-file.file")
_, err = os.Create(file)
So(err, ShouldBeNil)
_, err = os.Stat(file)
So(err, ShouldBeNil)
err = test.CopyTestKeysAndCerts(file)
So(err, ShouldNotBeNil)
})
}
+2 -2
View File
@@ -156,8 +156,8 @@ func CreateRandomImageWith() ManifestBuilder {
return CreateImageWith().RandomLayers(layerCount, layerSize).RandomConfig()
}
// CreateVulnerableImage creates a vulnerable image with the default config.
func CreateVulnerableImage() Image {
// CreateDefaultVulnerableImage creates a vulnerable image with the default config.
func CreateDefaultVulnerableImage() Image {
return CreateImageWith().VulnerableLayers().DefaultVulnConfig().Build()
}