test(refactor): cleanup/simplify testcases (#1124)

Signed-off-by: Nicol Draghici <idraghic@cisco.com>
This commit is contained in:
Nicol 2023-01-20 20:01:52 +02:00 committed by GitHub
parent 70a60b4660
commit f0d947809b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 144 additions and 392 deletions

View File

@ -6133,10 +6133,7 @@ func TestGCSignaturesAndUntaggedManifests(t *testing.T) {
ctlr.Config.Storage.GC = true
ctlr.Config.Storage.GCDelay = 1 * time.Millisecond
err := test.CopyFiles("../../test/data/zot-test", path.Join(dir, repoName))
if err != nil {
panic(err)
}
test.CopyTestFiles("../../test/data/zot-test", path.Join(dir, repoName))
cm := test.NewControllerManager(ctlr)
cm.StartServer()
@ -6289,10 +6286,7 @@ func TestGCSignaturesAndUntaggedManifests(t *testing.T) {
ctlr.Config.Storage.GC = true
ctlr.Config.Storage.GCDelay = 500 * time.Millisecond
err := test.CopyFiles("../../test/data/zot-test", path.Join(dir, repoName))
if err != nil {
panic(err)
}
test.CopyTestFiles("../../test/data/zot-test", path.Join(dir, repoName))
cm := test.NewControllerManager(ctlr)
cm.StartAndWait(port)
@ -6389,10 +6383,7 @@ func TestPeriodicGC(t *testing.T) {
ctlr.Config.Storage.GCInterval = 1 * time.Hour
ctlr.Config.Storage.GCDelay = 1 * time.Second
err = test.CopyFiles("../../test/data/zot-test", path.Join(dir, repoName))
if err != nil {
panic(err)
}
test.CopyTestFiles("../../test/data/zot-test", path.Join(dir, repoName))
cm := test.NewControllerManager(ctlr)
cm.StartAndWait(port)
@ -6466,10 +6457,7 @@ func TestPeriodicGC(t *testing.T) {
ctlr.Config.Storage.GCInterval = 1 * time.Hour
ctlr.Config.Storage.GCDelay = 1 * time.Second
err = test.CopyFiles("../../test/data/zot-test", path.Join(dir, repoName))
if err != nil {
panic(err)
}
test.CopyTestFiles("../../test/data/zot-test", path.Join(dir, repoName))
So(os.Chmod(dir, 0o000), ShouldBeNil)
@ -6842,10 +6830,7 @@ func makeController(conf *config.Config, dir string, copyTestDataDest string) *a
ctlr := api.NewController(conf)
if copyTestDataDest != "" {
err := test.CopyFiles(copyTestDataDest, dir)
if err != nil {
panic(err)
}
test.CopyTestFiles(copyTestDataDest, dir)
}
ctlr.Config.Storage.RootDirectory = dir

View File

@ -85,9 +85,7 @@ func TestTLSWithAuth(t *testing.T) {
home := os.Getenv("HOME")
destCertsDir := filepath.Join(home, certsDir1)
if err = test.CopyFiles(sourceCertsDir, destCertsDir); err != nil {
panic(err)
}
test.CopyTestFiles(sourceCertsDir, destCertsDir)
defer os.RemoveAll(destCertsDir)
args := []string{"imagetest", "--name", "dummyImageName", "--url", HOST1}
@ -167,9 +165,7 @@ func TestTLSWithoutAuth(t *testing.T) {
home := os.Getenv("HOME")
destCertsDir := filepath.Join(home, certsDir1)
if err = test.CopyFiles(sourceCertsDir, destCertsDir); err != nil {
panic(err)
}
test.CopyTestFiles(sourceCertsDir, destCertsDir)
defer os.RemoveAll(destCertsDir)
args := []string{"imagetest"}

View File

@ -352,10 +352,7 @@ func TestServerCVEResponseGQL(t *testing.T) {
dir := t.TempDir()
err := test.CopyFiles("../../test/data/zot-cve-test", path.Join(dir, "zot-cve-test"))
if err != nil {
panic(err)
}
test.CopyTestFiles("../../test/data/zot-cve-test", path.Join(dir, "zot-cve-test"))
conf.Storage.RootDirectory = dir
trivyConfig := &extconf.TrivyConfig{
@ -645,10 +642,7 @@ func TestNegativeServerResponse(t *testing.T) {
dir := t.TempDir()
err := test.CopyFiles("../../test/data/zot-cve-test", path.Join(dir, "zot-cve-test"))
if err != nil {
panic(err)
}
test.CopyTestFiles("../../test/data/zot-cve-test", path.Join(dir, "zot-cve-test"))
conf.Storage.RootDirectory = dir
trivyConfig := &extconf.TrivyConfig{
@ -731,12 +725,9 @@ func TestNegativeServerResponse(t *testing.T) {
dir := t.TempDir()
err := test.CopyFiles("../../test/data/zot-cve-test", path.Join(dir, "zot-cve-test"))
if err != nil {
panic(err)
}
test.CopyTestFiles("../../test/data/zot-cve-test", path.Join(dir, "zot-cve-test"))
err = os.RemoveAll(path.Join(dir, "zot-cve-test/blobs"))
err := os.RemoveAll(path.Join(dir, "zot-cve-test/blobs"))
if err != nil {
panic(err)
}
@ -818,10 +809,7 @@ func TestServerCVEResponse(t *testing.T) {
dir := t.TempDir()
err := test.CopyFiles("../../test/data/zot-cve-test", path.Join(dir, "zot-cve-test"))
if err != nil {
panic(err)
}
test.CopyTestFiles("../../test/data/zot-cve-test", path.Join(dir, "zot-cve-test"))
conf.Storage.RootDirectory = dir
trivyConfig := &extconf.TrivyConfig{

View File

@ -1284,12 +1284,9 @@ func TestServerResponseGQLWithoutPermissions(t *testing.T) {
dir := t.TempDir()
err := test.CopyFiles("../../test/data/zot-test", path.Join(dir, "zot-test"))
if err != nil {
panic(err)
}
test.CopyTestFiles("../../test/data/zot-test", path.Join(dir, "zot-test"))
err = os.Chmod(path.Join(dir, "zot-test", "blobs"), 0o000)
err := os.Chmod(path.Join(dir, "zot-test", "blobs"), 0o000)
if err != nil {
panic(err)
}

View File

@ -1,7 +1,6 @@
package common_test
import (
"context"
"crypto/x509"
"os"
"path"
@ -85,8 +84,9 @@ func TestCommon(t *testing.T) {
So(err, ShouldBeNil)
ctlr.Config.Storage.RootDirectory = tempDir
go startServer(ctlr)
defer stopServer(ctlr)
cm := test.NewControllerManager(ctlr)
cm.StartServer()
defer cm.StopServer()
test.WaitTillServerReady(baseURL)
var resultPtr interface{}
@ -97,16 +97,3 @@ func TestCommon(t *testing.T) {
So(err, ShouldNotBeNil)
})
}
func startServer(c *api.Controller) {
// this blocks
ctx := context.Background()
if err := c.Run(ctx); err != nil {
return
}
}
func stopServer(c *api.Controller) {
ctx := context.Background()
_ = c.Server.Shutdown(ctx)
}

View File

@ -14,8 +14,6 @@ import (
"testing"
godigest "github.com/opencontainers/go-digest"
//nolint: goimports
ispec "github.com/opencontainers/image-spec/specs-go/v1"
//nolint:golint,stylecheck,revive
. "github.com/smartystreets/goconvey/convey"
"github.com/smartystreets/goconvey/convey/reporting"
@ -486,90 +484,50 @@ func CheckWorkflows(t *testing.T, config *compliance.Config) {
So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, http.StatusNotFound)
// upload image config blob
resp, err = resty.R().Post(baseURL + "/v2/repo7/blobs/uploads/")
cfg, layers, manifest, err := test.GetImageComponents(1)
So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, http.StatusAccepted)
loc = test.Location(baseURL, resp)
cblob, cdigest := test.GetRandomImageConfig()
resp, err = resty.R().
SetContentLength(true).
SetHeader("Content-Length", fmt.Sprintf("%d", len(cblob))).
SetHeader("Content-Type", "application/octet-stream").
SetQueryParam("digest", cdigest.String()).
SetBody(cblob).
Put(loc)
repoName := "repo7"
err = test.UploadImage(
test.Image{
Config: cfg,
Layers: layers,
Manifest: manifest,
Tag: "test:1.0",
}, baseURL, repoName)
So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, http.StatusCreated)
// create a manifest
manifest := ispec.Manifest{
Config: ispec.Descriptor{
MediaType: "application/vnd.oci.image.config.v1+json",
Digest: cdigest,
Size: int64(len(cblob)),
},
Layers: []ispec.Descriptor{
{
MediaType: "application/vnd.oci.image.layer.v1.tar",
Digest: digest,
Size: int64(len(content)),
},
},
}
manifest.SchemaVersion = 2
content, err = json.Marshal(manifest)
So(err, ShouldBeNil)
digest = godigest.FromBytes(content)
So(digest, ShouldNotBeNil)
resp, err = resty.R().SetHeader("Content-Type", "application/vnd.oci.image.manifest.v1+json").
SetBody(content).Put(baseURL + "/v2/repo7/manifests/test:1.0")
So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, http.StatusCreated)
digestHdr := resp.Header().Get(constants.DistContentDigestKey)
So(digestHdr, ShouldNotBeEmpty)
So(digestHdr, ShouldEqual, digest.String())
resp, err = resty.R().SetHeader("Content-Type", "application/vnd.oci.image.manifest.v1+json").
SetBody(content).Put(baseURL + "/v2/repo7/manifests/test:1.0.1")
err = test.UploadImage(
test.Image{
Config: cfg,
Layers: layers,
Manifest: manifest,
Tag: "test:1.0.1",
}, baseURL, repoName)
So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, http.StatusCreated)
digestHdr = resp.Header().Get(constants.DistContentDigestKey)
So(digestHdr, ShouldNotBeEmpty)
So(digestHdr, ShouldEqual, digest.String())
content = []byte("this is a blob5")
digest = godigest.FromBytes(content)
So(digest, ShouldNotBeNil)
// create a manifest with same blob but a different tag
manifest = ispec.Manifest{
Config: ispec.Descriptor{
MediaType: "application/vnd.oci.image.config.v1+json",
Digest: cdigest,
Size: int64(len(cblob)),
},
Layers: []ispec.Descriptor{
{
MediaType: "application/vnd.oci.image.layer.v1.tar",
Digest: digest,
Size: int64(len(content)),
},
},
}
manifest.SchemaVersion = 2
content, err = json.Marshal(manifest)
So(err, ShouldBeNil)
digest = godigest.FromBytes(content)
So(digest, ShouldNotBeNil)
resp, err = resty.R().SetHeader("Content-Type", "application/vnd.oci.image.manifest.v1+json").
SetBody(content).Put(baseURL + "/v2/repo7/manifests/test:2.0")
err = test.UploadImage(
test.Image{
Config: cfg,
Layers: layers,
Manifest: manifest,
Tag: "test:2.0",
}, baseURL, repoName)
So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, http.StatusCreated)
digestHdr = resp.Header().Get(constants.DistContentDigestKey)
So(digestHdr, ShouldNotBeEmpty)
So(digestHdr, ShouldEqual, digest.String())
// check/get by tag
resp, err = resty.R().Head(baseURL + "/v2/repo7/manifests/test:1.0")
@ -637,73 +595,23 @@ func CheckWorkflows(t *testing.T, config *compliance.Config) {
_, _ = Print("\nPagination")
for index := 0; index <= 4; index++ {
// create a blob/layer
resp, err := resty.R().Post(baseURL + "/v2/page0/blobs/uploads/")
cfg, layers, manifest, err := test.GetImageComponents(1)
So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, http.StatusAccepted)
loc := test.Location(baseURL, resp)
So(loc, ShouldNotBeEmpty)
resp, err = resty.R().Get(loc)
repoName := "page0"
err = test.UploadImage(
test.Image{
Config: cfg,
Layers: layers,
Manifest: manifest,
Tag: fmt.Sprintf("test:%d.0", index),
}, baseURL, repoName)
So(err, ShouldBeNil)
content, err := json.Marshal(manifest)
So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, http.StatusNoContent)
content := []byte("this is a blob7")
digest := godigest.FromBytes(content)
So(digest, ShouldNotBeNil)
// monolithic blob upload: success
resp, err = resty.R().SetQueryParam("digest", digest.String()).
SetHeader("Content-Type", "application/octet-stream").SetBody(content).Put(loc)
So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, http.StatusCreated)
blobLoc := resp.Header().Get("Location")
So(blobLoc, ShouldNotBeEmpty)
So(resp.Header().Get("Content-Length"), ShouldEqual, "0")
So(resp.Header().Get(constants.DistContentDigestKey), ShouldNotBeEmpty)
// upload image config blob
resp, err = resty.R().Post(baseURL + "/v2/page0/blobs/uploads/")
So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, http.StatusAccepted)
loc = test.Location(baseURL, resp)
cblob, cdigest := test.GetRandomImageConfig()
resp, err = resty.R().
SetContentLength(true).
SetHeader("Content-Length", fmt.Sprintf("%d", len(cblob))).
SetHeader("Content-Type", "application/octet-stream").
SetQueryParam("digest", cdigest.String()).
SetBody(cblob).
Put(loc)
So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, http.StatusCreated)
// create a manifest
manifest := ispec.Manifest{
Config: ispec.Descriptor{
MediaType: "application/vnd.oci.image.config.v1+json",
Digest: cdigest,
Size: int64(len(cblob)),
},
Layers: []ispec.Descriptor{
{
MediaType: "application/vnd.oci.image.layer.v1.tar",
Digest: digest,
Size: int64(len(content)),
},
},
}
manifest.SchemaVersion = 2
content, err = json.Marshal(manifest)
So(err, ShouldBeNil)
digest = godigest.FromBytes(content)
So(digest, ShouldNotBeNil)
resp, err = resty.R().SetHeader("Content-Type", "application/vnd.oci.image.manifest.v1+json").
SetBody(content).Put(baseURL + fmt.Sprintf("/v2/page0/manifests/test:%d.0", index))
So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, http.StatusCreated)
d := resp.Header().Get(constants.DistContentDigestKey)
So(d, ShouldNotBeEmpty)
So(d, ShouldEqual, digest.String())
}
resp, err := resty.R().Get(baseURL + "/v2/page0/tags/list")
@ -828,119 +736,67 @@ func CheckWorkflows(t *testing.T, config *compliance.Config) {
So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, http.StatusNotFound)
// upload image config blob
resp, err = resty.R().Post(baseURL + "/v2/firsttest/first/blobs/uploads/")
cfg, layers, manifest, err := test.GetImageComponents(1)
So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, http.StatusAccepted)
loc := test.Location(baseURL, resp)
cblob, cdigest := test.GetRandomImageConfig()
resp, err = resty.R().
SetContentLength(true).
SetHeader("Content-Length", fmt.Sprintf("%d", len(cblob))).
SetHeader("Content-Type", "application/octet-stream").
SetQueryParam("digest", cdigest.String()).
SetBody(cblob).
Put(loc)
// subpath firsttest
err = test.UploadImage(
test.Image{
Config: cfg,
Layers: layers,
Manifest: manifest,
Tag: "test:1.0",
}, baseURL, "firsttest/first")
So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, http.StatusCreated)
// upload image config blob
resp, err = resty.R().Post(baseURL + "/v2/secondtest/second/blobs/uploads/")
So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, http.StatusAccepted)
loc = test.Location(baseURL, resp)
resp, err = resty.R().
SetContentLength(true).
SetHeader("Content-Length", fmt.Sprintf("%d", len(cblob))).
SetHeader("Content-Type", "application/octet-stream").
SetQueryParam("digest", cdigest.String()).
SetBody(cblob).
Put(loc)
So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, http.StatusCreated)
// create a manifest
manifest := ispec.Manifest{
Config: ispec.Descriptor{
MediaType: "application/vnd.oci.image.config.v1+json",
Digest: cdigest,
Size: int64(len(cblob)),
},
Layers: []ispec.Descriptor{
{
MediaType: "application/vnd.oci.image.layer.v1.tar",
Digest: digest,
Size: int64(len(content)),
},
},
}
manifest.SchemaVersion = 2
content, err = json.Marshal(manifest)
So(err, ShouldBeNil)
digest = godigest.FromBytes(content)
So(digest, ShouldNotBeNil)
// subpath firsttest
resp, err = resty.R().SetHeader("Content-Type", "application/vnd.oci.image.manifest.v1+json").
SetBody(content).Put(baseURL + "/v2/firsttest/first/manifests/test:1.0")
So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, http.StatusCreated)
digestHdr := resp.Header().Get(constants.DistContentDigestKey)
So(digestHdr, ShouldNotBeEmpty)
So(digestHdr, ShouldEqual, digest.String())
// subpath secondtest
resp, err = resty.R().SetHeader("Content-Type", "application/vnd.oci.image.manifest.v1+json").
SetBody(content).Put(baseURL + "/v2/secondtest/second/manifests/test:1.0")
err = test.UploadImage(
test.Image{
Config: cfg,
Layers: layers,
Manifest: manifest,
Tag: "test:1.0",
}, baseURL, "secondtest/second")
So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, http.StatusCreated)
digestHdr = resp.Header().Get(constants.DistContentDigestKey)
So(digestHdr, ShouldNotBeEmpty)
So(digestHdr, ShouldEqual, digest.String())
content, err = json.Marshal(manifest)
So(err, ShouldBeNil)
digest = godigest.FromBytes(content)
So(digest, ShouldNotBeNil)
content = []byte("this is a blob5")
digest = godigest.FromBytes(content)
So(digest, ShouldNotBeNil)
// create a manifest with same blob but a different tag
manifest = ispec.Manifest{
Config: ispec.Descriptor{
MediaType: "application/vnd.oci.image.config.v1+json",
Digest: cdigest,
Size: int64(len(cblob)),
},
Layers: []ispec.Descriptor{
{
MediaType: "application/vnd.oci.image.layer.v1.tar",
Digest: digest,
Size: int64(len(content)),
},
},
}
manifest.SchemaVersion = 2
// subpath firsttest
err = test.UploadImage(
test.Image{
Config: cfg,
Layers: layers,
Manifest: manifest,
Tag: "test:2.0",
}, baseURL, "firsttest/first")
So(err, ShouldBeNil)
content, err = json.Marshal(manifest)
So(err, ShouldBeNil)
digest = godigest.FromBytes(content)
So(digest, ShouldNotBeNil)
// subpath firsttest
resp, err = resty.R().SetHeader("Content-Type", "application/vnd.oci.image.manifest.v1+json").
SetBody(content).Put(baseURL + "/v2/firsttest/first/manifests/test:2.0")
So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, http.StatusCreated)
digestHdr = resp.Header().Get(constants.DistContentDigestKey)
So(digestHdr, ShouldNotBeEmpty)
So(digestHdr, ShouldEqual, digest.String())
// subpath secondtest
resp, err = resty.R().SetHeader("Content-Type", "application/vnd.oci.image.manifest.v1+json").
SetBody(content).Put(baseURL + "/v2/secondtest/second/manifests/test:2.0")
err = test.UploadImage(
test.Image{
Config: cfg,
Layers: layers,
Manifest: manifest,
Tag: "test:2.0",
}, baseURL, "secondtest/second")
So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, http.StatusCreated)
digestHdr = resp.Header().Get(constants.DistContentDigestKey)
So(digestHdr, ShouldNotBeEmpty)
So(digestHdr, ShouldEqual, digest.String())
// check/get by tag
resp, err = resty.R().Head(baseURL + "/v2/firsttest/first/manifests/test:1.0")

