-
Notifications
You must be signed in to change notification settings - Fork 1
/
redis_test.go
93 lines (75 loc) · 2.28 KB
/
redis_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//go:build integration
package integration_test
import (
"context"
"fmt"
"strconv"
"testing"
"time"
"github.com/avast/retry-go/v4"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/metal-stack/backup-restore-sidecar/pkg/generate/examples/examples"
)
func Test_Redis_Restore(t *testing.T) {
restoreFlow(t, &flowSpec{
databaseType: examples.Redis,
sts: examples.RedisSts,
backingResources: examples.RedisBackingResources,
addTestData: addRedisTestData,
verifyTestData: verifyRedisTestData,
})
}
func Test_Redis_RestoreLatestFromMultipleBackups(t *testing.T) {
restoreLatestFromMultipleBackupsFlow(t, &flowSpec{
databaseType: examples.Redis,
sts: examples.RedisSts,
backingResources: examples.RedisBackingResources,
addTestDataWithIndex: addRedisTestDataWithIndex,
verifyTestDataWithIndex: verifyRedisTestDataWithIndex,
})
}
func newRedisClient(t *testing.T, ctx context.Context) *redis.Client {
var cli *redis.Client
err := retry.Do(func() error {
var err error
cli = redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
if err != nil {
return err
}
return nil
}, retry.Context(ctx))
require.NoError(t, err)
return cli
}
func addRedisTestData(t *testing.T, ctx context.Context) {
cli := newRedisClient(t, ctx)
defer cli.Close()
_, err := cli.Set(ctx, "1", "I am precious", 1*time.Hour).Result()
require.NoError(t, err)
}
func verifyRedisTestData(t *testing.T, ctx context.Context) {
cli := newRedisClient(t, ctx)
defer cli.Close()
resp, err := cli.Get(ctx, "1").Result()
require.NoError(t, err)
require.NotEmpty(t, resp)
assert.Equal(t, "I am precious", resp)
}
func addRedisTestDataWithIndex(t *testing.T, ctx context.Context, index int) {
cli := newRedisClient(t, ctx)
defer cli.Close()
_, err := cli.Set(ctx, strconv.Itoa(index), fmt.Sprintf("idx-%d", index), 1*time.Hour).Result()
require.NoError(t, err)
}
func verifyRedisTestDataWithIndex(t *testing.T, ctx context.Context, index int) {
cli := newRedisClient(t, ctx)
defer cli.Close()
resp, err := cli.Get(ctx, strconv.Itoa(index)).Result()
require.NoError(t, err)
require.NotEmpty(t, resp)
assert.Equal(t, fmt.Sprintf("idx-%d", index), resp)
}