| 1 |
// Copyright 2022 The Kubernetes Authors. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package krusty_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
|
| 9 |
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest" |
| 10 |
) |
| 11 |
|
| 12 |
func TestPatchDeleteOfNotExistingAttributesShouldNotAddExtraElements(t *testing.T) { |
| 13 |
th := kusttest_test.MakeEnhancedHarness(t) |
| 14 |
defer th.Reset() |
| 15 |
|
| 16 |
th.WriteF("resource.yaml", ` |
| 17 |
apiVersion: apps/v1 |
| 18 |
kind: Deployment |
| 19 |
metadata: |
| 20 |
name: whatever |
| 21 |
spec: |
| 22 |
template: |
| 23 |
spec: |
| 24 |
containers: |
| 25 |
- env: |
| 26 |
- name: EXISTING |
| 27 |
value: EXISTING_VALUE |
| 28 |
- name: FOR_REMOVAL |
| 29 |
value: FOR_REMOVAL_VALUE |
| 30 |
name: whatever |
| 31 |
image: helloworld |
| 32 |
`) |
| 33 |
th.WriteF("patch.yaml", ` |
| 34 |
apiVersion: apps/v1 |
| 35 |
kind: Deployment |
| 36 |
metadata: |
| 37 |
name: whatever |
| 38 |
spec: |
| 39 |
template: |
| 40 |
spec: |
| 41 |
containers: |
| 42 |
- name: whatever |
| 43 |
env: |
| 44 |
- name: FOR_REMOVAL |
| 45 |
$patch: delete |
| 46 |
- name: NOT_EXISTING_FOR_REMOVAL |
| 47 |
$patch: delete |
| 48 |
`) |
| 49 |
th.WriteK(".", ` |
| 50 |
resources: |
| 51 |
- resource.yaml |
| 52 |
patches: |
| 53 |
- path: patch.yaml |
| 54 |
target: |
| 55 |
kind: Deployment |
| 56 |
`) |
| 57 |
|
| 58 |
// It's expected that removal of not existing elements should not introduce extra values, |
| 59 |
// as a patch can be applied to multiple resources, not all of them can have all the elements being deleted. |
| 60 |
expected := ` |
| 61 |
apiVersion: apps/v1 |
| 62 |
kind: Deployment |
| 63 |
metadata: |
| 64 |
name: whatever |
| 65 |
spec: |
| 66 |
template: |
| 67 |
spec: |
| 68 |
containers: |
| 69 |
- env: |
| 70 |
- name: EXISTING |
| 71 |
value: EXISTING_VALUE |
| 72 |
image: helloworld |
| 73 |
name: whatever |
| 74 |
` |
| 75 |
m := th.Run(".", th.MakeDefaultOptions()) |
| 76 |
th.AssertActualEqualsExpected(m, expected) |
| 77 |
} |