Skip to content

Commit 0963173

Browse files
committed
unit tests
Signed-off-by: Ashima-Ashima1 <[email protected]>
1 parent 23d6b73 commit 0963173

File tree

2 files changed

+169
-33
lines changed

2 files changed

+169
-33
lines changed

controllers/recoverstalevolume_controller_test.go

Lines changed: 114 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package controllers
22

33
import (
44
"errors"
5+
"os"
56
"testing"
67

78
"github.com/stretchr/testify/assert"
89
"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"
911
fakeget "github.ibm.com/alchemy-containers/ibm-object-csi-driver-operator/controllers/fake/client_get"
1012
fakegetdeploy "github.ibm.com/alchemy-containers/ibm-object-csi-driver-operator/controllers/fake/client_get/deployment"
1113
fakegetpvc "github.ibm.com/alchemy-containers/ibm-object-csi-driver-operator/controllers/fake/client_get/pvc"
@@ -17,6 +19,7 @@ import (
1719
"k8s.io/apimachinery/pkg/runtime"
1820
"k8s.io/apimachinery/pkg/types"
1921
fakeK8s "k8s.io/client-go/kubernetes/fake"
22+
"k8s.io/client-go/rest"
2023
"sigs.k8s.io/controller-runtime/pkg/client"
2124
"sigs.k8s.io/controller-runtime/pkg/client/fake"
2225
"sigs.k8s.io/controller-runtime/pkg/reconcile"
@@ -41,6 +44,10 @@ var (
4144
{
4245
DeploymentName: testDeploymentName,
4346
},
47+
{
48+
DeploymentName: "deployment-not-existing",
49+
DeploymentNamespace: testDeploymentNamespace,
50+
},
4451
},
4552
},
4653
}
@@ -53,7 +60,7 @@ var (
5360
Spec: appsv1.DeploymentSpec{},
5461
}
5562

