Skip to content

Commit 41e6f04

Browse files
committed
add unit tests
Signed-off-by: Alexander Minbaev <minbaev@gmail.com>
1 parent c8f4b10 commit 41e6f04

File tree

5 files changed

+386
-42
lines changed

5 files changed

+386
-42
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package mounter
2+
3+
import "errors"
4+
5+
type fakemountpointMounter struct {
6+
bucketName string
7+
objPath string
8+
endPoint string
9+
locConstraint string
10+
authType string
11+
accessKeys string
12+
kpRootKeyCrn string
13+
uid string
14+
gid string
15+
16+
isFailedMount bool
17+
}
18+
19+
func fakenewMountpointMounter(isFailedMount bool) Mounter {
20+
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,
31+
}
32+
}
33+
34+
func (mnpt *fakemountpointMounter) Mount(source string, target string) error {
35+
if mnpt.isFailedMount {
36+
return errors.New("failed to mount mountpoint")
37+
}
38+
return nil
39+
}
40+
41+
func (mnpt *fakemountpointMounter) Unmount(target string) error {
42+
return nil
43+
}

pkg/mounter/mounter-mountpoint.go

Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -23,53 +23,48 @@ import (
2323
)
2424

2525
// Mounter interface defined in mounter.go
26-
// mntS3Mounter Implements Mounter
27-
type mntS3Mounter struct {
28-
bucketName string //From Secret in SC
29-
objPath string //From Secret in SC
30-
endPoint string //From Secret in SC
31-
locConstraint string //From Secret in SC
32-
authType string
33-
accessKey string
34-
secretKey string
35-
mountOptions []string
36-
MounterUtils utils.MounterUtils
26+
// MountpointMounter Implements Mounter
27+
type MountpointMounter struct {
28+
BucketName string //From Secret in SC
29+
ObjPath string //From Secret in SC
30+
EndPoint string //From Secret in SC
31+
AccessKey string
32+
SecretKey string
33+
MountOptions []string
34+
MounterUtils utils.MounterUtils
3735
}
3836

39-
func NewMntS3Mounter(secretMap map[string]string, mountOptions []string, mounterUtils utils.MounterUtils) Mounter {
40-
klog.Info("-newMntS3Mounter-")
37+
func NewMountpointMounter(secretMap map[string]string, mountOptions []string, mounterUtils utils.MounterUtils) Mounter {
38+
klog.Info("-newMountpointMounter-")
4139

4240
var (
4341
val string
4442
check bool
45-
mounter *mntS3Mounter
43+
mounter *MountpointMounter
4644
)
4745

48-
mounter = &mntS3Mounter{}
46+
mounter = &MountpointMounter{}
4947

5048
if val, check = secretMap["cosEndpoint"]; check {
51-
mounter.endPoint = val
52-
}
53-
if val, check = secretMap["locationConstraint"]; check {
54-
mounter.locConstraint = val
49+
mounter.EndPoint = val
5550
}
5651
if val, check = secretMap["bucketName"]; check {
57-
mounter.bucketName = val
52+
mounter.BucketName = val
5853
}
5954
if val, check = secretMap["objPath"]; check {
60-
mounter.objPath = val
55+
mounter.ObjPath = val
6156
}
6257
if val, check = secretMap["accessKey"]; check {
63-
mounter.accessKey = val
58+
mounter.AccessKey = val
6459
}
6560
if val, check = secretMap["secretKey"]; check {
66-
mounter.secretKey = val
61+
mounter.SecretKey = val
6762
}
6863

69-
klog.Infof("newMntS3Mounter args:\n\tbucketName: [%s]\n\tobjPath: [%s]\n\tendPoint: [%s]\n\tlocationConstraint: [%s]\n\tauthType: [%s]",
70-
mounter.bucketName, mounter.objPath, mounter.endPoint, mounter.locConstraint, mounter.authType)
64+
klog.Infof("newMntS3Mounter args:\n\tbucketName: [%s]\n\tobjPath: [%s]\n\tendPoint: [%s]",
65+
mounter.BucketName, mounter.ObjPath, mounter.EndPoint)
7166

72-
mounter.mountOptions = mountOptions
67+
mounter.MountOptions = mountOptions
7368
mounter.MounterUtils = mounterUtils
7469

7570
return mounter
@@ -80,48 +75,48 @@ const (
8075
metaRootMntS3 = "/var/lib/ibmc-mntS3"
8176
)
8277

83-
func (mntS3 *mntS3Mounter) Stage(stagePath string) error {
78+
func (mntS3 *MountpointMounter) Stage(stagePath string) error {
8479
return nil
8580
}
86-
func (mntS3 *mntS3Mounter) Unstage(stagePath string) error {
81+
func (mntS3 *MountpointMounter) Unstage(stagePath string) error {
8782
return nil
8883
}
89-
func (mntS3 *mntS3Mounter) Mount(source string, target string) error {
90-
klog.Info("-MntS3Mounter Mount-")
84+
func (mntS3 *MountpointMounter) Mount(source string, target string) error {
85+
klog.Info("-MountpointMounter Mount-")
9186
klog.Infof("Mount args:\n\tsource: <%s>\n\ttarget: <%s>", source, target)
9287
var pathExist bool
9388
var err error
9489
metaPath := path.Join(metaRootMntS3, fmt.Sprintf("%x", sha256.Sum256([]byte(target))))
9590

9691
if pathExist, err = checkPath(metaPath); err != nil {
97-
klog.Errorf("MntS3Mounter Mount: Cannot stat directory %s: %v", metaPath, err)
92+
klog.Errorf("MountpointMounter Mount: Cannot stat directory %s: %v", metaPath, err)
9893
return err
9994
}
10095

10196
if !pathExist {
102-
if err = os.MkdirAll(metaPath, 0755); // #nosec G301: used for mntS3
97+
if err = mkdirAll(metaPath, 0755); // #nosec G301: used for mntS3
10398
err != nil {
104-
klog.Errorf("MntS3Mounter Mount: Cannot create directory %s: %v", metaPath, err)
99+
klog.Errorf("MountpointMounter Mount: Cannot create directory %s: %v", metaPath, err)
105100
return err
106101
}
107102
}
108103

109-
os.Setenv("AWS_ACCESS_KEY_ID", mntS3.accessKey)
110-
os.Setenv("AWS_SECRET_ACCESS_KEY", mntS3.secretKey)
104+
os.Setenv("AWS_ACCESS_KEY_ID", mntS3.AccessKey)
105+
os.Setenv("AWS_SECRET_ACCESS_KEY", mntS3.SecretKey)
111106

112107
args := []string{
113-
fmt.Sprintf("--endpoint-url=%v", mntS3.endPoint),
114-
mntS3.bucketName,
108+
fmt.Sprintf("--endpoint-url=%v", mntS3.EndPoint),
109+
mntS3.BucketName,
115110
target,
116111
}
117112

118-
if mntS3.objPath != "" {
119-
args = append(args, fmt.Sprintf("--prefix %s", mntS3.objPath))
113+
if mntS3.ObjPath != "" {
114+
args = append(args, fmt.Sprintf("--prefix %s", mntS3.ObjPath))
120115
}
121116
return mntS3.MounterUtils.FuseMount(target, mntS3Cmd, args)
122117
}
123-
func (mntS3 *mntS3Mounter) Unmount(target string) error {
124-
klog.Info("-MntS3Mounter Unmount-")
118+
func (mntS3 *MountpointMounter) Unmount(target string) error {
119+
klog.Info("-MountpointMounter Unmount-")
125120
metaPath := path.Join(metaRootMntS3, fmt.Sprintf("%x", sha256.Sum256([]byte(target))))
126121
err := os.RemoveAll(metaPath)
127122
if err != nil {

0 commit comments

Comments
 (0)