2022-11-02 17:53:08 -05:00
|
|
|
package cache_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
|
2024-01-31 23:34:07 -05:00
|
|
|
"zotregistry.dev/zot/errors"
|
|
|
|
"zotregistry.dev/zot/pkg/log"
|
|
|
|
"zotregistry.dev/zot/pkg/storage"
|
|
|
|
"zotregistry.dev/zot/pkg/storage/cache"
|
2022-11-02 17:53:08 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBoltDBCache(t *testing.T) {
|
|
|
|
Convey("Make a new cache", t, func() {
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
|
|
|
log := log.NewLogger("debug", "")
|
|
|
|
So(log, ShouldNotBeNil)
|
|
|
|
|
2023-11-24 03:38:36 -05:00
|
|
|
_, err := storage.Create("boltdb", "failTypeAssertion", log)
|
|
|
|
So(err, ShouldNotBeNil)
|
2022-11-02 17:53:08 -05:00
|
|
|
|
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{"/deadBEEF", "cache_test", true}, log)
|
|
|
|
So(cacheDriver, ShouldBeNil)
|
|
|
|
|
|
|
|
cacheDriver, _ = storage.Create("boltdb", cache.BoltDBDriverParameters{dir, "cache_test", true}, log)
|
|
|
|
So(cacheDriver, ShouldNotBeNil)
|
|
|
|
|
|
|
|
name := cacheDriver.Name()
|
|
|
|
So(name, ShouldEqual, "boltdb")
|
|
|
|
|
|
|
|
val, err := cacheDriver.GetBlob("key")
|
|
|
|
So(err, ShouldEqual, errors.ErrCacheMiss)
|
|
|
|
So(val, ShouldBeEmpty)
|
|
|
|
|
|
|
|
exists := cacheDriver.HasBlob("key", "value")
|
|
|
|
So(exists, ShouldBeFalse)
|
|
|
|
|
|
|
|
err = cacheDriver.PutBlob("key", path.Join(dir, "value"))
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = cacheDriver.PutBlob("key", "value")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
|
|
|
exists = cacheDriver.HasBlob("key", "value")
|
|
|
|
So(exists, ShouldBeTrue)
|
|
|
|
|
|
|
|
val, err = cacheDriver.GetBlob("key")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(val, ShouldNotBeEmpty)
|
|
|
|
|
|
|
|
err = cacheDriver.DeleteBlob("bogusKey", "bogusValue")
|
|
|
|
So(err, ShouldEqual, errors.ErrCacheMiss)
|
|
|
|
|
|
|
|
err = cacheDriver.DeleteBlob("key", "bogusValue")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
// try to insert empty path
|
|
|
|
err = cacheDriver.PutBlob("key", "")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(err, ShouldEqual, errors.ErrEmptyValue)
|
2023-10-10 12:29:07 -05:00
|
|
|
|
|
|
|
cacheDriver, _ = storage.Create("boltdb", cache.BoltDBDriverParameters{t.TempDir(), "cache_test", false}, log)
|
|
|
|
So(cacheDriver, ShouldNotBeNil)
|
|
|
|
|
|
|
|
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-02 17:53:08 -05:00
|
|
|
})
|
2024-07-08 13:35:44 -05:00
|
|
|
|
|
|
|
Convey("Test cache.GetAllBlos()", t, func() {
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
|
|
|
log := log.NewLogger("debug", "")
|
|
|
|
So(log, ShouldNotBeNil)
|
|
|
|
|
|
|
|
_, err := storage.Create("boltdb", "failTypeAssertion", log)
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{dir, "cache_test", false}, log)
|
|
|
|
So(cacheDriver, ShouldNotBeNil)
|
|
|
|
|
|
|
|
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-02 17:53:08 -05:00
|
|
|
}
|