| 1 |
// Copyright 2020 The Kubernetes Authors. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package annotations |
| 5 |
|
| 6 |
import ( |
| 7 |
"strings" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/stretchr/testify/assert" |
| 11 |
"sigs.k8s.io/kustomize/api/internal/plugins/builtinconfig" |
| 12 |
filtertest_test "sigs.k8s.io/kustomize/api/testutils/filtertest" |
| 13 |
"sigs.k8s.io/kustomize/api/types" |
| 14 |
"sigs.k8s.io/kustomize/kyaml/yaml" |
| 15 |
) |
| 16 |
|
| 17 |
var annosFs = builtinconfig.MakeDefaultConfig().CommonAnnotations |
| 18 |
|
| 19 |
func TestAnnotations_Filter(t *testing.T) { |
| 20 |
mutationTrackStub := filtertest_test.MutationTrackerStub{} |
| 21 |
testCases := map[string]struct { |
| 22 |
input string |
| 23 |
expectedOutput string |
| 24 |
filter Filter |
| 25 |
fsslice types.FsSlice |
| 26 |
setEntryCallback func(key, value, tag string, node *yaml.RNode) |
| 27 |
expectedSetEntryArgs []filtertest_test.SetValueArg |
| 28 |
}{ |
| 29 |
"add": { |
| 30 |
input: ` |
| 31 |
apiVersion: example.com/v1 |
| 32 |
kind: Foo |
| 33 |
metadata: |
| 34 |
name: instance |
| 35 |
annotations: |
| 36 |
hero: batman |
| 37 |
fiend: riddler |
| 38 |
`, |
| 39 |
expectedOutput: ` |
| 40 |
apiVersion: example.com/v1 |
| 41 |
kind: Foo |
| 42 |
metadata: |
| 43 |
name: instance |
| 44 |
annotations: |
| 45 |
hero: batman |
| 46 |
fiend: riddler |
| 47 |
auto: ford |
| 48 |
bean: cannellini |
| 49 |
clown: emmett kelley |
| 50 |
dragon: smaug |
| 51 |
`, |
| 52 |
filter: Filter{Annotations: annoMap{ |
| 53 |
"clown": "emmett kelley", |
| 54 |
"auto": "ford", |
| 55 |
"dragon": "smaug", |
| 56 |
"bean": "cannellini", |
| 57 |
}}, |
| 58 |
}, |
| 59 |
"update": { |
| 60 |
input: ` |
| 61 |
apiVersion: example.com/v1 |
| 62 |
kind: Foo |
| 63 |
metadata: |
| 64 |
name: instance |
| 65 |
annotations: |
| 66 |
hero: batman |
| 67 |
fiend: riddler |
| 68 |
`, |
| 69 |
expectedOutput: ` |
| 70 |
apiVersion: example.com/v1 |
| 71 |
kind: Foo |
| 72 |
metadata: |
| 73 |
name: instance |
| 74 |
annotations: |
| 75 |
hero: superman |
| 76 |
fiend: luthor |
| 77 |
bean: cannellini |
| 78 |
clown: emmett kelley |
| 79 |
`, |
| 80 |
filter: Filter{Annotations: annoMap{ |
| 81 |
"clown": "emmett kelley", |
| 82 |
"hero": "superman", |
| 83 |
"fiend": "luthor", |
| 84 |
"bean": "cannellini", |
| 85 |
}}, |
| 86 |
}, |
| 87 |
"data-fieldspecs": { |
| 88 |
input: ` |
| 89 |
apiVersion: example.com/v1 |
| 90 |
kind: Foo |
| 91 |
metadata: |
| 92 |
name: instance |
| 93 |
--- |
| 94 |
apiVersion: example.com/v1 |
| 95 |
kind: Bar |
| 96 |
metadata: |
| 97 |
name: instance |
| 98 |
`, |
| 99 |
expectedOutput: ` |
| 100 |
apiVersion: example.com/v1 |
| 101 |
kind: Foo |
| 102 |
metadata: |
| 103 |
name: instance |
| 104 |
annotations: |
| 105 |
sleater: kinney |
| 106 |
a: |
| 107 |
b: |
| 108 |
sleater: kinney |
| 109 |
--- |
| 110 |
apiVersion: example.com/v1 |
| 111 |
kind: Bar |
| 112 |
metadata: |
| 113 |
name: instance |
| 114 |
annotations: |
| 115 |
sleater: kinney |
| 116 |
a: |
| 117 |
b: |
| 118 |
sleater: kinney |
| 119 |
`, |
| 120 |
filter: Filter{Annotations: annoMap{ |
| 121 |
"sleater": "kinney", |
| 122 |
}}, |
| 123 |
fsslice: []types.FieldSpec{ |
| 124 |
{ |
| 125 |
Path: "a/b", |
| 126 |
CreateIfNotPresent: true, |
| 127 |
}, |
| 128 |
}, |
| 129 |
}, |
| 130 |
|
| 131 |
"number": { |
| 132 |
input: ` |
| 133 |
apiVersion: example.com/v1 |
| 134 |
kind: Foo |
| 135 |
metadata: |
| 136 |
name: instance |
| 137 |
annotations: |
| 138 |
hero: batman |
| 139 |
fiend: riddler |
| 140 |
`, |
| 141 |
expectedOutput: ` |
| 142 |
apiVersion: example.com/v1 |
| 143 |
kind: Foo |
| 144 |
metadata: |
| 145 |
name: instance |
| 146 |
annotations: |
| 147 |
hero: batman |
| 148 |
fiend: riddler |
| 149 |
2: ford |
| 150 |
clown: "1" |
| 151 |
`, |
| 152 |
filter: Filter{Annotations: annoMap{ |
| 153 |
"clown": "1", |
| 154 |
"2": "ford", |
| 155 |
}}, |
| 156 |
}, |
| 157 |
|
| 158 |
// test quoting of values which are not considered strings in yaml 1.1 |
| 159 |
"yaml_1_1_compatibility": { |
| 160 |
input: ` |
| 161 |
apiVersion: example.com/v1 |
| 162 |
kind: Foo |
| 163 |
metadata: |
| 164 |
name: instance |
| 165 |
annotations: |
| 166 |
hero: batman |
| 167 |
fiend: riddler |
| 168 |
`, |
| 169 |
expectedOutput: ` |
| 170 |
apiVersion: example.com/v1 |
| 171 |
kind: Foo |
| 172 |
metadata: |
| 173 |
name: instance |
| 174 |
annotations: |
| 175 |
hero: batman |
| 176 |
fiend: riddler |
| 177 |
a: "y" |
| 178 |
b: y1 |
| 179 |
c: "yes" |
| 180 |
d: yes1 |
| 181 |
e: "true" |
| 182 |
f: true1 |
| 183 |
`, |
| 184 |
filter: Filter{Annotations: annoMap{ |
| 185 |
"a": "y", |
| 186 |
"b": "y1", |
| 187 |
"c": "yes", |
| 188 |
"d": "yes1", |
| 189 |
"e": "true", |
| 190 |
"f": "true1", |
| 191 |
}}, |
| 192 |
}, |
| 193 |
|
| 194 |
// test quoting of values which are not considered strings in yaml 1.1 |
| 195 |
"null_annotations": { |
| 196 |
input: ` |
| 197 |
apiVersion: example.com/v1 |
| 198 |
kind: Foo |
| 199 |
metadata: |
| 200 |
name: instance |
| 201 |
annotations: null |
| 202 |
`, |
| 203 |
expectedOutput: ` |
| 204 |
apiVersion: example.com/v1 |
| 205 |
kind: Foo |
| 206 |
metadata: |
| 207 |
name: instance |
| 208 |
annotations: |
| 209 |
a: a1 |
| 210 |
b: b1 |
| 211 |
`, |
| 212 |
filter: Filter{Annotations: annoMap{ |
| 213 |
"a": "a1", |
| 214 |
"b": "b1", |
| 215 |
}}, |
| 216 |
}, |
| 217 |
|
| 218 |
// test usage of SetEntryCallback |
| 219 |
"set_entry_callback": { |
| 220 |
input: ` |
| 221 |
apiVersion: example.com/v1 |
| 222 |
kind: Foo |
| 223 |
metadata: |
| 224 |
name: instance |
| 225 |
`, |
| 226 |
expectedOutput: ` |
| 227 |
apiVersion: example.com/v1 |
| 228 |
kind: Foo |
| 229 |
metadata: |
| 230 |
name: instance |
| 231 |
annotations: |
| 232 |
a: a1 |
| 233 |
b: b1 |
| 234 |
spec: |
| 235 |
template: |
| 236 |
metadata: |
| 237 |
annotations: |
| 238 |
a: a1 |
| 239 |
b: b1 |
| 240 |
`, |
| 241 |
filter: Filter{ |
| 242 |
Annotations: annoMap{ |
| 243 |
"a": "a1", |
| 244 |
"b": "b1", |
| 245 |
}, |
| 246 |
}, |
| 247 |
setEntryCallback: mutationTrackStub.MutationTracker, |
| 248 |
fsslice: []types.FieldSpec{ |
| 249 |
{ |
| 250 |
Path: "spec/template/metadata/annotations", |
| 251 |
CreateIfNotPresent: true, |
| 252 |
}, |
| 253 |
}, |
| 254 |
expectedSetEntryArgs: []filtertest_test.SetValueArg{ |
| 255 |
{ |
| 256 |
Key: "a", |
| 257 |
Value: "a1", |
| 258 |
Tag: "!!str", |
| 259 |
NodePath: []string{"metadata", "annotations"}, |
| 260 |
}, |
| 261 |
{ |
| 262 |
Key: "a", |
| 263 |
Value: "a1", |
| 264 |
Tag: "!!str", |
| 265 |
NodePath: []string{"spec", "template", "metadata", "annotations"}, |
| 266 |
}, |
| 267 |
{ |
| 268 |
Key: "b", |
| 269 |
Value: "b1", |
| 270 |
Tag: "!!str", |
| 271 |
NodePath: []string{"metadata", "annotations"}, |
| 272 |
}, |
| 273 |
{ |
| 274 |
Key: "b", |
| 275 |
Value: "b1", |
| 276 |
Tag: "!!str", |
| 277 |
NodePath: []string{"spec", "template", "metadata", "annotations"}, |
| 278 |
}, |
| 279 |
}, |
| 280 |
}, |
| 281 |
} |
| 282 |
|
| 283 |
for tn, tc := range testCases { |
| 284 |
mutationTrackStub.Reset() |
| 285 |
t.Run(tn, func(t *testing.T) { |
| 286 |
filter := tc.filter |
| 287 |
filter.WithMutationTracker(tc.setEntryCallback) |
| 288 |
filter.FsSlice = append(annosFs, tc.fsslice...) //nolint:gocritic |
| 289 |
if !assert.Equal(t, |
| 290 |
strings.TrimSpace(tc.expectedOutput), |
| 291 |
strings.TrimSpace(filtertest_test.RunFilter(t, tc.input, filter))) { |
| 292 |
t.FailNow() |
| 293 |
} |
| 294 |
if !assert.Equal(t, tc.expectedSetEntryArgs, mutationTrackStub.SetValueArgs()) { |
| 295 |
t.FailNow() |
| 296 |
} |
| 297 |
}) |
| 298 |
} |
| 299 |
} |