1
2
3
4 package framework
5
6 import (
7 "sort"
8 "testing"
9
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12 "sigs.k8s.io/kustomize/kyaml/resid"
13 )
14
15 var demoFunctionDefinition = `
16 apiVersion: config.kubernetes.io/v1alpha1
17 kind: KRMFunctionDefinition
18 metadata:
19 name: demos.example.io
20 spec:
21 group: example.io
22 names:
23 kind: Demo
24 versions:
25 - name: v1alpha1
26 schema:
27 openAPIV3Schema:
28 properties:
29 apiVersion:
30 type: string
31 color:
32 type: string
33 kind:
34 type: string
35 metadata:
36 type: object
37 required:
38 - color
39 type: object
40 - name: v1alpha2
41 schema:
42 openAPIV3Schema:
43 properties:
44 apiVersion:
45 type: string
46 flavor:
47 type: string
48 kind:
49 type: string
50 metadata:
51 type: object
52 required:
53 - flavor
54 type: object
55 `
56
57 var demoCRD = `
58 apiVersion: apiextensions.k8s.io/v1
59 kind: CustomResourceDefinition
60 metadata:
61 name: demos.example.io
62 spec:
63 group: example.io
64 names:
65 kind: Demo
66 listKind: DemoList
67 plural: demos
68 singular: demo
69 scope: Namespaced
70 versions:
71 - name: v1alpha1
72 schema:
73 openAPIV3Schema:
74 properties:
75 apiVersion:
76 description: 'APIVersion defines the versioned schema of this representation
77 of an object. Servers should convert recognized schemas to the latest
78 internal value, and may reject unrecognized values. More info:
79 https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
80 type: string
81 color:
82 type: string
83 kind:
84 description: 'Kind is a string value representing the REST resource this
85 object represents. Servers may infer this from the endpoint the client
86 submits requests to. Cannot be updated. In CamelCase. More info:
87 https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
88 type: string
89 metadata:
90 type: object
91 required:
92 - color
93 type: object
94 served: true
95 storage: true
96 `
97
98 func TestSchemaFromFunctionDefinition(t *testing.T) {
99 tests := []struct {
100 name string
101 gvk resid.Gvk
102 data string
103 wantProps []string
104 wantErr string
105 }{
106 {
107 name: "demo KRMFunctionDefinition extract v1alpha1",
108 gvk: resid.NewGvk("example.io", "v1alpha1", "Demo"),
109 data: demoFunctionDefinition,
110 wantProps: []string{"apiVersion", "kind", "metadata", "color"},
111 }, {
112 name: "demo KRMFunctionDefinition extract v1alpha2",
113 gvk: resid.NewGvk("example.io", "v1alpha2", "Demo"),
114 data: demoFunctionDefinition,
115 wantProps: []string{"apiVersion", "kind", "metadata", "flavor"},
116 }, {
117 name: "works with CustomResourceDefinition",
118 gvk: resid.NewGvk("example.io", "v1alpha1", "Demo"),
119 data: demoCRD,
120 wantProps: []string{"apiVersion", "kind", "metadata", "color"},
121 }, {
122 name: "group mismatch",
123 gvk: resid.NewGvk("example.com", "v1alpha2", "Demo"),
124 data: demoFunctionDefinition,
125 wantErr: "KRMFunctionDefinition does not define Demo.v1alpha2.example.com (defines: [Demo.v1alpha1.example.io Demo.v1alpha2.example.io])",
126 }, {
127 name: "version mismatch",
128 gvk: resid.NewGvk("example.io", "v1alpha3", "Demo"),
129 data: demoFunctionDefinition,
130 wantErr: "KRMFunctionDefinition does not define Demo.v1alpha3.example.io (defines: [Demo.v1alpha1.example.io Demo.v1alpha2.example.io])",
131 }, {
132 name: "kind mismatch",
133 gvk: resid.NewGvk("example.io", "v1alpha2", "Demonstration"),
134 data: demoFunctionDefinition,
135 wantErr: "KRMFunctionDefinition does not define Demonstration.v1alpha2.example.io (defines: [Demo.v1alpha1.example.io Demo.v1alpha2.example.io])",
136 },
137 }
138 for _, tt := range tests {
139 t.Run(tt.name, func(t *testing.T) {
140 got, err := SchemaFromFunctionDefinition(tt.gvk, tt.data)
141 if tt.wantErr != "" {
142 require.EqualError(t, err, tt.wantErr)
143 } else {
144 require.NoError(t, err)
145 var gotProps []string
146 for prop := range got.Properties {
147 gotProps = append(gotProps, prop)
148 }
149 sort.Strings(tt.wantProps)
150 sort.Strings(gotProps)
151 assert.Equal(t, gotProps, tt.wantProps)
152 }
153 })
154 }
155 }
156
View as plain text