|
| 1 | +/* |
| 2 | +Copyright 2018 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package target |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "sigs.k8s.io/kustomize/pkg/internal/loadertest" |
| 23 | +) |
| 24 | + |
| 25 | +func writeSmallBase(t *testing.T, ldr loadertest.FakeLoader) { |
| 26 | + writeK(t, ldr, "/app/base", ` |
| 27 | +namePrefix: a- |
| 28 | +commonLabels: |
| 29 | + app: myApp |
| 30 | +resources: |
| 31 | +- deployment.yaml |
| 32 | +- service.yaml |
| 33 | +`) |
| 34 | + writeF(t, ldr, "/app/base/service.yaml", ` |
| 35 | +apiVersion: v1 |
| 36 | +kind: Service |
| 37 | +metadata: |
| 38 | + name: myService |
| 39 | +spec: |
| 40 | + selector: |
| 41 | + backend: bungie |
| 42 | + ports: |
| 43 | + - port: 7002 |
| 44 | +`) |
| 45 | + writeF(t, ldr, "/app/base/deployment.yaml", ` |
| 46 | +apiVersion: apps/v1 |
| 47 | +kind: Deployment |
| 48 | +metadata: |
| 49 | + name: myDeployment |
| 50 | +spec: |
| 51 | + template: |
| 52 | + metadata: |
| 53 | + labels: |
| 54 | + backend: awesome |
| 55 | + spec: |
| 56 | + containers: |
| 57 | + - name: whatever |
| 58 | + image: whatever |
| 59 | +`) |
| 60 | +} |
| 61 | + |
| 62 | +func TestSmallBase(t *testing.T) { |
| 63 | + ldr := loadertest.NewFakeLoader("/app/base") |
| 64 | + writeSmallBase(t, ldr) |
| 65 | + m, err := makeKustTarget(t, ldr).MakeCustomizedResMap() |
| 66 | + if err != nil { |
| 67 | + t.Fatalf("Err: %v", err) |
| 68 | + } |
| 69 | + if m == nil { |
| 70 | + t.Fatalf("Empty map.") |
| 71 | + } |
| 72 | + s, err := m.EncodeAsYaml() |
| 73 | + if err != nil { |
| 74 | + t.Fatalf("Err: %v", err) |
| 75 | + } |
| 76 | + assertExpectedEqualsActual(t, s, `apiVersion: v1 |
| 77 | +kind: Service |
| 78 | +metadata: |
| 79 | + labels: |
| 80 | + app: myApp |
| 81 | + name: a-myService |
| 82 | +spec: |
| 83 | + ports: |
| 84 | + - port: 7002 |
| 85 | + selector: |
| 86 | + app: myApp |
| 87 | + backend: bungie |
| 88 | +--- |
| 89 | +apiVersion: apps/v1 |
| 90 | +kind: Deployment |
| 91 | +metadata: |
| 92 | + labels: |
| 93 | + app: myApp |
| 94 | + name: a-myDeployment |
| 95 | +spec: |
| 96 | + selector: |
| 97 | + matchLabels: |
| 98 | + app: myApp |
| 99 | + template: |
| 100 | + metadata: |
| 101 | + labels: |
| 102 | + app: myApp |
| 103 | + backend: awesome |
| 104 | + spec: |
| 105 | + containers: |
| 106 | + - image: whatever |
| 107 | + name: whatever |
| 108 | +`) |
| 109 | +} |
| 110 | + |
| 111 | +func TestSmallOverlay(t *testing.T) { |
| 112 | + ldr := loadertest.NewFakeLoader("/app/overlay") |
| 113 | + writeSmallBase(t, ldr) |
| 114 | + writeK(t, ldr, "/app/overlay", ` |
| 115 | +namePrefix: b- |
| 116 | +commonLabels: |
| 117 | + env: prod |
| 118 | +bases: |
| 119 | +- ../base |
| 120 | +patchesStrategicMerge: |
| 121 | +- deployment/deployment.yaml |
| 122 | +imageTags: |
| 123 | +- name: whatever |
| 124 | + newTag: 1.8.0`) |
| 125 | + |
| 126 | + writeF(t, ldr, "/app/overlay/configmap/app.env", ` |
| 127 | +DB_USERNAME=admin |
| 128 | +DB_PASSWORD=somepw |
| 129 | +`) |
| 130 | + writeF(t, ldr, "/app/overlay/configmap/app-init.ini", ` |
| 131 | +FOO=bar |
| 132 | +BAR=baz |
| 133 | +`) |
| 134 | + writeF(t, ldr, "/app/overlay/deployment/deployment.yaml", ` |
| 135 | +apiVersion: apps/v1 |
| 136 | +kind: Deployment |
| 137 | +metadata: |
| 138 | + name: myDeployment |
| 139 | +spec: |
| 140 | + replicas: 1000 |
| 141 | +`) |
| 142 | + m, err := makeKustTarget(t, ldr).MakeCustomizedResMap() |
| 143 | + if err != nil { |
| 144 | + t.Fatalf("Err: %v", err) |
| 145 | + } |
| 146 | + s, err := m.EncodeAsYaml() |
| 147 | + if err != nil { |
| 148 | + t.Fatalf("Unexpected err: %v", err) |
| 149 | + } |
| 150 | + assertExpectedEqualsActual(t, s, `apiVersion: v1 |
| 151 | +kind: Service |
| 152 | +metadata: |
| 153 | + labels: |
| 154 | + app: myApp |
| 155 | + env: prod |
| 156 | + name: b-a-myService |
| 157 | +spec: |
| 158 | + ports: |
| 159 | + - port: 7002 |
| 160 | + selector: |
| 161 | + app: myApp |
| 162 | + backend: bungie |
| 163 | + env: prod |
| 164 | +--- |
| 165 | +apiVersion: apps/v1 |
| 166 | +kind: Deployment |
| 167 | +metadata: |
| 168 | + labels: |
| 169 | + app: myApp |
| 170 | + env: prod |
| 171 | + name: b-a-myDeployment |
| 172 | +spec: |
| 173 | + replicas: 1000 |
| 174 | + selector: |
| 175 | + matchLabels: |
| 176 | + app: myApp |
| 177 | + env: prod |
| 178 | + template: |
| 179 | + metadata: |
| 180 | + labels: |
| 181 | + app: myApp |
| 182 | + backend: awesome |
| 183 | + env: prod |
| 184 | + spec: |
| 185 | + containers: |
| 186 | + - image: whatever:1.8.0 |
| 187 | + name: whatever |
| 188 | +`) |
| 189 | +} |
0 commit comments