-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathmust_gather_operator_test.go
More file actions
1980 lines (1721 loc) · 73.4 KB
/
must_gather_operator_test.go
File metadata and controls
1980 lines (1721 loc) · 73.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
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
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// DO NOT REMOVE TAGS BELOW. IF ANY NEW TEST FILES ARE CREATED UNDER /test/e2e, PLEASE ADD THESE TAGS TO THEM IN ORDER TO BE EXCLUDED FROM UNIT TESTS.
//go:build e2e
// +build e2e
package e2e
import (
"bytes"
"context"
"embed"
"fmt"
"io"
"math/rand"
"os"
"path/filepath"
"strings"
"time"
"github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
imagev1 "github.com/openshift/api/image/v1"
mustgatherv1alpha1 "github.com/openshift/must-gather-operator/api/v1alpha1"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sruntime "k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
)
type MustGatherCROptions struct {
UploadTarget *UploadTargetOptions
PersistentVolume *PersistentVolumeOptions
Timeout *time.Duration
ImageStreamRef *mustgatherv1alpha1.ImageStreamTagRef
GatherSpec *mustgatherv1alpha1.GatherSpec
}
// UploadTargetOptions configures SFTP upload target
type UploadTargetOptions struct {
CaseID string
SecretName string
InternalUser bool
Host string
}
// PersistentVolumeOptions configures PV storage
type PersistentVolumeOptions struct {
PVCName string
SubPath string
}
// Test suite constants
const (
nonAdminUser = "must-gather-nonadmin-user"
nonAdminCRRoleName = "must-gather-nonadmin-clusterrole"
serviceAccount = "must-gather-serviceaccount"
nonAdminLabel = "support-log-gather"
// Operator constants
operatorNamespace = "must-gather-operator"
operatorDeployment = "must-gather-operator"
// Job/Pod constants
gatherContainerName = "gather"
uploadContainerName = "upload"
outputVolumeName = "must-gather-output"
jobNameLabelKey = "job-name"
// UploadTarget test constants
caseManagementSecretNameValid = "case-management-creds-valid"
caseManagementSecretNameInvalid = "case-management-creds-invalid"
caseManagementSecretNameEmptyUsername = "case-management-creds-empty-username"
caseManagementSecretNameEmptyPassword = "case-management-creds-empty-password"
stageHostName = "sftp.access.stage.redhat.com"
// PersistentVolume test constants
mustGatherPVCName = "must-gather-pvc"
caseCredsConfigDirEnvVar = "CASE_MANAGEMENT_CREDS_CONFIG_DIR"
vaultUsernameKey = "sftp-username-e2e"
vaultPasswordKey = "sftp-password-e2e"
)
//go:embed testdata/*
var testassets embed.FS
// Test suite variables
var (
testCtx context.Context
testScheme *k8sruntime.Scheme
adminRestConfig *rest.Config
adminClient client.Client
nonAdminClient client.Client
nonAdminClientset *kubernetes.Clientset
operatorImage string
setupComplete bool
)
func init() {
testScheme = k8sruntime.NewScheme()
utilruntime.Must(mustgatherv1alpha1.AddToScheme(testScheme))
utilruntime.Must(appsv1.AddToScheme(testScheme))
utilruntime.Must(corev1.AddToScheme(testScheme))
utilruntime.Must(rbacv1.AddToScheme(testScheme))
utilruntime.Must(batchv1.AddToScheme(testScheme))
utilruntime.Must(imagev1.AddToScheme(testScheme))
}
var _ = ginkgo.Describe("MustGather resource", ginkgo.Ordered, func() {
// BeforeAll - Admin Setup Phase
ginkgo.BeforeAll(func() {
testCtx = context.Background()
ginkgo.By("STEP 1: Admin sets up clients and ensures operator is installed")
var err error
adminRestConfig, err = config.GetConfig()
Expect(err).NotTo(HaveOccurred(), "Failed to get admin kube config")
adminClient, err = client.New(adminRestConfig, client.Options{Scheme: testScheme})
Expect(err).NotTo(HaveOccurred(), "Failed to create admin typed client")
ginkgo.By("Verifying must-gather-operator is deployed and available")
verifyOperatorDeployment()
ginkgo.By("Getting operator image for verification pods")
operatorImage, err = getOperatorImage()
Expect(err).NotTo(HaveOccurred(), "Failed to get operator image")
ginkgo.GinkgoWriter.Printf("Operator image: %s\n", operatorImage)
ginkgo.By("STEP 2: Creates test namespace")
namespace, err := loader.CreateTestNS("must-gather-operator-e2e", false)
Expect(err).NotTo(HaveOccurred())
ns = namespace
ginkgo.By("STEP 3: Creating ClusterRole for MustGather CRs")
loader.CreateFromFile(testassets.ReadFile, filepath.Join("testdata", "nonadmin-clusterrole.yaml"), ns.Name)
ginkgo.By("STEP 4: Creating ClusterRoleBinding for non-admin user")
loader.CreateFromFile(testassets.ReadFile, filepath.Join("testdata", "nonadmin-clusterrole-binding.yaml"), ns.Name)
ginkgo.By("STEP 5: Creating ServiceAccount and associated RBAC")
loader.CreateFromFile(testassets.ReadFile, filepath.Join("testdata", "serviceaccount.yaml"), ns.Name)
loader.CreateFromFile(testassets.ReadFile, filepath.Join("testdata", "serviceaccount-clusterrole.yaml"), ns.Name)
loader.CreateFromFile(testassets.ReadFile, filepath.Join("testdata", "serviceaccount-clusterrole-binding.yaml"), ns.Name)
ginkgo.By("Initializing non-admin client for tests")
nonAdminClient = createNonAdminClient()
setupComplete = true
ginkgo.GinkgoWriter.Println("Admin setup complete - RBAC and ServiceAccount configured")
})
// AfterAll - Cleanup
ginkgo.AfterAll(func() {
if !setupComplete {
ginkgo.GinkgoWriter.Println("Setup was not complete, skipping cleanup")
return
}
ginkgo.By("CLEANUP: Removing all test resources")
// Deleting namespace and all resources in it (including ServiceAccount)
loader.DeleteTestingNS(ns.Name, func() bool { return ginkgo.CurrentSpecReport().Failed() })
// Deleting ClusterRole, ClusterRoleBinding, and associated RBAC
loader.DeleteFromFile(testassets.ReadFile, filepath.Join("testdata", "nonadmin-clusterrole.yaml"), ns.Name)
loader.DeleteFromFile(testassets.ReadFile, filepath.Join("testdata", "nonadmin-clusterrole-binding.yaml"), ns.Name)
loader.DeleteFromFile(testassets.ReadFile, filepath.Join("testdata", "serviceaccount-clusterrole.yaml"), ns.Name)
loader.DeleteFromFile(testassets.ReadFile, filepath.Join("testdata", "serviceaccount-clusterrole-binding.yaml"), ns.Name)
})
// Test Cases
ginkgo.Context("Non-Admin User Operations", func() {
var mustGatherName string
var mustGatherCR *mustgatherv1alpha1.MustGather
ginkgo.BeforeEach(func() {
mustGatherName = fmt.Sprintf("non-admin-must-gather-e2e-%d", time.Now().UnixNano())
})
ginkgo.AfterEach(func() {
if mustGatherCR != nil {
ginkgo.By("Cleaning up MustGather CR")
_ = nonAdminClient.Delete(testCtx, mustGatherCR)
// Wait for cleanup
Eventually(func() bool {
err := nonAdminClient.Get(testCtx, client.ObjectKey{
Name: mustGatherName,
Namespace: ns.Name,
}, &mustgatherv1alpha1.MustGather{})
return apierrors.IsNotFound(err)
}).WithTimeout(2 * time.Minute).WithPolling(5 * time.Second).Should(BeTrue())
mustGatherCR = nil
}
})
ginkgo.It("can create, get, and list MustGather CRs", func() {
ginkgo.By("Creating MustGather CR using impersonation")
mustGatherCR = &mustgatherv1alpha1.MustGather{
ObjectMeta: metav1.ObjectMeta{
Name: mustGatherName,
Namespace: ns.Name,
Annotations: map[string]string{
"test.description": "Non-admin user submitted MustGather",
"test.user": nonAdminUser,
},
},
Spec: mustgatherv1alpha1.MustGatherSpec{
ServiceAccountName: serviceAccount,
},
}
err := nonAdminClient.Create(testCtx, mustGatherCR)
Expect(err).NotTo(HaveOccurred(), "Non-admin user should be able to create MustGather CR")
ginkgo.By("Verifying MustGather CR was created successfully")
fetchedMG := &mustgatherv1alpha1.MustGather{}
err = nonAdminClient.Get(testCtx, client.ObjectKey{
Name: mustGatherName,
Namespace: ns.Name,
}, fetchedMG)
Expect(err).NotTo(HaveOccurred())
Expect(fetchedMG.Spec.ServiceAccountName).To(Equal(serviceAccount))
ginkgo.GinkgoWriter.Printf("Non-admin user '%s' successfully created MustGather CR: %s\n",
nonAdminUser, mustGatherName)
ginkgo.By("Non-admin user listing MustGather CRs in their namespace")
mgList := &mustgatherv1alpha1.MustGatherList{}
err = nonAdminClient.List(testCtx, mgList, client.InNamespace(ns.Name))
Expect(err).NotTo(HaveOccurred(), "Non-admin should be able to list MustGather CRs")
Expect(mgList.Items).NotTo(BeEmpty(),
"List should contain at least the MustGather CR we just created")
ginkgo.By("Verifying the created CR is in the list")
found := false
for _, mg := range mgList.Items {
if mg.Name == mustGatherName {
found = true
break
}
}
Expect(found).To(BeTrue(), "Created MustGather CR should be in the list")
ginkgo.GinkgoWriter.Printf("Non-admin user can see %d MustGather CRs in namespace %s\n",
len(mgList.Items), ns.Name)
})
ginkgo.It("CANNOT perform admin operations", func() {
ginkgo.By("Attempting to delete a namespace (should fail)")
testNS := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "test-delete-namespace",
},
}
err := nonAdminClient.Delete(testCtx, testNS)
Expect(err).To(HaveOccurred(), "Non-admin should NOT be able to delete namespaces")
Expect(apierrors.IsForbidden(err)).To(BeTrue(), "Should get Forbidden error")
ginkgo.By("Attempting to create a ClusterRole (should fail)")
testCR := &rbacv1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Name: "test-unauthorized-clusterrole",
},
}
err = nonAdminClient.Create(testCtx, testCR)
Expect(err).To(HaveOccurred(), "Non-admin should NOT be able to create ClusterRoles")
Expect(apierrors.IsForbidden(err)).To(BeTrue(), "Should get Forbidden error")
ginkgo.GinkgoWriter.Println("Non-admin user correctly blocked from admin operations")
})
ginkgo.It("should create Job using ServiceAccount", func() {
ginkgo.By("creating MustGather CR")
mustGatherCR = createMustGatherCR(mustGatherName, ns.Name, serviceAccount, false, nil)
ginkgo.By("Waiting for operator to create Job")
job := &batchv1.Job{}
Eventually(func() error {
err := nonAdminClient.Get(testCtx, client.ObjectKey{
Name: mustGatherName,
Namespace: ns.Name,
}, job)
if err != nil {
// Debug: Check if Job exists with admin client
jobDebug := &batchv1.Job{}
errAdmin := adminClient.Get(testCtx, client.ObjectKey{
Name: mustGatherName,
Namespace: ns.Name,
}, jobDebug)
if errAdmin == nil {
ginkgo.GinkgoWriter.Printf("Job exists but non-admin can't see it (RBAC issue)\n")
} else {
ginkgo.GinkgoWriter.Printf("Job not found by operator yet: %v\n", errAdmin)
}
}
return err
}).WithTimeout(2*time.Minute).WithPolling(5*time.Second).Should(Succeed(),
"Non-admin user should be able to see Job created by operator")
ginkgo.By("Verifying Job uses ServiceAccount")
Expect(job.Spec.Template.Spec.ServiceAccountName).To(Equal(serviceAccount),
"Job must use the ServiceAccount specified in MustGather CR")
ginkgo.By("Verifying Job has required specifications")
Expect(len(job.Spec.Template.Spec.Containers)).To(BeNumerically(">=", 1),
"Job should have at least one container")
hasGatherContainer := false
for _, container := range job.Spec.Template.Spec.Containers {
if container.Name == gatherContainerName {
hasGatherContainer = true
break
}
}
Expect(hasGatherContainer).To(BeTrue(), "Job should have gather container")
ginkgo.By("Verifying Job has output volume for artifacts")
hasOutputVolume := false
for _, volume := range job.Spec.Template.Spec.Volumes {
if volume.Name == outputVolumeName {
hasOutputVolume = true
break
}
}
Expect(hasOutputVolume).To(BeTrue(), "Job should have must-gather-output volume")
})
ginkgo.It("should be able to monitor MustGather progress", func() {
ginkgo.By("creating MustGather CR")
mustGatherCR = createMustGatherCR(mustGatherName, ns.Name, serviceAccount, false, nil)
ginkgo.By("checking MustGather CR status")
fetchedMG := &mustgatherv1alpha1.MustGather{}
err := nonAdminClient.Get(testCtx, client.ObjectKey{
Name: mustGatherName,
Namespace: ns.Name,
}, fetchedMG)
Expect(err).NotTo(HaveOccurred(), "Non-admin user should be able to get MustGather CR")
ginkgo.GinkgoWriter.Printf("Non-admin can read MustGather status: %s\n", fetchedMG.Status.Status)
ginkgo.By("listing Jobs in namespace")
jobList := &batchv1.JobList{}
err = nonAdminClient.List(testCtx, jobList, client.InNamespace(ns.Name))
Expect(err).NotTo(HaveOccurred(), "Non-admin user should be able to list Jobs")
ginkgo.GinkgoWriter.Printf("Non-admin can see %d Jobs\n", len(jobList.Items))
ginkgo.By("listing Pods in namespace")
podList := &corev1.PodList{}
err = nonAdminClient.List(testCtx, podList, client.InNamespace(ns.Name))
Expect(err).NotTo(HaveOccurred(), "Non-admin user should be able to list Pods")
ginkgo.GinkgoWriter.Printf("Non-admin can see %d Pods\n", len(podList.Items))
ginkgo.By("checking for gather Pods")
Eventually(func() bool {
pods := &corev1.PodList{}
if err := nonAdminClient.List(testCtx, pods,
client.InNamespace(ns.Name),
client.MatchingLabels{jobNameLabelKey: mustGatherName}); err != nil {
return false
}
if len(pods.Items) > 0 {
ginkgo.GinkgoWriter.Printf("Non-admin can see gather pod: %s\n", pods.Items[0].Name)
}
return len(pods.Items) > 0
}).WithTimeout(2 * time.Minute).WithPolling(5 * time.Second).Should(BeTrue())
ginkgo.GinkgoWriter.Println("Non-admin user successfully monitored MustGather progress")
})
ginkgo.It("can delete their MustGather CR", func() {
mustGatherName := fmt.Sprintf("test-cleanup-%d", time.Now().UnixNano())
ginkgo.By("creating MustGather CR")
mg := createMustGatherCR(mustGatherName, ns.Name, serviceAccount, false, nil)
ginkgo.By("deleting their MustGather CR")
err := nonAdminClient.Delete(testCtx, mg)
Expect(err).NotTo(HaveOccurred(), "Non-admin should be able to delete their own CR")
ginkgo.By("Verifying CR is deleted")
Eventually(func() bool {
err := nonAdminClient.Get(testCtx, client.ObjectKey{
Name: mustGatherName,
Namespace: ns.Name,
}, &mustgatherv1alpha1.MustGather{})
return apierrors.IsNotFound(err)
}).WithTimeout(2 * time.Minute).WithPolling(5 * time.Second).Should(BeTrue())
ginkgo.GinkgoWriter.Printf("Non-admin user successfully deleted MustGather CR: %s\n", mustGatherName)
})
ginkgo.It("should clean up Job and Pod when MustGather CR is deleted", func() {
mustGatherName := fmt.Sprintf("test-cascading-delete-%d", time.Now().UnixNano())
ginkgo.By("Creating MustGather CR")
mg := createMustGatherCR(mustGatherName, ns.Name, serviceAccount, false, nil)
ginkgo.By("Waiting for Job to be created")
Eventually(func() error {
return nonAdminClient.Get(testCtx, client.ObjectKey{
Name: mustGatherName,
Namespace: ns.Name,
}, &batchv1.Job{})
}).WithTimeout(2 * time.Minute).WithPolling(5 * time.Second).Should(Succeed())
ginkgo.By("Waiting for Pod to be created")
Eventually(func() int {
podList := &corev1.PodList{}
if err := nonAdminClient.List(testCtx, podList,
client.InNamespace(ns.Name),
client.MatchingLabels{jobNameLabelKey: mustGatherName}); err != nil {
return 0
}
return len(podList.Items)
}).WithTimeout(2*time.Minute).WithPolling(5*time.Second).Should(BeNumerically(">=", 1),
"Pod should be created by Job")
ginkgo.By("Deleting MustGather CR")
err := nonAdminClient.Delete(testCtx, mg)
Expect(err).NotTo(HaveOccurred())
ginkgo.By("Verifying Job is eventually cleaned up")
Eventually(func() bool {
err := nonAdminClient.Get(testCtx, client.ObjectKey{
Name: mustGatherName,
Namespace: ns.Name,
}, &batchv1.Job{})
return apierrors.IsNotFound(err)
}).WithTimeout(2*time.Minute).WithPolling(5*time.Second).Should(BeTrue(),
"Job should be cleaned up when MustGather CR is deleted")
ginkgo.By("Verifying Pods are eventually cleaned up")
Eventually(func() int {
podList := &corev1.PodList{}
if err := nonAdminClient.List(testCtx, podList,
client.InNamespace(ns.Name),
client.MatchingLabels{jobNameLabelKey: mustGatherName}); err != nil {
return 0
}
return len(podList.Items)
}).WithTimeout(2*time.Minute).WithPolling(5*time.Second).Should(Equal(0),
"Pods should be cleaned up when MustGather CR is deleted")
})
ginkgo.It("should configure timeout correctly and complete successfully", func() {
ginkgo.By("Creating MustGather CR with 1 minute timeout")
timeout := 1 * time.Minute
mustGatherCR = createMustGatherCR(mustGatherName, ns.Name, serviceAccount, true, &MustGatherCROptions{
Timeout: &timeout,
})
ginkgo.By("Verifying MustGather CR has timeout set")
fetchedMG := &mustgatherv1alpha1.MustGather{}
err := nonAdminClient.Get(testCtx, client.ObjectKey{
Name: mustGatherName,
Namespace: ns.Name,
}, fetchedMG)
Expect(err).NotTo(HaveOccurred())
Expect(fetchedMG.Spec.MustGatherTimeout).NotTo(BeNil(), "MustGatherTimeout should be set")
Expect(fetchedMG.Spec.MustGatherTimeout.Duration).To(Equal(timeout),
"MustGatherTimeout should be 1 minute")
ginkgo.By("Waiting for Job to be created")
job := &batchv1.Job{}
Eventually(func() error {
return nonAdminClient.Get(testCtx, client.ObjectKey{
Name: mustGatherName,
Namespace: ns.Name,
}, job)
}).WithTimeout(2*time.Minute).WithPolling(5*time.Second).Should(Succeed(),
"Job should be created for MustGather with timeout")
ginkgo.By("Verifying Job's gather container has timeout in command")
var gatherContainer *corev1.Container
for i := range job.Spec.Template.Spec.Containers {
if job.Spec.Template.Spec.Containers[i].Name == gatherContainerName {
gatherContainer = &job.Spec.Template.Spec.Containers[i]
break
}
}
Expect(gatherContainer).NotTo(BeNil(), "Job should have gather container")
// The gather container command should include the configured timeout value (60 seconds)
commandStr := strings.Join(gatherContainer.Command, " ")
ginkgo.GinkgoWriter.Printf("Gather container command: %s\n", commandStr)
Expect(commandStr).To(ContainSubstring("timeout 60"),
"Gather container command should include timeout value of 60 seconds")
ginkgo.By("Waiting for Job to complete")
Eventually(func() bool {
if err := nonAdminClient.Get(testCtx, client.ObjectKey{
Name: mustGatherName,
Namespace: ns.Name,
}, job); err != nil {
return false
}
return job.Status.Succeeded > 0 || job.Status.Failed > 0
}).WithTimeout(2*time.Minute).WithPolling(10*time.Second).Should(BeTrue(),
"Job should complete within the timeout period")
ginkgo.By("Verifying MustGather CR status is updated")
err = nonAdminClient.Get(testCtx, client.ObjectKey{
Name: mustGatherName,
Namespace: ns.Name,
}, fetchedMG)
Expect(err).NotTo(HaveOccurred())
Expect(fetchedMG.Status.Completed).To(BeTrue(), "MustGather should be marked as completed")
ginkgo.GinkgoWriter.Printf("MustGather with timeout completed - Status: %s, Reason: %s\n",
fetchedMG.Status.Status, fetchedMG.Status.Reason)
})
})
ginkgo.Context("Resource Retention Tests", func() {
var mustGatherName string
var mustGatherCR *mustgatherv1alpha1.MustGather
ginkgo.BeforeEach(func() {
mustGatherName = fmt.Sprintf("mg-retain-resources-%d", time.Now().UnixNano())
})
ginkgo.AfterEach(func() {
if mustGatherCR != nil {
ginkgo.By("Cleaning up MustGather CR")
_ = nonAdminClient.Delete(testCtx, mustGatherCR)
// Wait for cleanup
Eventually(func() bool {
err := nonAdminClient.Get(testCtx, client.ObjectKey{
Name: mustGatherName,
Namespace: ns.Name,
}, &mustgatherv1alpha1.MustGather{})
return apierrors.IsNotFound(err)
}).WithTimeout(2 * time.Minute).WithPolling(5 * time.Second).Should(BeTrue())
mustGatherCR = nil
}
})
ginkgo.It("Pod should gather data and complete successfully", func() {
ginkgo.By("creating MustGather CR with RetainResourcesOnCompletion")
mustGatherCR = createMustGatherCR(mustGatherName, ns.Name, serviceAccount, true, nil)
ginkgo.By("Waiting for Job to be created")
job := &batchv1.Job{}
Eventually(func() error {
return nonAdminClient.Get(testCtx, client.ObjectKey{
Name: mustGatherName,
Namespace: ns.Name,
}, job)
}).WithTimeout(2 * time.Minute).WithPolling(5 * time.Second).Should(Succeed())
ginkgo.By("Waiting for Pod to be created")
var gatherPod *corev1.Pod
Eventually(func() bool {
podList := &corev1.PodList{}
if err := nonAdminClient.List(testCtx, podList,
client.InNamespace(ns.Name),
client.MatchingLabels{jobNameLabelKey: mustGatherName}); err != nil {
return false
}
if len(podList.Items) > 0 {
gatherPod = &podList.Items[0]
return true
}
return false
}).WithTimeout(2*time.Minute).WithPolling(5*time.Second).Should(BeTrue(),
"Pod should be created by Job")
ginkgo.By("Verifying Pod is scheduled")
Eventually(func() string {
pod := &corev1.Pod{}
_ = nonAdminClient.Get(testCtx, client.ObjectKey{
Name: gatherPod.Name,
Namespace: ns.Name,
}, pod)
return pod.Spec.NodeName
}).WithTimeout(2*time.Minute).WithPolling(5*time.Second).ShouldNot(BeEmpty(),
"Pod should be scheduled to a node")
ginkgo.By("Monitoring Pod execution phase")
Eventually(func() corev1.PodPhase {
pod := &corev1.Pod{}
err := nonAdminClient.Get(testCtx, client.ObjectKey{
Name: gatherPod.Name,
Namespace: ns.Name,
}, pod)
if err != nil {
return corev1.PodUnknown
}
return pod.Status.Phase
}).WithTimeout(2*time.Minute).WithPolling(5*time.Second).Should(
Or(Equal(corev1.PodRunning), Equal(corev1.PodSucceeded)),
"Pod should reach Running or Succeeded state")
ginkgo.By("Verifying gather container is collecting data")
Eventually(func() bool {
pod := &corev1.Pod{}
err := nonAdminClient.Get(testCtx, client.ObjectKey{
Name: gatherPod.Name,
Namespace: ns.Name,
}, pod)
if err != nil {
return false
}
for _, cs := range pod.Status.ContainerStatuses {
if cs.Name == gatherContainerName {
ginkgo.GinkgoWriter.Printf("Gather container - Ready: %v, RestartCount: %d\n",
cs.Ready, cs.RestartCount)
// Container should be running and not crash-looping
if cs.RestartCount > 2 {
return false
}
// Return true if container has started (even if not ready yet, data collection may be in progress)
if cs.State.Running != nil || cs.State.Terminated != nil {
return true
}
}
}
return false
}).WithTimeout(2*time.Minute).WithPolling(5*time.Second).Should(BeTrue(),
"Gather container should be running and collecting data without excessive restarts")
ginkgo.By("Waiting for Job to complete successfully")
Eventually(func() int32 {
_ = nonAdminClient.Get(testCtx, client.ObjectKey{
Name: mustGatherName,
Namespace: ns.Name,
}, job)
return job.Status.Succeeded
}).WithTimeout(5*time.Minute).WithPolling(5*time.Second).Should(
BeNumerically(">=", 1), "Job should complete successfully")
Expect(job.Status.Failed).To(Equal(int32(0)), "Job should not have any failed pods")
ginkgo.By("Verifying MustGather CR status is updated")
fetchedMG := &mustgatherv1alpha1.MustGather{}
Eventually(func() string {
_ = nonAdminClient.Get(testCtx, client.ObjectKey{
Name: mustGatherName,
Namespace: ns.Name,
}, fetchedMG)
return fetchedMG.Status.Status
}).WithTimeout(2*time.Minute).WithPolling(5*time.Second).ShouldNot(BeEmpty(),
"MustGather status should be updated by operator")
ginkgo.GinkgoWriter.Printf("MustGather Status: %s - Completed: %v - Reason: %s\n",
fetchedMG.Status.Status, fetchedMG.Status.Completed, fetchedMG.Status.Reason)
ginkgo.By("Verifying resources are retained after completion (RetainResourcesOnCompletion=true)")
// Wait a bit to ensure the operator has had time to process the completion
time.Sleep(10 * time.Second)
ginkgo.By("Verifying Job still exists after completion")
retainedJob := &batchv1.Job{}
err := nonAdminClient.Get(testCtx, client.ObjectKey{
Name: mustGatherName,
Namespace: ns.Name,
}, retainedJob)
Expect(err).NotTo(HaveOccurred(), "Job should still exist when RetainResourcesOnCompletion is true")
ginkgo.GinkgoWriter.Printf("Job %s is retained after completion\n", retainedJob.Name)
ginkgo.By("Verifying Pod still exists after completion")
retainedPodList := &corev1.PodList{}
err = nonAdminClient.List(testCtx, retainedPodList,
client.InNamespace(ns.Name),
client.MatchingLabels{jobNameLabelKey: mustGatherName})
Expect(err).NotTo(HaveOccurred())
Expect(retainedPodList.Items).NotTo(BeEmpty(),
"Pod should still exist when RetainResourcesOnCompletion is true")
ginkgo.GinkgoWriter.Printf("Pod %s is retained after completion\n", retainedPodList.Items[0].Name)
ginkgo.GinkgoWriter.Println("RetainResourcesOnCompletion=true verified: Job and Pod are retained after completion")
})
})
ginkgo.Context("Security and Isolation Tests", func() {
var mg *mustgatherv1alpha1.MustGather
ginkgo.AfterEach(func() {
if mg != nil {
ginkgo.By("Cleaning up MustGather CR")
_ = nonAdminClient.Delete(testCtx, mg)
mg = nil
}
})
ginkgo.It("should report error when ServiceAccount does not exist", func() {
mustGatherName := fmt.Sprintf("test-missing-sa-%d", time.Now().UnixNano())
ginkgo.By("Creating MustGather with non-existent ServiceAccount")
mg = createMustGatherCR(mustGatherName, ns.Name, "non-existent-sa-e2e", false, nil)
ginkgo.By("Verifying MustGather status has error condition")
Eventually(func() bool {
fetchedMG := &mustgatherv1alpha1.MustGather{}
err := adminClient.Get(testCtx, client.ObjectKey{
Name: mustGatherName,
Namespace: ns.Name,
}, fetchedMG)
if err != nil {
return false
}
// Check for error condition with service account message
for _, cond := range fetchedMG.Status.Conditions {
if cond.Type == "ReconcileError" && cond.Status == metav1.ConditionTrue {
if strings.Contains(strings.ToLower(cond.Message), "service account") &&
strings.Contains(strings.ToLower(cond.Message), "not found") {
ginkgo.GinkgoWriter.Printf("Found expected error condition: %s\n", cond.Message)
return true
}
}
}
return false
}).WithTimeout(1*time.Minute).WithPolling(5*time.Second).Should(BeTrue(),
"MustGather status should contain error condition about missing ServiceAccount")
ginkgo.By("Verifying Job was not created")
job := &batchv1.Job{}
err := adminClient.Get(testCtx, client.ObjectKey{
Name: mustGatherName,
Namespace: ns.Name,
}, job)
Expect(apierrors.IsNotFound(err)).To(BeTrue(),
"Job should not be created when ServiceAccount is missing")
ginkgo.By("Verifying warning event was generated")
events := &corev1.EventList{}
err = adminClient.List(testCtx, events, client.InNamespace(ns.Name))
Expect(err).NotTo(HaveOccurred())
foundWarningEvent := false
for _, event := range events.Items {
if event.InvolvedObject.Name == mustGatherName &&
event.Type == corev1.EventTypeWarning &&
strings.Contains(strings.ToLower(event.Message), "service account") {
foundWarningEvent = true
ginkgo.GinkgoWriter.Printf("Found warning event: %s\n", event.Message)
break
}
}
Expect(foundWarningEvent).To(BeTrue(), "Warning event should be generated for missing ServiceAccount")
ginkgo.GinkgoWriter.Println("ServiceAccount validation correctly prevented Job creation and reported error")
})
ginkgo.It("should enforce ServiceAccount permissions during data collection", func() {
mustGatherName := fmt.Sprintf("test-sa-permissions-%d", time.Now().UnixNano())
ginkgo.By("Creating MustGather with SA")
mg = createMustGatherCR(mustGatherName, ns.Name, serviceAccount, false, nil)
ginkgo.By("Waiting for Pod to start")
var gatherPod *corev1.Pod
Eventually(func() bool {
podList := &corev1.PodList{}
if err := nonAdminClient.List(testCtx, podList,
client.InNamespace(ns.Name),
client.MatchingLabels{jobNameLabelKey: mustGatherName}); err != nil {
return false
}
if len(podList.Items) > 0 {
gatherPod = &podList.Items[0]
return true
}
return false
}).WithTimeout(2 * time.Minute).WithPolling(5 * time.Second).Should(BeTrue())
ginkgo.By("Verifying Pod has no privilege escalation")
Expect(gatherPod.Spec.ServiceAccountName).To(Equal(serviceAccount))
// Check that pod doesn't request privileged mode
for _, container := range gatherPod.Spec.Containers {
if container.SecurityContext != nil && container.SecurityContext.Privileged != nil {
Expect(*container.SecurityContext.Privileged).To(BeFalse(),
"Container should not run in privileged mode")
}
}
ginkgo.By("Waiting for Pod to start running")
Eventually(func() corev1.PodPhase {
if err := nonAdminClient.Get(testCtx, client.ObjectKey{
Name: gatherPod.Name,
Namespace: ns.Name,
}, gatherPod); err != nil {
return corev1.PodUnknown
}
return gatherPod.Status.Phase
}).WithTimeout(2*time.Minute).WithPolling(5*time.Second).Should(
Or(Equal(corev1.PodRunning), Equal(corev1.PodSucceeded)),
"Pod should be running or succeeded before checking events")
ginkgo.By("Verifying Pod runs with restricted permissions (no privileged escalation events)")
events := &corev1.EventList{}
err := nonAdminClient.List(testCtx, events, client.InNamespace(ns.Name))
Expect(err).NotTo(HaveOccurred(), "Should be able to list events")
// Check that there are no privilege escalation or security context violation events
for _, event := range events.Items {
if event.InvolvedObject.Name == mustGatherName || event.InvolvedObject.Name == gatherPod.Name {
// Only check Warning events for security issues
if event.Type == corev1.EventTypeWarning {
lowerMsg := strings.ToLower(event.Message)
lowerReason := strings.ToLower(event.Reason)
// Check for specific security violation patterns
Expect(lowerReason).NotTo(Equal("failedcreate"),
"Pod creation should not fail: %s", event.Message)
Expect(lowerMsg).NotTo(ContainSubstring("forbidden"),
"Pod should not have forbidden errors: %s", event.Message)
Expect(lowerMsg).NotTo(ContainSubstring("denied"),
"Pod should not have permission denied errors: %s", event.Message)
}
}
}
ginkgo.GinkgoWriter.Println("Pod is running with properly restricted permissions")
})
ginkgo.It("should prevent non-admin user from modifying RBAC", func() {
ginkgo.By("Attempting to update ClusterRole (should fail)")
cr := &rbacv1.ClusterRole{}
err := adminClient.Get(testCtx, client.ObjectKey{Name: nonAdminCRRoleName}, cr)
Expect(err).NotTo(HaveOccurred(), "ClusterRole should exist")
err = nonAdminClient.Update(testCtx, cr)
Expect(err).To(HaveOccurred(), "Non-admin should NOT be able to modify ClusterRoles")
Expect(apierrors.IsForbidden(err)).To(BeTrue(), "Should get Forbidden error")
ginkgo.By("Attempting to update ServiceAccount (should fail)")
sa := &corev1.ServiceAccount{}
err = adminClient.Get(testCtx, client.ObjectKey{Name: serviceAccount, Namespace: ns.Name}, sa)
Expect(err).NotTo(HaveOccurred(), "ServiceAccount should exist")
err = nonAdminClient.Update(testCtx, sa)
Expect(err).To(HaveOccurred(), "Non-admin should NOT be able to modify ServiceAccounts")
Expect(apierrors.IsForbidden(err)).To(BeTrue(), "Should get Forbidden error")
})
})
ginkgo.Context("UploadTarget SFTP Configuration Tests", func() {
var mustGatherName string
var mustGatherCR *mustgatherv1alpha1.MustGather
ginkgo.BeforeEach(func() {
mustGatherName = fmt.Sprintf("mg-upload-target-e2e-test-%d", time.Now().UnixNano())
})
ginkgo.AfterEach(func() {
if mustGatherCR != nil {
ginkgo.By("Cleaning up MustGather CR")
_ = nonAdminClient.Delete(testCtx, mustGatherCR)
Eventually(func() bool {
err := nonAdminClient.Get(testCtx, client.ObjectKey{
Name: mustGatherName,
Namespace: ns.Name,
}, &mustgatherv1alpha1.MustGather{})
return apierrors.IsNotFound(err)
}).WithTimeout(2 * time.Minute).WithPolling(5 * time.Second).Should(BeTrue())
mustGatherCR = nil
}
})
ginkgo.It("should successfully upload must-gather data to SFTP server for external user", func() {
ginkgo.By("Getting SFTP credentials from Vault")
sftpUsername, sftpPassword, err := getCaseCredsFromVault()
Expect(err).NotTo(HaveOccurred(), "Failed to get SFTP credentials from Vault")
ginkgo.By("Creating case-management-creds-valid secret")
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: caseManagementSecretNameValid,
Namespace: ns.Name,
Labels: map[string]string{
"test": nonAdminLabel,
},
},
Type: corev1.SecretTypeOpaque,
StringData: map[string]string{
"username": sftpUsername,
"password": sftpPassword,
},
}
err = nonAdminClient.Create(testCtx, secret)
if err != nil && !apierrors.IsAlreadyExists(err) {
Expect(err).NotTo(HaveOccurred(), "Failed to create case management secret")
}
ginkgo.By("Creating MustGather CR with UploadTarget and internalUser=false")
// Generate unique caseID to avoid false positives from previous test runs
caseID := generateTestCaseID()
ginkgo.GinkgoWriter.Printf("Using unique caseID: %s\n", caseID)
mustGatherCR = createMustGatherCR(mustGatherName, ns.Name, serviceAccount, true, &MustGatherCROptions{
UploadTarget: &UploadTargetOptions{CaseID: caseID, SecretName: caseManagementSecretNameValid, InternalUser: false, Host: stageHostName},
})
ginkgo.By("Verifying MustGather CR has internalUser set to false")
fetchedMG := &mustgatherv1alpha1.MustGather{}
err = nonAdminClient.Get(testCtx, client.ObjectKey{
Name: mustGatherName,
Namespace: ns.Name,
}, fetchedMG)
Expect(err).NotTo(HaveOccurred())
Expect(fetchedMG.Spec.UploadTarget.SFTP.InternalUser).To(BeFalse(),
"InternalUser flag should be false for external user")
ginkgo.By("Waiting for Job to be created")
job := &batchv1.Job{}
Eventually(func(g Gomega) {
// First check if MustGather has failed validation
mg := &mustgatherv1alpha1.MustGather{}
g.Expect(nonAdminClient.Get(testCtx, client.ObjectKey{
Name: mustGatherName,
Namespace: ns.Name,
}, mg)).To(Succeed())
// If status is Failed, fail immediately with the reason
if mg.Status.Status == "Failed" {
ginkgo.Fail(fmt.Sprintf("MustGather validation failed before Job creation: %s", mg.Status.Reason))
}
// Otherwise, check if Job was created
g.Expect(nonAdminClient.Get(testCtx, client.ObjectKey{
Name: mustGatherName,
Namespace: ns.Name,
}, job)).To(Succeed(), "Job should be created for MustGather with UploadTarget")
}).WithTimeout(2 * time.Minute).WithPolling(5 * time.Second).Should(Succeed())
ginkgo.By("Verifying Job has upload container with correct environment variables")
var uploadContainer *corev1.Container
for i := range job.Spec.Template.Spec.Containers {
if job.Spec.Template.Spec.Containers[i].Name == uploadContainerName {
uploadContainer = &job.Spec.Template.Spec.Containers[i]
break
}
}
Expect(uploadContainer).NotTo(BeNil(), "Job should have upload container")
envVars := make(map[string]string)
for _, env := range uploadContainer.Env {
envVars[env.Name] = env.Value
}
Expect(envVars).To(HaveKey("caseid"), "Upload container should have caseid env var")
Expect(envVars).To(HaveKey("username"), "Upload container should have username env var")
Expect(envVars).To(HaveKey("password"), "Upload container should have password env var")
Expect(envVars).To(HaveKey("host"), "Upload container should have host env var")
Expect(envVars).To(HaveKey("internal_user"), "Upload container should have internal_user env var")
Expect(envVars["internal_user"]).To(Equal("false"), "internal_user should be 'false' for external user")
Expect(envVars["caseid"]).To(Equal(caseID), "caseid should match configured case ID")
ginkgo.By("Waiting for Pod to be created and start running")
var mustGatherPod *corev1.Pod
Eventually(func(g Gomega) {
podList := &corev1.PodList{}
g.Expect(nonAdminClient.List(testCtx, podList,
client.InNamespace(ns.Name),
client.MatchingLabels{jobNameLabelKey: mustGatherName})).To(Succeed())
g.Expect(podList.Items).NotTo(BeEmpty(), "Pod should be created by Job")
mustGatherPod = &podList.Items[0]
// Verify Pod has both gather and upload containers
containerNames := make(map[string]bool)
for _, c := range mustGatherPod.Spec.Containers {
containerNames[c.Name] = true
}
g.Expect(containerNames).To(HaveKey(gatherContainerName), "Pod should have gather container")
g.Expect(containerNames).To(HaveKey(uploadContainerName), "Pod should have upload container when UploadTarget is specified")
g.Expect(mustGatherPod.Status.Phase).To(
Or(Equal(corev1.PodRunning), Equal(corev1.PodSucceeded), Equal(corev1.PodFailed)),
"Pod should reach Running, Succeeded, or Failed state")
}).WithTimeout(2 * time.Minute).WithPolling(5 * time.Second).Should(Succeed())
ginkgo.By("Verifying both containers have started")
Eventually(func(g Gomega) {
g.Expect(nonAdminClient.Get(testCtx, client.ObjectKey{
Name: mustGatherPod.Name,
Namespace: ns.Name,
}, mustGatherPod)).To(Succeed())
containerStatuses := make(map[string]bool)
for _, cs := range mustGatherPod.Status.ContainerStatuses {
started := cs.State.Running != nil || cs.State.Terminated != nil
containerStatuses[cs.Name] = started
}