1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package metadata_test
16
17 import (
18 "testing"
19
20 dclmetadata "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/dcl/metadata"
21 testservicemetadataloader "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/servicemetadataloader"
22
23 dclunstruct "github.com/GoogleCloudPlatform/declarative-resource-client-library/unstructured"
24 "k8s.io/apimachinery/pkg/runtime/schema"
25 )
26
27 func Test_GVK_STV_Conversion(t *testing.T) {
28 tests := []struct {
29 name string
30 gvk schema.GroupVersionKind
31 stv dclunstruct.ServiceTypeVersion
32 }{
33 {
34 name: "test1_foo",
35 gvk: schema.GroupVersionKind{
36 Group: "test1.cnrm.cloud.google.com",
37 Version: "v1alpha1",
38 Kind: "Test1Foo",
39 },
40 stv: dclunstruct.ServiceTypeVersion{
41 Service: "test1",
42 Version: "beta",
43 Type: "Foo",
44 },
45 },
46 {
47 name: "test1_bar",
48 gvk: schema.GroupVersionKind{
49 Group: "test1.cnrm.cloud.google.com",
50 Version: "v1alpha1",
51 Kind: "Test1Bar",
52 },
53 stv: dclunstruct.ServiceTypeVersion{
54 Service: "test1",
55 Version: "beta",
56 Type: "Bar",
57 },
58 },
59 {
60 name: "acronym naming",
61 gvk: schema.GroupVersionKind{
62 Group: "test1.cnrm.cloud.google.com",
63 Version: "v1alpha1",
64 Kind: "Test1IDPConfig",
65 },
66 stv: dclunstruct.ServiceTypeVersion{
67 Service: "test1",
68 Version: "beta",
69 Type: "IdpConfig",
70 },
71 },
72 {
73 name: "service name is renamed in KCC",
74 gvk: schema.GroupVersionKind{
75 Group: "test3.cnrm.cloud.google.com",
76 Version: "v1alpha1",
77 Kind: "Test3Qux",
78 },
79 stv: dclunstruct.ServiceTypeVersion{
80 Service: "DCLTest3",
81 Version: "beta",
82 Type: "Qux",
83 },
84 },
85 {
86 name: "alpha resource within Test7 service",
87 gvk: schema.GroupVersionKind{
88 Group: "test7.cnrm.cloud.google.com",
89 Version: "v1alpha1",
90 Kind: "Test7AlphaResource",
91 },
92 stv: dclunstruct.ServiceTypeVersion{
93 Service: "test7",
94 Version: "alpha",
95 Type: "AlphaResource",
96 },
97 },
98 {
99 name: "beta resource within Test7 service",
100 gvk: schema.GroupVersionKind{
101 Group: "test7.cnrm.cloud.google.com",
102 Version: "v1alpha1",
103 Kind: "Test7BetaResource",
104 },
105 stv: dclunstruct.ServiceTypeVersion{
106 Service: "test7",
107 Version: "beta",
108 Type: "BetaResource",
109 },
110 },
111 {
112 name: "ga resource within Test7 service",
113 gvk: schema.GroupVersionKind{
114 Group: "test7.cnrm.cloud.google.com",
115 Version: "v1alpha1",
116 Kind: "Test7GaResource",
117 },
118 stv: dclunstruct.ServiceTypeVersion{
119 Service: "test7",
120 Version: "ga",
121 Type: "GaResource",
122 },
123 },
124 }
125 loader := testservicemetadataloader.NewForUnitTest()
126 for _, tc := range tests {
127 tc := tc
128 t.Run(tc.name, func(t *testing.T) {
129 t.Parallel()
130 gvk, err := dclmetadata.ToGroupVersionKind(tc.stv, loader)
131 if err != nil {
132 t.Fatalf("unexpected error converting to GroupVersionKind for ServiceTypeVersion %v: %v", tc.stv, err)
133 }
134 if gvk != tc.gvk {
135 t.Fatalf("got converted GroupVersionKind %v, but want %v", gvk, tc.gvk)
136 }
137 stv, err := dclmetadata.ToServiceTypeVersion(tc.gvk, loader)
138 if err != nil {
139 t.Fatalf("unexpected error converting to ServiceTypeVersion for GroupVersionKind %v: %v", tc.gvk, err)
140 }
141 if stv != tc.stv {
142 t.Fatalf("got converted ServiceTypeVersion %v, but want %v", stv, tc.stv)
143 }
144 })
145 }
146 }
147
148 func TestIsDCLBasedResourceKind(t *testing.T) {
149 loader := testservicemetadataloader.NewForUnitTest()
150 tests := []struct {
151 name string
152 gvk schema.GroupVersionKind
153 result bool
154 }{
155 {
156 name: "supported test resource Test1Foo",
157 gvk: schema.GroupVersionKind{
158 Group: "test1.cnrm.cloud.google.com",
159 Version: "v1alpha1",
160 Kind: "Test1Foo",
161 },
162 result: true,
163 },
164 {
165 name: "supported test resource Test2Baz",
166 gvk: schema.GroupVersionKind{
167 Group: "test2.cnrm.cloud.google.com",
168 Version: "v1alpha1",
169 Kind: "Test2Baz",
170 },
171 result: true,
172 },
173 {
174 name: "unsupported test resource in test1 service",
175 gvk: schema.GroupVersionKind{
176 Group: "test1.cnrm.cloud.google.com",
177 Version: "v1alpha1",
178 Kind: "Test1NewResource",
179 },
180 result: false,
181 },
182 {
183 name: "unsupported service",
184 gvk: schema.GroupVersionKind{
185 Group: "unsupportedservice.cnrm.cloud.google.com",
186 Version: "v1alpha1",
187 Kind: "UnsupportedServiceFoo",
188 },
189 result: false,
190 },
191 }
192
193 for _, tc := range tests {
194 tc := tc
195 t.Run(tc.name, func(t *testing.T) {
196 res := dclmetadata.IsDCLBasedResourceKind(tc.gvk, loader)
197 if res != tc.result {
198 t.Fatalf("expect to have %v, but got %v", tc.result, res)
199 }
200 })
201 }
202 }
203
View as plain text