@@ -439,49 +439,34 @@ func (t *testCase) evaluateOutput(createdSars []authv1.SubjectAccessReview, allo
439
439
// to help debug the RBAC test cases.
440
440
func prettyPrintSAR (sar authv1.SubjectAccessReview ) string {
441
441
442
+ // helper function to print the field values conditionally
443
+ ifNotNil := func (k , v string ) string {
444
+ if v != "" {
445
+ return "\n " + k + ": " + v
446
+ }
447
+ return ""
448
+ }
449
+
442
450
str := "\n SubjectAccessReviewSpec:"
443
451
// we print the field values conditionally since some fields might be empty
444
452
// this helps in making the output more readable
445
- if ns := sar .Spec .ResourceAttributes .Namespace ; ns != "" {
446
- str += "\n Namespace: " + ns
447
- }
448
- if verb := sar .Spec .ResourceAttributes .Verb ; verb != "" {
449
- str += "\n Verb: " + verb
450
- }
451
- if group := sar .Spec .ResourceAttributes .Group ; group != "" {
452
- str += "\n APIGroup: " + group
453
- }
454
- if resource := sar .Spec .ResourceAttributes .Resource ; resource != "" {
455
- str += "\n Resource: " + resource
456
- }
457
- if subresource := sar .Spec .ResourceAttributes .Subresource ; subresource != "" {
458
- str += "\n Subresource: " + subresource
459
- }
460
- if name := sar .Spec .ResourceAttributes .Name ; name != "" {
461
- str += "\n Name: " + name
462
- }
453
+ str += ifNotNil ("Namespace" , sar .Spec .ResourceAttributes .Namespace )
454
+ str += ifNotNil ("Verb" , sar .Spec .ResourceAttributes .Verb )
455
+ str += ifNotNil ("Group" , sar .Spec .ResourceAttributes .Group )
456
+ str += ifNotNil ("Resource" , sar .Spec .ResourceAttributes .Resource )
457
+ str += ifNotNil ("Subresource" , sar .Spec .ResourceAttributes .Subresource )
458
+ str += ifNotNil ("Name" , sar .Spec .ResourceAttributes .Name )
463
459
if sar .Spec .NonResourceAttributes != nil {
464
- if verb := sar .Spec .NonResourceAttributes .Verb ; verb != "" {
465
- str += "\n NonResourceVerb: " + verb
466
- }
467
- if path := sar .Spec .NonResourceAttributes .Path ; path != "" {
468
- str += "\n NonResourcePath: " + path
469
- }
470
- }
471
- if user := sar .Spec .User ; user != "" {
472
- str += "\n User: " + user
473
- }
474
- if groups := sar .Spec .Groups ; len (groups ) > 0 {
475
- str += "\n Groups: " + strings .Join (groups , "," )
460
+ str += ifNotNil ("Path" , sar .Spec .NonResourceAttributes .Path )
461
+ str += ifNotNil ("Verb" , sar .Spec .NonResourceAttributes .Verb )
476
462
}
463
+ str += ifNotNil ("User" , sar .Spec .User )
464
+ str += ifNotNil ("Groups" , strings .Join (sar .Spec .Groups , "," ))
477
465
str += "\n SubjectAccessReviewStatus:"
478
466
// these fields are always present in the SubjectAccessReviewStatus
479
467
str += "\n Allowed: " + strconv .FormatBool (sar .Status .Allowed )
480
468
str += "\n Denied: " + strconv .FormatBool (sar .Status .Denied )
481
-
482
- if reason := sar .Status .Reason ; reason != "" {
483
- str += "\n Reason: " + reason
484
- }
469
+ str += ifNotNil ("Reason" , sar .Status .Reason )
485
470
str += "\n "
486
471
return str
487
472
}
0 commit comments