2022-11-22 13:29:57 -05:00
|
|
|
package cache_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
godigest "github.com/opencontainers/go-digest"
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
|
2024-01-31 23:34:07 -05:00
|
|
|
"zotregistry.dev/zot/pkg/log"
|
|
|
|
"zotregistry.dev/zot/pkg/storage"
|
|
|
|
"zotregistry.dev/zot/pkg/storage/cache"
|
|
|
|
tskip "zotregistry.dev/zot/pkg/test/skip"
|
2022-11-22 13:29:57 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDynamoDB(t *testing.T) {
|
2023-10-05 06:34:50 -05:00
|
|
|
tskip.SkipDynamo(t)
|
2022-11-22 13:29:57 -05:00
|
|
|
Convey("Test dynamoDB", t, func(c C) {
|
|
|
|
log := log.NewLogger("debug", "")
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
|
|
|
// bad params
|
|
|
|
|
2023-11-24 03:38:36 -05:00
|
|
|
_, err := cache.NewDynamoDBCache("bad params", log)
|
|
|
|
So(err, ShouldNotBeNil)
|
2022-11-22 13:29:57 -05:00
|
|
|
|
|
|
|
keyDigest := godigest.FromString("key")
|
|
|
|
|
|
|
|
cacheDriver, err := storage.Create("dynamodb", cache.DynamoDBDriverParameters{
|
|
|
|
Endpoint: "http://brokenlink",
|
|
|
|
TableName: "BlobTable",
|
|
|
|
Region: "us-east-2",
|
|
|
|
}, log)
|
2023-11-24 03:38:36 -05:00
|
|
|
So(cacheDriver, ShouldBeNil)
|
2022-11-22 13:29:57 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
|
|
|
cacheDriver, err = storage.Create("dynamodb", cache.DynamoDBDriverParameters{
|
|
|
|
Endpoint: os.Getenv("DYNAMODBMOCK_ENDPOINT"),
|
|
|
|
TableName: "BlobTable",
|
|
|
|
Region: "us-east-2",
|
|
|
|
}, log)
|
|
|
|
So(cacheDriver, ShouldNotBeNil)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
returnedName := cacheDriver.Name()
|
|
|
|
So(returnedName, ShouldEqual, "dynamodb")
|
|
|
|
|
2023-11-24 03:38:36 -05:00
|
|
|
val, err := cacheDriver.GetBlob(keyDigest)
|
2022-11-22 13:29:57 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(val, ShouldBeEmpty)
|
|
|
|
|
|
|
|
err = cacheDriver.PutBlob(keyDigest, "")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
|
|
|
err = cacheDriver.PutBlob(keyDigest, path.Join(dir, "value"))
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
val, err = cacheDriver.GetBlob(keyDigest)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(val, ShouldNotBeEmpty)
|
|
|
|
|
2023-11-24 03:38:36 -05:00
|
|
|
exists := cacheDriver.HasBlob(keyDigest, path.Join(dir, "value"))
|
2022-11-22 13:29:57 -05:00
|
|
|
So(exists, ShouldBeTrue)
|
|
|
|
|
|
|
|
err = cacheDriver.DeleteBlob(keyDigest, path.Join(dir, "value"))
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
exists = cacheDriver.HasBlob(keyDigest, path.Join(dir, "value"))
|
|
|
|
So(exists, ShouldBeFalse)
|
|
|
|
|
|
|
|
err = cacheDriver.PutBlob(keyDigest, path.Join(dir, "value1"))
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = cacheDriver.PutBlob(keyDigest, path.Join(dir, "value2"))
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = cacheDriver.DeleteBlob(keyDigest, path.Join(dir, "value1"))
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
exists = cacheDriver.HasBlob(keyDigest, path.Join(dir, "value2"))
|
|
|
|
So(exists, ShouldBeTrue)
|
|
|
|
|
|
|
|
exists = cacheDriver.HasBlob(keyDigest, path.Join(dir, "value1"))
|
|
|
|
So(exists, ShouldBeFalse)
|
|
|
|
|
|
|
|
err = cacheDriver.DeleteBlob(keyDigest, path.Join(dir, "value2"))
|
|
|
|
So(err, ShouldBeNil)
|
2023-10-10 12:29:07 -05:00
|
|
|
|
|
|
|
err = cacheDriver.PutBlob("key1", "originalBlobPath")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = cacheDriver.PutBlob("key1", "duplicateBlobPath")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
val, err = cacheDriver.GetBlob("key1")
|
|
|
|
So(val, ShouldEqual, "originalBlobPath")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = cacheDriver.DeleteBlob("key1", "duplicateBlobPath")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
val, err = cacheDriver.GetBlob("key1")
|
|
|
|
So(val, ShouldEqual, "originalBlobPath")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = cacheDriver.PutBlob("key1", "duplicateBlobPath")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = cacheDriver.DeleteBlob("key1", "originalBlobPath")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
val, err = cacheDriver.GetBlob("key1")
|
|
|
|
So(val, ShouldEqual, "duplicateBlobPath")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = cacheDriver.DeleteBlob("key1", "duplicateBlobPath")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
// should be empty
|
|
|
|
val, err = cacheDriver.GetBlob("key1")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(val, ShouldBeEmpty)
|
|
|
|
|
|
|
|
// try to add three same values
|
|
|
|
err = cacheDriver.PutBlob("key2", "duplicate")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = cacheDriver.PutBlob("key2", "duplicate")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = cacheDriver.PutBlob("key2", "duplicate")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
val, err = cacheDriver.GetBlob("key2")
|
|
|
|
So(val, ShouldEqual, "duplicate")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = cacheDriver.DeleteBlob("key2", "duplicate")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
// should be empty
|
|
|
|
val, err = cacheDriver.GetBlob("key2")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(val, ShouldBeEmpty)
|
2022-11-22 13:29:57 -05:00
|
|
|
})
|
2024-07-08 13:35:44 -05:00
|
|
|
|
|
|
|
Convey("Test dynamoDB", t, func(c C) {
|
|
|
|
log := log.NewLogger("debug", "")
|
|
|
|
|
|
|
|
cacheDriver, err := storage.Create("dynamodb", cache.DynamoDBDriverParameters{
|
|
|
|
Endpoint: os.Getenv("DYNAMODBMOCK_ENDPOINT"),
|
|
|
|
TableName: "BlobTable",
|
|
|
|
Region: "us-east-2",
|
|
|
|
}, log)
|
|
|
|
So(cacheDriver, ShouldNotBeNil)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = cacheDriver.PutBlob("digest", "first")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = cacheDriver.PutBlob("digest", "second")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = cacheDriver.PutBlob("digest", "third")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
blobs, err := cacheDriver.GetAllBlobs("digest")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
So(blobs, ShouldResemble, []string{"first", "second", "third"})
|
|
|
|
|
|
|
|
err = cacheDriver.DeleteBlob("digest", "first")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
blobs, err = cacheDriver.GetAllBlobs("digest")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
So(blobs, ShouldResemble, []string{"second", "third"})
|
|
|
|
|
|
|
|
err = cacheDriver.DeleteBlob("digest", "third")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
blobs, err = cacheDriver.GetAllBlobs("digest")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
So(blobs, ShouldResemble, []string{"second"})
|
|
|
|
})
|
2022-11-22 13:29:57 -05:00
|
|
|
}
|
2023-11-24 03:38:36 -05:00
|
|
|
|
|
|
|
func TestDynamoDBError(t *testing.T) {
|
|
|
|
tskip.SkipDynamo(t)
|
|
|
|
Convey("Test dynamoDB", t, func(c C) {
|
|
|
|
log := log.NewLogger("debug", "")
|
|
|
|
|
|
|
|
cacheDriver, err := cache.NewDynamoDBCache(cache.DynamoDBDriverParameters{
|
|
|
|
Endpoint: os.Getenv("DYNAMODBMOCK_ENDPOINT"),
|
|
|
|
TableName: "BlobTable",
|
|
|
|
Region: "us-east-2",
|
|
|
|
}, log)
|
|
|
|
So(cacheDriver, ShouldNotBeNil)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
returnedName := cacheDriver.Name()
|
|
|
|
So(returnedName, ShouldEqual, "dynamodb")
|
|
|
|
|
|
|
|
cacheDriver.SetTableName("bad-table")
|
|
|
|
|
|
|
|
_, err = cacheDriver.GetBlob(godigest.FromString("str"))
|
|
|
|
So(err, ShouldNotBeNil)
|
2024-07-29 12:32:51 -05:00
|
|
|
|
2023-11-24 03:38:36 -05:00
|
|
|
found := cacheDriver.HasBlob(godigest.FromString("str"), "path")
|
|
|
|
So(found, ShouldBeFalse)
|
2024-07-29 12:32:51 -05:00
|
|
|
|
2023-11-24 03:38:36 -05:00
|
|
|
_, err = cacheDriver.GetDuplicateBlob(godigest.FromString("str"))
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
err = cacheDriver.DeleteBlob(godigest.FromString("str"), "path")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
}
|