-
Notifications
You must be signed in to change notification settings - Fork 1
/
etcd_test.go
99 lines (80 loc) · 2.42 KB
/
etcd_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
94
95
96
97
98
99
//go:build integration
package integration_test
import (
"context"
"fmt"
"strconv"
"testing"
"time"
"github.com/avast/retry-go/v4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
clientv3 "go.etcd.io/etcd/client/v3"
"github.com/metal-stack/backup-restore-sidecar/pkg/generate/examples/examples"
)
func Test_ETCD_Restore(t *testing.T) {
restoreFlow(t, &flowSpec{
databaseType: examples.Etcd,
sts: examples.EtcdSts,
backingResources: examples.EtcdBackingResources,
addTestData: addEtcdTestData,
verifyTestData: verifyEtcdTestData,
})
}
func Test_ETCD_RestoreLatestFromMultipleBackups(t *testing.T) {
restoreLatestFromMultipleBackupsFlow(t, &flowSpec{
databaseType: examples.Etcd,
sts: examples.EtcdSts,
backingResources: examples.EtcdBackingResources,
addTestDataWithIndex: addEtcdTestDataWithIndex,
verifyTestDataWithIndex: verifyEtcdTestDataWithIndex,
})
}
func newEtcdClient(t *testing.T, ctx context.Context) *clientv3.Client {
var cli *clientv3.Client
err := retry.Do(func() error {
var err error
cli, err = clientv3.New(clientv3.Config{
Endpoints: []string{"localhost:32379"},
DialTimeout: 5 * time.Second,
})
if err != nil {
return err
}
return nil
}, retry.Context(ctx))
require.NoError(t, err)
return cli
}
func addEtcdTestData(t *testing.T, ctx context.Context) {
cli := newEtcdClient(t, ctx)
defer cli.Close()
_, err := cli.Put(ctx, "1", "I am precious")
require.NoError(t, err)
}
func verifyEtcdTestData(t *testing.T, ctx context.Context) {
cli := newEtcdClient(t, ctx)
defer cli.Close()
resp, err := cli.Get(ctx, "1")
require.NoError(t, err)
require.Len(t, resp.Kvs, 1)
ev := resp.Kvs[0]
assert.Equal(t, "1", string(ev.Key))
assert.Equal(t, "I am precious", string(ev.Value))
}
func addEtcdTestDataWithIndex(t *testing.T, ctx context.Context, index int) {
cli := newEtcdClient(t, ctx)
defer cli.Close()
_, err := cli.Put(ctx, strconv.Itoa(index), fmt.Sprintf("idx-%d", index))
require.NoError(t, err)
}
func verifyEtcdTestDataWithIndex(t *testing.T, ctx context.Context, index int) {
cli := newEtcdClient(t, ctx)
defer cli.Close()
resp, err := cli.Get(ctx, strconv.Itoa(index))
require.NoError(t, err)
require.Len(t, resp.Kvs, 1)
ev := resp.Kvs[0]
assert.Equal(t, strconv.Itoa(index), string(ev.Key))
assert.Equal(t, fmt.Sprintf("idx-%d", index), string(ev.Value))
}