1+ /*
2+ Copyright 2018 The Kubernetes Authors.
3+
4+ Licensed under the Apache License, Version 2.0 (the "License");
5+ you may not use this file except in compliance with the License.
6+ You may obtain a copy of the License at
7+
8+ http://www.apache.org/licenses/LICENSE-2.0
9+
10+ Unless required by applicable law or agreed to in writing, software
11+ distributed under the License is distributed on an "AS IS" BASIS,
12+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ See the License for the specific language governing permissions and
14+ limitations under the License.
15+ */
16+
17+ package merge_test
18+
19+ import (
20+ "testing"
21+
22+ "sigs.k8s.io/structured-merge-diff/v4/fieldpath"
23+ . "sigs.k8s.io/structured-merge-diff/v4/internal/fixture"
24+ "sigs.k8s.io/structured-merge-diff/v4/typed"
25+ )
26+
27+
28+ var structParser = func () * typed.Parser {
29+ oldParser , err := typed .NewParser (`types:
30+ - name: v1
31+ map:
32+ fields:
33+ - name: struct
34+ type:
35+ namedType: struct
36+ - name: struct
37+ map:
38+ fields:
39+ - name: numeric
40+ type:
41+ scalar: numeric
42+ - name: string
43+ type:
44+ scalar: string` )
45+ if err != nil {
46+ panic (err )
47+ }
48+ return oldParser
49+ }()
50+
51+ var structWithAtomicParser = func () * typed.Parser {
52+ newParser , err := typed .NewParser ( `types:
53+ - name: v1
54+ map:
55+ fields:
56+ - name: struct
57+ type:
58+ namedType: struct
59+ - name: struct
60+ map:
61+ fields:
62+ - name: numeric
63+ type:
64+ scalar: numeric
65+ - name: string
66+ type:
67+ scalar: string
68+ elementRelationship: atomic` )
69+ if err != nil {
70+ panic (err )
71+ }
72+ return newParser
73+ }()
74+
75+ func TestSchemaChanges (t * testing.T ) {
76+ tests := map [string ]TestCase {
77+ "change-struct-to-atomic" : {
78+ Ops : []Operation {
79+ Apply {
80+ Manager : "one" ,
81+ Object : `
82+ struct:
83+ numeric: 1
84+ ` ,
85+ APIVersion : "v1" ,
86+ },
87+ ChangeParser {Parser : structWithAtomicParser },
88+ Apply {
89+ Manager : "two" ,
90+ Object : `
91+ struct:
92+ string: "string"
93+ ` ,
94+ APIVersion : "v1" ,
95+ },
96+ },
97+ Object : `
98+ struct:
99+ string: "string"
100+ ` ,
101+ APIVersion : "v1" ,
102+ Managed : fieldpath.ManagedFields {
103+ "two" : fieldpath .NewVersionedSet (
104+ _NS (
105+ _P ("struct" ),
106+ ),
107+ "v1" ,
108+ true ,
109+ ),
110+ },
111+ },
112+ }
113+
114+ for name , test := range tests {
115+ t .Run (name , func (t * testing.T ) {
116+ if err := test .Test (structParser ); err != nil {
117+ t .Fatal (err )
118+ }
119+ })
120+ }
121+ }
0 commit comments