Build: Fix Redis/Memcached integration tests (#64298)

* Build: Fix integration cache tests

* Allow REDIS_URL with scheme

* Reduce cache integration tests timeout to 5m

* Apply suggestion from code review

* Run redis/memcached integration tests in OSS pipelines

* Change redis image
This commit is contained in:
Sofia Papagiannaki
2023-04-05 11:55:55 +03:00
committed by GitHub
parent f69304fd50
commit caac9838d8
10 changed files with 215 additions and 57 deletions

View File

@@ -1,16 +1,23 @@
//go:build memcached
// +build memcached
package remotecache
import (
"os"
"testing"
"github.com/grafana/grafana/pkg/setting"
)
func TestMemcachedCacheStorage(t *testing.T) {
opts := &setting.RemoteCacheOptions{Name: memcachedCacheType, ConnStr: "localhost:11211"}
func TestIntegrationMemcachedCacheStorage(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
u, ok := os.LookupEnv("MEMCACHED_HOSTS")
if !ok || u == "" {
t.Skip("No Memcached hosts provided")
}
opts := &setting.RemoteCacheOptions{Name: memcachedCacheType, ConnStr: u}
client := createTestClient(t, opts, nil)
runTestsForClient(t, client)
runCountTestsForClient(t, opts, nil)

View File

@@ -1,17 +1,40 @@
//go:build redis
// +build redis
package remotecache
import (
"fmt"
"os"
"strings"
"testing"
"github.com/go-redis/redis/v8"
"github.com/grafana/grafana/pkg/setting"
)
func TestRedisCacheStorage(t *testing.T) {
func TestIntegrationRedisCacheStorage(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
opts := &setting.RemoteCacheOptions{Name: redisCacheType, ConnStr: "addr=localhost:6379"}
u, ok := os.LookupEnv("REDIS_URL")
if !ok || u == "" {
t.Skip("No redis URL supplied")
}
addr := u
db := 0
parsed, err := redis.ParseURL(u)
if err == nil {
addr = parsed.Addr
db = parsed.DB
}
b := strings.Builder{}
b.WriteString(fmt.Sprintf("addr=%s", addr))
if db != 0 {
b.WriteString(fmt.Sprintf(",db=%d", db))
}
opts := &setting.RemoteCacheOptions{Name: redisCacheType, ConnStr: b.String()}
client := createTestClient(t, opts, nil)
runTestsForClient(t, client)
runCountTestsForClient(t, opts, nil)

View File

@@ -95,7 +95,8 @@ func canPutGetAndDeleteCachedObjects(t *testing.T, client CacheStorage) {
assert.Equal(t, err, nil)
_, err = client.Get(context.Background(), "key1")
assert.Equal(t, err, ErrCacheItemNotFound)
// redis client returns redis.Nil error when key does not exist.
assert.Error(t, err)
}
func canNotFetchExpiredItems(t *testing.T, client CacheStorage) {
@@ -109,7 +110,8 @@ func canNotFetchExpiredItems(t *testing.T, client CacheStorage) {
// should not be able to read that value since its expired
_, err = client.Get(context.Background(), "key1")
assert.Equal(t, err, ErrCacheItemNotFound)
// redis client returns redis.Nil error when key does not exist.
assert.Error(t, err)
}
func TestCollectUsageStats(t *testing.T) {

View File

@@ -1,18 +1,34 @@
//go:build redis
// +build redis
package managedstream
import (
"os"
"testing"
"github.com/go-redis/redis/v8"
"github.com/stretchr/testify/require"
)
func TestRedisCacheStorage(t *testing.T) {
func TestIntegrationRedisCacheStorage(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
u, ok := os.LookupEnv("REDIS_URL")
if !ok || u == "" {
t.Skip("No redis URL supplied")
}
addr := u
db := 0
parsed, err := redis.ParseURL(u)
if err == nil {
addr = parsed.Addr
db = parsed.DB
}
redisClient := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Addr: addr,
DB: db,
})
c := NewRedisFrameCache(redisClient)
require.NotNil(t, c)