1
2
3
4 package storage
5
6 import (
7 "context"
8 "os"
9 "testing"
10
11 "github.com/pkg/errors"
12 "golang.org/x/sys/unix"
13 )
14
15 func clearTestDependencies() {
16 osStat = nil
17 unixUnmount = nil
18 osRemoveAll = nil
19 }
20
21 func Test_Unmount_Stat_Valid_Path(t *testing.T) {
22 clearTestDependencies()
23
24 expectedName := "/dev/fake"
25 osStat = func(name string) (os.FileInfo, error) {
26 if expectedName != name {
27 t.Errorf("expected name: %s, got: %s", expectedName, name)
28 return nil, errors.New("unexpected name")
29 }
30 return nil, nil
31 }
32 unixUnmount = func(target string, flags int) error {
33 return nil
34 }
35 err := UnmountPath(context.Background(), expectedName, false)
36 if err != nil {
37 t.Fatalf("expected nil error, got: %v", err)
38 }
39 }
40
41 func Test_Unmount_Stat_NotExist(t *testing.T) {
42 clearTestDependencies()
43
44
45 osStat = func(name string) (os.FileInfo, error) {
46 return nil, os.ErrNotExist
47 }
48 err := UnmountPath(context.Background(), "/dev/fake", false)
49 if err != nil {
50 t.Fatalf("expected nil error, got: %v", err)
51 }
52 }
53
54 func Test_Unmount_Stat_OtherError_Error(t *testing.T) {
55 clearTestDependencies()
56
57 expectedErr := errors.New("expected stat err")
58 osStat = func(name string) (os.FileInfo, error) {
59 return nil, expectedErr
60 }
61 err := UnmountPath(context.Background(), "/dev/fake", false)
62 if errors.Cause(err) != expectedErr {
63 t.Fatalf("expected err: %v, got: %v", expectedErr, err)
64 }
65 }
66
67 func Test_Unmount_Valid_Target(t *testing.T) {
68 clearTestDependencies()
69
70 osStat = func(name string) (os.FileInfo, error) {
71 return nil, nil
72 }
73 expectedTarget := "/dev/fake"
74 unixUnmount = func(target string, flags int) error {
75 if expectedTarget != target {
76 t.Errorf("expected target: %s, got: %s", expectedTarget, target)
77 return errors.New("unexpected target")
78 }
79 return nil
80 }
81 err := UnmountPath(context.Background(), expectedTarget, false)
82 if err != nil {
83 t.Fatalf("expected nil error, got: %v", err)
84 }
85 }
86
87 func Test_Unmount_Valid_Flags(t *testing.T) {
88 clearTestDependencies()
89
90 osStat = func(name string) (os.FileInfo, error) {
91 return nil, nil
92 }
93 unixUnmount = func(target string, flags int) error {
94 if flags != 0 {
95 t.Errorf("expected flags 0, got: %d", flags)
96 return errors.New("unexpected flags")
97 }
98 return nil
99 }
100 err := UnmountPath(context.Background(), "/fake/path", false)
101 if err != nil {
102 t.Fatalf("expected nil error, got: %v", err)
103 }
104 }
105
106 func Test_Unmount_NotMounted(t *testing.T) {
107 clearTestDependencies()
108
109 osStat = func(name string) (os.FileInfo, error) {
110 return nil, nil
111 }
112 unixUnmount = func(target string, flags int) error {
113 return unix.EINVAL
114 }
115 err := UnmountPath(context.Background(), "/dev/fake", false)
116 if err != nil {
117 t.Fatalf("expected nil error, got: %v", err)
118 }
119 }
120
121 func Test_Unmount_OtherError(t *testing.T) {
122 clearTestDependencies()
123
124 osStat = func(name string) (os.FileInfo, error) {
125 return nil, nil
126 }
127 expectedErr := errors.New("expected unmount error")
128 unixUnmount = func(target string, flags int) error {
129 return expectedErr
130 }
131 err := UnmountPath(context.Background(), "/dev/fake", false)
132 if errors.Cause(err) != expectedErr {
133 t.Fatalf("expected err: %v, got: %v", expectedErr, err)
134 }
135 }
136
137 func Test_Unmount_RemoveAll_Valid_Path(t *testing.T) {
138 clearTestDependencies()
139
140 osStat = func(name string) (os.FileInfo, error) {
141 return nil, nil
142 }
143 unixUnmount = func(target string, flags int) error {
144 return nil
145 }
146 expectedPath := "/fake/path"
147 osRemoveAll = func(path string) error {
148 if expectedPath != path {
149 t.Errorf("expected path %s, got: %s", expectedPath, path)
150 return errors.New("unexpected path")
151 }
152 return nil
153 }
154 err := UnmountPath(context.Background(), expectedPath, true)
155 if err != nil {
156 t.Fatalf("expected nil error, got: %v", err)
157 }
158 }
159
160 func Test_Unmount_RemoveAll_Called(t *testing.T) {
161 clearTestDependencies()
162
163 osStat = func(name string) (os.FileInfo, error) {
164 return nil, nil
165 }
166 unixUnmount = func(target string, flags int) error {
167 return nil
168 }
169 removeAllCalled := false
170 osRemoveAll = func(path string) error {
171 removeAllCalled = true
172 return nil
173 }
174 err := UnmountPath(context.Background(), "/fake/path", true)
175 if err != nil {
176 t.Fatalf("expected nil error, got: %v", err)
177 }
178 if !removeAllCalled {
179 t.Fatal("expected remove to be called")
180 }
181 }
182
183 func Test_UnmountAllInPath_Unmount_Order(t *testing.T) {
184 clearTestDependencies()
185 parent := "/fake"
186 child := "/fake/test"
187 listMounts = func(path string) ([]string, error) {
188 return []string{parent, child}, nil
189 }
190
191 osStat = func(name string) (os.FileInfo, error) {
192 return nil, nil
193 }
194
195 timesCalled := 0
196 unixUnmount = func(target string, flags int) error {
197 if timesCalled == 0 && target != child {
198 return errors.Errorf("expected to unmount %v first, got %v", child, target)
199 }
200 timesCalled += 1
201 return nil
202 }
203
204 osRemoveAll = func(path string) error {
205 return nil
206 }
207
208 err := UnmountAllInPath(context.Background(), parent, true)
209
210 if err != nil {
211 t.Fatalf("expected nil error, got: %v", err)
212 }
213 }
214
View as plain text