@@ -22,6 +22,7 @@ import (
22
22
"reflect"
23
23
"testing"
24
24
25
+ "github.com/stretchr/testify/assert"
25
26
"github.com/stretchr/testify/require"
26
27
27
28
"k8s.io/apimachinery/pkg/runtime"
@@ -52,7 +53,28 @@ func TestReadAdmissionConfiguration(t *testing.T) {
52
53
ConfigBody string
53
54
ExpectedAdmissionConfig * apiserver.AdmissionConfiguration
54
55
PluginNames []string
56
+ ExpectedError string
55
57
}{
58
+ "duplicate field configuration error" : {
59
+ ConfigBody : `{
60
+ "apiVersion": "apiserver.k8s.io/v1alpha1",
61
+ "kind": "AdmissionConfiguration",
62
+ "plugins": [
63
+ {"name": "ImagePolicyWebhook-duplicate", "name": "ImagePolicyWebhook", "path": "image-policy-webhook.json"},
64
+ {"name": "ResourceQuota"}
65
+ ]}` ,
66
+ ExpectedError : "strict decoding error: duplicate field" ,
67
+ },
68
+ "unknown field configuration error" : {
69
+ ConfigBody : `{
70
+ "apiVersion": "apiserver.k8s.io/v1alpha1",
71
+ "kind": "AdmissionConfiguration",
72
+ "plugins": [
73
+ {"foo": "bar", "name": "ImagePolicyWebhook", "path": "image-policy-webhook.json"},
74
+ {"name": "ResourceQuota"}
75
+ ]}` ,
76
+ ExpectedError : "strict decoding error: unknown field" ,
77
+ },
56
78
"v1alpha1 configuration - path fixup" : {
57
79
ConfigBody : `{
58
80
"apiVersion": "apiserver.k8s.io/v1alpha1",
@@ -192,12 +214,18 @@ func TestReadAdmissionConfiguration(t *testing.T) {
192
214
t .Fatalf ("unexpected err writing temp file: %v" , err )
193
215
}
194
216
config , err := ReadAdmissionConfiguration (testCase .PluginNames , configFileName , scheme )
195
- if err != nil {
217
+ if testCase .ExpectedError != "" {
218
+ if err != nil {
219
+ assert .Contains (t , err .Error (), testCase .ExpectedError )
220
+ } else {
221
+ t .Fatalf ("expected error %q but received none" , testCase .ExpectedError )
222
+ }
223
+ } else if err != nil {
196
224
t .Fatalf ("unexpected err: %v" , err )
225
+ } else if ! reflect .DeepEqual (config .(configProvider ).config , testCase .ExpectedAdmissionConfig ) {
226
+ t .Fatalf ("%s: Expected:\n \t %#v\n Got:\n \t %#v" , testName , testCase .ExpectedAdmissionConfig , config .(configProvider ).config )
197
227
}
198
- if ! reflect .DeepEqual (config .(configProvider ).config , testCase .ExpectedAdmissionConfig ) {
199
- t .Errorf ("%s: Expected:\n \t %#v\n Got:\n \t %#v" , testName , testCase .ExpectedAdmissionConfig , config .(configProvider ).config )
200
- }
228
+
201
229
}
202
230
}
203
231
0 commit comments