@@ -86,9 +86,11 @@ var _ = ginkgo.Describe(common.AccessControlTestKey, func() {
86
86
renderedTestCase , err := testConfigure .RenderTestCaseSpec (testcases .Cnf , testType )
87
87
gomega .Expect (err ).To (gomega .BeNil ())
88
88
gomega .Expect (renderedTestCase ).ToNot (gomega .BeNil ())
89
- for _ , testCase := range renderedTestCase .TestCase {
90
- if ! testCase .SkipTest {
91
- runTestOnPods (env , testCase , testType )
89
+
90
+ // Loop through test cases
91
+ for i := range renderedTestCase .TestCase {
92
+ if ! renderedTestCase .TestCase [i ].SkipTest {
93
+ runTestOnPods (env , & renderedTestCase .TestCase [i ], testType )
92
94
}
93
95
}
94
96
}
@@ -111,23 +113,19 @@ func addFailedTcInfo(failedTcs map[string][]failedTcInfo, tc, pod, ns string, co
111
113
}
112
114
}
113
115
114
- //nolint:gocritic, funlen // ignore hugeParam error. Pointers to loop iterator vars are bad and `testCmd` is likely to be such.
115
- func runTestOnPods (env * config.TestEnvironment , testCmd testcases.BaseTestCase , testType string ) {
116
+ //nolint:funlen // ignore hugeParam error. Pointers to loop iterator vars are bad and `testCmd` is likely to be such.
117
+ func runTestOnPods (env * config.TestEnvironment , testCmd * testcases.BaseTestCase , testType string ) {
116
118
const noContainerIdx = - 1
117
119
testID := identifiers .XformToGinkgoItIdentifierExtended (identifiers .TestHostResourceIdentifier , testCmd .Name )
118
120
ginkgo .It (testID , ginkgo .Label (testID ), func () {
119
121
context := env .GetLocalShellContext ()
120
122
failedTcs := map [string ][]failedTcInfo {} // maps a pod name to a slice of failed TCs
121
123
for _ , podUnderTest := range env .PodsUnderTest {
122
- podName := podUnderTest .Name
123
- podNamespace := podUnderTest .Namespace
124
124
if testCmd .ExpectedType == testcases .Function {
125
125
for _ , val := range testCmd .ExpectedStatus {
126
- testCmd .ExpectedStatusFn (podName , testcases .StatusFunctionType (val ))
126
+ testCmd .ExpectedStatusFn (podUnderTest . Name , testcases .StatusFunctionType (val ))
127
127
}
128
128
}
129
- testType := testType
130
- testCmd := testCmd
131
129
var args []interface {}
132
130
if testType == testcases .PrivilegedRoles {
133
131
args = []interface {}{podUnderTest .Namespace , podUnderTest .Namespace , podUnderTest .ServiceAccount }
@@ -144,8 +142,8 @@ func runTestOnPods(env *config.TestEnvironment, testCmd testcases.BaseTestCase,
144
142
if count > 0 {
145
143
count := 0
146
144
for count < podUnderTest .ContainerCount {
147
- ginkgo .By (fmt .Sprintf ("Executing TC %s on pod %s (ns %s), container index %d" , testCmd .Name , podNamespace , podName , count ))
148
- argsCount := append (args , count )
145
+ ginkgo .By (fmt .Sprintf ("Executing TC %s on pod %s (ns %s), container index %d" , testCmd .Name , podUnderTest . Namespace , podUnderTest . Name , count ))
146
+ argsCount := append (args , count ) //nolint:gocritic
149
147
cmd := fmt .Sprintf (testCmd .Command , argsCount ... )
150
148
cmdArgs := strings .Split (cmd , " " )
151
149
cnfInTest := containerpkg .NewPod (cmdArgs , podUnderTest .Name , podUnderTest .Namespace , testCmd .ExpectedStatus , testCmd .ResultType , testCmd .Action , common .DefaultTimeout )
@@ -155,15 +153,15 @@ func runTestOnPods(env *config.TestEnvironment, testCmd testcases.BaseTestCase,
155
153
gomega .Expect (test ).ToNot (gomega .BeNil ())
156
154
test .RunWithCallbacks (nil , func () {
157
155
tnf .ClaimFilePrintf ("FAILURE: Command sent: %s, Expectations: %v" , cmd , testCmd .ExpectedStatus )
158
- addFailedTcInfo (failedTcs , testCmd .Name , podName , podNamespace , count )
156
+ addFailedTcInfo (failedTcs , testCmd .Name , podUnderTest . Name , podUnderTest . Namespace , count )
159
157
}, func (e error ) {
160
158
tnf .ClaimFilePrintf ("ERROR: Command sent: %s, Expectations: %v, Error: %v" , cmd , testCmd .ExpectedStatus , e )
161
- addFailedTcInfo (failedTcs , testCmd .Name , podName , podNamespace , count )
159
+ addFailedTcInfo (failedTcs , testCmd .Name , podUnderTest . Name , podUnderTest . Namespace , count )
162
160
})
163
161
count ++
164
162
}
165
163
} else {
166
- ginkgo .By (fmt .Sprintf ("Executing TC %s on pod %s (ns %s)" , testCmd .Name , podNamespace , podName ))
164
+ ginkgo .By (fmt .Sprintf ("Executing TC %s on pod %s (ns %s)" , testCmd .Name , podUnderTest . Namespace , podUnderTest . Name ))
167
165
cmd := fmt .Sprintf (testCmd .Command , args ... )
168
166
cmdArgs := strings .Split (cmd , " " )
169
167
podTest := containerpkg .NewPod (cmdArgs , podUnderTest .Name , podUnderTest .Namespace , testCmd .ExpectedStatus , testCmd .ResultType , testCmd .Action , common .DefaultTimeout )
@@ -173,10 +171,10 @@ func runTestOnPods(env *config.TestEnvironment, testCmd testcases.BaseTestCase,
173
171
gomega .Expect (test ).ToNot (gomega .BeNil ())
174
172
test .RunWithCallbacks (nil , func () {
175
173
tnf .ClaimFilePrintf ("FAILURE: Command sent: %s, Expectations: %v" , cmd , testCmd .ExpectedStatus )
176
- addFailedTcInfo (failedTcs , testCmd .Name , podName , podNamespace , noContainerIdx )
174
+ addFailedTcInfo (failedTcs , testCmd .Name , podUnderTest . Name , podUnderTest . Namespace , noContainerIdx )
177
175
}, func (e error ) {
178
176
tnf .ClaimFilePrintf ("ERROR: Command sent: %s, Expectations: %v, Error: %v" , cmd , testCmd .ExpectedStatus , e )
179
- addFailedTcInfo (failedTcs , testCmd .Name , podName , podNamespace , noContainerIdx )
177
+ addFailedTcInfo (failedTcs , testCmd .Name , podUnderTest . Name , podUnderTest . Namespace , noContainerIdx )
180
178
})
181
179
}
182
180
}
@@ -189,24 +187,26 @@ func runTestOnPods(env *config.TestEnvironment, testCmd testcases.BaseTestCase,
189
187
}
190
188
191
189
func getCrsNamespaces (crdName , crdKind string , context * interactive.Context ) (map [string ]string , error ) {
192
- const expectedNumFields = 2
193
- const crNameFieldIdx = 0
194
- const namespaceFieldIdx = 0
195
-
196
190
gomega .Expect (crdKind ).NotTo (gomega .BeEmpty ())
197
191
getCrNamespaceCommand := fmt .Sprintf (ocGetCrNamespaceFormat , crdKind )
198
192
cmdOut := utils .ExecuteCommandAndValidate (getCrNamespaceCommand , common .DefaultTimeout , context , func () {
199
193
common .TcClaimLogPrintf ("CRD %s: Failed to get CRs (kind=%s)" , crdName , crdKind )
200
194
})
201
195
202
- crNamespaces := map [string ]string {}
196
+ return parseCrOutput (cmdOut )
197
+ }
203
198
204
- if cmdOut == "" {
199
+ func parseCrOutput (rawOutput string ) (map [string ]string , error ) {
200
+ const crNameFieldIdx = 0
201
+ const namespaceFieldIdx = 1
202
+ const expectedNumFields = 2
203
+ crNamespaces := map [string ]string {}
204
+ if rawOutput == "" {
205
205
// Filter out empty (0 CRs) output.
206
206
return crNamespaces , nil
207
207
}
208
208
209
- lines := strings .Split (cmdOut , "\n " )
209
+ lines := strings .Split (rawOutput , "\n " )
210
210
for _ , line := range lines {
211
211
lineFields := strings .Split (line , "," )
212
212
if len (lineFields ) != expectedNumFields {
@@ -234,15 +234,7 @@ func testCrsNamespaces(crNames, configNamespaces []string, context *interactive.
234
234
ginkgo .By (fmt .Sprintf ("CRD %s has %d CRs (plural name: %s)." , crdName , len (crNamespaces ), crdPluralName ))
235
235
for crName , namespace := range crNamespaces {
236
236
ginkgo .By (fmt .Sprintf ("Checking CR %s - Namespace %s" , crName , namespace ))
237
- found := false
238
- for _ , configNamespace := range configNamespaces {
239
- if namespace == configNamespace {
240
- found = true
241
- break
242
- }
243
- }
244
-
245
- if ! found {
237
+ if ! utils .StringInSlice (configNamespaces , namespace ) {
246
238
common .TcClaimLogPrintf ("CRD: %s (kind:%s) - CR %s has an invalid namespace (%s)" , crdName , crdPluralName , crName , namespace )
247
239
if crNames , exists := invalidCrs [crdName ]; exists {
248
240
invalidCrs [crdName ] = append (crNames , crName )
@@ -334,17 +326,14 @@ func testAutomountService(env *config.TestEnvironment) {
334
326
msg := []string {}
335
327
for _ , podUnderTest := range env .PodsUnderTest {
336
328
ginkgo .By (fmt .Sprintf ("check the existence of pod service account %s (ns= %s )" , podUnderTest .Namespace , podUnderTest .Name ))
337
- podName := podUnderTest .Name
338
- podNamespace := podUnderTest .Namespace
339
- serviceAccountName := podUnderTest .ServiceAccount
340
- gomega .Expect (serviceAccountName ).ToNot (gomega .BeEmpty ())
329
+ gomega .Expect (podUnderTest .ServiceAccount ).ToNot (gomega .BeEmpty ())
341
330
context := env .GetLocalShellContext ()
342
- tester := automountservice .NewAutomountService (automountservice .WithNamespace (podNamespace ), automountservice .WithServiceAccount (serviceAccountName ))
331
+ tester := automountservice .NewAutomountService (automountservice .WithNamespace (podUnderTest . Namespace ), automountservice .WithServiceAccount (podUnderTest . ServiceAccount ))
343
332
test , err := tnf .NewTest (context .GetExpecter (), tester , []reel.Handler {tester }, context .GetErrorChannel ())
344
333
gomega .Expect (err ).To (gomega .BeNil ())
345
334
test .RunAndValidate ()
346
335
serviceAccountToken := tester .Token ()
347
- tester = automountservice .NewAutomountService (automountservice .WithNamespace (podNamespace ), automountservice .WithPodname (podName ))
336
+ tester = automountservice .NewAutomountService (automountservice .WithNamespace (podUnderTest . Namespace ), automountservice .WithPodname (podUnderTest . Name ))
348
337
test , err = tnf .NewTest (context .GetExpecter (), tester , []reel.Handler {tester }, context .GetErrorChannel ())
349
338
gomega .Expect (err ).To (gomega .BeNil ())
350
339
test .RunAndValidate ()
@@ -357,7 +346,7 @@ func testAutomountService(env *config.TestEnvironment) {
357
346
// the test would pass iif token is explicitly set to false
358
347
// if the token is set to true in the pod, the test would fail right away
359
348
if podToken == automountservice .TokenIsTrue {
360
- msg = append (msg , fmt .Sprintf ("Pod %s:%s is configured with automountServiceAccountToken set to true " , podNamespace , podName ))
349
+ msg = append (msg , fmt .Sprintf ("Pod %s:%s is configured with automountServiceAccountToken set to true " , podUnderTest . Namespace , podUnderTest . Name ))
361
350
continue
362
351
}
363
352
// The pod token is false means the pod is configured properly
@@ -370,12 +359,12 @@ func testAutomountService(env *config.TestEnvironment) {
370
359
// using this service account are not configured properly, register the error
371
360
// message and fail
372
361
if serviceAccountToken == automountservice .TokenIsTrue {
373
- msg = append (msg , fmt .Sprintf ("serviceaccount %s:%s is configured with automountServiceAccountToken set to true, impacting pod %s " , podNamespace , serviceAccountName , podName ))
362
+ msg = append (msg , fmt .Sprintf ("serviceaccount %s:%s is configured with automountServiceAccountToken set to true, impacting pod %s " , podUnderTest . Namespace , podUnderTest . ServiceAccount , podUnderTest . Name ))
374
363
}
375
364
// the token should be set explicitly to false, otherwise, it's a failure
376
365
// register the error message and check the next pod
377
366
if serviceAccountToken == automountservice .TokenNotSet {
378
- msg = append (msg , fmt .Sprintf ("serviceaccount %s:%s is not configured with automountServiceAccountToken set to false, impacting pod %s " , podNamespace , serviceAccountName , podName ))
367
+ msg = append (msg , fmt .Sprintf ("serviceaccount %s:%s is not configured with automountServiceAccountToken set to false, impacting pod %s " , podUnderTest . Namespace , podUnderTest . ServiceAccount , podUnderTest . Name ))
379
368
}
380
369
}
381
370
if len (msg ) > 0 {
@@ -391,22 +380,19 @@ func testRoleBindings(env *config.TestEnvironment) {
391
380
failedPods := []* configsections.Pod {}
392
381
ginkgo .By ("Should not have RoleBinding in other namespaces" )
393
382
for _ , podUnderTest := range env .PodsUnderTest {
394
- podName := podUnderTest .Name
395
- podNamespace := podUnderTest .Namespace
396
- serviceAccountName := podUnderTest .ServiceAccount
397
383
context := env .GetLocalShellContext ()
398
- ginkgo .By (fmt .Sprintf ("Testing role binding %s %s" , podNamespace , podName ))
399
- if serviceAccountName == "" {
384
+ ginkgo .By (fmt .Sprintf ("Testing role binding %s %s" , podUnderTest . Namespace , podUnderTest . Name ))
385
+ if podUnderTest . ServiceAccount == "" {
400
386
ginkgo .Skip ("Can not test when serviceAccountName is empty. Please check previous tests for failures" )
401
387
}
402
- rbTester := rolebinding .NewRoleBinding (common .DefaultTimeout , serviceAccountName , podNamespace )
388
+ rbTester := rolebinding .NewRoleBinding (common .DefaultTimeout , podUnderTest . ServiceAccount , podUnderTest . Namespace )
403
389
test , err := tnf .NewTest (context .GetExpecter (), rbTester , []reel.Handler {rbTester }, context .GetErrorChannel ())
404
390
gomega .Expect (err ).To (gomega .BeNil ())
405
391
test .RunWithCallbacks (nil , func () {
406
- tnf .ClaimFilePrintf ("FAILURE: Pod %s (ns: %s) roleBindings: %v" , podName , podNamespace , rbTester .GetRoleBindings ())
392
+ tnf .ClaimFilePrintf ("FAILURE: Pod %s (ns: %s) roleBindings: %v" , podUnderTest . Name , podUnderTest . Namespace , rbTester .GetRoleBindings ())
407
393
failedPods = append (failedPods , podUnderTest )
408
394
}, func (err error ) {
409
- tnf .ClaimFilePrintf ("ERROR: Pod %s (ns: %s) roleBindings: %v, error: %v" , podName , podNamespace , rbTester .GetRoleBindings (), err )
395
+ tnf .ClaimFilePrintf ("ERROR: Pod %s (ns: %s) roleBindings: %v, error: %v" , podUnderTest . Name , podUnderTest . Namespace , rbTester .GetRoleBindings (), err )
410
396
failedPods = append (failedPods , podUnderTest )
411
397
})
412
398
}
@@ -423,22 +409,19 @@ func testClusterRoleBindings(env *config.TestEnvironment) {
423
409
ginkgo .By ("Should not have ClusterRoleBindings" )
424
410
failedPods := []* configsections.Pod {}
425
411
for _ , podUnderTest := range env .PodsUnderTest {
426
- podName := podUnderTest .Name
427
- podNamespace := podUnderTest .Namespace
428
- serviceAccountName := podUnderTest .ServiceAccount
429
412
context := env .GetLocalShellContext ()
430
- ginkgo .By (fmt .Sprintf ("Testing cluster role binding %s %s" , podNamespace , podName ))
431
- if serviceAccountName == "" {
413
+ ginkgo .By (fmt .Sprintf ("Testing cluster role binding %s %s" , podUnderTest . Namespace , podUnderTest . Name ))
414
+ if podUnderTest . ServiceAccount == "" {
432
415
ginkgo .Skip ("Can not test when serviceAccountName is empty. Please check previous tests for failures" )
433
416
}
434
- crbTester := clusterrolebinding .NewClusterRoleBinding (common .DefaultTimeout , serviceAccountName , podNamespace )
417
+ crbTester := clusterrolebinding .NewClusterRoleBinding (common .DefaultTimeout , podUnderTest . ServiceAccount , podUnderTest . Namespace )
435
418
test , err := tnf .NewTest (context .GetExpecter (), crbTester , []reel.Handler {crbTester }, context .GetErrorChannel ())
436
419
gomega .Expect (err ).To (gomega .BeNil ())
437
420
test .RunWithCallbacks (nil , func () {
438
- tnf .ClaimFilePrintf ("FAILURE: Pod: %s (ns: %s) SA: %s clusterRoleBindings: %v" , podName , podNamespace , serviceAccountName , crbTester .GetClusterRoleBindings ())
421
+ tnf .ClaimFilePrintf ("FAILURE: Pod: %s (ns: %s) SA: %s clusterRoleBindings: %v" , podUnderTest . Name , podUnderTest . Namespace , podUnderTest . ServiceAccount , crbTester .GetClusterRoleBindings ())
439
422
failedPods = append (failedPods , podUnderTest )
440
423
}, func (err error ) {
441
- tnf .ClaimFilePrintf ("ERROR: Pod: %s (ns: %s) SA: %s clusterRoleBindings: %v, error: %v" , podName , podNamespace , serviceAccountName , crbTester .GetClusterRoleBindings (), err )
424
+ tnf .ClaimFilePrintf ("ERROR: Pod: %s (ns: %s) SA: %s clusterRoleBindings: %v, error: %v" , podUnderTest . Name , podUnderTest . Namespace , podUnderTest . ServiceAccount , crbTester .GetClusterRoleBindings (), err )
442
425
failedPods = append (failedPods , podUnderTest )
443
426
})
444
427
}
0 commit comments