| 1 |
// Copyright 2019 The Kubernetes Authors. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package accumulator |
| 5 |
|
| 6 |
import ( |
| 7 |
"strings" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/stretchr/testify/require" |
| 11 |
"sigs.k8s.io/kustomize/api/internal/plugins/builtinconfig" |
| 12 |
"sigs.k8s.io/kustomize/api/provider" |
| 13 |
"sigs.k8s.io/kustomize/api/resmap" |
| 14 |
resmaptest_test "sigs.k8s.io/kustomize/api/testutils/resmaptest" |
| 15 |
"sigs.k8s.io/kustomize/kyaml/resid" |
| 16 |
) |
| 17 |
|
| 18 |
const notEqualErrFmt = "expected (self) doesn't match actual (other): %v" |
| 19 |
|
| 20 |
func TestNameReferenceHappyRun(t *testing.T) { |
| 21 |
m := resmaptest_test.NewRmBuilderDefault(t).AddWithName( |
| 22 |
"cm1", |
| 23 |
map[string]interface{}{ |
| 24 |
"apiVersion": "v1", |
| 25 |
"kind": "ConfigMap", |
| 26 |
"metadata": map[string]interface{}{ |
| 27 |
"name": "someprefix-cm1-somehash", |
| 28 |
}, |
| 29 |
}).AddWithName( |
| 30 |
"cm2", |
| 31 |
map[string]interface{}{ |
| 32 |
"apiVersion": "v1", |
| 33 |
"kind": "ConfigMap", |
| 34 |
"metadata": map[string]interface{}{ |
| 35 |
"name": "someprefix-cm2-somehash", |
| 36 |
}, |
| 37 |
}).AddWithName( |
| 38 |
"secret1", |
| 39 |
map[string]interface{}{ |
| 40 |
"apiVersion": "v1", |
| 41 |
"kind": "Secret", |
| 42 |
"metadata": map[string]interface{}{ |
| 43 |
"name": "someprefix-secret1-somehash", |
| 44 |
}, |
| 45 |
}).AddWithName( |
| 46 |
"claim1", |
| 47 |
map[string]interface{}{ |
| 48 |
"apiVersion": "v1", |
| 49 |
"kind": "PersistentVolumeClaim", |
| 50 |
"metadata": map[string]interface{}{ |
| 51 |
"name": "someprefix-claim1", |
| 52 |
}, |
| 53 |
}).Add( |
| 54 |
map[string]interface{}{ |
| 55 |
"group": "networking.k8s.io", |
| 56 |
"apiVersion": "v1beta1", |
| 57 |
"kind": "Ingress", |
| 58 |
"metadata": map[string]interface{}{ |
| 59 |
"name": "ingress1", |
| 60 |
"annotations": map[string]interface{}{ |
| 61 |
"ingress.kubernetes.io/auth-secret": "secret1", |
| 62 |
"nginx.ingress.kubernetes.io/auth-secret": "secret1", |
| 63 |
"nginx.ingress.kubernetes.io/auth-tls-secret": "secret1", |
| 64 |
}, |
| 65 |
}, |
| 66 |
"spec": map[string]interface{}{ |
| 67 |
"backend": map[string]interface{}{ |
| 68 |
"serviceName": "testsvc", |
| 69 |
"servicePort": "80", |
| 70 |
}, |
| 71 |
}, |
| 72 |
}, |
| 73 |
).Add( |
| 74 |
map[string]interface{}{ |
| 75 |
"group": "apps", |
| 76 |
"apiVersion": "v1", |
| 77 |
"kind": "Deployment", |
| 78 |
"metadata": map[string]interface{}{ |
| 79 |
"name": "deploy1", |
| 80 |
}, |
| 81 |
"spec": map[string]interface{}{ |
| 82 |
"template": map[string]interface{}{ |
| 83 |
"spec": map[string]interface{}{ |
| 84 |
"containers": []interface{}{ |
| 85 |
map[string]interface{}{ |
| 86 |
"name": "nginx", |
| 87 |
"image": "nginx:1.7.9", |
| 88 |
"env": []interface{}{ |
| 89 |
map[string]interface{}{ |
| 90 |
"name": "CM_FOO", |
| 91 |
"valueFrom": map[string]interface{}{ |
| 92 |
"configMapKeyRef": map[string]interface{}{ |
| 93 |
"name": "cm1", |
| 94 |
"key": "somekey", |
| 95 |
}, |
| 96 |
}, |
| 97 |
}, |
| 98 |
map[string]interface{}{ |
| 99 |
"name": "SECRET_FOO", |
| 100 |
"valueFrom": map[string]interface{}{ |
| 101 |
"secretKeyRef": map[string]interface{}{ |
| 102 |
"name": "secret1", |
| 103 |
"key": "somekey", |
| 104 |
}, |
| 105 |
}, |
| 106 |
}, |
| 107 |
}, |
| 108 |
"envFrom": []interface{}{ |
| 109 |
map[string]interface{}{ |
| 110 |
"configMapRef": map[string]interface{}{ |
| 111 |
"name": "cm1", |
| 112 |
"key": "somekey", |
| 113 |
}, |
| 114 |
}, |
| 115 |
map[string]interface{}{ |
| 116 |
"secretRef": map[string]interface{}{ |
| 117 |
"name": "secret1", |
| 118 |
"key": "somekey", |
| 119 |
}, |
| 120 |
}, |
| 121 |
}, |
| 122 |
}, |
| 123 |
}, |
| 124 |
"imagePullSecrets": []interface{}{ |
| 125 |
map[string]interface{}{ |
| 126 |
"name": "secret1", |
| 127 |
}, |
| 128 |
}, |
| 129 |
"volumes": map[string]interface{}{ |
| 130 |
"configMap": map[string]interface{}{ |
| 131 |
"name": "cm1", |
| 132 |
}, |
| 133 |
"projected": map[string]interface{}{ |
| 134 |
"sources": map[string]interface{}{ |
| 135 |
"configMap": map[string]interface{}{ |
| 136 |
"name": "cm2", |
| 137 |
}, |
| 138 |
"secret": map[string]interface{}{ |
| 139 |
"name": "secret1", |
| 140 |
}, |
| 141 |
}, |
| 142 |
}, |
| 143 |
"secret": map[string]interface{}{ |
| 144 |
"secretName": "secret1", |
| 145 |
}, |
| 146 |
"persistentVolumeClaim": map[string]interface{}{ |
| 147 |
"claimName": "claim1", |
| 148 |
}, |
| 149 |
}, |
| 150 |
}, |
| 151 |
}, |
| 152 |
}, |
| 153 |
}).Add( |
| 154 |
map[string]interface{}{ |
| 155 |
"group": "apps", |
| 156 |
"apiVersion": "v1", |
| 157 |
"kind": "StatefulSet", |
| 158 |
"metadata": map[string]interface{}{ |
| 159 |
"name": "statefulset1", |
| 160 |
}, |
| 161 |
"spec": map[string]interface{}{ |
| 162 |
"template": map[string]interface{}{ |
| 163 |
"spec": map[string]interface{}{ |
| 164 |
"containers": []interface{}{ |
| 165 |
map[string]interface{}{ |
| 166 |
"name": "nginx", |
| 167 |
"image": "nginx:1.7.9", |
| 168 |
}, |
| 169 |
}, |
| 170 |
"volumes": map[string]interface{}{ |
| 171 |
"projected": map[string]interface{}{ |
| 172 |
"sources": map[string]interface{}{ |
| 173 |
"configMap": map[string]interface{}{ |
| 174 |
"name": "cm2", |
| 175 |
}, |
| 176 |
"secret": map[string]interface{}{ |
| 177 |
"name": "secret1", |
| 178 |
}, |
| 179 |
}, |
| 180 |
}, |
| 181 |
}, |
| 182 |
}, |
| 183 |
}, |
| 184 |
}, |
| 185 |
}).AddWithName("sa", |
| 186 |
map[string]interface{}{ |
| 187 |
"apiVersion": "v1", |
| 188 |
"kind": "ServiceAccount", |
| 189 |
"metadata": map[string]interface{}{ |
| 190 |
"name": "someprefix-sa", |
| 191 |
"namespace": "test", |
| 192 |
}, |
| 193 |
}).Add( |
| 194 |
map[string]interface{}{ |
| 195 |
"apiVersion": "rbac.authorization.k8s.io/v1", |
| 196 |
"kind": "ClusterRoleBinding", |
| 197 |
"metadata": map[string]interface{}{ |
| 198 |
"name": "crb", |
| 199 |
}, |
| 200 |
"subjects": []interface{}{ |
| 201 |
map[string]interface{}{ |
| 202 |
"kind": "ServiceAccount", |
| 203 |
"name": "sa", |
| 204 |
"namespace": "test", |
| 205 |
}, |
| 206 |
}, |
| 207 |
}).Add( |
| 208 |
map[string]interface{}{ |
| 209 |
"apiVersion": "rbac.authorization.k8s.io/v1", |
| 210 |
"kind": "ClusterRole", |
| 211 |
"metadata": map[string]interface{}{ |
| 212 |
"name": "cr", |
| 213 |
}, |
| 214 |
"rules": []interface{}{ |
| 215 |
map[string]interface{}{ |
| 216 |
"resources": []interface{}{ |
| 217 |
"secrets", |
| 218 |
}, |
| 219 |
"resourceNames": []interface{}{ |
| 220 |
"secret1", |
| 221 |
"secret1", |
| 222 |
"secret2", |
| 223 |
"cm1", |
| 224 |
}, |
| 225 |
}, |
| 226 |
}, |
| 227 |
}).Add( |
| 228 |
map[string]interface{}{ |
| 229 |
"apiVersion": "batch/v1beta1", |
| 230 |
"kind": "CronJob", |
| 231 |
"metadata": map[string]interface{}{ |
| 232 |
"name": "cronjob1", |
| 233 |
}, |
| 234 |
"spec": map[string]interface{}{ |
| 235 |
"schedule": "0 14 * * *", |
| 236 |
"jobTemplate": map[string]interface{}{ |
| 237 |
"spec": map[string]interface{}{ |
| 238 |
"template": map[string]interface{}{ |
| 239 |
"spec": map[string]interface{}{ |
| 240 |
"containers": []interface{}{ |
| 241 |
map[string]interface{}{ |
| 242 |
"name": "main", |
| 243 |
"image": "myimage", |
| 244 |
}, |
| 245 |
}, |
| 246 |
"volumes": map[string]interface{}{ |
| 247 |
"projected": map[string]interface{}{ |
| 248 |
"sources": map[string]interface{}{ |
| 249 |
"configMap": map[string]interface{}{ |
| 250 |
"name": "cm2", |
| 251 |
}, |
| 252 |
"secret": map[string]interface{}{ |
| 253 |
"name": "secret1", |
| 254 |
}, |
| 255 |
}, |
| 256 |
}, |
| 257 |
}, |
| 258 |
}, |
| 259 |
}, |
| 260 |
}, |
| 261 |
}, |
| 262 |
}, |
| 263 |
}).ResMap() |
| 264 |
|
| 265 |
expected := resmaptest_test.NewSeededRmBuilderDefault( |
| 266 |
t, m.ShallowCopy()).ReplaceResource( |
| 267 |
map[string]interface{}{ |
| 268 |
"group": "apps", |
| 269 |
"apiVersion": "v1", |
| 270 |
"kind": "Deployment", |
| 271 |
"metadata": map[string]interface{}{ |
| 272 |
"name": "deploy1", |
| 273 |
}, |
| 274 |
"spec": map[string]interface{}{ |
| 275 |
"template": map[string]interface{}{ |
| 276 |
"spec": map[string]interface{}{ |
| 277 |
"containers": []interface{}{ |
| 278 |
map[string]interface{}{ |
| 279 |
"name": "nginx", |
| 280 |
"image": "nginx:1.7.9", |
| 281 |
"env": []interface{}{ |
| 282 |
map[string]interface{}{ |
| 283 |
"name": "CM_FOO", |
| 284 |
"valueFrom": map[string]interface{}{ |
| 285 |
"configMapKeyRef": map[string]interface{}{ |
| 286 |
"name": "someprefix-cm1-somehash", |
| 287 |
"key": "somekey", |
| 288 |
}, |
| 289 |
}, |
| 290 |
}, |
| 291 |
map[string]interface{}{ |
| 292 |
"name": "SECRET_FOO", |
| 293 |
"valueFrom": map[string]interface{}{ |
| 294 |
"secretKeyRef": map[string]interface{}{ |
| 295 |
"name": "someprefix-secret1-somehash", |
| 296 |
"key": "somekey", |
| 297 |
}, |
| 298 |
}, |
| 299 |
}, |
| 300 |
}, |
| 301 |
"envFrom": []interface{}{ |
| 302 |
map[string]interface{}{ |
| 303 |
"configMapRef": map[string]interface{}{ |
| 304 |
"name": "someprefix-cm1-somehash", |
| 305 |
"key": "somekey", |
| 306 |
}, |
| 307 |
}, |
| 308 |
map[string]interface{}{ |
| 309 |
"secretRef": map[string]interface{}{ |
| 310 |
"name": "someprefix-secret1-somehash", |
| 311 |
"key": "somekey", |
| 312 |
}, |
| 313 |
}, |
| 314 |
}, |
| 315 |
}, |
| 316 |
}, |
| 317 |
"imagePullSecrets": []interface{}{ |
| 318 |
map[string]interface{}{ |
| 319 |
"name": "someprefix-secret1-somehash", |
| 320 |
}, |
| 321 |
}, |
| 322 |
"volumes": map[string]interface{}{ |
| 323 |
"configMap": map[string]interface{}{ |
| 324 |
"name": "someprefix-cm1-somehash", |
| 325 |
}, |
| 326 |
"projected": map[string]interface{}{ |
| 327 |
"sources": map[string]interface{}{ |
| 328 |
"configMap": map[string]interface{}{ |
| 329 |
"name": "someprefix-cm2-somehash", |
| 330 |
}, |
| 331 |
"secret": map[string]interface{}{ |
| 332 |
"name": "someprefix-secret1-somehash", |
| 333 |
}, |
| 334 |
}, |
| 335 |
}, |
| 336 |
"secret": map[string]interface{}{ |
| 337 |
"secretName": "someprefix-secret1-somehash", |
| 338 |
}, |
| 339 |
"persistentVolumeClaim": map[string]interface{}{ |
| 340 |
"claimName": "someprefix-claim1", |
| 341 |
}, |
| 342 |
}, |
| 343 |
}, |
| 344 |
}, |
| 345 |
}, |
| 346 |
}).ReplaceResource( |
| 347 |
map[string]interface{}{ |
| 348 |
"group": "apps", |
| 349 |
"apiVersion": "v1", |
| 350 |
"kind": "StatefulSet", |
| 351 |
"metadata": map[string]interface{}{ |
| 352 |
"name": "statefulset1", |
| 353 |
}, |
| 354 |
"spec": map[string]interface{}{ |
| 355 |
"template": map[string]interface{}{ |
| 356 |
"spec": map[string]interface{}{ |
| 357 |
"containers": []interface{}{ |
| 358 |
map[string]interface{}{ |
| 359 |
"name": "nginx", |
| 360 |
"image": "nginx:1.7.9", |
| 361 |
}, |
| 362 |
}, |
| 363 |
"volumes": map[string]interface{}{ |
| 364 |
"projected": map[string]interface{}{ |
| 365 |
"sources": map[string]interface{}{ |
| 366 |
"configMap": map[string]interface{}{ |
| 367 |
"name": "someprefix-cm2-somehash", |
| 368 |
}, |
| 369 |
"secret": map[string]interface{}{ |
| 370 |
"name": "someprefix-secret1-somehash", |
| 371 |
}, |
| 372 |
}, |
| 373 |
}, |
| 374 |
}, |
| 375 |
}, |
| 376 |
}, |
| 377 |
}, |
| 378 |
}).ReplaceResource( |
| 379 |
map[string]interface{}{ |
| 380 |
"group": "networking.k8s.io", |
| 381 |
"apiVersion": "v1beta1", |
| 382 |
"kind": "Ingress", |
| 383 |
"metadata": map[string]interface{}{ |
| 384 |
"name": "ingress1", |
| 385 |
"annotations": map[string]interface{}{ |
| 386 |
"ingress.kubernetes.io/auth-secret": "someprefix-secret1-somehash", |
| 387 |
"nginx.ingress.kubernetes.io/auth-secret": "someprefix-secret1-somehash", |
| 388 |
"nginx.ingress.kubernetes.io/auth-tls-secret": "someprefix-secret1-somehash", |
| 389 |
}, |
| 390 |
}, |
| 391 |
"spec": map[string]interface{}{ |
| 392 |
"backend": map[string]interface{}{ |
| 393 |
"serviceName": "testsvc", |
| 394 |
"servicePort": "80", |
| 395 |
}, |
| 396 |
}, |
| 397 |
}).ReplaceResource( |
| 398 |
map[string]interface{}{ |
| 399 |
"apiVersion": "rbac.authorization.k8s.io/v1", |
| 400 |
"kind": "ClusterRoleBinding", |
| 401 |
"metadata": map[string]interface{}{ |
| 402 |
"name": "crb", |
| 403 |
}, |
| 404 |
"subjects": []interface{}{ |
| 405 |
map[string]interface{}{ |
| 406 |
"kind": "ServiceAccount", |
| 407 |
"name": "someprefix-sa", |
| 408 |
"namespace": "test", |
| 409 |
}, |
| 410 |
}, |
| 411 |
}).ReplaceResource( |
| 412 |
map[string]interface{}{ |
| 413 |
"apiVersion": "rbac.authorization.k8s.io/v1", |
| 414 |
"kind": "ClusterRole", |
| 415 |
"metadata": map[string]interface{}{ |
| 416 |
"name": "cr", |
| 417 |
}, |
| 418 |
"rules": []interface{}{ |
| 419 |
map[string]interface{}{ |
| 420 |
"resources": []interface{}{ |
| 421 |
"secrets", |
| 422 |
}, |
| 423 |
"resourceNames": []interface{}{ |
| 424 |
"someprefix-secret1-somehash", |
| 425 |
"someprefix-secret1-somehash", |
| 426 |
"secret2", |
| 427 |
"someprefix-cm1-somehash", |
| 428 |
}, |
| 429 |
}, |
| 430 |
}, |
| 431 |
}).ReplaceResource( |
| 432 |
map[string]interface{}{ |
| 433 |
"apiVersion": "batch/v1beta1", |
| 434 |
"kind": "CronJob", |
| 435 |
"metadata": map[string]interface{}{ |
| 436 |
"name": "cronjob1", |
| 437 |
}, |
| 438 |
"spec": map[string]interface{}{ |
| 439 |
"schedule": "0 14 * * *", |
| 440 |
"jobTemplate": map[string]interface{}{ |
| 441 |
"spec": map[string]interface{}{ |
| 442 |
"template": map[string]interface{}{ |
| 443 |
"spec": map[string]interface{}{ |
| 444 |
"containers": []interface{}{ |
| 445 |
map[string]interface{}{ |
| 446 |
"name": "main", |
| 447 |
"image": "myimage", |
| 448 |
}, |
| 449 |
}, |
| 450 |
"volumes": map[string]interface{}{ |
| 451 |
"projected": map[string]interface{}{ |
| 452 |
"sources": map[string]interface{}{ |
| 453 |
"configMap": map[string]interface{}{ |
| 454 |
"name": "someprefix-cm2-somehash", |
| 455 |
}, |
| 456 |
"secret": map[string]interface{}{ |
| 457 |
"name": "someprefix-secret1-somehash", |
| 458 |
}, |
| 459 |
}, |
| 460 |
}, |
| 461 |
}, |
| 462 |
}, |
| 463 |
}, |
| 464 |
}, |
| 465 |
}, |
| 466 |
}, |
| 467 |
}).ResMap() |
| 468 |
|
| 469 |
nrt := newNameReferenceTransformer(builtinconfig.MakeDefaultConfig().NameReference) |
| 470 |
err := nrt.Transform(m) |
| 471 |
if err != nil { |
| 472 |
t.Fatalf("unexpected error: %v", err) |
| 473 |
} |
| 474 |
|
| 475 |
if err = expected.ErrorIfNotEqualLists(m); err != nil { |
| 476 |
t.Fatalf(notEqualErrFmt, err) |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
func TestNameReferenceUnhappyRun(t *testing.T) { |
| 481 |
tests := []struct { |
| 482 |
resMap resmap.ResMap |
| 483 |
expectedErr string |
| 484 |
}{ |
| 485 |
{ |
| 486 |
resMap: resmaptest_test.NewRmBuilderDefault(t).Add( |
| 487 |
map[string]interface{}{ |
| 488 |
"apiVersion": "rbac.authorization.k8s.io/v1", |
| 489 |
"kind": "ClusterRole", |
| 490 |
"metadata": map[string]interface{}{ |
| 491 |
"name": "cr", |
| 492 |
}, |
| 493 |
"rules": []interface{}{ |
| 494 |
map[string]interface{}{ |
| 495 |
"resources": []interface{}{ |
| 496 |
"secrets", |
| 497 |
}, |
| 498 |
"resourceNames": []interface{}{ |
| 499 |
[]interface{}{}, |
| 500 |
}, |
| 501 |
}, |
| 502 |
}, |
| 503 |
}).ResMap(), |
| 504 |
expectedErr: "is expected to be"}, |
| 505 |
{ |
| 506 |
resMap: resmaptest_test.NewRmBuilderDefault(t).Add( |
| 507 |
map[string]interface{}{ |
| 508 |
"apiVersion": "rbac.authorization.k8s.io/v1", |
| 509 |
"kind": "ClusterRole", |
| 510 |
"metadata": map[string]interface{}{ |
| 511 |
"name": "cr", |
| 512 |
}, |
| 513 |
"rules": []interface{}{ |
| 514 |
map[string]interface{}{ |
| 515 |
"resources": []interface{}{ |
| 516 |
"secrets", |
| 517 |
}, |
| 518 |
"resourceNames": map[string]interface{}{ |
| 519 |
"foo": "bar", |
| 520 |
}, |
| 521 |
}, |
| 522 |
}, |
| 523 |
}).ResMap(), |
| 524 |
expectedErr: `updating name reference in 'rules/resourceNames' field of 'ClusterRole.v1.rbac.authorization.k8s.io/cr.[noNs]': ` + |
| 525 |
`considering field 'rules/resourceNames' of object ClusterRole.v1.rbac.authorization.k8s.io/cr.[noNs]: visit traversal on ` + |
| 526 |
`path: [resourceNames]: path config error; no 'name' field in node`, |
| 527 |
}, |
| 528 |
{ |
| 529 |
// When targeting a map reference, we need to update both name and namespace, so multiple |
| 530 |
// possible referral targets with different namespaces should not be considered identical. |
| 531 |
// This test covers a bug where the difference in namespace was ignored and one candidate was chosen at random. |
| 532 |
resMap: resmaptest_test.NewRmBuilderDefault(t).AddWithNsAndName( |
| 533 |
"", orgname, |
| 534 |
map[string]interface{}{ |
| 535 |
"apiVersion": "v1", |
| 536 |
"kind": "ServiceAccount", |
| 537 |
"metadata": map[string]interface{}{ |
| 538 |
"name": orgname, |
| 539 |
"namespace": ns1, |
| 540 |
}, |
| 541 |
}, |
| 542 |
).AddWithNsAndName( |
| 543 |
"", orgname, |
| 544 |
map[string]interface{}{ |
| 545 |
"apiVersion": "v1", |
| 546 |
"kind": "ServiceAccount", |
| 547 |
"metadata": map[string]interface{}{ |
| 548 |
"name": orgname, |
| 549 |
"namespace": ns2, |
| 550 |
}, |
| 551 |
}, |
| 552 |
).Add( |
| 553 |
map[string]interface{}{ |
| 554 |
"apiVersion": "rbac.authorization.k8s.io/v1", |
| 555 |
"kind": "ClusterRoleBinding", |
| 556 |
"metadata": map[string]interface{}{ |
| 557 |
"name": orgname, |
| 558 |
}, |
| 559 |
"roleRef": map[string]interface{}{ |
| 560 |
"apiGroup": "rbac.authorization.k8s.io", |
| 561 |
"kind": "ClusterRole", |
| 562 |
"name": orgname, |
| 563 |
}, |
| 564 |
"subjects": []interface{}{ |
| 565 |
map[string]interface{}{ |
| 566 |
"kind": "ServiceAccount", |
| 567 |
"name": orgname, |
| 568 |
}, |
| 569 |
}, |
| 570 |
}, |
| 571 |
).ResMap(), |
| 572 |
expectedErr: "found multiple possible referrals: ServiceAccount.v1.[noGrp]/uniquename.ns1, ServiceAccount.v1.[noGrp]/uniquename.ns2", |
| 573 |
}, |
| 574 |
} |
| 575 |
|
| 576 |
nrt := newNameReferenceTransformer(builtinconfig.MakeDefaultConfig().NameReference) |
| 577 |
for _, test := range tests { |
| 578 |
err := nrt.Transform(test.resMap) |
| 579 |
if err == nil { |
| 580 |
t.Fatalf("expected error to happen") |
| 581 |
} |
| 582 |
|
| 583 |
if !strings.Contains(err.Error(), test.expectedErr) { |
| 584 |
t.Fatalf("Incorrect error.\nExpected:\n %s\nGot:\n%v", |
| 585 |
test.expectedErr, err) |
| 586 |
} |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
func TestNameReferencePersistentVolumeHappyRun(t *testing.T) { |
| 591 |
rf := provider.NewDefaultDepProvider().GetResourceFactory() |
| 592 |
|
| 593 |
v1, err := rf.FromMapWithName( |
| 594 |
"volume1", |
| 595 |
map[string]interface{}{ |
| 596 |
"apiVersion": "v1", |
| 597 |
"kind": "PersistentVolume", |
| 598 |
"metadata": map[string]interface{}{ |
| 599 |
"name": "someprefix-volume1", |
| 600 |
}, |
| 601 |
}) |
| 602 |
if err != nil { |
| 603 |
t.Fatalf("failed to get new instance with given name: %v", err) |
| 604 |
} |
| 605 |
c1, err := rf.FromMapWithName( |
| 606 |
"claim1", |
| 607 |
map[string]interface{}{ |
| 608 |
"apiVersion": "v1", |
| 609 |
"kind": "PersistentVolumeClaim", |
| 610 |
"metadata": map[string]interface{}{ |
| 611 |
"name": "someprefix-claim1", |
| 612 |
"namespace": "some-namespace", |
| 613 |
}, |
| 614 |
"spec": map[string]interface{}{ |
| 615 |
"volumeName": "volume1", |
| 616 |
}, |
| 617 |
}) |
| 618 |
if err != nil { |
| 619 |
t.Fatalf("failed to get new instance with given name: %v", err) |
| 620 |
} |
| 621 |
v2 := v1.DeepCopy() |
| 622 |
c2, err := rf.FromMapWithName( |
| 623 |
"claim1", |
| 624 |
map[string]interface{}{ |
| 625 |
"apiVersion": "v1", |
| 626 |
"kind": "PersistentVolumeClaim", |
| 627 |
"metadata": map[string]interface{}{ |
| 628 |
"name": "someprefix-claim1", |
| 629 |
"namespace": "some-namespace", |
| 630 |
}, |
| 631 |
"spec": map[string]interface{}{ |
| 632 |
"volumeName": "someprefix-volume1", |
| 633 |
}, |
| 634 |
}) |
| 635 |
if err != nil { |
| 636 |
t.Fatalf("failed to get new instance with given name: %v", err) |
| 637 |
} |
| 638 |
|
| 639 |
m1 := resmaptest_test.NewRmBuilder(t, rf).AddR(v1).AddR(c1).ResMap() |
| 640 |
|
| 641 |
nrt := newNameReferenceTransformer(builtinconfig.MakeDefaultConfig().NameReference) |
| 642 |
if err := nrt.Transform(m1); err != nil { |
| 643 |
t.Fatalf("unexpected error: %v", err) |
| 644 |
} |
| 645 |
|
| 646 |
m2 := resmaptest_test.NewRmBuilder(t, rf).AddR(v2).AddR(c2).ResMap() |
| 647 |
v2.AppendRefBy(c2.CurId()) |
| 648 |
|
| 649 |
if err := m1.ErrorIfNotEqualLists(m2); err != nil { |
| 650 |
t.Fatalf(notEqualErrFmt, err) |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
// utility map to create a deployment object |
| 655 |
// with (metadatanamespace, metadataname) as key |
| 656 |
// and pointing to "refname" secret and configmap |
| 657 |
func deploymentMap(metadatanamespace string, metadataname string, |
| 658 |
configmapref string, secretref string) map[string]interface{} { |
| 659 |
deployment := map[string]interface{}{ |
| 660 |
"group": "apps", |
| 661 |
"apiVersion": "v1", |
| 662 |
"kind": "Deployment", |
| 663 |
"metadata": map[string]interface{}{ |
| 664 |
"name": metadataname, |
| 665 |
}, |
| 666 |
"spec": map[string]interface{}{ |
| 667 |
"template": map[string]interface{}{ |
| 668 |
"spec": map[string]interface{}{ |
| 669 |
"containers": []interface{}{ |
| 670 |
map[string]interface{}{ |
| 671 |
"name": "nginx", |
| 672 |
"image": "nginx:1.7.9", |
| 673 |
"env": []interface{}{ |
| 674 |
map[string]interface{}{ |
| 675 |
"name": "CM_FOO", |
| 676 |
"valueFrom": map[string]interface{}{ |
| 677 |
"configMapKeyRef": map[string]interface{}{ |
| 678 |
"name": configmapref, |
| 679 |
"key": "somekey", |
| 680 |
}, |
| 681 |
}, |
| 682 |
}, |
| 683 |
map[string]interface{}{ |
| 684 |
"name": "SECRET_FOO", |
| 685 |
"valueFrom": map[string]interface{}{ |
| 686 |
"secretKeyRef": map[string]interface{}{ |
| 687 |
"name": secretref, |
| 688 |
"key": "somekey", |
| 689 |
}, |
| 690 |
}, |
| 691 |
}, |
| 692 |
}, |
| 693 |
}, |
| 694 |
}, |
| 695 |
}, |
| 696 |
}, |
| 697 |
}, |
| 698 |
} |
| 699 |
|
| 700 |
if metadatanamespace != "" { |
| 701 |
metadata := deployment["metadata"].(map[string]interface{}) |
| 702 |
metadata["namespace"] = metadatanamespace |
| 703 |
} |
| 704 |
return deployment |
| 705 |
} |
| 706 |
|
| 707 |
const ( |
| 708 |
defaultNs = "default" |
| 709 |
ns1 = "ns1" |
| 710 |
ns2 = "ns2" |
| 711 |
ns3 = "ns3" |
| 712 |
ns4 = "ns4" |
| 713 |
|
| 714 |
orgname = "uniquename" |
| 715 |
prefixedname = "prefix-uniquename" |
| 716 |
suffixedname = "uniquename-suffix" |
| 717 |
modifiedname = "modifiedname" |
| 718 |
) |
| 719 |
|
| 720 |
// TestNameReferenceNamespace creates serviceAccount and clusterRoleBinding |
| 721 |
// object with the same original names (uniquename) in different namespaces |
| 722 |
// and with different current Id. |
| 723 |
func TestNameReferenceNamespace(t *testing.T) { |
| 724 |
m := resmaptest_test.NewRmBuilderDefault(t). |
| 725 |
// Add ConfigMap with the same org name in noNs, "ns1" and "ns2" namespaces |
| 726 |
AddWithName(orgname, map[string]interface{}{ |
| 727 |
"apiVersion": "v1", |
| 728 |
"kind": "ConfigMap", |
| 729 |
"metadata": map[string]interface{}{ |
| 730 |
"name": modifiedname, |
| 731 |
}}). |
| 732 |
AddWithNsAndName(ns1, orgname, map[string]interface{}{ |
| 733 |
"apiVersion": "v1", |
| 734 |
"kind": "ConfigMap", |
| 735 |
"metadata": map[string]interface{}{ |
| 736 |
"name": prefixedname, |
| 737 |
"namespace": ns1, |
| 738 |
}}). |
| 739 |
AddWithNsAndName(ns2, orgname, map[string]interface{}{ |
| 740 |
"apiVersion": "v1", |
| 741 |
"kind": "ConfigMap", |
| 742 |
"metadata": map[string]interface{}{ |
| 743 |
"name": suffixedname, |
| 744 |
"namespace": ns2, |
| 745 |
}}). |
| 746 |
// Add Secret with the same org name in noNs, "ns1" and "ns2" namespaces |
| 747 |
AddWithNsAndName(defaultNs, orgname, map[string]interface{}{ |
| 748 |
"apiVersion": "v1", |
| 749 |
"kind": "Secret", |
| 750 |
"metadata": map[string]interface{}{ |
| 751 |
"name": modifiedname, |
| 752 |
"namespace": defaultNs, |
| 753 |
}}). |
| 754 |
AddWithNsAndName(ns1, orgname, map[string]interface{}{ |
| 755 |
"apiVersion": "v1", |
| 756 |
"kind": "Secret", |
| 757 |
"metadata": map[string]interface{}{ |
| 758 |
"name": prefixedname, |
| 759 |
"namespace": ns1, |
| 760 |
}}). |
| 761 |
AddWithNsAndName(ns2, orgname, map[string]interface{}{ |
| 762 |
"apiVersion": "v1", |
| 763 |
"kind": "Secret", |
| 764 |
"metadata": map[string]interface{}{ |
| 765 |
"name": suffixedname, |
| 766 |
"namespace": ns2, |
| 767 |
}}). |
| 768 |
// Add Deployment with the same org name in noNs, "ns1" and "ns2" namespaces |
| 769 |
AddWithNsAndName(defaultNs, orgname, deploymentMap(defaultNs, modifiedname, modifiedname, modifiedname)). |
| 770 |
AddWithNsAndName(ns1, orgname, deploymentMap(ns1, prefixedname, orgname, orgname)). |
| 771 |
AddWithNsAndName(ns2, orgname, deploymentMap(ns2, suffixedname, orgname, orgname)).ResMap() |
| 772 |
|
| 773 |
expected := resmaptest_test.NewSeededRmBuilderDefault(t, m.ShallowCopy()). |
| 774 |
ReplaceResource(deploymentMap(defaultNs, modifiedname, modifiedname, modifiedname)). |
| 775 |
ReplaceResource(deploymentMap(ns1, prefixedname, prefixedname, prefixedname)). |
| 776 |
ReplaceResource(deploymentMap(ns2, suffixedname, suffixedname, suffixedname)).ResMap() |
| 777 |
|
| 778 |
nrt := newNameReferenceTransformer(builtinconfig.MakeDefaultConfig().NameReference) |
| 779 |
err := nrt.Transform(m) |
| 780 |
if err != nil { |
| 781 |
t.Fatalf("unexpected error: %v", err) |
| 782 |
} |
| 783 |
|
| 784 |
m.RemoveBuildAnnotations() |
| 785 |
if err = expected.ErrorIfNotEqualLists(m); err != nil { |
| 786 |
t.Fatalf(notEqualErrFmt, err) |
| 787 |
} |
| 788 |
} |
| 789 |
|
| 790 |
// TestNameReferenceNamespace creates serviceAccount and clusterRoleBinding |
| 791 |
// object with the same original names (uniquename) in different namespaces |
| 792 |
// and with different current Id. |
| 793 |
func TestNameReferenceClusterWide(t *testing.T) { |
| 794 |
m := resmaptest_test.NewRmBuilderDefault(t). |
| 795 |
// Add ServiceAccount with the same org name in noNs, "ns1" and "ns2" namespaces |
| 796 |
AddWithName(orgname, map[string]interface{}{ |
| 797 |
"apiVersion": "v1", |
| 798 |
"kind": "ServiceAccount", |
| 799 |
"metadata": map[string]interface{}{ |
| 800 |
"name": modifiedname, |
| 801 |
}}). |
| 802 |
AddWithNsAndName(ns1, orgname, map[string]interface{}{ |
| 803 |
"apiVersion": "v1", |
| 804 |
"kind": "ServiceAccount", |
| 805 |
"metadata": map[string]interface{}{ |
| 806 |
"name": prefixedname, |
| 807 |
"namespace": ns1, |
| 808 |
}}). |
| 809 |
AddWithNsAndName(ns2, orgname, map[string]interface{}{ |
| 810 |
"apiVersion": "v1", |
| 811 |
"kind": "ServiceAccount", |
| 812 |
"metadata": map[string]interface{}{ |
| 813 |
"name": suffixedname, |
| 814 |
"namespace": ns2, |
| 815 |
}}). |
| 816 |
// Add a PersistentVolume to have a clusterwide resource |
| 817 |
AddWithName(orgname, map[string]interface{}{ |
| 818 |
"apiVersion": "v1", |
| 819 |
"kind": "PersistentVolume", |
| 820 |
"metadata": map[string]interface{}{ |
| 821 |
"name": modifiedname, |
| 822 |
}}). |
| 823 |
AddWithName(orgname, map[string]interface{}{ |
| 824 |
"apiVersion": "rbac.authorization.k8s.io/v1", |
| 825 |
"kind": "ClusterRole", |
| 826 |
"metadata": map[string]interface{}{ |
| 827 |
"name": modifiedname, |
| 828 |
}, |
| 829 |
"rules": []interface{}{ |
| 830 |
map[string]interface{}{ |
| 831 |
"resources": []interface{}{ |
| 832 |
"persistentvolumes", |
| 833 |
}, |
| 834 |
"resourceNames": []interface{}{ |
| 835 |
orgname, |
| 836 |
}, |
| 837 |
}, |
| 838 |
}}). |
| 839 |
AddWithName(orgname, map[string]interface{}{ |
| 840 |
"apiVersion": "rbac.authorization.k8s.io/v1", |
| 841 |
"kind": "ClusterRoleBinding", |
| 842 |
"metadata": map[string]interface{}{ |
| 843 |
"name": modifiedname, |
| 844 |
}, |
| 845 |
"roleRef": map[string]interface{}{ |
| 846 |
"apiGroup": "rbac.authorization.k8s.io", |
| 847 |
"kind": "ClusterRole", |
| 848 |
"name": orgname, |
| 849 |
}, |
| 850 |
"subjects": []interface{}{ |
| 851 |
map[string]interface{}{ |
| 852 |
"kind": "ServiceAccount", |
| 853 |
"name": orgname, |
| 854 |
"namespace": defaultNs, |
| 855 |
}, |
| 856 |
map[string]interface{}{ |
| 857 |
"kind": "ServiceAccount", |
| 858 |
"name": orgname, |
| 859 |
"namespace": ns1, |
| 860 |
}, |
| 861 |
map[string]interface{}{ |
| 862 |
"kind": "ServiceAccount", |
| 863 |
"name": orgname, |
| 864 |
"namespace": ns2, |
| 865 |
}, |
| 866 |
map[string]interface{}{ |
| 867 |
"kind": "ServiceAccount", |
| 868 |
"name": orgname, |
| 869 |
"namespace": "random", |
| 870 |
}, |
| 871 |
}}).ResMap() |
| 872 |
|
| 873 |
expected := resmaptest_test.NewSeededRmBuilderDefault(t, m.ShallowCopy()). |
| 874 |
ReplaceResource( |
| 875 |
map[string]interface{}{ |
| 876 |
"apiVersion": "rbac.authorization.k8s.io/v1", |
| 877 |
"kind": "ClusterRole", |
| 878 |
"metadata": map[string]interface{}{ |
| 879 |
"name": modifiedname, |
| 880 |
}, |
| 881 |
// Behavior of the transformer is still imperfect |
| 882 |
// It should use the (resources,apigroup,resourceNames) as |
| 883 |
// combination to select the candidates. |
| 884 |
"rules": []interface{}{ |
| 885 |
map[string]interface{}{ |
| 886 |
"resources": []interface{}{ |
| 887 |
"persistentvolumes", |
| 888 |
}, |
| 889 |
"resourceNames": []interface{}{ |
| 890 |
modifiedname, |
| 891 |
}, |
| 892 |
}, |
| 893 |
}}). |
| 894 |
ReplaceResource( |
| 895 |
map[string]interface{}{ |
| 896 |
"apiVersion": "rbac.authorization.k8s.io/v1", |
| 897 |
"kind": "ClusterRoleBinding", |
| 898 |
"metadata": map[string]interface{}{ |
| 899 |
"name": modifiedname, |
| 900 |
}, |
| 901 |
"roleRef": map[string]interface{}{ |
| 902 |
"apiGroup": "rbac.authorization.k8s.io", |
| 903 |
"kind": "ClusterRole", |
| 904 |
"name": modifiedname, |
| 905 |
}, |
| 906 |
// The following tests required a change in |
| 907 |
// getNameFunc implementation in order to leverage |
| 908 |
// the namespace field. |
| 909 |
"subjects": []interface{}{ |
| 910 |
map[string]interface{}{ |
| 911 |
"kind": "ServiceAccount", |
| 912 |
"name": modifiedname, |
| 913 |
"namespace": defaultNs, |
| 914 |
}, |
| 915 |
map[string]interface{}{ |
| 916 |
"kind": "ServiceAccount", |
| 917 |
"name": prefixedname, |
| 918 |
"namespace": ns1, |
| 919 |
}, |
| 920 |
map[string]interface{}{ |
| 921 |
"kind": "ServiceAccount", |
| 922 |
"name": suffixedname, |
| 923 |
"namespace": ns2, |
| 924 |
}, |
| 925 |
map[string]interface{}{ |
| 926 |
"kind": "ServiceAccount", |
| 927 |
"name": orgname, |
| 928 |
"namespace": "random", |
| 929 |
}, |
| 930 |
}, |
| 931 |
}).ResMap() |
| 932 |
|
| 933 |
clusterRoleId := resid.NewResId( |
| 934 |
resid.NewGvk("rbac.authorization.k8s.io", "v1", "ClusterRole"), modifiedname) |
| 935 |
clusterRoleBindingId := resid.NewResId( |
| 936 |
resid.NewGvk("rbac.authorization.k8s.io", "v1", "ClusterRoleBinding"), modifiedname) |
| 937 |
clusterRole, _ := expected.GetByCurrentId(clusterRoleId) |
| 938 |
clusterRole.AppendRefBy(clusterRoleBindingId) |
| 939 |
|
| 940 |
nrt := newNameReferenceTransformer(builtinconfig.MakeDefaultConfig().NameReference) |
| 941 |
err := nrt.Transform(m) |
| 942 |
if err != nil { |
| 943 |
t.Fatalf("unexpected error: %v", err) |
| 944 |
} |
| 945 |
|
| 946 |
expected.RemoveBuildAnnotations() |
| 947 |
m.RemoveBuildAnnotations() |
| 948 |
|
| 949 |
if err = expected.ErrorIfNotEqualLists(m); err != nil { |
| 950 |
t.Fatalf(notEqualErrFmt, err) |
| 951 |
} |
| 952 |
} |
| 953 |
|
| 954 |
// TestNameReferenceNamespaceTransformation creates serviceAccount and clusterRoleBinding |
| 955 |
// object with the same original names (uniquename) in different namespaces |
| 956 |
// and with different current Id. |
| 957 |
func TestNameReferenceNamespaceTransformation(t *testing.T) { |
| 958 |
m := resmaptest_test.NewRmBuilderDefault(t). |
| 959 |
AddWithNsAndName(ns4, orgname, map[string]interface{}{ |
| 960 |
"apiVersion": "v1", |
| 961 |
"kind": "Secret", |
| 962 |
"metadata": map[string]interface{}{ |
| 963 |
"name": orgname, |
| 964 |
"namespace": ns4, |
| 965 |
}}). |
| 966 |
// Add ServiceAccount with the same org name in "ns1" namespaces |
| 967 |
AddWithNsAndName(ns1, orgname, map[string]interface{}{ |
| 968 |
"apiVersion": "v1", |
| 969 |
"kind": "ServiceAccount", |
| 970 |
"metadata": map[string]interface{}{ |
| 971 |
"name": prefixedname, |
| 972 |
"namespace": ns1, |
| 973 |
}}). |
| 974 |
// Simulate NamespaceTransformer effect (ns3 transformed in ns2) |
| 975 |
AddWithNsAndName(ns3, orgname, map[string]interface{}{ |
| 976 |
"apiVersion": "v1", |
| 977 |
"kind": "ServiceAccount", |
| 978 |
"metadata": map[string]interface{}{ |
| 979 |
"name": suffixedname, |
| 980 |
"namespace": ns2, |
| 981 |
}}). |
| 982 |
AddWithName(orgname, map[string]interface{}{ |
| 983 |
"apiVersion": "rbac.authorization.k8s.io/v1", |
| 984 |
"kind": "ClusterRole", |
| 985 |
"metadata": map[string]interface{}{ |
| 986 |
"name": modifiedname, |
| 987 |
}}). |
| 988 |
AddWithName(orgname, map[string]interface{}{ |
| 989 |
"apiVersion": "rbac.authorization.k8s.io/v1", |
| 990 |
"kind": "ClusterRoleBinding", |
| 991 |
"metadata": map[string]interface{}{ |
| 992 |
"name": modifiedname, |
| 993 |
}, |
| 994 |
"roleRef": map[string]interface{}{ |
| 995 |
"apiGroup": "rbac.authorization.k8s.io", |
| 996 |
"kind": "ClusterRole", |
| 997 |
"name": orgname, |
| 998 |
}, |
| 999 |
"subjects": []interface{}{ |
| 1000 |
map[string]interface{}{ |
| 1001 |
"kind": "ServiceAccount", |
| 1002 |
"name": orgname, |
| 1003 |
"namespace": ns1, |
| 1004 |
}, |
| 1005 |
map[string]interface{}{ |
| 1006 |
"kind": "ServiceAccount", |
| 1007 |
"name": orgname, |
| 1008 |
"namespace": ns3, |
| 1009 |
}, |
| 1010 |
map[string]interface{}{ |
| 1011 |
"kind": "ServiceAccount", |
| 1012 |
"name": orgname, |
| 1013 |
"namespace": "random", |
| 1014 |
}, |
| 1015 |
map[string]interface{}{ |
| 1016 |
"kind": "ServiceAccount", |
| 1017 |
"name": orgname, |
| 1018 |
"namespace": ns4, |
| 1019 |
}, |
| 1020 |
}}).ResMap() |
| 1021 |
|
| 1022 |
expected := resmaptest_test.NewSeededRmBuilderDefault(t, m.ShallowCopy()). |
| 1023 |
ReplaceResource( |
| 1024 |
map[string]interface{}{ |
| 1025 |
"apiVersion": "rbac.authorization.k8s.io/v1", |
| 1026 |
"kind": "ClusterRoleBinding", |
| 1027 |
"metadata": map[string]interface{}{ |
| 1028 |
"name": modifiedname, |
| 1029 |
}, |
| 1030 |
"roleRef": map[string]interface{}{ |
| 1031 |
"apiGroup": "rbac.authorization.k8s.io", |
| 1032 |
"kind": "ClusterRole", |
| 1033 |
"name": modifiedname, |
| 1034 |
}, |
| 1035 |
// The following tests required a change in |
| 1036 |
// getNameFunc implementation in order to leverage |
| 1037 |
// the namespace field. |
| 1038 |
"subjects": []interface{}{ |
| 1039 |
map[string]interface{}{ |
| 1040 |
"kind": "ServiceAccount", |
| 1041 |
"name": prefixedname, |
| 1042 |
"namespace": ns1, |
| 1043 |
}, |
| 1044 |
map[string]interface{}{ |
| 1045 |
"kind": "ServiceAccount", |
| 1046 |
"name": suffixedname, |
| 1047 |
"namespace": ns2, |
| 1048 |
}, |
| 1049 |
map[string]interface{}{ |
| 1050 |
"kind": "ServiceAccount", |
| 1051 |
"name": orgname, |
| 1052 |
"namespace": "random", |
| 1053 |
}, |
| 1054 |
map[string]interface{}{ |
| 1055 |
"kind": "ServiceAccount", |
| 1056 |
"name": orgname, |
| 1057 |
"namespace": ns4, |
| 1058 |
}, |
| 1059 |
}, |
| 1060 |
}).ResMap() |
| 1061 |
|
| 1062 |
clusterRoleId := resid.NewResId( |
| 1063 |
resid.NewGvk("rbac.authorization.k8s.io", "v1", "ClusterRole"), |
| 1064 |
modifiedname) |
| 1065 |
clusterRoleBindingId := resid.NewResId( |
| 1066 |
resid.NewGvk("rbac.authorization.k8s.io", "v1", "ClusterRoleBinding"), |
| 1067 |
modifiedname) |
| 1068 |
clusterRole, _ := expected.GetByCurrentId(clusterRoleId) |
| 1069 |
clusterRole.AppendRefBy(clusterRoleBindingId) |
| 1070 |
|
| 1071 |
nrt := newNameReferenceTransformer(builtinconfig.MakeDefaultConfig().NameReference) |
| 1072 |
err := nrt.Transform(m) |
| 1073 |
if err != nil { |
| 1074 |
t.Fatalf("unexpected error: %v", err) |
| 1075 |
} |
| 1076 |
|
| 1077 |
m.RemoveBuildAnnotations() |
| 1078 |
if err = expected.ErrorIfNotEqualLists(m); err != nil { |
| 1079 |
t.Fatalf(notEqualErrFmt, err) |
| 1080 |
} |
| 1081 |
} |
| 1082 |
|
| 1083 |
// TestNameReferenceNamespace creates configmap, secret, deployment |
| 1084 |
// It validates the change done is IsSameFuzzyNamespace which |
| 1085 |
// uses the IsNsEquals method instead of the simple == operator. |
| 1086 |
func TestNameReferenceCandidateSelection(t *testing.T) { |
| 1087 |
m := resmaptest_test.NewRmBuilderDefault(t). |
| 1088 |
AddWithName("cm1", map[string]interface{}{ |
| 1089 |
"apiVersion": "v1", |
| 1090 |
"kind": "ConfigMap", |
| 1091 |
"metadata": map[string]interface{}{ |
| 1092 |
"name": "p1-cm1-hash", |
| 1093 |
}}). |
| 1094 |
AddWithNsAndName("default", "secret1", map[string]interface{}{ |
| 1095 |
"apiVersion": "v1", |
| 1096 |
"kind": "Secret", |
| 1097 |
"metadata": map[string]interface{}{ |
| 1098 |
"name": "p1-secret1-hash", |
| 1099 |
"namespace": "default", |
| 1100 |
}}). |
| 1101 |
AddWithName("deploy1", deploymentMap("", "p1-deploy1", "cm1", "secret1")). |
| 1102 |
ResMap() |
| 1103 |
|
| 1104 |
expected := resmaptest_test.NewSeededRmBuilderDefault(t, m.ShallowCopy()). |
| 1105 |
ReplaceResource(deploymentMap("", "p1-deploy1", "p1-cm1-hash", "p1-secret1-hash")). |
| 1106 |
ResMap() |
| 1107 |
|
| 1108 |
nrt := newNameReferenceTransformer(builtinconfig.MakeDefaultConfig().NameReference) |
| 1109 |
err := nrt.Transform(m) |
| 1110 |
if err != nil { |
| 1111 |
t.Fatalf("unexpected error: %v", err) |
| 1112 |
} |
| 1113 |
|
| 1114 |
m.RemoveBuildAnnotations() |
| 1115 |
if err = expected.ErrorIfNotEqualLists(m); err != nil { |
| 1116 |
t.Fatalf(notEqualErrFmt, err) |
| 1117 |
} |
| 1118 |
} |
| 1119 |
|
| 1120 |
func TestNameReferenceCandidateDisambiguationByNamespace(t *testing.T) { |
| 1121 |
// The ClusterRole refers to both configmaps, since it is not namespace-specific. |
| 1122 |
// Since both names are updated consistently, the transformer should be able to |
| 1123 |
// silently update the ClusterRole as well. |
| 1124 |
// This test guards against a regression where allNamesAndNamespacesAreTheSame would be |
| 1125 |
// used to detect referral candidate identity instead of allNamesAreTheSame. |
| 1126 |
m := resmaptest_test.NewRmBuilderDefault(t).AddWithName(orgname, |
| 1127 |
map[string]interface{}{ |
| 1128 |
"apiVersion": "v1", |
| 1129 |
"kind": "ConfigMap", |
| 1130 |
"metadata": map[string]interface{}{ |
| 1131 |
"name": suffixedname, |
| 1132 |
"namespace": ns1, |
| 1133 |
}, |
| 1134 |
}, |
| 1135 |
).AddWithName(orgname, |
| 1136 |
map[string]interface{}{ |
| 1137 |
"apiVersion": "v1", |
| 1138 |
"kind": "ConfigMap", |
| 1139 |
"metadata": map[string]interface{}{ |
| 1140 |
"name": suffixedname, |
| 1141 |
"namespace": ns2, |
| 1142 |
}, |
| 1143 |
}, |
| 1144 |
).Add( |
| 1145 |
map[string]interface{}{ |
| 1146 |
"apiVersion": "rbac.authorization.k8s.io/v1", |
| 1147 |
"kind": "ClusterRole", |
| 1148 |
"metadata": map[string]interface{}{ |
| 1149 |
"name": orgname, |
| 1150 |
}, |
| 1151 |
"rules": []interface{}{ |
| 1152 |
map[string]interface{}{ |
| 1153 |
"resources": []interface{}{ |
| 1154 |
"configmaps", |
| 1155 |
}, |
| 1156 |
"resourceNames": []interface{}{ |
| 1157 |
orgname, |
| 1158 |
}, |
| 1159 |
}, |
| 1160 |
}, |
| 1161 |
}, |
| 1162 |
).ResMap() |
| 1163 |
|
| 1164 |
expected := resmaptest_test.NewSeededRmBuilderDefault(t, m.ShallowCopy()). |
| 1165 |
ReplaceResource(map[string]interface{}{ |
| 1166 |
"apiVersion": "rbac.authorization.k8s.io/v1", |
| 1167 |
"kind": "ClusterRole", |
| 1168 |
"metadata": map[string]interface{}{ |
| 1169 |
"name": orgname, |
| 1170 |
}, |
| 1171 |
"rules": []interface{}{ |
| 1172 |
map[string]interface{}{ |
| 1173 |
"resources": []interface{}{ |
| 1174 |
"configmaps", |
| 1175 |
}, |
| 1176 |
"resourceNames": []interface{}{ |
| 1177 |
suffixedname, |
| 1178 |
}, |
| 1179 |
}, |
| 1180 |
}, |
| 1181 |
}, |
| 1182 |
).ResMap() |
| 1183 |
|
| 1184 |
nrt := newNameReferenceTransformer(builtinconfig.MakeDefaultConfig().NameReference) |
| 1185 |
require.NoError(t, nrt.Transform(m)) |
| 1186 |
|
| 1187 |
m.RemoveBuildAnnotations() |
| 1188 |
if err := expected.ErrorIfNotEqualLists(m); err != nil { |
| 1189 |
t.Fatalf(notEqualErrFmt, err) |
| 1190 |
} |
| 1191 |
} |