File: union_test.go

package info (click to toggle)
golang-k8s-sigs-structured-merge-diff 4.0.2%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,504 kB
  • sloc: makefile: 4
file content (326 lines) | stat: -rw-r--r-- 8,306 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
Copyright 2019 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package typed_test

import (
	"testing"

	"sigs.k8s.io/structured-merge-diff/v4/typed"
)

var unionParser = func() typed.ParseableType {
	parser, err := typed.NewParser(`types:
- name: union
  map:
    fields:
    - name: discriminator
      type:
        scalar: string
    - name: one
      type:
        scalar: numeric
    - name: two
      type:
        scalar: numeric
    - name: three
      type:
        scalar: numeric
    - name: letter
      type:
        scalar: string
    - name: a
      type:
        scalar: numeric
    - name: b
      type:
        scalar: numeric
    unions:
    - discriminator: discriminator
      deduceInvalidDiscriminator: true
      fields:
      - fieldName: one
        discriminatorValue: One
      - fieldName: two
        discriminatorValue: TWO
      - fieldName: three
        discriminatorValue: three
    - discriminator: letter
      fields:
      - fieldName: a
        discriminatorValue: A
      - fieldName: b
        discriminatorValue: b`)
	if err != nil {
		panic(err)
	}
	return parser.Type("union")
}()

func TestNormalizeUnions(t *testing.T) {
	tests := []struct {
		name string
		old  typed.YAMLObject
		new  typed.YAMLObject
		out  typed.YAMLObject
	}{
		{
			name: "nothing changed, add discriminator",
			old:  `{"one": 1}`,
			new:  `{"one": 1}`,
			out:  `{"one": 1, "discriminator": "One"}`,
		},
		{
			name: "nothing changed, non-deduced",
			old:  `{"a": 1}`,
			new:  `{"a": 1}`,
			out:  `{"a": 1}`,
		},
		{
			name: "proper union update, setting discriminator",
			old:  `{"one": 1}`,
			new:  `{"two": 1}`,
			out:  `{"two": 1, "discriminator": "TWO"}`,
		},
		{
			name: "proper union update, non-deduced",
			old:  `{"a": 1}`,
			new:  `{"b": 1}`,
			out:  `{"b": 1}`,
		},
		{
			name: "proper union update from not-set, setting discriminator",
			old:  `{}`,
			new:  `{"two": 1}`,
			out:  `{"two": 1, "discriminator": "TWO"}`,
		},
		{
			name: "proper union update from not-set, non-deduced",
			old:  `{}`,
			new:  `{"b": 1}`,
			out:  `{"b": 1}`,
		},
		{
			name: "remove union, with discriminator",
			old:  `{"one": 1}`,
			new:  `{}`,
			out:  `{}`,
		},
		{
			name: "remove union and discriminator",
			old:  `{"one": 1, "discriminator": "One"}`,
			new:  `{}`,
			out:  `{}`,
		},
		{
			name: "remove union, not discriminator",
			old:  `{"one": 1, "discriminator": "One"}`,
			new:  `{"discriminator": "One"}`,
			out:  `{"discriminator": "One"}`,
		},
		{
			name: "remove union, not discriminator, non-deduced",
			old:  `{"a": 1, "letter": "A"}`,
			new:  `{"letter": "A"}`,
			out:  `{"letter": "A"}`,
		},
		{
			name: "change discriminator, nothing else",
			old:  `{"discriminator": "One"}`,
			new:  `{"discriminator": "random"}`,
			out:  `{"discriminator": "random"}`,
		},
		{
			name: "change discriminator, nothing else, non-deduced",
			old:  `{"letter": "A"}`,
			new:  `{"letter": "b"}`,
			out:  `{"letter": "b"}`,
		},
		{
			name: "change discriminator, nothing else, it drops other field",
			old:  `{"discriminator": "One", "one": 1}`,
			new:  `{"discriminator": "random", "one": 1}`,
			out:  `{"discriminator": "random"}`,
		},
		{
			name: "change discriminator, nothing else, it drops other field, non-deduced",
			old:  `{"letter": "A", "a": 1}`,
			new:  `{"letter": "b", "a": 1}`,
			out:  `{"letter": "b"}`,
		},
		{
			name: "remove discriminator, nothing else",
			old:  `{"discriminator": "One", "one": 1}`,
			new:  `{"one": 1}`,
			out:  `{"one": 1, "discriminator": "One"}`,
		},
		{
			name: "remove discriminator, nothing else, non-deduced",
			old:  `{"letter": "A", "a": 1}`,
			new:  `{"a": 1}`,
			out:  `{"a": 1}`,
		},
		{
			name: "remove discriminator, add new field",
			old:  `{"discriminator": "One", "one": 1}`,
			new:  `{"two": 1}`,
			out:  `{"two": 1, "discriminator": "TWO"}`,
		},
		{
			name: "remove discriminator, add new field, non-deduced",
			old:  `{"letter": "A", "a": 1}`,
			new:  `{"b": 1}`,
			out:  `{"b": 1}`,
		},
		{
			name: "both fields removed",
			old:  `{"one": 1, "two": 1}`,
			new:  `{}`,
			out:  `{}`,
		},
		{
			name: "one field removed",
			old:  `{"one": 1, "two": 1}`,
			new:  `{"one": 1}`,
			out:  `{"one": 1, "discriminator": "One"}`,
		},
		{
			name: "one field removed, non-deduced",
			old:  `{"a": 1, "b": 1}`,
			new:  `{"a": 1}`,
			out:  `{"a": 1}`,
		},
		// These use-cases shouldn't happen:
		{
			name: "one field removed, discriminator unchanged",
			old:  `{"one": 1, "two": 1, "discriminator": "TWO"}`,
			new:  `{"one": 1, "discriminator": "TWO"}`,
			out:  `{"one": 1, "discriminator": "One"}`,
		},
		{
			name: "one field removed, discriminator unchanged, non-deduced",
			old:  `{"a": 1, "b": 1, "letter": "b"}`,
			new:  `{"a": 1, "letter": "b"}`,
			out:  `{"a": 1, "letter": "b"}`,
		},
		{
			name: "one field removed, discriminator added",
			old:  `{"two": 2, "one": 1}`,
			new:  `{"one": 1, "discriminator": "TWO"}`,
			out:  `{"discriminator": "TWO"}`,
		},
		{
			name: "one field removed, discriminator added, non-deduced",
			old:  `{"b": 2, "a": 1}`,
			new:  `{"a": 1, "letter": "b"}`,
			out:  `{"letter": "b"}`,
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			old, err := unionParser.FromYAML(test.old)
			if err != nil {
				t.Fatalf("Failed to parse old object: %v", err)
			}
			new, err := unionParser.FromYAML(test.new)
			if err != nil {
				t.Fatalf("failed to parse new object: %v", err)
			}
			out, err := unionParser.FromYAML(test.out)
			if err != nil {
				t.Fatalf("failed to parse out object: %v", err)
			}
			got, err := old.NormalizeUnions(new)
			if err != nil {
				t.Fatalf("failed to normalize unions: %v", err)
			}
			comparison, err := out.Compare(got)
			if err != nil {
				t.Fatalf("failed to compare result and expected: %v", err)
			}
			if !comparison.IsSame() {
				t.Errorf("Result is different from expected:\n%v", comparison)
			}
		})
	}
}

