Skip to content

Commit 806c0b0

Browse files
authored
Merge pull request #45 from Ruhika1417/test-breaking-changes
checking breaking changes between 2 crds schemas.
2 parents 6bcdd63 + b46fb82 commit 806c0b0

File tree

6 files changed

+1051
-15
lines changed

6 files changed

+1051
-15
lines changed

cmd/breakingChanges/main.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
Copyright 2022 The Crossplane 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 main
18+
19+
import (
20+
"fmt"
21+
"log"
22+
"os"
23+
24+
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
25+
"k8s.io/apimachinery/pkg/util/yaml"
26+
)
27+
28+
func main() {
29+
oldfile, err := os.ReadFile("old.yaml")
30+
if err != nil {
31+
log.Fatal(err)
32+
}
33+
34+
newfile, err := os.ReadFile("new.yaml")
35+
if err != nil {
36+
log.Fatal(err)
37+
}
38+
39+
old := &v1.CustomResourceDefinition{}
40+
err = yaml.Unmarshal(oldfile, old)
41+
if err != nil {
42+
log.Fatal(err)
43+
}
44+
45+
new := &v1.CustomResourceDefinition{}
46+
err = yaml.Unmarshal(newfile, new)
47+
if err != nil {
48+
log.Fatal(err)
49+
}
50+
51+
list := PrintFields(old.Spec.Versions[0].Schema.OpenAPIV3Schema, "", new.Spec.Versions[0].Schema.OpenAPIV3Schema)
52+
for i := 0; i < len(list); i++ {
53+
fmt.Println(list[i])
54+
}
55+
}
56+
57+
// PrintFields function recursively traverses through the keys.
58+
func PrintFields(sch *v1.JSONSchemaProps, prefix string, newSchema *v1.JSONSchemaProps) []string {
59+
60+
var a []string
61+
62+
if len(sch.Properties) == 0 {
63+
return nil
64+
}
65+
66+
for key := range sch.Properties {
67+
val := sch.Properties[key]
68+
var temp string
69+
70+
if prefix == "" {
71+
temp = key
72+
} else {
73+
temp = prefix + "." + key
74+
}
75+
76+
prop, ok := newSchema.Properties[key]
77+
78+
if !ok {
79+
a = append(a, temp)
80+
continue
81+
}
82+
a = append(a, PrintFields(&val, temp, &prop)...)
83+
}
84+
return a
85+
}

cmd/breakingChanges/main_test.go

