@@ -2,10 +2,12 @@ package controllers
2
2
3
3
import (
4
4
"errors"
5
+ "os"
5
6
"testing"
6
7
7
8
"github.com/stretchr/testify/assert"
8
9
"github.ibm.com/alchemy-containers/ibm-object-csi-driver-operator/api/v1alpha1"
10
+ fakedelete "github.ibm.com/alchemy-containers/ibm-object-csi-driver-operator/controllers/fake/client_delete"
9
11
fakeget "github.ibm.com/alchemy-containers/ibm-object-csi-driver-operator/controllers/fake/client_get"
10
12
fakegetdeploy "github.ibm.com/alchemy-containers/ibm-object-csi-driver-operator/controllers/fake/client_get/deployment"
11
13
fakegetpvc "github.ibm.com/alchemy-containers/ibm-object-csi-driver-operator/controllers/fake/client_get/pvc"
@@ -17,6 +19,7 @@ import (
17
19
"k8s.io/apimachinery/pkg/runtime"
18
20
"k8s.io/apimachinery/pkg/types"
19
21
fakeK8s "k8s.io/client-go/kubernetes/fake"
22
+ "k8s.io/client-go/rest"
20
23
"sigs.k8s.io/controller-runtime/pkg/client"
21
24
"sigs.k8s.io/controller-runtime/pkg/client/fake"
22
25
"sigs.k8s.io/controller-runtime/pkg/reconcile"
41
44
{
42
45
DeploymentName : testDeploymentName ,
43
46
},
47
+ {
48
+ DeploymentName : "deployment-not-existing" ,
49
+ DeploymentNamespace : testDeploymentNamespace ,
50
+ },
44
51
},
45
52
},
46
53
}
53
60
Spec : appsv1.DeploymentSpec {},
54
61
}
55
62
56
- deploymentPod = & corev1.Pod {
63
+ deploymentPod1 = & corev1.Pod {
57
64
ObjectMeta : metav1.ObjectMeta {
58
65
Name : testDeploymentName + "-pod1" ,
59
66
Namespace : testDeploymentNamespace ,
@@ -62,25 +69,64 @@ var (
62
69
NodeName : testNode1 ,
63
70
Volumes : []corev1.Volume {
64
71
{
65
- Name : testPVName ,
72
+ Name : testPVName1 ,
73
+ VolumeSource : corev1.VolumeSource {
74
+ PersistentVolumeClaim : & corev1.PersistentVolumeClaimVolumeSource {
75
+ ClaimName : testPVCName1 ,
76
+ },
77
+ },
78
+ },
79
+ {
80
+ Name : "pvc-not-existing" ,
81
+ VolumeSource : corev1.VolumeSource {
82
+ PersistentVolumeClaim : & corev1.PersistentVolumeClaimVolumeSource {
83
+ ClaimName : "pv-not-existing" ,
84
+ },
85
+ },
86
+ },
87
+ },
88
+ },
89
+ }
90
+
91
+ deploymentPod2 = & corev1.Pod {
92
+ ObjectMeta : metav1.ObjectMeta {
93
+ Name : testDeploymentName + "-pod2" ,
94
+ Namespace : testDeploymentNamespace ,
95
+ },
96
+ Spec : corev1.PodSpec {
97
+ NodeName : testNode1 ,
98
+ Volumes : []corev1.Volume {
99
+ {
100
+ Name : testPVName2 ,
66
101
VolumeSource : corev1.VolumeSource {
67
102
PersistentVolumeClaim : & corev1.PersistentVolumeClaimVolumeSource {
68
- ClaimName : testPVCName ,
103
+ ClaimName : testPVCName2 ,
69
104
},
70
105
},
71
106
},
72
107
},
73
108
},
74
109
}
75
110
76
- pvc = & corev1.PersistentVolumeClaim {
111
+ pvc1 = & corev1.PersistentVolumeClaim {
77
112
ObjectMeta : metav1.ObjectMeta {
78
- Name : testPVCName ,
113
+ Name : testPVCName1 ,
79
114
Namespace : testDeploymentNamespace ,
80
115
},
81
116
Spec : corev1.PersistentVolumeClaimSpec {
82
117
StorageClassName : & testStorageClassName ,
83
- VolumeName : testPVName ,
118
+ VolumeName : testPVName1 ,
119
+ },
120
+ }
121
+
122
+ pvc2 = & corev1.PersistentVolumeClaim {
123
+ ObjectMeta : metav1.ObjectMeta {
124
+ Name : testPVCName2 ,
125
+ Namespace : testDeploymentNamespace ,
126
+ },
127
+ Spec : corev1.PersistentVolumeClaimSpec {
128
+ StorageClassName : & testStorageClassName ,
129
+ VolumeName : testPVName2 ,
84
130
},
85
131
}
86
132
@@ -129,8 +175,10 @@ func TestRecoverStaleVolumeReconcile(t *testing.T) {
129
175
objects : []runtime.Object {
130
176
recoverStaleVolumeCR ,
131
177
deployment ,
132
- deploymentPod ,
133
- pvc ,
178
+ deploymentPod1 ,
179
+ deploymentPod2 ,
180
+ pvc1 ,
181
+ pvc2 ,
134
182
nodeServerPod1 ,
135
183
nodeServerPod2 ,
136
184
nodeServerPod3 ,
@@ -179,8 +227,8 @@ func TestRecoverStaleVolumeReconcile(t *testing.T) {
179
227
objects : []runtime.Object {
180
228
recoverStaleVolumeCR ,
181
229
deployment ,
182
- deploymentPod ,
183
- pvc ,
230
+ deploymentPod1 ,
231
+ pvc1 ,
184
232
},
185
233
clientFunc : func (objs []runtime.Object ) client.WithWatch {
186
234
return fake .NewClientBuilder ().WithRuntimeObjects (objs ... ).Build ()
@@ -260,7 +308,7 @@ func TestRecoverStaleVolumeReconcile(t *testing.T) {
260
308
objects : []runtime.Object {
261
309
recoverStaleVolumeCR ,
262
310
deployment ,
263
- deploymentPod ,
311
+ deploymentPod1 ,
264
312
},
265
313
clientFunc : func (objs []runtime.Object ) client.WithWatch {
266
314
return fakegetpvc .NewClientBuilder ().WithRuntimeObjects (objs ... ).Build ()
@@ -276,8 +324,8 @@ func TestRecoverStaleVolumeReconcile(t *testing.T) {
276
324
objects : []runtime.Object {
277
325
recoverStaleVolumeCR ,
278
326
deployment ,
279
- deploymentPod ,
280
- pvc ,
327
+ deploymentPod1 ,
328
+ pvc1 ,
281
329
},
282
330
clientFunc : func (objs []runtime.Object ) client.WithWatch {
283
331
return fakelistnodeserverpod .NewClientBuilder ().WithRuntimeObjects (objs ... ).Build ()
@@ -288,26 +336,48 @@ func TestRecoverStaleVolumeReconcile(t *testing.T) {
288
336
expectedResp : reconcile.Result {},
289
337
expectedErr : errors .New (ListError ),
290
338
},
291
- // {
292
- // testCaseName: "Negative: Failed to get In Cluster Config",
293
- // objects: []runtime.Object{
294
- // recoverStaleVolumeCR,
295
- // deployment,
296
- // deploymentPod,
297
- // pvc,
298
- // nodeServerPod1,
299
- // nodeServerPod2,
300
- // nodeServerPod3,
301
- // },
302
- // clientFunc: func(objs []runtime.Object) client.WithWatch {
303
- // return fake.NewClientBuilder().WithRuntimeObjects(objs...).Build()
304
- // },
305
- // kubeClientFunc: func() (*KubernetesClient, error) {
306
- // return nil, rest.ErrNotInCluster
307
- // },
308
- // expectedResp: reconcile.Result{},
309
- // expectedErr: rest.ErrNotInCluster,
310
- // },
339
+ {
340
+ testCaseName : "Negative: Failed to get In Cluster Config" ,
341
+ objects : []runtime.Object {
342
+ recoverStaleVolumeCR ,
343
+ deployment ,
344
+ deploymentPod1 ,
345
+ pvc1 ,
346
+ nodeServerPod1 ,
347
+ nodeServerPod2 ,
348
+ nodeServerPod3 ,
349
+ },
350
+ clientFunc : func (objs []runtime.Object ) client.WithWatch {
351
+ return fake .NewClientBuilder ().WithRuntimeObjects (objs ... ).Build ()
352
+ },
353
+ kubeClientFunc : func () (* KubernetesClient , error ) {
354
+ return nil , rest .ErrNotInCluster
355
+ },
356
+ expectedResp : reconcile.Result {},
357
+ expectedErr : rest .ErrNotInCluster ,
358
+ },
359
+ {
360
+ testCaseName : "Negative: Failed to delete pod" ,
361
+ objects : []runtime.Object {
362
+ recoverStaleVolumeCR ,
363
+ deployment ,
364
+ deploymentPod1 ,
365
+ pvc1 ,
366
+ nodeServerPod1 ,
367
+ nodeServerPod2 ,
368
+ nodeServerPod3 ,
369
+ },
370
+ clientFunc : func (objs []runtime.Object ) client.WithWatch {
371
+ return fakedelete .NewClientBuilder ().WithRuntimeObjects (objs ... ).Build ()
372
+ },
373
+ kubeClientFunc : func () (* KubernetesClient , error ) {
374
+ return & KubernetesClient {
375
+ Clientset : fakeK8s .NewSimpleClientset (),
376
+ }, nil
377
+ },
378
+ expectedResp : reconcile.Result {},
379
+ expectedErr : errors .New (DeleteError ),
380
+ },
311
381
}
312
382
313
383
for _ , testcase := range testCases {
@@ -352,3 +422,14 @@ func TestContains(t *testing.T) {
352
422
assert .True (t , res )
353
423
})
354
424
}
425
+
426
+ func TestCreateK8sClient (t * testing.T ) {
427
+ t .Run ("" , func (t * testing.T ) {
428
+ os .Setenv ("KUBERNETES_SERVICE_HOST" , "test-service-host" )
429
+ os .Setenv ("KUBERNETES_SERVICE_PORT" , "test-service-port" )
430
+
431
+ client , err := createK8sClient ()
432
+ assert .Nil (t , client )
433
+ assert .Error (t , err )
434
+ })
435
+ }
0 commit comments