Skip to content

Commit

Permalink
Merge pull request #45 from Ruhika1417/test-breaking-changes
Browse files Browse the repository at this point in the history
 checking breaking changes between 2 crds schemas.
  • Loading branch information
muvaf authored Sep 1, 2022
2 parents 6bcdd63 + b46fb82 commit 806c0b0
Show file tree
Hide file tree
Showing 6 changed files with 1,051 additions and 15 deletions.
85 changes: 85 additions & 0 deletions cmd/breakingChanges/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
Copyright 2022 The Crossplane 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 main

import (
"fmt"
"log"
"os"

v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/util/yaml"
)

func main() {
oldfile, err := os.ReadFile("old.yaml")
if err != nil {
log.Fatal(err)
}

newfile, err := os.ReadFile("new.yaml")
if err != nil {
log.Fatal(err)
}

old := &v1.CustomResourceDefinition{}
err = yaml.Unmarshal(oldfile, old)
if err != nil {
log.Fatal(err)
}

new := &v1.CustomResourceDefinition{}
err = yaml.Unmarshal(newfile, new)
if err != nil {
log.Fatal(err)
}

list := PrintFields(old.Spec.Versions[0].Schema.OpenAPIV3Schema, "", new.Spec.Versions[0].Schema.OpenAPIV3Schema)
for i := 0; i < len(list); i++ {
fmt.Println(list[i])
}
}

