| 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 makeResourcesForPatchTest(th kusttest_test.Harness) { |
| 13 |
th.WriteF("base/deployment.yaml", ` |
| 14 |
apiVersion: apps/v1 |
| 15 |
kind: Deployment |
| 16 |
metadata: |
| 17 |
name: nginx |
| 18 |
labels: |
| 19 |
app: nginx |
| 20 |
spec: |
| 21 |
template: |
| 22 |
metadata: |
| 23 |
labels: |
| 24 |
app: nginx |
| 25 |
spec: |
| 26 |
containers: |
| 27 |
- name: nginx |
| 28 |
image: nginx |
| 29 |
volumeMounts: |
| 30 |
- name: nginx-persistent-storage |
| 31 |
mountPath: /tmp/ps |
| 32 |
volumes: |
| 33 |
- name: nginx-persistent-storage |
| 34 |
emptyDir: {} |
| 35 |
- configMap: |
| 36 |
name: configmap-in-base |
| 37 |
name: configmap-in-base |
| 38 |
`) |
| 39 |
} |
| 40 |
|
| 41 |
func TestStrategicMergePatchInline(t *testing.T) { |
| 42 |
th := kusttest_test.MakeHarness(t) |
| 43 |
makeResourcesForPatchTest(th) |
| 44 |
th.WriteK("base", ` |
| 45 |
resources: |
| 46 |
- deployment.yaml |
| 47 |
|
| 48 |
patchesStrategicMerge: |
| 49 |
- |- |
| 50 |
apiVersion: apps/v1 |
| 51 |
kind: Deployment |
| 52 |
metadata: |
| 53 |
name: nginx |
| 54 |
spec: |
| 55 |
template: |
| 56 |
spec: |
| 57 |
containers: |
| 58 |
- name: nginx |
| 59 |
image: image1 |
| 60 |
`) |
| 61 |
m := th.Run("base", th.MakeDefaultOptions()) |
| 62 |
th.AssertActualEqualsExpected(m, ` |
| 63 |
apiVersion: apps/v1 |
| 64 |
kind: Deployment |
| 65 |
metadata: |
| 66 |
labels: |
| 67 |
app: nginx |
| 68 |
name: nginx |
| 69 |
spec: |
| 70 |
template: |
| 71 |
metadata: |
| 72 |
labels: |
| 73 |
app: nginx |
| 74 |
spec: |
| 75 |
containers: |
| 76 |
- image: image1 |
| 77 |
name: nginx |
| 78 |
volumeMounts: |
| 79 |
- mountPath: /tmp/ps |
| 80 |
name: nginx-persistent-storage |
| 81 |
volumes: |
| 82 |
- emptyDir: {} |
| 83 |
name: nginx-persistent-storage |
| 84 |
- configMap: |
| 85 |
name: configmap-in-base |
| 86 |
name: configmap-in-base |
| 87 |
`) |
| 88 |
} |
| 89 |
|
| 90 |
func TestJSONPatchInline(t *testing.T) { |
| 91 |
th := kusttest_test.MakeHarness(t) |
| 92 |
makeResourcesForPatchTest(th) |
| 93 |
th.WriteK("base", ` |
| 94 |
resources: |
| 95 |
- deployment.yaml |
| 96 |
|
| 97 |
patchesJson6902: |
| 98 |
- target: |
| 99 |
group: apps |
| 100 |
version: v1 |
| 101 |
kind: Deployment |
| 102 |
name: nginx |
| 103 |
patch: |- |
| 104 |
- op: replace |
| 105 |
path: /spec/template/spec/containers/0/image |
| 106 |
value: image1 |
| 107 |
`) |
| 108 |
m := th.Run("base", th.MakeDefaultOptions()) |
| 109 |
th.AssertActualEqualsExpected(m, ` |
| 110 |
apiVersion: apps/v1 |
| 111 |
kind: Deployment |
| 112 |
metadata: |
| 113 |
labels: |
| 114 |
app: nginx |
| 115 |
name: nginx |
| 116 |
spec: |
| 117 |
template: |
| 118 |
metadata: |
| 119 |
labels: |
| 120 |
app: nginx |
| 121 |
spec: |
| 122 |
containers: |
| 123 |
- image: image1 |
| 124 |
name: nginx |
| 125 |
volumeMounts: |
| 126 |
- mountPath: /tmp/ps |
| 127 |
name: nginx-persistent-storage |
| 128 |
volumes: |
| 129 |
- emptyDir: {} |
| 130 |
name: nginx-persistent-storage |
| 131 |
- configMap: |
| 132 |
name: configmap-in-base |
| 133 |
name: configmap-in-base |
| 134 |
`) |
| 135 |
} |
| 136 |
|
| 137 |
func TestExtendedPatchInlineJSON(t *testing.T) { |
| 138 |
th := kusttest_test.MakeHarness(t) |
| 139 |
makeResourcesForPatchTest(th) |
| 140 |
th.WriteK("base", ` |
| 141 |
resources: |
| 142 |
- deployment.yaml |
| 143 |
|
| 144 |
patches: |
| 145 |
- target: |
| 146 |
kind: Deployment |
| 147 |
name: nginx |
| 148 |
patch: |- |
| 149 |
- op: replace |
| 150 |
path: /spec/template/spec/containers/0/image |
| 151 |
value: image1 |
| 152 |
`) |
| 153 |
m := th.Run("base", th.MakeDefaultOptions()) |
| 154 |
th.AssertActualEqualsExpected(m, ` |
| 155 |
apiVersion: apps/v1 |
| 156 |
kind: Deployment |
| 157 |
metadata: |
| 158 |
labels: |
| 159 |
app: nginx |
| 160 |
name: nginx |
| 161 |
spec: |
| 162 |
template: |
| 163 |
metadata: |
| 164 |
labels: |
| 165 |
app: nginx |
| 166 |
spec: |
| 167 |
containers: |
| 168 |
- image: image1 |
| 169 |
name: nginx |
| 170 |
volumeMounts: |
| 171 |
- mountPath: /tmp/ps |
| 172 |
name: nginx-persistent-storage |
| 173 |
volumes: |
| 174 |
- emptyDir: {} |
| 175 |
name: nginx-persistent-storage |
| 176 |
- configMap: |
| 177 |
name: configmap-in-base |
| 178 |
name: configmap-in-base |
| 179 |
`) |
| 180 |
} |
| 181 |
|
| 182 |
func TestExtendedPatchInlineYAML(t *testing.T) { |
| 183 |
th := kusttest_test.MakeHarness(t) |
| 184 |
makeResourcesForPatchTest(th) |
| 185 |
th.WriteK("base", ` |
| 186 |
resources: |
| 187 |
- deployment.yaml |
| 188 |
|
| 189 |
patches: |
| 190 |
- target: |
| 191 |
kind: Deployment |
| 192 |
name: nginx |
| 193 |
patch: |- |
| 194 |
apiVersion: apps/v1 |
| 195 |
kind: Deployment |
| 196 |
metadata: |
| 197 |
name: nginx |
| 198 |
spec: |
| 199 |
template: |
| 200 |
spec: |
| 201 |
containers: |
| 202 |
- name: nginx |
| 203 |
image: image1 |
| 204 |
`) |
| 205 |
m := th.Run("base", th.MakeDefaultOptions()) |
| 206 |
th.AssertActualEqualsExpected(m, ` |
| 207 |
apiVersion: apps/v1 |
| 208 |
kind: Deployment |
| 209 |
metadata: |
| 210 |
labels: |
| 211 |
app: nginx |
| 212 |
name: nginx |
| 213 |
spec: |
| 214 |
template: |
| 215 |
metadata: |
| 216 |
labels: |
| 217 |
app: nginx |
| 218 |
spec: |
| 219 |
containers: |
| 220 |
- image: image1 |
| 221 |
name: nginx |
| 222 |
volumeMounts: |
| 223 |
- mountPath: /tmp/ps |
| 224 |
name: nginx-persistent-storage |
| 225 |
volumes: |
| 226 |
- emptyDir: {} |
| 227 |
name: nginx-persistent-storage |
| 228 |
- configMap: |
| 229 |
name: configmap-in-base |
| 230 |
name: configmap-in-base |
| 231 |
`) |
| 232 |
} |
| 233 |
|
| 234 |
func TestPathWithCronJobV1(t *testing.T) { |
| 235 |
th := kusttest_test.MakeHarness(t) |
| 236 |
th.WriteK(".", ` |
| 237 |
resources: |
| 238 |
- resources.yaml |
| 239 |
patches: |
| 240 |
- patch: | |
| 241 |
apiVersion: batch/v1 |
| 242 |
kind: CronJob |
| 243 |
metadata: |
| 244 |
name: test |
| 245 |
spec: |
| 246 |
jobTemplate: |
| 247 |
spec: |
| 248 |
template: |
| 249 |
spec: |
| 250 |
containers: |
| 251 |
- name: test |
| 252 |
env: |
| 253 |
- name: ENV_NEW |
| 254 |
value: val_new |
| 255 |
target: |
| 256 |
kind: CronJob |
| 257 |
name: test |
| 258 |
`) |
| 259 |
th.WriteF("resources.yaml", ` |
| 260 |
apiVersion: batch/v1 |
| 261 |
kind: CronJob |
| 262 |
metadata: |
| 263 |
name: test |
| 264 |
spec: |
| 265 |
schedule: "5 10 * * 1" |
| 266 |
concurrencyPolicy: Forbid |
| 267 |
jobTemplate: |
| 268 |
spec: |
| 269 |
backoffLimit: 3 |
| 270 |
template: |
| 271 |
spec: |
| 272 |
restartPolicy: Never |
| 273 |
containers: |
| 274 |
- name: test |
| 275 |
image: bash |
| 276 |
command: |
| 277 |
- /bin/sh |
| 278 |
- -c |
| 279 |
- echo "test" |
| 280 |
env: |
| 281 |
- name: ENV1 |
| 282 |
value: val1 |
| 283 |
- name: ENV2 |
| 284 |
value: val2 |
| 285 |
- name: ENV3 |
| 286 |
value: val3`) |
| 287 |
m := th.Run(".", th.MakeDefaultOptions()) |
| 288 |
th.AssertActualEqualsExpected(m, ` |
| 289 |
apiVersion: batch/v1 |
| 290 |
kind: CronJob |
| 291 |
metadata: |
| 292 |
name: test |
| 293 |
spec: |
| 294 |
concurrencyPolicy: Forbid |
| 295 |
jobTemplate: |
| 296 |
spec: |
| 297 |
backoffLimit: 3 |
| 298 |
template: |
| 299 |
spec: |
| 300 |
containers: |
| 301 |
- command: |
| 302 |
- /bin/sh |
| 303 |
- -c |
| 304 |
- echo "test" |
| 305 |
env: |
| 306 |
- name: ENV_NEW |
| 307 |
value: val_new |
| 308 |
- name: ENV1 |
| 309 |
value: val1 |
| 310 |
- name: ENV2 |
| 311 |
value: val2 |
| 312 |
- name: ENV3 |
| 313 |
value: val3 |
| 314 |
image: bash |
| 315 |
name: test |
| 316 |
restartPolicy: Never |
| 317 |
schedule: 5 10 * * 1 |
| 318 |
`) |
| 319 |
} |