| 1 |
// Copyright 2019 The Kubernetes Authors. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package main_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"strings" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/stretchr/testify/require" |
| 11 |
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest" |
| 12 |
) |
| 13 |
|
| 14 |
const ( |
| 15 |
someDeploymentResources = ` |
| 16 |
apiVersion: apps/v1 |
| 17 |
metadata: |
| 18 |
name: myDeploy |
| 19 |
labels: |
| 20 |
old-label: old-value |
| 21 |
kind: Deployment |
| 22 |
spec: |
| 23 |
replica: 2 |
| 24 |
template: |
| 25 |
metadata: |
| 26 |
labels: |
| 27 |
old-label: old-value |
| 28 |
spec: |
| 29 |
containers: |
| 30 |
- name: nginx |
| 31 |
image: nginx |
| 32 |
--- |
| 33 |
apiVersion: apps/v1 |
| 34 |
metadata: |
| 35 |
name: yourDeploy |
| 36 |
labels: |
| 37 |
new-label: new-value |
| 38 |
kind: Deployment |
| 39 |
spec: |
| 40 |
replica: 1 |
| 41 |
template: |
| 42 |
metadata: |
| 43 |
labels: |
| 44 |
new-label: new-value |
| 45 |
spec: |
| 46 |
containers: |
| 47 |
- name: nginx |
| 48 |
image: nginx:1.7.9 |
| 49 |
--- |
| 50 |
apiVersion: apps/v1 |
| 51 |
metadata: |
| 52 |
name: myDeploy |
| 53 |
label: |
| 54 |
old-label: old-value |
| 55 |
kind: MyKind |
| 56 |
spec: |
| 57 |
template: |
| 58 |
metadata: |
| 59 |
labels: |
| 60 |
old-label: old-value |
| 61 |
spec: |
| 62 |
containers: |
| 63 |
- name: nginx |
| 64 |
image: nginx |
| 65 |
` |
| 66 |
) |
| 67 |
|
| 68 |
func TestPatchTransformerMissingFile(t *testing.T) { |
| 69 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 70 |
PrepBuiltin("PatchTransformer") |
| 71 |
defer th.Reset() |
| 72 |
|
| 73 |
th.RunTransformerAndCheckError(` |
| 74 |
apiVersion: builtin |
| 75 |
kind: PatchTransformer |
| 76 |
metadata: |
| 77 |
name: notImportantHere |
| 78 |
path: patch.yaml |
| 79 |
`, someDeploymentResources, func(t *testing.T, err error) { |
| 80 |
if err == nil { |
| 81 |
t.Fatalf("expected error") |
| 82 |
} |
| 83 |
if !strings.Contains(err.Error(), |
| 84 |
"'/patch.yaml' doesn't exist") { |
| 85 |
t.Fatalf("unexpected err: %v", err) |
| 86 |
} |
| 87 |
}) |
| 88 |
} |
| 89 |
|
| 90 |
func TestPatchTransformerBadPatch(t *testing.T) { |
| 91 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 92 |
PrepBuiltin("PatchTransformer") |
| 93 |
defer th.Reset() |
| 94 |
|
| 95 |
th.RunTransformerAndCheckError(` |
| 96 |
apiVersion: builtin |
| 97 |
kind: PatchTransformer |
| 98 |
metadata: |
| 99 |
name: notImportantHere |
| 100 |
patch: "thisIsNotAPatch" |
| 101 |
`, someDeploymentResources, func(t *testing.T, err error) { |
| 102 |
if err == nil { |
| 103 |
t.Fatalf("expected error") |
| 104 |
} |
| 105 |
if !strings.Contains(err.Error(), |
| 106 |
"unable to parse SM or JSON patch from ") { |
| 107 |
t.Fatalf("unexpected err: %v", err) |
| 108 |
} |
| 109 |
}) |
| 110 |
} |
| 111 |
|
| 112 |
func TestPatchTransformerMissingSelector(t *testing.T) { |
| 113 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 114 |
PrepBuiltin("PatchTransformer") |
| 115 |
defer th.Reset() |
| 116 |
|
| 117 |
th.RunTransformerAndCheckError(` |
| 118 |
apiVersion: builtin |
| 119 |
kind: PatchTransformer |
| 120 |
metadata: |
| 121 |
name: notImportantHere |
| 122 |
patch: '[{"op": "add", "path": "/spec/template/spec/dnsPolicy", "value": "ClusterFirst"}]' |
| 123 |
`, someDeploymentResources, func(t *testing.T, err error) { |
| 124 |
if err == nil { |
| 125 |
t.Fatalf("expected error") |
| 126 |
} |
| 127 |
if !strings.Contains(err.Error(), |
| 128 |
"must specify a target for JSON patch") { |
| 129 |
t.Fatalf("unexpected err: %v", err) |
| 130 |
} |
| 131 |
}) |
| 132 |
} |
| 133 |
|
| 134 |
func TestPatchTransformerBlankPatch(t *testing.T) { |
| 135 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 136 |
PrepBuiltin("PatchTransformer") |
| 137 |
defer th.Reset() |
| 138 |
|
| 139 |
th.RunTransformerAndCheckError(` |
| 140 |
apiVersion: builtin |
| 141 |
kind: PatchTransformer |
| 142 |
metadata: |
| 143 |
name: notImportantHere |
| 144 |
patch: " " |
| 145 |
target: |
| 146 |
name: .*Deploy |
| 147 |
`, someDeploymentResources, func(t *testing.T, err error) { |
| 148 |
if err == nil { |
| 149 |
t.Fatalf("expected error") |
| 150 |
} |
| 151 |
if !strings.Contains(err.Error(), |
| 152 |
"must specify one of patch and path in") { |
| 153 |
t.Fatalf("unexpected err: %v", err) |
| 154 |
} |
| 155 |
}) |
| 156 |
} |
| 157 |
|
| 158 |
func TestPatchTransformerBothEmptyPathAndPatch(t *testing.T) { |
| 159 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 160 |
PrepBuiltin("PatchTransformer") |
| 161 |
defer th.Reset() |
| 162 |
|
| 163 |
th.RunTransformerAndCheckError(` |
| 164 |
apiVersion: builtin |
| 165 |
kind: PatchTransformer |
| 166 |
metadata: |
| 167 |
name: notImportantHere |
| 168 |
`, someDeploymentResources, func(t *testing.T, err error) { |
| 169 |
if err == nil { |
| 170 |
t.Fatalf("expected error") |
| 171 |
} |
| 172 |
if !strings.Contains(err.Error(), |
| 173 |
"must specify one of patch and path in") { |
| 174 |
t.Fatalf("unexpected err: %v", err) |
| 175 |
} |
| 176 |
}) |
| 177 |
} |
| 178 |
|
| 179 |
func TestPatchTransformerBothNonEmptyPathAndPatch(t *testing.T) { |
| 180 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 181 |
PrepBuiltin("PatchTransformer") |
| 182 |
defer th.Reset() |
| 183 |
|
| 184 |
th.RunTransformerAndCheckError(` |
| 185 |
apiVersion: builtin |
| 186 |
kind: PatchTransformer |
| 187 |
metadata: |
| 188 |
name: notImportantHere |
| 189 |
Path: patch.yaml |
| 190 |
Patch: "something" |
| 191 |
`, someDeploymentResources, func(t *testing.T, err error) { |
| 192 |
if err == nil { |
| 193 |
t.Fatalf("expected error") |
| 194 |
} |
| 195 |
if !strings.Contains(err.Error(), |
| 196 |
"patch and path can't be set at the same time") { |
| 197 |
t.Fatalf("unexpected err: %v", err) |
| 198 |
} |
| 199 |
}) |
| 200 |
} |
| 201 |
|
| 202 |
const ( |
| 203 |
multipleSMPatchesFile = ` |
| 204 |
apiVersion: apps/v1 |
| 205 |
kind: Deployment |
| 206 |
metadata: |
| 207 |
name: myDeploy |
| 208 |
spec: |
| 209 |
template: |
| 210 |
spec: |
| 211 |
containers: |
| 212 |
- image: public.ecr.aws/nginx/nginx:mainline |
| 213 |
name: nginx |
| 214 |
--- |
| 215 |
apiVersion: apps/v1 |
| 216 |
kind: Deployment |
| 217 |
metadata: |
| 218 |
name: yourDeploy |
| 219 |
spec: |
| 220 |
template: |
| 221 |
metadata: |
| 222 |
labels: |
| 223 |
new-label: new-value-with-multipleSMPatchesFile |
| 224 |
` |
| 225 |
|
| 226 |
multipleSMPatchesSuccesfulResult = ` |
| 227 |
apiVersion: apps/v1 |
| 228 |
kind: Deployment |
| 229 |
metadata: |
| 230 |
labels: |
| 231 |
old-label: old-value |
| 232 |
name: myDeploy |
| 233 |
spec: |
| 234 |
replica: 2 |
| 235 |
template: |
| 236 |
metadata: |
| 237 |
labels: |
| 238 |
old-label: old-value |
| 239 |
spec: |
| 240 |
containers: |
| 241 |
- image: public.ecr.aws/nginx/nginx:mainline |
| 242 |
name: nginx |
| 243 |
--- |
| 244 |
apiVersion: apps/v1 |
| 245 |
kind: Deployment |
| 246 |
metadata: |
| 247 |
labels: |
| 248 |
new-label: new-value |
| 249 |
name: yourDeploy |
| 250 |
spec: |
| 251 |
replica: 1 |
| 252 |
template: |
| 253 |
metadata: |
| 254 |
labels: |
| 255 |
new-label: new-value-with-multipleSMPatchesFile |
| 256 |
spec: |
| 257 |
containers: |
| 258 |
- image: nginx:1.7.9 |
| 259 |
name: nginx |
| 260 |
--- |
| 261 |
apiVersion: apps/v1 |
| 262 |
kind: MyKind |
| 263 |
metadata: |
| 264 |
label: |
| 265 |
old-label: old-value |
| 266 |
name: myDeploy |
| 267 |
spec: |
| 268 |
template: |
| 269 |
metadata: |
| 270 |
labels: |
| 271 |
old-label: old-value |
| 272 |
spec: |
| 273 |
containers: |
| 274 |
- image: nginx |
| 275 |
name: nginx |
| 276 |
` |
| 277 |
) |
| 278 |
|
| 279 |
func TestMultipleSMPatchesWithFilePath(t *testing.T) { |
| 280 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 281 |
PrepBuiltin("PatchTransformer") |
| 282 |
defer th.Reset() |
| 283 |
|
| 284 |
th.WriteF(`multiplepatches.yaml`, multipleSMPatchesFile) |
| 285 |
|
| 286 |
th.RunTransformerAndCheckResult(` |
| 287 |
apiVersion: builtin |
| 288 |
kind: PatchTransformer |
| 289 |
metadata: |
| 290 |
name: notImportantHere |
| 291 |
Path: multiplepatches.yaml |
| 292 |
`, someDeploymentResources, multipleSMPatchesSuccesfulResult) |
| 293 |
} |
| 294 |
|
| 295 |
func TestMultipleSMPatchesWithPatch(t *testing.T) { |
| 296 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 297 |
PrepBuiltin("PatchTransformer") |
| 298 |
defer th.Reset() |
| 299 |
|
| 300 |
th.RunTransformerAndCheckResult(` |
| 301 |
apiVersion: builtin |
| 302 |
kind: PatchTransformer |
| 303 |
metadata: |
| 304 |
name: notImportantHere |
| 305 |
patch: |- |
| 306 |
apiVersion: apps/v1 |
| 307 |
kind: Deployment |
| 308 |
metadata: |
| 309 |
name: myDeploy |
| 310 |
spec: |
| 311 |
template: |
| 312 |
spec: |
| 313 |
containers: |
| 314 |
- image: public.ecr.aws/nginx/nginx:mainline |
| 315 |
name: nginx |
| 316 |
--- |
| 317 |
apiVersion: apps/v1 |
| 318 |
kind: Deployment |
| 319 |
metadata: |
| 320 |
name: yourDeploy |
| 321 |
spec: |
| 322 |
template: |
| 323 |
metadata: |
| 324 |
labels: |
| 325 |
new-label: new-value-with-multipleSMPatchesFile |
| 326 |
`, someDeploymentResources, multipleSMPatchesSuccesfulResult) |
| 327 |
} |
| 328 |
|
| 329 |
func TestMultipleSMPatchesAndTarget(t *testing.T) { |
| 330 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 331 |
PrepBuiltin("PatchTransformer") |
| 332 |
defer th.Reset() |
| 333 |
|
| 334 |
th.WriteF(`multiplepatches.yaml`, multipleSMPatchesFile) |
| 335 |
|
| 336 |
th.RunTransformerAndCheckError(` |
| 337 |
apiVersion: builtin |
| 338 |
kind: PatchTransformer |
| 339 |
metadata: |
| 340 |
name: notImportantHere |
| 341 |
Path: multiplepatches.yaml |
| 342 |
target: |
| 343 |
name: .*Deploy |
| 344 |
kind: Deployment |
| 345 |
`, someDeploymentResources, func(t *testing.T, err error) { |
| 346 |
t.Helper() |
| 347 |
require.ErrorContains(t, err, "Multiple Strategic-Merge Patches in one `patches` entry is not allowed to set `patches.target` field") |
| 348 |
}) |
| 349 |
} |
| 350 |
|
| 351 |
const ( |
| 352 |
oneDeployment = ` |
| 353 |
apiVersion: apps/v1 |
| 354 |
metadata: |
| 355 |
name: oneDeploy |
| 356 |
kind: Deployment |
| 357 |
spec: |
| 358 |
replica: 1 |
| 359 |
template: |
| 360 |
spec: |
| 361 |
containers: |
| 362 |
- name: nginx |
| 363 |
image: nginx:1.7.9 |
| 364 |
- name: sidecar |
| 365 |
image: busybox:1.36.1 |
| 366 |
` |
| 367 |
multiplePatchTransformerConfig = ` |
| 368 |
apiVersion: builtin |
| 369 |
kind: PatchTransformer |
| 370 |
metadata: |
| 371 |
name: notImportantHere |
| 372 |
patch: |- |
| 373 |
- op: replace |
| 374 |
path: /spec/template/spec/containers/0/image |
| 375 |
value: nginx:latest |
| 376 |
--- |
| 377 |
- op: replace |
| 378 |
path: /spec/template/spec/containers/1/image |
| 379 |
value: busybox:latest |
| 380 |
target: |
| 381 |
name: .*Deploy |
| 382 |
kind: Deployment |
| 383 |
` |
| 384 |
) |
| 385 |
|
| 386 |
func TestPatchTransformerNotAllowedMultipleJsonPatches(t *testing.T) { |
| 387 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 388 |
PrepBuiltin("PatchTransformer") |
| 389 |
defer th.Reset() |
| 390 |
|
| 391 |
// TODO(5049): Multiple JSON patch Yaml documents are not allowed and need to return error. |
| 392 |
th.RunTransformerAndCheckResult(multiplePatchTransformerConfig, oneDeployment, ` |
| 393 |
apiVersion: apps/v1 |
| 394 |
kind: Deployment |
| 395 |
metadata: |
| 396 |
name: oneDeploy |
| 397 |
spec: |
| 398 |
replica: 1 |
| 399 |
template: |
| 400 |
spec: |
| 401 |
containers: |
| 402 |
- image: nginx:latest |
| 403 |
name: nginx |
| 404 |
- image: busybox:1.36.1 |
| 405 |
name: sidecar |
| 406 |
`) |
| 407 |
// th.RunTransformerAndCheckError(multiplePatchTransformerConfig, oneDeployment, func(t *testing.T, err error) { |
| 408 |
// t.Helper() |
| 409 |
// if err == nil { |
| 410 |
// t.Fatalf("expected error") |
| 411 |
// } |
| 412 |
// if !strings.Contains(err.Error(), |
| 413 |
// "Multiple Json6902 Patch in 'patches' is not allowed.") { |
| 414 |
// t.Fatalf("unexpected err: %v", err) |
| 415 |
// } |
| 416 |
// }) |
| 417 |
} |
| 418 |
|
| 419 |
func TestPatchTransformerFromFiles(t *testing.T) { |
| 420 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 421 |
PrepBuiltin("PatchTransformer") |
| 422 |
defer th.Reset() |
| 423 |
|
| 424 |
th.WriteF("patch.yaml", ` |
| 425 |
apiVersion: apps/v1 |
| 426 |
kind: Deployment |
| 427 |
metadata: |
| 428 |
name: myDeploy |
| 429 |
spec: |
| 430 |
replica: 3 |
| 431 |
`) |
| 432 |
|
| 433 |
th.RunTransformerAndCheckResult(` |
| 434 |
apiVersion: builtin |
| 435 |
kind: PatchTransformer |
| 436 |
metadata: |
| 437 |
name: notImportantHere |
| 438 |
path: patch.yaml |
| 439 |
target: |
| 440 |
name: .*Deploy |
| 441 |
`, |
| 442 |
someDeploymentResources, |
| 443 |
` |
| 444 |
apiVersion: apps/v1 |
| 445 |
kind: Deployment |
| 446 |
metadata: |
| 447 |
labels: |
| 448 |
old-label: old-value |
| 449 |
name: myDeploy |
| 450 |
spec: |
| 451 |
replica: 3 |
| 452 |
template: |
| 453 |
metadata: |
| 454 |
labels: |
| 455 |
old-label: old-value |
| 456 |
spec: |
| 457 |
containers: |
| 458 |
- image: nginx |
| 459 |
name: nginx |
| 460 |
--- |
| 461 |
apiVersion: apps/v1 |
| 462 |
kind: Deployment |
| 463 |
metadata: |
| 464 |
labels: |
| 465 |
new-label: new-value |
| 466 |
name: yourDeploy |
| 467 |
spec: |
| 468 |
replica: 3 |
| 469 |
template: |
| 470 |
metadata: |
| 471 |
labels: |
| 472 |
new-label: new-value |
| 473 |
spec: |
| 474 |
containers: |
| 475 |
- image: nginx:1.7.9 |
| 476 |
name: nginx |
| 477 |
--- |
| 478 |
apiVersion: apps/v1 |
| 479 |
kind: MyKind |
| 480 |
metadata: |
| 481 |
label: |
| 482 |
old-label: old-value |
| 483 |
name: myDeploy |
| 484 |
spec: |
| 485 |
replica: 3 |
| 486 |
template: |
| 487 |
metadata: |
| 488 |
labels: |
| 489 |
old-label: old-value |
| 490 |
spec: |
| 491 |
containers: |
| 492 |
- image: nginx |
| 493 |
name: nginx |
| 494 |
`) |
| 495 |
} |
| 496 |
|
| 497 |
func TestPatchTransformerSmpSidecars(t *testing.T) { |
| 498 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 499 |
PrepBuiltin("PatchTransformer") |
| 500 |
defer th.Reset() |
| 501 |
|
| 502 |
th.WriteF("patch.yaml", ` |
| 503 |
apiVersion: apps/v1 |
| 504 |
kind: Deployment |
| 505 |
metadata: |
| 506 |
name: not-important |
| 507 |
spec: |
| 508 |
template: |
| 509 |
spec: |
| 510 |
containers: |
| 511 |
- name: istio-proxy |
| 512 |
image: docker.io/istio/proxyv2 |
| 513 |
args: |
| 514 |
- proxy |
| 515 |
- sidecar |
| 516 |
`) |
| 517 |
rm := th.LoadAndRunTransformer(` |
| 518 |
apiVersion: builtin |
| 519 |
kind: PatchTransformer |
| 520 |
metadata: |
| 521 |
name: notImportantHere |
| 522 |
path: patch.yaml |
| 523 |
target: |
| 524 |
name: myDeploy |
| 525 |
`, someDeploymentResources) |
| 526 |
th.AssertActualEqualsExpectedNoIdAnnotations(rm, ` |
| 527 |
apiVersion: apps/v1 |
| 528 |
kind: Deployment |
| 529 |
metadata: |
| 530 |
labels: |
| 531 |
old-label: old-value |
| 532 |
name: myDeploy |
| 533 |
spec: |
| 534 |
replica: 2 |
| 535 |
template: |
| 536 |
metadata: |
| 537 |
labels: |
| 538 |
old-label: old-value |
| 539 |
spec: |
| 540 |
containers: |
| 541 |
- args: |
| 542 |
- proxy |
| 543 |
- sidecar |
| 544 |
image: docker.io/istio/proxyv2 |
| 545 |
name: istio-proxy |
| 546 |
- image: nginx |
| 547 |
name: nginx |
| 548 |
--- |
| 549 |
apiVersion: apps/v1 |
| 550 |
kind: Deployment |
| 551 |
metadata: |
| 552 |
labels: |
| 553 |
new-label: new-value |
| 554 |
name: yourDeploy |
| 555 |
spec: |
| 556 |
replica: 1 |
| 557 |
template: |
| 558 |
metadata: |
| 559 |
labels: |
| 560 |
new-label: new-value |
| 561 |
spec: |
| 562 |
containers: |
| 563 |
- image: nginx:1.7.9 |
| 564 |
name: nginx |
| 565 |
--- |
| 566 |
apiVersion: apps/v1 |
| 567 |
kind: MyKind |
| 568 |
metadata: |
| 569 |
label: |
| 570 |
old-label: old-value |
| 571 |
name: myDeploy |
| 572 |
spec: |
| 573 |
template: |
| 574 |
metadata: |
| 575 |
labels: |
| 576 |
old-label: old-value |
| 577 |
spec: |
| 578 |
containers: |
| 579 |
- args: |
| 580 |
- proxy |
| 581 |
- sidecar |
| 582 |
image: docker.io/istio/proxyv2 |
| 583 |
name: istio-proxy |
| 584 |
- image: nginx |
| 585 |
name: nginx |
| 586 |
`) |
| 587 |
} |
| 588 |
|
| 589 |
func TestPatchTransformerWithInlineJson(t *testing.T) { |
| 590 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 591 |
PrepBuiltin("PatchTransformer") |
| 592 |
defer th.Reset() |
| 593 |
|
| 594 |
th.RunTransformerAndCheckResult(` |
| 595 |
apiVersion: builtin |
| 596 |
kind: PatchTransformer |
| 597 |
metadata: |
| 598 |
name: notImportantHere |
| 599 |
patch: '[{"op": "replace", "path": "/spec/template/spec/containers/0/image", "value": "nginx:latest"}]' |
| 600 |
target: |
| 601 |
name: .*Deploy |
| 602 |
kind: Deployment |
| 603 |
`, someDeploymentResources, |
| 604 |
` |
| 605 |
apiVersion: apps/v1 |
| 606 |
kind: Deployment |
| 607 |
metadata: |
| 608 |
labels: |
| 609 |
old-label: old-value |
| 610 |
name: myDeploy |
| 611 |
spec: |
| 612 |
replica: 2 |
| 613 |
template: |
| 614 |
metadata: |
| 615 |
labels: |
| 616 |
old-label: old-value |
| 617 |
spec: |
| 618 |
containers: |
| 619 |
- image: nginx:latest |
| 620 |
name: nginx |
| 621 |
--- |
| 622 |
apiVersion: apps/v1 |
| 623 |
kind: Deployment |
| 624 |
metadata: |
| 625 |
labels: |
| 626 |
new-label: new-value |
| 627 |
name: yourDeploy |
| 628 |
spec: |
| 629 |
replica: 1 |
| 630 |
template: |
| 631 |
metadata: |
| 632 |
labels: |
| 633 |
new-label: new-value |
| 634 |
spec: |
| 635 |
containers: |
| 636 |
- image: nginx:latest |
| 637 |
name: nginx |
| 638 |
--- |
| 639 |
apiVersion: apps/v1 |
| 640 |
kind: MyKind |
| 641 |
metadata: |
| 642 |
label: |
| 643 |
old-label: old-value |
| 644 |
name: myDeploy |
| 645 |
spec: |
| 646 |
template: |
| 647 |
metadata: |
| 648 |
labels: |
| 649 |
old-label: old-value |
| 650 |
spec: |
| 651 |
containers: |
| 652 |
- image: nginx |
| 653 |
name: nginx |
| 654 |
`) |
| 655 |
} |
| 656 |
|
| 657 |
func TestPatchTransformerWithInlineYaml(t *testing.T) { |
| 658 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 659 |
PrepBuiltin("PatchTransformer") |
| 660 |
defer th.Reset() |
| 661 |
|
| 662 |
th.RunTransformerAndCheckResult(` |
| 663 |
apiVersion: builtin |
| 664 |
kind: PatchTransformer |
| 665 |
metadata: |
| 666 |
name: notImportantHere |
| 667 |
target: |
| 668 |
name: .*Deploy |
| 669 |
kind: Deployment |
| 670 |
patch: |- |
| 671 |
apiVersion: apps/v1 |
| 672 |
metadata: |
| 673 |
name: myDeploy |
| 674 |
kind: Deployment |
| 675 |
spec: |
| 676 |
replica: 77 |
| 677 |
`, someDeploymentResources, ` |
| 678 |
apiVersion: apps/v1 |
| 679 |
kind: Deployment |
| 680 |
metadata: |
| 681 |
labels: |
| 682 |
old-label: old-value |
| 683 |
name: myDeploy |
| 684 |
spec: |
| 685 |
replica: 77 |
| 686 |
template: |
| 687 |
metadata: |
| 688 |
labels: |
| 689 |
old-label: old-value |
| 690 |
spec: |
| 691 |
containers: |
| 692 |
- image: nginx |
| 693 |
name: nginx |
| 694 |
--- |
| 695 |
apiVersion: apps/v1 |
| 696 |
kind: Deployment |
| 697 |
metadata: |
| 698 |
labels: |
| 699 |
new-label: new-value |
| 700 |
name: yourDeploy |
| 701 |
spec: |
| 702 |
replica: 77 |
| 703 |
template: |
| 704 |
metadata: |
| 705 |
labels: |
| 706 |
new-label: new-value |
| 707 |
spec: |
| 708 |
containers: |
| 709 |
- image: nginx:1.7.9 |
| 710 |
name: nginx |
| 711 |
--- |
| 712 |
apiVersion: apps/v1 |
| 713 |
kind: MyKind |
| 714 |
metadata: |
| 715 |
label: |
| 716 |
old-label: old-value |
| 717 |
name: myDeploy |
| 718 |
spec: |
| 719 |
template: |
| 720 |
metadata: |
| 721 |
labels: |
| 722 |
old-label: old-value |
| 723 |
spec: |
| 724 |
containers: |
| 725 |
- image: nginx |
| 726 |
name: nginx |
| 727 |
`) |
| 728 |
} |
| 729 |
|
| 730 |
func TestPatchTransformerWithInlineYamlRegexTarget(t *testing.T) { |
| 731 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 732 |
PrepBuiltin("PatchTransformer") |
| 733 |
defer th.Reset() |
| 734 |
|
| 735 |
th.RunTransformerAndCheckResult(` |
| 736 |
apiVersion: builtin |
| 737 |
kind: PatchTransformer |
| 738 |
metadata: |
| 739 |
name: notImportantHere |
| 740 |
target: |
| 741 |
name: .*Deploy |
| 742 |
kind: Deployment|MyKind |
| 743 |
group: \w{4} |
| 744 |
version: v\d |
| 745 |
patch: |- |
| 746 |
apiVersion: apps/v1 |
| 747 |
metadata: |
| 748 |
name: myDeploy |
| 749 |
kind: Deployment |
| 750 |
spec: |
| 751 |
replica: 77 |
| 752 |
`, someDeploymentResources, ` |
| 753 |
apiVersion: apps/v1 |
| 754 |
kind: Deployment |
| 755 |
metadata: |
| 756 |
labels: |
| 757 |
old-label: old-value |
| 758 |
name: myDeploy |
| 759 |
spec: |
| 760 |
replica: 77 |
| 761 |
template: |
| 762 |
metadata: |
| 763 |
labels: |
| 764 |
old-label: old-value |
| 765 |
spec: |
| 766 |
containers: |
| 767 |
- image: nginx |
| 768 |
name: nginx |
| 769 |
--- |
| 770 |
apiVersion: apps/v1 |
| 771 |
kind: Deployment |
| 772 |
metadata: |
| 773 |
labels: |
| 774 |
new-label: new-value |
| 775 |
name: yourDeploy |
| 776 |
spec: |
| 777 |
replica: 77 |
| 778 |
template: |
| 779 |
metadata: |
| 780 |
labels: |
| 781 |
new-label: new-value |
| 782 |
spec: |
| 783 |
containers: |
| 784 |
- image: nginx:1.7.9 |
| 785 |
name: nginx |
| 786 |
--- |
| 787 |
apiVersion: apps/v1 |
| 788 |
kind: MyKind |
| 789 |
metadata: |
| 790 |
label: |
| 791 |
old-label: old-value |
| 792 |
name: myDeploy |
| 793 |
spec: |
| 794 |
replica: 77 |
| 795 |
template: |
| 796 |
metadata: |
| 797 |
labels: |
| 798 |
old-label: old-value |
| 799 |
spec: |
| 800 |
containers: |
| 801 |
- image: nginx |
| 802 |
name: nginx |
| 803 |
`) |
| 804 |
} |
| 805 |
|
| 806 |
func TestPatchTransformerWithPatchDelete(t *testing.T) { |
| 807 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 808 |
PrepBuiltin("PatchTransformer") |
| 809 |
defer th.Reset() |
| 810 |
|
| 811 |
th.RunTransformerAndCheckResult(` |
| 812 |
apiVersion: builtin |
| 813 |
kind: PatchTransformer |
| 814 |
metadata: |
| 815 |
name: notImportantHere |
| 816 |
target: |
| 817 |
name: myDeploy |
| 818 |
kind: Deployment |
| 819 |
patch: |- |
| 820 |
apiVersion: apps/v1 |
| 821 |
metadata: |
| 822 |
name: myDeploy |
| 823 |
kind: Deployment |
| 824 |
$patch: delete |
| 825 |
`, someDeploymentResources, ` |
| 826 |
apiVersion: apps/v1 |
| 827 |
kind: Deployment |
| 828 |
metadata: |
| 829 |
labels: |
| 830 |
new-label: new-value |
| 831 |
name: yourDeploy |
| 832 |
spec: |
| 833 |
replica: 1 |
| 834 |
template: |
| 835 |
metadata: |
| 836 |
labels: |
| 837 |
new-label: new-value |
| 838 |
spec: |
| 839 |
containers: |
| 840 |
- image: nginx:1.7.9 |
| 841 |
name: nginx |
| 842 |
--- |
| 843 |
apiVersion: apps/v1 |
| 844 |
kind: MyKind |
| 845 |
metadata: |
| 846 |
label: |
| 847 |
old-label: old-value |
| 848 |
name: myDeploy |
| 849 |
spec: |
| 850 |
template: |
| 851 |
metadata: |
| 852 |
labels: |
| 853 |
old-label: old-value |
| 854 |
spec: |
| 855 |
containers: |
| 856 |
- image: nginx |
| 857 |
name: nginx |
| 858 |
`) |
| 859 |
} |
| 860 |
|
| 861 |
const anIngressResource = `apiVersion: networking.k8s.io/v1beta1 |
| 862 |
kind: Ingress |
| 863 |
metadata: |
| 864 |
name: my-ingress |
| 865 |
spec: |
| 866 |
rules: |
| 867 |
- host: foo.bar.com |
| 868 |
http: |
| 869 |
paths: |
| 870 |
- path: / |
| 871 |
backend: |
| 872 |
serviceName: homepage |
| 873 |
servicePort: 8888 |
| 874 |
- path: /api |
| 875 |
backend: |
| 876 |
serviceName: my-api |
| 877 |
servicePort: 7701 |
| 878 |
- path: /test |
| 879 |
backend: |
| 880 |
serviceName: hello |
| 881 |
servicePort: 7702 |
| 882 |
` |
| 883 |
|
| 884 |
func TestPatchTransformerJson(t *testing.T) { |
| 885 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 886 |
PrepBuiltin("PatchTransformer") |
| 887 |
defer th.Reset() |
| 888 |
th.WriteF("patch.json", `[ |
| 889 |
{"op": "replace", |
| 890 |
"path": "/spec/rules/0/host", |
| 891 |
"value": "foo.bar.io"}, |
| 892 |
|
| 893 |
{"op": "replace", |
| 894 |
"path": "/spec/rules/0/http/paths/0/backend/servicePort", |
| 895 |
"value": 80}, |
| 896 |
|
| 897 |
{"op": "add", |
| 898 |
"path": "/spec/rules/0/http/paths/1", |
| 899 |
"value": { "path": "/healthz", "backend": {"servicePort":7700} }} |
| 900 |
] |
| 901 |
`) |
| 902 |
|
| 903 |
th.RunTransformerAndCheckResult(` |
| 904 |
apiVersion: builtin |
| 905 |
kind: PatchTransformer |
| 906 |
metadata: |
| 907 |
name: notImportantHere |
| 908 |
path: patch.json |
| 909 |
target: |
| 910 |
kind: Ingress |
| 911 |
`, anIngressResource, ` |
| 912 |
apiVersion: networking.k8s.io/v1beta1 |
| 913 |
kind: Ingress |
| 914 |
metadata: |
| 915 |
name: my-ingress |
| 916 |
spec: |
| 917 |
rules: |
| 918 |
- host: foo.bar.io |
| 919 |
http: |
| 920 |
paths: |
| 921 |
- backend: |
| 922 |
serviceName: homepage |
| 923 |
servicePort: 80 |
| 924 |
path: / |
| 925 |
- backend: |
| 926 |
servicePort: 7700 |
| 927 |
path: /healthz |
| 928 |
- backend: |
| 929 |
serviceName: my-api |
| 930 |
servicePort: 7701 |
| 931 |
path: /api |
| 932 |
- backend: |
| 933 |
serviceName: hello |
| 934 |
servicePort: 7702 |
| 935 |
path: /test |
| 936 |
`) |
| 937 |
} |
| 938 |
|
| 939 |
func TestPatchTransformerJsonAsYaml(t *testing.T) { |
| 940 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 941 |
PrepBuiltin("PatchTransformer") |
| 942 |
defer th.Reset() |
| 943 |
th.WriteF("patch.yaml", ` |
| 944 |
- op: add |
| 945 |
path: /spec/rules/0/http/paths/- |
| 946 |
value: |
| 947 |
path: '/canada' |
| 948 |
backend: |
| 949 |
serviceName: hoser |
| 950 |
servicePort: 7703 |
| 951 |
`) |
| 952 |
|
| 953 |
th.RunTransformerAndCheckResult(` |
| 954 |
apiVersion: builtin |
| 955 |
kind: PatchTransformer |
| 956 |
metadata: |
| 957 |
name: notImportantHere |
| 958 |
path: patch.yaml |
| 959 |
target: |
| 960 |
kind: Ingress |
| 961 |
`, anIngressResource, ` |
| 962 |
apiVersion: networking.k8s.io/v1beta1 |
| 963 |
kind: Ingress |
| 964 |
metadata: |
| 965 |
name: my-ingress |
| 966 |
spec: |
| 967 |
rules: |
| 968 |
- host: foo.bar.com |
| 969 |
http: |
| 970 |
paths: |
| 971 |
- backend: |
| 972 |
serviceName: homepage |
| 973 |
servicePort: 8888 |
| 974 |
path: / |
| 975 |
- backend: |
| 976 |
serviceName: my-api |
| 977 |
servicePort: 7701 |
| 978 |
path: /api |
| 979 |
- backend: |
| 980 |
serviceName: hello |
| 981 |
servicePort: 7702 |
| 982 |
path: /test |
| 983 |
- backend: |
| 984 |
serviceName: hoser |
| 985 |
servicePort: 7703 |
| 986 |
path: /canada |
| 987 |
`) |
| 988 |
} |
| 989 |
|
| 990 |
// test for https://github.com/kubernetes-sigs/kustomize/issues/2767 |
| 991 |
// currently documents broken state. resulting ports: should have both |
| 992 |
// take-over-the-world and disappearing-act on 8080 |
| 993 |
func TestPatchTransformerSimilarArrays(t *testing.T) { |
| 994 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 995 |
PrepBuiltin("PatchTransformer") |
| 996 |
defer th.Reset() |
| 997 |
|
| 998 |
th.RunTransformerAndCheckResult(` |
| 999 |
apiVersion: builtin |
| 1000 |
kind: PatchTransformer |
| 1001 |
metadata: |
| 1002 |
name: test-transformer |
| 1003 |
patch: |- |
| 1004 |
apiVersion: apps/v1 |
| 1005 |
kind: Deployment |
| 1006 |
metadata: |
| 1007 |
name: test-transformer |
| 1008 |
labels: |
| 1009 |
test-transformer: did-my-job |
| 1010 |
target: |
| 1011 |
kind: Deployment |
| 1012 |
name: test-deployment |
| 1013 |
`, `apiVersion: apps/v1 |
| 1014 |
kind: Deployment |
| 1015 |
metadata: |
| 1016 |
name: test-deployment |
| 1017 |
spec: |
| 1018 |
template: |
| 1019 |
spec: |
| 1020 |
containers: |
| 1021 |
- image: test-image |
| 1022 |
name: test-deployment |
| 1023 |
ports: |
| 1024 |
- containerPort: 8080 |
| 1025 |
name: disappearing-act |
| 1026 |
protocol: TCP |
| 1027 |
- containerPort: 8080 |
| 1028 |
name: take-over-the-world |
| 1029 |
protocol: TCP |
| 1030 |
`, ` |
| 1031 |
apiVersion: apps/v1 |
| 1032 |
kind: Deployment |
| 1033 |
metadata: |
| 1034 |
labels: |
| 1035 |
test-transformer: did-my-job |
| 1036 |
name: test-deployment |
| 1037 |
spec: |
| 1038 |
template: |
| 1039 |
spec: |
| 1040 |
containers: |
| 1041 |
- image: test-image |
| 1042 |
name: test-deployment |
| 1043 |
ports: |
| 1044 |
- containerPort: 8080 |
| 1045 |
name: take-over-the-world |
| 1046 |
protocol: TCP |
| 1047 |
`) |
| 1048 |
} |
| 1049 |
|
| 1050 |
func TestPatchTransformerAnchor(t *testing.T) { |
| 1051 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 1052 |
PrepBuiltin("PatchTransformer") |
| 1053 |
defer th.Reset() |
| 1054 |
|
| 1055 |
th.RunTransformerAndCheckResult(` |
| 1056 |
apiVersion: builtin |
| 1057 |
kind: PatchTransformer |
| 1058 |
metadata: |
| 1059 |
name: test-transformer |
| 1060 |
patch: |- |
| 1061 |
apiVersion: apps/v1 |
| 1062 |
kind: Deployment |
| 1063 |
metadata: |
| 1064 |
name: test-deployment |
| 1065 |
spec: |
| 1066 |
selector: |
| 1067 |
matchLabels: |
| 1068 |
app: &name test-label |
| 1069 |
template: |
| 1070 |
metadata: |
| 1071 |
labels: |
| 1072 |
app: *name |
| 1073 |
target: |
| 1074 |
kind: Deployment |
| 1075 |
name: test-deployment |
| 1076 |
`, `apiVersion: apps/v1 |
| 1077 |
kind: Deployment |
| 1078 |
metadata: |
| 1079 |
name: &name test-deployment |
| 1080 |
spec: |
| 1081 |
selector: |
| 1082 |
matchLabels: |
| 1083 |
app: *name |
| 1084 |
template: |
| 1085 |
metadata: |
| 1086 |
labels: |
| 1087 |
app: *name |
| 1088 |
spec: |
| 1089 |
containers: |
| 1090 |
- image: test-image |
| 1091 |
name: *name |
| 1092 |
`, ` |
| 1093 |
apiVersion: apps/v1 |
| 1094 |
kind: Deployment |
| 1095 |
metadata: |
| 1096 |
name: test-deployment |
| 1097 |
spec: |
| 1098 |
selector: |
| 1099 |
matchLabels: |
| 1100 |
app: test-label |
| 1101 |
template: |
| 1102 |
metadata: |
| 1103 |
labels: |
| 1104 |
app: test-label |
| 1105 |
spec: |
| 1106 |
containers: |
| 1107 |
- image: test-image |
| 1108 |
name: test-deployment |
| 1109 |
`) |
| 1110 |
} |
| 1111 |
|
| 1112 |
func TestPatchTransformerPatchEmpty(t *testing.T) { |
| 1113 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 1114 |
PrepBuiltin("PatchTransformer") |
| 1115 |
defer th.Reset() |
| 1116 |
|
| 1117 |
th.WriteF("patch.yaml", ` |
| 1118 |
`) |
| 1119 |
|
| 1120 |
th.RunTransformerAndCheckError(` |
| 1121 |
apiVersion: builtin |
| 1122 |
kind: PatchTransformer |
| 1123 |
metadata: |
| 1124 |
name: notImportantHere |
| 1125 |
path: patch.yaml |
| 1126 |
target: |
| 1127 |
name: myDeploy |
| 1128 |
`, someDeploymentResources, func(t *testing.T, err error) { |
| 1129 |
t.Helper() |
| 1130 |
if err != nil { |
| 1131 |
t.Fatalf("unexpected error") |
| 1132 |
} |
| 1133 |
}) |
| 1134 |
} |
| 1135 |
|
| 1136 |
func TestPatchTransformerPatchEmptyOnlyComments(t *testing.T) { |
| 1137 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 1138 |
PrepBuiltin("PatchTransformer") |
| 1139 |
defer th.Reset() |
| 1140 |
|
| 1141 |
th.WriteF("patch.yaml", ` |
| 1142 |
# File with only comments |
| 1143 |
|
| 1144 |
# Is a virtually empty yaml |
| 1145 |
`) |
| 1146 |
|
| 1147 |
th.RunTransformerAndCheckError(` |
| 1148 |
apiVersion: builtin |
| 1149 |
kind: PatchTransformer |
| 1150 |
metadata: |
| 1151 |
name: notImportantHere |
| 1152 |
path: patch.yaml |
| 1153 |
target: |
| 1154 |
name: myDeploy |
| 1155 |
`, someDeploymentResources, func(t *testing.T, err error) { |
| 1156 |
t.Helper() |
| 1157 |
if err != nil { |
| 1158 |
t.Fatalf("unexpected error") |
| 1159 |
} |
| 1160 |
}) |
| 1161 |
} |