// PrintFields function recursively traverses through the keys.
func PrintFields(sch *v1.JSONSchemaProps, prefix string, newSchema *v1.JSONSchemaProps) []string {

var a []string

if len(sch.Properties) == 0 {
return nil
}

for key := range sch.Properties {
val := sch.Properties[key]
var temp string

if prefix == "" {
temp = key
} else {
temp = prefix + "." + key
}

prop, ok := newSchema.Properties[key]

if !ok {
a = append(a, temp)
continue
}
a = append(a, PrintFields(&val, temp, &prop)...)
}
return a
}
292 changes: 292 additions & 0 deletions cmd/breakingChanges/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,292 @@
/*
Copyright 2022 The Crossplane 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 main

import (
"testing"

"github.com/google/go-cmp/cmp"
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
)

func TestBreakingChanges(t *testing.T) {

type args struct {
oldyaml *v1.JSONSchemaProps
newyaml *v1.JSONSchemaProps
}
type want struct {
result []string
}
cases := map[string]struct {
args
want
}{
"No changes": {
args: args{
oldyaml: &v1.JSONSchemaProps{
Properties: map[string]v1.JSONSchemaProps{
"apiVersion": {},
"kind": {},
"metadata": {},
"status": {
Properties: map[string]v1.JSONSchemaProps{
"conditions": {},
"atProvider": {
Properties: map[string]v1.JSONSchemaProps{
"id": {},
},
},
},
},
"spec": {
Properties: map[string]v1.JSONSchemaProps{
"deletionPolicy": {},
"forProvider": {
Properties: map[string]v1.JSONSchemaProps{
"description": {},
"enableInboundForwarding": {},
"enableLogging": {},
"networks": {},
"alternativeNameServerConfig": {
Properties: map[string]v1.JSONSchemaProps{
"targetNameServers": {},
},
},
},
},
"providerRef": {
Properties: map[string]v1.JSONSchemaProps{
"name": {},
},
},
"providerConfigRef": {
Properties: map[string]v1.JSONSchemaProps{
"name": {},
},
},
"publishConnectionDetailsTo": {
Properties: map[string]v1.JSONSchemaProps{
"name": {},
"configRef": {
Properties: map[string]v1.JSONSchemaProps{
"name": {},
},
},
"metadata": {
Properties: map[string]v1.JSONSchemaProps{
"type": {},
"annotations": {},
"labels": {},
},
},
},
},
"writeConnectionSecretToRef": {
Properties: map[string]v1.JSONSchemaProps{
"name": {},
"namespace": {},
},
},
},
},
},
},

newyaml: &v1.JSONSchemaProps{
Properties: map[string]v1.JSONSchemaProps{
"apiVersion": {},
"kind": {},
"metadata": {},
"status": {
Properties: map[string]v1.JSONSchemaProps{
"conditions": {},
"atProvider": {
Properties: map[string]v1.JSONSchemaProps{
"id": {},
},
},
},
},
"spec": {
Properties: map[string]v1.JSONSchemaProps{
"deletionPolicy": {},
"forProvider": {
Properties: map[string]v1.JSONSchemaProps{
"description": {},
"enableInboundForwarding": {},
"enableLogging": {},
"networks": {},
"alternativeNameServerConfig": {
Properties: map[string]v1.JSONSchemaProps{
"targetNameServers": {},
},
},
},
},
"providerRef": {
Properties: map[string]v1.JSONSchemaProps{
"name": {},
},
},
"providerConfigRef": {
Properties: map[string]v1.JSONSchemaProps{
"name": {},
},
},
"publishConnectionDetailsTo": {
Properties: map[string]v1.JSONSchemaProps{
"name": {},
"configRef": {
Properties: map[string]v1.JSONSchemaProps{
"name": {},
},
},
"metadata": {
Properties: map[string]v1.JSONSchemaProps{
"type": {},
"annotations": {},
"labels": {},
},
},
},
},
"writeConnectionSecretToRef": {
Properties: map[string]v1.JSONSchemaProps{
"name": {},
"namespace": {},
},
},
},
},
},
},
},
},

"spec.forProvider.description": {
args: args{
oldyaml: &v1.JSONSchemaProps{
Properties: map[string]v1.JSONSchemaProps{
"spec": {
Properties: map[string]v1.JSONSchemaProps{
"deletionPolicy": {},
"forProvider": {
Properties: map[string]v1.JSONSchemaProps{
"description": {},
"enableInboundForwarding": {},
"enableLogging": {},
"networks": {},
"alternativeNameServerConfig": {
Properties: map[string]v1.JSONSchemaProps{
"targetNameServers": {},
},
},
},
},
},
},
},
},
newyaml: &v1.JSONSchemaProps{
Properties: map[string]v1.JSONSchemaProps{
"spec": {
Properties: map[string]v1.JSONSchemaProps{
"deletionPolicy": {},
"forProvider": {
Properties: map[string]v1.JSONSchemaProps{
"enableInboundForwarding": {},
"enableLogging": {},
"networks": {},
"alternativeNameServerConfig": {
Properties: map[string]v1.JSONSchemaProps{
"targetNameServers": {},
},
},
},
},
},
},
},
},
},
want: want{
result: []string{"spec.forProvider.description"},
},
},

"kind": {
args: args{
oldyaml: &v1.JSONSchemaProps{
Properties: map[string]v1.JSONSchemaProps{
"apiVersion": {},
"kind": {},
"metadata": {},
"status": {
Properties: map[string]v1.JSONSchemaProps{
"conditions": {},
"atProvider": {
Properties: map[string]v1.JSONSchemaProps{
"id": {},
},
},
},
},
"spec": {
Properties: map[string]v1.JSONSchemaProps{
"deletionPolicy": {},
},
},
},
},
newyaml: &v1.JSONSchemaProps{
Properties: map[string]v1.JSONSchemaProps{
"apiVersion": {},
"metadata": {},
"status": {
Properties: map[string]v1.JSONSchemaProps{
"conditions": {},
"atProvider": {
Properties: map[string]v1.JSONSchemaProps{
"id": {},
},
},
},
},
"spec": {
Properties: map[string]v1.JSONSchemaProps{
"deletionPolicy": {},
},
},
},
},
},
want: want{
result: []string{"kind"},
},
},
}

for name, tc := range cases {
t.Run(name, func(t *testing.T) {
got := PrintFields(tc.args.oldyaml, "", tc.args.newyaml)
if diff := cmp.Diff(got, tc.want.result); diff != "" {
t.Errorf("PrintFields(...): -want, +got:\n%s", diff)
}
})
}
}
Loading

0 comments on commit 806c0b0

Please sign in to comment.