2022-09-30 12:35:16 -05:00
|
|
|
package local_test
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-12-13 14:23:31 -05:00
|
|
|
"crypto/rand"
|
2021-07-16 22:53:05 -05:00
|
|
|
_ "crypto/sha256"
|
|
|
|
"encoding/json"
|
2022-07-27 12:37:55 -05:00
|
|
|
"errors"
|
2022-03-21 13:40:37 -05:00
|
|
|
"fmt"
|
2022-05-19 03:22:51 -05:00
|
|
|
"io"
|
2022-07-27 12:37:55 -05:00
|
|
|
"io/fs"
|
2021-12-13 14:23:31 -05:00
|
|
|
"math/big"
|
2021-07-16 22:53:05 -05:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
2022-07-27 12:37:55 -05:00
|
|
|
"syscall"
|
2021-07-16 22:53:05 -05:00
|
|
|
"testing"
|
2022-02-09 19:51:35 -05:00
|
|
|
"time"
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
godigest "github.com/opencontainers/go-digest"
|
2022-07-27 12:37:55 -05:00
|
|
|
imeta "github.com/opencontainers/image-spec/specs-go"
|
2021-07-16 22:53:05 -05:00
|
|
|
ispec "github.com/opencontainers/image-spec/specs-go/v1"
|
2022-07-14 07:30:24 -05:00
|
|
|
artifactspec "github.com/oras-project/artifacts-spec/specs-go/v1"
|
2021-07-16 22:53:05 -05:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
2022-10-20 11:39:20 -05:00
|
|
|
|
2022-07-27 12:37:55 -05:00
|
|
|
zerr "zotregistry.io/zot/errors"
|
2022-12-22 13:19:42 -05:00
|
|
|
"zotregistry.io/zot/pkg/common"
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/pkg/extensions/monitoring"
|
|
|
|
"zotregistry.io/zot/pkg/log"
|
|
|
|
"zotregistry.io/zot/pkg/storage"
|
2022-11-02 17:53:08 -05:00
|
|
|
"zotregistry.io/zot/pkg/storage/cache"
|
2023-01-18 11:24:44 -05:00
|
|
|
storageConstants "zotregistry.io/zot/pkg/storage/constants"
|
2022-09-30 12:35:16 -05:00
|
|
|
"zotregistry.io/zot/pkg/storage/local"
|
2022-01-19 14:54:17 -05:00
|
|
|
"zotregistry.io/zot/pkg/test"
|
2021-07-16 22:53:05 -05:00
|
|
|
)
|
|
|
|
|
2022-02-09 19:51:35 -05:00
|
|
|
const (
|
2022-09-30 12:35:16 -05:00
|
|
|
tag = "1.0"
|
|
|
|
repoName = "test"
|
2022-02-09 19:51:35 -05:00
|
|
|
)
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
func TestStorageFSAPIs(t *testing.T) {
|
2022-03-07 03:55:12 -05:00
|
|
|
dir := t.TempDir()
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, log)
|
2022-09-30 12:35:16 -05:00
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay, true,
|
2022-11-02 17:53:08 -05:00
|
|
|
true, log, metrics, nil, cacheDriver)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
Convey("Repo layout", t, func(c C) {
|
|
|
|
Convey("Bad image manifest", func() {
|
2022-07-27 12:37:55 -05:00
|
|
|
upload, err := imgStore.NewBlobUpload(repoName)
|
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
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
err = imgStore.FinishBlobUpload(repoName, upload, buf, digest)
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
annotationsMap := make(map[string]string)
|
2022-02-09 19:51:35 -05:00
|
|
|
annotationsMap[ispec.AnnotationRefName] = tag
|
2022-01-19 14:54:17 -05:00
|
|
|
|
|
|
|
cblob, cdigest := test.GetRandomImageConfig()
|
2022-10-22 15:46:13 -05:00
|
|
|
_, clen, err := imgStore.FullBlobUpload(repoName, bytes.NewReader(cblob), cdigest)
|
2022-01-19 14:54:17 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(clen, ShouldEqual, len(cblob))
|
2022-10-22 15:46:13 -05:00
|
|
|
hasBlob, _, err := imgStore.CheckBlob(repoName, cdigest)
|
2022-01-19 14:54:17 -05:00
|
|
|
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
|
2022-03-21 12:37:23 -05:00
|
|
|
manifestBuf, err := json.Marshal(manifest)
|
|
|
|
So(err, ShouldBeNil)
|
2021-12-13 14:23:31 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-11-08 03:38:16 -05:00
|
|
|
// invalid GetOrasReferrers
|
|
|
|
_, err = imgStore.GetOrasReferrers("invalid", "invalid", "invalid")
|
2021-10-29 21:10:55 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
2022-11-08 03:38:16 -05:00
|
|
|
_, err = imgStore.GetOrasReferrers(repoName, "invalid", "invalid")
|
2021-10-29 21:10:55 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
2022-11-08 03:38:16 -05:00
|
|
|
_, err = imgStore.GetOrasReferrers(repoName, digest, "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)
|
|
|
|
}
|
|
|
|
|
2022-11-18 12:35:28 -05:00
|
|
|
err = imgStore.DeleteImageManifest(repoName, digest.String(), false)
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-11-08 03:38:16 -05:00
|
|
|
func TestGetOrasReferrers(t *testing.T) {
|
2022-07-14 07:30:24 -05:00
|
|
|
dir := t.TempDir()
|
|
|
|
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, log)
|
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay, true, true, log, metrics, nil, cacheDriver)
|
2022-07-14 07:30:24 -05:00
|
|
|
|
|
|
|
Convey("Get referrers", t, func(c C) {
|
2022-09-30 12:35:16 -05:00
|
|
|
err := test.CopyFiles("../../../test/data/zot-test", path.Join(dir, "zot-test"))
|
2022-07-14 07:30:24 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
body := []byte("this is a blob")
|
|
|
|
digest := godigest.FromBytes(body)
|
|
|
|
buf := bytes.NewBuffer(body)
|
|
|
|
buflen := buf.Len()
|
2022-09-02 07:56:02 -05:00
|
|
|
err = os.WriteFile(path.Join(imgStore.RootDir(), //nolint: gosec
|
2022-07-14 07:30:24 -05:00
|
|
|
"zot-test", "blobs", digest.Algorithm().String(), digest.Encoded()),
|
|
|
|
buf.Bytes(), 0o644)
|
|
|
|
So(err, ShouldBeNil)
|
2022-10-22 15:46:13 -05:00
|
|
|
_, n, err := imgStore.FullBlobUpload("zot-test", buf, digest)
|
2022-07-14 07:30:24 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(n, ShouldEqual, buflen)
|
|
|
|
|
|
|
|
artifactManifest := artifactspec.Manifest{}
|
|
|
|
artifactManifest.ArtifactType = "signature-example"
|
2022-08-20 20:18:50 -05:00
|
|
|
artifactManifest.Subject = &artifactspec.Descriptor{
|
2022-07-14 07:30:24 -05:00
|
|
|
MediaType: ispec.MediaTypeImageManifest,
|
|
|
|
Digest: digest,
|
|
|
|
Size: int64(buflen),
|
|
|
|
}
|
|
|
|
artifactManifest.Blobs = []artifactspec.Descriptor{}
|
|
|
|
manBuf, err := json.Marshal(artifactManifest)
|
|
|
|
manBufLen := len(manBuf)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
manDigest := godigest.FromBytes(manBuf)
|
|
|
|
_, err = imgStore.PutImageManifest("zot-test", manDigest.Encoded(), artifactspec.MediaTypeArtifactManifest, manBuf)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
2022-11-08 03:38:16 -05:00
|
|
|
descriptors, err := imgStore.GetOrasReferrers("zot-test", digest, "signature-example")
|
2022-07-14 07:30:24 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(descriptors, ShouldNotBeEmpty)
|
|
|
|
So(descriptors[0].ArtifactType, ShouldEqual, "signature-example")
|
|
|
|
So(descriptors[0].MediaType, ShouldEqual, artifactspec.MediaTypeArtifactManifest)
|
|
|
|
So(descriptors[0].Size, ShouldEqual, manBufLen)
|
|
|
|
So(descriptors[0].Digest, ShouldEqual, manDigest)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-07-27 12:37:55 -05:00
|
|
|
func FuzzNewBlobUpload(f *testing.F) {
|
|
|
|
f.Fuzz(func(t *testing.T, data string) {
|
|
|
|
dir := t.TempDir()
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
t.Logf("Input argument is %s", data)
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, log)
|
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay, true, true, log, metrics, nil, cacheDriver)
|
2022-07-27 12:37:55 -05:00
|
|
|
|
|
|
|
_, err := imgStore.NewBlobUpload(data)
|
|
|
|
if err != nil {
|
|
|
|
if isKnownErr(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func FuzzPutBlobChunk(f *testing.F) {
|
|
|
|
f.Fuzz(func(t *testing.T, data string) {
|
|
|
|
dir := t.TempDir()
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
t.Logf("Input argument is %s", data)
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, log)
|
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay, true, true, log, metrics, nil, cacheDriver)
|
2022-07-27 12:37:55 -05:00
|
|
|
|
|
|
|
repoName := data
|
|
|
|
uuid, err := imgStore.NewBlobUpload(repoName)
|
|
|
|
if err != nil {
|
|
|
|
if isKnownErr(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer([]byte(data))
|
|
|
|
buflen := buf.Len()
|
|
|
|
_, err = imgStore.PutBlobChunk(repoName, uuid, 0, int64(buflen), buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func FuzzPutBlobChunkStreamed(f *testing.F) {
|
|
|
|
f.Fuzz(func(t *testing.T, data string) {
|
|
|
|
dir := t.TempDir()
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
t.Logf("Input argument is %s", data)
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, log)
|
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay, true, true, log, metrics, nil, cacheDriver)
|
2022-07-27 12:37:55 -05:00
|
|
|
|
|
|
|
repoName := data
|
|
|
|
|
|
|
|
uuid, err := imgStore.NewBlobUpload(repoName)
|
|
|
|
if err != nil {
|
|
|
|
if isKnownErr(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer([]byte(data))
|
|
|
|
_, err = imgStore.PutBlobChunkStreamed(repoName, uuid, buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func FuzzGetBlobUpload(f *testing.F) {
|
|
|
|
f.Fuzz(func(t *testing.T, data1 string, data2 string) {
|
|
|
|
dir := t.TempDir()
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, log)
|
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay, true, true, log, metrics, nil, cacheDriver)
|
2022-07-27 12:37:55 -05:00
|
|
|
|
|
|
|
_, err := imgStore.GetBlobUpload(data1, data2)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, zerr.ErrUploadNotFound) || isKnownErr(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func FuzzTestPutGetImageManifest(f *testing.F) {
|
|
|
|
f.Fuzz(func(t *testing.T, data []byte) {
|
|
|
|
log := &log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, *log)
|
|
|
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, *log)
|
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay, true, true, *log, metrics, nil, cacheDriver)
|
2022-07-27 12:37:55 -05:00
|
|
|
|
|
|
|
cblob, cdigest := test.GetRandomImageConfig()
|
|
|
|
|
|
|
|
ldigest, lblob, err := newRandomBlobForFuzz(data)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error occurred while generating random blob, %v", err)
|
|
|
|
}
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
_, _, err = imgStore.FullBlobUpload(repoName, bytes.NewReader(cblob), cdigest)
|
2022-07-27 12:37:55 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2022-10-22 15:46:13 -05:00
|
|
|
_, _, err = imgStore.FullBlobUpload(repoName, bytes.NewReader(lblob), ldigest)
|
2022-07-27 12:37:55 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
manifest, err := NewRandomImgManifest(data, cdigest, ldigest, cblob, lblob)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
manifestBuf, err := json.Marshal(manifest)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error %v occurred while marshaling manifest", err)
|
|
|
|
}
|
|
|
|
mdigest := godigest.FromBytes(manifestBuf)
|
|
|
|
_, err = imgStore.PutImageManifest(repoName, mdigest.String(), ispec.MediaTypeImageManifest, manifestBuf)
|
|
|
|
if err != nil && errors.Is(err, zerr.ErrBadManifest) {
|
|
|
|
t.Errorf("the error that occurred is %v \n", err)
|
|
|
|
}
|
|
|
|
_, _, _, err = imgStore.GetImageManifest(repoName, mdigest.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("the error that occurred is %v \n", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func FuzzTestPutDeleteImageManifest(f *testing.F) {
|
|
|
|
f.Fuzz(func(t *testing.T, data []byte) {
|
|
|
|
log := &log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, *log)
|
|
|
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, *log)
|
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay, true, true, *log, metrics, nil, cacheDriver)
|
2022-07-27 12:37:55 -05:00
|
|
|
|
|
|
|
cblob, cdigest := test.GetRandomImageConfig()
|
|
|
|
|
|
|
|
ldigest, lblob, err := newRandomBlobForFuzz(data)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error occurred while generating random blob, %v", err)
|
|
|
|
}
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
_, _, err = imgStore.FullBlobUpload(repoName, bytes.NewReader(cblob), cdigest)
|
2022-07-27 12:37:55 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
_, _, err = imgStore.FullBlobUpload(repoName, bytes.NewReader(lblob), ldigest)
|
2022-07-27 12:37:55 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
manifest, err := NewRandomImgManifest(data, cdigest, ldigest, cblob, lblob)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
manifestBuf, err := json.Marshal(manifest)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error %v occurred while marshaling manifest", err)
|
|
|
|
}
|
|
|
|
mdigest := godigest.FromBytes(manifestBuf)
|
|
|
|
_, err = imgStore.PutImageManifest(repoName, mdigest.String(), ispec.MediaTypeImageManifest, manifestBuf)
|
|
|
|
if err != nil && errors.Is(err, zerr.ErrBadManifest) {
|
|
|
|
t.Errorf("the error that occurred is %v \n", err)
|
|
|
|
}
|
|
|
|
|
2022-11-18 12:35:28 -05:00
|
|
|
err = imgStore.DeleteImageManifest(repoName, mdigest.String(), false)
|
2022-07-27 12:37:55 -05:00
|
|
|
if err != nil {
|
|
|
|
if isKnownErr(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Errorf("the error that occurred is %v \n", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// no integration with PutImageManifest, just throw fuzz data.
|
|
|
|
func FuzzTestDeleteImageManifest(f *testing.F) {
|
|
|
|
f.Fuzz(func(t *testing.T, data []byte) {
|
|
|
|
log := &log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, *log)
|
|
|
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, *log)
|
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay, true, true, *log, metrics, nil, cacheDriver)
|
2022-07-27 12:37:55 -05:00
|
|
|
|
|
|
|
digest, _, err := newRandomBlobForFuzz(data)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2022-11-18 12:35:28 -05:00
|
|
|
err = imgStore.DeleteImageManifest(string(data), digest.String(), false)
|
2022-07-27 12:37:55 -05:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, zerr.ErrRepoNotFound) || isKnownErr(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func FuzzDirExists(f *testing.F) {
|
2022-10-05 05:21:14 -05:00
|
|
|
f.Fuzz(func(t *testing.T, data string) {
|
2022-12-22 13:19:42 -05:00
|
|
|
_ = common.DirExists(data)
|
2022-07-27 12:37:55 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func FuzzInitRepo(f *testing.F) {
|
|
|
|
f.Fuzz(func(t *testing.T, data string) {
|
|
|
|
log := &log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, *log)
|
|
|
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, *log)
|
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay, true, true, *log, metrics, nil, cacheDriver)
|
2022-07-27 12:37:55 -05:00
|
|
|
err := imgStore.InitRepo(data)
|
|
|
|
if err != nil {
|
|
|
|
if isKnownErr(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func FuzzInitValidateRepo(f *testing.F) {
|
|
|
|
f.Fuzz(func(t *testing.T, data string) {
|
|
|
|
log := &log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, *log)
|
|
|
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, *log)
|
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay, true, true, *log, metrics, nil, cacheDriver)
|
2022-07-27 12:37:55 -05:00
|
|
|
err := imgStore.InitRepo(data)
|
|
|
|
if err != nil {
|
|
|
|
if isKnownErr(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
_, err = imgStore.ValidateRepo(data)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, zerr.ErrRepoNotFound) || errors.Is(err, zerr.ErrRepoBadVersion) || isKnownErr(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func FuzzGetImageTags(f *testing.F) {
|
|
|
|
f.Fuzz(func(t *testing.T, data string) {
|
|
|
|
log := &log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, *log)
|
|
|
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, *log)
|
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay, true, true, *log, metrics, nil, cacheDriver)
|
2022-07-27 12:37:55 -05:00
|
|
|
_, err := imgStore.GetImageTags(data)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, zerr.ErrRepoNotFound) || isKnownErr(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func FuzzBlobUploadPath(f *testing.F) {
|
|
|
|
f.Fuzz(func(t *testing.T, repo, uuid string) {
|
|
|
|
log := &log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, *log)
|
|
|
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, *log)
|
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay, true, true, *log, metrics, nil, cacheDriver)
|
2022-07-27 12:37:55 -05:00
|
|
|
|
|
|
|
_ = imgStore.BlobUploadPath(repo, uuid)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func FuzzBlobUploadInfo(f *testing.F) {
|
|
|
|
f.Fuzz(func(t *testing.T, data string, uuid string) {
|
|
|
|
log := &log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, *log)
|
|
|
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, *log)
|
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay, true, true, *log, metrics, nil, cacheDriver)
|
2022-07-27 12:37:55 -05:00
|
|
|
repo := data
|
|
|
|
|
|
|
|
_, err := imgStore.BlobUploadInfo(repo, uuid)
|
|
|
|
if err != nil {
|
|
|
|
if isKnownErr(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func FuzzTestGetImageManifest(f *testing.F) {
|
|
|
|
f.Fuzz(func(t *testing.T, data string) {
|
|
|
|
dir := t.TempDir()
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, log)
|
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay, true, true, log, metrics, nil, cacheDriver)
|
2022-07-27 12:37:55 -05:00
|
|
|
|
|
|
|
repoName := data
|
|
|
|
|
|
|
|
digest := godigest.FromBytes([]byte(data))
|
|
|
|
|
|
|
|
_, _, _, err := imgStore.GetImageManifest(repoName, digest.String())
|
|
|
|
if err != nil {
|
|
|
|
if isKnownErr(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func FuzzFinishBlobUpload(f *testing.F) {
|
|
|
|
f.Fuzz(func(t *testing.T, data string) {
|
|
|
|
dir := t.TempDir()
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, log)
|
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay, true, true, log, metrics, nil, cacheDriver)
|
2022-07-27 12:37:55 -05:00
|
|
|
|
|
|
|
repoName := data
|
|
|
|
|
|
|
|
upload, err := imgStore.NewBlobUpload(repoName)
|
|
|
|
if err != nil {
|
|
|
|
if isKnownErr(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
content := []byte(data)
|
|
|
|
buf := bytes.NewBuffer(content)
|
|
|
|
buflen := buf.Len()
|
|
|
|
digest := godigest.FromBytes(content)
|
|
|
|
|
|
|
|
_, err = imgStore.PutBlobChunk(repoName, upload, 0, int64(buflen), buf)
|
|
|
|
if err != nil {
|
|
|
|
if isKnownErr(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
err = imgStore.FinishBlobUpload(repoName, upload, buf, digest)
|
2022-07-27 12:37:55 -05:00
|
|
|
if err != nil {
|
|
|
|
if isKnownErr(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func FuzzFullBlobUpload(f *testing.F) {
|
|
|
|
f.Fuzz(func(t *testing.T, data []byte) {
|
|
|
|
log := &log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, *log)
|
|
|
|
repoName := "test"
|
|
|
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, *log)
|
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay, true, true, *log, metrics, nil, cacheDriver)
|
2022-07-27 12:37:55 -05:00
|
|
|
|
|
|
|
ldigest, lblob, err := newRandomBlobForFuzz(data)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error occurred while generating random blob, %v", err)
|
|
|
|
}
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
_, _, err = imgStore.FullBlobUpload(repoName, bytes.NewReader(lblob), ldigest)
|
2022-07-27 12:37:55 -05:00
|
|
|
if err != nil {
|
|
|
|
if isKnownErr(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func FuzzDedupeBlob(f *testing.F) {
|
|
|
|
f.Fuzz(func(t *testing.T, data string) {
|
|
|
|
log := &log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, *log)
|
|
|
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, *log)
|
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay, true, true, *log, metrics, nil, cacheDriver)
|
2022-07-27 12:37:55 -05:00
|
|
|
|
|
|
|
blobDigest := godigest.FromString(data)
|
|
|
|
|
|
|
|
// replacement for .uploads folder, usually retrieved from BlobUploadPath
|
|
|
|
src := path.Join(imgStore.RootDir(), "src")
|
|
|
|
blob := bytes.NewReader([]byte(data))
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
_, _, err := imgStore.FullBlobUpload("repoName", blob, blobDigest)
|
2022-07-27 12:37:55 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dst := imgStore.BlobPath("repoName", blobDigest)
|
|
|
|
|
|
|
|
err = os.MkdirAll(src, 0o755)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = imgStore.DedupeBlob(src, blobDigest, dst)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func FuzzDeleteBlobUpload(f *testing.F) {
|
|
|
|
f.Fuzz(func(t *testing.T, data string) {
|
|
|
|
log := &log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, *log)
|
|
|
|
repoName := data
|
|
|
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, *log)
|
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay, true, true, *log, metrics, nil, cacheDriver)
|
2022-07-27 12:37:55 -05:00
|
|
|
|
|
|
|
uuid, err := imgStore.NewBlobUpload(repoName)
|
|
|
|
if err != nil {
|
|
|
|
if isKnownErr(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = imgStore.DeleteBlobUpload(repoName, uuid)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func FuzzBlobPath(f *testing.F) {
|
|
|
|
f.Fuzz(func(t *testing.T, data string) {
|
|
|
|
log := &log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, *log)
|
|
|
|
repoName := data
|
|
|
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, *log)
|
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay, true, true, *log, metrics, nil, cacheDriver)
|
2022-07-27 12:37:55 -05:00
|
|
|
digest := godigest.FromString(data)
|
|
|
|
|
|
|
|
_ = imgStore.BlobPath(repoName, digest)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func FuzzCheckBlob(f *testing.F) {
|
|
|
|
f.Fuzz(func(t *testing.T, data string) {
|
|
|
|
log := &log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, *log)
|
|
|
|
repoName := data
|
|
|
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, *log)
|
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay, true, true, *log, metrics, nil, cacheDriver)
|
2022-07-27 12:37:55 -05:00
|
|
|
digest := godigest.FromString(data)
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
_, _, err := imgStore.FullBlobUpload(repoName, bytes.NewReader([]byte(data)), digest)
|
2022-07-27 12:37:55 -05:00
|
|
|
if err != nil {
|
|
|
|
if isKnownErr(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2022-10-22 15:46:13 -05:00
|
|
|
_, _, err = imgStore.CheckBlob(repoName, digest)
|
2022-07-27 12:37:55 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func FuzzGetBlob(f *testing.F) {
|
|
|
|
f.Fuzz(func(t *testing.T, data string) {
|
|
|
|
log := &log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, *log)
|
|
|
|
repoName := data
|
|
|
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, *log)
|
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay, true, true, *log, metrics, nil, cacheDriver)
|
2022-07-27 12:37:55 -05:00
|
|
|
digest := godigest.FromString(data)
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
_, _, err := imgStore.FullBlobUpload(repoName, bytes.NewReader([]byte(data)), digest)
|
2022-07-27 12:37:55 -05:00
|
|
|
if err != nil {
|
|
|
|
if isKnownErr(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
blobReadCloser, _, err := imgStore.GetBlob(repoName, digest, "application/vnd.oci.image.layer.v1.tar+gzip")
|
2022-07-27 12:37:55 -05:00
|
|
|
if err != nil {
|
|
|
|
if isKnownErr(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2022-08-19 05:38:59 -05:00
|
|
|
if err = blobReadCloser.Close(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2022-07-27 12:37:55 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func FuzzDeleteBlob(f *testing.F) {
|
|
|
|
f.Fuzz(func(t *testing.T, data string) {
|
|
|
|
log := &log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, *log)
|
|
|
|
repoName := data
|
|
|
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, *log)
|
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay, true, true, *log, metrics, nil, cacheDriver)
|
2022-07-27 12:37:55 -05:00
|
|
|
digest := godigest.FromString(data)
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
_, _, err := imgStore.FullBlobUpload(repoName, bytes.NewReader([]byte(data)), digest)
|
2022-07-27 12:37:55 -05:00
|
|
|
if err != nil {
|
|
|
|
if isKnownErr(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
err = imgStore.DeleteBlob(repoName, digest)
|
2022-07-27 12:37:55 -05:00
|
|
|
if err != nil {
|
|
|
|
if isKnownErr(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func FuzzGetIndexContent(f *testing.F) {
|
|
|
|
f.Fuzz(func(t *testing.T, data string) {
|
|
|
|
log := &log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, *log)
|
|
|
|
repoName := data
|
|
|
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, *log)
|
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay, true, true, *log, metrics, nil, cacheDriver)
|
2022-07-27 12:37:55 -05:00
|
|
|
digest := godigest.FromString(data)
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
_, _, err := imgStore.FullBlobUpload(repoName, bytes.NewReader([]byte(data)), digest)
|
2022-07-27 12:37:55 -05:00
|
|
|
if err != nil {
|
|
|
|
if isKnownErr(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = imgStore.GetIndexContent(repoName)
|
|
|
|
if err != nil {
|
|
|
|
if isKnownErr(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func FuzzGetBlobContent(f *testing.F) {
|
|
|
|
f.Fuzz(func(t *testing.T, data string) {
|
|
|
|
log := &log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, *log)
|
|
|
|
repoName := data
|
|
|
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, *log)
|
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay, true, true, *log, metrics, nil, cacheDriver)
|
2022-07-27 12:37:55 -05:00
|
|
|
digest := godigest.FromString(data)
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
_, _, err := imgStore.FullBlobUpload(repoName, bytes.NewReader([]byte(data)), digest)
|
2022-07-27 12:37:55 -05:00
|
|
|
if err != nil {
|
|
|
|
if isKnownErr(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
_, err = imgStore.GetBlobContent(repoName, digest)
|
2022-07-27 12:37:55 -05:00
|
|
|
if err != nil {
|
|
|
|
if isKnownErr(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-11-08 03:38:16 -05:00
|
|
|
func FuzzGetOrasReferrers(f *testing.F) {
|
2022-07-27 12:37:55 -05:00
|
|
|
f.Fuzz(func(t *testing.T, data string) {
|
|
|
|
log := &log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, *log)
|
|
|
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, *log)
|
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay, true, true, *log, metrics, nil, cacheDriver)
|
2022-07-27 12:37:55 -05:00
|
|
|
|
2022-09-30 12:35:16 -05:00
|
|
|
err := test.CopyFiles("../../../test/data/zot-test", path.Join(dir, "zot-test"))
|
2022-07-27 12:37:55 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
digest := godigest.FromBytes([]byte(data))
|
|
|
|
buf := bytes.NewBuffer([]byte(data))
|
|
|
|
buflen := buf.Len()
|
2022-09-02 07:56:02 -05:00
|
|
|
err = os.WriteFile(path.Join(imgStore.RootDir(), //nolint: gosec
|
2022-07-27 12:37:55 -05:00
|
|
|
"zot-test", "blobs", digest.Algorithm().String(), digest.Encoded()),
|
|
|
|
buf.Bytes(), 0o644)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2022-10-22 15:46:13 -05:00
|
|
|
_, _, err = imgStore.FullBlobUpload("zot-test", buf, digest)
|
2022-07-27 12:37:55 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
artifactManifest := artifactspec.Manifest{}
|
|
|
|
artifactManifest.ArtifactType = data
|
2022-08-20 20:18:50 -05:00
|
|
|
artifactManifest.Subject = &artifactspec.Descriptor{
|
2022-07-27 12:37:55 -05:00
|
|
|
MediaType: ispec.MediaTypeImageManifest,
|
|
|
|
Digest: digest,
|
|
|
|
Size: int64(buflen),
|
|
|
|
}
|
|
|
|
artifactManifest.Blobs = []artifactspec.Descriptor{}
|
|
|
|
|
|
|
|
manBuf, err := json.Marshal(artifactManifest)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
manDigest := godigest.FromBytes(manBuf)
|
|
|
|
_, err = imgStore.PutImageManifest("zot-test", manDigest.Encoded(), artifactspec.MediaTypeArtifactManifest, manBuf)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2022-11-08 03:38:16 -05:00
|
|
|
_, err = imgStore.GetOrasReferrers("zot-test", digest, data)
|
2022-07-27 12:37:55 -05:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, zerr.ErrManifestNotFound) || isKnownErr(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func FuzzRunGCRepo(f *testing.F) {
|
|
|
|
f.Fuzz(func(t *testing.T, data string) {
|
|
|
|
log := &log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, *log)
|
|
|
|
dir := t.TempDir()
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, *log)
|
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay, true, true, *log, metrics, nil, cacheDriver)
|
2022-07-27 12:37:55 -05:00
|
|
|
|
2022-09-23 00:27:56 -05:00
|
|
|
if err := imgStore.RunGCRepo(data); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2022-07-27 12:37:55 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
func TestDedupeLinks(t *testing.T) {
|
2022-03-07 03:55:12 -05:00
|
|
|
dir := t.TempDir()
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, log)
|
2022-11-24 14:00:22 -05:00
|
|
|
imgStore := local.NewImageStore(dir, false, storage.DefaultGCDelay,
|
2022-11-02 17:53:08 -05:00
|
|
|
true, true, log, metrics, nil, cacheDriver)
|
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)
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
err = imgStore.FinishBlobUpload("dedupe1", upload, buf, digest)
|
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
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
_, _, err = imgStore.CheckBlob("dedupe1", digest)
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
blobrc, _, err := imgStore.GetBlob("dedupe1", digest, "application/vnd.oci.image.layer.v1.tar+gzip")
|
2022-08-19 05:38:59 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = blobrc.Close()
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2022-01-19 14:54:17 -05:00
|
|
|
cblob, cdigest := test.GetRandomImageConfig()
|
2022-10-22 15:46:13 -05:00
|
|
|
_, clen, err := imgStore.FullBlobUpload("dedupe1", bytes.NewReader(cblob), cdigest)
|
2022-01-19 14:54:17 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(clen, ShouldEqual, len(cblob))
|
2022-10-22 15:46:13 -05:00
|
|
|
hasBlob, _, err := imgStore.CheckBlob("dedupe1", cdigest)
|
2022-01-19 14:54:17 -05:00
|
|
|
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
|
2022-03-21 12:37:23 -05:00
|
|
|
manifestBuf, err := json.Marshal(manifest)
|
|
|
|
So(err, ShouldBeNil)
|
2021-12-13 14:23:31 -05:00
|
|
|
digest = godigest.FromBytes(manifestBuf)
|
2022-06-24 08:08:47 -05:00
|
|
|
_, 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)
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
err = imgStore.FinishBlobUpload("dedupe2", upload, buf, digest)
|
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
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
_, _, err = imgStore.CheckBlob("dedupe2", digest)
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
blobrc, _, err = imgStore.GetBlob("dedupe2", digest, "application/vnd.oci.image.layer.v1.tar+gzip")
|
2022-08-19 05:38:59 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = blobrc.Close()
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2022-01-19 14:54:17 -05:00
|
|
|
cblob, cdigest = test.GetRandomImageConfig()
|
2022-10-22 15:46:13 -05:00
|
|
|
_, clen, err = imgStore.FullBlobUpload("dedupe2", bytes.NewReader(cblob), cdigest)
|
2022-01-19 14:54:17 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(clen, ShouldEqual, len(cblob))
|
2022-10-22 15:46:13 -05:00
|
|
|
hasBlob, _, err = imgStore.CheckBlob("dedupe2", cdigest)
|
2022-01-19 14:54:17 -05:00
|
|
|
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
|
2022-03-21 12:37:23 -05:00
|
|
|
manifestBuf, err = json.Marshal(manifest)
|
|
|
|
So(err, ShouldBeNil)
|
2021-12-13 14:23:31 -05:00
|
|
|
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)
|
2022-09-08 10:07:00 -05:00
|
|
|
|
|
|
|
Convey("storage and cache inconsistency", func() {
|
|
|
|
// delete blobs
|
|
|
|
err = os.Remove(path.Join(dir, "dedupe1", "blobs", "sha256", blobDigest1))
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err := os.Remove(path.Join(dir, "dedupe2", "blobs", "sha256", blobDigest2))
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
// now cache is inconsistent with storage (blobs present in cache but not in storage)
|
|
|
|
upload, err = imgStore.NewBlobUpload("dedupe3")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(upload, ShouldNotBeEmpty)
|
|
|
|
|
|
|
|
content = []byte("test-data3")
|
|
|
|
buf = bytes.NewBuffer(content)
|
|
|
|
buflen = buf.Len()
|
|
|
|
digest = godigest.FromBytes(content)
|
|
|
|
blob, err = imgStore.PutBlobChunkStreamed("dedupe3", upload, buf)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(blob, ShouldEqual, buflen)
|
|
|
|
blobDigest2 := strings.Split(digest.String(), ":")[1]
|
|
|
|
So(blobDigest2, ShouldNotBeEmpty)
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
err = imgStore.FinishBlobUpload("dedupe3", upload, buf, digest)
|
2022-09-08 10:07:00 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(blob, ShouldEqual, buflen)
|
|
|
|
})
|
2021-07-16 22:53:05 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2022-03-07 03:55:12 -05:00
|
|
|
dir := t.TempDir()
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, log)
|
|
|
|
il := local.NewImageStore(dir, true, storage.DefaultGCDelay, true, true, log, metrics, nil, cacheDriver)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
So(il.DedupeBlob("", "", ""), ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-05 05:21:14 -05:00
|
|
|
//nolint:gocyclo
|
2021-07-16 22:53:05 -05:00
|
|
|
func TestNegativeCases(t *testing.T) {
|
|
|
|
Convey("Invalid root dir", t, func(c C) {
|
2022-03-07 03:55:12 -05:00
|
|
|
dir := t.TempDir()
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, log)
|
2022-09-30 12:35:16 -05:00
|
|
|
So(local.NewImageStore(dir, true, storage.DefaultGCDelay, true,
|
2022-11-02 17:53:08 -05:00
|
|
|
true, log, metrics, nil, cacheDriver), ShouldNotBeNil)
|
2021-07-16 22:53:05 -05:00
|
|
|
if os.Geteuid() != 0 {
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: "/deadBEEF",
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, log)
|
2022-09-30 12:35:16 -05:00
|
|
|
So(local.NewImageStore("/deadBEEF", true, storage.DefaultGCDelay,
|
2022-11-02 17:53:08 -05:00
|
|
|
true, true, log, metrics, nil, cacheDriver), ShouldBeNil)
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Invalid init repo", t, func(c C) {
|
2022-03-07 03:55:12 -05:00
|
|
|
dir := t.TempDir()
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, log)
|
2022-09-30 12:35:16 -05:00
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay,
|
2022-11-02 17:53:08 -05:00
|
|
|
true, true, log, metrics, nil, cacheDriver)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2022-03-07 03:55:12 -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.
|
2022-10-05 05:21:14 -05:00
|
|
|
err = os.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)
|
2022-07-27 12:37:55 -05:00
|
|
|
|
|
|
|
// Init repo should fail if repo is invalid UTF-8
|
|
|
|
err = imgStore.InitRepo("hi \255")
|
|
|
|
So(err, ShouldNotBeNil)
|
2023-01-18 11:24:44 -05:00
|
|
|
|
|
|
|
// Init repo should fail if repo name does not match spec
|
|
|
|
err = imgStore.InitRepo("_trivy")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(errors.Is(err, zerr.ErrInvalidRepositoryName), ShouldBeTrue)
|
2021-07-16 22:53:05 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Invalid validate repo", t, func(c C) {
|
2022-03-07 03:55:12 -05:00
|
|
|
dir := t.TempDir()
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, log)
|
2022-09-30 12:35:16 -05:00
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay, true,
|
2022-11-02 17:53:08 -05:00
|
|
|
true, log, metrics, nil, cacheDriver)
|
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
|
|
|
|
2022-03-07 03:55:12 -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)
|
2022-07-27 12:37:55 -05:00
|
|
|
So(err, ShouldEqual, zerr.ErrRepoNotFound)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-10-05 05:21:14 -05:00
|
|
|
err = os.WriteFile(path.Join(dir, "invalid-test", "blobs"), []byte{}, 0o755) //nolint: gosec
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-10-05 05:21:14 -05:00
|
|
|
err = os.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)
|
|
|
|
}
|
|
|
|
|
2022-10-05 05:21:14 -05:00
|
|
|
err = os.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)
|
|
|
|
|
2022-10-05 05:21:14 -05:00
|
|
|
err = os.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)
|
2022-07-27 12:37:55 -05:00
|
|
|
So(err, ShouldEqual, zerr.ErrRepoBadVersion)
|
2021-07-16 22:53:05 -05:00
|
|
|
So(isValid, ShouldEqual, false)
|
|
|
|
|
2022-09-02 07:56:02 -05:00
|
|
|
files, err := os.ReadDir(path.Join(dir, "test"))
|
2021-07-16 22:53:05 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-01-18 11:24:44 -05:00
|
|
|
err = os.Chmod(dir, 0o755) // add 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) {
|
2022-09-30 12:35:16 -05:00
|
|
|
var ilfs local.ImageStoreLocal
|
2021-07-16 22:53:05 -05:00
|
|
|
_, err := ilfs.GetImageTags("test")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
2022-03-07 03:55:12 -05:00
|
|
|
dir := t.TempDir()
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, log)
|
2022-09-30 12:35:16 -05:00
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay,
|
2022-11-02 17:53:08 -05:00
|
|
|
true, true, log, metrics, nil, cacheDriver)
|
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)
|
2022-09-02 07:56:02 -05:00
|
|
|
So(os.WriteFile(path.Join(dir, "test", "index.json"), []byte{}, 0o600), ShouldBeNil)
|
2021-12-13 14:23:31 -05:00
|
|
|
_, err = imgStore.GetImageTags("test")
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Invalid get image manifest", t, func(c C) {
|
2022-09-30 12:35:16 -05:00
|
|
|
var ilfs local.ImageStoreLocal
|
2021-07-16 22:53:05 -05:00
|
|
|
_, _, _, err := ilfs.GetImageManifest("test", "")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
2022-03-07 03:55:12 -05:00
|
|
|
dir := t.TempDir()
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, log)
|
2022-09-30 12:35:16 -05:00
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay, true,
|
2022-11-02 17:53:08 -05:00
|
|
|
true, log, metrics, nil, cacheDriver)
|
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
|
|
|
|
2022-09-02 07:56:02 -05:00
|
|
|
err = os.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) {
|
2022-03-07 03:55:12 -05:00
|
|
|
dir := t.TempDir()
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, log)
|
2022-09-30 12:35:16 -05:00
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay,
|
2022-11-02 17:53:08 -05:00
|
|
|
true, true, log, metrics, nil, cacheDriver)
|
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
|
|
|
|
2022-03-07 03:55:12 -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)
|
|
|
|
}
|
2022-03-07 03:55:12 -05:00
|
|
|
t.Cleanup(func() {
|
|
|
|
err = os.Chmod(path.Join(dir, "test", ".uploads"), 0o700)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
})
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
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("DirExists call with a filename as argument", t, func(c C) {
|
2022-03-07 03:55:12 -05:00
|
|
|
dir := t.TempDir()
|
2021-11-10 09:31:03 -05:00
|
|
|
|
|
|
|
filePath := path.Join(dir, "file.txt")
|
2022-09-02 07:56:02 -05:00
|
|
|
err := os.WriteFile(filePath, []byte("some dummy file content"), 0o644) //nolint: gosec
|
2021-11-10 09:31:03 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-12-22 13:19:42 -05:00
|
|
|
ok := common.DirExists(filePath)
|
2021-11-10 09:31:03 -05:00
|
|
|
So(ok, ShouldBeFalse)
|
|
|
|
})
|
2022-07-27 12:37:55 -05:00
|
|
|
|
|
|
|
Convey("DirExists call with invalid UTF-8 as argument", t, func(c C) {
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
|
|
|
filePath := path.Join(dir, "hi \255")
|
2022-12-22 13:19:42 -05:00
|
|
|
ok := common.DirExists(filePath)
|
2022-07-27 12:37:55 -05:00
|
|
|
So(ok, ShouldBeFalse)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("DirExists call with name too long as argument", t, func(c C) {
|
|
|
|
var builder strings.Builder
|
|
|
|
for i := 0; i < 1025; i++ {
|
|
|
|
_, err := builder.WriteString("0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
path := builder.String()
|
2022-12-22 13:19:42 -05:00
|
|
|
ok := common.DirExists(path)
|
2022-07-27 12:37:55 -05:00
|
|
|
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)
|
|
|
|
|
2022-09-30 12:35:16 -05:00
|
|
|
err := local.ValidateHardLink(randomDir)
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
Convey("Test that ValidateHardLink returns error if rootDir is a file", t, func() {
|
2022-03-07 03:55:12 -05:00
|
|
|
dir := t.TempDir()
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
filePath := path.Join(dir, "file.txt")
|
2022-09-02 07:56:02 -05:00
|
|
|
err := os.WriteFile(filePath, []byte("some dummy file content"), 0o644) //nolint: gosec
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-09-30 12:35:16 -05:00
|
|
|
err = local.ValidateHardLink(filePath)
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
Convey("Test if filesystem supports hardlink", t, func() {
|
2022-03-07 03:55:12 -05:00
|
|
|
dir := t.TempDir()
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2022-09-30 12:35:16 -05:00
|
|
|
err := local.ValidateHardLink(dir)
|
2021-07-16 22:53:05 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2022-09-02 07:56:02 -05:00
|
|
|
err = os.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)
|
|
|
|
}
|
2022-03-07 03:55:12 -05:00
|
|
|
// Allow hardtest.txt to be cleaned up by t.TempDir()
|
|
|
|
t.Cleanup(func() {
|
|
|
|
err = os.Chmod(dir, 0o700)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
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-02-18 06:36:50 -05:00
|
|
|
func TestInjectWriteFile(t *testing.T) {
|
2022-01-20 23:11:44 -05:00
|
|
|
Convey("writeFile with commit", t, func() {
|
2022-03-07 03:55:12 -05:00
|
|
|
dir := t.TempDir()
|
2022-01-20 23:11:44 -05:00
|
|
|
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, log)
|
2022-09-30 12:35:16 -05:00
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay,
|
2022-11-02 17:53:08 -05:00
|
|
|
true, true, log, metrics, nil, cacheDriver)
|
2022-01-20 23:11:44 -05:00
|
|
|
|
|
|
|
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() {
|
2022-03-07 03:55:12 -05:00
|
|
|
dir := t.TempDir()
|
2022-01-20 23:11:44 -05:00
|
|
|
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, log)
|
2022-09-30 12:35:16 -05:00
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay,
|
2022-11-02 17:53:08 -05:00
|
|
|
true, false, log, metrics, nil, cacheDriver)
|
2022-01-20 23:11:44 -05:00
|
|
|
|
|
|
|
Convey("Failure path not reached", func() {
|
|
|
|
err := imgStore.InitRepo("repo1")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-09 19:51:35 -05:00
|
|
|
func TestGarbageCollect(t *testing.T) {
|
|
|
|
Convey("Repo layout", t, func(c C) {
|
2022-03-07 03:55:12 -05:00
|
|
|
dir := t.TempDir()
|
2022-02-09 19:51:35 -05:00
|
|
|
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
|
|
|
|
|
|
|
Convey("Garbage collect with default/long delay", func() {
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, log)
|
2022-09-30 12:35:16 -05:00
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay,
|
2022-11-02 17:53:08 -05:00
|
|
|
true, true, log, metrics, nil, cacheDriver)
|
2022-02-09 19:51:35 -05:00
|
|
|
repoName := "gc-long"
|
|
|
|
|
|
|
|
upload, err := imgStore.NewBlobUpload(repoName)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(upload, ShouldNotBeEmpty)
|
|
|
|
|
|
|
|
content := []byte("test-data1")
|
|
|
|
buf := bytes.NewBuffer(content)
|
|
|
|
buflen := buf.Len()
|
|
|
|
bdigest := godigest.FromBytes(content)
|
|
|
|
|
|
|
|
blob, err := imgStore.PutBlobChunk(repoName, upload, 0, int64(buflen), buf)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(blob, ShouldEqual, buflen)
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
err = imgStore.FinishBlobUpload(repoName, upload, buf, bdigest)
|
2022-02-09 19:51:35 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
annotationsMap := make(map[string]string)
|
|
|
|
annotationsMap[ispec.AnnotationRefName] = tag
|
|
|
|
|
|
|
|
cblob, cdigest := test.GetRandomImageConfig()
|
2022-10-22 15:46:13 -05:00
|
|
|
_, clen, err := imgStore.FullBlobUpload(repoName, bytes.NewReader(cblob), cdigest)
|
2022-02-09 19:51:35 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(clen, ShouldEqual, len(cblob))
|
2022-10-22 15:46:13 -05:00
|
|
|
hasBlob, _, err := imgStore.CheckBlob(repoName, cdigest)
|
2022-02-09 19:51:35 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(hasBlob, ShouldEqual, true)
|
|
|
|
|
|
|
|
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: bdigest,
|
|
|
|
Size: int64(buflen),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Annotations: annotationsMap,
|
|
|
|
}
|
|
|
|
|
|
|
|
manifest.SchemaVersion = 2
|
2022-03-21 12:37:23 -05:00
|
|
|
manifestBuf, err := json.Marshal(manifest)
|
|
|
|
So(err, ShouldBeNil)
|
2022-02-09 19:51:35 -05:00
|
|
|
digest := godigest.FromBytes(manifestBuf)
|
|
|
|
|
|
|
|
_, err = imgStore.PutImageManifest(repoName, tag, ispec.MediaTypeImageManifest, manifestBuf)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
hasBlob, _, err = imgStore.CheckBlob(repoName, bdigest)
|
2022-02-09 19:51:35 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(hasBlob, ShouldEqual, true)
|
|
|
|
|
2022-11-18 12:35:28 -05:00
|
|
|
err = imgStore.DeleteImageManifest(repoName, digest.String(), false)
|
2022-02-09 19:51:35 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
hasBlob, _, err = imgStore.CheckBlob(repoName, bdigest)
|
2022-02-09 19:51:35 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(hasBlob, ShouldEqual, true)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Garbage collect with short delay", func() {
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, log)
|
|
|
|
imgStore := local.NewImageStore(dir, true, 1*time.Second, true, true, log, metrics, nil, cacheDriver)
|
2022-02-09 19:51:35 -05:00
|
|
|
repoName := "gc-short"
|
|
|
|
|
|
|
|
// upload orphan blob
|
|
|
|
upload, err := imgStore.NewBlobUpload(repoName)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(upload, ShouldNotBeEmpty)
|
|
|
|
|
|
|
|
content := []byte("test-data1")
|
|
|
|
buf := bytes.NewBuffer(content)
|
|
|
|
buflen := buf.Len()
|
|
|
|
odigest := godigest.FromBytes(content)
|
|
|
|
|
|
|
|
blob, err := imgStore.PutBlobChunk(repoName, upload, 0, int64(buflen), buf)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(blob, ShouldEqual, buflen)
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
err = imgStore.FinishBlobUpload(repoName, upload, buf, odigest)
|
2022-02-09 19:51:35 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
// sleep so orphan blob can be GC'ed
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
|
|
|
// upload blob
|
|
|
|
upload, err = imgStore.NewBlobUpload(repoName)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(upload, ShouldNotBeEmpty)
|
|
|
|
|
|
|
|
content = []byte("test-data2")
|
|
|
|
buf = bytes.NewBuffer(content)
|
|
|
|
buflen = buf.Len()
|
|
|
|
bdigest := godigest.FromBytes(content)
|
|
|
|
|
|
|
|
blob, err = imgStore.PutBlobChunk(repoName, upload, 0, int64(buflen), buf)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(blob, ShouldEqual, buflen)
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
err = imgStore.FinishBlobUpload(repoName, upload, buf, bdigest)
|
2022-02-09 19:51:35 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
annotationsMap := make(map[string]string)
|
|
|
|
annotationsMap[ispec.AnnotationRefName] = tag
|
|
|
|
|
|
|
|
cblob, cdigest := test.GetRandomImageConfig()
|
2022-10-22 15:46:13 -05:00
|
|
|
_, clen, err := imgStore.FullBlobUpload(repoName, bytes.NewReader(cblob), cdigest)
|
2022-02-09 19:51:35 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(clen, ShouldEqual, len(cblob))
|
2022-10-22 15:46:13 -05:00
|
|
|
hasBlob, _, err := imgStore.CheckBlob(repoName, cdigest)
|
2022-02-09 19:51:35 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(hasBlob, ShouldEqual, true)
|
|
|
|
|
|
|
|
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: bdigest,
|
|
|
|
Size: int64(buflen),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Annotations: annotationsMap,
|
|
|
|
}
|
|
|
|
|
|
|
|
manifest.SchemaVersion = 2
|
2022-03-21 12:37:23 -05:00
|
|
|
manifestBuf, err := json.Marshal(manifest)
|
|
|
|
So(err, ShouldBeNil)
|
2022-02-09 19:51:35 -05:00
|
|
|
digest := godigest.FromBytes(manifestBuf)
|
|
|
|
|
|
|
|
_, err = imgStore.PutImageManifest(repoName, tag, ispec.MediaTypeImageManifest, manifestBuf)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
hasBlob, _, err = imgStore.CheckBlob(repoName, odigest)
|
2022-02-09 19:51:35 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(hasBlob, ShouldEqual, false)
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
hasBlob, _, err = imgStore.CheckBlob(repoName, bdigest)
|
2022-02-09 19:51:35 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(hasBlob, ShouldEqual, true)
|
|
|
|
|
|
|
|
// sleep so orphan blob can be GC'ed
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
2022-11-18 12:35:28 -05:00
|
|
|
err = imgStore.DeleteImageManifest(repoName, digest.String(), false)
|
2022-02-09 19:51:35 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
hasBlob, _, err = imgStore.CheckBlob(repoName, bdigest)
|
2022-02-09 19:51:35 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(hasBlob, ShouldEqual, false)
|
|
|
|
})
|
2022-03-24 12:52:39 -05:00
|
|
|
|
|
|
|
Convey("Garbage collect with dedupe", func() {
|
|
|
|
// garbage-collect is repo-local and dedupe is global and they can interact in strange ways
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, log)
|
|
|
|
imgStore := local.NewImageStore(dir, true, 5*time.Second, true, true, log, metrics, nil, cacheDriver)
|
2022-03-24 12:52:39 -05:00
|
|
|
|
|
|
|
// first upload an image to the first repo and wait for GC timeout
|
|
|
|
|
|
|
|
repo1Name := "gc1"
|
|
|
|
|
|
|
|
// upload blob
|
|
|
|
upload, err := imgStore.NewBlobUpload(repo1Name)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(upload, ShouldNotBeEmpty)
|
|
|
|
|
|
|
|
content := []byte("test-data")
|
|
|
|
buf := bytes.NewBuffer(content)
|
|
|
|
buflen := buf.Len()
|
|
|
|
bdigest := godigest.FromBytes(content)
|
|
|
|
tdigest := bdigest
|
|
|
|
|
|
|
|
blob, err := imgStore.PutBlobChunk(repo1Name, upload, 0, int64(buflen), buf)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(blob, ShouldEqual, buflen)
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
err = imgStore.FinishBlobUpload(repo1Name, upload, buf, bdigest)
|
2022-03-24 12:52:39 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
annotationsMap := make(map[string]string)
|
|
|
|
annotationsMap[ispec.AnnotationRefName] = tag
|
|
|
|
|
|
|
|
cblob, cdigest := test.GetRandomImageConfig()
|
2022-10-22 15:46:13 -05:00
|
|
|
_, clen, err := imgStore.FullBlobUpload(repo1Name, bytes.NewReader(cblob), cdigest)
|
2022-03-24 12:52:39 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(clen, ShouldEqual, len(cblob))
|
2022-10-22 15:46:13 -05:00
|
|
|
hasBlob, _, err := imgStore.CheckBlob(repo1Name, cdigest)
|
2022-03-24 12:52:39 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(hasBlob, ShouldEqual, true)
|
|
|
|
|
|
|
|
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: bdigest,
|
|
|
|
Size: int64(buflen),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Annotations: annotationsMap,
|
|
|
|
}
|
|
|
|
|
|
|
|
manifest.SchemaVersion = 2
|
2022-03-21 12:37:23 -05:00
|
|
|
manifestBuf, err := json.Marshal(manifest)
|
|
|
|
So(err, ShouldBeNil)
|
2022-03-24 12:52:39 -05:00
|
|
|
|
|
|
|
_, err = imgStore.PutImageManifest(repo1Name, tag, ispec.MediaTypeImageManifest, manifestBuf)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
hasBlob, _, err = imgStore.CheckBlob(repo1Name, tdigest)
|
2022-03-24 12:52:39 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(hasBlob, ShouldEqual, true)
|
|
|
|
|
|
|
|
// sleep so past GC timeout
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
hasBlob, _, err = imgStore.CheckBlob(repo1Name, tdigest)
|
2022-03-24 12:52:39 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(hasBlob, ShouldEqual, true)
|
|
|
|
|
|
|
|
// upload another image into a second repo with the same blob contents so dedupe is triggered
|
|
|
|
|
|
|
|
repo2Name := "gc2"
|
|
|
|
|
|
|
|
upload, err = imgStore.NewBlobUpload(repo2Name)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(upload, ShouldNotBeEmpty)
|
|
|
|
|
|
|
|
buf = bytes.NewBuffer(content)
|
|
|
|
buflen = buf.Len()
|
|
|
|
|
|
|
|
blob, err = imgStore.PutBlobChunk(repo2Name, upload, 0, int64(buflen), buf)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(blob, ShouldEqual, buflen)
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
err = imgStore.FinishBlobUpload(repo2Name, upload, buf, bdigest)
|
2022-03-24 12:52:39 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
annotationsMap = make(map[string]string)
|
|
|
|
annotationsMap[ispec.AnnotationRefName] = tag
|
|
|
|
|
|
|
|
cblob, cdigest = test.GetRandomImageConfig()
|
2022-10-22 15:46:13 -05:00
|
|
|
_, clen, err = imgStore.FullBlobUpload(repo2Name, bytes.NewReader(cblob), cdigest)
|
2022-03-24 12:52:39 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(clen, ShouldEqual, len(cblob))
|
2022-10-22 15:46:13 -05:00
|
|
|
hasBlob, _, err = imgStore.CheckBlob(repo2Name, cdigest)
|
2022-03-24 12:52:39 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(hasBlob, ShouldEqual, true)
|
|
|
|
|
|
|
|
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: bdigest,
|
|
|
|
Size: int64(buflen),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Annotations: annotationsMap,
|
|
|
|
}
|
|
|
|
|
|
|
|
manifest.SchemaVersion = 2
|
2022-03-21 12:37:23 -05:00
|
|
|
manifestBuf, err = json.Marshal(manifest)
|
|
|
|
So(err, ShouldBeNil)
|
2022-03-24 12:52:39 -05:00
|
|
|
|
|
|
|
_, err = imgStore.PutImageManifest(repo2Name, tag, ispec.MediaTypeImageManifest, manifestBuf)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
hasBlob, _, err = imgStore.CheckBlob(repo2Name, bdigest)
|
2022-03-24 12:52:39 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(hasBlob, ShouldEqual, true)
|
|
|
|
|
|
|
|
// immediately upload any other image to second repo which should invoke GC inline, but expect layers to persist
|
|
|
|
|
|
|
|
upload, err = imgStore.NewBlobUpload(repo2Name)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(upload, ShouldNotBeEmpty)
|
|
|
|
|
|
|
|
content = []byte("test-data-more")
|
|
|
|
buf = bytes.NewBuffer(content)
|
|
|
|
buflen = buf.Len()
|
|
|
|
bdigest = godigest.FromBytes(content)
|
|
|
|
|
|
|
|
blob, err = imgStore.PutBlobChunk(repo2Name, upload, 0, int64(buflen), buf)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(blob, ShouldEqual, buflen)
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
err = imgStore.FinishBlobUpload(repo2Name, upload, buf, bdigest)
|
2022-03-24 12:52:39 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
annotationsMap = make(map[string]string)
|
|
|
|
annotationsMap[ispec.AnnotationRefName] = tag
|
|
|
|
|
|
|
|
cblob, cdigest = test.GetRandomImageConfig()
|
2022-10-22 15:46:13 -05:00
|
|
|
_, clen, err = imgStore.FullBlobUpload(repo2Name, bytes.NewReader(cblob), cdigest)
|
2022-03-24 12:52:39 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(clen, ShouldEqual, len(cblob))
|
2022-10-22 15:46:13 -05:00
|
|
|
hasBlob, _, err = imgStore.CheckBlob(repo2Name, cdigest)
|
2022-03-24 12:52:39 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(hasBlob, ShouldEqual, true)
|
|
|
|
|
|
|
|
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: bdigest,
|
|
|
|
Size: int64(buflen),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Annotations: annotationsMap,
|
|
|
|
}
|
|
|
|
|
|
|
|
manifest.SchemaVersion = 2
|
2022-03-21 12:37:23 -05:00
|
|
|
manifestBuf, err = json.Marshal(manifest)
|
|
|
|
So(err, ShouldBeNil)
|
2022-03-24 12:52:39 -05:00
|
|
|
digest := godigest.FromBytes(manifestBuf)
|
|
|
|
|
|
|
|
_, err = imgStore.PutImageManifest(repo2Name, tag, ispec.MediaTypeImageManifest, manifestBuf)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
// original blob should exist
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
hasBlob, _, err = imgStore.CheckBlob(repo2Name, tdigest)
|
2022-03-24 12:52:39 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(hasBlob, ShouldEqual, true)
|
|
|
|
|
|
|
|
_, _, _, err = imgStore.GetImageManifest(repo2Name, digest.String())
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
2022-02-09 19:51:35 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-21 13:40:37 -05:00
|
|
|
func TestGarbageCollectForImageStore(t *testing.T) {
|
2022-05-09 17:30:11 -05:00
|
|
|
Convey("Garbage collect for a specific repo from an ImageStore", t, func(c C) {
|
2022-03-21 13:40:37 -05:00
|
|
|
dir := t.TempDir()
|
|
|
|
|
|
|
|
Convey("Garbage collect error for repo with config removed", func() {
|
2022-09-02 07:56:02 -05:00
|
|
|
logFile, _ := os.CreateTemp("", "zot-log*.txt")
|
2022-03-21 13:40:37 -05:00
|
|
|
|
|
|
|
defer os.Remove(logFile.Name()) // clean up
|
|
|
|
|
|
|
|
log := log.NewLogger("debug", logFile.Name())
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, log)
|
|
|
|
imgStore := local.NewImageStore(dir, true, 1*time.Second, true, true, log, metrics, nil, cacheDriver)
|
2022-03-21 13:40:37 -05:00
|
|
|
repoName := "gc-all-repos-short"
|
|
|
|
|
2023-01-20 13:01:52 -05:00
|
|
|
test.CopyTestFiles("../../../test/data/zot-test", path.Join(dir, repoName))
|
2022-03-21 13:40:37 -05:00
|
|
|
|
2022-05-10 03:28:26 -05:00
|
|
|
var manifestDigest godigest.Digest
|
2022-09-30 12:35:16 -05:00
|
|
|
manifestDigest, _, _ = test.GetOciLayoutDigests("../../../test/data/zot-test")
|
2023-01-20 13:01:52 -05:00
|
|
|
err := os.Remove(path.Join(dir, repoName, "blobs/sha256", manifestDigest.Encoded()))
|
2022-03-21 13:40:37 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-09-23 00:27:56 -05:00
|
|
|
err = imgStore.RunGCRepo(repoName)
|
|
|
|
So(err, ShouldNotBeNil)
|
2022-03-21 13:40:37 -05:00
|
|
|
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
|
|
|
|
data, err := os.ReadFile(logFile.Name())
|
|
|
|
So(err, ShouldBeNil)
|
2022-05-09 17:30:11 -05:00
|
|
|
So(string(data), ShouldContainSubstring,
|
|
|
|
fmt.Sprintf("error while running GC for %s", path.Join(imgStore.RootDir(), repoName)))
|
2022-03-21 13:40:37 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Garbage collect error - not enough permissions to access index.json", func() {
|
2022-09-02 07:56:02 -05:00
|
|
|
logFile, _ := os.CreateTemp("", "zot-log*.txt")
|
2022-03-21 13:40:37 -05:00
|
|
|
|
|
|
|
defer os.Remove(logFile.Name()) // clean up
|
|
|
|
|
|
|
|
log := log.NewLogger("debug", logFile.Name())
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, log)
|
|
|
|
imgStore := local.NewImageStore(dir, true, 1*time.Second, true, true, log, metrics, nil, cacheDriver)
|
2022-03-21 13:40:37 -05:00
|
|
|
repoName := "gc-all-repos-short"
|
|
|
|
|
2023-01-20 13:01:52 -05:00
|
|
|
test.CopyTestFiles("../../../test/data/zot-test", path.Join(dir, repoName))
|
2022-03-21 13:40:37 -05:00
|
|
|
|
|
|
|
So(os.Chmod(path.Join(dir, repoName, "index.json"), 0o000), ShouldBeNil)
|
|
|
|
|
2023-01-20 13:01:52 -05:00
|
|
|
err := imgStore.RunGCRepo(repoName)
|
2022-09-23 00:27:56 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
2022-03-21 13:40:37 -05:00
|
|
|
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
|
|
|
|
data, err := os.ReadFile(logFile.Name())
|
|
|
|
So(err, ShouldBeNil)
|
2022-05-09 17:30:11 -05:00
|
|
|
So(string(data), ShouldContainSubstring,
|
|
|
|
fmt.Sprintf("error while running GC for %s", path.Join(imgStore.RootDir(), repoName)))
|
2022-03-21 13:40:37 -05:00
|
|
|
So(os.Chmod(path.Join(dir, repoName, "index.json"), 0o755), ShouldBeNil)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-11-24 14:00:22 -05:00
|
|
|
func TestGarbageCollectErrors(t *testing.T) {
|
|
|
|
Convey("Make image store", t, func(c C) {
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
|
|
|
log := log.NewLogger("debug", "")
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, log)
|
|
|
|
imgStore := local.NewImageStore(dir, true, 500*time.Millisecond, true, true, log, metrics, nil, cacheDriver)
|
|
|
|
repoName := "gc-index"
|
|
|
|
|
|
|
|
// create a blob/layer
|
|
|
|
upload, err := imgStore.NewBlobUpload(repoName)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(upload, ShouldNotBeEmpty)
|
|
|
|
|
|
|
|
content := []byte("this is a blob1")
|
|
|
|
buf := bytes.NewBuffer(content)
|
|
|
|
buflen := buf.Len()
|
|
|
|
digest := godigest.FromBytes(content)
|
|
|
|
So(digest, ShouldNotBeNil)
|
|
|
|
blob, err := imgStore.PutBlobChunkStreamed(repoName, upload, buf)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(blob, ShouldEqual, buflen)
|
|
|
|
bdgst1 := digest
|
|
|
|
bsize1 := len(content)
|
|
|
|
|
|
|
|
err = imgStore.FinishBlobUpload(repoName, upload, buf, digest)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(blob, ShouldEqual, buflen)
|
|
|
|
|
|
|
|
Convey("Trigger error on GetImageIndex", func() {
|
|
|
|
var index ispec.Index
|
|
|
|
index.SchemaVersion = 2
|
|
|
|
index.MediaType = ispec.MediaTypeImageIndex
|
|
|
|
|
|
|
|
for i := 0; i < 4; i++ {
|
|
|
|
// upload image config blob
|
|
|
|
upload, err = imgStore.NewBlobUpload(repoName)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(upload, ShouldNotBeEmpty)
|
|
|
|
|
|
|
|
cblob, cdigest := test.GetRandomImageConfig()
|
|
|
|
buf = bytes.NewBuffer(cblob)
|
|
|
|
buflen = buf.Len()
|
|
|
|
blob, err = imgStore.PutBlobChunkStreamed(repoName, upload, buf)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(blob, ShouldEqual, buflen)
|
|
|
|
|
|
|
|
err = imgStore.FinishBlobUpload(repoName, upload, buf, cdigest)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(blob, ShouldEqual, buflen)
|
|
|
|
|
|
|
|
// create a manifest
|
|
|
|
manifest := ispec.Manifest{
|
|
|
|
Config: ispec.Descriptor{
|
|
|
|
MediaType: ispec.MediaTypeImageConfig,
|
|
|
|
Digest: cdigest,
|
|
|
|
Size: int64(len(cblob)),
|
|
|
|
},
|
|
|
|
Layers: []ispec.Descriptor{
|
|
|
|
{
|
|
|
|
MediaType: ispec.MediaTypeImageLayer,
|
|
|
|
Digest: bdgst1,
|
|
|
|
Size: int64(bsize1),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
manifest.SchemaVersion = 2
|
|
|
|
content, err = json.Marshal(manifest)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
digest = godigest.FromBytes(content)
|
|
|
|
So(digest, ShouldNotBeNil)
|
|
|
|
_, err = imgStore.PutImageManifest(repoName, digest.String(), ispec.MediaTypeImageManifest, content)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
index.Manifests = append(index.Manifests, ispec.Descriptor{
|
|
|
|
Digest: digest,
|
|
|
|
MediaType: ispec.MediaTypeImageManifest,
|
|
|
|
Size: int64(len(content)),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// upload index image
|
|
|
|
indexContent, err := json.Marshal(index)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
indexDigest := godigest.FromBytes(indexContent)
|
|
|
|
So(indexDigest, ShouldNotBeNil)
|
|
|
|
|
|
|
|
_, err = imgStore.PutImageManifest(repoName, "1.0", ispec.MediaTypeImageIndex, indexContent)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = os.Chmod(imgStore.BlobPath(repoName, indexDigest), 0o000)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
|
|
|
|
err = imgStore.RunGCRepo(repoName)
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Trigger error on GetBlobContent and Unmarshal for untagged manifest", func() {
|
|
|
|
// upload image config blob
|
|
|
|
upload, err = imgStore.NewBlobUpload(repoName)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(upload, ShouldNotBeEmpty)
|
|
|
|
|
|
|
|
cblob, cdigest := test.GetRandomImageConfig()
|
|
|
|
buf = bytes.NewBuffer(cblob)
|
|
|
|
buflen = buf.Len()
|
|
|
|
blob, err = imgStore.PutBlobChunkStreamed(repoName, upload, buf)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(blob, ShouldEqual, buflen)
|
|
|
|
|
|
|
|
err = imgStore.FinishBlobUpload(repoName, upload, buf, cdigest)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(blob, ShouldEqual, buflen)
|
|
|
|
|
|
|
|
// create a manifest
|
|
|
|
manifest := ispec.Manifest{
|
|
|
|
Config: ispec.Descriptor{
|
|
|
|
MediaType: ispec.MediaTypeImageConfig,
|
|
|
|
Digest: cdigest,
|
|
|
|
Size: int64(len(cblob)),
|
|
|
|
},
|
|
|
|
Layers: []ispec.Descriptor{
|
|
|
|
{
|
|
|
|
MediaType: ispec.MediaTypeImageLayer,
|
|
|
|
Digest: bdgst1,
|
|
|
|
Size: int64(bsize1),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
manifest.SchemaVersion = 2
|
|
|
|
content, err = json.Marshal(manifest)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
digest = godigest.FromBytes(content)
|
|
|
|
So(digest, ShouldNotBeNil)
|
|
|
|
|
|
|
|
_, err = imgStore.PutImageManifest(repoName, digest.String(), ispec.MediaTypeImageManifest, content)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
// trigger GetBlobContent error
|
|
|
|
err = os.Remove(imgStore.BlobPath(repoName, digest))
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
|
|
|
|
err = imgStore.RunGCRepo(repoName)
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
|
|
|
// trigger Unmarshal error
|
|
|
|
_, err = os.Create(imgStore.BlobPath(repoName, digest))
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = imgStore.RunGCRepo(repoName)
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Trigger manifest conflict error", func() {
|
|
|
|
// upload image config blob
|
|
|
|
upload, err = imgStore.NewBlobUpload(repoName)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(upload, ShouldNotBeEmpty)
|
|
|
|
|
|
|
|
cblob, cdigest := test.GetRandomImageConfig()
|
|
|
|
buf = bytes.NewBuffer(cblob)
|
|
|
|
buflen = buf.Len()
|
|
|
|
blob, err = imgStore.PutBlobChunkStreamed(repoName, upload, buf)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(blob, ShouldEqual, buflen)
|
|
|
|
|
|
|
|
err = imgStore.FinishBlobUpload(repoName, upload, buf, cdigest)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(blob, ShouldEqual, buflen)
|
|
|
|
|
|
|
|
// create a manifest
|
|
|
|
manifest := ispec.Manifest{
|
|
|
|
Config: ispec.Descriptor{
|
|
|
|
MediaType: ispec.MediaTypeImageConfig,
|
|
|
|
Digest: cdigest,
|
|
|
|
Size: int64(len(cblob)),
|
|
|
|
},
|
|
|
|
Layers: []ispec.Descriptor{
|
|
|
|
{
|
|
|
|
MediaType: ispec.MediaTypeImageLayer,
|
|
|
|
Digest: bdgst1,
|
|
|
|
Size: int64(bsize1),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
manifest.SchemaVersion = 2
|
|
|
|
content, err = json.Marshal(manifest)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
digest = godigest.FromBytes(content)
|
|
|
|
So(digest, ShouldNotBeNil)
|
|
|
|
|
|
|
|
_, err = imgStore.PutImageManifest(repoName, digest.String(), ispec.MediaTypeImageManifest, content)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
// upload again same manifest so that we trigger manifest conflict
|
|
|
|
_, err = imgStore.PutImageManifest(repoName, "1.0", ispec.MediaTypeImageManifest, content)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
|
|
|
|
err = imgStore.RunGCRepo(repoName)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
// blob shouldn't be gc'ed
|
|
|
|
found, _, err := imgStore.CheckBlob(repoName, digest)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(found, ShouldEqual, true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2022-05-19 03:22:51 -05:00
|
|
|
|
|
|
|
func TestInitRepo(t *testing.T) {
|
|
|
|
Convey("Get error when creating BlobUploadDir subdir on initRepo", t, func() {
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, log)
|
2022-09-30 12:35:16 -05:00
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay,
|
2022-11-02 17:53:08 -05:00
|
|
|
true, true, log, metrics, nil, cacheDriver)
|
2022-05-19 03:22:51 -05:00
|
|
|
|
|
|
|
err := os.Mkdir(path.Join(dir, "test-dir"), 0o000)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = imgStore.InitRepo("test-dir")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateRepo(t *testing.T) {
|
|
|
|
Convey("Get error when unable to read directory", t, func() {
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, log)
|
2022-09-30 12:35:16 -05:00
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay,
|
2022-11-02 17:53:08 -05:00
|
|
|
true, true, log, metrics, nil, cacheDriver)
|
2022-05-19 03:22:51 -05:00
|
|
|
|
|
|
|
err := os.Mkdir(path.Join(dir, "test-dir"), 0o000)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
_, err = imgStore.ValidateRepo("test-dir")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
2023-01-18 11:24:44 -05:00
|
|
|
|
|
|
|
Convey("Get error when repo name is not compliant with repo spec", t, func() {
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, log)
|
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay,
|
|
|
|
true, true, log, metrics, nil, cacheDriver)
|
|
|
|
|
|
|
|
_, err := imgStore.ValidateRepo(".")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(errors.Is(err, zerr.ErrInvalidRepositoryName), ShouldBeTrue)
|
|
|
|
|
|
|
|
_, err = imgStore.ValidateRepo("..")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(errors.Is(err, zerr.ErrInvalidRepositoryName), ShouldBeTrue)
|
|
|
|
|
|
|
|
err = os.Mkdir(path.Join(dir, "_test-dir"), 0o755)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
_, err = imgStore.ValidateRepo("_test-dir")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(errors.Is(err, zerr.ErrInvalidRepositoryName), ShouldBeTrue)
|
|
|
|
|
|
|
|
err = os.Mkdir(path.Join(dir, ".test-dir"), 0o755)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
_, err = imgStore.ValidateRepo(".test-dir")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(errors.Is(err, zerr.ErrInvalidRepositoryName), ShouldBeTrue)
|
|
|
|
|
|
|
|
err = os.Mkdir(path.Join(dir, "-test-dir"), 0o755)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
_, err = imgStore.ValidateRepo("-test-dir")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(errors.Is(err, zerr.ErrInvalidRepositoryName), ShouldBeTrue)
|
|
|
|
})
|
2022-05-19 03:22:51 -05:00
|
|
|
}
|
|
|
|
|
2023-01-18 11:24:44 -05:00
|
|
|
func TestGetRepositories(t *testing.T) {
|
|
|
|
Convey("Verify errors and repos returned by GetRepositories()", t, func() {
|
2022-05-19 03:22:51 -05:00
|
|
|
dir := t.TempDir()
|
|
|
|
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, log)
|
2022-09-30 12:35:16 -05:00
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay,
|
2022-11-02 17:53:08 -05:00
|
|
|
true, true, log, metrics, nil, cacheDriver,
|
2022-06-24 08:08:47 -05:00
|
|
|
)
|
2022-05-19 03:22:51 -05:00
|
|
|
|
2023-01-18 11:24:44 -05:00
|
|
|
// Create valid directory with permissions
|
|
|
|
err := os.Mkdir(path.Join(dir, "test-dir"), 0o755) //nolint: gosec
|
2022-05-19 03:22:51 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-01-18 11:24:44 -05:00
|
|
|
err = os.WriteFile(path.Join(dir, "test-dir/test-file"), []byte("this is test file"), 0o755) //nolint: gosec
|
2022-05-19 03:22:51 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-01-18 11:24:44 -05:00
|
|
|
// Folder is not a repo as it is missing the requires files/subfolder
|
|
|
|
repos, err := imgStore.GetRepositories()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(repos), ShouldEqual, 0)
|
|
|
|
|
|
|
|
il := ispec.ImageLayout{Version: ispec.ImageLayoutVersion}
|
|
|
|
layoutFileContent, err := json.Marshal(il)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
// Folder becomes a repo after the missing content is added
|
|
|
|
err = os.Mkdir(path.Join(dir, "test-dir", "blobs"), 0o755) //nolint: gosec
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = os.Mkdir(path.Join(dir, "test-dir", storageConstants.BlobUploadDir), 0o755) //nolint: gosec
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = os.WriteFile(path.Join(dir, "test-dir", "index.json"), []byte{}, 0o755) //nolint: gosec
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = os.WriteFile(path.Join(dir, "test-dir", ispec.ImageLayoutFile), layoutFileContent, 0o755) //nolint: gosec
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
// Verify the new repo is turned
|
|
|
|
repos, err = imgStore.GetRepositories()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(repos), ShouldEqual, 1)
|
|
|
|
So(repos[0], ShouldEqual, "test-dir")
|
|
|
|
|
|
|
|
// create directory starting with underscore, which is not OCI a dist spec compliant repo name
|
|
|
|
// [a-z0-9]+([._-][a-z0-9]+)*(/[a-z0-9]+([._-][a-z0-9]+)*)*
|
|
|
|
err = os.MkdirAll(path.Join(dir, "_trivy", "db"), 0o755)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = os.WriteFile(path.Join(dir, "_trivy", "db", "trivy.db"), []byte("this is test file"), 0o755) //nolint: gosec
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
// Folder with invalid name is not a repo as it is missing the requires files/subfolder
|
|
|
|
repos, err = imgStore.GetRepositories()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(repos), ShouldEqual, 1)
|
|
|
|
So(repos[0], ShouldEqual, "test-dir")
|
|
|
|
|
|
|
|
// Add missing content to folder with invalid name
|
|
|
|
err = os.Mkdir(path.Join(dir, "_trivy", "blobs"), 0o755) //nolint: gosec
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = os.Mkdir(path.Join(dir, "_trivy", storageConstants.BlobUploadDir), 0o755) //nolint: gosec
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = os.WriteFile(path.Join(dir, "_trivy", "index.json"), []byte{}, 0o755) //nolint: gosec
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = os.WriteFile(path.Join(dir, "_trivy", ispec.ImageLayoutFile), layoutFileContent, 0o755) //nolint: gosec
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
// Folder with invalid name doesn't become a repo after the missing content is added
|
|
|
|
repos, err = imgStore.GetRepositories()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
t.Logf("repos %v", repos)
|
|
|
|
So(len(repos), ShouldEqual, 1)
|
|
|
|
So(repos[0], ShouldEqual, "test-dir")
|
|
|
|
|
|
|
|
// Rename folder with invalid name to a valid one
|
|
|
|
err = os.Rename(path.Join(dir, "_trivy"), path.Join(dir, "test-dir-2"))
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
// Verify both repos are now visible
|
|
|
|
repos, err = imgStore.GetRepositories()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
t.Logf("repos %v", repos)
|
|
|
|
So(len(repos), ShouldEqual, 2)
|
|
|
|
So(repos, ShouldContain, "test-dir")
|
|
|
|
So(repos, ShouldContain, "test-dir-2")
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Verify GetRepositories() doesn't return '.' when having an oci layout as root directory ", t, func() {
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, log)
|
|
|
|
|
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay,
|
|
|
|
true, true, log, metrics, nil, cacheDriver,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Root dir does not contain repos
|
|
|
|
repos, err := imgStore.GetRepositories()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(repos), ShouldEqual, 0)
|
|
|
|
|
|
|
|
// Configure root directory as an oci layout
|
|
|
|
err = os.Mkdir(path.Join(dir, "blobs"), 0o755) //nolint: gosec
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = os.Mkdir(path.Join(dir, storageConstants.BlobUploadDir), 0o755) //nolint: gosec
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = os.WriteFile(path.Join(dir, "index.json"), []byte{}, 0o755) //nolint: gosec
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
il := ispec.ImageLayout{Version: ispec.ImageLayoutVersion}
|
|
|
|
layoutFileContent, err := json.Marshal(il)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = os.WriteFile(path.Join(dir, ispec.ImageLayoutFile), layoutFileContent, 0o755) //nolint: gosec
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
// Verify root directory is not returned as a repo
|
|
|
|
repos, err = imgStore.GetRepositories()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
t.Logf("repos %v", repos)
|
|
|
|
So(len(repos), ShouldEqual, 0)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Verify GetRepositories() doesn't return '..'", t, func() {
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
|
|
|
rootDir := path.Join(dir, "rootDir")
|
|
|
|
err := os.Mkdir(rootDir, 0o755)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: rootDir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, log)
|
|
|
|
|
|
|
|
imgStore := local.NewImageStore(rootDir, true, storage.DefaultGCDelay,
|
|
|
|
true, true, log, metrics, nil, cacheDriver,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Root dir does not contain repos
|
|
|
|
repos, err := imgStore.GetRepositories()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(repos), ShouldEqual, 0)
|
|
|
|
|
|
|
|
// Configure parent of root directory as an oci layout
|
|
|
|
err = os.Mkdir(path.Join(dir, "blobs"), 0o755) //nolint: gosec
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = os.Mkdir(path.Join(dir, storageConstants.BlobUploadDir), 0o755) //nolint: gosec
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = os.WriteFile(path.Join(dir, "index.json"), []byte{}, 0o755) //nolint: gosec
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
il := ispec.ImageLayout{Version: ispec.ImageLayoutVersion}
|
|
|
|
layoutFileContent, err := json.Marshal(il)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = os.WriteFile(path.Join(dir, ispec.ImageLayoutFile), layoutFileContent, 0o755) //nolint: gosec
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
// Verify root directory is not returned as a repo
|
|
|
|
repos, err = imgStore.GetRepositories()
|
2022-05-19 03:22:51 -05:00
|
|
|
So(err, ShouldBeNil)
|
2023-01-18 11:24:44 -05:00
|
|
|
t.Logf("repos %v", repos)
|
|
|
|
So(len(repos), ShouldEqual, 0)
|
2022-05-19 03:22:51 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-23 00:27:56 -05:00
|
|
|
func TestGetNextRepository(t *testing.T) {
|
|
|
|
dir := t.TempDir()
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, log)
|
2022-09-30 12:35:16 -05:00
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay,
|
2022-11-02 17:53:08 -05:00
|
|
|
true, true, log, metrics, nil, cacheDriver,
|
2022-09-23 00:27:56 -05:00
|
|
|
)
|
|
|
|
firstRepoName := "repo1"
|
|
|
|
secondRepoName := "repo2"
|
|
|
|
|
2023-01-20 13:01:52 -05:00
|
|
|
test.CopyTestFiles("../../../test/data/zot-test", path.Join(dir, firstRepoName))
|
2022-09-23 00:27:56 -05:00
|
|
|
|
2023-01-20 13:01:52 -05:00
|
|
|
test.CopyTestFiles("../../../test/data/zot-test", path.Join(dir, secondRepoName))
|
2022-09-23 00:27:56 -05:00
|
|
|
|
|
|
|
Convey("Return first repository", t, func() {
|
|
|
|
firstRepo, err := imgStore.GetNextRepository("")
|
|
|
|
So(firstRepo, ShouldEqual, firstRepoName)
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(err, ShouldEqual, io.EOF)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Return second repository", t, func() {
|
|
|
|
secondRepo, err := imgStore.GetNextRepository(firstRepoName)
|
|
|
|
So(secondRepo, ShouldEqual, secondRepoName)
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(err, ShouldEqual, io.EOF)
|
|
|
|
})
|
2022-12-07 14:04:33 -05:00
|
|
|
|
|
|
|
Convey("Return error", t, func() {
|
2023-01-20 13:01:52 -05:00
|
|
|
err := os.Chmod(imgStore.RootDir(), 0o000)
|
2022-12-07 14:04:33 -05:00
|
|
|
So(err, ShouldBeNil)
|
2023-01-20 13:01:52 -05:00
|
|
|
_, err = imgStore.GetNextRepository(firstRepoName)
|
2022-12-07 14:04:33 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
err = os.Chmod(imgStore.RootDir(), 0o755)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
2022-09-23 00:27:56 -05:00
|
|
|
}
|
|
|
|
|
2022-05-19 03:22:51 -05:00
|
|
|
func TestPutBlobChunkStreamed(t *testing.T) {
|
|
|
|
Convey("Get error on opening file", t, func() {
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, log)
|
2022-09-30 12:35:16 -05:00
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay,
|
2022-11-02 17:53:08 -05:00
|
|
|
true, true, log, metrics, nil, cacheDriver)
|
2022-05-19 03:22:51 -05:00
|
|
|
|
|
|
|
uuid, err := imgStore.NewBlobUpload("test")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
var reader io.Reader
|
|
|
|
blobPath := imgStore.BlobUploadPath("test", uuid)
|
|
|
|
err = os.Chmod(blobPath, 0o000)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
_, err = imgStore.PutBlobChunkStreamed("test", uuid, reader)
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
}
|
2022-07-27 12:37:55 -05:00
|
|
|
|
2022-08-30 17:12:10 -05:00
|
|
|
func TestPullRange(t *testing.T) {
|
|
|
|
Convey("Repo layout", t, func(c C) {
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
|
|
|
|
|
|
|
Convey("Negative cases", func() {
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, log)
|
2022-09-30 12:35:16 -05:00
|
|
|
imgStore := local.NewImageStore(dir, true, storage.DefaultGCDelay,
|
2022-11-02 17:53:08 -05:00
|
|
|
true, true, log, metrics, nil, cacheDriver)
|
2022-08-30 17:12:10 -05:00
|
|
|
repoName := "pull-range"
|
|
|
|
|
|
|
|
upload, err := imgStore.NewBlobUpload(repoName)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(upload, ShouldNotBeEmpty)
|
|
|
|
|
|
|
|
content := []byte("test-data1")
|
|
|
|
buf := bytes.NewBuffer(content)
|
|
|
|
buflen := buf.Len()
|
|
|
|
bdigest := godigest.FromBytes(content)
|
|
|
|
|
|
|
|
blob, err := imgStore.PutBlobChunk(repoName, upload, 0, int64(buflen), buf)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(blob, ShouldEqual, buflen)
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
err = imgStore.FinishBlobUpload(repoName, upload, buf, bdigest)
|
2022-08-30 17:12:10 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
_, _, _, err = imgStore.GetBlobPartial(repoName, "", "application/octet-stream", 0, 1)
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
_, _, _, err = imgStore.GetBlobPartial(repoName, bdigest, "application/octet-stream", 1, 0)
|
2022-08-30 17:12:10 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
_, _, _, err = imgStore.GetBlobPartial(repoName, bdigest, "application/octet-stream", 1, 0)
|
2022-08-30 17:12:10 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
|
|
|
blobPath := path.Join(imgStore.RootDir(), repoName, "blobs", bdigest.Algorithm().String(), bdigest.Encoded())
|
|
|
|
err = os.Chmod(blobPath, 0o000)
|
|
|
|
So(err, ShouldBeNil)
|
2022-10-22 15:46:13 -05:00
|
|
|
_, _, _, err = imgStore.GetBlobPartial(repoName, bdigest, "application/octet-stream", -1, 1)
|
2022-08-30 17:12:10 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-07-27 12:37:55 -05:00
|
|
|
func NewRandomImgManifest(data []byte, cdigest, ldigest godigest.Digest, cblob, lblob []byte) (*ispec.Manifest, error) {
|
|
|
|
annotationsMap := make(map[string]string)
|
|
|
|
|
|
|
|
key := string(data)
|
|
|
|
val := string(data)
|
|
|
|
annotationsMap[key] = val
|
|
|
|
|
|
|
|
schemaVersion := 2
|
|
|
|
|
|
|
|
manifest := ispec.Manifest{
|
|
|
|
MediaType: "application/vnd.oci.image.manifest.v1+json",
|
|
|
|
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: ldigest,
|
|
|
|
Size: int64(len(lblob)),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Annotations: annotationsMap,
|
|
|
|
Versioned: imeta.Versioned{
|
|
|
|
SchemaVersion: schemaVersion,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return &manifest, nil
|
|
|
|
}
|
|
|
|
|
2022-10-05 05:21:14 -05:00
|
|
|
func newRandomBlobForFuzz(data []byte) (godigest.Digest, []byte, error) { //nolint:unparam
|
2022-07-27 12:37:55 -05:00
|
|
|
return godigest.FromBytes(data), data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func isKnownErr(err error) bool {
|
|
|
|
if errors.Is(err, zerr.ErrInvalidRepositoryName) || errors.Is(err, zerr.ErrManifestNotFound) ||
|
|
|
|
errors.Is(err, zerr.ErrRepoNotFound) ||
|
|
|
|
errors.Is(err, zerr.ErrBadManifest) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if err, ok := err.(*fs.PathError); ok && errors.Is(err.Err, syscall.EACCES) || //nolint: errorlint
|
|
|
|
errors.Is(err.Err, syscall.ENAMETOOLONG) ||
|
|
|
|
errors.Is(err.Err, syscall.EINVAL) ||
|
|
|
|
errors.Is(err.Err, syscall.ENOENT) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|