56-
deploymentPod = &corev1.Pod{
63+
deploymentPod1 = &corev1.Pod{
5764
ObjectMeta: metav1.ObjectMeta{
5865
Name: testDeploymentName + "-pod1",
5966
Namespace: testDeploymentNamespace,
@@ -62,25 +69,64 @@ var (
6269
NodeName: testNode1,
6370
Volumes: []corev1.Volume{
6471
{
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,
66101
VolumeSource: corev1.VolumeSource{
67102
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
68-
ClaimName: testPVCName,
103+
ClaimName: testPVCName2,
69104
},
70105
},
71106
},
72107
},
73108
},
74109
}
75110

76-
pvc = &corev1.PersistentVolumeClaim{
111+
pvc1 = &corev1.PersistentVolumeClaim{
77112
ObjectMeta: metav1.ObjectMeta{
78-
Name: testPVCName,
113+
Name: testPVCName1,
79114
Namespace: testDeploymentNamespace,
80115
},
81116
Spec: corev1.PersistentVolumeClaimSpec{
82117
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,
84130
},
85131
}
86132

@@ -129,8 +175,10 @@ func TestRecoverStaleVolumeReconcile(t *testing.T) {
129175
objects: []runtime.Object{
130176
recoverStaleVolumeCR,
131177
deployment,
132-
deploymentPod,
133-
pvc,
178+
deploymentPod1,
179+
deploymentPod2,
180+
pvc1,
181+
pvc2,
134182
nodeServerPod1,
135183
nodeServerPod2,
136184
nodeServerPod3,
@@ -179,8 +227,8 @@ func TestRecoverStaleVolumeReconcile(t *testing.T) {
179227
objects: []runtime.Object{
180228
recoverStaleVolumeCR,
181229
deployment,
182-
deploymentPod,
183-
pvc,
230+
deploymentPod1,
231+
pvc1,
184232
},
185233
clientFunc: func(objs []runtime.Object) client.WithWatch {
186234
return fake.NewClientBuilder().WithRuntimeObjects(objs...).Build()
@@ -260,7 +308,7 @@ func TestRecoverStaleVolumeReconcile(t *testing.T) {
260308
objects: []runtime.Object{
261309
recoverStaleVolumeCR,
262310
deployment,
263-
deploymentPod,
311+
deploymentPod1,
264312
},
265313
clientFunc: func(objs []runtime.Object) client.WithWatch {
266314
return fakegetpvc.NewClientBuilder().WithRuntimeObjects(objs...).Build()
@@ -276,8 +324,8 @@ func TestRecoverStaleVolumeReconcile(t *testing.T) {
276324
objects: []runtime.Object{
277325
recoverStaleVolumeCR,
278326
deployment,
279-
deploymentPod,
280-
pvc,
327+
deploymentPod1,
328+
pvc1,
281329
},
282330
clientFunc: func(objs []runtime.Object) client.WithWatch {
283331
return fakelistnodeserverpod.NewClientBuilder().WithRuntimeObjects(objs...).Build()
@@ -288,26 +336,48 @@ func TestRecoverStaleVolumeReconcile(t *testing.T) {
288336
expectedResp: reconcile.Result{},
289337
expectedErr: errors.New(ListError),
290338
},
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+
},
311381
}
312382

313383
for _, testcase := range testCases {
@@ -352,3 +422,14 @@ func TestContains(t *testing.T) {
352422
assert.True(t, res)
353423
})
354424
}
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+
}

controllers/test_variables.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package controllers
2+
3+
import (
4+
"context"
5+
6+
"github.ibm.com/alchemy-containers/ibm-object-csi-driver-operator/api/v1alpha1"
7+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8+
"k8s.io/apimachinery/pkg/runtime"
9+
"k8s.io/client-go/kubernetes/scheme"
10+
"sigs.k8s.io/controller-runtime/pkg/log"
11+
)
12+
13+
const (
14+
CreateError = "failed to create"
15+
DeleteError = "failed to delete"
16+
GetError = "failed to get"
17+
ListError = "failed to list"
18+
UpdateError = "failed to update"
19+
20+
NotFoundError = "not found"
21+
)
22+
23+
var (
24+
testLog = log.Log.WithName("test")
25+
testCtx = context.TODO()
26+
27+
currentTime = metav1.Now()
28+
29+
ibmObjectCSICRName = "test-csi-cr"
30+
ibmObjectCSICRNamespace = "test-namespace"
31+
ibmObjectCSIfinalizer = "ibmobjectcsi.objectdriver.csi.ibm.com"
32+
33+
recoverStaleVolCRName = "test-vol-cr"
34+
recoverStaleVolCRNamespace = "test-namespace"
35+
testDeploymentName = "test-deployment"
36+
testDeploymentNamespace = "default"
37+
testPVName1 = "test-pv-1"
38+
testPVName2 = "test-pv-2"
39+
testPVCName1 = "test-pvc-1"
40+
testPVCName2 = "test-pvc-2"
41+
testStorageClassName = "test-csi-storage-class"
42+
testNode1 = "test-node-1"
43+
testNode2 = "test-node-2"
44+
testNode3 = "test-node-3"
45+
)
46+
47+
var testNodeServerPodLogs = `I0207 09:02:14.466230 1 nodeserver.go:188] NodeGetVolumeStats: Request: {VolumeId:test-pv-1 VolumePath:/var/data/kubelet/pods/360289c4-6eca-4275-a9ce-14938335117e/volumes/kubernetes.io~csi/pvc-7400b05a-12a4-43ac-bfda-a5ce6445b6ef/mount StagingTargetPath: XXX_NoUnkeyedLiteral:{} XXX_unrecognized:[] XXX_sizecache:0}
48+
I0207 09:02:14.466270 1 nodeserver.go:198] NodeGetVolumeStats: Start getting Stats
49+
E0207 09:02:14.466335 1 server.go:158] GRPC error: transport endpoint is not connected`
50+
51+
func setupScheme() *runtime.Scheme {
52+
s := scheme.Scheme
53+
_ = v1alpha1.AddToScheme(s)
54+
return s
55+
}

0 commit comments

Comments
 (0)