Browse Source

return error if cache type is invalid

bergquist 7 years ago
parent
commit
5186273731

+ 2 - 1
pkg/infra/remotecache/memcached_storage_integration_test.go

@@ -10,5 +10,6 @@ import (
 
 func TestMemcachedCacheStorage(t *testing.T) {
 	opts := &setting.RemoteCacheOptions{Name: "memcached", ConnStr: "localhost:11211"}
-	runTestsForClient(t, createTestClient(t, opts, nil))
+	client := createTestClient(t, opts, nil)
+	runTestsForClient(t, client)
 }

+ 2 - 1
pkg/infra/remotecache/redis_storage_integration_test.go

@@ -11,5 +11,6 @@ import (
 func TestRedisCacheStorage(t *testing.T) {
 
 	opts := &setting.RemoteCacheOptions{Name: "redis", ConnStr: "localhost:6379"}
-	runTestsForClient(t, createTestClient(t, opts, nil))
+	client := createTestClient(t, opts, nil)
+	runTestsForClient(t, client)
 }

+ 15 - 8
pkg/infra/remotecache/remotecache.go

@@ -16,7 +16,11 @@ import (
 )
 
 var (
+	// ErrCacheItemNotFound is returned if cache does not exist
 	ErrCacheItemNotFound = errors.New("cache item not found")
+
+	// ErrInvalidCacheType is returned if the type is invalid
+	ErrInvalidCacheType = errors.New("invalid remote cache name")
 )
 
 func init() {
@@ -61,10 +65,9 @@ func (ds *RemoteCache) Delete(key string) error {
 // Init initializes the service
 func (ds *RemoteCache) Init() error {
 	ds.log = log.New("cache.remote")
-
-	ds.client = createClient(ds.Cfg.RemoteCacheOptions, ds.SQLStore)
-
-	return nil
+	var err error
+	ds.client, err = createClient(ds.Cfg.RemoteCacheOptions, ds.SQLStore)
+	return err
 }
 
 // Run start the backend processes for cache clients
@@ -79,16 +82,20 @@ func (ds *RemoteCache) Run(ctx context.Context) error {
 	return ctx.Err()
 }
 
-func createClient(opts *setting.RemoteCacheOptions, sqlstore *sqlstore.SqlStore) CacheStorage {
+func createClient(opts *setting.RemoteCacheOptions, sqlstore *sqlstore.SqlStore) (CacheStorage, error) {
 	if opts.Name == "redis" {
-		return newRedisStorage(opts)
+		return newRedisStorage(opts), nil
 	}
 
 	if opts.Name == "memcached" {
-		return newMemcachedStorage(opts)
+		return newMemcachedStorage(opts), nil
+	}
+
+	if opts.Name == "database" {
+		return newDatabaseCache(sqlstore), nil
 	}
 
-	return newDatabaseCache(sqlstore)
+	return nil, ErrInvalidCacheType
 }
 
 // Register records a type, identified by a value for that type, under its

+ 5 - 1
pkg/infra/remotecache/remotecache_test.go

@@ -45,10 +45,14 @@ func TestCachedBasedOnConfig(t *testing.T) {
 	})
 
 	client := createTestClient(t, cfg.RemoteCacheOptions, sqlstore.InitTestDB(t))
-
 	runTestsForClient(t, client)
 }
 
+func TestInvalidCacheTypeReturnsError(t *testing.T) {
+	_, err := createClient(&setting.RemoteCacheOptions{Name: "invalid"}, nil)
+	assert.Equal(t, err, ErrInvalidCacheType)
+}
+
 func runTestsForClient(t *testing.T, client CacheStorage) {
 	canPutGetAndDeleteCachedObjects(t, client)
 	canNotFetchExpiredItems(t, client)