| 1 |
// Copyright 2019 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 makeCommonFileForMergeEnvFromTest(th kusttest_test.Harness) { |
| 13 |
th.WriteF("deployment.yaml", ` |
| 14 |
apiVersion: apps/v1 |
| 15 |
kind: Deployment |
| 16 |
metadata: |
| 17 |
name: nginx |
| 18 |
spec: |
| 19 |
template: |
| 20 |
spec: |
| 21 |
containers: |
| 22 |
- name: nginx |
| 23 |
image: image1 |
| 24 |
envFrom: |
| 25 |
- configMapRef: |
| 26 |
name: some-config |
| 27 |
- configMapRef: |
| 28 |
name: more-config |
| 29 |
`) |
| 30 |
} |
| 31 |
|
| 32 |
// When patching, `envFrom` should merge the list instead of replacing it. |
| 33 |
func TestMergeEnvFrom(t *testing.T) { |
| 34 |
th := kusttest_test.MakeHarness(t) |
| 35 |
makeCommonFileForMergeEnvFromTest(th) |
| 36 |
th.WriteK(".", ` |
| 37 |
resources: |
| 38 |
- deployment.yaml |
| 39 |
|
| 40 |
patchesStrategicMerge: |
| 41 |
- |- |
| 42 |
apiVersion: apps/v1 |
| 43 |
kind: Deployment |
| 44 |
metadata: |
| 45 |
name: nginx |
| 46 |
spec: |
| 47 |
template: |
| 48 |
spec: |
| 49 |
containers: |
| 50 |
- name: nginx |
| 51 |
envFrom: |
| 52 |
- configMapRef: |
| 53 |
name: another-config |
| 54 |
`) |
| 55 |
m := th.Run(".", th.MakeDefaultOptions()) |
| 56 |
th.AssertActualEqualsExpected(m, ` |
| 57 |
apiVersion: apps/v1 |
| 58 |
kind: Deployment |
| 59 |
metadata: |
| 60 |
name: nginx |
| 61 |
spec: |
| 62 |
template: |
| 63 |
spec: |
| 64 |
containers: |
| 65 |
- envFrom: |
| 66 |
- configMapRef: |
| 67 |
name: another-config |
| 68 |
image: image1 |
| 69 |
name: nginx |
| 70 |
`) |
| 71 |
} |
| 72 |
|
| 73 |
func TestMergeEnvFromViaJsonInline(t *testing.T) { |
| 74 |
th := kusttest_test.MakeHarness(t) |
| 75 |
makeCommonFileForMergeEnvFromTest(th) |
| 76 |
th.WriteK(".", ` |
| 77 |
resources: |
| 78 |
- deployment.yaml |
| 79 |
patches: |
| 80 |
- target: |
| 81 |
kind: Deployment |
| 82 |
name: nginx |
| 83 |
patch: |- |
| 84 |
- op: add |
| 85 |
path: /spec/template/spec/containers/0/envFrom/- |
| 86 |
value: |
| 87 |
configMapRef: |
| 88 |
name: another-config |
| 89 |
`) |
| 90 |
m := th.Run(".", th.MakeDefaultOptions()) |
| 91 |
th.AssertActualEqualsExpected(m, ` |
| 92 |
apiVersion: apps/v1 |
| 93 |
kind: Deployment |
| 94 |
metadata: |
| 95 |
name: nginx |
| 96 |
spec: |
| 97 |
template: |
| 98 |
spec: |
| 99 |
containers: |
| 100 |
- envFrom: |
| 101 |
- configMapRef: |
| 102 |
name: some-config |
| 103 |
- configMapRef: |
| 104 |
name: more-config |
| 105 |
- configMapRef: |
| 106 |
name: another-config |
| 107 |
image: image1 |
| 108 |
name: nginx |
| 109 |
`) |
| 110 |
} |