2016-10-24 23:26:54 -04:00
|
|
|
package opts
|
|
|
|
|
|
|
|
import (
|
2016-11-08 00:32:21 -05:00
|
|
|
"os"
|
2022-03-10 10:40:49 -05:00
|
|
|
"path/filepath"
|
2016-10-24 23:26:54 -04:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
mounttypes "github.com/docker/docker/api/types/mount"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2016-10-24 23:26:54 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
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"
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal(expected, mount.String()))
|
2016-10-24 23:26:54 -04:00
|
|
|
}
|
|
|
|
|
2022-03-10 10:40:49 -05:00
|
|
|
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]))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-24 23:26:54 -04:00
|
|
|
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
|
|
|
|
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, mount.Set(testcase))
|
2016-10-24 23:26:54 -04:00
|
|
|
|
|
|
|
mounts := mount.Value()
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Assert(t, is.Len(mounts, 1))
|
|
|
|
assert.Check(t, is.DeepEqual(mounttypes.Mount{
|
2016-10-24 23:26:54 -04:00
|
|
|
Type: mounttypes.TypeBind,
|
|
|
|
Source: "/source",
|
|
|
|
Target: "/target",
|
2018-03-05 18:53:52 -05:00
|
|
|
}, mounts[0]))
|
2016-10-24 23:26:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, mount.Set(testcase))
|
2016-10-24 23:26:54 -04:00
|
|
|
|
|
|
|
mounts := mount.Value()
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Assert(t, is.Len(mounts, 1))
|
|
|
|
assert.Check(t, is.DeepEqual(mounttypes.Mount{
|
2016-10-24 23:26:54 -04:00
|
|
|
Type: mounttypes.TypeVolume,
|
|
|
|
Source: "/source",
|
|
|
|
Target: "/target",
|
2018-03-05 18:53:52 -05:00
|
|
|
}, mounts[0]))
|
2016-10-24 23:26:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestMountOptDefaultType ensures that a mount without the type defaults to a
|
|
|
|
// volume mount.
|
|
|
|
func TestMountOptDefaultType(t *testing.T) {
|
|
|
|
var mount MountOpt
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, mount.Set("target=/target,source=/foo"))
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal(mounttypes.TypeVolume, mount.values[0].Type))
|
2016-10-24 23:26:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMountOptSetErrorNoTarget(t *testing.T) {
|
|
|
|
var mount MountOpt
|
2018-03-06 15:54:24 -05:00
|
|
|
assert.Error(t, mount.Set("type=volume,source=/foo"), "target is required")
|
2016-10-24 23:26:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMountOptSetErrorInvalidKey(t *testing.T) {
|
|
|
|
var mount MountOpt
|
2018-03-06 15:54:24 -05:00
|
|
|
assert.Error(t, mount.Set("type=volume,bogus=foo"), "unexpected key 'bogus' in 'bogus=foo'")
|
2016-10-24 23:26:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMountOptSetErrorInvalidField(t *testing.T) {
|
|
|
|
var mount MountOpt
|
2018-03-06 15:54:24 -05:00
|
|
|
assert.Error(t, mount.Set("type=volume,bogus"), "invalid field 'bogus' must be a key=value pair")
|
2016-10-24 23:26:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMountOptSetErrorInvalidReadOnly(t *testing.T) {
|
|
|
|
var mount MountOpt
|
2018-03-06 15:54:24 -05:00
|
|
|
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")
|
2016-10-24 23:26:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMountOptDefaultEnableReadOnly(t *testing.T) {
|
|
|
|
var m MountOpt
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo"))
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, !m.values[0].ReadOnly)
|
2016-10-24 23:26:54 -04:00
|
|
|
|
|
|
|
m = MountOpt{}
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo,readonly"))
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, m.values[0].ReadOnly)
|
2016-10-24 23:26:54 -04:00
|
|
|
|
|
|
|
m = MountOpt{}
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo,readonly=1"))
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, m.values[0].ReadOnly)
|
2016-10-24 23:26:54 -04:00
|
|
|
|
|
|
|
m = MountOpt{}
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo,readonly=true"))
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, m.values[0].ReadOnly)
|
2016-10-24 23:26:54 -04:00
|
|
|
|
|
|
|
m = MountOpt{}
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo,readonly=0"))
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, !m.values[0].ReadOnly)
|
2016-10-24 23:26:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMountOptVolumeNoCopy(t *testing.T) {
|
|
|
|
var m MountOpt
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, m.Set("type=volume,target=/foo,volume-nocopy"))
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("", m.values[0].Source))
|
2016-10-24 23:26:54 -04:00
|
|
|
|
|
|
|
m = MountOpt{}
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, m.Set("type=volume,target=/foo,source=foo"))
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, m.values[0].VolumeOptions == nil)
|
2016-10-24 23:26:54 -04:00
|
|
|
|
|
|
|
m = MountOpt{}
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, m.Set("type=volume,target=/foo,source=foo,volume-nocopy=true"))
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, m.values[0].VolumeOptions != nil)
|
|
|
|
assert.Check(t, m.values[0].VolumeOptions.NoCopy)
|
2016-10-24 23:26:54 -04:00
|
|
|
|
|
|
|
m = MountOpt{}
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, m.Set("type=volume,target=/foo,source=foo,volume-nocopy"))
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, m.values[0].VolumeOptions != nil)
|
|
|
|
assert.Check(t, m.values[0].VolumeOptions.NoCopy)
|
2016-10-24 23:26:54 -04:00
|
|
|
|
|
|
|
m = MountOpt{}
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, m.Set("type=volume,target=/foo,source=foo,volume-nocopy=1"))
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, m.values[0].VolumeOptions != nil)
|
|
|
|
assert.Check(t, m.values[0].VolumeOptions.NoCopy)
|
2016-10-24 23:26:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMountOptTypeConflict(t *testing.T) {
|
|
|
|
var m MountOpt
|
2018-03-06 14:03:47 -05:00
|
|
|
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")
|
2016-10-24 23:26:54 -04:00
|
|
|
}
|
2016-11-08 00:32:21 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, mount.Set(testcase))
|
2016-11-08 00:32:21 -05:00
|
|
|
|
|
|
|
mounts := mount.Value()
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Assert(t, is.Len(mounts, 1))
|
|
|
|
assert.Check(t, is.DeepEqual(mounttypes.Mount{
|
2016-11-08 00:32:21 -05:00
|
|
|
Type: mounttypes.TypeTmpfs,
|
|
|
|
Target: "/target",
|
|
|
|
TmpfsOptions: &mounttypes.TmpfsOptions{
|
|
|
|
SizeBytes: 1024 * 1024, // not 1000 * 1000
|
2022-09-30 13:13:22 -04:00
|
|
|
Mode: os.FileMode(0o700),
|
2016-11-08 00:32:21 -05:00
|
|
|
},
|
2018-03-05 18:53:52 -05:00
|
|
|
}, mounts[0]))
|
2016-11-08 00:32:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMountOptSetTmpfsError(t *testing.T) {
|
|
|
|
var m MountOpt
|
2018-03-06 14:03:47 -05:00
|
|
|
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")
|
2016-11-08 00:32:21 -05:00
|
|
|
}
|