diff --git a/pkg/api/controller_test.go b/pkg/api/controller_test.go index 2d7f9ef2e..a61de5fb3 100644 --- a/pkg/api/controller_test.go +++ b/pkg/api/controller_test.go @@ -160,12 +160,23 @@ func TestCreateCacheDatabaseDriver(t *testing.T) { log := log.NewLogger("debug", "") - // fail create db, no perm dir := t.TempDir() conf := config.New() conf.Storage.RootDirectory = dir conf.Storage.Dedupe = true conf.Storage.RemoteCache = true + + // test error on invalid redis client config + conf.Storage.CacheDriver = map[string]interface{}{ + "name": "redis", + "url": false, + } + + driver, err := storage.CreateCacheDatabaseDriver(conf.Storage.StorageConfig, log) + So(err, ShouldNotBeNil) + So(driver, ShouldBeNil) + + // test valid redis client config conf.Storage.CacheDriver = map[string]interface{}{ "name": "redis", "url": "redis://" + miniRedis.Addr(), @@ -178,7 +189,7 @@ func TestCreateCacheDatabaseDriver(t *testing.T) { "url": "us-east-2", } - driver, err := storage.CreateCacheDatabaseDriver(conf.Storage.StorageConfig, log) + driver, err = storage.CreateCacheDatabaseDriver(conf.Storage.StorageConfig, log) So(err, ShouldBeNil) So(driver, ShouldNotBeNil) So(driver.Name(), ShouldEqual, "redis") diff --git a/pkg/storage/cache.go b/pkg/storage/cache.go index 430840ee8..ec80f259e 100644 --- a/pkg/storage/cache.go +++ b/pkg/storage/cache.go @@ -43,27 +43,16 @@ func CreateCacheDatabaseDriver(storageConfig config.StorageConfig, log zlog.Logg if name == constants.DynamoDBDriverName { // dynamodb - dynamoParams := cache.DynamoDBDriverParameters{} - dynamoParams.Endpoint, _ = storageConfig.CacheDriver["endpoint"].(string) - dynamoParams.Region, _ = storageConfig.CacheDriver["region"].(string) - dynamoParams.TableName, _ = storageConfig.CacheDriver["cachetablename"].(string) - - return Create(name, dynamoParams, log) + return Create(name, getDynamoParams(&storageConfig), log) } if name == constants.RedisDriverName { // redis - client, err := rediscfg.GetRedisClient(storageConfig.CacheDriver, log) + redisParams, err := getRedisParams(&storageConfig, log) if err != nil { return nil, err } - redisParams := cache.RedisDriverParameters{} - redisParams.RootDir = storageConfig.RootDirectory - redisParams.Client = client - redisParams.KeyPrefix, _ = storageConfig.CacheDriver["keyprefix"].(string) - redisParams.UseRelPaths = getUseRelPaths(&storageConfig) - return Create(name, redisParams, log) } } @@ -96,3 +85,28 @@ func getUseRelPaths(storageConfig *config.StorageConfig) bool { // In case of local storage we use rel paths, in case of S3 we don't return storageConfig.StorageDriver == nil } + +func getDynamoParams(storageConfig *config.StorageConfig) cache.DynamoDBDriverParameters { + dynamoParams := cache.DynamoDBDriverParameters{} + dynamoParams.Endpoint, _ = storageConfig.CacheDriver["endpoint"].(string) + dynamoParams.Region, _ = storageConfig.CacheDriver["region"].(string) + dynamoParams.TableName, _ = storageConfig.CacheDriver["cachetablename"].(string) + + return dynamoParams +} + +func getRedisParams(storageConfig *config.StorageConfig, log zlog.Logger) (cache.RedisDriverParameters, error) { + redisParams := cache.RedisDriverParameters{} + + client, err := rediscfg.GetRedisClient(storageConfig.CacheDriver, log) + if err != nil { + return redisParams, err + } + + redisParams.RootDir = storageConfig.RootDirectory + redisParams.Client = client + redisParams.KeyPrefix, _ = storageConfig.CacheDriver["keyprefix"].(string) + redisParams.UseRelPaths = getUseRelPaths(storageConfig) + + return redisParams, nil +}