-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe2e_test.go
More file actions
412 lines (361 loc) · 13.4 KB
/
e2e_test.go
File metadata and controls
412 lines (361 loc) · 13.4 KB
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
Copyright 2024.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package e2e
import (
"fmt"
"os/exec"
"strings"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/uselagoon/dbaas-controller/test/utils"
)
const (
namespace = "dbaas-controller-system"
timeout = "300s"
)
var _ = Describe("controller", Ordered, func() {
BeforeAll(func() {
By("installing prometheus operator")
Expect(utils.InstallPrometheusOperator()).To(Succeed())
By("installing the cert-manager")
Expect(utils.InstallCertManager()).To(Succeed())
By("installing relational databases pods")
Expect(utils.InstallRelationalDatabases()).To(Succeed())
By("installing mongodb pods")
Expect(utils.InstallMongoDB()).To(Succeed())
By("creating manager namespace")
cmd := exec.Command("kubectl", "create", "ns", namespace)
_, _ = utils.Run(cmd)
})
AfterAll(func() {
By("uninstalling the Prometheus manager bundle")
utils.UninstallPrometheusOperator()
By("uninstalling the cert-manager bundle")
utils.UninstallCertManager()
By("removing the RelationalDatabaseProvider resource")
for _, name := range []string{"mysql", "mysql-scope", "postgres", "mongodb"} {
cmd := exec.Command(
"kubectl",
"patch",
"relationaldatabaseprovider",
fmt.Sprintf("relationaldatabaseprovider-%s-sample", name),
"-p",
`{"metadata":{"finalizers":[]}}`,
"--type=merge",
)
_, _ = utils.Run(cmd)
cmd = exec.Command(
"kubectl", "delete", "--force", "relationaldatabaseprovider", fmt.Sprintf(
"relationaldatabaseprovider-%s-sample", name))
_, _ = utils.Run(cmd)
}
By("removing the DatabaseRequest resource")
for _, name := range []string{"mysql", "mysql-scope", "postgres", "seed"} {
cmd := exec.Command(
"kubectl",
"patch",
"databaserequest",
fmt.Sprintf("databaserequest-%s-sample", name),
"-p",
`{"metadata":{"finalizers":[]}}`,
"--type=merge",
)
_, _ = utils.Run(cmd)
cmd = exec.Command(
"kubectl", "delete", "--force", "databaserequest", fmt.Sprintf(
"databaserequest-%s-sample", name))
_, _ = utils.Run(cmd)
}
By("removing manager namespace")
cmd := exec.Command("kubectl", "delete", "ns", namespace)
_, _ = utils.Run(cmd)
By("uninstalling relational databases pods")
utils.UninstallRelationalDatabases()
By("uninstalling mongodb pods")
utils.UninstallMongoDB()
By("removing service and secret")
for _, name := range []string{"mysql", "mysql-scope", "postgres", "mongodb"} {
cmd = exec.Command(
"kubectl", "delete", "service", "-n", "default", "-l", "app.kubernetes.io/instance=databaserequest-"+name+"-sample")
_, _ = utils.Run(cmd)
cmd = exec.Command(
"kubectl", "delete", "secret", "-n", "default", "-l", "app.kubernetes.io/instance=databaserequest-"+name+"-sample")
_, _ = utils.Run(cmd)
}
})
Context("Operator", func() {
It("should run successfully", func() {
var controllerPodName string
var err error
// projectimage stores the name of the image used in the example
var projectimage = "example.com/dbaas-controller:v0.0.1"
By("building the manager(Operator) image")
cmd := exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectimage))
_, err = utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
By("loading the the manager(Operator) image on Kind")
err = utils.LoadImageToKindClusterWithName(projectimage)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
By("installing CRDs")
cmd = exec.Command("make", "install")
_, err = utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
By("deploying the controller-manager")
cmd = exec.Command("make", "deploy", fmt.Sprintf("IMG=%s", projectimage))
_, err = utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
By("validating that the controller-manager pod is running as expected")
verifyControllerUp := func() error {
// Get pod name
cmd = exec.Command("kubectl", "get",
"pods", "-l", "control-plane=controller-manager",
"-o", "go-template={{ range .items }}"+
"{{ if not .metadata.deletionTimestamp }}"+
"{{ .metadata.name }}"+
"{{ \"\\n\" }}{{ end }}{{ end }}",
"-n", namespace,
)
podOutput, err := utils.Run(cmd)
ExpectWithOffset(2, err).NotTo(HaveOccurred())
podNames := utils.GetNonEmptyLines(string(podOutput))
if len(podNames) != 1 {
return fmt.Errorf("expect 1 controller pods running, but got %d", len(podNames))
}
controllerPodName = podNames[0]
ExpectWithOffset(2, controllerPodName).Should(ContainSubstring("controller-manager"))
// Validate pod status
cmd = exec.Command("kubectl", "get",
"pods", controllerPodName, "-o", "jsonpath={.status.phase}",
"-n", namespace,
)
status, err := utils.Run(cmd)
ExpectWithOffset(2, err).NotTo(HaveOccurred())
if string(status) != "Running" {
return fmt.Errorf("controller pod in %s status", status)
}
return nil
}
EventuallyWithOffset(1, verifyControllerUp, time.Minute, time.Second).Should(Succeed())
By("validating that all database providers and database requests are working")
for _, name := range []string{"mysql", "mysql-scope", "postgres", "seed"} {
if name != "seed" {
By("creating a RelationalDatabaseProvider resource")
cmd = exec.Command(
"kubectl",
"apply",
"-f",
fmt.Sprintf("config/samples/crd_v1alpha1_relationaldatabaseprovider_%s.yaml", name),
)
_, err = utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
By("validating that the RelationalDatabaseProvider resource is created")
cmd = exec.Command(
"kubectl",
"wait",
"--for=condition=Ready",
"relationaldatabaseprovider",
fmt.Sprintf("relationaldatabaseprovider-%s-sample", name),
fmt.Sprintf("--timeout=%s", timeout),
)
_, err = utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
} else {
By("creating seed secret for the DatabaseRequest resource")
cmd = exec.Command("kubectl", "apply", "-f", "test/e2e/testdata/seed-secret.yaml")
_, err = utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
By("creating mysql pod to create seed credentials")
cmd = exec.Command("kubectl", "apply", "-f", "test/e2e/testdata/mysql-client-pod.yaml")
_, err = utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
// We need to wait here a bit because we changed the code to not retry on specific failures in the seed.
// As we can see this is problematic... before it would just have automatically retried and worked after the mysql
// pod was fully up. Now we need to sleep some arbitrary time to make sure the seed database is up
time.Sleep(10 * time.Second)
}
By("creating a DatabaseRequest resource")
cmd = exec.Command(
"kubectl",
"-n", "default",
"apply",
"-f",
fmt.Sprintf("config/samples/crd_v1alpha1_databaserequest_%s.yaml", name),
)
_, err = utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
By("validating that the DatabaseRequest resource is created")
cmd = exec.Command(
"kubectl",
"-n", "default",
"wait",
"--for=condition=Ready",
"databaserequest",
fmt.Sprintf("databaserequest-%s-sample", name),
fmt.Sprintf("--timeout=%s", timeout),
)
_, err = utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
// verify that the service and secret got created
By("validating that the service is created")
cmd = exec.Command(
"kubectl",
"get",
"service",
"-n", "default",
"-l", fmt.Sprintf("app.kubernetes.io/instance=databaserequest-%s-sample", name),
)
serviceOutput, err := utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
serviceNames := utils.GetNonEmptyLines(string(serviceOutput))
ExpectWithOffset(1, serviceNames).Should(HaveLen(2))
ExpectWithOffset(1, serviceNames[1]).Should(ContainSubstring(fmt.Sprintf("first-%s-db", name)))
By("validating that the secret is created")
cmd = exec.Command(
"kubectl",
"get",
"secret",
"-n", "default",
"-l", fmt.Sprintf("app.kubernetes.io/instance=databaserequest-%s-sample", name),
)
secretOutput, err := utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
secretNames := utils.GetNonEmptyLines(string(secretOutput))
ExpectWithOffset(1, secretNames).Should(HaveLen(2))
if name == "seed" {
By("checking that the seed secret is deleted")
cmd = exec.Command("kubectl", "get", "secret", "seed-mysql-secret")
_, err := utils.Run(cmd)
// expect error to occurred
ExpectWithOffset(1, err).To(HaveOccurred())
}
By("deleting the DatabaseRequest resource the database is getting deprovisioned")
cmd = exec.Command(
"kubectl",
"-n", "default",
"delete",
"databaserequest",
fmt.Sprintf("databaserequest-%s-sample", name),
)
_, err = utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
By("validating that the service is deleted")
cmd = exec.Command(
"kubectl",
"get",
"service",
"-n", "default",
"-l", fmt.Sprintf("app.kubernetes.io/instance=databaserequest-%s-sample", name),
)
serviceOutput, err = utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
serviceNames = utils.GetNonEmptyLines(string(serviceOutput))
ExpectWithOffset(1, serviceNames).Should(HaveLen(1))
By("validating that the secret is deleted")
cmd = exec.Command(
"kubectl",
"get",
"secret",
"-n", "default",
"-l", fmt.Sprintf("app.kubernetes.io/instance=databaserequest-%s-sample", name),
)
secretOutput, err = utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
secretNames = utils.GetNonEmptyLines(string(secretOutput))
ExpectWithOffset(1, secretNames).Should(HaveLen(1))
}
By("validating that broken seed database request are failing in exptected way")
for _, name := range []string{"credential-broken-seed", "non-existing-database-seed"} {
By("creating seed secret for the DatabaseRequest resource")
cmd = exec.Command("kubectl", "apply", "-f", fmt.Sprintf("test/e2e/testdata/%s-secret.yaml", name))
_, err = utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
By("creating a DatabaseRequest resource")
// replace - with _
dbrName := strings.ReplaceAll(name, "-", "_")
cmd = exec.Command(
"kubectl",
"-n", "default",
"apply",
"-f",
fmt.Sprintf("test/e2e/testdata/crd_v1alpha1_%s_databaserequest.yaml", dbrName),
)
_, err = utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
By("validating that the DatabaseRequest resource is created but fails")
cmd = exec.Command(
"kubectl",
"-n", "default",
"get",
"databaserequest",
fmt.Sprintf("%s-databaserequest-sample", name),
"-o=jsonpath={.status.conditions[?(@.type=='Ready')].status}",
)
for i := 0; i < 3; i++ {
output, err := utils.Run(cmd)
if err != nil {
ExpectWithOffset(1, err).NotTo(HaveOccurred())
}
if strings.TrimSpace(string(output)) == "False" {
break
} else if i == 2 {
Expect(strings.TrimSpace(string(output))).To(Equal("False"))
}
// give it a bit of time to fail
time.Sleep(time.Second)
}
// verify that the service and secret got created
By("validating that the service is not created")
cmd = exec.Command(
"kubectl",
"get",
"service",
"-n", "default",
"-l", fmt.Sprintf("app.kubernetes.io/instance=%s-databaserequest-sample", name),
)
output, err := utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
Expect(strings.TrimSpace(string(output))).To(Equal("No resources found in default namespace."))
By("validating that the secret is not created")
cmd = exec.Command(
"kubectl",
"get",
"secret",
"-n", "default",
"-l", fmt.Sprintf("app.kubernetes.io/instance=%s-databaserequest-sample", name),
)
serviceOutput, err := utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
Expect(strings.TrimSpace(string(serviceOutput))).To(Equal("No resources found in default namespace."))
By("validating that the seed secret is not deleted")
cmd = exec.Command("kubectl", "get", "secret", fmt.Sprintf("%s-secret", name))
_, err = utils.Run(cmd)
// expect no error to have occurred because the secret should not get deleted
// if the database request is not successfully created
ExpectWithOffset(1, err).NotTo(HaveOccurred())
By("deleting the DatabaseRequest resource the database is getting deprovisioned")
cmd = exec.Command(
"kubectl",
"-n", "default",
"delete",
"databaserequest",
fmt.Sprintf("%s-databaserequest-sample", name),
)
_, err = utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
}
})
// uncomment to debug ...
// time.Sleep(15 * time.Minute)
})
})