@@ -21,6 +21,7 @@ import (
21
21
"strings"
22
22
"testing"
23
23
24
+ "k8s.io/api/core/v1"
24
25
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25
26
api "k8s.io/kubernetes/pkg/apis/core"
26
27
)
41
42
allowSpecific = map [string ]string {
42
43
AllowedProfilesAnnotationKey : "foo" ,
43
44
}
45
+ allowSpecificLocalhost = map [string ]string {
46
+ AllowedProfilesAnnotationKey : v1 .SeccompLocalhostProfileNamePrefix + "foo" ,
47
+ }
44
48
)
45
49
46
50
func TestNewStrategy (t * testing.T ) {
@@ -102,9 +106,11 @@ func TestNewStrategy(t *testing.T) {
102
106
}
103
107
104
108
func TestGenerate (t * testing.T ) {
109
+ bar := "bar"
105
110
tests := map [string ]struct {
106
111
pspAnnotations map [string ]string
107
112
podAnnotations map [string ]string
113
+ seccompProfile * api.SeccompProfile
108
114
expectedProfile string
109
115
}{
110
116
"no seccomp, no pod annotations" : {
@@ -143,10 +149,25 @@ func TestGenerate(t *testing.T) {
143
149
},
144
150
expectedProfile : "bar" ,
145
151
},
152
+ "seccomp with default, pod field" : {
153
+ pspAnnotations : allowAnyDefault ,
154
+ seccompProfile : & api.SeccompProfile {
155
+ Type : api .SeccompProfileTypeLocalhost ,
156
+ LocalhostProfile : & bar ,
157
+ },
158
+ expectedProfile : "localhost/bar" ,
159
+ },
146
160
}
147
161
for k , v := range tests {
148
162
s := NewStrategy (v .pspAnnotations )
149
- actual , err := s .Generate (v .podAnnotations , nil )
163
+ actual , err := s .Generate (v .podAnnotations , & api.Pod {
164
+ Spec : api.PodSpec {
165
+ SecurityContext : & api.PodSecurityContext {
166
+ SeccompProfile : v .seccompProfile ,
167
+ },
168
+ },
169
+ })
170
+
150
171
if err != nil {
151
172
t .Errorf ("%s received error during generation %#v" , k , err )
152
173
continue
@@ -158,9 +179,11 @@ func TestGenerate(t *testing.T) {
158
179
}
159
180
160
181
func TestValidatePod (t * testing.T ) {
182
+ foo := "foo"
161
183
tests := map [string ]struct {
162
184
pspAnnotations map [string ]string
163
185
podAnnotations map [string ]string
186
+ seccompProfile * api.SeccompProfile
164
187
expectedError string
165
188
}{
166
189
"no pod annotations, required profiles" : {
@@ -206,12 +229,44 @@ func TestValidatePod(t *testing.T) {
206
229
podAnnotations : nil ,
207
230
expectedError : "" ,
208
231
},
232
+ "valid pod annotations and field, required profiles" : {
233
+ pspAnnotations : allowSpecific ,
234
+ podAnnotations : map [string ]string {
235
+ api .SeccompPodAnnotationKey : "foo" ,
236
+ },
237
+ seccompProfile : & api.SeccompProfile {
238
+ Type : api .SeccompProfileTypeLocalhost ,
239
+ LocalhostProfile : & foo ,
240
+ },
241
+ expectedError : "" ,
242
+ },
243
+ "valid pod field and no annotation, required profiles" : {
244
+ pspAnnotations : allowSpecific ,
245
+ seccompProfile : & api.SeccompProfile {
246
+ Type : api .SeccompProfileTypeLocalhost ,
247
+ LocalhostProfile : & foo ,
248
+ },
249
+ expectedError : "Forbidden: localhost/foo is not an allowed seccomp profile. Valid values are foo" ,
250
+ },
251
+ "valid pod field and no annotation, required profiles (localhost)" : {
252
+ pspAnnotations : allowSpecificLocalhost ,
253
+ seccompProfile : & api.SeccompProfile {
254
+ Type : api .SeccompProfileTypeLocalhost ,
255
+ LocalhostProfile : & foo ,
256
+ },
257
+ expectedError : "" ,
258
+ },
209
259
}
210
260
for k , v := range tests {
211
261
pod := & api.Pod {
212
262
ObjectMeta : metav1.ObjectMeta {
213
263
Annotations : v .podAnnotations ,
214
264
},
265
+ Spec : api.PodSpec {
266
+ SecurityContext : & api.PodSecurityContext {
267
+ SeccompProfile : v .seccompProfile ,
268
+ },
269
+ },
215
270
}
216
271
s := NewStrategy (v .pspAnnotations )
217
272
errs := s .ValidatePod (pod )
@@ -231,9 +286,12 @@ func TestValidatePod(t *testing.T) {
231
286
}
232
287
233
288
func TestValidateContainer (t * testing.T ) {
289
+ foo := "foo"
290
+ bar := "bar"
234
291
tests := map [string ]struct {
235
292
pspAnnotations map [string ]string
236
293
podAnnotations map [string ]string
294
+ seccompProfile * api.SeccompProfile
237
295
expectedError string
238
296
}{
239
297
"no pod annotations, required profiles" : {
@@ -293,6 +351,22 @@ func TestValidateContainer(t *testing.T) {
293
351
},
294
352
expectedError : "Forbidden: bar is not an allowed seccomp profile. Valid values are foo" ,
295
353
},
354
+ "valid container field and no annotation, required profiles" : {
355
+ pspAnnotations : allowSpecificLocalhost ,
356
+ seccompProfile : & api.SeccompProfile {
357
+ Type : api .SeccompProfileTypeLocalhost ,
358
+ LocalhostProfile : & foo ,
359
+ },
360
+ expectedError : "" ,
361
+ },
362
+ "invalid container field and no annotation, required profiles" : {
363
+ pspAnnotations : allowSpecificLocalhost ,
364
+ seccompProfile : & api.SeccompProfile {
365
+ Type : api .SeccompProfileTypeLocalhost ,
366
+ LocalhostProfile : & bar ,
367
+ },
368
+ expectedError : "Forbidden: localhost/bar is not an allowed seccomp profile. Valid values are localhost/foo" ,
369
+ },
296
370
}
297
371
for k , v := range tests {
298
372
pod := & api.Pod {
@@ -302,6 +376,9 @@ func TestValidateContainer(t *testing.T) {
302
376
}
303
377
container := & api.Container {
304
378
Name : "container" ,
379
+ SecurityContext : & api.SecurityContext {
380
+ SeccompProfile : v .seccompProfile ,
381
+ },
305
382
}
306
383
307
384
s := NewStrategy (v .pspAnnotations )
0 commit comments