-
Notifications
You must be signed in to change notification settings - Fork 3
/
cloudpather_test.go
429 lines (408 loc) · 9.86 KB
/
cloudpather_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
Copyright 2020 Kamal Nasser All rights reserved.
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 kubectldoweb
import (
"bytes"
"context"
"fmt"
"testing"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"
restclient "k8s.io/client-go/rest"
)
func newFakeDOCloudPather() *DOCloudPather {
return &DOCloudPather{
clientset: fake.NewSimpleClientset(),
output: &bytes.Buffer{},
}
}
func TestDOCloudPather_Cluster(t *testing.T) {
cp := newFakeDOCloudPather()
id := "random-uuid"
tests := []struct {
name string
clientConfig *restclient.Config
want string
wantErr bool
}{
{
name: "valid DOKS cluster",
clientConfig: &restclient.Config{
Host: fmt.Sprintf("https://%s%s", id, hostnameSuffix),
},
want: fmt.Sprintf("kubernetes/clusters/%s", id),
wantErr: false,
},
{
name: "non-DOKS cluster",
clientConfig: &restclient.Config{
Host: fmt.Sprintf("https://%s.example.com", id),
},
want: "",
wantErr: true,
},
{
name: "invalid host",
clientConfig: &restclient.Config{
Host: "http://a b.com/",
},
want: "",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cp.clientConfig = tt.clientConfig
got, err := cp.Cluster(context.TODO())
if (err != nil) != tt.wantErr {
t.Errorf("DOCloudPather.Cluster() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("DOCloudPather.Cluster() = %v, want %v", got, tt.want)
}
})
}
}
func TestDOCloudPather_Node(t *testing.T) {
ctx := context.TODO()
id := "random-id"
nodeName := "node-1"
tests := []struct {
name string
nodeName string
node *corev1.Node
want string
wantErr bool
}{
{
name: "inexistent node",
nodeName: nodeName,
node: nil,
want: "",
wantErr: true,
},
{
name: "valid DOKS node",
nodeName: nodeName,
node: &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: nodeName,
},
Spec: corev1.NodeSpec{
ProviderID: fmt.Sprintf("%s%s", nodeIDPrefix, id),
},
},
want: fmt.Sprintf("droplets/%s", id),
wantErr: false,
},
{
name: "non-DOKS node",
nodeName: nodeName,
node: &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: nodeName,
},
Spec: corev1.NodeSpec{
ProviderID: fmt.Sprintf("whomst://%s", id),
},
},
want: "",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cp := newFakeDOCloudPather()
if tt.node != nil {
cp.clientset.CoreV1().Nodes().Create(ctx, tt.node, metav1.CreateOptions{})
}
got, err := cp.Node(ctx, tt.nodeName)
if (err != nil) != tt.wantErr {
t.Errorf("DOCloudPather.Node() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("DOCloudPather.Node() = %v, want %v", got, tt.want)
}
})
}
}
func TestDOCloudPather_Service(t *testing.T) {
ctx := context.TODO()
id := "random-id"
namespace := "ns"
svcName := "svc-1"
tests := []struct {
name string
serviceName string
service *corev1.Service
want string
wantErr bool
}{
{
name: "inexistent service",
serviceName: svcName,
service: nil,
want: "",
wantErr: true,
},
{
name: "valid DOKS LB service",
serviceName: svcName,
service: &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: svcName,
Annotations: map[string]string{
lbaasAnnotation: id,
},
},
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeLoadBalancer,
},
},
want: fmt.Sprintf("networking/load_balancers/%s", id),
wantErr: false,
},
{
name: "non-DOKS LB service",
serviceName: svcName,
service: &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: svcName,
},
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeLoadBalancer,
},
},
want: "",
wantErr: true,
},
{
name: "non-LB service",
serviceName: svcName,
service: &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: svcName,
},
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeClusterIP,
},
},
want: "",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cp := newFakeDOCloudPather()
if tt.service != nil {
cp.clientset.CoreV1().Services(namespace).Create(ctx, tt.service, metav1.CreateOptions{})
}
got, err := cp.Service(ctx, namespace, tt.serviceName)
if (err != nil) != tt.wantErr {
t.Errorf("DOCloudPather.Service() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("DOCloudPather.Service() = %v, want %v", got, tt.want)
}
})
}
}
func TestDOCloudPather_PersistentVolume(t *testing.T) {
ctx := context.TODO()
pvName := "pv-1"
tests := []struct {
name string
pvName string
pv *corev1.PersistentVolume
want string
wantOutput string
wantErr bool
}{
{
name: "inexistent pv",
pvName: pvName,
pv: nil,
want: "",
wantOutput: "",
wantErr: true,
},
{
name: "valid DO CSI pv",
pvName: pvName,
pv: &corev1.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{
Name: pvName,
},
Spec: corev1.PersistentVolumeSpec{
StorageClassName: storageClassName,
},
},
want: "volumes",
wantOutput: fmt.Sprintf("PersistentVolume name: %s\n", pvName),
wantErr: false,
},
{
name: "non-DO CSI pv",
pvName: pvName,
pv: &corev1.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{
Name: pvName,
},
Spec: corev1.PersistentVolumeSpec{
StorageClassName: "whomst",
},
},
want: "",
wantOutput: "",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cp := newFakeDOCloudPather()
if tt.pv != nil {
cp.clientset.CoreV1().PersistentVolumes().Create(ctx, tt.pv, metav1.CreateOptions{})
}
got, err := cp.PersistentVolume(ctx, tt.pvName)
if (err != nil) != tt.wantErr {
t.Errorf("DOCloudPather.PersistentVolume() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("DOCloudPather.PersistentVolume() = %v, want %v", got, tt.want)
}
gotOutput := cp.output.(*bytes.Buffer).String()
if gotOutput != tt.wantOutput {
t.Errorf("DOCloudPather.PersistentVolume() output = %v, want %v", gotOutput, tt.wantOutput)
}
})
}
}
func TestDOCloudPather_PersistentVolumeClaim(t *testing.T) {
ctx := context.TODO()
pvName := "pv-1"
pvcName := "pvc-1"
namespace := "ns"
storageClassNameString := string(storageClassName)
nonDOStorageClassName := "whomst"
tests := []struct {
name string
pvcName string
pvc *corev1.PersistentVolumeClaim
want string
wantOutput string
wantErr bool
}{
{
name: "inexistent pvc",
pvcName: pvcName,
pvc: nil,
want: "",
wantOutput: "",
wantErr: true,
},
{
name: "unbound/pending pvc",
pvcName: pvcName,
pvc: &corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: pvcName,
},
Spec: corev1.PersistentVolumeClaimSpec{
StorageClassName: &storageClassNameString,
},
Status: corev1.PersistentVolumeClaimStatus{
Phase: corev1.ClaimPending,
},
},
want: "",
wantOutput: "",
wantErr: true,
},
{
name: "pvc with nil storage class",
pvcName: pvcName,
pvc: &corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: pvcName,
},
Spec: corev1.PersistentVolumeClaimSpec{},
},
want: "",
wantOutput: "",
wantErr: true,
},
{
name: "valid DO CSI bound pvc",
pvcName: pvcName,
pvc: &corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: pvcName,
},
Spec: corev1.PersistentVolumeClaimSpec{
VolumeName: pvName,
StorageClassName: &storageClassNameString,
},
Status: corev1.PersistentVolumeClaimStatus{
Phase: corev1.ClaimBound,
},
},
want: "volumes",
wantOutput: fmt.Sprintf("PersistentVolume name: %s\n", pvName),
wantErr: false,
},
{
name: "non-DO CSI bound pvc",
pvcName: pvcName,
pvc: &corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: pvcName,
},
Spec: corev1.PersistentVolumeClaimSpec{
VolumeName: pvName,
StorageClassName: &nonDOStorageClassName,
},
Status: corev1.PersistentVolumeClaimStatus{
Phase: corev1.ClaimBound,
},
},
want: "",
wantOutput: "",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cp := newFakeDOCloudPather()
if tt.pvc != nil {
cp.clientset.CoreV1().PersistentVolumeClaims(namespace).Create(ctx, tt.pvc, metav1.CreateOptions{})
}
got, err := cp.PersistentVolumeClaim(ctx, namespace, tt.pvcName)
if (err != nil) != tt.wantErr {
t.Errorf("DOCloudPather.PersistentVolumeClaim() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("DOCloudPather.PersistentVolumeClaim() = %v, want %v", got, tt.want)
}
gotOutput := cp.output.(*bytes.Buffer).String()
if gotOutput != tt.wantOutput {
t.Errorf("DOCloudPather.PersistentVolumeClaim() output = %v, want %v", gotOutput, tt.wantOutput)
}
})
}
}