File: schema_change_test.go

package info (click to toggle)
golang-k8s-sigs-structured-merge-diff 4.1.2%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,580 kB
  • sloc: makefile: 4
file content (254 lines) | stat: -rw-r--r-- 5,436 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
/*
Copyright 2018 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 merge_test

import (
	"testing"

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

var structParser = func() *typed.Parser {
	oldParser, err := typed.NewParser(`types:
- name: v1
  map:
    fields:
      - name: struct
        type:
          namedType: struct
- name: struct
  map:
    fields:
    - name: numeric
      type:
        scalar: numeric
    - name: string
      type:
        scalar: string`)
	if err != nil {
		panic(err)
	}
	return oldParser
}()

var structWithAtomicParser = func() *typed.Parser {
	newParser, err := typed.NewParser(`types:
- name: v1
  map:
    fields:
      - name: struct
        type:
          namedType: struct
- name: struct
  map:
    fields:
    - name: numeric
      type:
        scalar: numeric
    - name: string
      type:
        scalar: string
    elementRelationship: atomic`)
	if err != nil {
		panic(err)
	}
	return newParser
}()

func TestGranularToAtomicSchemaChanges(t *testing.T) {
	tests := map[string]TestCase{
		"to-atomic": {
			Ops: []Operation{
				Apply{
					Manager: "one",
					Object: `
						struct:
						  numeric: 1
					`,
					APIVersion: "v1",
				},
				ChangeParser{Parser: structWithAtomicParser},
				Apply{
					Manager: "two",
					Object: `
						struct:
						  string: "string"
					`,
					APIVersion: "v1",
					Conflicts: merge.Conflicts{
						merge.Conflict{Manager: "one", Path: _P("struct")},
					},
				},
				ForceApply{
					Manager: "two",
					Object: `
						struct:
						  string: "string"
					`,
					APIVersion: "v1",
				},
			},
			Object: `
				struct:
				  string: "string"
			`,
			APIVersion: "v1",
			Managed: fieldpath.ManagedFields{
				"two": fieldpath.NewVersionedSet(_NS(
					_P("struct"),
				), "v1", true),
			},
		},
		"to-atomic-owner-with-no-child-fields": {
			Ops: []Operation{
				Apply{
					Manager: "one",
					Object: `
						struct:
						  numeric: 1
					`,
					APIVersion: "v1",
				},
				ForceApply{ // take the only child field from manager "one"
					Manager: "two",
					Object: `
						struct:
						  numeric: 2
					`,
					APIVersion: "v1",
				},
				ChangeParser{Parser: structWithAtomicParser},
				Apply{
					Manager: "three",
					Object: `
						struct:
						  string: "string"
					`,
					APIVersion: "v1",
					Conflicts: merge.Conflicts{
						// We expect no conflict with "one" because we do not allow a manager
						// to own a map without owning any of the children.
						merge.Conflict{Manager: "two", Path: _P("struct")},
					},
				},
				ForceApply{
					Manager: "two",
					Object: `
						struct:
						  string: "string"
					`,
					APIVersion: "v1",
				},
			},
			Object: `
				struct:
				  string: "string"
			`,
			APIVersion: "v1",
			Managed: fieldpath.ManagedFields{
				"two": fieldpath.NewVersionedSet(_NS(
					_P("struct"),
				), "v1", true),
			},
		},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			if err := test.Test(structParser); err != nil {
				t.Fatal(err)
			}
		})
	}
}

func TestAtomicToGranularSchemaChanges(t *testing.T) {
	tests := map[string]TestCase{
		"to-granular": {
			Ops: []Operation{
				Apply{
					Manager: "one",
					Object: `
						struct:
						  numeric: 1
						  string: "a"
					`,
					APIVersion: "v1",
				},
				Apply{
					Manager: "two",
					Object: `
						struct:
						  string: "b"
					`,
					APIVersion: "v1",
					Conflicts: merge.Conflicts{
						merge.Conflict{Manager: "one", Path: _P("struct")},
					},
				},
				ChangeParser{Parser: structParser},
				// No conflict after changing struct to a granular schema
				Apply{
					Manager: "two",
					Object: `
						struct:
						  string: "b"
					`,
					APIVersion: "v1",
				},
			},
			Object: `
				struct:
				  numeric: 1
				  string: "b"
			`,
			APIVersion: "v1",
			Managed: fieldpath.ManagedFields{
				// Note that manager one previously owned
				// the top level _P("struct")
				// which included all of its subfields
				// when the struct field was atomic.
				//
				// Upon changing the schema of struct from
				// atomic to granular, manager one continues
				// to own the same fieldset as before,
				// but does not retain ownership of any of the subfields.
				//
				// This is a known limitation due to the inability
				// to accurately determine whether an empty field
				// was previously atomic or not.
				"one": fieldpath.NewVersionedSet(_NS(
					_P("struct"),
				), "v1", true),
				"two": fieldpath.NewVersionedSet(_NS(
					_P("struct", "string"),
				), "v1", true),
			},
		},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			if err := test.Test(structWithAtomicParser); err != nil {
				t.Fatal(err)
			}
		})
	}
}