@@ -20,10 +20,8 @@ import (
20
20
"fmt"
21
21
"reflect"
22
22
"strconv"
23
- "strings"
24
23
"testing"
25
24
26
- "github.com/davecgh/go-spew/spew"
27
25
"github.com/stretchr/testify/assert"
28
26
"github.com/stretchr/testify/require"
29
27
@@ -85,13 +83,9 @@ func TestMutatePodNonmutating(t *testing.T) {
85
83
psp := createPSP ()
86
84
87
85
provider , err := NewSimpleProvider (psp , "namespace" , NewSimpleStrategyFactory ())
88
- if err != nil {
89
- t .Fatalf ("unable to create provider %v" , err )
90
- }
86
+ require .NoError (t , err , "unable to create provider" )
91
87
err = provider .MutatePod (pod )
92
- if err != nil {
93
- t .Fatalf ("unable to create psc %v" , err )
94
- }
88
+ require .NoError (t , err , "unable to modify pod" )
95
89
96
90
// Creating the provider or the security context should not have mutated the psp or pod
97
91
// since all the strategies were permissive
@@ -160,13 +154,9 @@ func TestMutateContainerNonmutating(t *testing.T) {
160
154
psp := createPSP ()
161
155
162
156
provider , err := NewSimpleProvider (psp , "namespace" , NewSimpleStrategyFactory ())
163
- if err != nil {
164
- t .Fatalf ("unable to create provider %v" , err )
165
- }
157
+ require .NoError (t , err , "unable to create provider" )
166
158
err = provider .MutatePod (pod )
167
- if err != nil {
168
- t .Fatalf ("unable to create container security context %v" , err )
169
- }
159
+ require .NoError (t , err , "unable to modify pod" )
170
160
171
161
// Creating the provider or the security context should not have mutated the psp or pod
172
162
// since all the strategies were permissive
@@ -443,19 +433,14 @@ func TestValidatePodFailures(t *testing.T) {
443
433
expectedError : "Flexvolume driver is not allowed to be used" ,
444
434
},
445
435
}
446
- for k , v := range errorCases {
447
- provider , err := NewSimpleProvider (v .psp , "namespace" , NewSimpleStrategyFactory ())
448
- if err != nil {
449
- t .Fatalf ("unable to create provider %v" , err )
450
- }
451
- errs := provider .ValidatePod (v .pod )
452
- if len (errs ) == 0 {
453
- t .Errorf ("%s expected validation failure but did not receive errors" , k )
454
- continue
455
- }
456
- if ! strings .Contains (errs [0 ].Error (), v .expectedError ) {
457
- t .Errorf ("%s received unexpected error %v" , k , errs )
458
- }
436
+ for name , test := range errorCases {
437
+ t .Run (name , func (t * testing.T ) {
438
+ provider , err := NewSimpleProvider (test .psp , "namespace" , NewSimpleStrategyFactory ())
439
+ require .NoError (t , err , "unable to create provider" )
440
+ errs := provider .ValidatePod (test .pod )
441
+ require .NotEmpty (t , errs , "expected validation failure but did not receive errors" )
442
+ assert .Contains (t , errs [0 ].Error (), test .expectedError , "received unexpected error" )
443
+ })
459
444
}
460
445
}
461
446
@@ -618,20 +603,13 @@ func TestValidateContainerFailures(t *testing.T) {
618
603
},
619
604
}
620
605
621
- for k , v := range errorCases {
622
- t .Run (k , func (t * testing.T ) {
623
- provider , err := NewSimpleProvider (v .psp , "namespace" , NewSimpleStrategyFactory ())
624
- if err != nil {
625
- t .Fatalf ("unable to create provider %v" , err )
626
- }
627
- errs := provider .ValidatePod (v .pod )
628
- if len (errs ) == 0 {
629
- t .Errorf ("expected validation failure but did not receive errors" )
630
- return
631
- }
632
- if ! strings .Contains (errs [0 ].Error (), v .expectedError ) {
633
- t .Errorf ("unexpected error %v\n expected: %s" , errs , v .expectedError )
634
- }
606
+ for name , test := range errorCases {
607
+ t .Run (name , func (t * testing.T ) {
608
+ provider , err := NewSimpleProvider (test .psp , "namespace" , NewSimpleStrategyFactory ())
609
+ require .NoError (t , err , "unable to create provider" )
610
+ errs := provider .ValidatePod (test .pod )
611
+ require .NotEmpty (t , errs , "expected validation failure but did not receive errors" )
612
+ assert .Contains (t , errs [0 ].Error (), test .expectedError , "unexpected error" )
635
613
})
636
614
}
637
615
}
@@ -909,16 +887,13 @@ func TestValidatePodSuccess(t *testing.T) {
909
887
},
910
888
}
911
889
912
- for k , v := range successCases {
913
- provider , err := NewSimpleProvider (v .psp , "namespace" , NewSimpleStrategyFactory ())
914
- if err != nil {
915
- t .Fatalf ("unable to create provider %v" , err )
916
- }
917
- errs := provider .ValidatePod (v .pod )
918
- if len (errs ) != 0 {
919
- t .Errorf ("%s expected validation pass but received errors %v" , k , errs )
920
- continue
921
- }
890
+ for name , test := range successCases {
891
+ t .Run (name , func (t * testing.T ) {
892
+ provider , err := NewSimpleProvider (test .psp , "namespace" , NewSimpleStrategyFactory ())
893
+ require .NoError (t , err , "unable to create provider" )
894
+ errs := provider .ValidatePod (test .pod )
895
+ assert .Empty (t , errs , "expected validation pass but received errors" )
896
+ })
922
897
}
923
898
}
924
899
@@ -1076,16 +1051,12 @@ func TestValidateContainerSuccess(t *testing.T) {
1076
1051
},
1077
1052
}
1078
1053
1079
- for k , v := range successCases {
1080
- t .Run (k , func (t * testing.T ) {
1081
- provider , err := NewSimpleProvider (v .psp , "namespace" , NewSimpleStrategyFactory ())
1082
- if err != nil {
1083
- t .Fatalf ("unable to create provider %v" , err )
1084
- }
1085
- errs := provider .ValidatePod (v .pod )
1086
- if len (errs ) != 0 {
1087
- t .Errorf ("%s expected validation pass but received errors %v\n %s" , k , errs , spew .Sdump (v .pod .ObjectMeta ))
1088
- }
1054
+ for name , test := range successCases {
1055
+ t .Run (name , func (t * testing.T ) {
1056
+ provider , err := NewSimpleProvider (test .psp , "namespace" , NewSimpleStrategyFactory ())
1057
+ require .NoError (t , err , "unable to create provider" )
1058
+ errs := provider .ValidatePod (test .pod )
1059
+ assert .Empty (t , errs , "expected validation pass but received errors" )
1089
1060
})
1090
1061
}
1091
1062
}
@@ -1144,29 +1115,21 @@ func TestGenerateContainerSecurityContextReadOnlyRootFS(t *testing.T) {
1144
1115
},
1145
1116
}
1146
1117
1147
- for k , v := range tests {
1148
- provider , err := NewSimpleProvider (v .psp , "namespace" , NewSimpleStrategyFactory ())
1149
- if err != nil {
1150
- t .Errorf ("%s unable to create provider %v" , k , err )
1151
- continue
1152
- }
1153
- err = provider .MutatePod (v .pod )
1154
- if err != nil {
1155
- t .Errorf ("%s unable to create container security context %v" , k , err )
1156
- continue
1157
- }
1158
-
1159
- sc := v .pod .Spec .Containers [0 ].SecurityContext
1160
- if v .expected == nil && sc .ReadOnlyRootFilesystem != nil {
1161
- t .Errorf ("%s expected a nil ReadOnlyRootFilesystem but got %t" , k , * sc .ReadOnlyRootFilesystem )
1162
- }
1163
- if v .expected != nil && sc .ReadOnlyRootFilesystem == nil {
1164
- t .Errorf ("%s expected a non nil ReadOnlyRootFilesystem but received nil" , k )
1165
- }
1166
- if v .expected != nil && sc .ReadOnlyRootFilesystem != nil && (* v .expected != * sc .ReadOnlyRootFilesystem ) {
1167
- t .Errorf ("%s expected a non nil ReadOnlyRootFilesystem set to %t but got %t" , k , * v .expected , * sc .ReadOnlyRootFilesystem )
1168
- }
1118
+ for name , test := range tests {
1119
+ t .Run (name , func (t * testing.T ) {
1120
+ provider , err := NewSimpleProvider (test .psp , "namespace" , NewSimpleStrategyFactory ())
1121
+ require .NoError (t , err , "unable to create provider" )
1122
+ err = provider .MutatePod (test .pod )
1123
+ require .NoError (t , err , "unable to mutate container" )
1169
1124
1125
+ sc := test .pod .Spec .Containers [0 ].SecurityContext
1126
+ if test .expected == nil {
1127
+ assert .Nil (t , sc .ReadOnlyRootFilesystem , "expected a nil ReadOnlyRootFilesystem" )
1128
+ } else {
1129
+ require .NotNil (t , sc .ReadOnlyRootFilesystem , "expected a non nil ReadOnlyRootFilesystem" )
1130
+ assert .Equal (t , * test .expected , * sc .ReadOnlyRootFilesystem )
1131
+ }
1132
+ })
1170
1133
}
1171
1134
}
1172
1135
@@ -1256,55 +1219,42 @@ func TestValidateAllowedVolumes(t *testing.T) {
1256
1219
// reflectively create the volume source
1257
1220
fieldVal := val .Type ().Field (i )
1258
1221
1259
- volumeSource := api.VolumeSource {}
1260
- volumeSourceVolume := reflect .New (fieldVal .Type .Elem ())
1222
+ t .Run (fieldVal .Name , func (t * testing.T ) {
1223
+ volumeSource := api.VolumeSource {}
1224
+ volumeSourceVolume := reflect .New (fieldVal .Type .Elem ())
1261
1225
1262
- reflect .ValueOf (& volumeSource ).Elem ().FieldByName (fieldVal .Name ).Set (volumeSourceVolume )
1263
- volume := api.Volume {VolumeSource : volumeSource }
1226
+ reflect .ValueOf (& volumeSource ).Elem ().FieldByName (fieldVal .Name ).Set (volumeSourceVolume )
1227
+ volume := api.Volume {VolumeSource : volumeSource }
1264
1228
1265
- // sanity check before moving on
1266
- fsType , err := psputil .GetVolumeFSType (volume )
1267
- if err != nil {
1268
- t .Errorf ("error getting FSType for %s: %s" , fieldVal .Name , err .Error ())
1269
- continue
1270
- }
1271
-
1272
- // add the volume to the pod
1273
- pod := defaultPod ()
1274
- pod .Spec .Volumes = []api.Volume {volume }
1275
-
1276
- // create a PSP that allows no volumes
1277
- psp := defaultPSP ()
1229
+ // sanity check before moving on
1230
+ fsType , err := psputil .GetVolumeFSType (volume )
1231
+ require .NoError (t , err , "error getting FSType" )
1278
1232
1279
- provider , err := NewSimpleProvider (psp , "namespace" , NewSimpleStrategyFactory ())
1280
- if err != nil {
1281
- t .Errorf ("error creating provider for %s: %s" , fieldVal .Name , err .Error ())
1282
- continue
1283
- }
1233
+ // add the volume to the pod
1234
+ pod := defaultPod ()
1235
+ pod .Spec .Volumes = []api.Volume {volume }
1284
1236
1285
- // expect a denial for this PSP and test the error message to ensure it's related to the volumesource
1286
- errs := provider .ValidatePod (pod )
1287
- if len (errs ) != 1 {
1288
- t .Errorf ("expected exactly 1 error for %s but got %v" , fieldVal .Name , errs )
1289
- } else {
1290
- if ! strings .Contains (errs .ToAggregate ().Error (), fmt .Sprintf ("%s volumes are not allowed to be used" , fsType )) {
1291
- t .Errorf ("did not find the expected error, received: %v" , errs )
1292
- }
1293
- }
1237
+ // create a PSP that allows no volumes
1238
+ psp := defaultPSP ()
1294
1239
1295
- // now add the fstype directly to the psp and it should validate
1296
- psp .Spec .Volumes = []policy.FSType {fsType }
1297
- errs = provider .ValidatePod (pod )
1298
- if len (errs ) != 0 {
1299
- t .Errorf ("directly allowing volume expected no errors for %s but got %v" , fieldVal .Name , errs )
1300
- }
1240
+ provider , err := NewSimpleProvider (psp , "namespace" , NewSimpleStrategyFactory ())
1241
+ require .NoError (t , err , "error creating provider" )
1301
1242
1302
- // now change the psp to allow any volumes and the pod should still validate
1303
- psp .Spec .Volumes = []policy.FSType {policy .All }
1304
- errs = provider .ValidatePod (pod )
1305
- if len (errs ) != 0 {
1306
- t .Errorf ("wildcard volume expected no errors for %s but got %v" , fieldVal .Name , errs )
1307
- }
1243
+ // expect a denial for this PSP and test the error message to ensure it's related to the volumesource
1244
+ errs := provider .ValidatePod (pod )
1245
+ require .Len (t , errs , 1 , "expected exactly 1 error" )
1246
+ assert .Contains (t , errs .ToAggregate ().Error (), fmt .Sprintf ("%s volumes are not allowed to be used" , fsType ), "did not find the expected error" )
1247
+
1248
+ // now add the fstype directly to the psp and it should validate
1249
+ psp .Spec .Volumes = []policy.FSType {fsType }
1250
+ errs = provider .ValidatePod (pod )
1251
+ assert .Empty (t , errs , "directly allowing volume expected no errors" )
1252
+
1253
+ // now change the psp to allow any volumes and the pod should still validate
1254
+ psp .Spec .Volumes = []policy.FSType {policy .All }
1255
+ errs = provider .ValidatePod (pod )
1256
+ assert .Empty (t , errs , "wildcard volume expected no errors" )
1257
+ })
1308
1258
}
1309
1259
}
1310
1260
0 commit comments