2020-02-17 16:57:15 -05:00
|
|
|
package storage_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2020-05-27 16:27:35 -05:00
|
|
|
"path"
|
2020-02-17 16:57:15 -05:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/errors"
|
|
|
|
"zotregistry.io/zot/pkg/log"
|
|
|
|
"zotregistry.io/zot/pkg/storage"
|
2020-02-17 16:57:15 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCache(t *testing.T) {
|
|
|
|
Convey("Make a new cache", t, func() {
|
|
|
|
dir, err := ioutil.TempDir("", "cache_test")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(dir, ShouldNotBeEmpty)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
log := log.NewLogger("debug", "")
|
|
|
|
So(log, ShouldNotBeNil)
|
|
|
|
|
|
|
|
So(storage.NewCache("/deadBEEF", "cache_test", log), ShouldBeNil)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
cache := storage.NewCache(dir, "cache_test", log)
|
|
|
|
So(cache, ShouldNotBeNil)
|
2020-02-17 16:57:15 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
val, err := cache.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
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
exists := cache.HasBlob("key", "value")
|
|
|
|
So(exists, ShouldBeFalse)
|
2020-02-17 16:57:15 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = cache.PutBlob("key", path.Join(dir, "value"))
|
2020-02-17 16:57:15 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2021-12-07 13:26:26 -05:00
|
|
|
err = cache.PutBlob("key", "value")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
exists = cache.HasBlob("key", "value")
|
|
|
|
So(exists, ShouldBeTrue)
|
2020-02-17 16:57:15 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
val, err = cache.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
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = cache.DeleteBlob("bogusKey", "bogusValue")
|
2020-02-17 16:57:15 -05:00
|
|
|
So(err, ShouldEqual, errors.ErrCacheMiss)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = cache.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
|
2021-12-13 14:23:31 -05:00
|
|
|
err = cache.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
|
|
|
})
|
|
|
|
}
|