converter_test.go 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. Copyright 2018 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 converter
  14. import (
  15. "net/http"
  16. "net/http/httptest"
  17. "strings"
  18. "testing"
  19. "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
  20. "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
  22. "k8s.io/apimachinery/pkg/runtime"
  23. "k8s.io/apimachinery/pkg/runtime/serializer/json"
  24. )
  25. func TestConverter(t *testing.T) {
  26. sampleObj := `kind: ConversionReview
  27. apiVersion: apiextensions.k8s.io/v1beta1
  28. request:
  29. uid: 0000-0000-0000-0000
  30. desiredAPIVersion: stable.example.com/v2
  31. objects:
  32. - apiVersion: stable.example.com/v1
  33. kind: CronTab
  34. metadata:
  35. name: my-new-cron-object
  36. spec:
  37. cronSpec: "* * * * */5"
  38. image: my-awesome-cron-image
  39. hostPort: "localhost:7070"
  40. `
  41. // First try json, it should fail as the data is taml
  42. response := httptest.NewRecorder()
  43. request, err := http.NewRequest("POST", "/convert", strings.NewReader(sampleObj))
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. request.Header.Add("Content-Type", "application/json")
  48. ServeExampleConvert(response, request)
  49. convertReview := v1beta1.ConversionReview{}
  50. scheme := runtime.NewScheme()
  51. jsonSerializer := json.NewSerializer(json.DefaultMetaFactory, scheme, scheme, false)
  52. if _, _, err := jsonSerializer.Decode(response.Body.Bytes(), nil, &convertReview); err != nil {
  53. t.Fatal(err)
  54. }
  55. if convertReview.Response.Result.Status != v1.StatusFailure {
  56. t.Fatalf("expected the operation to fail when yaml is provided with json header")
  57. } else if !strings.Contains(convertReview.Response.Result.Message, "json parse error") {
  58. t.Fatalf("expected to fail on json parser, but it failed with: %v", convertReview.Response.Result.Message)
  59. }
  60. // Now try yaml, and it should successfully convert
  61. response = httptest.NewRecorder()
  62. request, err = http.NewRequest("POST", "/convert", strings.NewReader(sampleObj))
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. request.Header.Add("Content-Type", "application/yaml")
  67. ServeExampleConvert(response, request)
  68. convertReview = v1beta1.ConversionReview{}
  69. yamlSerializer := json.NewYAMLSerializer(json.DefaultMetaFactory, scheme, scheme)
  70. if _, _, err := yamlSerializer.Decode(response.Body.Bytes(), nil, &convertReview); err != nil {
  71. t.Fatalf("cannot decode data: \n %v\n Error: %v", response.Body, err)
  72. }
  73. if convertReview.Response.Result.Status != v1.StatusSuccess {
  74. t.Fatalf("cr conversion failed: %v", convertReview.Response)
  75. }
  76. convertedObj := unstructured.Unstructured{}
  77. if _, _, err := yamlSerializer.Decode(convertReview.Response.ConvertedObjects[0].Raw, nil, &convertedObj); err != nil {
  78. t.Fatal(err)
  79. }
  80. if e, a := "stable.example.com/v2", convertedObj.GetAPIVersion(); e != a {
  81. t.Errorf("expected= %v, actual= %v", e, a)
  82. }
  83. if e, a := "localhost", convertedObj.Object["host"]; e != a {
  84. t.Errorf("expected= %v, actual= %v", e, a)
  85. }
  86. if e, a := "7070", convertedObj.Object["port"]; e != a {
  87. t.Errorf("expected= %v, actual= %v", e, a)
  88. }
  89. }