2016-12-05 16:14:08 -05:00
|
|
|
package convert
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
composetypes "github.com/docker/cli/cli/compose/types"
|
2016-12-05 16:14:08 -05:00
|
|
|
"github.com/docker/docker/api/types/mount"
|
2018-03-05 18:53:52 -05:00
|
|
|
"github.com/gotestyourself/gotestyourself/assert"
|
|
|
|
is "github.com/gotestyourself/gotestyourself/assert/cmp"
|
2016-12-05 16:14:08 -05:00
|
|
|
)
|
|
|
|
|
2016-12-18 10:50:32 -05:00
|
|
|
func TestConvertVolumeToMountAnonymousVolume(t *testing.T) {
|
2017-01-27 16:56:45 -05:00
|
|
|
config := composetypes.ServiceVolumeConfig{
|
|
|
|
Type: "volume",
|
|
|
|
Target: "/foo/bar",
|
|
|
|
}
|
2016-12-18 10:50:32 -05:00
|
|
|
expected := mount.Mount{
|
|
|
|
Type: mount.TypeVolume,
|
|
|
|
Target: "/foo/bar",
|
|
|
|
}
|
2017-01-27 16:56:45 -05:00
|
|
|
mount, err := convertVolumeToMount(config, volumes{}, NewNamespace("foo"))
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.DeepEqual(expected, mount))
|
2016-12-18 10:50:32 -05:00
|
|
|
}
|
|
|
|
|
2018-01-16 11:52:26 -05:00
|
|
|
func TestConvertVolumeToMountAnonymousBind(t *testing.T) {
|
|
|
|
config := composetypes.ServiceVolumeConfig{
|
|
|
|
Type: "bind",
|
|
|
|
Target: "/foo/bar",
|
|
|
|
Bind: &composetypes.ServiceVolumeBind{
|
|
|
|
Propagation: "slave",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_, err := convertVolumeToMount(config, volumes{}, NewNamespace("foo"))
|
2018-03-06 15:54:24 -05:00
|
|
|
assert.Error(t, err, "invalid bind source, source cannot be empty")
|
2018-01-16 11:52:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConvertVolumeToMountUnapprovedType(t *testing.T) {
|
|
|
|
config := composetypes.ServiceVolumeConfig{
|
|
|
|
Type: "foo",
|
|
|
|
Target: "/foo/bar",
|
|
|
|
}
|
|
|
|
_, err := convertVolumeToMount(config, volumes{}, NewNamespace("foo"))
|
2018-03-06 15:54:24 -05:00
|
|
|
assert.Error(t, err, "volume type must be volume, bind, or tmpfs")
|
2018-01-16 11:52:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConvertVolumeToMountConflictingOptionsBindInVolume(t *testing.T) {
|
2016-12-18 19:50:08 -05:00
|
|
|
namespace := NewNamespace("foo")
|
2017-01-27 16:56:45 -05:00
|
|
|
|
|
|
|
config := composetypes.ServiceVolumeConfig{
|
|
|
|
Type: "volume",
|
|
|
|
Source: "foo",
|
|
|
|
Target: "/target",
|
|
|
|
Bind: &composetypes.ServiceVolumeBind{
|
|
|
|
Propagation: "slave",
|
|
|
|
},
|
2016-12-18 19:50:08 -05:00
|
|
|
}
|
2017-01-27 16:56:45 -05:00
|
|
|
_, err := convertVolumeToMount(config, volumes{}, namespace)
|
2018-03-06 15:54:24 -05:00
|
|
|
assert.Error(t, err, "bind options are incompatible with type volume")
|
2017-01-27 16:56:45 -05:00
|
|
|
}
|
|
|
|
|
2018-01-16 11:52:26 -05:00
|
|
|
func TestConvertVolumeToMountConflictingOptionsTmpfsInVolume(t *testing.T) {
|
|
|
|
namespace := NewNamespace("foo")
|
|
|
|
|
|
|
|
config := composetypes.ServiceVolumeConfig{
|
|
|
|
Type: "volume",
|
|
|
|
Source: "foo",
|
|
|
|
Target: "/target",
|
|
|
|
Tmpfs: &composetypes.ServiceVolumeTmpfs{
|
|
|
|
Size: 1000,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_, err := convertVolumeToMount(config, volumes{}, namespace)
|
2018-03-06 15:54:24 -05:00
|
|
|
assert.Error(t, err, "tmpfs options are incompatible with type volume")
|
2018-01-16 11:52:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConvertVolumeToMountConflictingOptionsVolumeInBind(t *testing.T) {
|
2017-01-27 16:56:45 -05:00
|
|
|
namespace := NewNamespace("foo")
|
|
|
|
|
|
|
|
config := composetypes.ServiceVolumeConfig{
|
|
|
|
Type: "bind",
|
|
|
|
Source: "/foo",
|
|
|
|
Target: "/target",
|
|
|
|
Volume: &composetypes.ServiceVolumeVolume{
|
|
|
|
NoCopy: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_, err := convertVolumeToMount(config, volumes{}, namespace)
|
2018-03-06 15:54:24 -05:00
|
|
|
assert.Error(t, err, "volume options are incompatible with type bind")
|
2016-12-18 19:50:08 -05:00
|
|
|
}
|
|
|
|
|
2018-01-16 11:52:26 -05:00
|
|
|
func TestConvertVolumeToMountConflictingOptionsTmpfsInBind(t *testing.T) {
|
|
|
|
namespace := NewNamespace("foo")
|
|
|
|
|
|
|
|
config := composetypes.ServiceVolumeConfig{
|
|
|
|
Type: "bind",
|
|
|
|
Source: "/foo",
|
|
|
|
Target: "/target",
|
|
|
|
Tmpfs: &composetypes.ServiceVolumeTmpfs{
|
|
|
|
Size: 1000,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_, err := convertVolumeToMount(config, volumes{}, namespace)
|
2018-03-06 15:54:24 -05:00
|
|
|
assert.Error(t, err, "tmpfs options are incompatible with type bind")
|
2018-01-16 11:52:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConvertVolumeToMountConflictingOptionsBindInTmpfs(t *testing.T) {
|
|
|
|
namespace := NewNamespace("foo")
|
|
|
|
|
|
|
|
config := composetypes.ServiceVolumeConfig{
|
|
|
|
Type: "tmpfs",
|
|
|
|
Target: "/target",
|
|
|
|
Bind: &composetypes.ServiceVolumeBind{
|
|
|
|
Propagation: "slave",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_, err := convertVolumeToMount(config, volumes{}, namespace)
|
2018-03-06 15:54:24 -05:00
|
|
|
assert.Error(t, err, "bind options are incompatible with type tmpfs")
|
2018-01-16 11:52:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConvertVolumeToMountConflictingOptionsVolumeInTmpfs(t *testing.T) {
|
|
|
|
namespace := NewNamespace("foo")
|
|
|
|
|
|
|
|
config := composetypes.ServiceVolumeConfig{
|
|
|
|
Type: "tmpfs",
|
|
|
|
Target: "/target",
|
|
|
|
Volume: &composetypes.ServiceVolumeVolume{
|
|
|
|
NoCopy: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_, err := convertVolumeToMount(config, volumes{}, namespace)
|
2018-03-06 15:54:24 -05:00
|
|
|
assert.Error(t, err, "volume options are incompatible with type tmpfs")
|
2018-01-16 11:52:26 -05:00
|
|
|
}
|
|
|
|
|
2016-12-05 16:14:08 -05:00
|
|
|
func TestConvertVolumeToMountNamedVolume(t *testing.T) {
|
|
|
|
stackVolumes := volumes{
|
|
|
|
"normal": composetypes.VolumeConfig{
|
|
|
|
Driver: "glusterfs",
|
|
|
|
DriverOpts: map[string]string{
|
|
|
|
"opt": "value",
|
|
|
|
},
|
|
|
|
Labels: map[string]string{
|
|
|
|
"something": "labeled",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
namespace := NewNamespace("foo")
|
|
|
|
expected := mount.Mount{
|
|
|
|
Type: mount.TypeVolume,
|
|
|
|
Source: "foo_normal",
|
|
|
|
Target: "/foo",
|
|
|
|
ReadOnly: true,
|
|
|
|
VolumeOptions: &mount.VolumeOptions{
|
|
|
|
Labels: map[string]string{
|
|
|
|
LabelNamespace: "foo",
|
|
|
|
"something": "labeled",
|
|
|
|
},
|
|
|
|
DriverConfig: &mount.Driver{
|
|
|
|
Name: "glusterfs",
|
|
|
|
Options: map[string]string{
|
|
|
|
"opt": "value",
|
|
|
|
},
|
|
|
|
},
|
2017-01-27 16:56:45 -05:00
|
|
|
NoCopy: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
config := composetypes.ServiceVolumeConfig{
|
|
|
|
Type: "volume",
|
|
|
|
Source: "normal",
|
|
|
|
Target: "/foo",
|
|
|
|
ReadOnly: true,
|
|
|
|
Volume: &composetypes.ServiceVolumeVolume{
|
|
|
|
NoCopy: true,
|
2016-12-05 16:14:08 -05:00
|
|
|
},
|
|
|
|
}
|
2017-01-27 16:56:45 -05:00
|
|
|
mount, err := convertVolumeToMount(config, stackVolumes, namespace)
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.DeepEqual(expected, mount))
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
|
2017-06-29 19:25:50 -04:00
|
|
|
func TestConvertVolumeToMountNamedVolumeWithNameCustomizd(t *testing.T) {
|
|
|
|
stackVolumes := volumes{
|
|
|
|
"normal": composetypes.VolumeConfig{
|
|
|
|
Name: "user_specified_name",
|
|
|
|
Driver: "vsphere",
|
|
|
|
DriverOpts: map[string]string{
|
|
|
|
"opt": "value",
|
|
|
|
},
|
|
|
|
Labels: map[string]string{
|
|
|
|
"something": "labeled",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
namespace := NewNamespace("foo")
|
|
|
|
expected := mount.Mount{
|
|
|
|
Type: mount.TypeVolume,
|
|
|
|
Source: "user_specified_name",
|
|
|
|
Target: "/foo",
|
|
|
|
ReadOnly: true,
|
|
|
|
VolumeOptions: &mount.VolumeOptions{
|
|
|
|
Labels: map[string]string{
|
|
|
|
LabelNamespace: "foo",
|
|
|
|
"something": "labeled",
|
|
|
|
},
|
|
|
|
DriverConfig: &mount.Driver{
|
|
|
|
Name: "vsphere",
|
|
|
|
Options: map[string]string{
|
|
|
|
"opt": "value",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
NoCopy: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
config := composetypes.ServiceVolumeConfig{
|
|
|
|
Type: "volume",
|
|
|
|
Source: "normal",
|
|
|
|
Target: "/foo",
|
|
|
|
ReadOnly: true,
|
|
|
|
Volume: &composetypes.ServiceVolumeVolume{
|
|
|
|
NoCopy: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
mount, err := convertVolumeToMount(config, stackVolumes, namespace)
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.DeepEqual(expected, mount))
|
2017-06-29 19:25:50 -04:00
|
|
|
}
|
|
|
|
|
2016-12-05 16:14:08 -05:00
|
|
|
func TestConvertVolumeToMountNamedVolumeExternal(t *testing.T) {
|
|
|
|
stackVolumes := volumes{
|
|
|
|
"outside": composetypes.VolumeConfig{
|
2017-11-06 17:03:43 -05:00
|
|
|
Name: "special",
|
|
|
|
External: composetypes.External{External: true},
|
2016-12-05 16:14:08 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
namespace := NewNamespace("foo")
|
|
|
|
expected := mount.Mount{
|
2017-11-06 17:03:43 -05:00
|
|
|
Type: mount.TypeVolume,
|
|
|
|
Source: "special",
|
|
|
|
Target: "/foo",
|
|
|
|
VolumeOptions: &mount.VolumeOptions{NoCopy: false},
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
2017-01-27 16:56:45 -05:00
|
|
|
config := composetypes.ServiceVolumeConfig{
|
|
|
|
Type: "volume",
|
|
|
|
Source: "outside",
|
|
|
|
Target: "/foo",
|
|
|
|
}
|
|
|
|
mount, err := convertVolumeToMount(config, stackVolumes, namespace)
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.DeepEqual(expected, mount))
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
|
2017-02-15 04:32:37 -05:00
|
|
|
func TestConvertVolumeToMountNamedVolumeExternalNoCopy(t *testing.T) {
|
|
|
|
stackVolumes := volumes{
|
|
|
|
"outside": composetypes.VolumeConfig{
|
2017-11-06 17:03:43 -05:00
|
|
|
Name: "special",
|
|
|
|
External: composetypes.External{External: true},
|
2017-02-15 04:32:37 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
namespace := NewNamespace("foo")
|
|
|
|
expected := mount.Mount{
|
|
|
|
Type: mount.TypeVolume,
|
|
|
|
Source: "special",
|
|
|
|
Target: "/foo",
|
|
|
|
VolumeOptions: &mount.VolumeOptions{
|
|
|
|
NoCopy: true,
|
|
|
|
},
|
|
|
|
}
|
2017-01-27 16:56:45 -05:00
|
|
|
config := composetypes.ServiceVolumeConfig{
|
|
|
|
Type: "volume",
|
|
|
|
Source: "outside",
|
|
|
|
Target: "/foo",
|
|
|
|
Volume: &composetypes.ServiceVolumeVolume{
|
|
|
|
NoCopy: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
mount, err := convertVolumeToMount(config, stackVolumes, namespace)
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.DeepEqual(expected, mount))
|
2017-02-15 04:32:37 -05:00
|
|
|
}
|
|
|
|
|
2016-12-05 16:14:08 -05:00
|
|
|
func TestConvertVolumeToMountBind(t *testing.T) {
|
|
|
|
stackVolumes := volumes{}
|
|
|
|
namespace := NewNamespace("foo")
|
|
|
|
expected := mount.Mount{
|
|
|
|
Type: mount.TypeBind,
|
|
|
|
Source: "/bar",
|
|
|
|
Target: "/foo",
|
|
|
|
ReadOnly: true,
|
|
|
|
BindOptions: &mount.BindOptions{Propagation: mount.PropagationShared},
|
|
|
|
}
|
2017-01-27 16:56:45 -05:00
|
|
|
config := composetypes.ServiceVolumeConfig{
|
|
|
|
Type: "bind",
|
|
|
|
Source: "/bar",
|
|
|
|
Target: "/foo",
|
|
|
|
ReadOnly: true,
|
|
|
|
Bind: &composetypes.ServiceVolumeBind{Propagation: "shared"},
|
|
|
|
}
|
|
|
|
mount, err := convertVolumeToMount(config, stackVolumes, namespace)
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.DeepEqual(expected, mount))
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConvertVolumeToMountVolumeDoesNotExist(t *testing.T) {
|
|
|
|
namespace := NewNamespace("foo")
|
2017-01-27 16:56:45 -05:00
|
|
|
config := composetypes.ServiceVolumeConfig{
|
|
|
|
Type: "volume",
|
|
|
|
Source: "unknown",
|
|
|
|
Target: "/foo",
|
|
|
|
ReadOnly: true,
|
|
|
|
}
|
|
|
|
_, err := convertVolumeToMount(config, volumes{}, namespace)
|
2018-03-06 15:54:24 -05:00
|
|
|
assert.Error(t, err, "undefined volume \"unknown\"")
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
2018-01-16 11:52:26 -05:00
|
|
|
|
|
|
|
func TestConvertTmpfsToMountVolume(t *testing.T) {
|
|
|
|
config := composetypes.ServiceVolumeConfig{
|
|
|
|
Type: "tmpfs",
|
|
|
|
Target: "/foo/bar",
|
|
|
|
Tmpfs: &composetypes.ServiceVolumeTmpfs{
|
|
|
|
Size: 1000,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
expected := mount.Mount{
|
|
|
|
Type: mount.TypeTmpfs,
|
|
|
|
Target: "/foo/bar",
|
|
|
|
TmpfsOptions: &mount.TmpfsOptions{SizeBytes: 1000},
|
|
|
|
}
|
|
|
|
mount, err := convertVolumeToMount(config, volumes{}, NewNamespace("foo"))
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.DeepEqual(expected, mount))
|
2018-01-16 11:52:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConvertTmpfsToMountVolumeWithSource(t *testing.T) {
|
|
|
|
config := composetypes.ServiceVolumeConfig{
|
|
|
|
Type: "tmpfs",
|
|
|
|
Source: "/bar",
|
|
|
|
Target: "/foo/bar",
|
|
|
|
Tmpfs: &composetypes.ServiceVolumeTmpfs{
|
|
|
|
Size: 1000,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := convertVolumeToMount(config, volumes{}, NewNamespace("foo"))
|
2018-03-06 15:54:24 -05:00
|
|
|
assert.Error(t, err, "invalid tmpfs source, source must be empty")
|
2018-01-16 11:52:26 -05:00
|
|
|
}
|