forked from docker/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mount_test.go
302 lines (266 loc) · 8.9 KB
/
mount_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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package opts
import (
"os"
"path/filepath"
"testing"
mounttypes "github.com/docker/docker/api/types/mount"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
func TestMountOptString(t *testing.T) {
mount := MountOpt{
values: []mounttypes.Mount{
{
Type: mounttypes.TypeBind,
Source: "/home/path",
Target: "/target",
},
{
Type: mounttypes.TypeVolume,
Source: "foo",
Target: "/target/foo",
},
},
}
expected := "bind /home/path /target, volume foo /target/foo"
assert.Check(t, is.Equal(expected, mount.String()))
}
func TestMountRelative(t *testing.T) {
for _, testcase := range []struct {
name string
path string
bind string
}{
{
name: "Current path",
path: ".",
bind: "type=bind,source=.,target=/target",
}, {
name: "Current path with slash",
path: "./",
bind: "type=bind,source=./,target=/target",
},
} {
t.Run(testcase.name, func(t *testing.T) {
var mount MountOpt
assert.NilError(t, mount.Set(testcase.bind))
mounts := mount.Value()
assert.Assert(t, is.Len(mounts, 1))
abs, err := filepath.Abs(testcase.path)
assert.NilError(t, err)
assert.Check(t, is.DeepEqual(mounttypes.Mount{
Type: mounttypes.TypeBind,
Source: abs,
Target: "/target",
}, mounts[0]))
})
}
}
func TestMountOptSetBindNoErrorBind(t *testing.T) {
for _, testcase := range []string{
// tests several aliases that should have same result.
"type=bind,target=/target,source=/source",
"type=bind,src=/source,dst=/target",
"type=bind,source=/source,dst=/target",
"type=bind,src=/source,target=/target",
} {
var mount MountOpt
assert.NilError(t, mount.Set(testcase))
mounts := mount.Value()
assert.Assert(t, is.Len(mounts, 1))
assert.Check(t, is.DeepEqual(mounttypes.Mount{
Type: mounttypes.TypeBind,
Source: "/source",
Target: "/target",
}, mounts[0]))
}
}
func TestMountOptSetVolumeNoError(t *testing.T) {
for _, testcase := range []string{
// tests several aliases that should have same result.
"type=volume,target=/target,source=/source",
"type=volume,src=/source,dst=/target",
"type=volume,source=/source,dst=/target",
"type=volume,src=/source,target=/target",
} {
var mount MountOpt
assert.NilError(t, mount.Set(testcase))
mounts := mount.Value()
assert.Assert(t, is.Len(mounts, 1))
assert.Check(t, is.DeepEqual(mounttypes.Mount{
Type: mounttypes.TypeVolume,
Source: "/source",
Target: "/target",
}, mounts[0]))
}
}
// TestMountOptDefaultType ensures that a mount without the type defaults to a
// volume mount.
func TestMountOptDefaultType(t *testing.T) {
var mount MountOpt
assert.NilError(t, mount.Set("target=/target,source=/foo"))
assert.Check(t, is.Equal(mounttypes.TypeVolume, mount.values[0].Type))
}
func TestMountOptSetErrorNoTarget(t *testing.T) {
var mount MountOpt
assert.Error(t, mount.Set("type=volume,source=/foo"), "target is required")
}
func TestMountOptSetErrorInvalidKey(t *testing.T) {
var mount MountOpt
assert.Error(t, mount.Set("type=volume,bogus=foo"), "unexpected key 'bogus' in 'bogus=foo'")
}
func TestMountOptSetErrorInvalidField(t *testing.T) {
var mount MountOpt
assert.Error(t, mount.Set("type=volume,bogus"), "invalid field 'bogus' must be a key=value pair")
}
func TestMountOptSetErrorInvalidReadOnly(t *testing.T) {
var mount MountOpt
assert.Error(t, mount.Set("type=volume,readonly=no"), "invalid value for readonly: no")
assert.Error(t, mount.Set("type=volume,readonly=invalid"), "invalid value for readonly: invalid")
}
func TestMountOptDefaultEnableReadOnly(t *testing.T) {
var m MountOpt
assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo"))
assert.Check(t, !m.values[0].ReadOnly)
m = MountOpt{}
assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo,readonly"))
assert.Check(t, m.values[0].ReadOnly)
m = MountOpt{}
assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo,readonly=1"))
assert.Check(t, m.values[0].ReadOnly)
m = MountOpt{}
assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo,readonly=true"))
assert.Check(t, m.values[0].ReadOnly)
m = MountOpt{}
assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo,readonly=0"))
assert.Check(t, !m.values[0].ReadOnly)
}
func TestMountOptVolumeNoCopy(t *testing.T) {
var m MountOpt
assert.NilError(t, m.Set("type=volume,target=/foo,volume-nocopy"))
assert.Check(t, is.Equal("", m.values[0].Source))
m = MountOpt{}
assert.NilError(t, m.Set("type=volume,target=/foo,source=foo"))
assert.Check(t, m.values[0].VolumeOptions == nil)
m = MountOpt{}
assert.NilError(t, m.Set("type=volume,target=/foo,source=foo,volume-nocopy=true"))
assert.Check(t, m.values[0].VolumeOptions != nil)
assert.Check(t, m.values[0].VolumeOptions.NoCopy)
m = MountOpt{}
assert.NilError(t, m.Set("type=volume,target=/foo,source=foo,volume-nocopy"))
assert.Check(t, m.values[0].VolumeOptions != nil)
assert.Check(t, m.values[0].VolumeOptions.NoCopy)
m = MountOpt{}
assert.NilError(t, m.Set("type=volume,target=/foo,source=foo,volume-nocopy=1"))
assert.Check(t, m.values[0].VolumeOptions != nil)
assert.Check(t, m.values[0].VolumeOptions.NoCopy)
}
func TestMountOptTypeConflict(t *testing.T) {
var m MountOpt
assert.ErrorContains(t, m.Set("type=bind,target=/foo,source=/foo,volume-nocopy=true"), "cannot mix")
assert.ErrorContains(t, m.Set("type=volume,target=/foo,source=/foo,bind-propagation=rprivate"), "cannot mix")
}
func TestMountOptSetTmpfsNoError(t *testing.T) {
for _, testcase := range []string{
// tests several aliases that should have same result.
"type=tmpfs,target=/target,tmpfs-size=1m,tmpfs-mode=0700",
"type=tmpfs,target=/target,tmpfs-size=1MB,tmpfs-mode=700",
} {
var mount MountOpt
assert.NilError(t, mount.Set(testcase))
mounts := mount.Value()
assert.Assert(t, is.Len(mounts, 1))
assert.Check(t, is.DeepEqual(mounttypes.Mount{
Type: mounttypes.TypeTmpfs,
Target: "/target",
TmpfsOptions: &mounttypes.TmpfsOptions{
SizeBytes: 1024 * 1024, // not 1000 * 1000
Mode: os.FileMode(0o700),
},
}, mounts[0]))
}
}
func TestMountOptSetTmpfsError(t *testing.T) {
var m MountOpt
assert.ErrorContains(t, m.Set("type=tmpfs,target=/foo,tmpfs-size=foo"), "invalid value for tmpfs-size")
assert.ErrorContains(t, m.Set("type=tmpfs,target=/foo,tmpfs-mode=foo"), "invalid value for tmpfs-mode")
assert.ErrorContains(t, m.Set("type=tmpfs"), "target is required")
}
func TestMountOptSetBindNonRecursive(t *testing.T) {
var mount MountOpt
assert.NilError(t, mount.Set("type=bind,source=/foo,target=/bar,bind-nonrecursive"))
assert.Check(t, is.DeepEqual([]mounttypes.Mount{
{
Type: mounttypes.TypeBind,
Source: "/foo",
Target: "/bar",
BindOptions: &mounttypes.BindOptions{
NonRecursive: true,
},
},
}, mount.Value()))
}
func TestMountOptSetBindRecursive(t *testing.T) {
t.Run("enabled", func(t *testing.T) {
var mount MountOpt
assert.NilError(t, mount.Set("type=bind,source=/foo,target=/bar,bind-recursive=enabled"))
assert.Check(t, is.DeepEqual([]mounttypes.Mount{
{
Type: mounttypes.TypeBind,
Source: "/foo",
Target: "/bar",
},
}, mount.Value()))
})
t.Run("disabled", func(t *testing.T) {
var mount MountOpt
assert.NilError(t, mount.Set("type=bind,source=/foo,target=/bar,bind-recursive=disabled"))
assert.Check(t, is.DeepEqual([]mounttypes.Mount{
{
Type: mounttypes.TypeBind,
Source: "/foo",
Target: "/bar",
BindOptions: &mounttypes.BindOptions{
NonRecursive: true,
},
},
}, mount.Value()))
})
t.Run("writable", func(t *testing.T) {
var mount MountOpt
assert.Error(t, mount.Set("type=bind,source=/foo,target=/bar,bind-recursive=writable"),
"option 'bind-recursive=writable' requires 'readonly' to be specified in conjunction")
assert.NilError(t, mount.Set("type=bind,source=/foo,target=/bar,bind-recursive=writable,readonly"))
assert.Check(t, is.DeepEqual([]mounttypes.Mount{
{
Type: mounttypes.TypeBind,
Source: "/foo",
Target: "/bar",
ReadOnly: true,
BindOptions: &mounttypes.BindOptions{
ReadOnlyNonRecursive: true,
},
},
}, mount.Value()))
})
t.Run("readonly", func(t *testing.T) {
var mount MountOpt
assert.Error(t, mount.Set("type=bind,source=/foo,target=/bar,bind-recursive=readonly"),
"option 'bind-recursive=readonly' requires 'readonly' to be specified in conjunction")
assert.Error(t, mount.Set("type=bind,source=/foo,target=/bar,bind-recursive=readonly,readonly"),
"option 'bind-recursive=readonly' requires 'bind-propagation=rprivate' to be specified in conjunction")
assert.NilError(t, mount.Set("type=bind,source=/foo,target=/bar,bind-recursive=readonly,readonly,bind-propagation=rprivate"))
assert.Check(t, is.DeepEqual([]mounttypes.Mount{
{
Type: mounttypes.TypeBind,
Source: "/foo",
Target: "/bar",
ReadOnly: true,
BindOptions: &mounttypes.BindOptions{
ReadOnlyForceRecursive: true,
Propagation: mounttypes.PropagationRPrivate,
},
},
}, mount.Value()))
})
}