k8s.io/kubernetes@v1.29.3/pkg/kubectl/cmd/convert/convert_test.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors. 3 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 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package convert 18 19 import ( 20 "bytes" 21 "fmt" 22 "net/http" 23 "strings" 24 "testing" 25 26 "k8s.io/cli-runtime/pkg/genericiooptions" 27 "k8s.io/client-go/rest/fake" 28 cmdtesting "k8s.io/kubectl/pkg/cmd/testing" 29 ) 30 31 type testcase struct { 32 name string 33 file string 34 outputVersion string 35 fields []checkField 36 } 37 38 type checkField struct { 39 expected string 40 } 41 42 func TestConvertObject(t *testing.T) { 43 testcases := []testcase{ 44 { 45 name: "apps deployment to extensions deployment", 46 file: "../../../../test/fixtures/pkg/kubectl/cmd/convert/appsdeployment.yaml", 47 outputVersion: "extensions/v1beta1", 48 fields: []checkField{ 49 { 50 expected: "apiVersion: extensions/v1beta1", 51 }, 52 }, 53 }, 54 { 55 name: "extensions deployment to apps deployment", 56 file: "../../../../test/fixtures/pkg/kubectl/cmd/convert/extensionsdeployment.yaml", 57 outputVersion: "apps/v1beta2", 58 fields: []checkField{ 59 { 60 expected: "apiVersion: apps/v1beta2", 61 }, 62 }, 63 }, 64 { 65 name: "v1 HPA to v2beta1 HPA", 66 file: "../../../../test/fixtures/pkg/kubectl/cmd/convert/v1HPA.yaml", 67 outputVersion: "autoscaling/v2beta1", 68 fields: []checkField{ 69 { 70 expected: "apiVersion: autoscaling/v2beta1", 71 }, 72 { 73 expected: "name: cpu", 74 }, 75 { 76 expected: "targetAverageUtilization: 50", 77 }, 78 }, 79 }, 80 { 81 name: "v2beta1 HPA to v1 HPA", 82 file: "../../../../test/fixtures/pkg/kubectl/cmd/convert/v2beta1HPA.yaml", 83 outputVersion: "autoscaling/v1", 84 fields: []checkField{ 85 { 86 expected: "apiVersion: autoscaling/v1", 87 }, 88 { 89 expected: "targetCPUUtilizationPercentage: 50", 90 }, 91 }, 92 }, 93 { 94 name: "v1beta1 Ingress to extensions Ingress", 95 file: "../../../../test/fixtures/pkg/kubectl/cmd/convert/v1beta1ingress.yaml", 96 outputVersion: "extensions/v1beta1", 97 fields: []checkField{ 98 { 99 expected: "apiVersion: extensions/v1beta1", 100 }, 101 }, 102 }, 103 { 104 name: "converting multiple including service to neworking.k8s.io/v1", 105 file: "../../../../test/fixtures/pkg/kubectl/cmd/convert/serviceandingress.yaml", 106 outputVersion: "networking.k8s.io/v1", 107 fields: []checkField{ 108 { 109 expected: "apiVersion: networking.k8s.io/v1", 110 }, 111 }, 112 }, 113 } 114 115 for _, tc := range testcases { 116 for _, field := range tc.fields { 117 t.Run(fmt.Sprintf("%s %s", tc.name, field), func(t *testing.T) { 118 tf := cmdtesting.NewTestFactory().WithNamespace("test") 119 defer tf.Cleanup() 120 121 tf.UnstructuredClient = &fake.RESTClient{ 122 Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { 123 t.Fatalf("unexpected request: %#v\n%#v", req.URL, req) 124 return nil, nil 125 }), 126 } 127 128 buf := bytes.NewBuffer([]byte{}) 129 cmd := NewCmdConvert(tf, genericiooptions.IOStreams{Out: buf, ErrOut: buf}) 130 cmd.Flags().Set("filename", tc.file) 131 cmd.Flags().Set("output-version", tc.outputVersion) 132 cmd.Flags().Set("local", "true") 133 cmd.Flags().Set("output", "yaml") 134 cmd.Run(cmd, []string{}) 135 if !strings.Contains(buf.String(), field.expected) { 136 t.Errorf("unexpected output when converting %s to %q, expected: %q, but got %q", tc.file, tc.outputVersion, field.expected, buf.String()) 137 } 138 }) 139 } 140 } 141 }