github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/internal/util/attribution/attribution_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 attribution 16 17 import ( 18 "os" 19 "strings" 20 "testing" 21 22 "github.com/stretchr/testify/assert" 23 ) 24 25 func TestUsageProcess(t *testing.T) { 26 var tests = []struct { 27 name string 28 input string 29 group string 30 disable bool 31 expected string 32 errMsg string 33 }{ 34 { 35 name: "Don't add to non-cnrm resource", 36 input: ` 37 apiVersion: apps/v1 38 kind: Deployment 39 metadata: 40 name: nginx-deployment 41 namespace: my-space 42 spec: 43 replicas: 3 44 `, 45 expected: ` 46 apiVersion: apps/v1 47 kind: Deployment 48 metadata: 49 name: nginx-deployment 50 namespace: my-space 51 spec: 52 replicas: 3 53 `, 54 }, 55 { 56 name: "Create metrics annotation", 57 input: ` 58 apiVersion: compute.cnrm.cloud.google.com/v1beta1 59 kind: ComputeSubnetwork 60 metadata: 61 name: network-name-subnetwork 62 `, 63 group: "pkg", 64 expected: `apiVersion: compute.cnrm.cloud.google.com/v1beta1 65 kind: ComputeSubnetwork 66 metadata: 67 name: network-name-subnetwork 68 annotations: 69 cnrm.cloud.google.com/blueprint: 'kpt-pkg' 70 `, 71 }, 72 { 73 name: "Create metrics annotation", 74 input: ` 75 apiVersion: compute.cnrm.cloud.google.com/v1beta1 76 kind: ComputeSubnetwork 77 metadata: 78 name: network-name-subnetwork 79 `, 80 group: "pkg", 81 disable: true, 82 expected: `apiVersion: compute.cnrm.cloud.google.com/v1beta1 83 kind: ComputeSubnetwork 84 metadata: 85 name: network-name-subnetwork 86 `, 87 }, 88 { 89 name: "Add new group to existing metrics annotation", 90 input: ` 91 apiVersion: compute.cnrm.cloud.google.com/v1beta1 92 kind: ComputeSubnetwork 93 metadata: 94 name: network-name-subnetwork 95 annotations: 96 cnrm.cloud.google.com/blueprint: 'kpt-pkg' 97 `, 98 group: "fn", 99 expected: `apiVersion: compute.cnrm.cloud.google.com/v1beta1 100 kind: ComputeSubnetwork 101 metadata: 102 name: network-name-subnetwork 103 annotations: 104 cnrm.cloud.google.com/blueprint: 'kpt-pkg-fn' 105 `, 106 }, 107 { 108 name: "Add new group to existing metrics annotation 2", 109 input: ` 110 apiVersion: compute.cnrm.cloud.google.com/v1beta1 111 kind: ComputeSubnetwork 112 metadata: 113 name: network-name-subnetwork 114 annotations: 115 cnrm.cloud.google.com/blueprint: 'kpt-pkg-fn' 116 `, 117 group: "live", 118 expected: `apiVersion: compute.cnrm.cloud.google.com/v1beta1 119 kind: ComputeSubnetwork 120 metadata: 121 name: network-name-subnetwork 122 annotations: 123 cnrm.cloud.google.com/blueprint: 'kpt-pkg-fn-live' 124 `, 125 }, 126 { 127 name: "no-op if group is already present", 128 input: ` 129 apiVersion: compute.cnrm.cloud.google.com/v1beta1 130 kind: ComputeSubnetwork 131 metadata: 132 name: network-name-subnetwork 133 annotations: 134 cnrm.cloud.google.com/blueprint: 'kpt-pkg-fn-live' 135 `, 136 group: "fn", 137 expected: `apiVersion: compute.cnrm.cloud.google.com/v1beta1 138 kind: ComputeSubnetwork 139 metadata: 140 name: network-name-subnetwork 141 annotations: 142 cnrm.cloud.google.com/blueprint: 'kpt-pkg-fn-live' 143 `, 144 }, 145 { 146 name: "add kpt prefix to existing annotation", 147 input: ` 148 apiVersion: compute.cnrm.cloud.google.com/v1beta1 149 kind: ComputeSubnetwork 150 metadata: 151 name: network-name-subnetwork 152 annotations: 153 cnrm.cloud.google.com/blueprint: cnrm/landing-zone:networking/v0.4.0 154 `, 155 group: "fn", 156 expected: `apiVersion: compute.cnrm.cloud.google.com/v1beta1 157 kind: ComputeSubnetwork 158 metadata: 159 name: network-name-subnetwork 160 annotations: 161 cnrm.cloud.google.com/blueprint: cnrm/landing-zone:networking/v0.4.0,kpt-fn 162 `, 163 }, 164 { 165 name: "add group for existing annotation and existing kpt suffix", 166 input: ` 167 apiVersion: compute.cnrm.cloud.google.com/v1beta1 168 kind: ComputeSubnetwork 169 metadata: 170 name: network-name-subnetwork 171 annotations: 172 cnrm.cloud.google.com/blueprint: cnrm/landing-zone:networking/v0.4.0,kpt-fn 173 `, 174 group: "pkg", 175 expected: `apiVersion: compute.cnrm.cloud.google.com/v1beta1 176 kind: ComputeSubnetwork 177 metadata: 178 name: network-name-subnetwork 179 annotations: 180 cnrm.cloud.google.com/blueprint: cnrm/landing-zone:networking/v0.4.0,kpt-pkg-fn 181 `, 182 }, 183 { 184 name: "add group for existing annotation and existing kpt substring", 185 input: ` 186 apiVersion: compute.cnrm.cloud.google.com/v1beta1 187 kind: ComputeSubnetwork 188 metadata: 189 name: network-name-subnetwork 190 annotations: 191 cnrm.cloud.google.com/blueprint: cnrm/landing-zone:networking/v0.4.0,kpt-fn,blueprints_controller 192 `, 193 group: "pkg", 194 expected: `apiVersion: compute.cnrm.cloud.google.com/v1beta1 195 kind: ComputeSubnetwork 196 metadata: 197 name: network-name-subnetwork 198 annotations: 199 cnrm.cloud.google.com/blueprint: cnrm/landing-zone:networking/v0.4.0,kpt-pkg-fn,blueprints_controller 200 `, 201 }, 202 } 203 for i := range tests { 204 test := tests[i] 205 t.Run(test.name, func(t *testing.T) { 206 baseDir := t.TempDir() 207 208 r, err := os.CreateTemp(baseDir, "k8s-cli-*.yaml") 209 if !assert.NoError(t, err) { 210 t.FailNow() 211 } 212 err = os.WriteFile(r.Name(), []byte(test.input), 0600) 213 if !assert.NoError(t, err) { 214 t.FailNow() 215 } 216 217 if test.disable { 218 err = os.Setenv("KPT_DISABLE_ATTRIBUTION", "true") 219 if !assert.NoError(t, err) { 220 t.FailNow() 221 } 222 defer os.Setenv("KPT_DISABLE_ATTRIBUTION", "") 223 } 224 225 a := Attributor{PackagePaths: []string{baseDir}, CmdGroup: test.group} 226 a.Process() 227 actualResources, err := os.ReadFile(r.Name()) 228 if !assert.NoError(t, err) { 229 t.FailNow() 230 } 231 if !assert.Equal(t, 232 strings.TrimSpace(test.expected), 233 strings.TrimSpace(string(actualResources))) { 234 t.FailNow() 235 } 236 }) 237 } 238 }