master_openapi_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // +build !race
  2. /*
  3. Copyright 2014 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package master
  15. // This test file is separated from master_test.go so we would be able to disable
  16. // race check for it. TestValidOpenAPISpec will became extremely slow if -race
  17. // flag exists, and will cause the tests to timeout.
  18. import (
  19. "net/http"
  20. "net/http/httptest"
  21. "testing"
  22. "github.com/go-openapi/loads"
  23. "github.com/go-openapi/spec"
  24. "github.com/go-openapi/strfmt"
  25. "github.com/go-openapi/validate"
  26. openapinamer "k8s.io/apiserver/pkg/endpoints/openapi"
  27. genericapiserver "k8s.io/apiserver/pkg/server"
  28. "k8s.io/kubernetes/pkg/api/legacyscheme"
  29. openapigen "k8s.io/kubernetes/pkg/generated/openapi"
  30. )
  31. // TestValidOpenAPISpec verifies that the open api is added
  32. // at the proper endpoint and the spec is valid.
  33. func TestValidOpenAPISpec(t *testing.T) {
  34. etcdserver, config, assert := setUp(t)
  35. defer etcdserver.Terminate(t)
  36. config.GenericConfig.EnableIndex = true
  37. config.GenericConfig.OpenAPIConfig = genericapiserver.DefaultOpenAPIConfig(openapigen.GetOpenAPIDefinitions, openapinamer.NewDefinitionNamer(legacyscheme.Scheme))
  38. config.GenericConfig.OpenAPIConfig.Info = &spec.Info{
  39. InfoProps: spec.InfoProps{
  40. Title: "Kubernetes",
  41. Version: "unversioned",
  42. },
  43. }
  44. master, err := config.Complete().New(genericapiserver.NewEmptyDelegate())
  45. if err != nil {
  46. t.Fatalf("Error in bringing up the master: %v", err)
  47. }
  48. // make sure swagger.json is not registered before calling PrepareRun.
  49. server := httptest.NewServer(master.GenericAPIServer.Handler.Director)
  50. defer server.Close()
  51. resp, err := http.Get(server.URL + "/openapi/v2")
  52. if !assert.NoError(err) {
  53. t.Errorf("unexpected error: %v", err)
  54. }
  55. assert.Equal(http.StatusNotFound, resp.StatusCode)
  56. master.GenericAPIServer.PrepareRun()
  57. resp, err = http.Get(server.URL + "/openapi/v2")
  58. if !assert.NoError(err) {
  59. t.Errorf("unexpected error: %v", err)
  60. }
  61. assert.Equal(http.StatusOK, resp.StatusCode)
  62. // as json schema
  63. var sch spec.Schema
  64. if assert.NoError(decodeResponse(resp, &sch)) {
  65. validator := validate.NewSchemaValidator(spec.MustLoadSwagger20Schema(), nil, "", strfmt.Default)
  66. res := validator.Validate(&sch)
  67. assert.NoError(res.AsError())
  68. }
  69. // Validate OpenApi spec
  70. doc, err := loads.Spec(server.URL + "/openapi/v2")
  71. if assert.NoError(err) {
  72. validator := validate.NewSpecValidator(doc.Schema(), strfmt.Default)
  73. res, warns := validator.Validate(doc)
  74. assert.NoError(res.AsError())
  75. if !warns.IsValid() {
  76. t.Logf("Open API spec on root has some warnings : %v", warns)
  77. }
  78. }
  79. }