github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/internal/util/addmergecomment/addmergecomment_test.go (about) 1 // Copyright 2021 Google LLC 2 // 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 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package addmergecomment 16 17 import ( 18 "os" 19 "strings" 20 "testing" 21 22 "github.com/stretchr/testify/assert" 23 ) 24 25 func TestAddMetadataComment(t *testing.T) { 26 var tests = []struct { 27 name string 28 input string 29 expected string 30 errMsg string 31 }{ 32 { 33 name: "Add kpt merge annotation with name and namespace", 34 input: ` 35 apiVersion: apps/v1 36 kind: Deployment 37 metadata: 38 name: nginx-deployment 39 namespace: my-space 40 spec: 41 replicas: 3 42 `, 43 expected: ` 44 apiVersion: apps/v1 45 kind: Deployment 46 metadata: # kpt-merge: my-space/nginx-deployment 47 name: nginx-deployment 48 namespace: my-space 49 annotations: 50 internal.kpt.dev/upstream-identifier: 'apps|Deployment|my-space|nginx-deployment' 51 spec: 52 replicas: 3 53 `, 54 }, 55 { 56 name: "Add kpt merge comment with name and no namespace", 57 input: ` 58 apiVersion: apps/v1 59 kind: Deployment 60 metadata: 61 name: nginx-deployment 62 namespace: default 63 spec: 64 replicas: 3 65 `, 66 expected: ` 67 apiVersion: apps/v1 68 kind: Deployment 69 metadata: # kpt-merge: default/nginx-deployment 70 name: nginx-deployment 71 namespace: default 72 annotations: 73 internal.kpt.dev/upstream-identifier: 'apps|Deployment|default|nginx-deployment' 74 spec: 75 replicas: 3 76 `, 77 }, 78 { 79 name: "Add kpt merge comment with name and no namespace", 80 input: ` 81 apiVersion: apps/v1 82 kind: Deployment 83 metadata: 84 name: nginx-deployment 85 spec: 86 replicas: 3 87 `, 88 expected: ` 89 apiVersion: apps/v1 90 kind: Deployment 91 metadata: # kpt-merge: /nginx-deployment 92 name: nginx-deployment 93 annotations: 94 internal.kpt.dev/upstream-identifier: 'apps|Deployment|default|nginx-deployment' 95 spec: 96 replicas: 3 97 `, 98 }, 99 { 100 name: "Skip adding kpt merge comment if already present", 101 input: ` 102 apiVersion: apps/v1 103 kind: Deployment 104 metadata: # kpt-merge: my-space/nginx-deployment 105 name: nginx-deployment-new 106 spec: 107 replicas: 3 108 `, 109 expected: ` 110 apiVersion: apps/v1 111 kind: Deployment 112 metadata: # kpt-merge: my-space/nginx-deployment 113 name: nginx-deployment-new 114 annotations: 115 internal.kpt.dev/upstream-identifier: 'apps|Deployment|my-space|nginx-deployment' 116 spec: 117 replicas: 3 118 `, 119 }, 120 { 121 name: "Skip adding kpt merge comment if skip meta", 122 input: ` 123 apiVersion: apps/v1 124 kind: MyKind 125 spec: 126 replicas: 3 127 `, 128 expected: ` 129 apiVersion: apps/v1 130 kind: MyKind 131 spec: 132 replicas: 3 133 `, 134 }, 135 { 136 name: "Skip adding kpt merge comment if non-KRM resource", 137 input: `- op: replace 138 path: /spec 139 value: 140 group: kubeflow.org 141 `, 142 expected: `- op: replace 143 path: /spec 144 value: 145 group: kubeflow.org 146 `, 147 }, 148 { 149 name: "Preserve label comments and orders during upstream-id label editing", 150 input: ` 151 apiVersion: v1 152 kind: Namespace 153 metadata: # kpt-merge: /some-app 154 name: some-app # kpt-set: ${some-app} 155 annotations: 156 a-custom: value1 157 b-custom: value2 158 ncp/static_snat_ip: 122.122.122.122 # kpt-set: ${gateway-snat-ip} 159 160 spec: 161 replicas: 3 # kpt-set: ${other} 162 `, 163 expected: ` 164 apiVersion: v1 165 kind: Namespace 166 metadata: # kpt-merge: /some-app 167 name: some-app # kpt-set: ${some-app} 168 annotations: 169 a-custom: value1 170 b-custom: value2 171 ncp/static_snat_ip: 122.122.122.122 # kpt-set: ${gateway-snat-ip} 172 internal.kpt.dev/upstream-identifier: '|Namespace|default|some-app' 173 spec: 174 replicas: 3 # kpt-set: ${other} 175 `, 176 }, 177 } 178 for i := range tests { 179 test := tests[i] 180 t.Run(test.name, func(t *testing.T) { 181 baseDir := t.TempDir() 182 183 r, err := os.CreateTemp(baseDir, "k8s-cli-*.yaml") 184 if !assert.NoError(t, err) { 185 t.FailNow() 186 } 187 err = os.WriteFile(r.Name(), []byte(test.input), 0600) 188 if !assert.NoError(t, err) { 189 t.FailNow() 190 } 191 192 err = Process(baseDir) 193 if test.errMsg != "" { 194 if !assert.NotNil(t, err) { 195 t.FailNow() 196 } 197 if !assert.Contains(t, err.Error(), test.errMsg) { 198 t.FailNow() 199 } 200 } 201 202 if test.errMsg == "" && !assert.NoError(t, err) { 203 t.FailNow() 204 } 205 206 actualResources, err := os.ReadFile(r.Name()) 207 if !assert.NoError(t, err) { 208 t.FailNow() 209 } 210 if !assert.Equal(t, 211 strings.TrimSpace(test.expected), 212 strings.TrimSpace(string(actualResources))) { 213 t.FailNow() 214 } 215 }) 216 } 217 }