Skip to content

Commit

Permalink
feat(redis): more tests for redis metadb implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Andrei Aaron <[email protected]>
  • Loading branch information
andaaron committed Jan 22, 2025
1 parent 59934fb commit 1e62dc7
Show file tree
Hide file tree
Showing 3 changed files with 1,283 additions and 8 deletions.
33 changes: 31 additions & 2 deletions pkg/extensions/search/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"testing"
"time"

"github.com/alicebob/miniredis/v2"
guuid "github.com/gofrs/uuid"
regTypes "github.com/google/go-containerregistry/pkg/v1/types"
notreg "github.com/notaryproject/notation-go/registry"
Expand Down Expand Up @@ -3905,14 +3906,28 @@ func TestGlobalSearch(t *testing.T) { //nolint: gocyclo
So(len(results.Repos), ShouldEqual, 0)
})

Convey("test nested indexes", t, func() {
Convey("test nested indexes CVE scanning disabled", t, func() {
log := log.NewLogger("debug", "")
rootDir := t.TempDir()
port := GetFreePort()
baseURL := GetBaseURL(port)
conf := config.New()
conf.HTTP.Port = port
conf.Storage.RootDirectory = rootDir

Convey("test with boltdb", func() {
conf.Storage.CacheDriver = nil
})

Convey("test with redis", func() {
miniRedis := miniredis.RunT(t)

conf.Storage.CacheDriver = map[string]interface{}{
"name": "redis",
"url": "redis://" + miniRedis.Addr(),
}
})

defaultVal := true
conf.Extensions = &extconf.ExtensionConfig{
Search: &extconf.SearchConfig{BaseConfig: extconf.BaseConfig{Enable: &defaultVal}},
Expand Down Expand Up @@ -4054,14 +4069,28 @@ func TestGlobalSearch(t *testing.T) { //nolint: gocyclo
}
})

Convey("test nested indexes", t, func() {
Convey("test nested indexes CVE scanning enabled", t, func() {
log := log.NewLogger("debug", "")
rootDir := t.TempDir()
port := GetFreePort()
baseURL := GetBaseURL(port)
conf := config.New()
conf.HTTP.Port = port
conf.Storage.RootDirectory = rootDir

Convey("test with boltdb", func() {
conf.Storage.CacheDriver = nil
})

Convey("test with redis", func() {
miniRedis := miniredis.RunT(t)

conf.Storage.CacheDriver = map[string]interface{}{
"name": "redis",
"url": "redis://" + miniRedis.Addr(),
}
})

defaultVal := true

updateDuration, _ := time.ParseDuration("1h")
Expand Down
14 changes: 8 additions & 6 deletions pkg/meta/redisdb/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ func (rc *RedisDB) ToggleBookmarkRepo(ctx context.Context, repo string) (mTypes.

// UserDB profile/api key CRUD.
func (rc *RedisDB) GetUserData(ctx context.Context) (mTypes.UserData, error) {
var userData mTypes.UserData
userData := mTypes.UserData{}
userData.APIKeys = make(map[string]mTypes.APIKeyDetails)

userAc, err := reqCtx.UserAcFromContext(ctx)
if err != nil {
Expand All @@ -282,7 +283,7 @@ func (rc *RedisDB) GetUserData(ctx context.Context) (mTypes.UserData, error) {
rc.Log.Error().Err(err).Str("hget", rc.UserDataKey).Str("userid", userid).
Msg("failed to get user data record")

return mTypes.UserData{}, fmt.Errorf("failed to get user data record for identity %s: %w", userid, err)
return userData, fmt.Errorf("failed to get user data record for identity %s: %w", userid, err)
}

if errors.Is(err, redis.Nil) {
Expand All @@ -291,6 +292,11 @@ func (rc *RedisDB) GetUserData(ctx context.Context) (mTypes.UserData, error) {

err = json.Unmarshal(userDataBlob, &userData)

if userData.APIKeys == nil {
// Unmarshal may have reset the value
userData.APIKeys = make(map[string]mTypes.APIKeyDetails)
}

return userData, err
}

Expand Down Expand Up @@ -501,10 +507,6 @@ func (rc *RedisDB) AddUserAPIKey(ctx context.Context, hashedKey string, apiKeyDe
return err
}

if userData.APIKeys == nil {
userData.APIKeys = make(map[string]mTypes.APIKeyDetails)
}

userData.APIKeys[hashedKey] = *apiKeyDetails

userDataBlob, err := json.Marshal(userData)
Expand Down
Loading

0 comments on commit 1e62dc7

Please sign in to comment.