@@ -192,43 +192,59 @@ func TestValidateMultipleResourcesWithErrors(t *testing.T) {
192192 }
193193}
194194
195- func TestDetermineSchema (t * testing.T ) {
196- config := NewDefaultConfig ()
197- schema := determineSchema ("sample" , "v1" , config )
198- if schema != "https://kubernetesjsonschema.dev/master-standalone/sample-v1.json" {
199- t .Errorf ("Schema should default to master, instead %s" , schema )
200- }
201- }
202-
203- func TestDetermineSchemaForVersions (t * testing.T ) {
204- config := NewDefaultConfig ()
205- config .KubernetesVersion = "1.0"
206- schema := determineSchema ("sample" , "v1" , config )
207- if schema != "https://kubernetesjsonschema.dev/v1.0-standalone/sample-v1.json" {
208- t .Errorf ("Should be able to specify a version, instead %s" , schema )
195+ func TestDetermineSchemaURL (t * testing.T ) {
196+ var tests = []struct {
197+ config * Config
198+ baseURL string
199+ kind string
200+ version string
201+ expected string
202+ }{
203+ {
204+ config : NewDefaultConfig (),
205+ baseURL : "https://base" ,
206+ kind : "sample" ,
207+ version : "v1" ,
208+ expected : "https://base/master-standalone/sample-v1.json" ,
209+ },
210+ {
211+ config : & Config {KubernetesVersion : "2" },
212+ baseURL : "https://base" ,
213+ kind : "sample" ,
214+ version : "v1" ,
215+ expected : "https://base/v2-standalone/sample-v1.json" ,
216+ },
217+ {
218+ config : & Config {KubernetesVersion : "master" , Strict : true },
219+ baseURL : "https://base" ,
220+ kind : "sample" ,
221+ version : "v1" ,
222+ expected : "https://base/master-standalone-strict/sample-v1.json" ,
223+ },
224+ {
225+ config : NewDefaultConfig (),
226+ baseURL : "https://base" ,
227+ kind : "sample" ,
228+ version : "extensions/v1beta1" ,
229+ expected : "https://base/master-standalone/sample-extensions-v1beta1.json" ,
230+ },
231+ {
232+ config : & Config {KubernetesVersion : "master" , OpenShift : true },
233+ baseURL : "https://base" ,
234+ kind : "sample" ,
235+ version : "v1" ,
236+ expected : "https://base/master-standalone/sample.json" ,
237+ },
209238 }
210- }
211-
212- func TestDetermineSchemaForOpenShift (t * testing.T ) {
213- config := NewDefaultConfig ()
214- config .OpenShift = true
215- schema := determineSchema ("sample" , "v1" , config )
216- if schema != "https://raw.githubusercontent.com/garethr/openshift-json-schema/master/master-standalone/sample.json" {
217- t .Errorf ("Should be able to toggle to OpenShift schemas, instead %s" , schema )
239+ for _ , test := range tests {
240+ schemaURL := determineSchemaURL (test .baseURL , test .kind , test .version , test .config )
241+ if schemaURL != test .expected {
242+ t .Errorf ("Schema URL should be %s, got %s" , test .expected , schemaURL )
243+ }
218244 }
219245}
220246
221247func TestDetermineSchemaForSchemaLocation (t * testing.T ) {
222- config := NewDefaultConfig ()
223- config .SchemaLocation = "file:///home/me"
224- schema := determineSchema ("sample" , "v1" , config )
225- expectedSchema := "file:///home/me/master-standalone/sample-v1.json"
226- if schema != expectedSchema {
227- t .Errorf ("Should be able to specify a schema location, expected %s, got %s instead " , expectedSchema , schema )
228- }
229- }
230-
231- func TestDetermineSchemaForEnvVariable (t * testing.T ) {
232248 oldVal , found := os .LookupEnv ("KUBEVAL_SCHEMA_LOCATION" )
233249 defer func () {
234250 if found {
@@ -237,43 +253,70 @@ func TestDetermineSchemaForEnvVariable(t *testing.T) {
237253 os .Unsetenv ("KUBEVAL_SCHEMA_LOCATION" )
238254 }
239255 }()
240- config := NewDefaultConfig ()
241- os .Setenv ("KUBEVAL_SCHEMA_LOCATION" , "file:///home/me" )
242- schema := determineSchema ("sample" , "v1" , config )
243- expectedSchema := "file:///home/me/master-standalone/sample-v1.json"
244- if schema != expectedSchema {
245- t .Errorf ("Should be able to specify a schema location, expected %s, got %s instead " , expectedSchema , schema )
256+
257+ var tests = []struct {
258+ config * Config
259+ envVar string
260+ expected string
261+ }{
262+ {
263+ config : & Config {OpenShift : true },
264+ envVar : "" ,
265+ expected : OpenShiftSchemaLocation ,
266+ },
267+ {
268+ config : & Config {SchemaLocation : "https://base" },
269+ envVar : "" ,
270+ expected : "https://base" ,
271+ },
272+ {
273+ config : & Config {},
274+ envVar : "https://base" ,
275+ expected : "https://base" ,
276+ },
277+ {
278+ config : & Config {},
279+ envVar : "" ,
280+ expected : DefaultSchemaLocation ,
281+ },
282+ }
283+ for i , test := range tests {
284+ os .Setenv ("KUBEVAL_SCHEMA_LOCATION" , test .envVar )
285+ schemaBaseURL := determineSchemaBaseURL (test .config )
286+ if schemaBaseURL != test .expected {
287+ t .Errorf ("test #%d: Schema Base URL should be %s, got %s" , i , test .expected , schemaBaseURL )
288+ }
246289 }
247290}
248291
249292func TestGetString (t * testing.T ) {
250- var tests = []struct {
251- body map [string ]interface {}
252- key string
293+ var tests = []struct {
294+ body map [string ]interface {}
295+ key string
253296 expectedVal string
254297 expectError bool
255298 }{
256299 {
257- body : map [string ]interface {}{"goodKey" : "goodVal" },
258- key : "goodKey" ,
300+ body : map [string ]interface {}{"goodKey" : "goodVal" },
301+ key : "goodKey" ,
259302 expectedVal : "goodVal" ,
260303 expectError : false ,
261304 },
262305 {
263- body : map [string ]interface {}{},
264- key : "missingKey" ,
306+ body : map [string ]interface {}{},
307+ key : "missingKey" ,
265308 expectedVal : "" ,
266309 expectError : true ,
267310 },
268311 {
269- body : map [string ]interface {}{"nilKey" : nil },
270- key : "nilKey" ,
312+ body : map [string ]interface {}{"nilKey" : nil },
313+ key : "nilKey" ,
271314 expectedVal : "" ,
272315 expectError : true ,
273316 },
274317 {
275- body : map [string ]interface {}{"badKey" : 5 },
276- key : "badKey" ,
318+ body : map [string ]interface {}{"badKey" : 5 },
319+ key : "badKey" ,
277320 expectedVal : "" ,
278321 expectError : true ,
279322 },
@@ -322,6 +365,28 @@ func TestSkipCrdSchemaMiss(t *testing.T) {
322365 }
323366}
324367
368+ func TestAdditionalSchemas (t * testing.T ) {
369+ // This test uses a hack - first tell kubeval to use a bogus URL as its
370+ // primary search location, then give the DefaultSchemaLocation as an
371+ // additional schema.
372+ // This should cause kubeval to fail when looking for the schema in the
373+ // primary location, then succeed when it finds the schema at the
374+ // "additional location"
375+ config := NewDefaultConfig ()
376+ config .SchemaLocation = "testLocation"
377+ config .AdditionalSchemaLocations = []string {DefaultSchemaLocation }
378+
379+ config .FileName = "valid.yaml"
380+ filePath , _ := filepath .Abs ("../fixtures/valid.yaml" )
381+ fileContents , _ := ioutil .ReadFile (filePath )
382+ results , err := Validate (fileContents , config )
383+ if err != nil {
384+ t .Errorf ("Unexpected error: %s" , err .Error ())
385+ } else if len (results [0 ].Errors ) != 0 {
386+ t .Errorf ("Validate should pass when testing a valid configuration using additional schema" )
387+ }
388+ }
389+
325390func TestFlagAdding (t * testing.T ) {
326391 cmd := & cobra.Command {}
327392 config := & Config {}
@@ -336,6 +401,7 @@ func TestFlagAdding(t *testing.T) {
336401 "filename" ,
337402 "skip-kinds" ,
338403 "schema-location" ,
404+ "additional-schema-locations" ,
339405 "kubernetes-version" ,
340406 }
341407
0 commit comments