| 1 |
// Copyright 2019 The Kubernetes Authors. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package main_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
"strings" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest" |
| 14 |
) |
| 15 |
|
| 16 |
func errorContains(err error, possibilities ...string) bool { |
| 17 |
for _, x := range possibilities { |
| 18 |
if strings.Contains(err.Error(), x) { |
| 19 |
return true |
| 20 |
} |
| 21 |
} |
| 22 |
return false |
| 23 |
} |
| 24 |
|
| 25 |
const ( |
| 26 |
target = ` |
| 27 |
apiVersion: apps/v1 |
| 28 |
metadata: |
| 29 |
name: myDeploy |
| 30 |
kind: Deployment |
| 31 |
spec: |
| 32 |
replica: 2 |
| 33 |
template: |
| 34 |
metadata: |
| 35 |
labels: |
| 36 |
old-label: old-value |
| 37 |
spec: |
| 38 |
containers: |
| 39 |
- name: nginx |
| 40 |
image: nginx |
| 41 |
` |
| 42 |
targetWithNamespace = ` |
| 43 |
apiVersion: apps/v1 |
| 44 |
metadata: |
| 45 |
name: myDeploy |
| 46 |
namespace: namespace1 |
| 47 |
kind: Deployment |
| 48 |
spec: |
| 49 |
replica: 2 |
| 50 |
template: |
| 51 |
metadata: |
| 52 |
labels: |
| 53 |
old-label: old-value |
| 54 |
spec: |
| 55 |
containers: |
| 56 |
- name: nginx |
| 57 |
image: nginx |
| 58 |
` |
| 59 |
targetNoschema = ` |
| 60 |
apiVersion: example.com/v1 |
| 61 |
kind: Foo |
| 62 |
metadata: |
| 63 |
name: my-foo |
| 64 |
spec: |
| 65 |
bar: |
| 66 |
A: X |
| 67 |
B: Y |
| 68 |
` |
| 69 |
) |
| 70 |
|
| 71 |
func TestPatchStrategicMergeTransformerMissingFile(t *testing.T) { |
| 72 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 73 |
PrepBuiltin("PatchStrategicMergeTransformer") |
| 74 |
defer th.Reset() |
| 75 |
_, err := th.RunTransformer(` |
| 76 |
apiVersion: builtin |
| 77 |
kind: PatchStrategicMergeTransformer |
| 78 |
metadata: |
| 79 |
name: notImportantHere |
| 80 |
paths: |
| 81 |
- patch.yaml |
| 82 |
`, target) |
| 83 |
if assert.Error(t, err) && !errorContains(err, |
| 84 |
"'/patch.yaml' doesn't exist", |
| 85 |
"cannot unmarshal string") { |
| 86 |
t.Fatalf("unexpected err: %v", err) |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
func TestBadPatchStrategicMergeTransformer(t *testing.T) { |
| 91 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 92 |
PrepBuiltin("PatchStrategicMergeTransformer") |
| 93 |
defer th.Reset() |
| 94 |
_, err := th.RunTransformer(` |
| 95 |
apiVersion: builtin |
| 96 |
kind: PatchStrategicMergeTransformer |
| 97 |
metadata: |
| 98 |
name: notImportantHere |
| 99 |
patches: 'thisIsNotAPatch' |
| 100 |
`, target) |
| 101 |
if assert.Error(t, err) && !errorContains(err, |
| 102 |
"cannot unmarshal string into Go value of type map[string]interface {}", |
| 103 |
"fails configuration: missing Resource metadata") { |
| 104 |
t.Fatalf("unexpected err: %v", err) |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
func TestBothEmptyPatchStrategicMergeTransformer(t *testing.T) { |
| 109 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 110 |
PrepBuiltin("PatchStrategicMergeTransformer") |
| 111 |
defer th.Reset() |
| 112 |
_, err := th.RunTransformer(` |
| 113 |
apiVersion: builtin |
| 114 |
kind: PatchStrategicMergeTransformer |
| 115 |
metadata: |
| 116 |
name: notImportantHere |
| 117 |
`, target) |
| 118 |
if assert.Error(t, err) && !errorContains( |
| 119 |
err, "empty file path and empty patch content") { |
| 120 |
t.Fatalf("unexpected err: %v", err) |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
func TestPatchStrategicMergeTransformerFromFiles(t *testing.T) { |
| 125 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 126 |
PrepBuiltin("PatchStrategicMergeTransformer") |
| 127 |
defer th.Reset() |
| 128 |
|
| 129 |
th.WriteF("/patch.yaml", ` |
| 130 |
apiVersion: apps/v1 |
| 131 |
metadata: |
| 132 |
name: myDeploy |
| 133 |
kind: Deployment |
| 134 |
spec: |
| 135 |
template: |
| 136 |
metadata: |
| 137 |
labels: |
| 138 |
new-label: new-value |
| 139 |
replica: 3 |
| 140 |
`) |
| 141 |
|
| 142 |
th.RunTransformerAndCheckResult(` |
| 143 |
apiVersion: builtin |
| 144 |
kind: PatchStrategicMergeTransformer |
| 145 |
metadata: |
| 146 |
name: notImportantHere |
| 147 |
paths: |
| 148 |
- patch.yaml |
| 149 |
`, |
| 150 |
target, ` |
| 151 |
apiVersion: apps/v1 |
| 152 |
kind: Deployment |
| 153 |
metadata: |
| 154 |
name: myDeploy |
| 155 |
spec: |
| 156 |
replica: 3 |
| 157 |
template: |
| 158 |
metadata: |
| 159 |
labels: |
| 160 |
new-label: new-value |
| 161 |
old-label: old-value |
| 162 |
spec: |
| 163 |
containers: |
| 164 |
- image: nginx |
| 165 |
name: nginx |
| 166 |
`) |
| 167 |
} |
| 168 |
|
| 169 |
func TestPatchStrategicMergeTransformerWithInlineJSON(t *testing.T) { |
| 170 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 171 |
PrepBuiltin("PatchStrategicMergeTransformer") |
| 172 |
defer th.Reset() |
| 173 |
|
| 174 |
th.RunTransformerAndCheckResult(` |
| 175 |
apiVersion: builtin |
| 176 |
kind: PatchStrategicMergeTransformer |
| 177 |
metadata: |
| 178 |
name: notImportantHere |
| 179 |
patches: '{"apiVersion": "apps/v1", "metadata": {"name": "myDeploy"}, "kind": "Deployment", "spec": {"replica": 3}}' |
| 180 |
`, |
| 181 |
target, ` |
| 182 |
apiVersion: apps/v1 |
| 183 |
kind: Deployment |
| 184 |
metadata: |
| 185 |
name: myDeploy |
| 186 |
spec: |
| 187 |
replica: 3 |
| 188 |
template: |
| 189 |
metadata: |
| 190 |
labels: |
| 191 |
old-label: old-value |
| 192 |
spec: |
| 193 |
containers: |
| 194 |
- image: nginx |
| 195 |
name: nginx |
| 196 |
`) |
| 197 |
} |
| 198 |
|
| 199 |
func TestPatchStrategicMergeTransformerWithInlineYAML(t *testing.T) { |
| 200 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 201 |
PrepBuiltin("PatchStrategicMergeTransformer") |
| 202 |
defer th.Reset() |
| 203 |
|
| 204 |
th.RunTransformerAndCheckResult(` |
| 205 |
apiVersion: builtin |
| 206 |
kind: PatchStrategicMergeTransformer |
| 207 |
metadata: |
| 208 |
name: notImportantHere |
| 209 |
patches: |- |
| 210 |
apiVersion: apps/v1 |
| 211 |
metadata: |
| 212 |
name: myDeploy |
| 213 |
kind: Deployment |
| 214 |
spec: |
| 215 |
replica: 3 |
| 216 |
--- |
| 217 |
apiVersion: apps/v1 |
| 218 |
metadata: |
| 219 |
name: myDeploy |
| 220 |
kind: Deployment |
| 221 |
spec: |
| 222 |
template: |
| 223 |
spec: |
| 224 |
containers: |
| 225 |
- name: nginx |
| 226 |
image: nginx:latest |
| 227 |
`, |
| 228 |
target, ` |
| 229 |
apiVersion: apps/v1 |
| 230 |
kind: Deployment |
| 231 |
metadata: |
| 232 |
name: myDeploy |
| 233 |
spec: |
| 234 |
replica: 3 |
| 235 |
template: |
| 236 |
metadata: |
| 237 |
labels: |
| 238 |
old-label: old-value |
| 239 |
spec: |
| 240 |
containers: |
| 241 |
- image: nginx:latest |
| 242 |
name: nginx |
| 243 |
`) |
| 244 |
} |
| 245 |
|
| 246 |
func TestPatchStrategicMergeTransformerMultiplePatches(t *testing.T) { |
| 247 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 248 |
PrepBuiltin("PatchStrategicMergeTransformer") |
| 249 |
defer th.Reset() |
| 250 |
|
| 251 |
th.WriteF("patch1.yaml", ` |
| 252 |
apiVersion: apps/v1 |
| 253 |
metadata: |
| 254 |
name: myDeploy |
| 255 |
kind: Deployment |
| 256 |
spec: |
| 257 |
template: |
| 258 |
spec: |
| 259 |
containers: |
| 260 |
- name: nginx |
| 261 |
image: nginx:latest |
| 262 |
env: |
| 263 |
- name: SOMEENV |
| 264 |
value: BAR |
| 265 |
`) |
| 266 |
|
| 267 |
th.WriteF("patch2.yaml", ` |
| 268 |
apiVersion: apps/v1 |
| 269 |
metadata: |
| 270 |
name: myDeploy |
| 271 |
kind: Deployment |
| 272 |
spec: |
| 273 |
template: |
| 274 |
spec: |
| 275 |
containers: |
| 276 |
- name: nginx |
| 277 |
env: |
| 278 |
- name: ANOTHERENV |
| 279 |
value: HELLO |
| 280 |
- name: busybox |
| 281 |
image: busybox |
| 282 |
`) |
| 283 |
|
| 284 |
th.RunTransformerAndCheckResult(` |
| 285 |
apiVersion: builtin |
| 286 |
kind: PatchStrategicMergeTransformer |
| 287 |
metadata: |
| 288 |
name: notImportantHere |
| 289 |
paths: |
| 290 |
- patch1.yaml |
| 291 |
- patch2.yaml |
| 292 |
`, |
| 293 |
target, ` |
| 294 |
apiVersion: apps/v1 |
| 295 |
kind: Deployment |
| 296 |
metadata: |
| 297 |
name: myDeploy |
| 298 |
spec: |
| 299 |
replica: 2 |
| 300 |
template: |
| 301 |
metadata: |
| 302 |
labels: |
| 303 |
old-label: old-value |
| 304 |
spec: |
| 305 |
containers: |
| 306 |
- env: |
| 307 |
- name: ANOTHERENV |
| 308 |
value: HELLO |
| 309 |
- name: SOMEENV |
| 310 |
value: BAR |
| 311 |
image: nginx:latest |
| 312 |
name: nginx |
| 313 |
- image: busybox |
| 314 |
name: busybox |
| 315 |
`) |
| 316 |
} |
| 317 |
|
| 318 |
func TestStrategicMergeTransformerWrongNamespace(t *testing.T) { |
| 319 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 320 |
PrepBuiltin("PatchStrategicMergeTransformer") |
| 321 |
defer th.Reset() |
| 322 |
th.WriteF("patch.yaml", ` |
| 323 |
apiVersion: apps/v1 |
| 324 |
metadata: |
| 325 |
name: myDeploy |
| 326 |
namespace: namespace2 |
| 327 |
kind: Deployment |
| 328 |
spec: |
| 329 |
template: |
| 330 |
spec: |
| 331 |
containers: |
| 332 |
- name: nginx |
| 333 |
image: nginx:latest |
| 334 |
env: |
| 335 |
- name: SOMEENV |
| 336 |
value: BAR |
| 337 |
`) |
| 338 |
_, err := th.RunTransformer(` |
| 339 |
apiVersion: builtin |
| 340 |
kind: PatchStrategicMergeTransformer |
| 341 |
metadata: |
| 342 |
name: notImportantHere |
| 343 |
paths: |
| 344 |
- patch.yaml |
| 345 |
`, targetWithNamespace) |
| 346 |
if assert.Error(t, err) && !errorContains( |
| 347 |
err, "failed to find unique target for patch") { |
| 348 |
t.Fatalf("expected error to contain %q but get %v", "failed to find target for patch", err) |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
// issue #2734 -- https://github.com/kubernetes-sigs/kustomize/issues/2734 |
| 353 |
// kyaml cleans up things some folks prefer it didnt |
| 354 |
// 1. [] -- see initContainers and imagePullSecrets |
| 355 |
// 2. {} -- see emptyDir |
| 356 |
// 3. null -- see creationTimestamp |
| 357 |
|
| 358 |
const anUncleanDeploymentResource = `apiVersion: apps/v1 |
| 359 |
kind: Deployment |
| 360 |
metadata: |
| 361 |
name: sas-crunchy-data-postgres-operator |
| 362 |
spec: |
| 363 |
replicas: 1 |
| 364 |
template: |
| 365 |
metadata: |
| 366 |
creationTimestamp: null |
| 367 |
spec: |
| 368 |
serviceAccountName: postgres-operator |
| 369 |
containers: |
| 370 |
- name: apiserver |
| 371 |
image: sas-crunchy-data-operator-api-server |
| 372 |
imagePullPolicy: IfNotPresent |
| 373 |
ports: |
| 374 |
- containerPort: 8443 |
| 375 |
envFrom: [] |
| 376 |
volumeMounts: |
| 377 |
- name: security-ssh |
| 378 |
mountPath: /security-ssh |
| 379 |
- mountPath: /tmp |
| 380 |
name: tmp |
| 381 |
imagePullSecrets: [] |
| 382 |
initContainers: [] |
| 383 |
volumes: |
| 384 |
- emptyDir: {} |
| 385 |
name: security-ssh |
| 386 |
- emptyDir: {} |
| 387 |
name: tmp |
| 388 |
` |
| 389 |
|
| 390 |
// This is the preffered result (and what we get with kustomize 3.7.0) |
| 391 |
const expectedCleanedDeployment = `apiVersion: apps/v1 |
| 392 |
kind: Deployment |
| 393 |
metadata: |
| 394 |
labels: |
| 395 |
workload.sas.com/class: stateless |
| 396 |
name: sas-crunchy-data-postgres-operator |
| 397 |
spec: |
| 398 |
replicas: 1 |
| 399 |
template: |
| 400 |
metadata: |
| 401 |
creationTimestamp: null |
| 402 |
labels: |
| 403 |
workload.sas.com/class: stateless |
| 404 |
spec: |
| 405 |
containers: |
| 406 |
- envFrom: [] |
| 407 |
image: sas-crunchy-data-operator-api-server |
| 408 |
imagePullPolicy: IfNotPresent |
| 409 |
name: apiserver |
| 410 |
ports: |
| 411 |
- containerPort: 8443 |
| 412 |
volumeMounts: |
| 413 |
- mountPath: /security-ssh |
| 414 |
name: security-ssh |
| 415 |
- mountPath: /tmp |
| 416 |
name: tmp |
| 417 |
imagePullSecrets: [] |
| 418 |
initContainers: [] |
| 419 |
serviceAccountName: postgres-operator |
| 420 |
tolerations: |
| 421 |
- effect: NoSchedule |
| 422 |
key: workload.sas.com/class |
| 423 |
operator: Equal |
| 424 |
value: stateful |
| 425 |
volumes: |
| 426 |
- emptyDir: {} |
| 427 |
name: security-ssh |
| 428 |
- emptyDir: {} |
| 429 |
name: tmp |
| 430 |
` |
| 431 |
|
| 432 |
func TestPatchStrategicMergeTransformerCleanupItems(t *testing.T) { |
| 433 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 434 |
PrepBuiltin("PatchStrategicMergeTransformer") |
| 435 |
defer th.Reset() |
| 436 |
|
| 437 |
th.WriteF("/patch.yaml", ` |
| 438 |
apiVersion: apps/v1 |
| 439 |
kind: Deployment |
| 440 |
metadata: |
| 441 |
name: sas-crunchy-data-postgres-operator |
| 442 |
labels: |
| 443 |
workload.sas.com/class: stateless |
| 444 |
spec: |
| 445 |
template: |
| 446 |
metadata: |
| 447 |
labels: |
| 448 |
workload.sas.com/class: stateless |
| 449 |
spec: |
| 450 |
tolerations: |
| 451 |
- effect: NoSchedule |
| 452 |
key: workload.sas.com/class |
| 453 |
operator: Equal |
| 454 |
value: stateful |
| 455 |
`) |
| 456 |
|
| 457 |
th.RunTransformerAndCheckResult(` |
| 458 |
apiVersion: builtin |
| 459 |
kind: PatchStrategicMergeTransformer |
| 460 |
metadata: |
| 461 |
name: notImportantHere |
| 462 |
paths: |
| 463 |
- patch.yaml |
| 464 |
`, |
| 465 |
anUncleanDeploymentResource, |
| 466 |
expectedCleanedDeployment) // prefer expectedCleanedDeployment |
| 467 |
} |
| 468 |
|
| 469 |
func TestStrategicMergeTransformerNoSchema(t *testing.T) { |
| 470 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 471 |
PrepBuiltin("PatchStrategicMergeTransformer") |
| 472 |
defer th.Reset() |
| 473 |
|
| 474 |
th.WriteF("patch.yaml", ` |
| 475 |
apiVersion: example.com/v1 |
| 476 |
kind: Foo |
| 477 |
metadata: |
| 478 |
name: my-foo |
| 479 |
spec: |
| 480 |
bar: |
| 481 |
B: |
| 482 |
C: Z |
| 483 |
`) |
| 484 |
th.RunTransformerAndCheckResult(` |
| 485 |
apiVersion: builtin |
| 486 |
kind: PatchStrategicMergeTransformer |
| 487 |
metadata: |
| 488 |
name: notImportantHere |
| 489 |
paths: |
| 490 |
- patch.yaml |
| 491 |
`, |
| 492 |
targetNoschema, |
| 493 |
` |
| 494 |
apiVersion: example.com/v1 |
| 495 |
kind: Foo |
| 496 |
metadata: |
| 497 |
name: my-foo |
| 498 |
spec: |
| 499 |
bar: |
| 500 |
A: X |
| 501 |
C: Z |
| 502 |
`) |
| 503 |
} |
| 504 |
|
| 505 |
func TestStrategicMergeTransformerNoSchemaMultiPatches(t *testing.T) { |
| 506 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 507 |
PrepBuiltin("PatchStrategicMergeTransformer") |
| 508 |
defer th.Reset() |
| 509 |
// This patch wants to delete "B". |
| 510 |
th.WriteF("patch1.yaml", ` |
| 511 |
apiVersion: example.com/v1 |
| 512 |
kind: Foo |
| 513 |
metadata: |
| 514 |
name: my-foo |
| 515 |
spec: |
| 516 |
bar: |
| 517 |
B: |
| 518 |
C: Z |
| 519 |
`) |
| 520 |
th.WriteF("patch2.yaml", ` |
| 521 |
apiVersion: example.com/v1 |
| 522 |
kind: Foo |
| 523 |
metadata: |
| 524 |
name: my-foo |
| 525 |
spec: |
| 526 |
bar: |
| 527 |
C: Z |
| 528 |
D: W |
| 529 |
baz: |
| 530 |
hello: world |
| 531 |
`) |
| 532 |
resMap, err := th.RunTransformer(` |
| 533 |
apiVersion: builtin |
| 534 |
kind: PatchStrategicMergeTransformer |
| 535 |
metadata: |
| 536 |
name: notImportantHere |
| 537 |
paths: |
| 538 |
- patch1.yaml |
| 539 |
- patch2.yaml |
| 540 |
`, `apiVersion: example.com/v1 |
| 541 |
kind: Foo |
| 542 |
metadata: |
| 543 |
name: my-foo |
| 544 |
spec: |
| 545 |
bar: |
| 546 |
A: X |
| 547 |
B: Y |
| 548 |
`) |
| 549 |
require.NoError(t, err) |
| 550 |
th.AssertActualEqualsExpectedNoIdAnnotations( |
| 551 |
resMap, ` |
| 552 |
apiVersion: example.com/v1 |
| 553 |
kind: Foo |
| 554 |
metadata: |
| 555 |
name: my-foo |
| 556 |
spec: |
| 557 |
bar: |
| 558 |
A: X |
| 559 |
C: Z |
| 560 |
D: W |
| 561 |
baz: |
| 562 |
hello: world |
| 563 |
`) |
| 564 |
resMap, err = th.RunTransformer(` |
| 565 |
apiVersion: builtin |
| 566 |
kind: PatchStrategicMergeTransformer |
| 567 |
metadata: |
| 568 |
name: notImportantHere |
| 569 |
paths: |
| 570 |
- patch2.yaml |
| 571 |
`, `apiVersion: example.com/v1 |
| 572 |
kind: Foo |
| 573 |
metadata: |
| 574 |
name: my-foo |
| 575 |
spec: |
| 576 |
bar: |
| 577 |
A: X |
| 578 |
B: Y |
| 579 |
`) |
| 580 |
require.NoError(t, err) |
| 581 |
th.AssertActualEqualsExpectedNoIdAnnotations( |
| 582 |
// This time only patch2 is applied. |
| 583 |
resMap, ` |
| 584 |
apiVersion: example.com/v1 |
| 585 |
kind: Foo |
| 586 |
metadata: |
| 587 |
name: my-foo |
| 588 |
spec: |
| 589 |
bar: |
| 590 |
A: X |
| 591 |
B: "Y" |
| 592 |
C: Z |
| 593 |
D: W |
| 594 |
baz: |
| 595 |
hello: world |
| 596 |
`) |
| 597 |
} |
| 598 |
|
| 599 |
// simple utility function to add an namespace in a resource |
| 600 |
// used as base, patch or expected result. Simply looks |
| 601 |
// for specs: in order to add namespace: xxxx before this line |
| 602 |
func addNamespace(namespace string, base string) string { |
| 603 |
res := strings.Replace(base, |
| 604 |
"\nspec:\n", |
| 605 |
"\n namespace: "+namespace+"\nspec:\n", |
| 606 |
1) |
| 607 |
return res |
| 608 |
} |
| 609 |
|
| 610 |
// compareExpectedError compares the expectedError and the actualError return by GetFieldValue |
| 611 |
func compareExpectedError(t *testing.T, name string, err error, errorMsg string) { |
| 612 |
if assert.Error(t, err, name) && !errorContains(err, errorMsg) { |
| 613 |
t.Fatalf("%q; - expected error: \"%s\", got error: \"%v\"", |
| 614 |
name, errorMsg, err.Error()) |
| 615 |
} |
| 616 |
} |
| 617 |
|
| 618 |
const Deployment string = "Deployment" |
| 619 |
const MyCRD string = "MyCRD" |
| 620 |
|
| 621 |
// baseResource produces a base object which used to test |
| 622 |
// patch transformation |
| 623 |
// Also the structure is matching the Deployment syntax |
| 624 |
// the kind can be replaced to allow testing using CRD |
| 625 |
// without access to the schema |
| 626 |
func baseResource(kind string) string { |
| 627 |
|
| 628 |
res := ` |
| 629 |
apiVersion: apps/v1 |
| 630 |
kind: %s |
| 631 |
metadata: |
| 632 |
name: deploy1 |
| 633 |
spec: |
| 634 |
template: |
| 635 |
metadata: |
| 636 |
labels: |
| 637 |
old-label: old-value |
| 638 |
spec: |
| 639 |
containers: |
| 640 |
- name: nginx |
| 641 |
image: nginx` |
| 642 |
return fmt.Sprintf(res, kind) |
| 643 |
} |
| 644 |
|
| 645 |
// addContainerAndEnvPatch produces a patch object which adds |
| 646 |
// an entry in the env slice of the first/nginx container |
| 647 |
// as well as adding a label in the metadata |
| 648 |
// Note that for SMP/WithSchema merge, the name:nginx entry |
| 649 |
// is mandatory |
| 650 |
func addLabelAndEnvPatch(kind string) string { |
| 651 |
res := ` |
| 652 |
apiVersion: apps/v1 |
| 653 |
kind: %s |
| 654 |
metadata: |
| 655 |
name: deploy1 |
| 656 |
spec: |
| 657 |
template: |
| 658 |
metadata: |
| 659 |
labels: |
| 660 |
some-label: some-value |
| 661 |
spec: |
| 662 |
containers: |
| 663 |
- name: nginx |
| 664 |
env: |
| 665 |
- name: SOMEENV |
| 666 |
value: SOMEVALUE` |
| 667 |
|
| 668 |
return fmt.Sprintf(res, kind) |
| 669 |
} |
| 670 |
|
| 671 |
// addContainerAndEnvPatch produces a patch object which adds |
| 672 |
// an entry in the env slice of the first/nginx container |
| 673 |
// as well as adding a second container in the container list |
| 674 |
// Note that for SMP/WithSchema merge, the name:nginx entry |
| 675 |
// is mandatory |
| 676 |
func addContainerAndEnvPatch(kind string) string { |
| 677 |
|
| 678 |
res := ` |
| 679 |
apiVersion: apps/v1 |
| 680 |
kind: %s |
| 681 |
metadata: |
| 682 |
name: deploy1 |
| 683 |
spec: |
| 684 |
template: |
| 685 |
spec: |
| 686 |
containers: |
| 687 |
- name: nginx |
| 688 |
env: |
| 689 |
- name: ANOTHERENV |
| 690 |
value: ANOTHERVALUE |
| 691 |
- name: anothercontainer |
| 692 |
image: anotherimage` |
| 693 |
|
| 694 |
return fmt.Sprintf(res, kind) |
| 695 |
} |
| 696 |
|
| 697 |
// addContainerAndEnvPatch produces a patch object which replaces |
| 698 |
// the value of the image field in the first/nginx container |
| 699 |
// Note that for SMP/WithSchema merge, the name:nginx entry |
| 700 |
// is mandatory |
| 701 |
func changeImagePatch(kind string, newImage string) string { |
| 702 |
|
| 703 |
res := ` |
| 704 |
apiVersion: apps/v1 |
| 705 |
kind: %s |
| 706 |
metadata: |
| 707 |
name: deploy1 |
| 708 |
spec: |
| 709 |
template: |
| 710 |
spec: |
| 711 |
containers: |
| 712 |
- name: nginx |
| 713 |
image: %s` |
| 714 |
|
| 715 |
return fmt.Sprintf(res, kind, newImage) |
| 716 |
} |
| 717 |
|
| 718 |
// utility method building the expected output of a SMP |
| 719 |
func expectedResultSMP() string { |
| 720 |
|
| 721 |
return `apiVersion: apps/v1 |
| 722 |
kind: Deployment |
| 723 |
metadata: |
| 724 |
name: deploy1 |
| 725 |
spec: |
| 726 |
template: |
| 727 |
metadata: |
| 728 |
labels: |
| 729 |
old-label: old-value |
| 730 |
some-label: some-value |
| 731 |
spec: |
| 732 |
containers: |
| 733 |
- env: |
| 734 |
- name: SOMEENV |
| 735 |
value: SOMEVALUE |
| 736 |
image: nginx |
| 737 |
name: nginx |
| 738 |
` |
| 739 |
} |
| 740 |
|
| 741 |
// utility method building the expected output of a JMP. |
| 742 |
// imagename parameter allows to build a result consistent |
| 743 |
// with the JMP behavior which basically overrides the |
| 744 |
// entire "containers" list. |
| 745 |
func expectedResultJMP(imagename string) string { |
| 746 |
|
| 747 |
res := `apiVersion: apps/v1 |
| 748 |
kind: MyCRD |
| 749 |
metadata: |
| 750 |
name: deploy1 |
| 751 |
spec: |
| 752 |
template: |
| 753 |
metadata: |
| 754 |
labels: |
| 755 |
old-label: old-value |
| 756 |
some-label: some-value |
| 757 |
spec: |
| 758 |
containers: |
| 759 |
- env: |
| 760 |
- name: SOMEENV |
| 761 |
value: SOMEVALUE |
| 762 |
name: nginx |
| 763 |
` |
| 764 |
|
| 765 |
if imagename == "" { |
| 766 |
return res |
| 767 |
} |
| 768 |
|
| 769 |
res = `apiVersion: apps/v1 |
| 770 |
kind: MyCRD |
| 771 |
metadata: |
| 772 |
name: deploy1 |
| 773 |
spec: |
| 774 |
template: |
| 775 |
metadata: |
| 776 |
labels: |
| 777 |
old-label: old-value |
| 778 |
some-label: some-value |
| 779 |
spec: |
| 780 |
containers: |
| 781 |
- image: %s |
| 782 |
name: nginx |
| 783 |
` |
| 784 |
|
| 785 |
return fmt.Sprintf(res, imagename) |
| 786 |
} |
| 787 |
|
| 788 |
// utility method to build the expected result of a multipatch |
| 789 |
// the order of the patches still have influence especially |
| 790 |
// in the insertion location within arrays. |
| 791 |
func expectedResultMultiPatch(kind string, reversed bool) string { |
| 792 |
|
| 793 |
res := `apiVersion: apps/v1 |
| 794 |
kind: %s |
| 795 |
metadata: |
| 796 |
name: deploy1 |
| 797 |
spec: |
| 798 |
template: |
| 799 |
metadata: |
| 800 |
labels: |
| 801 |
old-label: old-value |
| 802 |
some-label: some-value |
| 803 |
spec: |
| 804 |
containers: |
| 805 |
- env: |
| 806 |
- name: ANOTHERENV |
| 807 |
value: ANOTHERVALUE |
| 808 |
- name: SOMEENV |
| 809 |
value: SOMEVALUE |
| 810 |
image: nginx:latest |
| 811 |
name: nginx |
| 812 |
- image: anotherimage |
| 813 |
name: anothercontainer |
| 814 |
` |
| 815 |
|
| 816 |
reversedres := `apiVersion: apps/v1 |
| 817 |
kind: %s |
| 818 |
metadata: |
| 819 |
name: deploy1 |
| 820 |
spec: |
| 821 |
template: |
| 822 |
metadata: |
| 823 |
labels: |
| 824 |
old-label: old-value |
| 825 |
some-label: some-value |
| 826 |
spec: |
| 827 |
containers: |
| 828 |
- env: |
| 829 |
- name: SOMEENV |
| 830 |
value: SOMEVALUE |
| 831 |
- name: ANOTHERENV |
| 832 |
value: ANOTHERVALUE |
| 833 |
image: nginx:latest |
| 834 |
name: nginx |
| 835 |
- image: anotherimage |
| 836 |
name: anothercontainer |
| 837 |
` |
| 838 |
|
| 839 |
if reversed { |
| 840 |
return fmt.Sprintf(reversedres, kind) |
| 841 |
} |
| 842 |
|
| 843 |
return fmt.Sprintf(res, kind) |
| 844 |
} |
| 845 |
|
| 846 |
func toConfig(patches ...string) string { |
| 847 |
config := ` |
| 848 |
apiVersion: builtin |
| 849 |
kind: PatchStrategicMergeTransformer |
| 850 |
metadata: |
| 851 |
name: notImportantHere |
| 852 |
paths: |
| 853 |
` |
| 854 |
for idx := range patches { |
| 855 |
config = fmt.Sprintf("%s\n- ./patch%d.yaml", config, idx) |
| 856 |
} |
| 857 |
|
| 858 |
return config |
| 859 |
} |
| 860 |
|
| 861 |
// TestSinglePatch validates the single patch use cases |
| 862 |
// regarless of the schema availibility, which in turns |
| 863 |
// relies on StrategicMergePatch or simple JSON Patch. |
| 864 |
func TestSinglePatch(t *testing.T) { |
| 865 |
tests := []struct { |
| 866 |
name string |
| 867 |
base string |
| 868 |
patch string |
| 869 |
expected string |
| 870 |
errorExpected bool |
| 871 |
errorMsg string |
| 872 |
}{ |
| 873 |
{ |
| 874 |
name: "withschema", |
| 875 |
base: baseResource(Deployment), |
| 876 |
patch: addLabelAndEnvPatch(Deployment), |
| 877 |
errorExpected: false, |
| 878 |
expected: expectedResultSMP(), |
| 879 |
}, |
| 880 |
{ |
| 881 |
name: "noschema", |
| 882 |
base: baseResource(MyCRD), |
| 883 |
patch: addLabelAndEnvPatch(MyCRD), |
| 884 |
errorExpected: false, |
| 885 |
expected: expectedResultJMP(""), |
| 886 |
}, |
| 887 |
} |
| 888 |
|
| 889 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 890 |
PrepBuiltin("PatchStrategicMergeTransformer") |
| 891 |
defer th.Reset() |
| 892 |
|
| 893 |
for _, test := range tests { |
| 894 |
th.ResetLoaderRoot(fmt.Sprintf("/%s", test.name)) |
| 895 |
th.WriteF(fmt.Sprintf("/%s/patch%d.yaml", test.name, 0), test.patch) |
| 896 |
if test.errorExpected { |
| 897 |
_, err := th.RunTransformer(toConfig(test.patch), test.base) |
| 898 |
compareExpectedError(t, test.name, err, test.errorMsg) |
| 899 |
} else { |
| 900 |
th.RunTransformerAndCheckResult( |
| 901 |
toConfig(test.patch), test.base, test.expected) |
| 902 |
} |
| 903 |
} |
| 904 |
} |
| 905 |
|
| 906 |
type testRecord struct { |
| 907 |
base string |
| 908 |
patch []string |
| 909 |
expected string |
| 910 |
errorExpected bool |
| 911 |
errorMsg string |
| 912 |
} |
| 913 |
|
| 914 |
// TestMultiplePatches checks that the patches are applied |
| 915 |
// properly, that the same result is obtained, |
| 916 |
// regardless of the order of the patches and regardless |
| 917 |
// of the schema availability (SMP vs JSON) |
| 918 |
func TestMultiplePatches(t *testing.T) { |
| 919 |
tests := map[string]testRecord{ |
| 920 |
"withschema-label-image-container": { |
| 921 |
base: baseResource(Deployment), |
| 922 |
patch: []string{ |
| 923 |
addLabelAndEnvPatch(Deployment), |
| 924 |
changeImagePatch(Deployment, "nginx:latest"), |
| 925 |
addContainerAndEnvPatch(Deployment), |
| 926 |
}, |
| 927 |
expected: expectedResultMultiPatch(Deployment, false), |
| 928 |
}, |
| 929 |
"withschema-image-container-label": { |
| 930 |
base: baseResource(Deployment), |
| 931 |
patch: []string{ |
| 932 |
changeImagePatch(Deployment, "nginx:latest"), |
| 933 |
addContainerAndEnvPatch(Deployment), |
| 934 |
addLabelAndEnvPatch(Deployment), |
| 935 |
}, |
| 936 |
expected: expectedResultMultiPatch(Deployment, true), |
| 937 |
}, |
| 938 |
"withschema-container-label-image": { |
| 939 |
base: baseResource(Deployment), |
| 940 |
patch: []string{ |
| 941 |
addContainerAndEnvPatch(Deployment), |
| 942 |
addLabelAndEnvPatch(Deployment), |
| 943 |
changeImagePatch(Deployment, "nginx:latest"), |
| 944 |
}, |
| 945 |
expected: expectedResultMultiPatch(Deployment, true), |
| 946 |
}, |
| 947 |
"noschema-1.7.9-label-latest": { |
| 948 |
base: baseResource(MyCRD), |
| 949 |
patch: []string{ |
| 950 |
changeImagePatch(MyCRD, "nginx:1.7.9"), |
| 951 |
addLabelAndEnvPatch(MyCRD), |
| 952 |
changeImagePatch(MyCRD, "nginx:latest"), |
| 953 |
}, |
| 954 |
errorExpected: false, |
| 955 |
// Theses patches aren't commutable (you get a different result |
| 956 |
// if they are ordered differently). This is allowed without error. |
| 957 |
expected: expectedResultJMP("nginx:latest"), |
| 958 |
}, |
| 959 |
"noschema-latest-label-1.7.9": { |
| 960 |
base: baseResource(MyCRD), |
| 961 |
patch: []string{ |
| 962 |
changeImagePatch(MyCRD, "nginx:latest"), |
| 963 |
addLabelAndEnvPatch(MyCRD), |
| 964 |
changeImagePatch(MyCRD, "nginx:1.7.9"), |
| 965 |
}, |
| 966 |
errorExpected: false, |
| 967 |
// Theses patches aren't commutable (you get a different result |
| 968 |
// if they are ordered differently). This is allowed without error. |
| 969 |
expected: expectedResultJMP("nginx:1.7.9"), |
| 970 |
}, |
| 971 |
} |
| 972 |
|
| 973 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 974 |
PrepBuiltin("PatchStrategicMergeTransformer") |
| 975 |
defer th.Reset() |
| 976 |
|
| 977 |
for name, test := range tests { |
| 978 |
t.Run(name, func(t *testing.T) { |
| 979 |
th.ResetLoaderRoot(fmt.Sprintf("/%s", name)) |
| 980 |
for idx, patch := range test.patch { |
| 981 |
th.WriteF(fmt.Sprintf("/%s/patch%d.yaml", name, idx), patch) |
| 982 |
} |
| 983 |
if test.errorExpected { |
| 984 |
_, err := th.RunTransformer(toConfig(test.patch...), test.base) |
| 985 |
compareExpectedError(t, name, err, test.errorMsg) |
| 986 |
} else { |
| 987 |
th.RunTransformerAndCheckResult( |
| 988 |
toConfig(test.patch...), test.base, test.expected) |
| 989 |
} |
| 990 |
}) |
| 991 |
} |
| 992 |
} |
| 993 |
|
| 994 |
// TestMultipleNamespaces before the same patch |
| 995 |
// on two objects have the same name but in a different namespaces |
| 996 |
func TestMultipleNamespaces(t *testing.T) { |
| 997 |
tests := []struct { |
| 998 |
name string |
| 999 |
base []string |
| 1000 |
patch []string |
| 1001 |
expected []string |
| 1002 |
errorExpected bool |
| 1003 |
errorMsg string |
| 1004 |
}{ |
| 1005 |
{ |
| 1006 |
name: "withschema-ns1-ns2", |
| 1007 |
base: []string{ |
| 1008 |
addNamespace("ns1", baseResource(Deployment)), |
| 1009 |
addNamespace("ns2", baseResource(Deployment)), |
| 1010 |
}, |
| 1011 |
patch: []string{ |
| 1012 |
addNamespace("ns1", addLabelAndEnvPatch(Deployment)), |
| 1013 |
addNamespace("ns2", addLabelAndEnvPatch(Deployment)), |
| 1014 |
}, |
| 1015 |
errorExpected: false, |
| 1016 |
expected: []string{ |
| 1017 |
addNamespace("ns1", expectedResultSMP()), |
| 1018 |
addNamespace("ns2", expectedResultSMP()), |
| 1019 |
}, |
| 1020 |
}, |
| 1021 |
{ |
| 1022 |
name: "noschema-ns1-ns2", |
| 1023 |
base: []string{ |
| 1024 |
addNamespace("ns1", baseResource(MyCRD)), |
| 1025 |
addNamespace("ns2", baseResource(MyCRD)), |
| 1026 |
}, |
| 1027 |
patch: []string{ |
| 1028 |
addNamespace("ns1", addLabelAndEnvPatch(MyCRD)), |
| 1029 |
addNamespace("ns2", addLabelAndEnvPatch(MyCRD)), |
| 1030 |
}, |
| 1031 |
errorExpected: false, |
| 1032 |
expected: []string{ |
| 1033 |
addNamespace("ns1", expectedResultJMP("")), |
| 1034 |
addNamespace("ns2", expectedResultJMP("")), |
| 1035 |
}, |
| 1036 |
}, |
| 1037 |
{ |
| 1038 |
name: "withschema-ns1-ns2", |
| 1039 |
base: []string{addNamespace("ns1", baseResource(Deployment))}, |
| 1040 |
patch: []string{addNamespace("ns2", changeImagePatch(Deployment, "nginx:1.7.9"))}, |
| 1041 |
errorExpected: true, |
| 1042 |
errorMsg: "failed to find unique target for patch", |
| 1043 |
}, |
| 1044 |
{ |
| 1045 |
name: "withschema-nil-ns2", |
| 1046 |
base: []string{baseResource(Deployment)}, |
| 1047 |
patch: []string{addNamespace("ns2", changeImagePatch(Deployment, "nginx:1.7.9"))}, |
| 1048 |
errorExpected: true, |
| 1049 |
errorMsg: "failed to find unique target for patch", |
| 1050 |
}, |
| 1051 |
{ |
| 1052 |
name: "withschema-ns1-nil", |
| 1053 |
base: []string{addNamespace("ns1", baseResource(Deployment))}, |
| 1054 |
patch: []string{changeImagePatch(Deployment, "nginx:1.7.9")}, |
| 1055 |
errorExpected: true, |
| 1056 |
errorMsg: "failed to find unique target for patch", |
| 1057 |
}, |
| 1058 |
{ |
| 1059 |
name: "noschema-ns1-ns2", |
| 1060 |
base: []string{addNamespace("ns1", baseResource(MyCRD))}, |
| 1061 |
patch: []string{addNamespace("ns2", changeImagePatch(MyCRD, "nginx:1.7.9"))}, |
| 1062 |
errorExpected: true, |
| 1063 |
errorMsg: "failed to find unique target for patch", |
| 1064 |
}, |
| 1065 |
{ |
| 1066 |
name: "noschema-nil-ns2", |
| 1067 |
base: []string{baseResource(MyCRD)}, |
| 1068 |
patch: []string{addNamespace("ns2", changeImagePatch(MyCRD, "nginx:1.7.9"))}, |
| 1069 |
errorExpected: true, |
| 1070 |
errorMsg: "failed to find unique target for patch", |
| 1071 |
}, |
| 1072 |
{ |
| 1073 |
name: "noschema-ns1-nil", |
| 1074 |
base: []string{addNamespace("ns1", baseResource(MyCRD))}, |
| 1075 |
patch: []string{changeImagePatch(MyCRD, "nginx:1.7.9")}, |
| 1076 |
errorExpected: true, |
| 1077 |
errorMsg: "failed to find unique target for patch", |
| 1078 |
}, |
| 1079 |
} |
| 1080 |
|
| 1081 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 1082 |
PrepBuiltin("PatchStrategicMergeTransformer") |
| 1083 |
defer th.Reset() |
| 1084 |
|
| 1085 |
for _, test := range tests { |
| 1086 |
th.ResetLoaderRoot(fmt.Sprintf("/%s", test.name)) |
| 1087 |
for idx, patch := range test.patch { |
| 1088 |
th.WriteF(fmt.Sprintf("/%s/patch%d.yaml", test.name, idx), patch) |
| 1089 |
} |
| 1090 |
|
| 1091 |
if test.errorExpected { |
| 1092 |
_, err := th.RunTransformer( |
| 1093 |
toConfig(test.patch...), strings.Join(test.base, "\n---\n")) |
| 1094 |
compareExpectedError(t, test.name, err, test.errorMsg) |
| 1095 |
} else { |
| 1096 |
th.RunTransformerAndCheckResult( |
| 1097 |
toConfig(test.patch...), |
| 1098 |
strings.Join(test.base, "\n---\n"), |
| 1099 |
strings.Join(test.expected, "---\n")) |
| 1100 |
} |
| 1101 |
} |
| 1102 |
} |
| 1103 |
|
| 1104 |
func TestPatchStrategicMergeTransformerPatchDelete1(t *testing.T) { |
| 1105 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 1106 |
PrepBuiltin("PatchStrategicMergeTransformer") |
| 1107 |
defer th.Reset() |
| 1108 |
|
| 1109 |
th.WriteF("patch.yaml", ` |
| 1110 |
apiVersion: apps/v1 |
| 1111 |
metadata: |
| 1112 |
name: myDeploy |
| 1113 |
kind: Deployment |
| 1114 |
spec: |
| 1115 |
replica: 2 |
| 1116 |
template: |
| 1117 |
$patch: delete |
| 1118 |
metadata: |
| 1119 |
labels: |
| 1120 |
old-label: old-value |
| 1121 |
spec: |
| 1122 |
containers: |
| 1123 |
- name: nginx |
| 1124 |
image: nginx |
| 1125 |
`) |
| 1126 |
|
| 1127 |
rm := th.LoadAndRunTransformer(` |
| 1128 |
apiVersion: builtin |
| 1129 |
kind: PatchStrategicMergeTransformer |
| 1130 |
metadata: |
| 1131 |
name: notImportantHere |
| 1132 |
paths: |
| 1133 |
- patch.yaml |
| 1134 |
`, target) |
| 1135 |
|
| 1136 |
th.AssertActualEqualsExpectedNoIdAnnotations(rm, ` |
| 1137 |
apiVersion: apps/v1 |
| 1138 |
kind: Deployment |
| 1139 |
metadata: |
| 1140 |
name: myDeploy |
| 1141 |
spec: |
| 1142 |
replica: 2 |
| 1143 |
`) |
| 1144 |
} |
| 1145 |
|
| 1146 |
func TestPatchStrategicMergeTransformerPatchDelete2(t *testing.T) { |
| 1147 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 1148 |
PrepBuiltin("PatchStrategicMergeTransformer") |
| 1149 |
defer th.Reset() |
| 1150 |
|
| 1151 |
th.WriteF("patch.yaml", ` |
| 1152 |
apiVersion: apps/v1 |
| 1153 |
metadata: |
| 1154 |
name: myDeploy |
| 1155 |
kind: Deployment |
| 1156 |
spec: |
| 1157 |
$patch: delete |
| 1158 |
replica: 2 |
| 1159 |
template: |
| 1160 |
metadata: |
| 1161 |
labels: |
| 1162 |
old-label: old-value |
| 1163 |
spec: |
| 1164 |
containers: |
| 1165 |
- name: nginx |
| 1166 |
image: nginx |
| 1167 |
`) |
| 1168 |
|
| 1169 |
rm := th.LoadAndRunTransformer(` |
| 1170 |
apiVersion: builtin |
| 1171 |
kind: PatchStrategicMergeTransformer |
| 1172 |
metadata: |
| 1173 |
name: notImportantHere |
| 1174 |
paths: |
| 1175 |
- patch.yaml |
| 1176 |
`, target) |
| 1177 |
|
| 1178 |
th.AssertActualEqualsExpectedNoIdAnnotations(rm, ` |
| 1179 |
apiVersion: apps/v1 |
| 1180 |
kind: Deployment |
| 1181 |
metadata: |
| 1182 |
name: myDeploy |
| 1183 |
`) |
| 1184 |
} |
| 1185 |
|
| 1186 |
func TestPatchStrategicMergeTransformerPatchDelete3(t *testing.T) { |
| 1187 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 1188 |
PrepBuiltin("PatchStrategicMergeTransformer") |
| 1189 |
defer th.Reset() |
| 1190 |
|
| 1191 |
th.WriteF("patch.yaml", ` |
| 1192 |
apiVersion: apps/v1 |
| 1193 |
metadata: |
| 1194 |
name: myDeploy |
| 1195 |
kind: Deployment |
| 1196 |
$patch: delete |
| 1197 |
`) |
| 1198 |
|
| 1199 |
rm := th.LoadAndRunTransformer(` |
| 1200 |
apiVersion: builtin |
| 1201 |
kind: PatchStrategicMergeTransformer |
| 1202 |
metadata: |
| 1203 |
name: notImportantHere |
| 1204 |
paths: |
| 1205 |
- patch.yaml |
| 1206 |
`, target) |
| 1207 |
|
| 1208 |
th.AssertActualEqualsExpected(rm, ``) |
| 1209 |
} |