Skip to content

Commit e90b9e4

Browse files
authored
Add Missing Unit Tests (#241)
* add uts Signed-off-by: Ashima-Ashima1 <[email protected]> * add uts Signed-off-by: Ashima-Ashima1 <[email protected]> * add uts Signed-off-by: Ashima-Ashima1 <[email protected]> * add uts Signed-off-by: Ashima-Ashima1 <[email protected]> * add uts Signed-off-by: Ashima-Ashima1 <[email protected]> * add uts Signed-off-by: Ashima-Ashima1 <[email protected]> --------- Signed-off-by: Ashima-Ashima1 <[email protected]>
1 parent 12012a9 commit e90b9e4

File tree

6 files changed

+141
-19
lines changed

6 files changed

+141
-19
lines changed

.secrets.baseline

Lines changed: 4 additions & 4 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-07-23T05:15:31Z",
6+
"generated_at": "2025-07-24T10:06:47Z",
77
"plugins_used": [
88
{
99
"name": "AWSKeyDetector"
@@ -182,15 +182,15 @@
182182
"hashed_secret": "39f69c278f46165447f30d10acf54277aaa3d5fc",
183183
"is_secret": false,
184184
"is_verified": false,
185-
"line_number": 95,
185+
"line_number": 98,
186186
"type": "Secret Keyword",
187187
"verified_result": null
188188
},
189189
{
190190
"hashed_secret": "2ace62c1befa19e3ea37dd52be9f6d508c5163e6",
191191
"is_secret": false,
192192
"is_verified": false,
193-
"line_number": 261,
193+
"line_number": 264,
194194
"type": "Secret Keyword",
195195
"verified_result": null
196196
}
@@ -199,7 +199,7 @@
199199
{
200200
"hashed_secret": "2e7a7ee14caebf378fc32d6cf6f557f347c96773",
201201
"is_verified": false,
202-
"line_number": 78,
202+
"line_number": 77,
203203
"type": "Secret Keyword",
204204
"verified_result": null
205205
}

pkg/driver/nodeserver_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,35 @@ func TestNodePublishVolume(t *testing.T) {
218218
expectedResp: nil,
219219
expectedErr: errors.New("failed to valid mount"),
220220
},
221+
{
222+
testCaseName: "Negative: s3 endpoint not provided",
223+
req: &csi.NodePublishVolumeRequest{
224+
VolumeId: testVolumeID,
225+
TargetPath: testTargetPath,
226+
VolumeCapability: &csi.VolumeCapability{
227+
AccessMode: &csi.VolumeCapability_AccessMode{
228+
Mode: volumeCapabilities[0],
229+
},
230+
},
231+
Secrets: map[string]string{
232+
"accessKey": "testAccessKey",
233+
"secretKey": "testSecretKey",
234+
"apiKey": "testApiKey", // pragma: allowlist secret
235+
"serviceId": "testServiceId",
236+
"kpRootKeyCRN": "testKpRootKeyCRN",
237+
"iamEndpoint": "testIamEndpoint",
238+
"bucketName": bucketName,
239+
},
240+
},
241+
driverStatsUtils: utils.NewFakeStatsUtilsImpl(utils.FakeStatsUtilsFuncStruct{
242+
CheckMountFn: func(targetPath string) error {
243+
return nil
244+
},
245+
}),
246+
Mounter: &mounter.FakeMounterFactory{},
247+
expectedResp: nil,
248+
expectedErr: errors.New("S3 Service endpoint not provided"),
249+
},
221250
{
222251
testCaseName: "Negative: Failed to fetch PV",
223252
req: &csi.NodePublishVolumeRequest{

pkg/mounter/mounter-rclone.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ const (
5050
envAuth = "true"
5151
)
5252

53-
var createConfigWrap = createConfig
53+
var (
54+
createConfigWrap = createConfig
55+
removeConfigFile = removeRcloneConfigFile
56+
)
5457

5558
func NewRcloneMounter(secretMap map[string]string, mountOptions []string, mounterUtils utils.MounterUtils) Mounter {
5659
klog.Info("-newRcloneMounter-")
@@ -232,7 +235,7 @@ func (rclone *RcloneMounter) Unmount(target string) error {
232235
return err
233236
}
234237

235-
removeRcloneConfigFile(constants.MounterConfigPathOnHost, target)
238+
removeConfigFile(constants.MounterConfigPathOnHost, target)
236239
return nil
237240
}
238241
klog.Info("NodeServer Unmounting...")
@@ -242,7 +245,7 @@ func (rclone *RcloneMounter) Unmount(target string) error {
242245
return err
243246
}
244247

245-
removeRcloneConfigFile(constants.MounterConfigPathOnPodRclone, target)
248+
removeConfigFile(constants.MounterConfigPathOnPodRclone, target)
246249
return nil
247250
}
248251

@@ -347,7 +350,7 @@ func removeRcloneConfigFile(configPath, target string) {
347350
configPathWithVolID := path.Join(configPath, fmt.Sprintf("%x", sha256.Sum256([]byte(target))))
348351

349352
for retry := 1; retry <= maxRetries; retry++ {
350-
_, err := os.Stat(configPathWithVolID)
353+
_, err := Stat(configPathWithVolID)
351354
if err != nil {
352355
if os.IsNotExist(err) {
353356
klog.Infof("removeRcloneConfigFile: Config file directory does not exist: %s", configPathWithVolID)

pkg/mounter/mounter-rclone_test.go

Lines changed: 95 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package mounter
22

33
import (
44
"errors"
5-
"fmt"
65
"os"
76
"testing"
87

@@ -190,6 +189,8 @@ func TestRcloneMount_WorkerNode_Negative(t *testing.T) {
190189
func TestRcloneUnmount_NodeServer(t *testing.T) {
191190
mountWorker = false
192191

192+
removeConfigFile = func(_, _ string) {}
193+
193194
rclone := &RcloneMounter{MounterUtils: mounterUtils.NewFakeMounterUtilsImpl(mounterUtils.FakeMounterUtilsFuncStruct{
194195
FuseUnmountFn: func(path string) error {
195196
return nil
@@ -203,6 +204,8 @@ func TestRcloneUnmount_NodeServer(t *testing.T) {
203204
func TestRcloneUnmount_WorkerNode(t *testing.T) {
204205
mountWorker = true
205206

207+
removeConfigFile = func(_, _ string) {}
208+
206209
rclone := &RcloneMounter{MounterUtils: mounterUtils.NewFakeMounterUtilsImpl(mounterUtils.FakeMounterUtilsFuncStruct{
207210
FuseUnmountFn: func(path string) error {
208211
return nil
@@ -235,6 +238,22 @@ func TestRcloneUnmount_WorkerNode_Negative(t *testing.T) {
235238
assert.Contains(t, err.Error(), "failed to create http request")
236239
}
237240

241+
func TestRcloneUnmount_NodeServer_Negative(t *testing.T) {
242+
mountWorker = false
243+
244+
removeConfigFile = func(_, _ string) {}
245+
246+
rclone := &RcloneMounter{MounterUtils: mounterUtils.NewFakeMounterUtilsImpl(mounterUtils.FakeMounterUtilsFuncStruct{
247+
FuseUnmountFn: func(path string) error {
248+
return errors.New("failed to unmount")
249+
},
250+
})}
251+
252+
err := rclone.Unmount(target)
253+
assert.Error(t, err)
254+
assert.Contains(t, err.Error(), "failed to unmount")
255+
}
256+
238257
func TestCreateConfig_Success(t *testing.T) {
239258
rclone := &RcloneMounter{
240259
AccessKeys: "accessKey:secretKey",
@@ -247,7 +266,7 @@ func TestCreateConfig_Success(t *testing.T) {
247266

248267
func TestCreateConfig_MakeDirFails(t *testing.T) {
249268
MakeDir = func(string, os.FileMode) error {
250-
return fmt.Errorf("mkdir failed")
269+
return errors.New("mkdir failed")
251270
}
252271
err := createConfig("/tmp/testconfig", &RcloneMounter{})
253272
assert.ErrorContains(t, err, "mkdir failed")
@@ -256,7 +275,7 @@ func TestCreateConfig_MakeDirFails(t *testing.T) {
256275
func TestCreateConfig_FileCreateFails(t *testing.T) {
257276
MakeDir = func(string, os.FileMode) error { return nil }
258277
CreateFile = func(string) (*os.File, error) {
259-
return nil, fmt.Errorf("file create failed")
278+
return nil, errors.New("file create failed")
260279
}
261280
err := createConfig("/tmp/testconfig", &RcloneMounter{})
262281
assert.ErrorContains(t, err, "file create failed")
@@ -268,8 +287,80 @@ func TestCreateConfig_ChmodFails(t *testing.T) {
268287
return os.CreateTemp("", "test")
269288
}
270289
Chmod = func(string, os.FileMode) error {
271-
return fmt.Errorf("chmod failed")
290+
return errors.New("chmod failed")
272291
}
273292
err := createConfig("/tmp/testconfig", &RcloneMounter{})
274293
assert.ErrorContains(t, err, "chmod failed")
275294
}
295+
296+
func TestRemoveRcloneConfigFile_PathNotExists(t *testing.T) {
297+
Stat = func(path string) (os.FileInfo, error) {
298+
return nil, os.ErrNotExist
299+
}
300+
defer func() {
301+
Stat = os.Stat
302+
}()
303+
304+
removeRcloneConfigFile("/test", target)
305+
}
306+
307+
func TestRemoveRcloneConfigFile_StatRetryThenSuccess(t *testing.T) {
308+
attempt := 0
309+
Stat = func(_ string) (os.FileInfo, error) {
310+
if attempt == 0 {
311+
attempt++
312+
return nil, errors.New("stat error")
313+
}
314+
return nil, nil
315+
}
316+
defer func() {
317+
Stat = os.Stat
318+
}()
319+
320+
RemoveAll = func(_ string) error {
321+
return nil
322+
}
323+
324+
removeRcloneConfigFile("/test1", target)
325+
}
326+
327+
func TestRemoveRcloneConfigFile_RemoveRetryThenSuccess(t *testing.T) {
328+
Stat = func(_ string) (os.FileInfo, error) {
329+
return nil, nil
330+
}
331+
332+
attempt := 0
333+
RemoveAll = func(_ string) error {
334+
if attempt == 0 {
335+
attempt++
336+
return errors.New("remove error")
337+
}
338+
return nil
339+
}
340+
341+
defer func() {
342+
Stat = os.Stat
343+
RemoveAll = os.RemoveAll
344+
}()
345+
346+
removeRcloneConfigFile("/test", target)
347+
}
348+
349+
func TestRemoveRcloneConfigFile_Negative(t *testing.T) {
350+
called := 0
351+
Stat = func(_ string) (os.FileInfo, error) {
352+
return nil, nil
353+
}
354+
RemoveAll = func(_ string) error {
355+
called++
356+
return errors.New("remove failed")
357+
}
358+
359+
defer func() {
360+
Stat = os.Stat
361+
RemoveAll = os.RemoveAll
362+
}()
363+
364+
removeRcloneConfigFile("/test", target)
365+
assert.Equal(t, maxRetries, called)
366+
}

pkg/mounter/mounter-s3fs_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,26 +240,26 @@ func TestRemoveS3FSCredFile_PathNotExists(t *testing.T) {
240240

241241
func TestRemoveS3FSCredFile_StatRetryThenSuccess(t *testing.T) {
242242
attempt := 0
243-
Stat = func(path string) (os.FileInfo, error) {
243+
Stat = func(_ string) (os.FileInfo, error) {
244244
if attempt == 0 {
245245
attempt++
246246
return nil, errors.New("stat error")
247247
}
248248
return nil, nil
249249
}
250-
Remove = func(path string) error {
250+
RemoveAll = func(_ string) error {
251251
return nil
252252
}
253253

254254
removeS3FSCredFile("/test", target)
255255
}
256256

257257
func TestRemoveS3FSCredFile_RemoveRetryThenSuccess(t *testing.T) {
258-
Stat = func(path string) (os.FileInfo, error) {
258+
Stat = func(_ string) (os.FileInfo, error) {
259259
return nil, nil
260260
}
261261
attempt := 0
262-
Remove = func(path string) error {
262+
RemoveAll = func(_ string) error {
263263
if attempt == 0 {
264264
attempt++
265265
return errors.New("remove error")
@@ -272,10 +272,10 @@ func TestRemoveS3FSCredFile_RemoveRetryThenSuccess(t *testing.T) {
272272

273273
func TestRemoveS3FSCredFile_Negative(t *testing.T) {
274274
called := 0
275-
Stat = func(path string) (os.FileInfo, error) {
275+
Stat = func(_ string) (os.FileInfo, error) {
276276
return nil, nil
277277
}
278-
RemoveAll = func(path string) error {
278+
RemoveAll = func(_ string) error {
279279
called++
280280
return errors.New("remove failed")
281281
}

pkg/mounter/mounter.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ var (
2626
CreateFile = os.Create
2727
Chmod = os.Chmod
2828
Stat = os.Stat
29-
Remove = os.Remove
3029
RemoveAll = os.RemoveAll
3130
)
3231

0 commit comments

Comments
 (0)