View File

@ -5,7 +5,6 @@ package log_test
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
@ -49,10 +48,7 @@ type AuditLog struct {
func TestAuditLogMessages(t *testing.T) {
Convey("Make a new controller", t, func() {
dir := t.TempDir()
err := CopyFiles("../../test/data", dir)
if err != nil {
panic(err)
}
CopyTestFiles("../../test/data", dir)
port := GetFreePort()
baseURL := GetBaseURL(port)
@ -74,26 +70,10 @@ func TestAuditLogMessages(t *testing.T) {
ctlr := api.NewController(conf)
ctlr.Config.Storage.RootDirectory = dir
go func() {
// this blocks
if err := ctlr.Run(context.Background()); err != nil {
return
}
}()
// wait till ready
for {
_, err := resty.R().Get(baseURL)
if err == nil {
break
}
time.Sleep(100 * time.Millisecond)
}
defer func() {
ctx := context.Background()
_ = ctlr.Server.Shutdown(ctx)
}()
ctlrManager := NewControllerManager(ctlr)
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
Convey("Open auditLog file", func() {
auditFile, err := os.Open(auditPath)

View File

@ -2153,14 +2153,11 @@ func TestGarbageCollectForImageStore(t *testing.T) {
imgStore := local.NewImageStore(dir, true, 1*time.Second, true, true, log, metrics, nil, cacheDriver)
repoName := "gc-all-repos-short"
err := test.CopyFiles("../../../test/data/zot-test", path.Join(dir, repoName))
if err != nil {
panic(err)
}
test.CopyTestFiles("../../../test/data/zot-test", path.Join(dir, repoName))
var manifestDigest godigest.Digest
manifestDigest, _, _ = test.GetOciLayoutDigests("../../../test/data/zot-test")
err = os.Remove(path.Join(dir, repoName, "blobs/sha256", manifestDigest.Encoded()))
err := os.Remove(path.Join(dir, repoName, "blobs/sha256", manifestDigest.Encoded()))
if err != nil {
panic(err)
}
@ -2191,14 +2188,11 @@ func TestGarbageCollectForImageStore(t *testing.T) {
imgStore := local.NewImageStore(dir, true, 1*time.Second, true, true, log, metrics, nil, cacheDriver)
repoName := "gc-all-repos-short"
err := test.CopyFiles("../../../test/data/zot-test", path.Join(dir, repoName))
if err != nil {
panic(err)
}
test.CopyTestFiles("../../../test/data/zot-test", path.Join(dir, repoName))
So(os.Chmod(path.Join(dir, repoName, "index.json"), 0o000), ShouldBeNil)
err = imgStore.RunGCRepo(repoName)
err := imgStore.RunGCRepo(repoName)
So(err, ShouldNotBeNil)
time.Sleep(500 * time.Millisecond)
@ -2739,15 +2733,9 @@ func TestGetNextRepository(t *testing.T) {
firstRepoName := "repo1"
secondRepoName := "repo2"
err := test.CopyFiles("../../../test/data/zot-test", path.Join(dir, firstRepoName))
if err != nil {
panic(err)
}
test.CopyTestFiles("../../../test/data/zot-test", path.Join(dir, firstRepoName))
err = test.CopyFiles("../../../test/data/zot-test", path.Join(dir, secondRepoName))
if err != nil {
panic(err)
}
test.CopyTestFiles("../../../test/data/zot-test", path.Join(dir, secondRepoName))
Convey("Return first repository", t, func() {
firstRepo, err := imgStore.GetNextRepository("")
@ -2764,9 +2752,9 @@ func TestGetNextRepository(t *testing.T) {
})
Convey("Return error", t, func() {
err = os.Chmod(imgStore.RootDir(), 0o000)
err := os.Chmod(imgStore.RootDir(), 0o000)
So(err, ShouldBeNil)
_, err := imgStore.GetNextRepository(firstRepoName)
_, err = imgStore.GetNextRepository(firstRepoName)
So(err, ShouldNotBeNil)
err = os.Chmod(imgStore.RootDir(), 0o755)
So(err, ShouldBeNil)

View File

@ -4,7 +4,6 @@
package test_test
import (
"context"
"encoding/json"
"fmt"
"os"
@ -30,12 +29,9 @@ func TestCopyFiles(t *testing.T) {
Convey("destDir is a file", t, func() {
dir := t.TempDir()
err := test.CopyFiles("../../test/data", dir)
if err != nil {
panic(err)
}
test.CopyTestFiles("../../test/data", dir)
err = test.CopyFiles(dir, "/etc/passwd")
err := test.CopyFiles(dir, "/etc/passwd")
So(err, ShouldNotBeNil)
})
Convey("sourceDir does not have read permissions", t, func() {
@ -126,12 +122,9 @@ func TestGetOciLayoutDigests(t *testing.T) {
})
Convey("no permissions when getting index", t, func() {
err := test.CopyFiles("../../test/data/zot-test", path.Join(dir, "test-index"))
if err != nil {
panic(err)
}
test.CopyTestFiles("../../test/data/zot-test", path.Join(dir, "test-index"))
err = os.Chmod(path.Join(dir, "test-index", "index.json"), 0o000)
err := os.Chmod(path.Join(dir, "test-index", "index.json"), 0o000)
if err != nil {
panic(err)
}
@ -145,10 +138,7 @@ func TestGetOciLayoutDigests(t *testing.T) {
})
Convey("can't access manifest digest", t, func() {
err := test.CopyFiles("../../test/data/zot-test", path.Join(dir, "test-manifest"))
if err != nil {
panic(err)
}
test.CopyTestFiles("../../test/data/zot-test", path.Join(dir, "test-manifest"))
buf, err := os.ReadFile(path.Join(dir, "test-manifest", "index.json"))
if err != nil {
@ -238,10 +228,10 @@ func TestUploadBlob(t *testing.T) {
}
ctlr := api.NewController(conf)
go startServer(ctlr)
defer stopServer(ctlr)
test.WaitTillServerReady(baseURL)
ctlrManager := test.NewControllerManager(ctlr)
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
err = test.UploadBlob(baseURL, "test", []byte("test"), "zot.com.test")
So(err, ShouldEqual, test.ErrPostBlob)
@ -257,10 +247,9 @@ func TestUploadBlob(t *testing.T) {
conf.Storage.RootDirectory = tempDir
ctlr := api.NewController(conf)
go startServer(ctlr)
defer stopServer(ctlr)
test.WaitTillServerReady(baseURL)
ctlrManager := test.NewControllerManager(ctlr)
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
blob := new([]byte)
@ -278,10 +267,9 @@ func TestUploadBlob(t *testing.T) {
conf.Storage.RootDirectory = tempDir
ctlr := api.NewController(conf)
go startServer(ctlr)
defer stopServer(ctlr)
test.WaitTillServerReady(baseURL)
ctlrManager := test.NewControllerManager(ctlr)
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
blob := []byte("test")
blobDigest := godigest.FromBytes(blob)
@ -326,10 +314,10 @@ func TestUploadBlob(t *testing.T) {
conf.Storage.RootDirectory = tempDir
ctlr := api.NewController(conf)
go startServer(ctlr)
defer stopServer(ctlr)
test.WaitTillServerReady(baseURL)
ctlrManager := test.NewControllerManager(ctlr)
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
blob := []byte("test")
@ -370,10 +358,10 @@ func TestUploadImage(t *testing.T) {
}
ctlr := api.NewController(conf)
go startServer(ctlr)
defer stopServer(ctlr)
test.WaitTillServerReady(baseURL)
ctlrManager := test.NewControllerManager(ctlr)
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
img := test.Image{
Layers: make([][]byte, 10),
@ -392,10 +380,10 @@ func TestUploadImage(t *testing.T) {
conf.Storage.RootDirectory = t.TempDir()
ctlr := api.NewController(conf)
go startServer(ctlr)
defer stopServer(ctlr)
test.WaitTillServerReady(baseURL)
ctlrManager := test.NewControllerManager(ctlr)
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
img := test.Image{
Layers: make([][]byte, 10), // invalid format that will result in an error
@ -415,10 +403,10 @@ func TestUploadImage(t *testing.T) {
conf.Storage.RootDirectory = t.TempDir()
ctlr := api.NewController(conf)
go startServer(ctlr)
defer stopServer(ctlr)
test.WaitTillServerReady(baseURL)
ctlrManager := test.NewControllerManager(ctlr)
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
layerBlob := []byte("test")
@ -483,9 +471,9 @@ func TestUploadImage(t *testing.T) {
ctlr.Config.Storage.RootDirectory = tempDir
go startServer(ctlr)
defer stopServer(ctlr)
test.WaitTillServerReady(baseURL)
ctlrManager := test.NewControllerManager(ctlr)
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
Convey("Request fail while pushing layer", func() {
err := test.UploadImageWithBasicAuth(test.Image{Layers: [][]byte{{1, 2, 3}}}, "badURL", "", "", "")
@ -515,10 +503,10 @@ func TestUploadImage(t *testing.T) {
conf.Storage.RootDirectory = tempDir
ctlr := api.NewController(conf)
go startServer(ctlr)
defer stopServer(ctlr)
test.WaitTillServerReady(baseURL)
ctlrManager := test.NewControllerManager(ctlr)
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
layerBlob := []byte("test")
layerBlobDigest := godigest.FromBytes(layerBlob)
@ -568,10 +556,10 @@ func TestUploadImage(t *testing.T) {
conf.Storage.RootDirectory = tempDir
ctlr := api.NewController(conf)
go startServer(ctlr)
defer stopServer(ctlr)
test.WaitTillServerReady(baseURL)
ctlrManager := test.NewControllerManager(ctlr)
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
layerBlob := []byte("test")
@ -621,10 +609,10 @@ func TestInjectUploadImage(t *testing.T) {
conf.Storage.RootDirectory = tempDir
ctlr := api.NewController(conf)
go startServer(ctlr)
defer stopServer(ctlr)
test.WaitTillServerReady(baseURL)
ctlrManager := test.NewControllerManager(ctlr)
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
layerBlob := []byte("test")
layerPath := path.Join(tempDir, "test", ".uploads")
@ -717,10 +705,10 @@ func TestInjectUploadImageWithBasicAuth(t *testing.T) {
}
ctlr := api.NewController(conf)
go startServer(ctlr)
defer stopServer(ctlr)
test.WaitTillServerReady(baseURL)
ctlrManager := test.NewControllerManager(ctlr)
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
layerBlob := []byte("test")
layerPath := path.Join(tempDir, "test", ".uploads")
@ -769,16 +757,3 @@ func TestInjectUploadImageWithBasicAuth(t *testing.T) {
})
})
}
func startServer(c *api.Controller) {
// this blocks
ctx := context.Background()
if err := c.Run(ctx); err != nil {
return
}
}
func stopServer(c *api.Controller) {
ctx := context.Background()
_ = c.Server.Shutdown(ctx)
}