@@ -24,15 +24,14 @@ import (
24
24
"net/http/httptest"
25
25
"os"
26
26
"path/filepath"
27
- "reflect"
28
27
"testing"
29
28
"time"
30
29
30
+ "github.com/google/go-cmp/cmp"
31
31
"github.com/stretchr/testify/assert"
32
32
33
33
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
34
34
"k8s.io/apimachinery/pkg/runtime"
35
- "k8s.io/apimachinery/pkg/util/diff"
36
35
apiserveroptions "k8s.io/apiserver/pkg/server/options"
37
36
componentbaseconfig "k8s.io/component-base/config"
38
37
kubeschedulerconfig "k8s.io/kubernetes/pkg/scheduler/apis/config"
@@ -73,7 +72,7 @@ func TestSchedulerOptions(t *testing.T) {
73
72
configFile := filepath .Join (tmpDir , "scheduler.yaml" )
74
73
configKubeconfig := filepath .Join (tmpDir , "config.kubeconfig" )
75
74
if err := ioutil .WriteFile (configFile , []byte (fmt .Sprintf (`
76
- apiVersion: kubescheduler.config.k8s.io/v1alpha1
75
+ apiVersion: kubescheduler.config.k8s.io/v1alpha2
77
76
kind: KubeSchedulerConfiguration
78
77
clientConnection:
79
78
kubeconfig: "%s"
@@ -103,8 +102,8 @@ users:
103
102
t .Fatal (err )
104
103
}
105
104
106
- oldconfigFile := filepath .Join (tmpDir , "scheduler_old.yaml" )
107
- if err := ioutil .WriteFile (oldconfigFile , []byte (fmt .Sprintf (`
105
+ oldConfigFile := filepath .Join (tmpDir , "scheduler_old.yaml" )
106
+ if err := ioutil .WriteFile (oldConfigFile , []byte (fmt .Sprintf (`
108
107
apiVersion: componentconfig/v1alpha1
109
108
kind: KubeSchedulerConfiguration
110
109
clientConnection:
@@ -114,9 +113,9 @@ leaderElection:
114
113
t .Fatal (err )
115
114
}
116
115
117
- invalidconfigFile := filepath .Join (tmpDir , "scheduler_invalid_wrong_api_version.yaml" )
118
- if err := ioutil .WriteFile (invalidconfigFile , []byte (fmt .Sprintf (`
119
- apiVersion: componentconfig/v1alpha2
116
+ unknownVersionConfig := filepath .Join (tmpDir , "scheduler_invalid_wrong_api_version.yaml" )
117
+ if err := ioutil .WriteFile (unknownVersionConfig , []byte (fmt .Sprintf (`
118
+ apiVersion: kubescheduler.config.k8s.io/unknown
120
119
kind: KubeSchedulerConfiguration
121
120
clientConnection:
122
121
kubeconfig: "%s"
@@ -125,9 +124,31 @@ leaderElection:
125
124
t .Fatal (err )
126
125
}
127
126
127
+ noVersionConfig := filepath .Join (tmpDir , "scheduler_invalid_no_version.yaml" )
128
+ if err := ioutil .WriteFile (noVersionConfig , []byte (fmt .Sprintf (`
129
+ kind: KubeSchedulerConfiguration
130
+ clientConnection:
131
+ kubeconfig: "%s"
132
+ leaderElection:
133
+ leaderElect: true` , configKubeconfig )), os .FileMode (0600 )); err != nil {
134
+ t .Fatal (err )
135
+ }
136
+
137
+ unknownFieldConfigLenient := filepath .Join (tmpDir , "scheduler_invalid_unknown_field_lenient.yaml" )
138
+ if err := ioutil .WriteFile (unknownFieldConfigLenient , []byte (fmt .Sprintf (`
139
+ apiVersion: kubescheduler.config.k8s.io/v1alpha1
140
+ kind: KubeSchedulerConfiguration
141
+ clientConnection:
142
+ kubeconfig: "%s"
143
+ leaderElection:
144
+ leaderElect: true
145
+ foo: bar` , configKubeconfig )), os .FileMode (0600 )); err != nil {
146
+ t .Fatal (err )
147
+ }
148
+
128
149
unknownFieldConfig := filepath .Join (tmpDir , "scheduler_invalid_unknown_field.yaml" )
129
150
if err := ioutil .WriteFile (unknownFieldConfig , []byte (fmt .Sprintf (`
130
- apiVersion: kubescheduler.config.k8s.io/v1alpha1
151
+ apiVersion: kubescheduler.config.k8s.io/v1alpha2
131
152
kind: KubeSchedulerConfiguration
132
153
clientConnection:
133
154
kubeconfig: "%s"
@@ -137,9 +158,21 @@ foo: bar`, configKubeconfig)), os.FileMode(0600)); err != nil {
137
158
t .Fatal (err )
138
159
}
139
160
161
+ duplicateFieldConfigLenient := filepath .Join (tmpDir , "scheduler_invalid_duplicate_fields_lenient.yaml" )
162
+ if err := ioutil .WriteFile (duplicateFieldConfigLenient , []byte (fmt .Sprintf (`
163
+ apiVersion: kubescheduler.config.k8s.io/v1alpha1
164
+ kind: KubeSchedulerConfiguration
165
+ clientConnection:
166
+ kubeconfig: "%s"
167
+ leaderElection:
168
+ leaderElect: true
169
+ leaderElect: false` , configKubeconfig )), os .FileMode (0600 )); err != nil {
170
+ t .Fatal (err )
171
+ }
172
+
140
173
duplicateFieldConfig := filepath .Join (tmpDir , "scheduler_invalid_duplicate_fields.yaml" )
141
174
if err := ioutil .WriteFile (duplicateFieldConfig , []byte (fmt .Sprintf (`
142
- apiVersion: kubescheduler.config.k8s.io/v1alpha1
175
+ apiVersion: kubescheduler.config.k8s.io/v1alpha2
143
176
kind: KubeSchedulerConfiguration
144
177
clientConnection:
145
178
kubeconfig: "%s"
@@ -176,7 +209,7 @@ users:
176
209
// plugin config
177
210
pluginconfigFile := filepath .Join (tmpDir , "plugin.yaml" )
178
211
if err := ioutil .WriteFile (pluginconfigFile , []byte (fmt .Sprintf (`
179
- apiVersion: kubescheduler.config.k8s.io/v1alpha1
212
+ apiVersion: kubescheduler.config.k8s.io/v1alpha2
180
213
kind: KubeSchedulerConfiguration
181
214
clientConnection:
182
215
kubeconfig: "%s"
@@ -293,7 +326,7 @@ pluginConfig:
293
326
{
294
327
name : "config file in componentconfig/v1alpha1" ,
295
328
options : & Options {
296
- ConfigFile : oldconfigFile ,
329
+ ConfigFile : oldConfigFile ,
297
330
ComponentConfig : func () kubeschedulerconfig.KubeSchedulerConfiguration {
298
331
cfg , err := newDefaultComponentConfig ()
299
332
if err != nil {
@@ -306,9 +339,14 @@ pluginConfig:
306
339
},
307
340
308
341
{
309
- name : "invalid config file in componentconfig/v1alpha2" ,
310
- options : & Options {ConfigFile : invalidconfigFile },
311
- expectedError : "no kind \" KubeSchedulerConfiguration\" is registered for version \" componentconfig/v1alpha2\" " ,
342
+ name : "unknown version kubescheduler.config.k8s.io/unknown" ,
343
+ options : & Options {ConfigFile : unknownVersionConfig },
344
+ expectedError : "no kind \" KubeSchedulerConfiguration\" is registered for version \" kubescheduler.config.k8s.io/unknown\" " ,
345
+ },
346
+ {
347
+ name : "config file with no version" ,
348
+ options : & Options {ConfigFile : noVersionConfig },
349
+ expectedError : "Object 'apiVersion' is missing" ,
312
350
},
313
351
{
314
352
name : "kubeconfig flag" ,
@@ -522,9 +560,9 @@ pluginConfig:
522
560
expectedError : "no configuration has been provided" ,
523
561
},
524
562
{
525
- name : "unknown field" ,
563
+ name : "unknown field lenient (v1alpha1) " ,
526
564
options : & Options {
527
- ConfigFile : unknownFieldConfig ,
565
+ ConfigFile : unknownFieldConfigLenient ,
528
566
},
529
567
// TODO (obitech): Remove this comment and add a new test for v1alpha2, when it's available, as this should fail then.
530
568
// expectedError: "found unknown field: foo",
@@ -565,9 +603,17 @@ pluginConfig:
565
603
},
566
604
},
567
605
{
568
- name : "duplicate fields " ,
606
+ name : "unknown field " ,
569
607
options : & Options {
570
- ConfigFile : duplicateFieldConfig ,
608
+ ConfigFile : unknownFieldConfig ,
609
+ },
610
+ expectedError : "found unknown field: foo" ,
611
+ checkErrFn : runtime .IsStrictDecodingError ,
612
+ },
613
+ {
614
+ name : "duplicate fields lenient (v1alpha1)" ,
615
+ options : & Options {
616
+ ConfigFile : duplicateFieldConfigLenient ,
571
617
},
572
618
// TODO (obitech): Remove this comment and add a new test for v1alpha2, when it's available, as this should fail then.
573
619
// expectedError: `key "leaderElect" already set`,
@@ -607,6 +653,14 @@ pluginConfig:
607
653
Plugins : nil ,
608
654
},
609
655
},
656
+ {
657
+ name : "duplicate fields" ,
658
+ options : & Options {
659
+ ConfigFile : duplicateFieldConfig ,
660
+ },
661
+ expectedError : `key "leaderElect" already set` ,
662
+ checkErrFn : runtime .IsStrictDecodingError ,
663
+ },
610
664
}
611
665
612
666
for _ , tc := range testcases {
@@ -618,7 +672,7 @@ pluginConfig:
618
672
if err != nil {
619
673
if tc .expectedError != "" || tc .checkErrFn != nil {
620
674
if tc .expectedError != "" {
621
- assert .Contains (t , err .Error (), tc .expectedError , tc . name )
675
+ assert .Contains (t , err .Error (), tc .expectedError )
622
676
}
623
677
if tc .checkErrFn != nil {
624
678
assert .True (t , tc .checkErrFn (err ), "got error: %v" , err )
@@ -629,8 +683,8 @@ pluginConfig:
629
683
return
630
684
}
631
685
632
- if ! reflect . DeepEqual ( config . ComponentConfig , tc . expectedConfig ) {
633
- t .Errorf ("config.diff :\n %s" , diff . ObjectReflectDiff ( tc . expectedConfig , config . ComponentConfig ) )
686
+ if diff := cmp . Diff ( tc . expectedConfig , config . ComponentConfig ); diff != "" {
687
+ t .Errorf ("incorrect config (-want, +got) :\n %s" , diff )
634
688
}
635
689
636
690
// ensure we have a client
0 commit comments