@@ -24,10 +24,11 @@ import (
24
24
"os"
25
25
"path/filepath"
26
26
"reflect"
27
- "strings"
28
27
"testing"
29
28
"time"
30
29
30
+ "github.com/stretchr/testify/assert"
31
+
31
32
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
32
33
"k8s.io/apimachinery/pkg/runtime"
33
34
"k8s.io/apimachinery/pkg/util/diff"
@@ -112,7 +113,7 @@ leaderElection:
112
113
t .Fatal (err )
113
114
}
114
115
115
- invalidconfigFile := filepath .Join (tmpDir , "scheduler_invalid .yaml" )
116
+ invalidconfigFile := filepath .Join (tmpDir , "scheduler_invalid_wrong_api_version .yaml" )
116
117
if err := ioutil .WriteFile (invalidconfigFile , []byte (fmt .Sprintf (`
117
118
apiVersion: componentconfig/v1alpha2
118
119
kind: KubeSchedulerConfiguration
@@ -123,6 +124,30 @@ leaderElection:
123
124
t .Fatal (err )
124
125
}
125
126
127
+ unknownFieldConfig := filepath .Join (tmpDir , "scheduler_invalid_unknown_field.yaml" )
128
+ if err := ioutil .WriteFile (unknownFieldConfig , []byte (fmt .Sprintf (`
129
+ apiVersion: kubescheduler.config.k8s.io/v1alpha1
130
+ kind: KubeSchedulerConfiguration
131
+ clientConnection:
132
+ kubeconfig: "%s"
133
+ leaderElection:
134
+ leaderElect: true
135
+ foo: bar` , configKubeconfig )), os .FileMode (0600 )); err != nil {
136
+ t .Fatal (err )
137
+ }
138
+
139
+ duplicateFieldConfig := filepath .Join (tmpDir , "scheduler_invalid_duplicate_fields.yaml" )
140
+ if err := ioutil .WriteFile (duplicateFieldConfig , []byte (fmt .Sprintf (`
141
+ apiVersion: kubescheduler.config.k8s.io/v1alpha1
142
+ kind: KubeSchedulerConfiguration
143
+ clientConnection:
144
+ kubeconfig: "%s"
145
+ leaderElection:
146
+ leaderElect: true
147
+ leaderElect: false` , configKubeconfig )), os .FileMode (0600 )); err != nil {
148
+ t .Fatal (err )
149
+ }
150
+
126
151
// flag-specified kubeconfig
127
152
flagKubeconfig := filepath .Join (tmpDir , "flag.kubeconfig" )
128
153
if err := ioutil .WriteFile (flagKubeconfig , []byte (fmt .Sprintf (`
@@ -189,6 +214,7 @@ pluginConfig:
189
214
expectedUsername string
190
215
expectedError string
191
216
expectedConfig kubeschedulerconfig.KubeSchedulerConfiguration
217
+ checkErrFn func (err error ) bool
192
218
}{
193
219
{
194
220
name : "config file" ,
@@ -433,6 +459,22 @@ pluginConfig:
433
459
options : & Options {},
434
460
expectedError : "no configuration has been provided" ,
435
461
},
462
+ {
463
+ name : "unknown field" ,
464
+ options : & Options {
465
+ ConfigFile : unknownFieldConfig ,
466
+ },
467
+ expectedError : "found unknown field: foo" ,
468
+ checkErrFn : runtime .IsStrictDecodingError ,
469
+ },
470
+ {
471
+ name : "duplicate fields" ,
472
+ options : & Options {
473
+ ConfigFile : duplicateFieldConfig ,
474
+ },
475
+ expectedError : `key "leaderElect" already set` ,
476
+ checkErrFn : runtime .IsStrictDecodingError ,
477
+ },
436
478
}
437
479
438
480
for _ , tc := range testcases {
@@ -442,11 +484,16 @@ pluginConfig:
442
484
443
485
// handle errors
444
486
if err != nil {
445
- if tc .expectedError == "" {
446
- t .Error (err )
447
- } else if ! strings .Contains (err .Error (), tc .expectedError ) {
448
- t .Errorf ("expected %q, got %q" , tc .expectedError , err .Error ())
487
+ if tc .expectedError != "" || tc .checkErrFn != nil {
488
+ if tc .expectedError != "" {
489
+ assert .Contains (t , err .Error (), tc .expectedError , tc .name )
490
+ }
491
+ if tc .checkErrFn != nil {
492
+ assert .True (t , tc .checkErrFn (err ), "got error: %v" , err )
493
+ }
494
+ return
449
495
}
496
+ assert .NoError (t , err )
450
497
return
451
498
}
452
499
0 commit comments