-
Notifications
You must be signed in to change notification settings - Fork 47
/
cluster.go
856 lines (757 loc) · 33.8 KB
/
cluster.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
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
package integration_tests
import (
"context"
"encoding/base64"
"fmt"
"github.com/hashicorp/go-multierror"
. "github.com/neo4j/helm-charts/internal/helpers"
"github.com/neo4j/helm-charts/internal/integration_tests/gcloud"
"github.com/neo4j/helm-charts/internal/model"
"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"log"
"os"
"os/exec"
"regexp"
"strings"
"testing"
"time"
)
// labelNodes labels all the node with testLabel=namespace-<number>
func labelNodes(t *testing.T, namespace string) error {
var errors *multierror.Error
nodesList, err := getNodesList()
if err != nil {
return err
}
for index, node := range nodesList.Items {
labelName := fmt.Sprintf("testLabel=%s-%d", namespace, index+1)
err = run(t, "kubectl", "label", "nodes", node.ObjectMeta.Name, labelName)
if err != nil {
errors = multierror.Append(errors, err)
t.Logf("Node Label failed for %s: %v", node.ObjectMeta.Name, err)
}
}
return errors.ErrorOrNil()
}
// removeLabelFromNodes removes label testLabel from all the nodes added via labelNodes func
func removeLabelFromNodes(t *testing.T) error {
var errors *multierror.Error
nodesList, err := getNodesList()
if err != nil {
return err
}
for _, node := range nodesList.Items {
err = run(t, "kubectl", "label", "nodes", node.ObjectMeta.Name, "testLabel-")
if err != nil {
errors = multierror.Append(errors, err)
t.Logf("Node Label removal failed for %s: %v", node.ObjectMeta.Name, err)
}
}
return errors.ErrorOrNil()
}
// clusterTests contains all the tests related to cluster
func clusterTests(clusterRelease model.ReleaseName) ([]SubTest, error) {
subTests := []SubTest{
{name: "Install Backup Helm Chart For AWS With NodeSelector", test: func(t *testing.T) {
t.Parallel()
assert.NoError(t, InstallNeo4jBackupAWSHelmChartWithNodeSelector(t, clusterRelease), "Backup to AWS should succeed")
}},
{name: "Install Backup Helm Chart For AWS Using MinIO", test: func(t *testing.T) {
t.Parallel()
assert.NoError(t, InstallNeo4jBackupAWSHelmChartViaMinIO(t, clusterRelease), "Backup to AWS using MinIO should succeed")
}},
{name: "Check Cluster Core Logs Format", test: func(t *testing.T) {
t.Parallel()
assert.NoError(t, CheckLogsFormat(t, clusterRelease), "Cluster core logs format should be in JSON")
}},
{name: "Check Neo4j Operations Pod for enabling server", test: func(t *testing.T) {
t.Parallel()
assert.NoError(t, CheckNeo4jOperationsPod(t, clusterRelease), "Neo4j Operations Pod should get executed")
}},
{name: "ImagePullSecret tests", test: func(t *testing.T) {
t.Parallel()
assert.NoError(t, imagePullSecretTests(t, clusterRelease), "Perform ImagePullSecret Tests")
}},
{name: "Check PriorityClassName", test: func(t *testing.T) {
t.Parallel()
assert.NoError(t, checkPriorityClassName(t, clusterRelease), "priorityClassName should match")
}},
{name: "Check K8s", test: func(t *testing.T) {
assert.NoError(t, checkK8s(t, clusterRelease), "Neo4j Config check should succeed")
}},
{name: "Check Ldap Password", test: func(t *testing.T) {
assert.NoError(t, checkLdapPassword(t, clusterRelease), "LdapPassword should be set")
}},
{name: "Create Node", test: func(t *testing.T) {
assert.NoError(t, createNode(t, clusterRelease), "Create Node should succeed")
}},
{name: "Count Nodes", test: func(t *testing.T) {
assert.NoError(t, checkNodeCount(t, clusterRelease), "Count Nodes should succeed")
}},
{name: "Database Creation Tests", test: func(t *testing.T) {
assert.NoError(t, databaseCreationTests(t, clusterRelease, "customers"), "Creates \"customer\" database and checks for its existence")
}},
{name: "Install Backup Helm Chart For GCP With Workload Identity For Cluster", test: func(t *testing.T) {
assert.NoError(t, InstallNeo4jBackupGCPHelmChartWithWorkloadIdentityForCluster(t, clusterRelease), "Backup to GCP with workload identity should succeed")
}},
}
return subTests, nil
}
func InstallNeo4jBackupGCPHelmChartWithWorkloadIdentityForCluster(t *testing.T, clusterReleaseName model.ReleaseName) error {
if model.Neo4jEdition == "community" {
t.Skip()
return nil
}
shortName := clusterReleaseName.ShortName()
currentUnixTime := time.Now().Unix()
backupReleaseName := model.NewReleaseName(fmt.Sprintf("%s-gcp-workload-%s", shortName, TestRunIdentifier))
gcpServiceAccountName := fmt.Sprintf("%s-%d", gcpServiceAccountNamePrefix, currentUnixTime)
k8sServiceAccountName := fmt.Sprintf("%s-%d", k8sServiceAccountNamePrefix, currentUnixTime)
namespace := string(clusterReleaseName.Namespace())
t.Cleanup(func() {
_ = runAll(t, "helm", [][]string{
{"uninstall", backupReleaseName.String(), "--wait", "--timeout", "3m", "--namespace", namespace},
}, false)
_ = deleteGCPServiceAccount(gcpServiceAccountName)
})
serviceAccount := v1.ServiceAccount{
TypeMeta: metav1.TypeMeta{
Kind: "ServiceAccount",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: k8sServiceAccountName,
Namespace: namespace,
Annotations: map[string]string{
"iam.gke.io/gcp-service-account": fmt.Sprintf("%s@%s.iam.gserviceaccount.com", gcpServiceAccountName, gcloud.CurrentProject()),
},
},
}
_, err := Clientset.CoreV1().ServiceAccounts(namespace).Create(context.Background(), &serviceAccount, metav1.CreateOptions{})
assert.NoError(t, err, fmt.Sprintf("error seen while creating k8s service account for cluster %s. \n Err := %v", k8sServiceAccountName, err))
err = createGCPServiceAccount(k8sServiceAccountName, namespace, gcpServiceAccountName)
assert.NoError(t, err, fmt.Sprintf("error seen while creating GCP service account for cluster %s. \n Err := %v", gcpServiceAccountName, err))
bucketName := model.BucketName
helmClient := model.NewHelmClient(model.DefaultNeo4jBackupChartName)
helmValues := model.DefaultNeo4jBackupValues
helmValues.Backup = model.Backup{
BucketName: bucketName,
DatabaseAdminServiceName: fmt.Sprintf("%s-admin", clusterReleaseName.String()),
DatabaseNamespace: string(clusterReleaseName.Namespace()),
Database: "neo4j,system",
CloudProvider: "gcp",
Verbose: true,
Type: "FULL",
KeepBackupFiles: true,
}
helmValues.ServiceAccountName = k8sServiceAccountName
_, err = helmClient.Install(t, backupReleaseName.String(), namespace, helmValues)
assert.NoError(t, err)
time.Sleep(2 * time.Minute)
cronjob, err := Clientset.BatchV1().CronJobs(namespace).Get(context.Background(), backupReleaseName.String(), metav1.GetOptions{})
assert.NoError(t, err, "cannot retrieve gcp backup cronjob")
assert.Equal(t, cronjob.Spec.Schedule, helmValues.Neo4J.JobSchedule, fmt.Sprintf("gcp cronjob schedule %s not matching with the schedule defined in values.yaml %s", cronjob.Spec.Schedule, helmValues.Neo4J.JobSchedule))
pods, err := Clientset.CoreV1().Pods(namespace).List(context.Background(), metav1.ListOptions{})
assert.NoError(t, err, "error while retrieving pod list during gcp backup operation")
var found bool
for _, pod := range pods.Items {
if strings.Contains(pod.Name, "gcp-workload") {
found = true
out, err := exec.Command("kubectl", "logs", pod.Name, "--namespace", namespace).CombinedOutput()
assert.NoError(t, err, "error while getting gcp workload backup pod logs")
assert.NotNil(t, out, "gcp backup logs cannot be retrieved")
assert.Contains(t, string(out), "Backup Completed for database neo4j system !!")
assert.Regexp(t, regexp.MustCompile("neo4j(.*)backup uploaded to GCS bucket"), string(out))
assert.Regexp(t, regexp.MustCompile("system(.*)backup uploaded to GCS bucket"), string(out))
assert.Regexp(t, regexp.MustCompile("No inconsistencies found"), string(out))
assert.NotContains(t, string(out), "Deleting file")
break
}
}
assert.Equal(t, true, found, "no gcp workload backup pod found")
return nil
}
// InstallNeo4jBackupAWSHelmChartWithNodeSelector installs backup cronjob using the given nodeselector labels
func InstallNeo4jBackupAWSHelmChartWithNodeSelector(t *testing.T, releaseName model.ReleaseName) error {
if model.Neo4jEdition == "community" {
t.Skip()
return nil
}
backupReleaseName := model.NewReleaseName("cluster-backup-aws-" + TestRunIdentifier)
namespace := string(releaseName.Namespace())
t.Cleanup(func() {
_ = runAll(t, "helm", [][]string{
{"uninstall", backupReleaseName.String(), "--wait", "--timeout", "3m", "--namespace", namespace},
}, false)
})
bucketName := model.BucketName
helmClient := model.NewHelmClient(model.DefaultNeo4jBackupChartName)
helmValues := model.DefaultNeo4jBackupValues
helmValues.Backup = model.Backup{
BucketName: bucketName,
DatabaseAdminServiceName: fmt.Sprintf("%s-admin", releaseName.String()),
DatabaseNamespace: namespace,
Database: "neo4j,system",
CloudProvider: "aws",
SecretName: "awscred",
SecretKeyName: "credentials",
Verbose: true,
Type: "FULL",
}
helmValues.NodeSelector = map[string]string{
"testLabel": fmt.Sprintf("%s-5", namespace),
}
helmValues.ConsistencyCheck.Database = "neo4j"
_, err := helmClient.Install(t, backupReleaseName.String(), namespace, helmValues)
assert.NoError(t, err)
time.Sleep(2 * time.Minute)
cronjob, err := Clientset.BatchV1().CronJobs(namespace).Get(context.Background(), backupReleaseName.String(), metav1.GetOptions{})
assert.NoError(t, err, "cannot retrieve aws backup cronjob")
assert.Equal(t, cronjob.Spec.Schedule, helmValues.Neo4J.JobSchedule, fmt.Sprintf("aws cronjob schedule %s not matching with the schedule defined in values.yaml %s", cronjob.Spec.Schedule, helmValues.Neo4J.JobSchedule))
nodeSelectorNode, err := getNodeWithLabel(fmt.Sprintf("testLabel=%s-5", namespace))
assert.NoError(t, err)
pods, err := Clientset.CoreV1().Pods(namespace).List(context.Background(), metav1.ListOptions{})
assert.NoError(t, err, "error while retrieving pod list during aws backup operation")
var found bool
for _, pod := range pods.Items {
if strings.Contains(pod.Name, "cluster-backup-aws") {
found = true
out, err := exec.Command("kubectl", "logs", pod.Name, "--namespace", namespace).CombinedOutput()
assert.NoError(t, err, "error while getting aws backup pod logs")
assert.NotNil(t, out, "aws backup logs cannot be retrieved")
assert.Contains(t, string(out), "Backup Completed for database neo4j system !!")
assert.Regexp(t, regexp.MustCompile("neo4j(.*)backup uploaded to s3 bucket"), string(out))
assert.Regexp(t, regexp.MustCompile("system(.*)backup uploaded to s3 bucket"), string(out))
assert.Regexp(t, regexp.MustCompile("No inconsistencies found"), string(out))
assert.Equal(t, nodeSelectorNode.Name, pod.Spec.NodeName, fmt.Sprintf("backup pod %s is not scheduled on the correct node %s", pod.Spec.NodeName, nodeSelectorNode.Name))
break
}
}
assert.Equal(t, true, found, "no aws backup pod found")
return nil
}
// InstallNeo4jBackupAWSHelmChartViaMinIO installs backup cronjob and performs backup to minio bucket
func InstallNeo4jBackupAWSHelmChartViaMinIO(t *testing.T, releaseName model.ReleaseName) error {
if model.Neo4jEdition == "community" {
t.Skip()
return nil
}
backupReleaseName := model.NewReleaseName("cluster-backup-aws-minio" + TestRunIdentifier)
namespace := string(releaseName.Namespace())
t.Cleanup(func() {
_ = runAll(t, "helm", [][]string{
{"uninstall", backupReleaseName.String(), "--wait", "--timeout", "3m", "--namespace", namespace},
}, false)
deleteMinio(namespace)
})
tenantName := "tenant1"
secretName := "miniocred"
err := installMinio(namespace, tenantName)
assert.NoError(t, err, "error while installing minio")
err = kCreateMinioSecret(namespace, tenantName, secretName)
assert.NoError(t, err, "error while generating minio kubernetes secret")
bucketName := model.BucketName
helmClient := model.NewHelmClient(model.DefaultNeo4jBackupChartName)
helmValues := model.DefaultNeo4jBackupValues
helmValues.Backup = model.Backup{
BucketName: bucketName,
DatabaseAdminServiceName: fmt.Sprintf("%s-admin", releaseName.String()),
DatabaseNamespace: string(releaseName.Namespace()),
Database: "neo4j,system",
CloudProvider: "aws",
SecretName: secretName,
SecretKeyName: "credentials",
MinioEndpoint: fmt.Sprintf("http://%s-hl.%s.svc.cluster.local:9000", tenantName, namespace),
Verbose: true,
Type: "FULL",
}
helmValues.ConsistencyCheck.Database = "neo4j"
_, err = helmClient.Install(t, backupReleaseName.String(), namespace, helmValues)
assert.NoError(t, err)
time.Sleep(2 * time.Minute)
cronjob, err := Clientset.BatchV1().CronJobs(namespace).Get(context.Background(), backupReleaseName.String(), metav1.GetOptions{})
assert.NoError(t, err, "cannot retrieve aws backup cronjob")
assert.Equal(t, cronjob.Spec.Schedule, helmValues.Neo4J.JobSchedule, fmt.Sprintf("aws cronjob schedule %s not matching with the schedule defined in values.yaml %s", cronjob.Spec.Schedule, helmValues.Neo4J.JobSchedule))
pods, err := Clientset.CoreV1().Pods(namespace).List(context.Background(), metav1.ListOptions{})
assert.NoError(t, err, "error while retrieving pod list during aws backup operation")
var found bool
for _, pod := range pods.Items {
if strings.Contains(pod.Name, "cluster-backup-aws-minio") {
found = true
out, err := exec.Command("kubectl", "logs", pod.Name, "--namespace", namespace).CombinedOutput()
assert.NoError(t, err, "error while getting aws backup pod logs")
assert.NotNil(t, out, "aws backup logs cannot be retrieved")
assert.Contains(t, string(out), "Backup Completed for database neo4j system !!")
assert.Regexp(t, regexp.MustCompile("neo4j(.*)backup uploaded to s3 bucket"), string(out))
assert.Regexp(t, regexp.MustCompile("system(.*)backup uploaded to s3 bucket"), string(out))
assert.Regexp(t, regexp.MustCompile("No inconsistencies found"), string(out))
break
}
}
assert.Equal(t, true, found, "no aws minio backup pod found")
return nil
}
// CheckLogsFormat checks whether the neo4j logs are in json format or not
func CheckLogsFormat(t *testing.T, releaseName model.ReleaseName) error {
stdout, stderr, err := ExecInPod(releaseName, []string{"cat", "/logs/neo4j.log"}, "")
if !assert.NoError(t, err) {
return fmt.Errorf("error seen while executing command `cat /logs/neo4j.log' ,\n err :- %v", err)
}
if !assert.Contains(t, stdout, ",\"level\":\"INFO\",\"category\":\"c.n.s.e.EnterpriseBootstrapper\",\"message\":\"Command expansion is explicitly enabled for configuration\"}") {
return fmt.Errorf("foes not contain the required json format\n stdout := %s", stdout)
}
if !assert.Len(t, stderr, 0) {
return fmt.Errorf("stderr found while checking logs \n stderr := %s", stderr)
}
return nil
}
// CheckNeo4jOperationsPod checks whether the neo4j operations pod is executed or not
func CheckNeo4jOperationsPod(t *testing.T, releaseName model.ReleaseName) error {
fetchPods := func() (*v1.PodList, error) {
pods, err := getPodsWithSpecificLabel(releaseName.Namespace(), "app=neo4j-operations")
if err != nil {
return &v1.PodList{}, fmt.Errorf("error seen while fetching list of pods \n %v", err)
}
if len(pods.Items) == 0 {
return &v1.PodList{}, fmt.Errorf("no pods found")
}
if len(pods.Items) > 1 {
return &v1.PodList{}, fmt.Errorf("more than one operations pod found")
}
return pods, nil
}
pods, err := fetchPods()
if err != nil {
return err
}
pod := pods.Items[0]
for pod.Status.Phase == v1.PodRunning {
t.Logf("operations pod in running state..Waiting for it to be completed")
time.Sleep(30 * time.Second)
pods, err = fetchPods()
if err != nil {
return err
}
pod = pods.Items[0]
}
if pod.Status.Phase != v1.PodSucceeded {
return fmt.Errorf("pod phase %v is not succeeded", pod.Status.Phase)
}
out, err := exec.Command("kubectl", "logs", pod.Name, "--namespace", string(releaseName.Namespace())).CombinedOutput()
if err != nil {
t.Logf("error while fetching operations pod logs")
return err
}
stringOutput := strings.ToLower(string(out))
if !strings.Contains(stringOutput, "server is already enabled") {
return fmt.Errorf("operations pod does not contain valid logs \n logs := %s", string(out))
}
return nil
}
// imagePullSecretTests runs tests related to imagePullSecret feature
func imagePullSecretTests(t *testing.T, name model.ReleaseName) error {
t.Run("Check cluster core has imagePullSecret image", func(t *testing.T) {
t.Parallel()
assert.NoError(t, checkCoreImageName(t, name), "Core-1 image name should match with customImage")
})
t.Run("Check imagePullSecret \"demo\" is created", func(t *testing.T) {
t.Parallel()
assert.NoError(t, checkImagePullSecret(t, name), "ImagePullSecret named \"demo\" should be present")
})
return nil
}
// nodeSelectorTests runs tests related to nodeSelector feature
func nodeSelectorTests(name model.ReleaseName) []SubTest {
namespace := string(name.Namespace())
return []SubTest{
{name: fmt.Sprintf("Check cluster core 1 is assigned with label %s", model.NodeSelectorLabel(namespace)), test: func(t *testing.T) {
t.Parallel()
assert.NoError(t, checkNodeSelectorLabel(t, name, model.NodeSelectorLabel(namespace)), fmt.Sprintf("Core-1 Pod should be deployed on node with label %s", model.NodeSelectorLabel(namespace)))
}},
}
}
// databaseCreationTests creates a database against a cluster and checks if its created or not
func databaseCreationTests(t *testing.T, loadBalancerName model.ReleaseName, dataBaseName string) error {
t.Run("Create Database customers", func(t *testing.T) {
assert.NoError(t, createDatabase(t, loadBalancerName, dataBaseName), "Creates database")
})
t.Run("Check Database customers exists", func(t *testing.T) {
assert.NoError(t, checkDataBaseExists(t, loadBalancerName, dataBaseName), "Checks if database exists or not")
})
return nil
}
// checkPriorityClassName checks the priorityClassName is set to the pod or not
func checkPriorityClassName(t *testing.T, releaseName model.ReleaseName) error {
pods, err := getAllPods(releaseName.Namespace())
if !assert.NoError(t, err) {
return err
}
priorityClassName := model.PriorityClassName(string(releaseName.Namespace()))
for _, pod := range pods.Items {
if strings.Contains(pod.Name, "core-2") {
if !assert.Equal(t, priorityClassName, pod.Spec.PriorityClassName) {
return fmt.Errorf("priorityClassName %s not matching with %s", pod.Spec.PriorityClassName, priorityClassName)
}
break
}
}
return nil
}
// checkCoreImageName checks whether core-1 image is matching with imagePullSecret image or not
func checkCoreImageName(t *testing.T, releaseName model.ReleaseName) error {
pods, err := getAllPods(releaseName.Namespace())
if !assert.NoError(t, err) {
return err
}
for _, pod := range pods.Items {
if strings.Contains(pod.Name, "core-1") {
container := pod.Spec.Containers[0]
if !assert.Equal(t, container.Image, model.ImagePullSecretCustomImageName) {
return fmt.Errorf("container image %s not matching with imagePullSecet customImage %s", container.Image, model.ImagePullSecretCustomImageName)
}
break
}
}
return nil
}
// checkNodeSelectorLabel checks whether the given pod is associated with the correct node or not
func checkNodeSelectorLabel(t *testing.T, releaseName model.ReleaseName, labelName string) error {
nodeSelectorNode, err := getNodeWithLabel(labelName)
if !assert.NoError(t, err) {
return err
}
pod, err := getSpecificPod(releaseName.Namespace(), releaseName.PodName())
if !assert.NoError(t, err) {
return fmt.Errorf("error while fetching pod list \n %v", err)
}
if !assert.Equal(t, nodeSelectorNode.Name, pod.Spec.NodeName) {
return fmt.Errorf("pod %s is not scheduled on the correct node %s", pod.Spec.NodeName, nodeSelectorNode.Name)
}
return nil
}
// checkImagePullSecret checks whether a secret of type docker-registry is created or not
func checkImagePullSecret(t *testing.T, releaseName model.ReleaseName) error {
secret, err := getSpecificSecret(releaseName.Namespace(), "demo")
if !assert.NoError(t, err) {
return fmt.Errorf("No secret found for the provided imagePullSecret \n %v", err)
}
if !assert.Equal(t, secret.Name, "demo") {
return fmt.Errorf("imagePullSecret name %s does not match with demo", secret.Name)
}
return nil
}
// headLessServiceTests contains all the tests related to headless service
func headLessServiceTests(headlessService model.ReleaseName) []SubTest {
return []SubTest{
{name: "Check Headless Service Configuration", test: func(t *testing.T) {
t.Parallel()
assert.NoError(t, checkHeadlessServiceConfiguration(t, headlessService), "Checks Headless Service configuration")
}},
{name: "Check Headless Service Endpoints", test: func(t *testing.T) {
t.Parallel()
assert.NoError(t, checkHeadlessServiceEndpoints(t, headlessService), "headless service endpoints should be equal to the cluster core created")
}},
}
}
// apocConfigTests contains all the tests related to apoc configs
func apocConfigTests(releaseName model.ReleaseName) []SubTest {
return []SubTest{
{name: "Execute apoc query", test: func(t *testing.T) {
t.Parallel()
assert.NoError(t, checkApocConfig(t, releaseName), "Apoc Cypher Query failing to execute")
}},
}
}
// checkClusterCorePasswordFailure checks if a cluster core is failing on installation or not with an incorrect password
func checkClusterCorePasswordFailure(t *testing.T) error {
//creating a sample cluster core definition (which is not supposed to get installed)
clusterReleaseName := model.NewReleaseName("cluster-" + TestRunIdentifier)
core := clusterCore{model.NewCoreReleaseName(clusterReleaseName, 4), nil}
releaseName := core.Name()
// we are not using the customized run() func here since we need to assert the error received on stdout
//(present in out variable and not in err)
out, err := exec.Command(
"helm",
model.BaseHelmCommand("install", releaseName, model.HelmChart, model.Neo4jEdition, "--set", "neo4j.password=my-password", "--set", "neo4j.name="+model.DefaultNeo4jName)...).CombinedOutput()
if !assert.Error(t, err) {
return fmt.Errorf("helm install should fail without the default password")
}
if !assert.Contains(t, string(out), "The desired password does not match the password stored in the Kubernetes Secret") {
return fmt.Errorf("error thrown on password failure is different , err := %s", string(out))
}
return nil
}
func checkK8s(t *testing.T, name model.ReleaseName) error {
t.Run("check pods", func(t *testing.T) {
t.Parallel()
assert.NoError(t, checkPods(t, name))
})
t.Run("check lb", func(t *testing.T) {
t.Parallel()
assert.NoError(t, checkLoadBalancerService(t, name, model.DefaultClusterSize))
})
return nil
}
// checkLoadBalancerService checks whether the loadbalancer exists or not
// It also checks that the number of endpoints should match with the given number of expected endpoints
func checkLoadBalancerService(t *testing.T, name model.ReleaseName, expectedEndPoints int) error {
serviceName := fmt.Sprintf("%s-lb-neo4j", model.DefaultNeo4jName)
lbService, err := Clientset.CoreV1().Services(string(name.Namespace())).Get(context.TODO(), serviceName, metav1.GetOptions{})
if !assert.NoError(t, err) {
return fmt.Errorf("loadbalancer service %s not found , Error seen := %v", serviceName, err)
}
lbEndpoints, err := Clientset.CoreV1().Endpoints(string(name.Namespace())).Get(context.TODO(), lbService.Name, metav1.GetOptions{})
if !assert.NoError(t, err) {
return fmt.Errorf("failed to get loadbalancer service endpoints %v", err)
}
if !assert.Len(t, lbEndpoints.Subsets, 1) {
return fmt.Errorf("lbendpoints subsets length should be equal to 1")
}
if !assert.Len(t, lbEndpoints.Subsets[0].Addresses, expectedEndPoints) {
return fmt.Errorf("loadbalancer endpoints count should be %d", expectedEndPoints)
}
return nil
}
// checkPods checks for the number of pods which should be 5 (3 cluster core + 2 read replica)
func checkPods(t *testing.T, name model.ReleaseName) error {
pods, err := getPodsWithSpecificLabel(name.Namespace(), "helm.neo4j.com/clustering=true")
if !assert.NoError(t, err) {
return err
}
if !assert.Len(t, pods.Items, model.DefaultClusterSize) {
return fmt.Errorf("number of pods should be %d", model.DefaultClusterSize)
}
for _, pod := range pods.Items {
if assert.Contains(t, pod.Labels, "app") {
if !assert.Equal(t, model.DefaultNeo4jName, pod.Labels["app"]) {
return fmt.Errorf("pod should have label app=%s , found app=%s", model.DefaultNeo4jName, pod.Labels["app"])
}
}
}
return nil
}
// checkNeo4jLogsForAnyErrors checks whether neo4j.log and debug.log contain any errors or not
func checkNeo4jLogsForAnyErrors(t *testing.T, name model.ReleaseName) error {
cmd := []string{
"bash",
"-c",
"cat /logs/neo4j.log /logs/debug.log",
}
stdout, stderr, err := ExecInPod(name, cmd, "")
if !assert.NoError(t, err) {
return err
}
if !assert.Len(t, stderr, 0) {
return fmt.Errorf("stderr found \n %s", stderr)
}
//commenting this one out, the issue is reported to kernel team (card created)
//https://trello.com/c/z0g4J7om/7548-neo4j-447-startup-error-seen-in-community-edition
// Should be uncommented or removed based on the findings in the above card
if !assert.NotContains(t, stdout, " ERROR [") {
return fmt.Errorf("Contains error logs \n%s", stdout)
}
return nil
}
// checkHeadlessServiceConfiguration checks whether the provided service is headless service or not
func checkHeadlessServiceConfiguration(t *testing.T, service model.ReleaseName) error {
serviceName := fmt.Sprintf("%s-headless", model.DefaultNeo4jName)
headlessService, err := Clientset.CoreV1().Services(string(service.Namespace())).Get(context.TODO(), serviceName, metav1.GetOptions{})
if !assert.NoError(t, err) {
return fmt.Errorf("headless service %s not found , Error seen := %v", service.String(), err)
}
//throw error if headless service is nil which means no headless service object is released
if !assert.NotNil(t, headlessService) {
return fmt.Errorf("headless service is nil")
}
if !assert.Equal(t, headlessService.Spec.ClusterIP, "None") {
return fmt.Errorf("provided clusterIP is not 'None'...it is %s", headlessService.Spec.ClusterIP)
}
return nil
}
// checkHeadlessServiceEndpoints checks whether the headless endpoints have the cluster cores or not
// By default , headless service includes cluster core only and no read replicas
func checkHeadlessServiceEndpoints(t *testing.T, service model.ReleaseName) error {
serviceName := fmt.Sprintf("%s-headless", model.DefaultNeo4jName)
//get the endpoints associated with the headless service
endpoints, err := Clientset.CoreV1().Endpoints(string(service.Namespace())).Get(context.TODO(), serviceName, metav1.GetOptions{})
if !assert.NoError(t, err) {
return fmt.Errorf("failed to get headless service endpoints %v", err)
}
if !assert.NotEmpty(t, endpoints.Subsets) {
return fmt.Errorf("headlessService endpoints subset cannot be empty")
}
if !assert.Len(t, endpoints.Subsets, 1) {
return fmt.Errorf("headlessService endpoints subset length should be 1 whereas it is %d", len(endpoints.Subsets))
}
if !assert.NotEmpty(t, endpoints.Subsets[0].Addresses) {
return fmt.Errorf("headlessService endpoints addresses list cannot be empty")
}
//get the list of endpoint ip's
var endPointIPs []string
for _, endpointAddress := range endpoints.Subsets[0].Addresses {
endPointIPs = append(endPointIPs, endpointAddress.IP)
}
headlessService, err := Clientset.CoreV1().Services(string(service.Namespace())).Get(context.TODO(), serviceName, metav1.GetOptions{})
if !assert.NoError(t, err) {
return fmt.Errorf("headless service %s not found , Error seen := %v", service.String(), err)
}
//get the list of pods which match the headless service selectors
headlessServiceSelectors := labels.Set(headlessService.Spec.Selector)
listOptions := metav1.ListOptions{LabelSelector: headlessServiceSelectors.AsSelector().String()}
pods, err := Clientset.CoreV1().Pods(string(service.Namespace())).List(context.TODO(), listOptions)
if !assert.NoError(t, err) {
return fmt.Errorf("cannot get pods matching with headless service selector")
}
if !assert.NotEmpty(t, pods) {
return fmt.Errorf("pods list matching headless service selector cannot be empty")
}
//get the list of podIPs matching the headless service selector
var podIPs []string
for _, pod := range pods.Items {
podIPs = append(podIPs, pod.Status.PodIP)
}
//compare podIps and headlessService endPoint IPs ...both should match
if !assert.ElementsMatch(t, podIPs, endPointIPs) {
return fmt.Errorf("podIPs %v and endPointIps %v do not match", podIPs, endPointIPs)
}
return nil
}
func performBackgroundInstall(t *testing.T, componentsToParallelInstall []helmComponent, clusterReleaseName model.ReleaseName) ([]Closeable, error) {
results := make(chan parallelResult)
for _, component := range componentsToParallelInstall {
go func(comp helmComponent) {
var parallelResult = parallelResult{
Closeable: nil,
error: fmt.Errorf("illegal state: background install did not take place for %s in %s", comp.Name(), clusterReleaseName),
}
defer func() { results <- parallelResult }()
parallelResult = comp.Install(t)
}(component)
}
var closeables []Closeable
var combinedError error
for i := 0; i < len(componentsToParallelInstall); i++ {
result := <-results
closeables = append(closeables, result.Closeable)
if result.error != nil {
combinedError = CombineErrors(combinedError, result.error)
}
}
if !assert.NoError(t, combinedError) {
return closeables, combinedError
}
return closeables, nil
}
func installMinio(namespace string, tenantName string) error {
stdout, stderr, err := RunCommand(exec.Command("kubectl", "minio", "version"))
printStdOutStdErr(stdout, stderr, []string{"kubectl", "minio", "version"})
if !strings.Contains(strings.ToLower(string(stdout)), "kubectl-plugin version") {
if err != nil {
log.Printf("%v", string(stderr))
return err
}
}
stdout, stderr, err = RunCommand(exec.Command("kubectl", "minio", "init", "-n", namespace))
printStdOutStdErr(stdout, stderr, []string{"kubectl", "minio", "init", "-n", namespace})
if !strings.Contains(strings.ToLower(string(stdout)), "To open Operator UI, start a port forward using this command") {
if err != nil {
log.Printf("%v", string(stderr))
return err
}
}
stdout, stderr, err = RunCommand(exec.Command("kubectl", "minio", "tenant", "create", tenantName, "--servers", "2", "--volumes", "4", "--capacity", "10Gi", "--disable-tls", "-n", namespace))
printStdOutStdErr(stdout, stderr, []string{"kubectl", "minio", "tenant", "create", tenantName, "--servers", "2", "--volumes", "4", "--capacity", "10Gi", "--disable-tls", "-n", namespace})
if err != nil {
log.Printf("%v", string(stderr))
return err
}
return nil
}
func deleteMinio(namespace string) error {
_, stderr, err := RunCommand(exec.Command("kubectl", "minio", "delete", "-f", "-d", "-n", namespace))
if err != nil {
log.Printf("%v", string(stderr))
return err
}
return nil
}
func getMiniIOKeys(namespace string, tenantName string) (string, string, error) {
secretName := fmt.Sprintf("%s-user-1", tenantName)
stdout, stderr, err := RunCommand(exec.Command("kubectl", "get", "secret", secretName, "-n", namespace, "--template={{.data.CONSOLE_ACCESS_KEY}}"))
printStdOutStdErr(stdout, stderr, []string{"kubectl", "get", "secret", secretName, "-n", namespace, "--template={{.data.CONSOLE_ACCESS_KEY}}"})
if err != nil {
log.Printf("%v", string(stderr))
return "", "", err
}
accessKey, err := base64.StdEncoding.DecodeString(string(stdout))
if err != nil {
log.Printf("Unable to decode minio access key")
return "", "", err
}
stdout, stderr, err = RunCommand(exec.Command("kubectl", "get", "secret", secretName, "-n", namespace, "--template={{.data.CONSOLE_SECRET_KEY}}"))
printStdOutStdErr(stdout, stderr, []string{"kubectl", "get", "secret", secretName, "-n", namespace, "--template={{.data.CONSOLE_SECRET_KEY}}"})
if err != nil {
log.Printf("%v", string(stderr))
return "", "", err
}
secretKey, err := base64.StdEncoding.DecodeString(string(stdout))
if err != nil {
log.Printf("Unable to decode minio secret key")
return "", "", err
}
log.Printf("Access Key = %s , Secret Key = %s", string(accessKey), string(secretKey))
return string(accessKey), string(secretKey), nil
}
func kCreateMinioSecret(namespace string, tenantName string, secretName string) error {
accessKey, secretKey, err := getMiniIOKeys(namespace, tenantName)
if err != nil {
return err
}
tempDir, err := os.MkdirTemp("", namespace)
if err != nil {
return err
}
path, err := createAwsCredFile(tempDir, accessKey, secretKey)
if err != nil {
return err
}
_, stderr, err := RunCommand(exec.Command("kubectl", "create", "secret", "-n", namespace, "generic", secretName, fmt.Sprintf("--from-file=credentials=%s", path)))
if err != nil {
log.Printf("%v", string(stderr))
return err
}
port, cleanupProxy, err := proxyMinioTenant(namespace, tenantName)
defer cleanupProxy()
if err != nil {
return err
}
endpoint := fmt.Sprintf("http://127.0.0.1:%d", port)
err = createMinioBucket(accessKey, secretKey, endpoint)
if err != nil {
return err
}
return nil
}
func createMinioBucket(accessKey string, secretKey string, endpoint string) error {
stdout, stderr, err := RunCommand(exec.Command("mc", "alias", "set", "myminio", endpoint, accessKey, secretKey))
printStdOutStdErr(stdout, stderr, []string{"mc", "alias", "set", "myminio", endpoint, accessKey, secretKey})
if err != nil {
log.Printf("%v", string(stderr))
return err
}
stdout, stderr, err = RunCommand(exec.Command("mc", "mb", "myminio/helm-backup-test"))
printStdOutStdErr(stdout, stderr, []string{"mc", "mb", "myminio/helm-backup-test"})
if err != nil {
log.Printf("%v", string(stderr))
return err
}
return nil
}
func printStdOutStdErr(stdOut []byte, stderr []byte, command []string) {
log.Println("Command = ", command)
log.Println("stdout = ", string(stdOut))
log.Println("stderr = ", string(stderr))
}