| 1 |
// Copyright 2020 The Kubernetes Authors. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package labels |
| 5 |
|
| 6 |
import ( |
| 7 |
"strings" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/stretchr/testify/assert" |
| 11 |
filtertest_test "sigs.k8s.io/kustomize/api/testutils/filtertest" |
| 12 |
"sigs.k8s.io/kustomize/api/types" |
| 13 |
"sigs.k8s.io/kustomize/kyaml/resid" |
| 14 |
"sigs.k8s.io/kustomize/kyaml/yaml" |
| 15 |
) |
| 16 |
|
| 17 |
func TestLabels_Filter(t *testing.T) { |
| 18 |
mutationTrackerStub := filtertest_test.MutationTrackerStub{} |
| 19 |
testCases := map[string]struct { |
| 20 |
input string |
| 21 |
expectedOutput string |
| 22 |
filter Filter |
| 23 |
setEntryCallback func(key, value, tag string, node *yaml.RNode) |
| 24 |
expectedSetEntryArgs []filtertest_test.SetValueArg |
| 25 |
}{ |
| 26 |
"add": { |
| 27 |
input: ` |
| 28 |
apiVersion: example.com/v1 |
| 29 |
kind: Foo |
| 30 |
metadata: |
| 31 |
name: instance |
| 32 |
labels: |
| 33 |
hero: batman |
| 34 |
fiend: riddler |
| 35 |
`, |
| 36 |
expectedOutput: ` |
| 37 |
apiVersion: example.com/v1 |
| 38 |
kind: Foo |
| 39 |
metadata: |
| 40 |
name: instance |
| 41 |
labels: |
| 42 |
hero: batman |
| 43 |
fiend: riddler |
| 44 |
auto: ford |
| 45 |
bean: cannellini |
| 46 |
clown: emmett kelley |
| 47 |
dragon: smaug |
| 48 |
`, |
| 49 |
filter: Filter{ |
| 50 |
Labels: labelMap{ |
| 51 |
"clown": "emmett kelley", |
| 52 |
"auto": "ford", |
| 53 |
"dragon": "smaug", |
| 54 |
"bean": "cannellini", |
| 55 |
}, |
| 56 |
FsSlice: []types.FieldSpec{ |
| 57 |
{ |
| 58 |
Path: "metadata/labels", |
| 59 |
CreateIfNotPresent: true, |
| 60 |
}, |
| 61 |
}, |
| 62 |
}, |
| 63 |
}, |
| 64 |
"update": { |
| 65 |
input: ` |
| 66 |
apiVersion: example.com/v1 |
| 67 |
kind: Foo |
| 68 |
metadata: |
| 69 |
name: instance |
| 70 |
labels: |
| 71 |
hero: batman |
| 72 |
fiend: riddler |
| 73 |
`, |
| 74 |
expectedOutput: ` |
| 75 |
apiVersion: example.com/v1 |
| 76 |
kind: Foo |
| 77 |
metadata: |
| 78 |
name: instance |
| 79 |
labels: |
| 80 |
hero: superman |
| 81 |
fiend: luthor |
| 82 |
bean: cannellini |
| 83 |
clown: emmett kelley |
| 84 |
`, |
| 85 |
filter: Filter{ |
| 86 |
Labels: labelMap{ |
| 87 |
"clown": "emmett kelley", |
| 88 |
"hero": "superman", |
| 89 |
"fiend": "luthor", |
| 90 |
"bean": "cannellini", |
| 91 |
}, FsSlice: []types.FieldSpec{ |
| 92 |
{ |
| 93 |
Path: "metadata/labels", |
| 94 |
CreateIfNotPresent: true, |
| 95 |
}, |
| 96 |
}, |
| 97 |
}, |
| 98 |
}, |
| 99 |
|
| 100 |
"data-fieldspecs": { |
| 101 |
input: ` |
| 102 |
apiVersion: example.com/v1 |
| 103 |
kind: Foo |
| 104 |
metadata: |
| 105 |
name: instance |
| 106 |
--- |
| 107 |
apiVersion: example.com/v1 |
| 108 |
kind: Bar |
| 109 |
metadata: |
| 110 |
name: instance |
| 111 |
`, |
| 112 |
expectedOutput: ` |
| 113 |
apiVersion: example.com/v1 |
| 114 |
kind: Foo |
| 115 |
metadata: |
| 116 |
name: instance |
| 117 |
labels: |
| 118 |
sleater: kinney |
| 119 |
a: |
| 120 |
b: |
| 121 |
sleater: kinney |
| 122 |
--- |
| 123 |
apiVersion: example.com/v1 |
| 124 |
kind: Bar |
| 125 |
metadata: |
| 126 |
name: instance |
| 127 |
labels: |
| 128 |
sleater: kinney |
| 129 |
a: |
| 130 |
b: |
| 131 |
sleater: kinney |
| 132 |
`, |
| 133 |
filter: Filter{ |
| 134 |
Labels: labelMap{ |
| 135 |
"sleater": "kinney", |
| 136 |
}, |
| 137 |
FsSlice: []types.FieldSpec{ |
| 138 |
{ |
| 139 |
Path: "metadata/labels", |
| 140 |
CreateIfNotPresent: true, |
| 141 |
}, |
| 142 |
{ |
| 143 |
Path: "a/b", |
| 144 |
CreateIfNotPresent: true, |
| 145 |
}, |
| 146 |
}, |
| 147 |
}, |
| 148 |
}, |
| 149 |
|
| 150 |
"fieldSpecWithKind": { |
| 151 |
input: ` |
| 152 |
apiVersion: example.com/v1 |
| 153 |
kind: Foo |
| 154 |
metadata: |
| 155 |
name: instance |
| 156 |
--- |
| 157 |
apiVersion: example.com/v2 |
| 158 |
kind: Bar |
| 159 |
metadata: |
| 160 |
name: instance |
| 161 |
`, |
| 162 |
expectedOutput: ` |
| 163 |
apiVersion: example.com/v1 |
| 164 |
kind: Foo |
| 165 |
metadata: |
| 166 |
name: instance |
| 167 |
labels: |
| 168 |
cheese: cheddar |
| 169 |
--- |
| 170 |
apiVersion: example.com/v2 |
| 171 |
kind: Bar |
| 172 |
metadata: |
| 173 |
name: instance |
| 174 |
labels: |
| 175 |
cheese: cheddar |
| 176 |
a: |
| 177 |
b: |
| 178 |
cheese: cheddar |
| 179 |
`, |
| 180 |
filter: Filter{ |
| 181 |
Labels: labelMap{ |
| 182 |
"cheese": "cheddar", |
| 183 |
}, |
| 184 |
FsSlice: []types.FieldSpec{ |
| 185 |
{ |
| 186 |
Path: "metadata/labels", |
| 187 |
CreateIfNotPresent: true, |
| 188 |
}, |
| 189 |
{ |
| 190 |
Gvk: resid.Gvk{ |
| 191 |
Kind: "Bar", |
| 192 |
}, |
| 193 |
Path: "a/b", |
| 194 |
CreateIfNotPresent: true, |
| 195 |
}, |
| 196 |
}, |
| 197 |
}, |
| 198 |
}, |
| 199 |
|
| 200 |
"fieldSpecWithVersion": { |
| 201 |
input: ` |
| 202 |
apiVersion: example.com/v1 |
| 203 |
kind: Foo |
| 204 |
metadata: |
| 205 |
name: instance |
| 206 |
--- |
| 207 |
apiVersion: example.com/v2 |
| 208 |
kind: Bar |
| 209 |
metadata: |
| 210 |
name: instance |
| 211 |
`, |
| 212 |
expectedOutput: ` |
| 213 |
apiVersion: example.com/v1 |
| 214 |
kind: Foo |
| 215 |
metadata: |
| 216 |
name: instance |
| 217 |
labels: |
| 218 |
cheese: cheddar |
| 219 |
a: |
| 220 |
b: |
| 221 |
cheese: cheddar |
| 222 |
--- |
| 223 |
apiVersion: example.com/v2 |
| 224 |
kind: Bar |
| 225 |
metadata: |
| 226 |
name: instance |
| 227 |
labels: |
| 228 |
cheese: cheddar |
| 229 |
`, |
| 230 |
filter: Filter{ |
| 231 |
Labels: labelMap{ |
| 232 |
"cheese": "cheddar", |
| 233 |
}, |
| 234 |
FsSlice: []types.FieldSpec{ |
| 235 |
{ |
| 236 |
Path: "metadata/labels", |
| 237 |
CreateIfNotPresent: true, |
| 238 |
}, |
| 239 |
{ |
| 240 |
Gvk: resid.Gvk{ |
| 241 |
Version: "v1", |
| 242 |
}, |
| 243 |
Path: "a/b", |
| 244 |
CreateIfNotPresent: true, |
| 245 |
}, |
| 246 |
}, |
| 247 |
}, |
| 248 |
}, |
| 249 |
"fieldSpecWithVersionInConfigButNoGroupInData": { |
| 250 |
input: ` |
| 251 |
apiVersion: v1 |
| 252 |
kind: Foo |
| 253 |
metadata: |
| 254 |
name: instance |
| 255 |
--- |
| 256 |
apiVersion: v2 |
| 257 |
kind: Bar |
| 258 |
metadata: |
| 259 |
name: instance |
| 260 |
`, |
| 261 |
expectedOutput: ` |
| 262 |
apiVersion: v1 |
| 263 |
kind: Foo |
| 264 |
metadata: |
| 265 |
name: instance |
| 266 |
labels: |
| 267 |
cheese: cheddar |
| 268 |
a: |
| 269 |
b: |
| 270 |
cheese: cheddar |
| 271 |
--- |
| 272 |
apiVersion: v2 |
| 273 |
kind: Bar |
| 274 |
metadata: |
| 275 |
name: instance |
| 276 |
labels: |
| 277 |
cheese: cheddar |
| 278 |
`, |
| 279 |
filter: Filter{ |
| 280 |
Labels: labelMap{ |
| 281 |
"cheese": "cheddar", |
| 282 |
}, |
| 283 |
FsSlice: []types.FieldSpec{ |
| 284 |
{ |
| 285 |
Path: "metadata/labels", |
| 286 |
CreateIfNotPresent: true, |
| 287 |
}, |
| 288 |
{ |
| 289 |
Gvk: resid.Gvk{ |
| 290 |
Version: "v1", |
| 291 |
}, |
| 292 |
Path: "a/b", |
| 293 |
CreateIfNotPresent: true, |
| 294 |
}, |
| 295 |
}, |
| 296 |
}, |
| 297 |
}, |
| 298 |
|
| 299 |
"number": { |
| 300 |
input: ` |
| 301 |
apiVersion: example.com/v1 |
| 302 |
kind: Foo |
| 303 |
metadata: |
| 304 |
name: instance |
| 305 |
labels: |
| 306 |
hero: batman |
| 307 |
fiend: riddler |
| 308 |
`, |
| 309 |
expectedOutput: ` |
| 310 |
apiVersion: example.com/v1 |
| 311 |
kind: Foo |
| 312 |
metadata: |
| 313 |
name: instance |
| 314 |
labels: |
| 315 |
hero: batman |
| 316 |
fiend: riddler |
| 317 |
1: emmett kelley |
| 318 |
auto: "2" |
| 319 |
`, |
| 320 |
filter: Filter{ |
| 321 |
Labels: labelMap{ |
| 322 |
"1": "emmett kelley", |
| 323 |
"auto": "2", |
| 324 |
}, |
| 325 |
FsSlice: []types.FieldSpec{ |
| 326 |
{ |
| 327 |
Path: "metadata/labels", |
| 328 |
CreateIfNotPresent: true, |
| 329 |
}, |
| 330 |
}, |
| 331 |
}, |
| 332 |
}, |
| 333 |
|
| 334 |
// test quoting of values which are not considered strings in yaml 1.1 |
| 335 |
"yaml_1_1_compatibility": { |
| 336 |
input: ` |
| 337 |
apiVersion: example.com/v1 |
| 338 |
kind: Foo |
| 339 |
metadata: |
| 340 |
name: instance |
| 341 |
labels: |
| 342 |
hero: batman |
| 343 |
fiend: riddler |
| 344 |
`, |
| 345 |
expectedOutput: ` |
| 346 |
apiVersion: example.com/v1 |
| 347 |
kind: Foo |
| 348 |
metadata: |
| 349 |
name: instance |
| 350 |
labels: |
| 351 |
hero: batman |
| 352 |
fiend: riddler |
| 353 |
a: "y" |
| 354 |
b: y1 |
| 355 |
c: "yes" |
| 356 |
d: yes1 |
| 357 |
e: "true" |
| 358 |
f: true1 |
| 359 |
`, |
| 360 |
filter: Filter{ |
| 361 |
Labels: labelMap{ |
| 362 |
"a": "y", |
| 363 |
"b": "y1", |
| 364 |
"c": "yes", |
| 365 |
"d": "yes1", |
| 366 |
"e": "true", |
| 367 |
"f": "true1", |
| 368 |
}, |
| 369 |
FsSlice: []types.FieldSpec{ |
| 370 |
{ |
| 371 |
Path: "metadata/labels", |
| 372 |
CreateIfNotPresent: true, |
| 373 |
}, |
| 374 |
}, |
| 375 |
}, |
| 376 |
}, |
| 377 |
|
| 378 |
"null_labels": { |
| 379 |
input: ` |
| 380 |
apiVersion: example.com/v1 |
| 381 |
kind: Foo |
| 382 |
metadata: |
| 383 |
name: instance |
| 384 |
labels: null |
| 385 |
`, |
| 386 |
expectedOutput: ` |
| 387 |
apiVersion: example.com/v1 |
| 388 |
kind: Foo |
| 389 |
metadata: |
| 390 |
name: instance |
| 391 |
labels: |
| 392 |
a: a1 |
| 393 |
`, |
| 394 |
filter: Filter{ |
| 395 |
Labels: labelMap{ |
| 396 |
"a": "a1", |
| 397 |
}, |
| 398 |
FsSlice: []types.FieldSpec{ |
| 399 |
{ |
| 400 |
Path: "metadata/labels", |
| 401 |
CreateIfNotPresent: true, |
| 402 |
}, |
| 403 |
}, |
| 404 |
}, |
| 405 |
}, |
| 406 |
|
| 407 |
// test usage of SetEntryCallback |
| 408 |
"set_entry_callback": { |
| 409 |
input: ` |
| 410 |
apiVersion: example.com/v1 |
| 411 |
kind: Foo |
| 412 |
metadata: |
| 413 |
name: instance |
| 414 |
labels: |
| 415 |
witcher: geralt |
| 416 |
`, |
| 417 |
expectedOutput: ` |
| 418 |
apiVersion: example.com/v1 |
| 419 |
kind: Foo |
| 420 |
metadata: |
| 421 |
name: instance |
| 422 |
labels: |
| 423 |
witcher: geralt |
| 424 |
mage: yennefer |
| 425 |
a: |
| 426 |
b: |
| 427 |
mage: yennefer |
| 428 |
`, |
| 429 |
filter: Filter{ |
| 430 |
Labels: labelMap{ |
| 431 |
"mage": "yennefer", |
| 432 |
}, |
| 433 |
FsSlice: []types.FieldSpec{ |
| 434 |
{ |
| 435 |
Path: "metadata/labels", |
| 436 |
CreateIfNotPresent: true, |
| 437 |
}, |
| 438 |
{ |
| 439 |
Path: "a/b", |
| 440 |
CreateIfNotPresent: true, |
| 441 |
}, |
| 442 |
}, |
| 443 |
}, |
| 444 |
setEntryCallback: mutationTrackerStub.MutationTracker, |
| 445 |
expectedSetEntryArgs: []filtertest_test.SetValueArg{ |
| 446 |
{ |
| 447 |
Key: "mage", |
| 448 |
Value: "yennefer", |
| 449 |
Tag: "!!str", |
| 450 |
NodePath: []string{"metadata", "labels"}, |
| 451 |
}, |
| 452 |
{ |
| 453 |
Key: "mage", |
| 454 |
Value: "yennefer", |
| 455 |
Tag: "!!str", |
| 456 |
NodePath: []string{"a", "b"}, |
| 457 |
}, |
| 458 |
}, |
| 459 |
}, |
| 460 |
} |
| 461 |
|
| 462 |
for tn, tc := range testCases { |
| 463 |
mutationTrackerStub.Reset() |
| 464 |
t.Run(tn, func(t *testing.T) { |
| 465 |
tc.filter.WithMutationTracker(tc.setEntryCallback) |
| 466 |
if !assert.Equal(t, |
| 467 |
strings.TrimSpace(tc.expectedOutput), |
| 468 |
strings.TrimSpace(filtertest_test.RunFilter(t, tc.input, tc.filter))) { |
| 469 |
t.FailNow() |
| 470 |
} |
| 471 |
if !assert.Equal(t, tc.expectedSetEntryArgs, mutationTrackerStub.SetValueArgs()) { |
| 472 |
t.FailNow() |
| 473 |
} |
| 474 |
}) |
| 475 |
} |
| 476 |
} |