Lines changed: 292 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
1+
/*
2+
Copyright 2022 The Crossplane 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 main
18+
19+
import (
20+
"testing"
21+
22+
"github.com/google/go-cmp/cmp"
23+
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
24+
)
25+
26+
func TestBreakingChanges(t *testing.T) {
27+
28+
type args struct {
29+
oldyaml *v1.JSONSchemaProps
30+
newyaml *v1.JSONSchemaProps
31+
}
32+
type want struct {
33+
result []string
34+
}
35+
cases := map[string]struct {
36+
args
37+
want
38+
}{
39+
"No changes": {
40+
args: args{
41+
oldyaml: &v1.JSONSchemaProps{
42+
Properties: map[string]v1.JSONSchemaProps{
43+
"apiVersion": {},
44+
"kind": {},
45+
"metadata": {},
46+
"status": {
47+
Properties: map[string]v1.JSONSchemaProps{
48+
"conditions": {},
49+
"atProvider": {
50+
Properties: map[string]v1.JSONSchemaProps{
51+
"id": {},
52+
},
53+
},
54+
},
55+
},
56+
"spec": {
57+
Properties: map[string]v1.JSONSchemaProps{
58+
"deletionPolicy": {},
59+
"forProvider": {
60+
Properties: map[string]v1.JSONSchemaProps{
61+
"description": {},
62+
"enableInboundForwarding": {},
63+
"enableLogging": {},
64+
"networks": {},
65+
"alternativeNameServerConfig": {
66+
Properties: map[string]v1.JSONSchemaProps{
67+
"targetNameServers": {},
68+
},
69+
},
70+
},
71+
},
72+
"providerRef": {
73+
Properties: map[string]v1.JSONSchemaProps{
74+
"name": {},
75+
},
76+
},
77+
"providerConfigRef": {
78+
Properties: map[string]v1.JSONSchemaProps{
79+
"name": {},
80+
},
81+
},
82+
"publishConnectionDetailsTo": {
83+
Properties: map[string]v1.JSONSchemaProps{
84+
"name": {},
85+
"configRef": {
86+
Properties: map[string]v1.JSONSchemaProps{
87+
"name": {},
88+
},
89+
},
90+
"metadata": {
91+
Properties: map[string]v1.JSONSchemaProps{
92+
"type": {},
93+
"annotations": {},
94+
"labels": {},
95+
},
96+
},
97+
},
98+
},
99+
"writeConnectionSecretToRef": {
100+
Properties: map[string]v1.JSONSchemaProps{
101+
"name": {},
102+
"namespace": {},
103+
},
104+
},
105+
},
106+
},
107+
},
108+
},
109+
110+
newyaml: &v1.JSONSchemaProps{
111+
Properties: map[string]v1.JSONSchemaProps{
112+
"apiVersion": {},
113+
"kind": {},
114+
"metadata": {},
115+
"status": {
116+
Properties: map[string]v1.JSONSchemaProps{
117+
"conditions": {},
118+
"atProvider": {
119+
Properties: map[string]v1.JSONSchemaProps{
120+
"id": {},
121+
},
122+
},
123+
},
124+
},
125+
"spec": {
126+
Properties: map[string]v1.JSONSchemaProps{
127+
"deletionPolicy": {},
128+
"forProvider": {
129+
Properties: map[string]v1.JSONSchemaProps{
130+
"description": {},
131+
"enableInboundForwarding": {},
132+
"enableLogging": {},
133+
"networks": {},
134+
"alternativeNameServerConfig": {
135+
Properties: map[string]v1.JSONSchemaProps{
136+
"targetNameServers": {},
137+
},
138+
},
139+
},
140+
},
141+
"providerRef": {
142+
Properties: map[string]v1.JSONSchemaProps{
143+
"name": {},
144+
},
145+
},
146+
"providerConfigRef": {
147+
Properties: map[string]v1.JSONSchemaProps{
148+
"name": {},
149+
},
150+
},
151+
"publishConnectionDetailsTo": {
152+
Properties: map[string]v1.JSONSchemaProps{
153+
"name": {},
154+
"configRef": {
155+
Properties: map[string]v1.JSONSchemaProps{
156+
"name": {},
157+
},
158+
},
159+
"metadata": {
160+
Properties: map[string]v1.JSONSchemaProps{
161+
"type": {},
162+
"annotations": {},
163+
"labels": {},
164+
},
165+
},
166+
},
167+
},
168+
"writeConnectionSecretToRef": {
169+
Properties: map[string]v1.JSONSchemaProps{
170+
"name": {},
171+
"namespace": {},
172+
},
173+
},
174+
},
175+
},
176+
},
177+
},
178+
},
179+
},
180+
181+
"spec.forProvider.description": {
182+
args: args{
183+
oldyaml: &v1.JSONSchemaProps{
184+
Properties: map[string]v1.JSONSchemaProps{
185+
"spec": {
186+
Properties: map[string]v1.JSONSchemaProps{
187+
"deletionPolicy": {},
188+
"forProvider": {
189+
Properties: map[string]v1.JSONSchemaProps{
190+
"description": {},
191+
"enableInboundForwarding": {},
192+
"enableLogging": {},
193+
"networks": {},
194+
"alternativeNameServerConfig": {
195+
Properties: map[string]v1.JSONSchemaProps{
196+
"targetNameServers": {},
197+
},
198+
},
199+
},
200+
},
201+
},
202+
},
203+
},
204+
},
205+
newyaml: &v1.JSONSchemaProps{
206+
Properties: map[string]v1.JSONSchemaProps{
207+
"spec": {
208+
Properties: map[string]v1.JSONSchemaProps{
209+
"deletionPolicy": {},
210+
"forProvider": {
211+
Properties: map[string]v1.JSONSchemaProps{
212+
"enableInboundForwarding": {},
213+
"enableLogging": {},
214+
"networks": {},
215+
"alternativeNameServerConfig": {
216+
Properties: map[string]v1.JSONSchemaProps{
217+
"targetNameServers": {},
218+
},
219+
},
220+
},
221+
},
222+
},
223+
},
224+
},
225+
},
226+
},
227+
want: want{
228+
result: []string{"spec.forProvider.description"},
229+
},
230+
},
231+
232+
"kind": {
233+
args: args{
234+
oldyaml: &v1.JSONSchemaProps{
235+
Properties: map[string]v1.JSONSchemaProps{
236+
"apiVersion": {},
237+
"kind": {},
238+
"metadata": {},
239+
"status": {
240+
Properties: map[string]v1.JSONSchemaProps{
241+
"conditions": {},
242+
"atProvider": {
243+
Properties: map[string]v1.JSONSchemaProps{
244+
"id": {},
245+
},
246+
},
247+
},
248+
},
249+
"spec": {
250+
Properties: map[string]v1.JSONSchemaProps{
251+
"deletionPolicy": {},
252+
},
253+
},
254+
},
255+
},
256+
newyaml: &v1.JSONSchemaProps{
257+
Properties: map[string]v1.JSONSchemaProps{
258+
"apiVersion": {},
259+
"metadata": {},
260+
"status": {
261+
Properties: map[string]v1.JSONSchemaProps{
262+
"conditions": {},
263+
"atProvider": {
264+
Properties: map[string]v1.JSONSchemaProps{
265+
"id": {},
266+
},
267+
},
268+
},
269+
},
270+
"spec": {
271+
Properties: map[string]v1.JSONSchemaProps{
272+
"deletionPolicy": {},
273+
},
274+
},
275+
},
276+
},
277+
},
278+
want: want{
279+
result: []string{"kind"},
280+
},
281+
},
282+
}
283+
284+
for name, tc := range cases {
285+
t.Run(name, func(t *testing.T) {
286+
got := PrintFields(tc.args.oldyaml, "", tc.args.newyaml)
287+
if diff := cmp.Diff(got, tc.want.result); diff != "" {
288+
t.Errorf("PrintFields(...): -want, +got:\n%s", diff)
289+
}
290+
})
291+
}
292+
}

0 commit comments

Comments
 (0)