2016-12-05 16:14:08 -05:00
|
|
|
package convert
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types/mount"
|
2016-12-20 16:26:49 -05:00
|
|
|
composetypes "github.com/docker/docker/cli/compose/types"
|
2016-12-05 16:14:08 -05:00
|
|
|
"github.com/docker/docker/pkg/testutil/assert"
|
|
|
|
)
|
|
|
|
|
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"))
|
2016-12-18 10:50:32 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.DeepEqual(t, mount, expected)
|
|
|
|
}
|
|
|
|
|
2017-01-27 16:56:45 -05:00
|
|
|
func TestConvertVolumeToMountConflictingOptionsBind(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)
|
|
|
|
assert.Error(t, err, "bind options are incompatible")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConvertVolumeToMountConflictingOptionsVolume(t *testing.T) {
|
|
|
|
namespace := NewNamespace("foo")
|
|
|
|
|
|
|
|
config := composetypes.ServiceVolumeConfig{
|
|
|
|
Type: "bind",
|
|
|
|
Source: "/foo",
|
|
|
|
Target: "/target",
|
|
|
|
Volume: &composetypes.ServiceVolumeVolume{
|
|
|
|
NoCopy: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_, err := convertVolumeToMount(config, volumes{}, namespace)
|
|
|
|
assert.Error(t, err, "volume options are incompatible")
|
2016-12-18 19:50:08 -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)
|
2016-12-05 16:14:08 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.DeepEqual(t, mount, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConvertVolumeToMountNamedVolumeExternal(t *testing.T) {
|
|
|
|
stackVolumes := volumes{
|
|
|
|
"outside": composetypes.VolumeConfig{
|
|
|
|
External: composetypes.External{
|
|
|
|
External: true,
|
|
|
|
Name: "special",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
namespace := NewNamespace("foo")
|
|
|
|
expected := mount.Mount{
|
|
|
|
Type: mount.TypeVolume,
|
|
|
|
Source: "special",
|
|
|
|
Target: "/foo",
|
2017-02-15 04:32:37 -05:00
|
|
|
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)
|
2016-12-05 16:14:08 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.DeepEqual(t, mount, expected)
|
|
|
|
}
|
|
|
|
|
2017-02-15 04:32:37 -05:00
|
|
|
func TestConvertVolumeToMountNamedVolumeExternalNoCopy(t *testing.T) {
|
|
|
|
stackVolumes := volumes{
|
|
|
|
"outside": composetypes.VolumeConfig{
|
|
|
|
External: composetypes.External{
|
|
|
|
External: true,
|
|
|
|
Name: "special",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
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)
|
2017-02-15 04:32:37 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.DeepEqual(t, mount, expected)
|
|
|
|
}
|
|
|
|
|
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)
|
2016-12-05 16:14:08 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.DeepEqual(t, mount, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2016-12-05 16:14:08 -05:00
|
|
|
assert.Error(t, err, "undefined volume: unknown")
|
|
|
|
}
|