-
Notifications
You must be signed in to change notification settings - Fork 0
/
exportfs_test.go
241 lines (225 loc) · 7.18 KB
/
exportfs_test.go
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package nfsmanager
import (
"fmt"
"os/exec"
"reflect"
"testing"
)
func Test_exportFSCommandLine(t *testing.T) {
type args struct {
path string
host string
options []nfsOption
}
tests := []struct {
name string
args args
want []string
}{
{"No Options", args{"/foo/bar", "192.168.1.1", []nfsOption{}}, []string{"exportfs", "192.168.1.1:/foo/bar"}},
{"One extra-less option", args{"/foo/bar", "192.168.1.1", []nfsOption{NoRootSquash}}, []string{"exportfs", "192.168.1.1:/foo/bar", "-o", "no_root_squash"}},
{"Two extra-less options", args{"/foo/bar", "192.168.1.1", []nfsOption{NoRootSquash, InsecureLocks}}, []string{"exportfs", "192.168.1.1:/foo/bar", "-o", "no_root_squash,insecure_locks"}},
{"Two option: one extra-less, one with extras", args{"/foo/bar", "192.168.1.1", []nfsOption{NoRootSquash, FsID("some-id")}}, []string{"exportfs", "192.168.1.1:/foo/bar", "-o", "no_root_squash,fsid=some-id"}},
{"Option with multiple extras", args{"/foo/bar", "192.168.1.1", []nfsOption{Replicas("foo", "bar")}}, []string{"exportfs", "192.168.1.1:/foo/bar", "-o", "replicas=foo:bar"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := exportFSCommandLine(tt.args.path, tt.args.host, tt.args.options); !reflect.DeepEqual(got, tt.want) {
t.Errorf("exportFSCommandLine() = %v, want %v", got, tt.want)
}
})
}
}
func Test_unExportFSCommandLine(t *testing.T) {
type args struct {
path string
host string
}
tests := []struct {
name string
args args
want []string
}{
{"No Options", args{"/foo/bar", "192.168.1.1"}, []string{"exportfs", "-u", "192.168.1.1:/foo/bar"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := unExportFSCommandLine(tt.args.path, tt.args.host); !reflect.DeepEqual(got, tt.want) {
t.Errorf("exportFSCommandLine() = %v, want %v", got, tt.want)
}
})
}
}
func Test_nfsOptions(t *testing.T) {
tests := []struct {
name string
option nfsOption
want string
}{
{"Secure", Secure, "secure"},
{"RW", RW, "rw"},
{"ASync", ASync, "async"},
{"Sync", Sync, "sync"},
{"NoWDelay", NoWDelay, "no_wdelay"},
{"NoHide", NoHide, "nohide"},
{"CrossMnt", CrossMnt, "crossmnt"},
{"NoSubtreeCheck", NoSubtreeCheck, "no_subtree_check"},
{"InsecureLocks", InsecureLocks, "insecure_locks"},
{"NoAuthNLM", NoAuthNLM, "no_auth_nlm"},
{"SecureLocks", SecureLocks, "secure_locks"},
{"AuthNLM", AuthNLM, "auth_nlm"},
{"MountPoint with empty string arg", MountPoint(""), "mountpoint"},
{"MountPoint with arg", MountPoint("/foo/bar"), "mountpoint=/foo/bar"},
{"MP with empty string arg", MP(""), "mp"},
{"MP with arg", MP("/foo/bar"), "mp=/foo/bar"},
{"FsID", FsID("the-fs-id"), "fsid=the-fs-id"},
{"NoRDirPlus", NoRDirPlus, "nordirplus"},
{"Refer with no args", Refer(), ""},
{"Refer with args", Refer("foo", "bar"), "refer=foo:bar"},
{"Refer with args, first is empty string", Refer("", "bar"), "refer=bar"},
{"Refer with args, second is empty string", Refer("foo", ""), "refer=foo"},
{"Refer with args, all are empty strings", Refer("", ""), ""},
{"Replicas with no args", Replicas(), ""},
{"Replicas with args", Replicas("foo", "bar"), "replicas=foo:bar"},
{"Replicas with args, first is empty string", Replicas("", "bar"), "replicas=bar"},
{"Replicas with args, second is empty string", Replicas("foo", ""), "replicas=foo"},
{"Replicas with args, all are empty strings", Replicas("", ""), ""},
{"PNFS", PNFS, "pnfs"},
{"NoPNFS", NoPNFS, "no_pnfs"},
{"RootSquash", RootSquash, "root_squash"},
{"NoRootSquash", NoRootSquash, "no_root_squash"},
{"AllSquash", AllSquash, "all_squash"},
{"AnonUID", AnonUID(1234), "anonuid=1234"},
{"AnonGID", AnonGID(2345), "anongid=2345"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.option.string(); got != tt.want {
t.Errorf("option.string() = %v, want %v", got, tt.want)
}
})
}
}
func Test_runAndRetryWithSudoOnFailure(t *testing.T) {
succeed := func(name string, arg ...string) *exec.Cmd {
return exec.Command("true")
}
fail := func(name string, arg ...string) *exec.Cmd {
return exec.Command("false")
}
succeedOnlyWithSudo := func(name string, arg ...string) *exec.Cmd {
if name == "sudo" {
return succeed(name, arg...)
}
return fail(name, arg...)
}
type fields struct {
Command execCommander
}
type args struct {
path string
host string
options []nfsOption
}
tests := []struct {
name string
fields fields
wantErr bool
}{
{"Works without sudo", fields{succeed}, false},
{"Works with sudo", fields{succeedOnlyWithSudo}, false},
{"Fails either way", fields{fail}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := runAndRetryWithSudoOnFailure([]string{"true"}, tt.fields.Command); (err != nil) != tt.wantErr {
t.Errorf("nfsManager.ExportFs() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestNFSManager(t *testing.T) {
tests := []struct {
name string
want *nfsManager
}{
// TODO: Add test cases.
{"NFSManager", &nfsManager{Command: exec.Command}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := reflect.ValueOf(NFSManager().Command).Pointer()
b := reflect.ValueOf(exec.Command).Pointer()
if a != b {
t.Errorf("NFSManager's Command = %v, want %v (exec.Command)", a, b)
}
})
}
}
func Test_nfsManager_ExportFs(t *testing.T) {
type args struct {
path string
host string
options []nfsOption
}
tests := []struct {
name string
args args
wantErr bool
}{
{"Success", args{"/foo/bar", "the.client", []nfsOption{}}, false},
{"Failure", args{"/foo/bar", "the.client", []nfsOption{}}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
n := NFSManager()
commandRetrier := func(cmdLine []string, command execCommander) error {
want := exportFSCommandLine(tt.args.path, tt.args.host, tt.args.options)
if !reflect.DeepEqual(want, cmdLine) {
t.Errorf("Got cmdLine = %v, wanted %v", cmdLine, want)
}
if tt.wantErr {
return fmt.Errorf("Mock failure")
}
return nil
}
n.commandRetrier = commandRetrier
if err := n.ExportFs(tt.args.path, tt.args.host, tt.args.options...); (err != nil) != tt.wantErr {
t.Errorf("nfsManager.ExportFs() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func Test_nfsManager_UnExportFs(t *testing.T) {
type args struct {
path string
host string
}
tests := []struct {
name string
args args
wantErr bool
}{
{"Success", args{"/foo/bar", "the.client"}, false},
{"Failure", args{"/foo/bar", "the.client"}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
n := NFSManager()
commandRetrier := func(cmdLine []string, command execCommander) error {
want := unExportFSCommandLine(tt.args.path, tt.args.host)
if !reflect.DeepEqual(want, cmdLine) {
t.Errorf("Got cmdLine = %v, wanted %v", cmdLine, want)
}
if tt.wantErr {
return fmt.Errorf("Mock failure")
}
return nil
}
n.commandRetrier = commandRetrier
if err := n.UnExportFs(tt.args.path, tt.args.host); (err != nil) != tt.wantErr {
t.Errorf("nfsManager.ExportFs() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}