generated from cybozu-go/neco-template
-
Notifications
You must be signed in to change notification settings - Fork 22
/
replication_test.go
350 lines (318 loc) · 10.7 KB
/
replication_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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package e2e
import (
_ "embed"
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
mocov1beta1 "github.com/cybozu-go/moco/api/v1beta1"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
)
//go:embed testdata/donor.yaml
var donorYAML string
//go:embed testdata/replication.yaml
var replYAML string
var _ = Context("replication", func() {
if doUpgrade {
return
}
It("should prepare a donor instance", func() {
kubectlSafe(fillTemplate(donorYAML), "apply", "-f", "-")
Eventually(func() error {
cluster, err := getCluster("donor", "single")
if err != nil {
return err
}
for _, cond := range cluster.Status.Conditions {
if cond.Type != mocov1beta1.ConditionHealthy {
continue
}
if cond.Status == corev1.ConditionTrue {
return nil
}
return fmt.Errorf("cluster is not healthy: %s", cond.Status)
}
return errors.New("no health condition")
}).Should(Succeed())
kubectlSafe(nil, "moco", "-n", "donor", "mysql", "-u", "moco-writable", "single", "--",
"-e", "CREATE DATABASE test")
kubectlSafe(nil, "moco", "-n", "donor", "mysql", "-u", "moco-writable", "single", "--",
"-D", "test", "-e", "CREATE TABLE t (i INT PRIMARY KEY AUTO_INCREMENT, data TEXT NOT NULL) ENGINE=InnoDB")
kubectlSafe(nil, "moco", "-n", "donor", "mysql", "-u", "moco-writable", "single", "--",
"-D", "test", "--init_command=SET autocommit=1", "-e", "INSERT INTO t (data) VALUES ('aaa'), ('bbb'), ('ccc')")
kubectlSafe(nil, "moco", "-n", "donor", "mysql", "-u", "moco-admin", "single", "--",
"-e", "CREATE USER donor IDENTIFIED BY 'abc'")
kubectlSafe(nil, "moco", "-n", "donor", "mysql", "-u", "moco-admin", "single", "--",
"-e", "GRANT BACKUP_ADMIN, REPLICATION SLAVE ON *.* TO 'donor'@'%'")
kubectlSafe(nil, "moco", "-n", "donor", "mysql", "-u", "moco-admin", "single", "--",
"-e", "DROP USER 'moco-readonly'@'%'")
kubectlSafe(nil, "moco", "-n", "donor", "mysql", "-u", "moco-admin", "single", "--",
"-e", "DROP USER 'moco-writable'@'%'")
})
It("should construct an intermediate primary and replicas", func() {
kubectlSafe(fillTemplate(replYAML), "apply", "-f", "-")
out := kubectlSafe(nil, "moco", "-n", "donor", "credential", "-u", "moco-admin", "single")
adminPasswd := strings.TrimSpace(string(out))
kubectlSafe(nil, "-n", "repl", "create", "secret", "generic", "donor",
"--from-literal=HOST=moco-single-primary.donor.svc",
"--from-literal=PORT=3306",
"--from-literal=USER=donor",
"--from-literal=PASSWORD=abc",
"--from-literal=INIT_USER=moco-admin",
"--from-literal=INIT_PASSWORD="+adminPasswd,
)
Eventually(func() error {
cluster, err := getCluster("repl", "test")
if err != nil {
return err
}
for _, cond := range cluster.Status.Conditions {
if cond.Type != mocov1beta1.ConditionHealthy {
continue
}
if cond.Status == corev1.ConditionTrue {
return nil
}
return fmt.Errorf("cluster is not healthy: %s", cond.Status)
}
return errors.New("no health condition")
}).Should(Succeed())
})
It("should replicate data from the donor", func() {
out := kubectlSafe(nil, "moco", "-n", "repl", "mysql", "--index", "1", "test", "--",
"-N", "-D", "test", "-e", "SELECT COUNT(*) FROM t")
count, err := strconv.Atoi(strings.TrimSpace(string(out)))
Expect(err).NotTo(HaveOccurred())
Expect(count).To(Equal(3))
kubectlSafe(nil, "moco", "-n", "donor", "mysql", "-u", "moco-admin", "single", "--",
"-D", "test", "--init_command=SET autocommit=1", "-e", "INSERT INTO t (data) VALUES ('ddd')")
Eventually(func() int {
out, err := kubectl(nil, "moco", "-n", "repl", "mysql", "--index", "2", "test", "--",
"-N", "-D", "test", "-e", "SELECT COUNT(*) FROM t")
if err != nil {
return 0
}
count, _ := strconv.Atoi(strings.TrimSpace(string(out)))
return count
}).Should(Equal(4))
})
It("should switch the primary if requested", func() {
kubectlSafe(nil, "moco", "-n", "repl", "switchover", "test")
Eventually(func() int {
cluster, err := getCluster("repl", "test")
if err != nil {
return 0
}
return cluster.Status.CurrentPrimaryIndex
}).ShouldNot(Equal(0))
Eventually(func() error {
cluster, err := getCluster("repl", "test")
if err != nil {
return err
}
for _, cond := range cluster.Status.Conditions {
if cond.Type != mocov1beta1.ConditionHealthy {
continue
}
if cond.Status == corev1.ConditionTrue {
return nil
}
return fmt.Errorf("cluster is not healthy: %s", cond.Status)
}
return errors.New("no health condition")
}).Should(Succeed())
})
It("should be able to scale out the cluster", func() {
Eventually(func() error {
cluster, err := getCluster("repl", "test")
if err != nil {
return err
}
cluster.Spec.Replicas = 5
data, _ := json.Marshal(cluster)
_, err = kubectl(data, "apply", "-f", "-")
return err
}).Should(Succeed())
Eventually(func() error {
cluster, err := getCluster("repl", "test")
if err != nil {
return err
}
if cluster.Status.SyncedReplicas != 5 {
return fmt.Errorf("synced replicas is not 5: %d", cluster.Status.SyncedReplicas)
}
for _, cond := range cluster.Status.Conditions {
if cond.Type != mocov1beta1.ConditionHealthy {
continue
}
if cond.Status == corev1.ConditionTrue {
return nil
}
return fmt.Errorf("cluster is not healthy: %s", cond.Status)
}
return errors.New("no health condition")
}).Should(Succeed())
})
It("should detect errant transactions", func() {
Eventually(func() error {
_, err := kubectl(nil, "moco", "-n", "repl", "mysql", "-u", "moco-admin", "--index", "0", "test", "--",
"-e", "SET GLOBAL read_only=0")
if err != nil {
return err
}
_, err = kubectl(nil, "moco", "-n", "repl", "mysql", "-u", "moco-admin", "--index", "0", "test", "--",
"-e", "CREATE DATABASE errant")
return err
}).Should(Succeed())
Eventually(func() int {
cluster, err := getCluster("repl", "test")
if err != nil {
return 0
}
return cluster.Status.ErrantReplicas
}).Should(Equal(1))
})
It("should do a failover if the primary lost data", func() {
cluster, err := getCluster("repl", "test")
Expect(err).NotTo(HaveOccurred())
primary := cluster.Status.CurrentPrimaryIndex
kubectlSafe(nil, "delete", "-n", "repl", "--wait=false", "pvc", fmt.Sprintf("mysql-data-moco-test-%d", primary))
kubectlSafe(nil, "delete", "-n", "repl", "--grace-period=1", "pod", cluster.PodName(primary))
Eventually(func() error {
out, err := kubectl(nil, "-n", "repl", "get", "pod", cluster.PodName(primary), "-o", "json")
if err != nil {
return err
}
pod := &corev1.Pod{}
err = json.Unmarshal(out, pod)
if err != nil {
return err
}
for _, cond := range pod.Status.Conditions {
if cond.Type != corev1.PodScheduled {
continue
}
if cond.Reason == "Unschedulable" {
fmt.Println("re-deleting pending pod...")
_, err := kubectl(nil, "delete", "-n", "repl", "--grace-period=1", "pod", cluster.PodName(primary))
if err != nil {
return fmt.Errorf("failed to delete pod: %w", err)
}
return errors.New("pod is unschedulable")
}
if cond.Status == corev1.ConditionTrue {
return nil
}
}
return errors.New("no pod scheduled status")
}).Should(Succeed())
Eventually(func() error {
cluster, err := getCluster("repl", "test")
if err != nil {
return err
}
if cluster.Status.CurrentPrimaryIndex == primary {
return fmt.Errorf("primary is not changed from %d", primary)
}
for _, cond := range cluster.Status.Conditions {
if cond.Type != mocov1beta1.ConditionAvailable {
continue
}
if cond.Status == corev1.ConditionTrue {
return nil
}
return fmt.Errorf("cluster is not available: %s", cond.Status)
}
return errors.New("no available condition")
}).Should(Succeed())
})
It("should clear errant status after re-initializing the errant replica", func() {
cluster, err := getCluster("repl", "test")
Expect(err).NotTo(HaveOccurred())
kubectlSafe(nil, "delete", "-n", "repl", "--wait=false", "pvc", "mysql-data-moco-test-0")
kubectlSafe(nil, "delete", "-n", "repl", "--grace-period=1", "pod", cluster.PodName(0))
Eventually(func() error {
out, err := kubectl(nil, "-n", "repl", "get", "pod", cluster.PodName(0), "-o", "json")
if err != nil {
return err
}
pod := &corev1.Pod{}
err = json.Unmarshal(out, pod)
if err != nil {
return err
}
for _, cond := range pod.Status.Conditions {
if cond.Type != corev1.PodScheduled {
continue
}
if cond.Reason == "Unschedulable" {
fmt.Println("re-deleting pending pod...")
_, err := kubectl(nil, "delete", "-n", "repl", "--grace-period=1", "pod", cluster.PodName(0))
if err != nil {
return fmt.Errorf("failed to delete pod: %w", err)
}
return errors.New("pod is unschedulable")
}
if cond.Status == corev1.ConditionTrue {
return nil
}
}
return errors.New("no pod scheduled status")
}).Should(Succeed())
Eventually(func() error {
cluster, err := getCluster("repl", "test")
if err != nil {
return err
}
for _, cond := range cluster.Status.Conditions {
if cond.Type != mocov1beta1.ConditionHealthy {
continue
}
if cond.Status == corev1.ConditionTrue {
return nil
}
return fmt.Errorf("cluster is not healthy: %s", cond.Status)
}
return errors.New("no health condition")
}).Should(Succeed())
})
It("should be able to stop replication from the donor", func() {
Eventually(func() error {
cluster, err := getCluster("repl", "test")
if err != nil {
return err
}
cluster.Spec.ReplicationSourceSecretName = nil
data, _ := json.Marshal(cluster)
_, err = kubectl(data, "apply", "-f", "-")
return err
}).Should(Succeed())
Eventually(func() error {
_, err := kubectl(nil, "moco", "-n", "repl", "mysql", "-u", "moco-writable", "test", "--",
"-D", "test", "--init_command=SET autocommit=1", "-e", "INSERT INTO t (data) VALUES ('eee')")
return err
}).Should(Succeed())
})
It("should allow reads via Service", func() {
By("obtaining the credential")
out := kubectlSafe(nil, "moco", "-n", "repl", "credential", "-u", "moco-readonly", "test")
passwd := strings.TrimSpace(string(out))
By("running mysql in a pod")
Eventually(func() int {
out, err := runInPod("mysql", "-u", "moco-readonly", "-p"+passwd,
"-h", "moco-test-replica.repl.svc", "-N", "-D", "test", "-e", "SELECT COUNT(*) FROM t")
if err != nil {
return 0
}
count, _ := strconv.Atoi(strings.TrimSpace(string(out)))
return count
}).Should(Equal(5))
})
It("should delete clusters", func() {
kubectlSafe(nil, "delete", "-n", "donor", "mysqlclusters", "--all")
kubectlSafe(nil, "delete", "-n", "repl", "mysqlclusters", "--all")
})
})