This repository was archived by the owner on Aug 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 605
Expand file tree
/
Copy pathmountcli_test.go
More file actions
139 lines (117 loc) · 4.02 KB
/
Copy pathmountcli_test.go
File metadata and controls
139 lines (117 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package mountcli
import (
"path/filepath"
"regexp"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
const inputDarwin = `
mount1 on /path/mount1 (osxfusefs, nodev, nosuid, synchronous, mounted by username)
mount2 on /path/mount2 (osxfusefs, nodev, nosuid, synchronous, mounted by username)
`
const inputLinux = `
mount1 on /path/mount1 type fuse (rw,nosuid,nodev,allow_other)
mount2 on /path/mount2 type fuse (rw,nosuid,nodev,allow_other)
`
// generateBinRunner returns a binRunner func which simply returns whatever string
// it was originally given.
func generateBinRunner(s string) func(string, string) (string, error) {
return func(string, string) (string, error) {
return s, nil
}
}
func TestMount(t *testing.T) {
clis := map[string]*Mountcli{
"Darwin": {
binRunner: generateBinRunner(inputDarwin),
matcher: regexp.MustCompile("^(.*?) on (.*?) \\(osxfusefs,"),
filterTag: "osxfusefs",
},
"Linux": {
binRunner: generateBinRunner(inputLinux),
matcher: regexp.MustCompile("^(.*?) on (.*?) type fuse"),
filterTag: "fuse",
},
}
for name, m := range clis {
Convey("Mount"+" "+name, t, func() {
Convey("GetAllMountedPaths", func() {
Convey("It should return all matching paths", func() {
paths, err := m.GetAllMountedPaths()
So(err, ShouldBeNil)
So(paths, ShouldResemble, []string{"/path/mount1", "/path/mount2"})
})
})
Convey("FindMountedPathByName", func() {
Convey("It should return the matching path", func() {
path, err := m.FindMountedPathByName("mount1")
So(err, ShouldBeNil)
So(path, ShouldEqual, "/path/mount1")
})
Convey("It should return err on no match", func() {
_, err := m.FindMountedPathByName("nomatch")
So(err, ShouldEqual, ErrNoMountName)
})
})
Convey("FindMountNameByPath", func() {
Convey("It should return the matching path", func() {
machine, err := m.FindMountNameByPath("/path/mount1")
So(err, ShouldBeNil)
So(machine, ShouldEqual, "mount1")
machine, err = m.FindMountNameByPath("/path/mount2")
So(err, ShouldBeNil)
So(machine, ShouldEqual, "mount2")
})
Convey("It should return err on no match", func() {
_, err := m.FindMountNameByPath("/unknownpath")
So(err, ShouldEqual, ErrNoMountPath)
})
Convey("It should return the machine mount on root path", func() {
machine, err := m.FindMountNameByPath("/path/mount1/another/another")
So(err, ShouldBeNil)
So(machine, ShouldEqual, "mount1")
})
Convey("It should work with trailing slashes", func() {
machine, err := m.FindMountNameByPath("/path/mount1/")
So(err, ShouldBeNil)
So(machine, ShouldEqual, "mount1")
machine, err = m.FindMountNameByPath("/path/mount1/another/")
So(err, ShouldBeNil)
So(machine, ShouldEqual, "mount1")
})
})
mounts, err := m.GetAllMountedPaths()
So(err, ShouldBeNil)
So(len(mounts), ShouldBeGreaterThan, 0)
m1 := mounts[0]
Convey("GetRelativeMountPath", func() {
Convey("It should return empty string if path is same as mount", func() {
relative, err := m.FindRelativeMountPath(m1)
So(err, ShouldBeNil)
So(relative, ShouldEqual, "")
})
Convey("It should return relative path if path is inside the mount", func() {
relative, err := m.FindRelativeMountPath(filepath.Join(m1, "a/b/c"))
So(err, ShouldBeNil)
So(relative, ShouldEqual, "a/b/c")
})
Convey("It should return error if path is not inside mount", func() {
_, err := m.FindRelativeMountPath("unknownpath")
So(err, ShouldEqual, ErrNotInMount)
})
})
Convey("IsPathInMountedPath", func() {
Convey("It should return false if path is not inside mount", func() {
inside, err := m.IsPathInMountedPath("unknownpath")
So(err, ShouldBeNil)
So(inside, ShouldEqual, false)
})
Convey("It should return true if path is inside mount", func() {
inside, err := m.IsPathInMountedPath(filepath.Join(m1, "a"))
So(err, ShouldBeNil)
So(inside, ShouldEqual, true)
})
})
})
}
}