@@ -31,7 +31,6 @@ import (
31
31
policy "k8s.io/api/policy/v1beta1"
32
32
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
33
33
"k8s.io/apimachinery/pkg/util/diff"
34
- "k8s.io/apimachinery/pkg/util/validation/field"
35
34
api "k8s.io/kubernetes/pkg/apis/core"
36
35
k8s_api_v1 "k8s.io/kubernetes/pkg/apis/core/v1"
37
36
"k8s.io/kubernetes/pkg/security/apparmor"
@@ -41,7 +40,7 @@ import (
41
40
42
41
const defaultContainerName = "test-c"
43
42
44
- func TestDefaultPodSecurityContextNonmutating (t * testing.T ) {
43
+ func TestMutatePodNonmutating (t * testing.T ) {
45
44
// Create a pod with a security context that needs filling in
46
45
createPod := func () * api.Pod {
47
46
return & api.Pod {
@@ -89,7 +88,7 @@ func TestDefaultPodSecurityContextNonmutating(t *testing.T) {
89
88
if err != nil {
90
89
t .Fatalf ("unable to create provider %v" , err )
91
90
}
92
- err = provider .DefaultPodSecurityContext (pod )
91
+ err = provider .MutatePod (pod )
93
92
if err != nil {
94
93
t .Fatalf ("unable to create psc %v" , err )
95
94
}
@@ -98,14 +97,14 @@ func TestDefaultPodSecurityContextNonmutating(t *testing.T) {
98
97
// since all the strategies were permissive
99
98
if ! reflect .DeepEqual (createPod (), pod ) {
100
99
diffs := diff .ObjectDiff (createPod (), pod )
101
- t .Errorf ("pod was mutated by DefaultPodSecurityContext . diff:\n %s" , diffs )
100
+ t .Errorf ("pod was mutated by MutatePod . diff:\n %s" , diffs )
102
101
}
103
102
if ! reflect .DeepEqual (createPSP (), psp ) {
104
- t .Error ("psp was mutated by DefaultPodSecurityContext " )
103
+ t .Error ("psp was mutated by MutatePod " )
105
104
}
106
105
}
107
106
108
- func TestDefaultContainerSecurityContextNonmutating (t * testing.T ) {
107
+ func TestMutateContainerNonmutating (t * testing.T ) {
109
108
untrue := false
110
109
tests := []struct {
111
110
security * api.SecurityContext
@@ -134,7 +133,6 @@ func TestDefaultContainerSecurityContextNonmutating(t *testing.T) {
134
133
Name : "psp-sa" ,
135
134
Annotations : map [string ]string {
136
135
seccomp .AllowedProfilesAnnotationKey : "*" ,
137
- seccomp .DefaultProfileAnnotationKey : "foo" ,
138
136
},
139
137
},
140
138
Spec : policy.PodSecurityPolicySpec {
@@ -165,7 +163,7 @@ func TestDefaultContainerSecurityContextNonmutating(t *testing.T) {
165
163
if err != nil {
166
164
t .Fatalf ("unable to create provider %v" , err )
167
165
}
168
- err = provider .DefaultContainerSecurityContext (pod , & pod . Spec . Containers [ 0 ] )
166
+ err = provider .MutatePod (pod )
169
167
if err != nil {
170
168
t .Fatalf ("unable to create container security context %v" , err )
171
169
}
@@ -174,15 +172,15 @@ func TestDefaultContainerSecurityContextNonmutating(t *testing.T) {
174
172
// since all the strategies were permissive
175
173
if ! reflect .DeepEqual (createPod (), pod ) {
176
174
diffs := diff .ObjectDiff (createPod (), pod )
177
- t .Errorf ("pod was mutated by DefaultContainerSecurityContext . diff:\n %s" , diffs )
175
+ t .Errorf ("pod was mutated. diff:\n %s" , diffs )
178
176
}
179
177
if ! reflect .DeepEqual (createPSP (), psp ) {
180
- t .Error ("psp was mutated by DefaultContainerSecurityContext " )
178
+ t .Error ("psp was mutated" )
181
179
}
182
180
}
183
181
}
184
182
185
- func TestValidatePodSecurityContextFailures (t * testing.T ) {
183
+ func TestValidatePodFailures (t * testing.T ) {
186
184
failHostNetworkPod := defaultPod ()
187
185
failHostNetworkPod .Spec .SecurityContext .HostNetwork = true
188
186
@@ -504,6 +502,7 @@ func TestValidateContainerFailures(t *testing.T) {
504
502
},
505
503
}
506
504
failSELinuxPod := defaultPod ()
505
+ failSELinuxPod .Spec .SecurityContext .SELinuxOptions = & api.SELinuxOptions {Level : "foo" }
507
506
failSELinuxPod .Spec .Containers [0 ].SecurityContext .SELinuxOptions = & api.SELinuxOptions {
508
507
Level : "bar" ,
509
508
}
@@ -620,22 +619,24 @@ func TestValidateContainerFailures(t *testing.T) {
620
619
}
621
620
622
621
for k , v := range errorCases {
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 .ValidateContainer (v .pod , & v .pod .Spec .Containers [0 ], field .NewPath ("" ))
628
- if len (errs ) == 0 {
629
- t .Errorf ("%s expected validation failure but did not receive errors" , k )
630
- continue
631
- }
632
- if ! strings .Contains (errs [0 ].Error (), v .expectedError ) {
633
- t .Errorf ("%s received unexpected error %v\n expected: %s" , k , errs , v .expectedError )
634
- }
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
+ }
635
+ })
635
636
}
636
637
}
637
638
638
- func TestValidatePodSecurityContextSuccess (t * testing.T ) {
639
+ func TestValidatePodSuccess (t * testing.T ) {
639
640
hostNetworkPSP := defaultPSP ()
640
641
hostNetworkPSP .Spec .HostNetwork = true
641
642
hostNetworkPod := defaultPod ()
@@ -941,6 +942,7 @@ func TestValidateContainerSuccess(t *testing.T) {
941
942
},
942
943
}
943
944
seLinuxPod := defaultPod ()
945
+ seLinuxPod .Spec .SecurityContext .SELinuxOptions = & api.SELinuxOptions {Level : "foo" }
944
946
seLinuxPod .Spec .Containers [0 ].SecurityContext .SELinuxOptions = & api.SELinuxOptions {
945
947
Level : "foo" ,
946
948
}
@@ -1007,6 +1009,7 @@ func TestValidateContainerSuccess(t *testing.T) {
1007
1009
1008
1010
seccompPod := defaultPod ()
1009
1011
seccompPod .Annotations = map [string ]string {
1012
+ api .SeccompPodAnnotationKey : "foo" ,
1010
1013
api .SeccompContainerAnnotationKeyPrefix + seccompPod .Spec .Containers [0 ].Name : "foo" ,
1011
1014
}
1012
1015
@@ -1074,15 +1077,16 @@ func TestValidateContainerSuccess(t *testing.T) {
1074
1077
}
1075
1078
1076
1079
for k , v := range successCases {
1077
- provider , err := NewSimpleProvider (v .psp , "namespace" , NewSimpleStrategyFactory ())
1078
- if err != nil {
1079
- t .Fatalf ("unable to create provider %v" , err )
1080
- }
1081
- errs := provider .ValidateContainer (v .pod , & v .pod .Spec .Containers [0 ], field .NewPath ("" ))
1082
- if len (errs ) != 0 {
1083
- t .Errorf ("%s expected validation pass but received errors %v\n %s" , k , errs , spew .Sdump (v .pod .ObjectMeta ))
1084
- continue
1085
- }
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
+ }
1089
+ })
1086
1090
}
1087
1091
}
1088
1092
@@ -1146,7 +1150,7 @@ func TestGenerateContainerSecurityContextReadOnlyRootFS(t *testing.T) {
1146
1150
t .Errorf ("%s unable to create provider %v" , k , err )
1147
1151
continue
1148
1152
}
1149
- err = provider .DefaultContainerSecurityContext (v .pod , & v . pod . Spec . Containers [ 0 ] )
1153
+ err = provider .MutatePod (v .pod )
1150
1154
if err != nil {
1151
1155
t .Errorf ("%s unable to create container security context %v" , k , err )
1152
1156
continue
@@ -1351,10 +1355,10 @@ func TestAllowPrivilegeEscalation(t *testing.T) {
1351
1355
provider , err := NewSimpleProvider (psp , "namespace" , NewSimpleStrategyFactory ())
1352
1356
require .NoError (t , err )
1353
1357
1354
- err = provider .DefaultContainerSecurityContext (pod , & pod . Spec . Containers [ 0 ] )
1358
+ err = provider .MutatePod (pod )
1355
1359
require .NoError (t , err )
1356
1360
1357
- errs := provider .ValidateContainer (pod , & pod . Spec . Containers [ 0 ], field . NewPath ( "" ) )
1361
+ errs := provider .ValidatePod (pod )
1358
1362
if test .expectErr {
1359
1363
assert .NotEmpty (t , errs , "expected validation error" )
1360
1364
} else {
0 commit comments