github.com/jbking/gohan@v0.0.0-20151217002006-b41ccf1c2a96/schema/schema_test.go (about) 1 // Copyright (C) 2015 NTT Innovation Institute, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 12 // implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 package schema 17 18 import ( 19 "fmt" 20 21 . "github.com/onsi/ginkgo" 22 . "github.com/onsi/gomega" 23 ) 24 25 var _ = Describe("Schema", func() { 26 Describe("Schema manager", func() { 27 It("should reorder schemas if it is DAG", func() { 28 manager := GetManager() 29 schemaPath := "../tests/test_schema_dag_dependency.yaml" 30 Expect(manager.LoadSchemaFromFile(schemaPath)).To(Succeed()) 31 Expect(manager.schemaOrder).To(Equal( 32 []string{"red_resource", 33 "blue_resource", 34 "green_resource", 35 "orange_resource"})) 36 }) 37 38 It("should read schemas even if it isn't DAG", func() { 39 manager := GetManager() 40 schemaPath := "../tests/test_schema_non_dag_dependency.yaml" 41 Expect(manager.LoadSchemaFromFile(schemaPath)).To(Succeed()) 42 }) 43 44 AfterEach(func() { 45 ClearManager() 46 }) 47 }) 48 49 Describe("Formatters", func() { 50 var netSchema *Schema 51 52 BeforeEach(func() { 53 var exists bool 54 manager := GetManager() 55 schemaPath := "../tests/test_schema.json" 56 Expect(manager.LoadSchemaFromFile(schemaPath)).To(Succeed()) 57 netSchema, exists = manager.Schema("net") 58 Expect(exists).To(BeTrue()) 59 }) 60 61 AfterEach(func() { 62 ClearManager() 63 }) 64 65 It("CIDR", func() { 66 netMap := map[string]interface{}{"cidr": "asdf"} 67 Expect(netSchema.ValidateOnCreate(netMap)).To(MatchError(getErrorMessage("cidr", "cidr"))) 68 69 netMap = map[string]interface{}{"cidr": "10.10.10.10/24"} 70 Expect(netSchema.ValidateOnCreate(netMap)).To(Succeed()) 71 72 netMap = map[string]interface{}{"cidr": "127.0.0.1"} 73 Expect(netSchema.ValidateOnCreate(netMap)).To(MatchError(getErrorMessage("cidr", "cidr"))) 74 }) 75 76 It("MAC", func() { 77 netMap := map[string]interface{}{"mac": "aa:bb:cc:dd:ee"} 78 Expect(netSchema.ValidateOnCreate(netMap)).To(MatchError(getErrorMessage("mac", "mac"))) 79 80 netMap = map[string]interface{}{"mac": "aa-aa-aa-aa-aa-aa"} 81 Expect(netSchema.ValidateOnCreate(netMap)).To(MatchError(getErrorMessage("mac", "mac"))) 82 83 netMap = map[string]interface{}{"mac": "aa:bb:cc:dd:ee:ff"} 84 Expect(netSchema.ValidateOnCreate(netMap)).To(Succeed()) 85 86 netMap = map[string]interface{}{"mac": "11:22:33:DD:1e:FF"} 87 Expect(netSchema.ValidateOnCreate(netMap)).To(Succeed()) 88 }) 89 90 It("UUID", func() { 91 netMap := map[string]interface{}{"id": "wrong-id"} 92 Expect(netSchema.ValidateOnCreate(netMap)).To(MatchError(getErrorMessage("id", "uuid"))) 93 94 netMap = map[string]interface{}{"id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"} 95 Expect(netSchema.ValidateOnCreate(netMap)).To(MatchError(getErrorMessage("id", "uuid"))) 96 97 netMap = map[string]interface{}{"id": "de305d54-75b4-431b-adb2-eb6b9e546014"} 98 Expect(netSchema.ValidateOnCreate(netMap)).To(Succeed()) 99 100 netMap = map[string]interface{}{"id": "de305d5475b4431badb2eb6b9e546014"} 101 Expect(netSchema.ValidateOnCreate(netMap)).To(Succeed()) 102 }) 103 104 It("Port", func() { 105 netMap := map[string]interface{}{"port": "wrong-port"} 106 Expect(netSchema.ValidateOnCreate(netMap)).To(MatchError(getErrorMessage("port", "port"))) 107 108 netMap = map[string]interface{}{"port": "-1"} 109 Expect(netSchema.ValidateOnCreate(netMap)).To(MatchError(getErrorMessage("port", "port"))) 110 111 netMap = map[string]interface{}{"port": "0"} 112 Expect(netSchema.ValidateOnCreate(netMap)).To(MatchError(getErrorMessage("port", "port"))) 113 114 netMap = map[string]interface{}{"port": "65536"} 115 Expect(netSchema.ValidateOnCreate(netMap)).To(MatchError(getErrorMessage("port", "port"))) 116 117 netMap = map[string]interface{}{"port": "42"} 118 Expect(netSchema.ValidateOnCreate(netMap)).To(Succeed()) 119 }) 120 121 It("Regex", func() { 122 netMap := map[string]interface{}{"regex": "[[[{{{"} 123 Expect(netSchema.ValidateOnCreate(netMap)).To(MatchError(getErrorMessage("regex", "regex"))) 124 125 netMap = map[string]interface{}{"regex": "[a-z0-7]{3}.*[,.;']{1,2}"} 126 Expect(netSchema.ValidateOnCreate(netMap)).To(Succeed()) 127 }) 128 }) 129 }) 130 131 func getErrorMessage(fieldName string, formatterName string) string { 132 return fmt.Sprintf("Json validation error:\n\t%s: Does not match format '%s',", fieldName, formatterName) 133 }