github.com/fabiokung/docker@v0.11.2-0.20170222101415-4534dcd49497/cli/compose/convert/volume_test.go (about) 1 package convert 2 3 import ( 4 "testing" 5 6 "github.com/docker/docker/api/types/mount" 7 composetypes "github.com/docker/docker/cli/compose/types" 8 "github.com/docker/docker/pkg/testutil/assert" 9 ) 10 11 func TestIsReadOnly(t *testing.T) { 12 assert.Equal(t, isReadOnly([]string{"foo", "bar", "ro"}), true) 13 assert.Equal(t, isReadOnly([]string{"ro"}), true) 14 assert.Equal(t, isReadOnly([]string{}), false) 15 assert.Equal(t, isReadOnly([]string{"foo", "rw"}), false) 16 assert.Equal(t, isReadOnly([]string{"foo"}), false) 17 } 18 19 func TestIsNoCopy(t *testing.T) { 20 assert.Equal(t, isNoCopy([]string{"foo", "bar", "nocopy"}), true) 21 assert.Equal(t, isNoCopy([]string{"nocopy"}), true) 22 assert.Equal(t, isNoCopy([]string{}), false) 23 assert.Equal(t, isNoCopy([]string{"foo", "rw"}), false) 24 } 25 26 func TestGetBindOptions(t *testing.T) { 27 opts := getBindOptions([]string{"slave"}) 28 expected := mount.BindOptions{Propagation: mount.PropagationSlave} 29 assert.Equal(t, *opts, expected) 30 } 31 32 func TestGetBindOptionsNone(t *testing.T) { 33 opts := getBindOptions([]string{"ro"}) 34 assert.Equal(t, opts, (*mount.BindOptions)(nil)) 35 } 36 37 func TestConvertVolumeToMountAnonymousVolume(t *testing.T) { 38 stackVolumes := volumes{} 39 namespace := NewNamespace("foo") 40 expected := mount.Mount{ 41 Type: mount.TypeVolume, 42 Target: "/foo/bar", 43 } 44 mount, err := convertVolumeToMount("/foo/bar", stackVolumes, namespace) 45 assert.NilError(t, err) 46 assert.DeepEqual(t, mount, expected) 47 } 48 49 func TestConvertVolumeToMountInvalidFormat(t *testing.T) { 50 namespace := NewNamespace("foo") 51 invalids := []string{"::", "::cc", ":bb:", "aa::", "aa::cc", "aa:bb:", " : : ", " : :cc", " :bb: ", "aa: : ", "aa: :cc", "aa:bb: "} 52 for _, vol := range invalids { 53 _, err := convertVolumeToMount(vol, volumes{}, namespace) 54 assert.Error(t, err, "invalid volume: "+vol) 55 } 56 } 57 58 func TestConvertVolumeToMountNamedVolume(t *testing.T) { 59 stackVolumes := volumes{ 60 "normal": composetypes.VolumeConfig{ 61 Driver: "glusterfs", 62 DriverOpts: map[string]string{ 63 "opt": "value", 64 }, 65 Labels: map[string]string{ 66 "something": "labeled", 67 }, 68 }, 69 } 70 namespace := NewNamespace("foo") 71 expected := mount.Mount{ 72 Type: mount.TypeVolume, 73 Source: "foo_normal", 74 Target: "/foo", 75 ReadOnly: true, 76 VolumeOptions: &mount.VolumeOptions{ 77 Labels: map[string]string{ 78 LabelNamespace: "foo", 79 "something": "labeled", 80 }, 81 DriverConfig: &mount.Driver{ 82 Name: "glusterfs", 83 Options: map[string]string{ 84 "opt": "value", 85 }, 86 }, 87 }, 88 } 89 mount, err := convertVolumeToMount("normal:/foo:ro", stackVolumes, namespace) 90 assert.NilError(t, err) 91 assert.DeepEqual(t, mount, expected) 92 } 93 94 func TestConvertVolumeToMountNamedVolumeExternal(t *testing.T) { 95 stackVolumes := volumes{ 96 "outside": composetypes.VolumeConfig{ 97 External: composetypes.External{ 98 External: true, 99 Name: "special", 100 }, 101 }, 102 } 103 namespace := NewNamespace("foo") 104 expected := mount.Mount{ 105 Type: mount.TypeVolume, 106 Source: "special", 107 Target: "/foo", 108 VolumeOptions: &mount.VolumeOptions{ 109 NoCopy: false, 110 }, 111 } 112 mount, err := convertVolumeToMount("outside:/foo", stackVolumes, namespace) 113 assert.NilError(t, err) 114 assert.DeepEqual(t, mount, expected) 115 } 116 117 func TestConvertVolumeToMountNamedVolumeExternalNoCopy(t *testing.T) { 118 stackVolumes := volumes{ 119 "outside": composetypes.VolumeConfig{ 120 External: composetypes.External{ 121 External: true, 122 Name: "special", 123 }, 124 }, 125 } 126 namespace := NewNamespace("foo") 127 expected := mount.Mount{ 128 Type: mount.TypeVolume, 129 Source: "special", 130 Target: "/foo", 131 VolumeOptions: &mount.VolumeOptions{ 132 NoCopy: true, 133 }, 134 } 135 mount, err := convertVolumeToMount("outside:/foo:nocopy", stackVolumes, namespace) 136 assert.NilError(t, err) 137 assert.DeepEqual(t, mount, expected) 138 } 139 140 func TestConvertVolumeToMountBind(t *testing.T) { 141 stackVolumes := volumes{} 142 namespace := NewNamespace("foo") 143 expected := mount.Mount{ 144 Type: mount.TypeBind, 145 Source: "/bar", 146 Target: "/foo", 147 ReadOnly: true, 148 BindOptions: &mount.BindOptions{Propagation: mount.PropagationShared}, 149 } 150 mount, err := convertVolumeToMount("/bar:/foo:ro,shared", stackVolumes, namespace) 151 assert.NilError(t, err) 152 assert.DeepEqual(t, mount, expected) 153 } 154 155 func TestConvertVolumeToMountVolumeDoesNotExist(t *testing.T) { 156 namespace := NewNamespace("foo") 157 _, err := convertVolumeToMount("unknown:/foo:ro", volumes{}, namespace) 158 assert.Error(t, err, "undefined volume: unknown") 159 }