Skip to content

Commit baa0462

Browse files
committed
Added unit tests for file package
1 parent e7a0ef3 commit baa0462

File tree

2 files changed

+1158
-5
lines changed

2 files changed

+1158
-5
lines changed

Diff for: pkg/file/file.go

+35-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
/*
2+
Copyright © 2025 Dell Inc. or its subsidiaries. All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the License for the specific language governing permissions and
12+
limitations under the License.
13+
*/
14+
115
package file
216

317
import (
@@ -295,13 +309,13 @@ func StageFileSystem(ctx context.Context, reqID, symID, fsID string, privTgt str
295309
// staging already done
296310
return &csi.NodeStageVolumeResponse{}, nil
297311
}
298-
if err := os.MkdirAll(privTgt, 0o750); err != nil {
312+
if err := MkdirAll(privTgt, 0o750); err != nil {
299313
return nil, status.Errorf(codes.Internal,
300314
"can't create target folder %s: %s", privTgt, err.Error())
301315
}
302316
log.Info("stage path successfully created")
303317

304-
if err := gofsutil.Mount(ctx, nfsExportPath, privTgt, "nfs"); err != nil {
318+
if err := Mount(ctx, nfsExportPath, privTgt, "nfs"); err != nil {
305319
return nil, status.Errorf(codes.Internal,
306320
"error mount nfs share %s to target path: %s", nfsExportPath, err.Error())
307321
}
@@ -344,7 +358,7 @@ func PublishFileSystem(ctx context.Context, req *csi.NodePublishVolumeRequest, r
344358
return &csi.NodePublishVolumeResponse{}, nil
345359
}
346360

347-
if err := os.MkdirAll(targetPath, 0o750); err != nil {
361+
if err := MkdirAll(targetPath, 0o750); err != nil {
348362
return nil, status.Errorf(codes.Internal,
349363
"can't create target folder %s: %s", targetPath, err.Error())
350364
}
@@ -357,7 +371,7 @@ func PublishFileSystem(ctx context.Context, req *csi.NodePublishVolumeRequest, r
357371
mntFlags = append(mntFlags, "ro")
358372
}
359373
stagingPath := req.GetStagingTargetPath()
360-
if err := gofsutil.BindMount(ctx, stagingPath, targetPath, mntFlags...); err != nil {
374+
if err := BindMount(ctx, stagingPath, targetPath, mntFlags...); err != nil {
361375
return nil, status.Errorf(codes.Internal,
362376
"error bind disk %s to target path %s: err %s", stagingPath, targetPath, err.Error())
363377
}
@@ -438,7 +452,7 @@ func isAlreadyPublished(targetPath string) (bool, error) {
438452

439453
func getTargetMount(target string) (gofsutil.Info, bool, error) {
440454
var targetMount gofsutil.Info
441-
mounts, err := gofsutil.GetMounts(context.Background())
455+
mounts, err := GetMounts(context.Background())
442456
if err != nil {
443457
log.Error("could not reliably determine existing mount status")
444458
return targetMount, false, status.Error(codes.Internal, "could not reliably determine existing mount status")
@@ -453,3 +467,19 @@ func getTargetMount(target string) (gofsutil.Info, bool, error) {
453467
}
454468
return targetMount, found, nil
455469
}
470+
471+
var GetMounts = func(ctx context.Context) ([]gofsutil.Info, error) {
472+
return gofsutil.GetMounts(ctx)
473+
}
474+
475+
var Mount = func(ctx context.Context, source string, target string, fstype string, options ...string) error {
476+
return gofsutil.Mount(ctx, source, target, fstype, options...)
477+
}
478+
479+
var BindMount = func(ctx context.Context, source string, target string, options ...string) error {
480+
return gofsutil.BindMount(ctx, source, target, options...)
481+
}
482+
483+
var MkdirAll = func(path string, perm os.FileMode) error {
484+
return os.MkdirAll(path, perm)
485+
}

0 commit comments

Comments
 (0)