2022-06-06 08:01:12 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2024-10-11 08:32:36 +00:00
|
|
|
"strings"
|
2022-06-06 08:01:12 +00:00
|
|
|
"testing"
|
|
|
|
|
2024-03-12 08:28:24 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-contract/frostfsid/client"
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/data"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/creds/accessbox"
|
|
|
|
cidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id/test"
|
2024-10-11 08:32:36 +00:00
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2023-03-07 14:38:08 +00:00
|
|
|
oidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id/test"
|
2024-03-12 08:28:24 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2022-06-06 08:01:12 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"go.uber.org/zap/zaptest/observer"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccessBoxCacheType(t *testing.T) {
|
|
|
|
logger, observedLog := getObservedLogger()
|
|
|
|
cache := NewAccessBoxCache(DefaultAccessBoxConfig(logger))
|
|
|
|
|
|
|
|
addr := oidtest.Address()
|
|
|
|
box := &accessbox.Box{}
|
2024-10-18 13:59:52 +00:00
|
|
|
val := &AccessBoxCacheValue{
|
|
|
|
Box: box,
|
|
|
|
}
|
2022-06-06 08:01:12 +00:00
|
|
|
|
2024-10-11 08:32:36 +00:00
|
|
|
accessKeyID := getAccessKeyID(addr)
|
|
|
|
|
2024-10-18 13:59:52 +00:00
|
|
|
err := cache.Put(accessKeyID, val)
|
2022-06-06 08:01:12 +00:00
|
|
|
require.NoError(t, err)
|
2024-10-18 13:59:52 +00:00
|
|
|
resVal := cache.Get(accessKeyID)
|
|
|
|
require.Equal(t, box, resVal.Box)
|
2022-06-06 08:01:12 +00:00
|
|
|
require.Equal(t, 0, observedLog.Len())
|
|
|
|
|
2024-10-11 08:32:36 +00:00
|
|
|
err = cache.cache.Set(accessKeyID, "tmp")
|
2022-06-06 08:01:12 +00:00
|
|
|
require.NoError(t, err)
|
2024-10-11 08:32:36 +00:00
|
|
|
assertInvalidCacheEntry(t, cache.Get(accessKeyID), observedLog)
|
2022-06-06 08:01:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBucketsCacheType(t *testing.T) {
|
|
|
|
logger, observedLog := getObservedLogger()
|
|
|
|
cache := NewBucketCache(DefaultBucketConfig(logger))
|
|
|
|
|
|
|
|
bktInfo := &data.BucketInfo{Name: "bucket"}
|
|
|
|
|
2023-11-23 07:44:50 +00:00
|
|
|
err := cache.Put(bktInfo)
|
2022-06-06 08:01:12 +00:00
|
|
|
require.NoError(t, err)
|
2023-11-16 12:10:51 +00:00
|
|
|
val := cache.Get("", bktInfo.Name)
|
2022-06-06 08:01:12 +00:00
|
|
|
require.Equal(t, bktInfo, val)
|
|
|
|
require.Equal(t, 0, observedLog.Len())
|
|
|
|
|
2023-11-23 07:44:50 +00:00
|
|
|
err = cache.cache.Set(bktInfo.Name+"."+bktInfo.Zone, "tmp")
|
2022-06-06 08:01:12 +00:00
|
|
|
require.NoError(t, err)
|
2023-11-23 07:44:50 +00:00
|
|
|
assertInvalidCacheEntry(t, cache.Get(bktInfo.Zone, bktInfo.Name), observedLog)
|
2022-06-06 08:01:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestObjectNamesCacheType(t *testing.T) {
|
|
|
|
logger, observedLog := getObservedLogger()
|
|
|
|
cache := NewObjectsNameCache(DefaultObjectsNameConfig(logger))
|
|
|
|
|
|
|
|
key := "name"
|
|
|
|
addr := oidtest.Address()
|
|
|
|
|
|
|
|
err := cache.Put(key, addr)
|
|
|
|
require.NoError(t, err)
|
|
|
|
val := cache.Get(key)
|
|
|
|
require.Equal(t, addr, *val)
|
|
|
|
require.Equal(t, 0, observedLog.Len())
|
|
|
|
|
|
|
|
err = cache.cache.Set(key, "tmp")
|
|
|
|
require.NoError(t, err)
|
|
|
|
assertInvalidCacheEntry(t, cache.Get(key), observedLog)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestObjectCacheType(t *testing.T) {
|
|
|
|
logger, observedLog := getObservedLogger()
|
|
|
|
cache := New(DefaultObjectsConfig(logger))
|
|
|
|
|
|
|
|
addr := oidtest.Address()
|
2022-08-08 22:32:33 +00:00
|
|
|
|
|
|
|
extObjInfo := &data.ExtendedObjectInfo{
|
|
|
|
ObjectInfo: &data.ObjectInfo{
|
|
|
|
ID: addr.Object(),
|
|
|
|
CID: addr.Container(),
|
|
|
|
},
|
|
|
|
NodeVersion: &data.NodeVersion{
|
|
|
|
BaseNodeVersion: data.BaseNodeVersion{
|
|
|
|
FilePath: "obj",
|
|
|
|
Size: 50,
|
|
|
|
},
|
|
|
|
IsUnversioned: true,
|
|
|
|
},
|
|
|
|
IsLatest: true,
|
2022-06-24 09:26:43 +00:00
|
|
|
}
|
2022-06-06 08:01:12 +00:00
|
|
|
|
2022-08-08 22:32:33 +00:00
|
|
|
err := cache.PutObject(extObjInfo)
|
2022-06-06 08:01:12 +00:00
|
|
|
require.NoError(t, err)
|
2022-06-24 09:26:43 +00:00
|
|
|
val := cache.GetObject(addr)
|
2022-08-08 22:32:33 +00:00
|
|
|
require.Equal(t, extObjInfo, val)
|
2022-06-06 08:01:12 +00:00
|
|
|
require.Equal(t, 0, observedLog.Len())
|
|
|
|
|
2022-06-28 12:56:41 +00:00
|
|
|
err = cache.cache.Set(addr, "tmp")
|
2022-06-06 08:01:12 +00:00
|
|
|
require.NoError(t, err)
|
2022-06-24 09:26:43 +00:00
|
|
|
assertInvalidCacheEntry(t, cache.GetObject(addr), observedLog)
|
2022-06-06 08:01:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestObjectsListCacheType(t *testing.T) {
|
|
|
|
logger, observedLog := getObservedLogger()
|
|
|
|
cache := NewObjectsListCache(DefaultObjectsListConfig(logger))
|
|
|
|
|
|
|
|
cnrID := cidtest.ID()
|
2022-06-28 12:56:41 +00:00
|
|
|
key := ObjectsListKey{cid: cnrID, prefix: "obj"}
|
2022-10-24 14:44:11 +00:00
|
|
|
versions := []*data.NodeVersion{{BaseNodeVersion: data.BaseNodeVersion{OID: oidtest.ID()}}}
|
2022-06-06 08:01:12 +00:00
|
|
|
|
2022-10-24 14:44:11 +00:00
|
|
|
err := cache.PutVersions(key, versions)
|
2022-06-06 08:01:12 +00:00
|
|
|
require.NoError(t, err)
|
2022-10-24 14:44:11 +00:00
|
|
|
val := cache.GetVersions(key)
|
|
|
|
require.Equal(t, versions, val)
|
2022-06-06 08:01:12 +00:00
|
|
|
require.Equal(t, 0, observedLog.Len())
|
|
|
|
|
|
|
|
err = cache.cache.Set(key, "tmp")
|
|
|
|
require.NoError(t, err)
|
2022-10-24 14:44:11 +00:00
|
|
|
assertInvalidCacheEntry(t, cache.GetVersions(key), observedLog)
|
2022-06-06 08:01:12 +00:00
|
|
|
|
|
|
|
err = cache.cache.Set("key", "tmp")
|
|
|
|
require.NoError(t, err)
|
|
|
|
cache.CleanCacheEntriesContainingObject(key.prefix, cnrID)
|
|
|
|
require.Equal(t, 2, observedLog.Len())
|
|
|
|
require.Equal(t, observedLog.All()[1].Message, "invalid cache key type")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestObjectInfoCacheType(t *testing.T) {
|
|
|
|
logger, observedLog := getObservedLogger()
|
|
|
|
cache := NewSystemCache(DefaultSystemConfig(logger))
|
|
|
|
|
|
|
|
key := "key"
|
|
|
|
objInfo := &data.ObjectInfo{Name: key}
|
|
|
|
|
|
|
|
err := cache.PutObject(key, objInfo)
|
|
|
|
require.NoError(t, err)
|
|
|
|
val := cache.GetObject(key)
|
|
|
|
require.Equal(t, objInfo, val)
|
|
|
|
require.Equal(t, 0, observedLog.Len())
|
|
|
|
|
|
|
|
err = cache.cache.Set(key, "tmp")
|
|
|
|
require.NoError(t, err)
|
|
|
|
assertInvalidCacheEntry(t, cache.GetObject(key), observedLog)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCORsCacheType(t *testing.T) {
|
|
|
|
logger, observedLog := getObservedLogger()
|
|
|
|
cache := NewSystemCache(DefaultSystemConfig(logger))
|
|
|
|
|
|
|
|
key := "key"
|
|
|
|
cors := &data.CORSConfiguration{}
|
|
|
|
|
|
|
|
err := cache.PutCORS(key, cors)
|
|
|
|
require.NoError(t, err)
|
|
|
|
val := cache.GetCORS(key)
|
|
|
|
require.Equal(t, cors, val)
|
|
|
|
require.Equal(t, 0, observedLog.Len())
|
|
|
|
|
|
|
|
err = cache.cache.Set(key, "tmp")
|
|
|
|
require.NoError(t, err)
|
|
|
|
assertInvalidCacheEntry(t, cache.GetCORS(key), observedLog)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSettingsCacheType(t *testing.T) {
|
|
|
|
logger, observedLog := getObservedLogger()
|
|
|
|
cache := NewSystemCache(DefaultSystemConfig(logger))
|
|
|
|
|
|
|
|
key := "key"
|
2022-07-20 10:30:19 +00:00
|
|
|
settings := &data.BucketSettings{Versioning: data.VersioningEnabled}
|
2022-06-06 08:01:12 +00:00
|
|
|
|
|
|
|
err := cache.PutSettings(key, settings)
|
|
|
|
require.NoError(t, err)
|
|
|
|
val := cache.GetSettings(key)
|
|
|
|
require.Equal(t, settings, val)
|
|
|
|
require.Equal(t, 0, observedLog.Len())
|
|
|
|
|
|
|
|
err = cache.cache.Set(key, "tmp")
|
|
|
|
require.NoError(t, err)
|
|
|
|
assertInvalidCacheEntry(t, cache.GetSettings(key), observedLog)
|
|
|
|
}
|
|
|
|
|
2024-03-12 08:28:24 +00:00
|
|
|
func TestFrostFSIDSubjectCacheType(t *testing.T) {
|
|
|
|
logger, observedLog := getObservedLogger()
|
|
|
|
cache := NewFrostfsIDCache(DefaultFrostfsIDConfig(logger))
|
|
|
|
|
|
|
|
key, err := util.Uint160DecodeStringLE("4ea976429703418ef00fc4912a409b6a0b973034")
|
|
|
|
require.NoError(t, err)
|
|
|
|
value := &client.SubjectExtended{}
|
|
|
|
|
|
|
|
err = cache.PutSubject(key, value)
|
|
|
|
require.NoError(t, err)
|
|
|
|
val := cache.GetSubject(key)
|
|
|
|
require.Equal(t, value, val)
|
|
|
|
require.Equal(t, 0, observedLog.Len())
|
|
|
|
|
|
|
|
err = cache.cache.Set(key, "tmp")
|
|
|
|
require.NoError(t, err)
|
|
|
|
assertInvalidCacheEntry(t, cache.GetSubject(key), observedLog)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFrostFSIDUserKeyCacheType(t *testing.T) {
|
|
|
|
logger, observedLog := getObservedLogger()
|
|
|
|
cache := NewFrostfsIDCache(DefaultFrostfsIDConfig(logger))
|
|
|
|
|
|
|
|
ns, name := "ns", "name"
|
|
|
|
value, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = cache.PutUserKey(ns, name, value.PublicKey())
|
|
|
|
require.NoError(t, err)
|
|
|
|
val := cache.GetUserKey(ns, name)
|
|
|
|
require.Equal(t, value.PublicKey(), val)
|
|
|
|
require.Equal(t, 0, observedLog.Len())
|
|
|
|
|
|
|
|
err = cache.cache.Set(ns+"/"+name, "tmp")
|
|
|
|
require.NoError(t, err)
|
|
|
|
assertInvalidCacheEntry(t, cache.GetUserKey(ns, name), observedLog)
|
|
|
|
}
|
|
|
|
|
2022-06-06 08:01:12 +00:00
|
|
|
func assertInvalidCacheEntry(t *testing.T, val interface{}, observedLog *observer.ObservedLogs) {
|
|
|
|
require.Nil(t, val)
|
|
|
|
require.Equal(t, 1, observedLog.Len())
|
|
|
|
require.Equal(t, observedLog.All()[0].Message, "invalid cache entry type")
|
|
|
|
}
|
|
|
|
|
|
|
|
func getObservedLogger() (*zap.Logger, *observer.ObservedLogs) {
|
|
|
|
loggerCore, observedLog := observer.New(zap.WarnLevel)
|
|
|
|
return zap.New(loggerCore), observedLog
|
|
|
|
}
|
2024-10-11 08:32:36 +00:00
|
|
|
|
|
|
|
func getAccessKeyID(addr oid.Address) string {
|
|
|
|
return strings.ReplaceAll(addr.EncodeToString(), "/", "0")
|
|
|
|
}
|