| 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 |
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest" |
| 11 |
) |
| 12 |
|
| 13 |
const target = ` |
| 14 |
apiVersion: apps/v1 |
| 15 |
metadata: |
| 16 |
name: myDeploy |
| 17 |
kind: Deployment |
| 18 |
spec: |
| 19 |
replica: 2 |
| 20 |
template: |
| 21 |
metadata: |
| 22 |
labels: |
| 23 |
old-label: old-value |
| 24 |
spec: |
| 25 |
containers: |
| 26 |
- name: nginx |
| 27 |
image: nginx |
| 28 |
` |
| 29 |
|
| 30 |
func TestPatchJson6902TransformerMissingFile(t *testing.T) { |
| 31 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 32 |
PrepBuiltin("PatchJson6902Transformer") |
| 33 |
defer th.Reset() |
| 34 |
|
| 35 |
th.RunTransformerAndCheckError(` |
| 36 |
apiVersion: builtin |
| 37 |
kind: PatchJson6902Transformer |
| 38 |
metadata: |
| 39 |
name: notImportantHere |
| 40 |
target: |
| 41 |
group: apps |
| 42 |
version: v1 |
| 43 |
kind: Deployment |
| 44 |
name: myDeploy |
| 45 |
path: jsonpatch.json |
| 46 |
`, target, func(t *testing.T, err error) { |
| 47 |
if err == nil { |
| 48 |
t.Fatalf("expected error") |
| 49 |
} |
| 50 |
if !strings.Contains(err.Error(), "'/jsonpatch.json' doesn't exist") { |
| 51 |
t.Fatalf("unexpected err: %v", err) |
| 52 |
} |
| 53 |
}) |
| 54 |
} |
| 55 |
|
| 56 |
func TestBadPatchJson6902Transformer(t *testing.T) { |
| 57 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 58 |
PrepBuiltin("PatchJson6902Transformer") |
| 59 |
defer th.Reset() |
| 60 |
|
| 61 |
th.RunTransformerAndCheckError(` |
| 62 |
apiVersion: builtin |
| 63 |
kind: PatchJson6902Transformer |
| 64 |
metadata: |
| 65 |
name: notImportantHere |
| 66 |
target: |
| 67 |
group: apps |
| 68 |
version: v1 |
| 69 |
kind: Deployment |
| 70 |
name: myDeploy |
| 71 |
jsonOp: 'thisIsNotAPatch' |
| 72 |
`, target, func(t *testing.T, err error) { |
| 73 |
if err == nil { |
| 74 |
t.Fatalf("expected error") |
| 75 |
} |
| 76 |
if !strings.Contains(err.Error(), "cannot unmarshal string into Go value of type jsonpatch") { |
| 77 |
t.Fatalf("unexpected err: %v", err) |
| 78 |
} |
| 79 |
}) |
| 80 |
} |
| 81 |
|
| 82 |
func TestBothEmptyJson6902Transformer(t *testing.T) { |
| 83 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 84 |
PrepBuiltin("PatchJson6902Transformer") |
| 85 |
defer th.Reset() |
| 86 |
|
| 87 |
th.RunTransformerAndCheckError(` |
| 88 |
apiVersion: builtin |
| 89 |
kind: PatchJson6902Transformer |
| 90 |
metadata: |
| 91 |
name: notImportantHere |
| 92 |
target: |
| 93 |
group: apps |
| 94 |
version: v1 |
| 95 |
kind: Deployment |
| 96 |
name: myDeploy |
| 97 |
`, target, func(t *testing.T, err error) { |
| 98 |
if err == nil { |
| 99 |
t.Fatalf("expected error") |
| 100 |
} |
| 101 |
if !strings.Contains(err.Error(), "empty file path and empty jsonOp") { |
| 102 |
t.Fatalf("unexpected err: %v", err) |
| 103 |
} |
| 104 |
}) |
| 105 |
} |
| 106 |
|
| 107 |
func TestBothSpecifiedJson6902Transformer(t *testing.T) { |
| 108 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 109 |
PrepBuiltin("PatchJson6902Transformer") |
| 110 |
defer th.Reset() |
| 111 |
|
| 112 |
th.WriteF("/app/jsonpatch.json", `[ |
| 113 |
{"op": "replace", "path": "/spec/template/spec/containers/0/name", "value": "my-nginx"}, |
| 114 |
{"op": "add", "path": "/spec/replica", "value": "999"}, |
| 115 |
{"op": "add", "path": "/spec/template/spec/containers/0/command", "value": ["arg1", "arg2", "arg3"]} |
| 116 |
]`) |
| 117 |
|
| 118 |
th.RunTransformerAndCheckError(` |
| 119 |
apiVersion: builtin |
| 120 |
kind: PatchJson6902Transformer |
| 121 |
metadata: |
| 122 |
name: notImportantHere |
| 123 |
target: |
| 124 |
group: apps |
| 125 |
version: v1 |
| 126 |
kind: Deployment |
| 127 |
name: myDeploy |
| 128 |
path: jsonpatch.json |
| 129 |
jsonOp: '[{"op": "add", "path": "/spec/template/spec/dnsPolicy", "value": "ClusterFirst"}]' |
| 130 |
`, target, func(t *testing.T, err error) { |
| 131 |
if err == nil { |
| 132 |
t.Fatalf("expected error") |
| 133 |
} |
| 134 |
if !strings.Contains(err.Error(), "must specify a file path or jsonOp, not both") { |
| 135 |
t.Fatalf("unexpected err: %v", err) |
| 136 |
} |
| 137 |
}) |
| 138 |
} |
| 139 |
|
| 140 |
func TestPatchJson6902TransformerFromJsonFile(t *testing.T) { |
| 141 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 142 |
PrepBuiltin("PatchJson6902Transformer") |
| 143 |
defer th.Reset() |
| 144 |
|
| 145 |
th.WriteF("jsonpatch.json", `[ |
| 146 |
{"op": "replace", "path": "/spec/template/spec/containers/0/name", "value": "my-nginx"}, |
| 147 |
{"op": "add", "path": "/spec/replica", "value": "999"}, |
| 148 |
{"op": "add", "path": "/spec/template/spec/containers/0/command", "value": ["arg1", "arg2", "arg3"]} |
| 149 |
]`) |
| 150 |
|
| 151 |
th.RunTransformerAndCheckResult(` |
| 152 |
apiVersion: builtin |
| 153 |
kind: PatchJson6902Transformer |
| 154 |
metadata: |
| 155 |
name: notImportantHere |
| 156 |
target: |
| 157 |
group: apps |
| 158 |
version: v1 |
| 159 |
kind: Deployment |
| 160 |
name: myDeploy |
| 161 |
path: jsonpatch.json |
| 162 |
`, |
| 163 |
target, |
| 164 |
` |
| 165 |
apiVersion: apps/v1 |
| 166 |
kind: Deployment |
| 167 |
metadata: |
| 168 |
name: myDeploy |
| 169 |
spec: |
| 170 |
replica: "999" |
| 171 |
template: |
| 172 |
metadata: |
| 173 |
labels: |
| 174 |
old-label: old-value |
| 175 |
spec: |
| 176 |
containers: |
| 177 |
- command: |
| 178 |
- arg1 |
| 179 |
- arg2 |
| 180 |
- arg3 |
| 181 |
image: nginx |
| 182 |
name: my-nginx |
| 183 |
`) |
| 184 |
} |
| 185 |
|
| 186 |
func TestPatchJson6902TransformerFromYamlFile(t *testing.T) { |
| 187 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 188 |
PrepBuiltin("PatchJson6902Transformer") |
| 189 |
defer th.Reset() |
| 190 |
|
| 191 |
th.WriteF("jsonpatch.json", ` |
| 192 |
- op: add |
| 193 |
path: /spec/template/spec/containers/0/command |
| 194 |
value: ["arg1", "arg2", "arg3"] |
| 195 |
`) |
| 196 |
|
| 197 |
th.RunTransformerAndCheckResult(` |
| 198 |
apiVersion: builtin |
| 199 |
kind: PatchJson6902Transformer |
| 200 |
metadata: |
| 201 |
name: notImportantHere |
| 202 |
target: |
| 203 |
group: apps |
| 204 |
version: v1 |
| 205 |
kind: Deployment |
| 206 |
name: myDeploy |
| 207 |
path: jsonpatch.json |
| 208 |
`, |
| 209 |
target, |
| 210 |
` |
| 211 |
apiVersion: apps/v1 |
| 212 |
kind: Deployment |
| 213 |
metadata: |
| 214 |
name: myDeploy |
| 215 |
spec: |
| 216 |
replica: 2 |
| 217 |
template: |
| 218 |
metadata: |
| 219 |
labels: |
| 220 |
old-label: old-value |
| 221 |
spec: |
| 222 |
containers: |
| 223 |
- command: |
| 224 |
- arg1 |
| 225 |
- arg2 |
| 226 |
- arg3 |
| 227 |
image: nginx |
| 228 |
name: nginx |
| 229 |
`) |
| 230 |
} |
| 231 |
|
| 232 |
func TestPatchJson6902TransformerWithInlineJSON(t *testing.T) { |
| 233 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 234 |
PrepBuiltin("PatchJson6902Transformer") |
| 235 |
defer th.Reset() |
| 236 |
|
| 237 |
th.RunTransformerAndCheckResult(` |
| 238 |
apiVersion: builtin |
| 239 |
kind: PatchJson6902Transformer |
| 240 |
metadata: |
| 241 |
name: notImportantHere |
| 242 |
target: |
| 243 |
group: apps |
| 244 |
version: v1 |
| 245 |
kind: Deployment |
| 246 |
name: myDeploy |
| 247 |
jsonOp: '[{"op": "add", "path": "/spec/template/spec/dnsPolicy", "value": "ClusterFirst"}]' |
| 248 |
`, |
| 249 |
target, |
| 250 |
` |
| 251 |
apiVersion: apps/v1 |
| 252 |
kind: Deployment |
| 253 |
metadata: |
| 254 |
name: myDeploy |
| 255 |
spec: |
| 256 |
replica: 2 |
| 257 |
template: |
| 258 |
metadata: |
| 259 |
labels: |
| 260 |
old-label: old-value |
| 261 |
spec: |
| 262 |
containers: |
| 263 |
- image: nginx |
| 264 |
name: nginx |
| 265 |
dnsPolicy: ClusterFirst |
| 266 |
`) |
| 267 |
} |
| 268 |
|
| 269 |
func TestPatchJson6902TransformerWithInlineYAML(t *testing.T) { |
| 270 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 271 |
PrepBuiltin("PatchJson6902Transformer") |
| 272 |
defer th.Reset() |
| 273 |
|
| 274 |
th.RunTransformerAndCheckResult(` |
| 275 |
apiVersion: builtin |
| 276 |
kind: PatchJson6902Transformer |
| 277 |
metadata: |
| 278 |
name: notImportantHere |
| 279 |
target: |
| 280 |
group: apps |
| 281 |
version: v1 |
| 282 |
kind: Deployment |
| 283 |
name: myDeploy |
| 284 |
jsonOp: |- |
| 285 |
- op: add |
| 286 |
path: /spec/template/spec/dnsPolicy |
| 287 |
value: ClusterFirst |
| 288 |
`, |
| 289 |
target, |
| 290 |
` |
| 291 |
apiVersion: apps/v1 |
| 292 |
kind: Deployment |
| 293 |
metadata: |
| 294 |
name: myDeploy |
| 295 |
spec: |
| 296 |
replica: 2 |
| 297 |
template: |
| 298 |
metadata: |
| 299 |
labels: |
| 300 |
old-label: old-value |
| 301 |
spec: |
| 302 |
containers: |
| 303 |
- image: nginx |
| 304 |
name: nginx |
| 305 |
dnsPolicy: ClusterFirst |
| 306 |
`) |
| 307 |
} |