@@ -21,6 +21,7 @@ import (
21
21
"testing"
22
22
23
23
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24
+ "k8s.io/apimachinery/pkg/runtime/schema"
24
25
)
25
26
26
27
func TestString (t * testing.T ) {
@@ -64,3 +65,196 @@ func TestString(t *testing.T) {
64
65
})
65
66
}
66
67
}
68
+
69
+ func TestAPIEnablement (t * testing.T ) {
70
+ tests := []struct {
71
+ name string
72
+ cluster * Cluster
73
+ gvk schema.GroupVersionKind
74
+ expected APIEnablementStatus
75
+ }{
76
+ {
77
+ name : "API enabled - exact match found" ,
78
+ cluster : & Cluster {
79
+ Status : ClusterStatus {
80
+ APIEnablements : []APIEnablement {
81
+ {
82
+ GroupVersion : "apps/v1" ,
83
+ Resources : []APIResource {
84
+ {Name : "deployments" , Kind : "Deployment" },
85
+ {Name : "replicasets" , Kind : "ReplicaSet" },
86
+ },
87
+ },
88
+ },
89
+ Conditions : []metav1.Condition {
90
+ {
91
+ Type : ClusterConditionCompleteAPIEnablements ,
92
+ Status : metav1 .ConditionTrue ,
93
+ },
94
+ },
95
+ },
96
+ },
97
+ gvk : schema.GroupVersionKind {Group : "apps" , Version : "v1" , Kind : "Deployment" },
98
+ expected : APIEnabled ,
99
+ },
100
+ {
101
+ name : "API disabled - not found in complete list" ,
102
+ cluster : & Cluster {
103
+ Status : ClusterStatus {
104
+ APIEnablements : []APIEnablement {
105
+ {
106
+ GroupVersion : "apps/v1" ,
107
+ Resources : []APIResource {
108
+ {Name : "deployments" , Kind : "Deployment" },
109
+ },
110
+ },
111
+ },
112
+ Conditions : []metav1.Condition {
113
+ {
114
+ Type : ClusterConditionCompleteAPIEnablements ,
115
+ Status : metav1 .ConditionTrue ,
116
+ },
117
+ },
118
+ },
119
+ },
120
+ gvk : schema.GroupVersionKind {Group : "batch" , Version : "v1" , Kind : "Job" },
121
+ expected : APIDisabled ,
122
+ },
123
+ {
124
+ name : "API unknown - not found in partial list" ,
125
+ cluster : & Cluster {
126
+ Status : ClusterStatus {
127
+ APIEnablements : []APIEnablement {
128
+ {
129
+ GroupVersion : "apps/v1" ,
130
+ Resources : []APIResource {
131
+ {Name : "deployments" , Kind : "Deployment" },
132
+ },
133
+ },
134
+ },
135
+ Conditions : []metav1.Condition {
136
+ {
137
+ Type : ClusterConditionCompleteAPIEnablements ,
138
+ Status : metav1 .ConditionFalse ,
139
+ },
140
+ },
141
+ },
142
+ },
143
+ gvk : schema.GroupVersionKind {Group : "batch" , Version : "v1" , Kind : "Job" },
144
+ expected : APIUnknown ,
145
+ },
146
+ {
147
+ name : "API unknown - no CompleteAPIEnablements condition" ,
148
+ cluster : & Cluster {
149
+ Status : ClusterStatus {
150
+ APIEnablements : []APIEnablement {
151
+ {
152
+ GroupVersion : "apps/v1" ,
153
+ Resources : []APIResource {
154
+ {Name : "deployments" , Kind : "Deployment" },
155
+ },
156
+ },
157
+ },
158
+ Conditions : []metav1.Condition {},
159
+ },
160
+ },
161
+ gvk : schema.GroupVersionKind {Group : "batch" , Version : "v1" , Kind : "Job" },
162
+ expected : APIUnknown ,
163
+ },
164
+ {
165
+ name : "API enabled - found in core group" ,
166
+ cluster : & Cluster {
167
+ Status : ClusterStatus {
168
+ APIEnablements : []APIEnablement {
169
+ {
170
+ GroupVersion : "v1" ,
171
+ Resources : []APIResource {
172
+ {Name : "pods" , Kind : "Pod" },
173
+ {Name : "services" , Kind : "Service" },
174
+ },
175
+ },
176
+ },
177
+ Conditions : []metav1.Condition {
178
+ {
179
+ Type : ClusterConditionCompleteAPIEnablements ,
180
+ Status : metav1 .ConditionTrue ,
181
+ },
182
+ },
183
+ },
184
+ },
185
+ gvk : schema.GroupVersionKind {Group : "" , Version : "v1" , Kind : "Pod" },
186
+ expected : APIEnabled ,
187
+ },
188
+ {
189
+ name : "API disabled - wrong kind in same group version" ,
190
+ cluster : & Cluster {
191
+ Status : ClusterStatus {
192
+ APIEnablements : []APIEnablement {
193
+ {
194
+ GroupVersion : "apps/v1" ,
195
+ Resources : []APIResource {
196
+ {Name : "deployments" , Kind : "Deployment" },
197
+ },
198
+ },
199
+ },
200
+ Conditions : []metav1.Condition {
201
+ {
202
+ Type : ClusterConditionCompleteAPIEnablements ,
203
+ Status : metav1 .ConditionTrue ,
204
+ },
205
+ },
206
+ },
207
+ },
208
+ gvk : schema.GroupVersionKind {Group : "apps" , Version : "v1" , Kind : "StatefulSet" },
209
+ expected : APIDisabled ,
210
+ },
211
+ {
212
+ name : "API disabled - empty APIEnablements with complete condition" ,
213
+ cluster : & Cluster {
214
+ Status : ClusterStatus {
215
+ APIEnablements : []APIEnablement {},
216
+ Conditions : []metav1.Condition {
217
+ {
218
+ Type : ClusterConditionCompleteAPIEnablements ,
219
+ Status : metav1 .ConditionTrue ,
220
+ },
221
+ },
222
+ },
223
+ },
224
+ gvk : schema.GroupVersionKind {Group : "apps" , Version : "v1" , Kind : "Deployment" },
225
+ expected : APIDisabled ,
226
+ },
227
+ {
228
+ name : "API enabled - custom resource found" ,
229
+ cluster : & Cluster {
230
+ Status : ClusterStatus {
231
+ APIEnablements : []APIEnablement {
232
+ {
233
+ GroupVersion : "example.com/v1alpha1" ,
234
+ Resources : []APIResource {
235
+ {Name : "customresources" , Kind : "CustomResource" },
236
+ },
237
+ },
238
+ },
239
+ Conditions : []metav1.Condition {
240
+ {
241
+ Type : ClusterConditionCompleteAPIEnablements ,
242
+ Status : metav1 .ConditionTrue ,
243
+ },
244
+ },
245
+ },
246
+ },
247
+ gvk : schema.GroupVersionKind {Group : "example.com" , Version : "v1alpha1" , Kind : "CustomResource" },
248
+ expected : APIEnabled ,
249
+ },
250
+ }
251
+
252
+ for _ , tt := range tests {
253
+ t .Run (tt .name , func (t * testing.T ) {
254
+ result := tt .cluster .APIEnablement (tt .gvk )
255
+ if result != tt .expected {
256
+ t .Errorf ("APIEnablement() = %v, want %v" , result , tt .expected )
257
+ }
258
+ })
259
+ }
260
+ }
0 commit comments