-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrethinkdb_test.go
168 lines (134 loc) · 4.31 KB
/
rethinkdb_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//go:build integration
package integration_test
import (
"context"
"fmt"
"strconv"
"testing"
"github.com/metal-stack/backup-restore-sidecar/pkg/generate/examples/examples"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/avast/retry-go/v4"
r "gopkg.in/rethinkdb/rethinkdb-go.v6"
)
const (
rethinkDbDatabaseName = "backup-restore"
rethinkDbTable = "precioustestdata"
)
type rethinkDbTestData struct {
ID string `rethinkdb:"id"`
Data string `rethinkdb:"data"`
}
func Test_RethinkDB_Restore(t *testing.T) {
restoreFlow(t, &flowSpec{
databaseType: examples.RethinkDB,
sts: examples.RethinkDbSts,
backingResources: examples.RethinkDbBackingResources,
addTestData: addRethinkDbTestData,
verifyTestData: verifyRethinkDbTestData,
})
}
func Test_RethinkDB_RestoreLatestFromMultipleBackups(t *testing.T) {
restoreLatestFromMultipleBackupsFlow(t, &flowSpec{
databaseType: examples.RethinkDB,
sts: examples.RethinkDbSts,
backingResources: examples.RethinkDbBackingResources,
addTestDataWithIndex: addRethinkDbTestDataWithIndex,
verifyTestDataWithIndex: verifyRethinkDbTestDataWithIndex,
})
}
func newRethinkdbSession(t *testing.T, ctx context.Context) *r.Session {
var session *r.Session
err := retry.Do(func() error {
var err error
session, err = r.Connect(r.ConnectOpts{
Addresses: []string{"localhost:28015"},
Database: rethinkDbDatabaseName,
Username: "admin",
Password: examples.RethinkDbPassword,
MaxIdle: 10,
MaxOpen: 20,
})
if err != nil {
return fmt.Errorf("cannot connect to DB: %w", err)
}
return nil
}, retry.Context(ctx))
require.NoError(t, err)
return session
}
func addRethinkDbTestData(t *testing.T, ctx context.Context) {
session := newRethinkdbSession(t, ctx)
_, err := r.DBCreate(rethinkDbDatabaseName).RunWrite(session)
require.NoError(t, err)
_, err = r.DB(rethinkDbDatabaseName).TableCreate(rethinkDbTable).RunWrite(session)
require.NoError(t, err)
_, err = r.DB(rethinkDbDatabaseName).Table(rethinkDbTable).Insert(rethinkDbTestData{
ID: "1",
Data: "i am precious",
}).RunWrite(session)
require.NoError(t, err)
cursor, err := r.DB(rethinkDbDatabaseName).Table(rethinkDbTable).Get("1").Run(session)
require.NoError(t, err)
var d1 rethinkDbTestData
err = cursor.One(&d1)
require.NoError(t, err)
require.Equal(t, "i am precious", d1.Data)
}
func verifyRethinkDbTestData(t *testing.T, ctx context.Context) {
session := newRethinkdbSession(t, ctx)
var d2 rethinkDbTestData
err := retry.Do(func() error {
cursor, err := r.DB(rethinkDbDatabaseName).Table(rethinkDbTable).Get("1").Run(session)
if err != nil {
return err
}
err = cursor.One(&d2)
if err != nil {
return err
}
return nil
})
require.NoError(t, err)
assert.Equal(t, "i am precious", d2.Data)
}
func addRethinkDbTestDataWithIndex(t *testing.T, ctx context.Context, index int) {
session := newRethinkdbSession(t, ctx)
_, err := r.Expr([]string{rethinkDbDatabaseName}).Difference(r.DBList()).ForEach(func(x r.Term) r.Term {
return r.DBCreate(rethinkDbDatabaseName)
}).RunWrite(session)
require.NoError(t, err)
db := r.DB(rethinkDbDatabaseName)
_, err = r.Expr([]string{rethinkDbTable}).Difference(db.TableList()).ForEach(func(r r.Term) r.Term {
return db.TableCreate(r)
}).RunWrite(session)
require.NoError(t, err)
_, err = r.DB(rethinkDbDatabaseName).Table(rethinkDbTable).Insert(rethinkDbTestData{
ID: strconv.Itoa(index),
Data: fmt.Sprintf("idx-%d", index),
}).RunWrite(session)
require.NoError(t, err)
cursor, err := r.DB(rethinkDbDatabaseName).Table(rethinkDbTable).Get(strconv.Itoa(index)).Run(session)
require.NoError(t, err)
var d1 rethinkDbTestData
err = cursor.One(&d1)
require.NoError(t, err)
require.Equal(t, fmt.Sprintf("idx-%d", index), d1.Data)
}
func verifyRethinkDbTestDataWithIndex(t *testing.T, ctx context.Context, index int) {
session := newRethinkdbSession(t, ctx)
var d2 rethinkDbTestData
err := retry.Do(func() error {
cursor, err := r.DB(rethinkDbDatabaseName).Table(rethinkDbTable).Get(strconv.Itoa(index)).Run(session)
if err != nil {
return err
}
err = cursor.One(&d2)
if err != nil {
return err
}
return nil
})
require.NoError(t, err)
assert.Equal(t, fmt.Sprintf("idx-%d", index), d2.Data)
}