schema_test.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package validation
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "io/ioutil"
  18. "math/rand"
  19. "strings"
  20. "testing"
  21. "k8s.io/kubernetes/pkg/api"
  22. "k8s.io/kubernetes/pkg/api/testapi"
  23. apitesting "k8s.io/kubernetes/pkg/api/testing"
  24. "k8s.io/kubernetes/pkg/apis/extensions"
  25. "k8s.io/kubernetes/pkg/runtime"
  26. k8syaml "k8s.io/kubernetes/pkg/util/yaml"
  27. "github.com/ghodss/yaml"
  28. )
  29. func readPod(filename string) ([]byte, error) {
  30. data, err := ioutil.ReadFile("testdata/" + testapi.Default.GroupVersion().Version + "/" + filename)
  31. if err != nil {
  32. return nil, err
  33. }
  34. return data, nil
  35. }
  36. func readSwaggerFile() ([]byte, error) {
  37. return readSwaggerApiFile(testapi.Default)
  38. }
  39. func readSwaggerApiFile(group testapi.TestGroup) ([]byte, error) {
  40. // TODO: Figure out a better way of finding these files
  41. var pathToSwaggerSpec string
  42. if group.GroupVersion().Group == "" {
  43. pathToSwaggerSpec = "../../../api/swagger-spec/" + group.GroupVersion().Version + ".json"
  44. } else {
  45. pathToSwaggerSpec = "../../../api/swagger-spec/" + group.GroupVersion().Group + "_" + group.GroupVersion().Version + ".json"
  46. }
  47. return ioutil.ReadFile(pathToSwaggerSpec)
  48. }
  49. // Mock delegating Schema. Not a full fake impl.
  50. type Factory struct {
  51. defaultSchema Schema
  52. extensionsSchema Schema
  53. }
  54. var _ Schema = &Factory{}
  55. // TODO: Consider using a mocking library instead or fully fleshing this out into a fake impl and putting it in some
  56. // generally available location
  57. func (f *Factory) ValidateBytes(data []byte) error {
  58. var obj interface{}
  59. out, err := k8syaml.ToJSON(data)
  60. if err != nil {
  61. return err
  62. }
  63. data = out
  64. if err := json.Unmarshal(data, &obj); err != nil {
  65. return err
  66. }
  67. fields, ok := obj.(map[string]interface{})
  68. if !ok {
  69. return fmt.Errorf("error in unmarshaling data %s", string(data))
  70. }
  71. // Note: This only supports the 2 api versions we expect from the test it is currently supporting.
  72. groupVersion := fields["apiVersion"]
  73. switch groupVersion {
  74. case "v1":
  75. return f.defaultSchema.ValidateBytes(data)
  76. case "extensions/v1beta1":
  77. return f.extensionsSchema.ValidateBytes(data)
  78. default:
  79. return fmt.Errorf("Unsupported API version %s", groupVersion)
  80. }
  81. }
  82. func loadSchemaForTest() (Schema, error) {
  83. data, err := readSwaggerFile()
  84. if err != nil {
  85. return nil, err
  86. }
  87. return NewSwaggerSchemaFromBytes(data, nil)
  88. }
  89. func loadSchemaForTestWithFactory(group testapi.TestGroup, factory Schema) (Schema, error) {
  90. data, err := readSwaggerApiFile(group)
  91. if err != nil {
  92. return nil, err
  93. }
  94. return NewSwaggerSchemaFromBytes(data, factory)
  95. }
  96. func NewFactory() (*Factory, error) {
  97. f := &Factory{}
  98. defaultSchema, err := loadSchemaForTestWithFactory(testapi.Default, f)
  99. if err != nil {
  100. return nil, err
  101. }
  102. f.defaultSchema = defaultSchema
  103. extensionSchema, err := loadSchemaForTestWithFactory(testapi.Extensions, f)
  104. if err != nil {
  105. return nil, err
  106. }
  107. f.extensionsSchema = extensionSchema
  108. return f, nil
  109. }
  110. func TestLoad(t *testing.T) {
  111. _, err := loadSchemaForTest()
  112. if err != nil {
  113. t.Errorf("Failed to load: %v", err)
  114. }
  115. }
  116. func TestValidateOk(t *testing.T) {
  117. schema, err := loadSchemaForTest()
  118. if err != nil {
  119. t.Fatalf("Failed to load: %v", err)
  120. }
  121. tests := []struct {
  122. obj runtime.Object
  123. typeName string
  124. }{
  125. {obj: &api.Pod{}},
  126. {obj: &api.Service{}},
  127. {obj: &api.ReplicationController{}},
  128. }
  129. seed := rand.Int63()
  130. apiObjectFuzzer := apitesting.FuzzerFor(nil, testapi.Default.InternalGroupVersion(), rand.NewSource(seed))
  131. for i := 0; i < 5; i++ {
  132. for _, test := range tests {
  133. testObj := test.obj
  134. apiObjectFuzzer.Fuzz(testObj)
  135. data, err := runtime.Encode(testapi.Default.Codec(), testObj)
  136. if err != nil {
  137. t.Errorf("unexpected error: %v", err)
  138. }
  139. err = schema.ValidateBytes(data)
  140. if err != nil {
  141. t.Errorf("unexpected error: %v", err)
  142. }
  143. }
  144. }
  145. }
  146. func TestValidateDifferentApiVersions(t *testing.T) {
  147. schema, err := loadSchemaForTest()
  148. if err != nil {
  149. t.Fatalf("Failed to load: %v", err)
  150. }
  151. pod := &api.Pod{}
  152. pod.APIVersion = "v1"
  153. pod.Kind = "Pod"
  154. deployment := &extensions.Deployment{}
  155. deployment.APIVersion = "extensions/v1beta1"
  156. deployment.Kind = "Deployment"
  157. list := &api.List{}
  158. list.APIVersion = "v1"
  159. list.Kind = "List"
  160. list.Items = []runtime.Object{pod, deployment}
  161. bytes, err := json.Marshal(list)
  162. if err != nil {
  163. t.Error(err)
  164. }
  165. err = schema.ValidateBytes(bytes)
  166. if err == nil {
  167. t.Error(fmt.Errorf("Expected error when validating different api version and no delegate exists."))
  168. }
  169. f, err := NewFactory()
  170. if err != nil {
  171. t.Error(fmt.Errorf("Failed to create Schema factory %v.", err))
  172. }
  173. err = f.ValidateBytes(bytes)
  174. if err != nil {
  175. t.Error(fmt.Errorf("Failed to validate object with multiple ApiGroups: %v.", err))
  176. }
  177. }
  178. func TestInvalid(t *testing.T) {
  179. schema, err := loadSchemaForTest()
  180. if err != nil {
  181. t.Fatalf("Failed to load: %v", err)
  182. }
  183. tests := []string{
  184. "invalidPod1.json", // command is a string, instead of []string.
  185. "invalidPod2.json", // hostPort if of type string, instead of int.
  186. "invalidPod3.json", // volumes is not an array of objects.
  187. "invalidPod4.yaml", // string list with empty string.
  188. "invalidPod.yaml", // command is a string, instead of []string.
  189. }
  190. for _, test := range tests {
  191. pod, err := readPod(test)
  192. if err != nil {
  193. t.Errorf("could not read file: %s, err: %v", test, err)
  194. }
  195. err = schema.ValidateBytes(pod)
  196. if err == nil {
  197. t.Errorf("unexpected non-error, err: %s for pod: %s", err, pod)
  198. }
  199. }
  200. }
  201. func TestValid(t *testing.T) {
  202. schema, err := loadSchemaForTest()
  203. if err != nil {
  204. t.Fatalf("Failed to load: %v", err)
  205. }
  206. tests := []string{
  207. "validPod.yaml",
  208. }
  209. for _, test := range tests {
  210. pod, err := readPod(test)
  211. if err != nil {
  212. t.Errorf("could not read file: %s, err: %v", test, err)
  213. }
  214. err = schema.ValidateBytes(pod)
  215. if err != nil {
  216. t.Errorf("unexpected error: %s, for pod %s", err, pod)
  217. }
  218. }
  219. }
  220. func TestVersionRegex(t *testing.T) {
  221. testCases := []struct {
  222. typeName string
  223. match bool
  224. }{
  225. {
  226. typeName: "v1.Binding",
  227. match: true,
  228. },
  229. {
  230. typeName: "v1beta1.Binding",
  231. match: true,
  232. },
  233. {
  234. typeName: "Binding",
  235. match: false,
  236. },
  237. }
  238. for _, test := range testCases {
  239. if versionRegexp.MatchString(test.typeName) && !test.match {
  240. t.Errorf("unexpected error: expect %s not to match the regular expression", test.typeName)
  241. }
  242. if !versionRegexp.MatchString(test.typeName) && test.match {
  243. t.Errorf("unexpected error: expect %s to match the regular expression", test.typeName)
  244. }
  245. }
  246. }
  247. // Tests that validation works fine when spec contains "type": "any" instead of "type": "object"
  248. // Ref: https://github.com/kubernetes/kubernetes/issues/24309
  249. func TestTypeAny(t *testing.T) {
  250. data, err := readSwaggerFile()
  251. if err != nil {
  252. t.Errorf("failed to read swagger file: %v", err)
  253. }
  254. // Replace type: "any" in the spec by type: "object" and verify that the validation still passes.
  255. newData := strings.Replace(string(data), `"type": "object"`, `"type": "any"`, -1)
  256. schema, err := NewSwaggerSchemaFromBytes([]byte(newData), nil)
  257. if err != nil {
  258. t.Fatalf("Failed to load: %v", err)
  259. }
  260. tests := []string{
  261. "validPod.yaml",
  262. }
  263. for _, test := range tests {
  264. podBytes, err := readPod(test)
  265. if err != nil {
  266. t.Errorf("could not read file: %s, err: %v", test, err)
  267. }
  268. // Verify that pod has at least one label (labels are type "any")
  269. var pod api.Pod
  270. err = yaml.Unmarshal(podBytes, &pod)
  271. if err != nil {
  272. t.Errorf("error in unmarshalling pod: %v", err)
  273. }
  274. if len(pod.Labels) == 0 {
  275. t.Errorf("invalid test input: the pod should have at least one label")
  276. }
  277. err = schema.ValidateBytes(podBytes)
  278. if err != nil {
  279. t.Errorf("unexpected error: %s, for pod %s", err, string(podBytes))
  280. }
  281. }
  282. }