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 pathunmount_test.go
More file actions
208 lines (173 loc) · 5.04 KB
/
Copy pathunmount_test.go
File metadata and controls
208 lines (173 loc) · 5.04 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/koding/logging"
"koding/klient/remote/restypes"
//"koding/klientctl/klientctlerrors"
"koding/klientctl/list"
"koding/klientctl/util/testutil"
"koding/mountcli"
. "github.com/smartystreets/goconvey/convey"
)
var discardLogger logging.Logger
type mockMountFinder struct {
Path string
Error error
LastCalledWith string
}
func (m *mockMountFinder) FindMountedPathByName(s string) (string, error) {
m.LastCalledWith = s
return m.Path, m.Error
}
func init() {
discardLogger = logging.NewLogger("test")
discardLogger.SetHandler(logging.NewWriterHandler(ioutil.Discard))
}
func TestUnmountRemoveMountFolder(t *testing.T) {
Convey("Given a folder with a file in it", t, func() {
tmpDir, err := ioutil.TempDir("", "non-empty-folder")
So(err, ShouldBeNil)
defer os.RemoveAll(tmpDir)
err = ioutil.WriteFile(filepath.Join(tmpDir, "file"), []byte("foo"), 0755)
So(err, ShouldBeNil)
var stdout bytes.Buffer
c := &UnmountCommand{
Options: UnmountOptions{
Path: tmpDir,
},
Log: discardLogger,
Stdout: &stdout,
fileRemover: os.Remove,
}
Convey("It should fail removing the folder", func() {
So(c.removeMountFolder(), ShouldNotBeNil)
})
Convey("It should print a warning", func() {
c.removeMountFolder()
So(stdout.String(), ShouldContainSubstring, UnmountFailedRemoveMountPath)
})
os.RemoveAll(tmpDir)
})
Convey("Given a folder with a dir in it", t, func() {
tmpDir, err := ioutil.TempDir("", "non-empty-folder")
So(err, ShouldBeNil)
defer os.RemoveAll(tmpDir)
err = os.Mkdir(filepath.Join(tmpDir, "embedded-dir"), 0755)
So(err, ShouldBeNil)
var stdout bytes.Buffer
c := &UnmountCommand{
Options: UnmountOptions{
Path: tmpDir,
},
Log: discardLogger,
Stdout: &stdout,
fileRemover: os.Remove,
}
Convey("Then it should fail removing the folder", func() {
So(c.removeMountFolder(), ShouldNotBeNil)
})
Convey("It should print a warning", func() {
c.removeMountFolder()
So(stdout.String(), ShouldContainSubstring, UnmountFailedRemoveMountPath)
})
os.RemoveAll(tmpDir)
})
Convey("Given a folder that is set as NeverRemoved", t, func() {
tmpDir, err := ioutil.TempDir("", "non-empty-folder")
So(err, ShouldBeNil)
defer os.RemoveAll(tmpDir)
var stdout bytes.Buffer
c := &UnmountCommand{
Options: UnmountOptions{
Path: tmpDir,
NeverRemove: []string{"/foo", "/bar", tmpDir},
},
Log: discardLogger,
Stdout: &stdout,
fileRemover: os.Remove,
}
Convey("Then it should fail removing the folder", func() {
err := c.removeMountFolder()
So(err, ShouldNotBeNil)
So(strings.ToLower(err.Error()), ShouldContainSubstring, "restricted")
})
Convey("It should print a warning", func() {
c.removeMountFolder()
So(stdout.String(), ShouldContainSubstring, AttemptedRemoveRestrictedPath)
})
})
Convey("Given no path", t, func() {
var stdout bytes.Buffer
c := &UnmountCommand{
Log: discardLogger,
Stdout: &stdout,
fileRemover: os.Remove,
}
Convey("It should fail.", func() {
err := c.removeMountFolder()
So(err, ShouldNotBeNil)
So(err.Error(), ShouldContainSubstring, "empty")
})
Convey("It should warn the user", func() {
c.removeMountFolder()
So(stdout.String(), ShouldContainSubstring, UnmountFailedRemoveMountPath)
})
})
}
func TestUnmountFindMountAndPath(t *testing.T) {
Convey("Given an UnmountCommand", t, func() {
fakeKlient := &testutil.FakeKlient{}
var stdout bytes.Buffer
c := &UnmountCommand{
Klient: fakeKlient,
Log: discardLogger,
Stdout: &stdout,
mountFinder: &testutil.FakeMountcli{
ReturnMountByPathErr: mountcli.ErrNoMountName,
},
}
Convey("Given a machine name that does not exist", func() {
c.Options.MountName = "bar"
fakeKlient.ReturnInfos = []list.KiteInfo{{ListMachineInfo: restypes.ListMachineInfo{
VMName: "foo",
}},
}
Convey("It should fail and inform the user", func() {
err := c.findMountAndPath()
So(err, ShouldNotBeNil)
So(stdout.String(), ShouldContainSubstring, MachineNotFound)
})
})
Convey("Given a machine that has mounts", func() {
c.Options.MountName = "foo"
fakeKlient.ReturnInfos = []list.KiteInfo{{ListMachineInfo: restypes.ListMachineInfo{
VMName: "foo",
// Content doesn't matter, just length
Mounts: []restypes.ListMountInfo{{}},
}},
}
Convey("It should not return an error", func() {
So(c.findMountAndPath(), ShouldBeNil)
})
})
Convey("Given a machine that has no mounts", func() {
c.Options.MountName = "foo"
fakeKlient.ReturnInfos = []list.KiteInfo{{ListMachineInfo: restypes.ListMachineInfo{
VMName: "foo",
// Content doesn't matter, just length
Mounts: []restypes.ListMountInfo{},
}},
}
Convey("It should fail and inform the user", func() {
err := c.findMountAndPath()
So(err, ShouldNotBeNil)
So(stdout.String(), ShouldContainSubstring, MountNotFound)
})
})
})
}