2021-07-16 22:53:05 -05:00
|
|
|
package storage_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-12-13 14:23:31 -05:00
|
|
|
"crypto/rand"
|
2021-07-16 22:53:05 -05:00
|
|
|
_ "crypto/sha256"
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
2021-12-13 14:23:31 -05:00
|
|
|
"math/big"
|
2021-07-16 22:53:05 -05:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
godigest "github.com/opencontainers/go-digest"
|
|
|
|
ispec "github.com/opencontainers/image-spec/specs-go/v1"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/errors"
|
|
|
|
"zotregistry.io/zot/pkg/extensions/monitoring"
|
|
|
|
"zotregistry.io/zot/pkg/log"
|
|
|
|
"zotregistry.io/zot/pkg/storage"
|
2022-01-19 14:54:17 -05:00
|
|
|
"zotregistry.io/zot/pkg/test"
|
2021-07-16 22:53:05 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStorageFSAPIs(t *testing.T) {
|
|
|
|
dir, err := ioutil.TempDir("", "oci-repo-test")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2022-01-20 23:11:44 -05:00
|
|
|
imgStore := storage.NewImageStore(dir, true, true, true, log, metrics)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
Convey("Repo layout", t, func(c C) {
|
|
|
|
repoName := "test"
|
|
|
|
|
|
|
|
Convey("Bad image manifest", func() {
|
2021-12-13 14:23:31 -05:00
|
|
|
upload, err := imgStore.NewBlobUpload("test")
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldBeNil)
|
2021-12-13 14:23:31 -05:00
|
|
|
So(upload, ShouldNotBeEmpty)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
content := []byte("test-data1")
|
|
|
|
buf := bytes.NewBuffer(content)
|
2021-12-13 14:23:31 -05:00
|
|
|
buflen := buf.Len()
|
|
|
|
digest := godigest.FromBytes(content)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
blob, err := imgStore.PutBlobChunk(repoName, upload, 0, int64(buflen), buf)
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldBeNil)
|
2021-12-13 14:23:31 -05:00
|
|
|
So(blob, ShouldEqual, buflen)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = imgStore.FinishBlobUpload("test", upload, buf, digest.String())
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
annotationsMap := make(map[string]string)
|
|
|
|
annotationsMap[ispec.AnnotationRefName] = "1.0"
|
2022-01-19 14:54:17 -05:00
|
|
|
|
|
|
|
cblob, cdigest := test.GetRandomImageConfig()
|
|
|
|
_, clen, err := imgStore.FullBlobUpload("test", bytes.NewReader(cblob), cdigest.String())
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(clen, ShouldEqual, len(cblob))
|
|
|
|
hasBlob, _, err := imgStore.CheckBlob("test", cdigest.String())
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(hasBlob, ShouldEqual, true)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
manifest := ispec.Manifest{
|
2021-07-16 22:53:05 -05:00
|
|
|
Config: ispec.Descriptor{
|
2022-01-19 14:54:17 -05:00
|
|
|
MediaType: "application/vnd.oci.image.config.v1+json",
|
|
|
|
Digest: cdigest,
|
|
|
|
Size: int64(len(cblob)),
|
2021-07-16 22:53:05 -05:00
|
|
|
},
|
|
|
|
Layers: []ispec.Descriptor{
|
|
|
|
{
|
|
|
|
MediaType: "application/vnd.oci.image.layer.v1.tar",
|
2021-12-13 14:23:31 -05:00
|
|
|
Digest: digest,
|
|
|
|
Size: int64(buflen),
|
2021-07-16 22:53:05 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Annotations: annotationsMap,
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
manifest.SchemaVersion = 2
|
|
|
|
manifestBuf, _ := json.Marshal(manifest)
|
|
|
|
digest = godigest.FromBytes(manifestBuf)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = os.Chmod(path.Join(imgStore.RootDir(), repoName, "index.json"), 0o000)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
_, err = imgStore.PutImageManifest(repoName, "1.0", ispec.MediaTypeImageManifest, manifestBuf)
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = os.Chmod(path.Join(imgStore.RootDir(), repoName, "index.json"), 0o755)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
_, err = imgStore.PutImageManifest(repoName, "1.0", ispec.MediaTypeImageManifest, manifestBuf)
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
manifestPath := path.Join(imgStore.RootDir(), repoName, "blobs", digest.Algorithm().String(), digest.Encoded())
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = os.Chmod(manifestPath, 0o000)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
_, _, _, err = imgStore.GetImageManifest(repoName, digest.String())
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
|
|
|
err = os.Remove(manifestPath)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
_, _, _, err = imgStore.GetImageManifest(repoName, digest.String())
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = os.Chmod(path.Join(imgStore.RootDir(), repoName), 0o000)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
_, err = imgStore.PutImageManifest(repoName, "2.0", ispec.MediaTypeImageManifest, manifestBuf)
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
2021-12-13 14:23:31 -05:00
|
|
|
err = os.Chmod(path.Join(imgStore.RootDir(), repoName), 0o755)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-10-29 21:10:55 -05:00
|
|
|
// invalid GetReferrers
|
2021-12-13 14:23:31 -05:00
|
|
|
_, err = imgStore.GetReferrers("invalid", "invalid", "invalid")
|
2021-10-29 21:10:55 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
_, err = imgStore.GetReferrers(repoName, "invalid", "invalid")
|
2021-10-29 21:10:55 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
_, err = imgStore.GetReferrers(repoName, digest.String(), "invalid")
|
2021-10-29 21:10:55 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
// invalid DeleteImageManifest
|
2021-12-13 14:23:31 -05:00
|
|
|
indexPath := path.Join(imgStore.RootDir(), repoName, "index.json")
|
|
|
|
err = os.Chmod(indexPath, 0o000)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = imgStore.DeleteImageManifest(repoName, digest.String())
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = os.RemoveAll(path.Join(imgStore.RootDir(), repoName))
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDedupeLinks(t *testing.T) {
|
|
|
|
dir, err := ioutil.TempDir("", "oci-repo-test")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2022-01-20 23:11:44 -05:00
|
|
|
imgStore := storage.NewImageStore(dir, true, true, true, log, metrics)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
Convey("Dedupe", t, func(c C) {
|
|
|
|
// manifest1
|
2021-12-13 14:23:31 -05:00
|
|
|
upload, err := imgStore.NewBlobUpload("dedupe1")
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldBeNil)
|
2021-12-13 14:23:31 -05:00
|
|
|
So(upload, ShouldNotBeEmpty)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
content := []byte("test-data3")
|
|
|
|
buf := bytes.NewBuffer(content)
|
2021-12-13 14:23:31 -05:00
|
|
|
buflen := buf.Len()
|
|
|
|
digest := godigest.FromBytes(content)
|
|
|
|
blob, err := imgStore.PutBlobChunkStreamed("dedupe1", upload, buf)
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldBeNil)
|
2021-12-13 14:23:31 -05:00
|
|
|
So(blob, ShouldEqual, buflen)
|
|
|
|
blobDigest1 := strings.Split(digest.String(), ":")[1]
|
2021-07-16 22:53:05 -05:00
|
|
|
So(blobDigest1, ShouldNotBeEmpty)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = imgStore.FinishBlobUpload("dedupe1", upload, buf, digest.String())
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldBeNil)
|
2021-12-13 14:23:31 -05:00
|
|
|
So(blob, ShouldEqual, buflen)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
_, _, err = imgStore.CheckBlob("dedupe1", digest.String())
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
_, _, err = imgStore.GetBlob("dedupe1", digest.String(), "application/vnd.oci.image.layer.v1.tar+gzip")
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2022-01-19 14:54:17 -05:00
|
|
|
cblob, cdigest := test.GetRandomImageConfig()
|
|
|
|
_, clen, err := imgStore.FullBlobUpload("dedupe1", bytes.NewReader(cblob), cdigest.String())
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(clen, ShouldEqual, len(cblob))
|
|
|
|
hasBlob, _, err := imgStore.CheckBlob("dedupe1", cdigest.String())
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(hasBlob, ShouldEqual, true)
|
|
|
|
|
|
|
|
manifest := ispec.Manifest{
|
2021-07-16 22:53:05 -05:00
|
|
|
Config: ispec.Descriptor{
|
2022-01-19 14:54:17 -05:00
|
|
|
MediaType: "application/vnd.oci.image.config.v1+json",
|
|
|
|
Digest: cdigest,
|
|
|
|
Size: int64(len(cblob)),
|
2021-07-16 22:53:05 -05:00
|
|
|
},
|
|
|
|
Layers: []ispec.Descriptor{
|
|
|
|
{
|
|
|
|
MediaType: "application/vnd.oci.image.layer.v1.tar",
|
2021-12-13 14:23:31 -05:00
|
|
|
Digest: digest,
|
|
|
|
Size: int64(buflen),
|
2021-07-16 22:53:05 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
manifest.SchemaVersion = 2
|
|
|
|
manifestBuf, _ := json.Marshal(manifest)
|
|
|
|
digest = godigest.FromBytes(manifestBuf)
|
|
|
|
_, err = imgStore.PutImageManifest("dedupe1", digest.String(), ispec.MediaTypeImageManifest, manifestBuf)
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
_, _, _, err = imgStore.GetImageManifest("dedupe1", digest.String())
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
// manifest2
|
2021-12-13 14:23:31 -05:00
|
|
|
upload, err = imgStore.NewBlobUpload("dedupe2")
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldBeNil)
|
2021-12-13 14:23:31 -05:00
|
|
|
So(upload, ShouldNotBeEmpty)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
content = []byte("test-data3")
|
|
|
|
buf = bytes.NewBuffer(content)
|
2021-12-13 14:23:31 -05:00
|
|
|
buflen = buf.Len()
|
|
|
|
digest = godigest.FromBytes(content)
|
|
|
|
blob, err = imgStore.PutBlobChunkStreamed("dedupe2", upload, buf)
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldBeNil)
|
2021-12-13 14:23:31 -05:00
|
|
|
So(blob, ShouldEqual, buflen)
|
|
|
|
blobDigest2 := strings.Split(digest.String(), ":")[1]
|
2021-07-16 22:53:05 -05:00
|
|
|
So(blobDigest2, ShouldNotBeEmpty)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = imgStore.FinishBlobUpload("dedupe2", upload, buf, digest.String())
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldBeNil)
|
2021-12-13 14:23:31 -05:00
|
|
|
So(blob, ShouldEqual, buflen)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
_, _, err = imgStore.CheckBlob("dedupe2", digest.String())
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
_, _, err = imgStore.GetBlob("dedupe2", digest.String(), "application/vnd.oci.image.layer.v1.tar+gzip")
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2022-01-19 14:54:17 -05:00
|
|
|
cblob, cdigest = test.GetRandomImageConfig()
|
|
|
|
_, clen, err = imgStore.FullBlobUpload("dedupe2", bytes.NewReader(cblob), cdigest.String())
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(clen, ShouldEqual, len(cblob))
|
|
|
|
hasBlob, _, err = imgStore.CheckBlob("dedupe2", cdigest.String())
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(hasBlob, ShouldEqual, true)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
manifest = ispec.Manifest{
|
2021-07-16 22:53:05 -05:00
|
|
|
Config: ispec.Descriptor{
|
2022-01-19 14:54:17 -05:00
|
|
|
MediaType: "application/vnd.oci.image.config.v1+json",
|
|
|
|
Digest: cdigest,
|
|
|
|
Size: int64(len(cblob)),
|
2021-07-16 22:53:05 -05:00
|
|
|
},
|
|
|
|
Layers: []ispec.Descriptor{
|
|
|
|
{
|
|
|
|
MediaType: "application/vnd.oci.image.layer.v1.tar",
|
2021-12-13 14:23:31 -05:00
|
|
|
Digest: digest,
|
|
|
|
Size: int64(buflen),
|
2021-07-16 22:53:05 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
manifest.SchemaVersion = 2
|
|
|
|
manifestBuf, _ = json.Marshal(manifest)
|
|
|
|
digest = godigest.FromBytes(manifestBuf)
|
|
|
|
_, err = imgStore.PutImageManifest("dedupe2", "1.0", ispec.MediaTypeImageManifest, manifestBuf)
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
_, _, _, err = imgStore.GetImageManifest("dedupe2", digest.String())
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
// verify that dedupe with hard links happened
|
|
|
|
fi1, err := os.Stat(path.Join(dir, "dedupe2", "blobs", "sha256", blobDigest1))
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
fi2, err := os.Stat(path.Join(dir, "dedupe2", "blobs", "sha256", blobDigest2))
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(os.SameFile(fi1, fi2), ShouldBeTrue)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDedupe(t *testing.T) {
|
|
|
|
Convey("Dedupe", t, func(c C) {
|
|
|
|
Convey("Nil ImageStore", func() {
|
|
|
|
var is storage.ImageStore
|
|
|
|
So(func() { _ = is.DedupeBlob("", "", "") }, ShouldPanic)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Valid ImageStore", func() {
|
|
|
|
dir, err := ioutil.TempDir("", "oci-repo-test")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2022-01-20 23:11:44 -05:00
|
|
|
il := storage.NewImageStore(dir, true, true, true, log, metrics)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
So(il.DedupeBlob("", "", ""), ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// nolint: gocyclo
|
|
|
|
func TestNegativeCases(t *testing.T) {
|
|
|
|
Convey("Invalid root dir", t, func(c C) {
|
|
|
|
dir, err := ioutil.TempDir("", "oci-repo-test")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
os.RemoveAll(dir)
|
|
|
|
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
|
|
|
|
2022-01-20 23:11:44 -05:00
|
|
|
So(storage.NewImageStore(dir, true, true, true, log, metrics), ShouldNotBeNil)
|
2021-07-16 22:53:05 -05:00
|
|
|
if os.Geteuid() != 0 {
|
2022-01-20 23:11:44 -05:00
|
|
|
So(storage.NewImageStore("/deadBEEF", true, true, true, log, metrics), ShouldBeNil)
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Invalid init repo", t, func(c C) {
|
|
|
|
dir, err := ioutil.TempDir("", "oci-repo-test")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2022-01-20 23:11:44 -05:00
|
|
|
imgStore := storage.NewImageStore(dir, true, true, true, log, metrics)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = os.Chmod(dir, 0o000) // remove all perms
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.Geteuid() != 0 {
|
2021-12-13 14:23:31 -05:00
|
|
|
err = imgStore.InitRepo("test")
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = os.Chmod(dir, 0o755)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init repo should fail if repo is a file.
|
2021-12-13 14:23:31 -05:00
|
|
|
err = ioutil.WriteFile(path.Join(dir, "file-test"), []byte("this is test file"), 0o755) // nolint:gosec
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldBeNil)
|
2021-12-13 14:23:31 -05:00
|
|
|
err = imgStore.InitRepo("file-test")
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = os.Mkdir(path.Join(dir, "test-dir"), 0o755)
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = imgStore.InitRepo("test-dir")
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Invalid validate repo", t, func(c C) {
|
|
|
|
dir, err := ioutil.TempDir("", "oci-repo-test")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2022-01-20 23:11:44 -05:00
|
|
|
imgStore := storage.NewImageStore(dir, true, true, true, log, metrics)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
So(imgStore, ShouldNotBeNil)
|
|
|
|
So(imgStore.InitRepo("test"), ShouldBeNil)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = os.MkdirAll(path.Join(dir, "invalid-test"), 0o755)
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = os.Chmod(path.Join(dir, "invalid-test"), 0o000) // remove all perms
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
_, err = imgStore.ValidateRepo("invalid-test")
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(err, ShouldEqual, errors.ErrRepoNotFound)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = os.Chmod(path.Join(dir, "invalid-test"), 0o755) // remove all perms
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = ioutil.WriteFile(path.Join(dir, "invalid-test", "blobs"), []byte{}, 0o755) // nolint: gosec
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = ioutil.WriteFile(path.Join(dir, "invalid-test", "index.json"), []byte{}, 0o755) // nolint: gosec
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = ioutil.WriteFile(path.Join(dir, "invalid-test", ispec.ImageLayoutFile), []byte{}, 0o755) // nolint: gosec
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
isValid, err := imgStore.ValidateRepo("invalid-test")
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(isValid, ShouldEqual, false)
|
|
|
|
|
|
|
|
err = os.Remove(path.Join(dir, "invalid-test", "blobs"))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
err = os.Mkdir(path.Join(dir, "invalid-test", "blobs"), 0o755)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
isValid, err = imgStore.ValidateRepo("invalid-test")
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(isValid, ShouldEqual, false)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = ioutil.WriteFile(path.Join(dir, "invalid-test", ispec.ImageLayoutFile), []byte("{}"), 0o755) // nolint: gosec
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
isValid, err = imgStore.ValidateRepo("invalid-test")
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(err, ShouldEqual, errors.ErrRepoBadVersion)
|
|
|
|
So(isValid, ShouldEqual, false)
|
|
|
|
|
|
|
|
files, err := ioutil.ReadDir(path.Join(dir, "test"))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range files {
|
|
|
|
os.Remove(path.Join(dir, "test", f.Name()))
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
_, err = imgStore.ValidateRepo("test")
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
|
|
|
err = os.RemoveAll(path.Join(dir, "test"))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
_, err = imgStore.ValidateRepo("test")
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = os.Chmod(dir, 0o000) // remove all perms
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.Geteuid() != 0 {
|
2021-12-13 14:23:31 -05:00
|
|
|
So(func() { _, _ = imgStore.ValidateRepo("test") }, ShouldPanic)
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = os.Chmod(dir, 0o755) // remove all perms
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.RemoveAll(dir)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
_, err = imgStore.GetRepositories()
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Invalid get image tags", t, func(c C) {
|
|
|
|
var ilfs storage.ImageStoreFS
|
|
|
|
_, err := ilfs.GetImageTags("test")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
|
|
|
dir, err := ioutil.TempDir("", "oci-repo-test")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2022-01-20 23:11:44 -05:00
|
|
|
imgStore := storage.NewImageStore(dir, true, true, true, log, metrics)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
So(imgStore, ShouldNotBeNil)
|
|
|
|
So(imgStore.InitRepo("test"), ShouldBeNil)
|
2021-07-16 22:53:05 -05:00
|
|
|
So(os.Remove(path.Join(dir, "test", "index.json")), ShouldBeNil)
|
2021-12-13 14:23:31 -05:00
|
|
|
_, err = imgStore.GetImageTags("test")
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(os.RemoveAll(path.Join(dir, "test")), ShouldBeNil)
|
2021-12-13 14:23:31 -05:00
|
|
|
So(imgStore.InitRepo("test"), ShouldBeNil)
|
|
|
|
So(ioutil.WriteFile(path.Join(dir, "test", "index.json"), []byte{}, 0o600), ShouldBeNil)
|
|
|
|
_, err = imgStore.GetImageTags("test")
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Invalid get image manifest", t, func(c C) {
|
|
|
|
var ilfs storage.ImageStoreFS
|
|
|
|
_, _, _, err := ilfs.GetImageManifest("test", "")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
|
|
|
dir, err := ioutil.TempDir("", "oci-repo-test")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2022-01-20 23:11:44 -05:00
|
|
|
imgStore := storage.NewImageStore(dir, true, true, true, log, metrics)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
So(imgStore, ShouldNotBeNil)
|
|
|
|
So(imgStore.InitRepo("test"), ShouldBeNil)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = os.Chmod(path.Join(dir, "test", "index.json"), 0o000)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
_, _, _, err = imgStore.GetImageManifest("test", "")
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
|
|
|
err = os.Remove(path.Join(dir, "test", "index.json"))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
_, _, _, err = imgStore.GetImageManifest("test", "")
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
|
|
|
err = os.RemoveAll(path.Join(dir, "test"))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
So(imgStore.InitRepo("test"), ShouldBeNil)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = ioutil.WriteFile(path.Join(dir, "test", "index.json"), []byte{}, 0o600)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
_, _, _, err = imgStore.GetImageManifest("test", "")
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Invalid new blob upload", t, func(c C) {
|
|
|
|
dir, err := ioutil.TempDir("", "oci-repo-test")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2022-01-20 23:11:44 -05:00
|
|
|
imgStore := storage.NewImageStore(dir, true, true, true, log, metrics)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
So(imgStore, ShouldNotBeNil)
|
|
|
|
So(imgStore.InitRepo("test"), ShouldBeNil)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = os.Chmod(path.Join(dir, "test", ".uploads"), 0o000)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
_, err = imgStore.NewBlobUpload("test")
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = os.Chmod(path.Join(dir, "test"), 0o000)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
_, err = imgStore.NewBlobUpload("test")
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = os.Chmod(path.Join(dir, "test"), 0o755)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
So(imgStore.InitRepo("test"), ShouldBeNil)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
_, err = imgStore.NewBlobUpload("test")
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = os.Chmod(path.Join(dir, "test", ".uploads"), 0o755)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
upload, err := imgStore.NewBlobUpload("test")
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = os.Chmod(path.Join(dir, "test", ".uploads"), 0o000)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
content := []byte("test-data3")
|
|
|
|
buf := bytes.NewBuffer(content)
|
|
|
|
l := buf.Len()
|
2021-12-13 14:23:31 -05:00
|
|
|
_, err = imgStore.PutBlobChunkStreamed("test", upload, buf)
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
_, err = imgStore.PutBlobChunk("test", upload, 0, int64(l), buf)
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
|
2021-11-10 09:31:03 -05:00
|
|
|
Convey("Invalid dedupe scenarios", t, func() {
|
2021-07-16 22:53:05 -05:00
|
|
|
dir, err := ioutil.TempDir("", "oci-repo-test")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2022-01-20 23:11:44 -05:00
|
|
|
imgStore := storage.NewImageStore(dir, true, true, true, log, metrics)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
upload, err := imgStore.NewBlobUpload("dedupe1")
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldBeNil)
|
2021-12-13 14:23:31 -05:00
|
|
|
So(upload, ShouldNotBeEmpty)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
content := []byte("test-data3")
|
|
|
|
buf := bytes.NewBuffer(content)
|
2021-12-13 14:23:31 -05:00
|
|
|
buflen := buf.Len()
|
|
|
|
digest := godigest.FromBytes(content)
|
|
|
|
blob, err := imgStore.PutBlobChunkStreamed("dedupe1", upload, buf)
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldBeNil)
|
2021-12-13 14:23:31 -05:00
|
|
|
So(blob, ShouldEqual, buflen)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
blobDigest1 := strings.Split(digest.String(), ":")[1]
|
2021-07-16 22:53:05 -05:00
|
|
|
So(blobDigest1, ShouldNotBeEmpty)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = imgStore.FinishBlobUpload("dedupe1", upload, buf, digest.String())
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldBeNil)
|
2021-12-13 14:23:31 -05:00
|
|
|
So(blob, ShouldEqual, buflen)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
// Create a file at the same place where FinishBlobUpload will create
|
2021-12-13 14:23:31 -05:00
|
|
|
err = imgStore.InitRepo("dedupe2")
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = os.MkdirAll(path.Join(dir, "dedupe2", "blobs/sha256"), 0o755)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = ioutil.WriteFile(path.Join(dir, "dedupe2", "blobs/sha256", blobDigest1), content, 0o755) // nolint: gosec
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
upload, err = imgStore.NewBlobUpload("dedupe2")
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldBeNil)
|
2021-12-13 14:23:31 -05:00
|
|
|
So(upload, ShouldNotBeEmpty)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
content = []byte("test-data3")
|
|
|
|
buf = bytes.NewBuffer(content)
|
2021-12-13 14:23:31 -05:00
|
|
|
buflen = buf.Len()
|
|
|
|
digest = godigest.FromBytes(content)
|
|
|
|
blob, err = imgStore.PutBlobChunkStreamed("dedupe2", upload, buf)
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldBeNil)
|
2021-12-13 14:23:31 -05:00
|
|
|
So(blob, ShouldEqual, buflen)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
cmd := exec.Command("sudo", "chattr", "+i", path.Join(dir, "dedupe2", "blobs/sha256", blobDigest1)) // nolint: gosec
|
|
|
|
_, err = cmd.Output()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = imgStore.FinishBlobUpload("dedupe2", upload, buf, digest.String())
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
2021-12-13 14:23:31 -05:00
|
|
|
So(blob, ShouldEqual, buflen)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
cmd = exec.Command("sudo", "chattr", "-i", path.Join(dir, "dedupe2", "blobs/sha256", blobDigest1)) // nolint: gosec
|
|
|
|
_, err = cmd.Output()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = imgStore.FinishBlobUpload("dedupe2", upload, buf, digest.String())
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldBeNil)
|
2021-12-13 14:23:31 -05:00
|
|
|
So(blob, ShouldEqual, buflen)
|
2021-07-16 22:53:05 -05:00
|
|
|
})
|
2021-11-10 09:31:03 -05:00
|
|
|
|
|
|
|
Convey("DirExists call with a filename as argument", t, func(c C) {
|
|
|
|
dir, err := ioutil.TempDir("", "oci-repo-test")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
filePath := path.Join(dir, "file.txt")
|
2021-12-13 14:23:31 -05:00
|
|
|
err = ioutil.WriteFile(filePath, []byte("some dummy file content"), 0o644) //nolint: gosec
|
2021-11-10 09:31:03 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ok := storage.DirExists(filePath)
|
|
|
|
So(ok, ShouldBeFalse)
|
|
|
|
})
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHardLink(t *testing.T) {
|
|
|
|
Convey("Test that ValidateHardLink creates rootDir if it does not exist", t, func() {
|
|
|
|
var randomDir string
|
|
|
|
|
|
|
|
for {
|
2021-12-13 14:23:31 -05:00
|
|
|
nBig, err := rand.Int(rand.Reader, big.NewInt(100))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
randomDir = "/tmp/" + randSeq(int(nBig.Int64()))
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
if _, err := os.Stat(randomDir); os.IsNotExist(err) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(randomDir)
|
|
|
|
|
|
|
|
err := storage.ValidateHardLink(randomDir)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
Convey("Test that ValidateHardLink returns error if rootDir is a file", t, func() {
|
|
|
|
dir, err := ioutil.TempDir("", "storage-hard-test")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
filePath := path.Join(dir, "file.txt")
|
2021-12-13 14:23:31 -05:00
|
|
|
err = ioutil.WriteFile(filePath, []byte("some dummy file content"), 0o644) //nolint: gosec
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = storage.ValidateHardLink(filePath)
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
Convey("Test if filesystem supports hardlink", t, func() {
|
|
|
|
dir, err := ioutil.TempDir("", "storage-hard-test")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
err = storage.ValidateHardLink(dir)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = ioutil.WriteFile(path.Join(dir, "hardtest.txt"), []byte("testing hard link code"), 0o644) //nolint: gosec
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = os.Chmod(dir, 0o400)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Link(path.Join(dir, "hardtest.txt"), path.Join(dir, "duphardtest.txt"))
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = os.Chmod(dir, 0o644)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-20 23:11:44 -05:00
|
|
|
func TestWriteFile(t *testing.T) {
|
|
|
|
Convey("writeFile with commit", t, func() {
|
|
|
|
dir, err := ioutil.TempDir("", "oci-repo-test")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
|
|
|
imgStore := storage.NewImageStore(dir, true, true, true, log, metrics)
|
|
|
|
|
|
|
|
Convey("Failure path1", func() {
|
|
|
|
injected := test.InjectFailure(0)
|
|
|
|
|
|
|
|
err := imgStore.InitRepo("repo1")
|
|
|
|
if injected {
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
} else {
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Failure path2", func() {
|
|
|
|
injected := test.InjectFailure(1)
|
|
|
|
|
|
|
|
err := imgStore.InitRepo("repo2")
|
|
|
|
if injected {
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
} else {
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("writeFile without commit", t, func() {
|
|
|
|
dir, err := ioutil.TempDir("", "oci-repo-test")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
|
|
|
imgStore := storage.NewImageStore(dir, true, true, false, log, metrics)
|
|
|
|
|
|
|
|
Convey("Failure path not reached", func() {
|
|
|
|
err := imgStore.InitRepo("repo1")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
func randSeq(n int) string {
|
2021-12-13 14:23:31 -05:00
|
|
|
letters := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
|
|
|
|
|
|
|
buf := make([]rune, n)
|
|
|
|
for index := range buf {
|
|
|
|
nBig, err := rand.Int(rand.Reader, big.NewInt(int64(len(letters))))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
buf[index] = letters[int(nBig.Int64())]
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
return string(buf)
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|