func TestNormalizeUnionError(t *testing.T) {
	tests := []struct {
		name string
		old  typed.YAMLObject
		new  typed.YAMLObject
	}{
		{
			name: "dumb client update, no discriminator",
			old:  `{"one": 1}`,
			new:  `{"one": 2, "two": 1}`,
		},
		{
			name: "new object has three of same union set",
			old:  `{"one": 1}`,
			new:  `{"one": 2, "two": 1, "three": 3}`,
		},
		{
			name: "dumb client doesn't update discriminator",
			old:  `{"one": 1, "discriminator": "One"}`,
			new:  `{"one": 2, "two": 1, "discriminator": "One"}`,
		},
		{
			name: "client sends new field that and discriminator change",
			old:  `{}`,
			new:  `{"one": 1, "discriminator": "Two"}`,
		},
		{
			name: "client sends new fields that don't match discriminator change",
			old:  `{}`,
			new:  `{"one": 1, "two": 1, "discriminator": "One"}`,
		},
		{
			name: "old object has two of same union set",
			old:  `{"one": 1, "two": 2}`,
			new:  `{"one": 2, "two": 1}`,
		},
		{
			name: "old object has two of same union, but we add third",
			old:  `{"discriminator": "One", "one": 1, "two": 1}`,
			new:  `{"discriminator": "One", "one": 1, "two": 1, "three": 1}`,
		},
		{
			name: "one field removed, 2 left, discriminator unchanged",
			old:  `{"one": 1, "two": 1, "three": 1, "discriminator": "TWO"}`,
			new:  `{"one": 1, "two": 1, "discriminator": "TWO"}`,
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			old, err := unionParser.FromYAML(test.old)
			if err != nil {
				t.Fatalf("Failed to parse old object: %v", err)
			}
			new, err := unionParser.FromYAML(test.new)
			if err != nil {
				t.Fatalf("failed to parse new object: %v", err)
			}
			_, err = old.NormalizeUnions(new)
			if err == nil {
				t.Fatal("Normalization should have failed, but hasn't.")
			}
		})
	}
}