2020-02-17 16:57:15 -05:00
|
|
|
package storage_test
|
|
|
|
|
|
|
|
import (
|
2020-05-27 16:27:35 -05:00
|
|
|
"path"
|
2020-02-17 16:57:15 -05:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
2022-10-20 11:39:20 -05:00
|
|
|
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/errors"
|
|
|
|
"zotregistry.io/zot/pkg/log"
|
|
|
|
"zotregistry.io/zot/pkg/storage"
|
2022-11-02 17:53:08 -05:00
|
|
|
"zotregistry.io/zot/pkg/storage/cache"
|
2020-02-17 16:57:15 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCache(t *testing.T) {
|
|
|
|
Convey("Make a new cache", t, func() {
|
2022-03-07 03:55:12 -05:00
|
|
|
dir := t.TempDir()
|
2020-02-17 16:57:15 -05:00
|
|
|
|
|
|
|
log := log.NewLogger("debug", "")
|
|
|
|
So(log, ShouldNotBeNil)
|
|
|
|
|
2022-11-02 17:53:08 -05:00
|
|
|
So(func() { _, _ = storage.Create("boltdb", "failTypeAssertion", log) }, ShouldPanic)
|
2020-02-17 16:57:15 -05:00
|
|
|
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: "/deadBEEF",
|
|
|
|
Name: "cache_test",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, log)
|
|
|
|
So(cacheDriver, ShouldBeNil)
|
2020-02-17 16:57:15 -05:00
|
|
|
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ = storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache_test",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, log)
|
|
|
|
So(cacheDriver, ShouldNotBeNil)
|
|
|
|
|
2023-09-01 12:54:39 -05:00
|
|
|
So(cacheDriver.UsesRelativePaths(), ShouldBeTrue)
|
|
|
|
|
2022-11-02 17:53:08 -05:00
|
|
|
name := cacheDriver.Name()
|
|
|
|
So(name, ShouldEqual, "boltdb")
|
|
|
|
|
|
|
|
val, err := cacheDriver.GetBlob("key")
|
2020-02-17 16:57:15 -05:00
|
|
|
So(err, ShouldEqual, errors.ErrCacheMiss)
|
2021-12-13 14:23:31 -05:00
|
|
|
So(val, ShouldBeEmpty)
|
2020-02-17 16:57:15 -05:00
|
|
|
|
2022-11-02 17:53:08 -05:00
|
|
|
exists := cacheDriver.HasBlob("key", "value")
|
2021-12-13 14:23:31 -05:00
|
|
|
So(exists, ShouldBeFalse)
|
2020-02-17 16:57:15 -05:00
|
|
|
|
2022-11-02 17:53:08 -05:00
|
|
|
err = cacheDriver.PutBlob("key", path.Join(dir, "value"))
|
2020-02-17 16:57:15 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2022-11-02 17:53:08 -05:00
|
|
|
err = cacheDriver.PutBlob("key", "value")
|
2021-12-07 13:26:26 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
2022-11-02 17:53:08 -05:00
|
|
|
exists = cacheDriver.HasBlob("key", "value")
|
2021-12-13 14:23:31 -05:00
|
|
|
So(exists, ShouldBeTrue)
|
2020-02-17 16:57:15 -05:00
|
|
|
|
2022-11-02 17:53:08 -05:00
|
|
|
val, err = cacheDriver.GetBlob("key")
|
2020-02-17 16:57:15 -05:00
|
|
|
So(err, ShouldBeNil)
|
2021-12-13 14:23:31 -05:00
|
|
|
So(val, ShouldNotBeEmpty)
|
2020-02-17 16:57:15 -05:00
|
|
|
|
2022-11-02 17:53:08 -05:00
|
|
|
err = cacheDriver.DeleteBlob("bogusKey", "bogusValue")
|
2020-02-17 16:57:15 -05:00
|
|
|
So(err, ShouldEqual, errors.ErrCacheMiss)
|
|
|
|
|
2022-11-02 17:53:08 -05:00
|
|
|
err = cacheDriver.DeleteBlob("key", "bogusValue")
|
2020-02-17 16:57:15 -05:00
|
|
|
So(err, ShouldBeNil)
|
2021-07-20 16:04:10 -05:00
|
|
|
|
|
|
|
// try to insert empty path
|
2022-11-02 17:53:08 -05:00
|
|
|
err = cacheDriver.PutBlob("key", "")
|
2021-07-20 16:04:10 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(err, ShouldEqual, errors.ErrEmptyValue)
|
2020-02-17 16:57:15 -05:00
|
|
|
})
|
|
|
|
}
|