convert_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. Copyright 2017 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 convert
  14. import (
  15. "bytes"
  16. "fmt"
  17. "net/http"
  18. "strings"
  19. "testing"
  20. "k8s.io/cli-runtime/pkg/genericclioptions"
  21. "k8s.io/client-go/rest/fake"
  22. cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
  23. )
  24. type testcase struct {
  25. name string
  26. file string
  27. outputVersion string
  28. fields []checkField
  29. }
  30. type checkField struct {
  31. expected string
  32. }
  33. func TestConvertObject(t *testing.T) {
  34. testcases := []testcase{
  35. {
  36. name: "apps deployment to extensions deployment",
  37. file: "../../../../test/fixtures/pkg/kubectl/cmd/convert/appsdeployment.yaml",
  38. outputVersion: "extensions/v1beta1",
  39. fields: []checkField{
  40. {
  41. expected: "apiVersion: extensions/v1beta1",
  42. },
  43. },
  44. },
  45. {
  46. name: "extensions deployment to apps deployment",
  47. file: "../../../../test/fixtures/pkg/kubectl/cmd/convert/extensionsdeployment.yaml",
  48. outputVersion: "apps/v1beta2",
  49. fields: []checkField{
  50. {
  51. expected: "apiVersion: apps/v1beta2",
  52. },
  53. },
  54. },
  55. {
  56. name: "v1 HPA to v2beta1 HPA",
  57. file: "../../../../test/fixtures/pkg/kubectl/cmd/convert/v1HPA.yaml",
  58. outputVersion: "autoscaling/v2beta1",
  59. fields: []checkField{
  60. {
  61. expected: "apiVersion: autoscaling/v2beta1",
  62. },
  63. {
  64. expected: "name: cpu",
  65. },
  66. {
  67. expected: "targetAverageUtilization: 50",
  68. },
  69. },
  70. },
  71. {
  72. name: "v2beta1 HPA to v1 HPA",
  73. file: "../../../../test/fixtures/pkg/kubectl/cmd/convert/v2beta1HPA.yaml",
  74. outputVersion: "autoscaling/v1",
  75. fields: []checkField{
  76. {
  77. expected: "apiVersion: autoscaling/v1",
  78. },
  79. {
  80. expected: "targetCPUUtilizationPercentage: 50",
  81. },
  82. },
  83. },
  84. }
  85. for _, tc := range testcases {
  86. for _, field := range tc.fields {
  87. t.Run(fmt.Sprintf("%s %s", tc.name, field), func(t *testing.T) {
  88. tf := cmdtesting.NewTestFactory().WithNamespace("test")
  89. defer tf.Cleanup()
  90. tf.UnstructuredClient = &fake.RESTClient{
  91. Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
  92. t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
  93. return nil, nil
  94. }),
  95. }
  96. buf := bytes.NewBuffer([]byte{})
  97. cmd := NewCmdConvert(tf, genericclioptions.IOStreams{Out: buf, ErrOut: buf})
  98. cmd.Flags().Set("filename", tc.file)
  99. cmd.Flags().Set("output-version", tc.outputVersion)
  100. cmd.Flags().Set("local", "true")
  101. cmd.Flags().Set("output", "yaml")
  102. cmd.Run(cmd, []string{})
  103. if !strings.Contains(buf.String(), field.expected) {
  104. t.Errorf("unexpected output when converting %s to %q, expected: %q, but got %q", tc.file, tc.outputVersion, field.expected, buf.String())
  105. }
  106. })
  107. }
  108. }
  109. }