Skip to content

Commit b883d73

Browse files
authored
NodeServer UTs (#195)
* minor updates for uts Signed-off-by: Ashima-Ashima1 <[email protected]> * node server UTs Signed-off-by: Ashima-Ashima1 <[email protected]> * node server UTs Signed-off-by: Ashima-Ashima1 <[email protected]> --------- Signed-off-by: Ashima-Ashima1 <[email protected]>
1 parent 26f9fd1 commit b883d73

File tree

10 files changed

+131
-59
lines changed

10 files changed

+131
-59
lines changed

.secrets.baseline

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"files": "go.sum|^.secrets.baseline$",
44
"lines": null
55
},
6-
"generated_at": "2025-06-09T06:34:04Z",
6+
"generated_at": "2025-06-11T14:13:17Z",
77
"plugins_used": [
88
{
99
"name": "AWSKeyDetector"
@@ -270,7 +270,7 @@
270270
{
271271
"hashed_secret": "c7c6508b19455e3e8040e60e9833fbede92e5d8e",
272272
"is_verified": false,
273-
"line_number": 343,
273+
"line_number": 353,
274274
"type": "Secret Keyword",
275275
"verified_result": null
276276
}

pkg/constants/constants.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const (
3636
MounterConfigPathOnPodRclone = "/root/.config/rclone"
3737

3838
IsNodeServer = "IS_NODE_SERVER"
39+
KubeNodeName = "KUBE_NODE_NAME"
3940
)
4041

4142
var (

pkg/driver/nodeserver.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ func (ns *nodeServer) NodeUnpublishVolume(_ context.Context, req *csi.NodeUnpubl
177177
}
178178
klog.Infof("Unmounting target path %s", targetPath)
179179

180-
attrib, err := utils.GetPVAttributes(volumeID)
180+
attrib, err := ns.Stats.GetPVAttributes(volumeID)
181181
if err != nil {
182182
return nil, status.Error(codes.NotFound, "Failed to get PV details")
183183
}
@@ -286,7 +286,7 @@ func (ns *nodeServer) NodeGetCapabilities(_ context.Context, req *csi.NodeGetCap
286286
func (ns *nodeServer) NodeGetInfo(_ context.Context, req *csi.NodeGetInfoRequest) (*csi.NodeGetInfoResponse, error) {
287287
klog.V(3).Infof("NodeGetInfo: called with args %+v", req)
288288

289-
nodeName := os.Getenv("KUBE_NODE_NAME")
289+
nodeName := os.Getenv(constants.KubeNodeName)
290290
if nodeName == "" {
291291
return nil, fmt.Errorf("KUBE_NODE_NAME env variable not set")
292292
}

pkg/driver/nodeserver_test.go

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package driver
1818

1919
import (
2020
"errors"
21+
"fmt"
2122
"os"
2223
"reflect"
2324
"testing"
@@ -337,26 +338,32 @@ func TestNodePublishVolume(t *testing.T) {
337338
}
338339
}
339340

340-
/*
341341
func TestNodeUnpublishVolume(t *testing.T) {
342342
testCases := []struct {
343-
testCaseName string
344-
req *csi.NodeUnpublishVolumeRequest
345-
mounterUtils mounterUtils.MounterUtils
346-
expectedResp *csi.NodeUnpublishVolumeResponse
347-
expectedErr error
343+
testCaseName string
344+
req *csi.NodeUnpublishVolumeRequest
345+
driverStatsUtils utils.StatsUtils
346+
Mounter mounter.NewMounterFactory
347+
expectedResp *csi.NodeUnpublishVolumeResponse
348+
expectedErr error
348349
}{
349350
{
350351
testCaseName: "Positive: Successful",
351352
req: &csi.NodeUnpublishVolumeRequest{
352353
VolumeId: testVolumeID,
353354
TargetPath: testTargetPath,
354355
},
355-
mounterUtils: mounterUtils.NewFakeMounterUtilsImpl(mounterUtils.FakeMounterUtilsFuncStruct{
356-
FuseUnmountFn: func(path string) error {
357-
return nil
356+
driverStatsUtils: utils.NewFakeStatsUtilsImpl(utils.FakeStatsUtilsFuncStruct{
357+
GetPVAttributesFn: func(volumeID string) (map[string]string, error) {
358+
return map[string]string{
359+
"mounter": "s3fs",
360+
}, nil
358361
},
359362
}),
363+
Mounter: &mounter.FakeMounterFactory{
364+
Mounter: constants.S3FS,
365+
IsFailedUnmount: false,
366+
},
360367
expectedResp: &csi.NodeUnpublishVolumeResponse{},
361368
expectedErr: nil,
362369
},
@@ -374,27 +381,48 @@ func TestNodeUnpublishVolume(t *testing.T) {
374381
expectedResp: nil,
375382
expectedErr: errors.New("Target path missing in request"),
376383
},
384+
{
385+
testCaseName: "Negative: Failed to get PV details",
386+
req: &csi.NodeUnpublishVolumeRequest{
387+
VolumeId: testVolumeID,
388+
TargetPath: testTargetPath,
389+
},
390+
driverStatsUtils: utils.NewFakeStatsUtilsImpl(utils.FakeStatsUtilsFuncStruct{
391+
GetPVAttributesFn: func(volumeID string) (map[string]string, error) {
392+
return nil, errors.New("pv not found")
393+
},
394+
}),
395+
expectedResp: nil,
396+
expectedErr: errors.New("Failed to get PV details"),
397+
},
377398
{
378399
testCaseName: "Negative: Unmount failed",
379400
req: &csi.NodeUnpublishVolumeRequest{
380401
VolumeId: testVolumeID,
381402
TargetPath: testTargetPath,
382403
},
383-
mounterUtils: mounterUtils.NewFakeMounterUtilsImpl(mounterUtils.FakeMounterUtilsFuncStruct{
384-
FuseUnmountFn: func(path string) error {
385-
return errors.New("cannot force unmount")
404+
driverStatsUtils: utils.NewFakeStatsUtilsImpl(utils.FakeStatsUtilsFuncStruct{
405+
GetPVAttributesFn: func(volumeID string) (map[string]string, error) {
406+
return map[string]string{
407+
"mounter": "s3fs",
408+
}, nil
386409
},
387410
}),
411+
Mounter: &mounter.FakeMounterFactory{
412+
Mounter: constants.S3FS,
413+
IsFailedUnmount: true,
414+
},
388415
expectedResp: nil,
389-
expectedErr: errors.New("cannot force unmount"),
416+
expectedErr: errors.New("failed to unmount s3fs"),
390417
},
391418
}
392419

393420
for _, tc := range testCases {
394421
t.Log("Testcase being executed", zap.String("testcase", tc.testCaseName))
395422

396423
nodeServer := nodeServer{
397-
MounterUtils: tc.mounterUtils,
424+
Stats: tc.driverStatsUtils,
425+
Mounter: tc.Mounter,
398426
}
399427
actualResp, actualErr := nodeServer.NodeUnpublishVolume(ctx, tc.req)
400428

@@ -410,7 +438,6 @@ func TestNodeUnpublishVolume(t *testing.T) {
410438
}
411439
}
412440
}
413-
*/
414441

415442
func TestNodeGetVolumeStats(t *testing.T) {
416443
testCases := []struct {
@@ -618,14 +645,16 @@ func TestNodeGetCapabilities(t *testing.T) {
618645
func TestNodeGetInfo(t *testing.T) {
619646
testCases := []struct {
620647
testCaseName string
648+
envKubeNodeName string
621649
driverStatsUtils utils.StatsUtils
622650
req *csi.NodeGetInfoRequest
623651
expectedResp *csi.NodeGetInfoResponse
624652
expectedErr error
625653
}{
626654
{
627-
testCaseName: "Positive: Successful",
628-
req: &csi.NodeGetInfoRequest{},
655+
testCaseName: "Positive: Successful",
656+
envKubeNodeName: "testNode",
657+
req: &csi.NodeGetInfoRequest{},
629658
driverStatsUtils: utils.NewFakeStatsUtilsImpl(utils.FakeStatsUtilsFuncStruct{
630659
GetRegionAndZoneFn: func(nodeName string) (string, string, error) {
631660
return "test-region", "test-zone", nil
@@ -643,18 +672,35 @@ func TestNodeGetInfo(t *testing.T) {
643672
},
644673
expectedErr: nil,
645674
},
675+
{
676+
testCaseName: "Negative: Failed to get KUBE_NODE_NAME env variable",
677+
envKubeNodeName: "",
678+
driverStatsUtils: &utils.DriverStatsUtils{},
679+
req: &csi.NodeGetInfoRequest{},
680+
expectedResp: nil,
681+
expectedErr: errors.New("KUBE_NODE_NAME env variable not set"),
682+
},
683+
{
684+
testCaseName: "Negative: Failed to get region and zone",
685+
envKubeNodeName: "testNode",
686+
driverStatsUtils: &utils.DriverStatsUtils{},
687+
req: &csi.NodeGetInfoRequest{},
688+
expectedResp: nil,
689+
expectedErr: errors.New("unable to load in-cluster configuration"),
690+
},
646691
}
647692

648693
for _, tc := range testCases {
649694
t.Log("Testcase being executed", zap.String("testcase", tc.testCaseName))
650695

651-
_ = os.Setenv("KUBE_NODE_NAME", "testNode")
696+
_ = os.Setenv(constants.KubeNodeName, tc.envKubeNodeName)
652697

653698
nodeServer := nodeServer{
654699
NodeID: testNodeID,
655700
Stats: tc.driverStatsUtils,
656701
}
657702
actualResp, actualErr := nodeServer.NodeGetInfo(ctx, tc.req)
703+
fmt.Println(actualErr)
658704

659705
if tc.expectedErr != nil {
660706
assert.Error(t, actualErr)

pkg/mounter/fake_mounter-rclone.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,23 @@ type fakercloneMounter struct {
1313
uid string
1414
gid string
1515

16-
isFailedMount bool
16+
isFailedMount bool
17+
isFailedUnmount bool
1718
}
1819

19-
func fakenewRcloneMounter(isFailedMount bool) Mounter {
20+
func fakenewRcloneMounter(isFailedMount, isFailedUnmount bool) Mounter {
2021
return &fakercloneMounter{
21-
bucketName: bucketName,
22-
objPath: objPath,
23-
endPoint: endPoint,
24-
locConstraint: region,
25-
accessKeys: keys,
26-
authType: authType,
27-
kpRootKeyCrn: "",
28-
uid: "",
29-
gid: "",
30-
isFailedMount: isFailedMount,
22+
bucketName: bucketName,
23+
objPath: objPath,
24+
endPoint: endPoint,
25+
locConstraint: region,
26+
accessKeys: keys,
27+
authType: authType,
28+
kpRootKeyCrn: "",
29+
uid: "",
30+
gid: "",
31+
isFailedMount: isFailedMount,
32+
isFailedUnmount: isFailedUnmount,
3133
}
3234
}
3335

@@ -39,5 +41,8 @@ func (rclone *fakercloneMounter) Mount(source string, target string) error {
3941
}
4042

4143
func (rclone *fakercloneMounter) Unmount(target string) error {
44+
if rclone.isFailedUnmount {
45+
return errors.New("failed to unmount rclone")
46+
}
4247
return nil
4348
}

pkg/mounter/fake_mounter-s3fs.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,21 @@ type fakes3fsMounter struct {
1111
accessKeys string
1212
kpRootKeyCrn string
1313

14-
isFailedMount bool
14+
isFailedMount bool
15+
isFailedUnmount bool
1516
}
1617

17-
func fakenewS3fsMounter(isFailedMount bool) Mounter {
18+
func fakenewS3fsMounter(isFailedMount, isFailedUnmount bool) Mounter {
1819
return &fakes3fsMounter{
19-
bucketName: bucketName,
20-
objPath: objPath,
21-
endPoint: endPoint,
22-
locConstraint: region,
23-
accessKeys: keys,
24-
authType: authType,
25-
kpRootKeyCrn: "",
26-
isFailedMount: isFailedMount,
20+
bucketName: bucketName,
21+
objPath: objPath,
22+
endPoint: endPoint,
23+
locConstraint: region,
24+
accessKeys: keys,
25+
authType: authType,
26+
kpRootKeyCrn: "",
27+
isFailedMount: isFailedMount,
28+
isFailedUnmount: isFailedUnmount,
2729
}
2830
}
2931

@@ -35,5 +37,8 @@ func (s3fs *fakes3fsMounter) Mount(source string, target string) error {
3537
}
3638

3739
func (s3fs *fakes3fsMounter) Unmount(target string) error {
40+
if s3fs.isFailedUnmount {
41+
return errors.New("failed to unmount s3fs")
42+
}
3843
return nil
3944
}

pkg/mounter/fake_mounter.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,18 @@ const (
1212
)
1313

1414
type FakeMounterFactory struct {
15-
Mounter string
16-
IsFailedMount bool
15+
Mounter string
16+
IsFailedMount bool
17+
IsFailedUnmount bool
1718
}
1819

1920
func (f *FakeMounterFactory) NewMounter(attrib map[string]string, secretMap map[string]string, mountFlags []string) Mounter {
2021
switch f.Mounter {
2122
case constants.S3FS:
22-
return fakenewS3fsMounter(f.IsFailedMount)
23+
return fakenewS3fsMounter(f.IsFailedMount, f.IsFailedUnmount)
2324
case constants.RClone:
24-
return fakenewRcloneMounter(f.IsFailedMount)
25+
return fakenewRcloneMounter(f.IsFailedMount, f.IsFailedUnmount)
2526
default:
26-
return fakenewS3fsMounter(f.IsFailedMount)
27+
return fakenewS3fsMounter(f.IsFailedMount, f.IsFailedUnmount)
2728
}
2829
}

pkg/utils/driver_utils.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type StatsUtils interface {
3030
GetBucketUsage(volumeID string) (int64, error)
3131
GetBucketNameFromPV(volumeID string) (string, error)
3232
GetRegionAndZone(nodeName string) (string, string, error)
33+
GetPVAttributes(volumeID string) (map[string]string, error)
3334
}
3435

3536
type DriverStatsUtils struct {
@@ -160,6 +161,15 @@ func (su *DriverStatsUtils) GetBucketNameFromPV(volumeID string) (string, error)
160161
return tempBucketName, nil
161162
}
162163

164+
func (su *DriverStatsUtils) GetPVAttributes(volumeID string) (map[string]string, error) {
165+
pv, err := GetPV(volumeID)
166+
if err != nil {
167+
return nil, err
168+
}
169+
170+
return pv.Spec.CSI.VolumeAttributes, nil
171+
}
172+
163173
func ReplaceAndReturnCopy(req interface{}) (interface{}, error) {
164174
switch r := req.(type) {
165175
case *csi.CreateVolumeRequest:
@@ -350,12 +360,3 @@ func fetchSecretUsingPV(volumeID string) (*v1.Secret, error) {
350360
klog.Info("secret details found. secretName: ", secret.Name)
351361
return secret, nil
352362
}
353-
354-
func GetPVAttributes(volumeID string) (map[string]string, error) {
355-
pv, err := GetPV(volumeID)
356-
if err != nil {
357-
return nil, err
358-
}
359-
360-
return pv.Spec.CSI.VolumeAttributes, nil
361-
}

pkg/utils/fake_driver_utils.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type FakeStatsUtilsFuncStruct struct {
1010
GetBucketUsageFn func(volumeID string) (int64, error)
1111
GetBucketNameFromPVFn func(volumeID string) (string, error)
1212
GetRegionAndZoneFn func(nodeName string) (string, string, error)
13+
GetPVAttributesFn func(volumeID string) (map[string]string, error)
1314
}
1415

1516
type FakeStatsUtilsFuncStructImpl struct {
@@ -72,3 +73,10 @@ func (m *FakeStatsUtilsFuncStructImpl) GetRegionAndZone(nodeName string) (string
7273
}
7374
panic("requested method should not be nil")
7475
}
76+
77+
func (m *FakeStatsUtilsFuncStructImpl) GetPVAttributes(volumeID string) (map[string]string, error) {
78+
if m.FuncStruct.GetPVAttributesFn != nil {
79+
return m.FuncStruct.GetPVAttributesFn(volumeID)
80+
}
81+
panic("requested method should not be nil")
82+
}

0 commit comments

Comments
 (0)