2017-01-24 16:53:36 -05:00
|
|
|
package loader
|
|
|
|
|
|
|
|
import (
|
2017-05-09 19:21:17 -04:00
|
|
|
"fmt"
|
2017-01-24 16:53:36 -05:00
|
|
|
"testing"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli/compose/types"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2017-01-24 16:53:36 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestParseVolumeAnonymousVolume(t *testing.T) {
|
|
|
|
for _, path := range []string{"/path", "/path/foo"} {
|
2017-05-09 19:21:17 -04:00
|
|
|
volume, err := ParseVolume(path)
|
2017-01-24 16:53:36 -05:00
|
|
|
expected := types.ServiceVolumeConfig{Type: "volume", Target: path}
|
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, volume))
|
2017-01-24 16:53:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseVolumeAnonymousVolumeWindows(t *testing.T) {
|
|
|
|
for _, path := range []string{"C:\\path", "Z:\\path\\foo"} {
|
2017-05-09 19:21:17 -04:00
|
|
|
volume, err := ParseVolume(path)
|
2017-01-24 16:53:36 -05:00
|
|
|
expected := types.ServiceVolumeConfig{Type: "volume", Target: path}
|
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, volume))
|
2017-01-24 16:53:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseVolumeTooManyColons(t *testing.T) {
|
2017-05-09 19:21:17 -04:00
|
|
|
_, err := ParseVolume("/foo:/foo:ro:foo")
|
2018-03-06 15:54:24 -05:00
|
|
|
assert.Error(t, err, "invalid spec: /foo:/foo:ro:foo: too many colons")
|
2017-01-24 16:53:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseVolumeShortVolumes(t *testing.T) {
|
|
|
|
for _, path := range []string{".", "/a"} {
|
2017-05-09 19:21:17 -04:00
|
|
|
volume, err := ParseVolume(path)
|
2017-01-24 16:53:36 -05:00
|
|
|
expected := types.ServiceVolumeConfig{Type: "volume", Target: path}
|
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, volume))
|
2017-01-24 16:53:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseVolumeMissingSource(t *testing.T) {
|
|
|
|
for _, spec := range []string{":foo", "/foo::ro"} {
|
2017-05-09 19:21:17 -04:00
|
|
|
_, err := ParseVolume(spec)
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, err, "empty section between colons")
|
2017-01-24 16:53:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseVolumeBindMount(t *testing.T) {
|
|
|
|
for _, path := range []string{"./foo", "~/thing", "../other", "/foo", "/home/user"} {
|
2017-05-09 19:21:17 -04:00
|
|
|
volume, err := ParseVolume(path + ":/target")
|
2017-01-24 16:53:36 -05:00
|
|
|
expected := types.ServiceVolumeConfig{
|
|
|
|
Type: "bind",
|
|
|
|
Source: path,
|
|
|
|
Target: "/target",
|
|
|
|
}
|
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, volume))
|
2017-01-24 16:53:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseVolumeRelativeBindMountWindows(t *testing.T) {
|
|
|
|
for _, path := range []string{
|
|
|
|
"./foo",
|
|
|
|
"~/thing",
|
|
|
|
"../other",
|
|
|
|
"D:\\path", "/home/user",
|
|
|
|
} {
|
2017-05-09 19:21:17 -04:00
|
|
|
volume, err := ParseVolume(path + ":d:\\target")
|
2017-01-24 16:53:36 -05:00
|
|
|
expected := types.ServiceVolumeConfig{
|
|
|
|
Type: "bind",
|
|
|
|
Source: path,
|
|
|
|
Target: "d:\\target",
|
|
|
|
}
|
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, volume))
|
2017-01-24 16:53:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseVolumeWithBindOptions(t *testing.T) {
|
2017-05-09 19:21:17 -04:00
|
|
|
volume, err := ParseVolume("/source:/target:slave")
|
2017-01-24 16:53:36 -05:00
|
|
|
expected := types.ServiceVolumeConfig{
|
|
|
|
Type: "bind",
|
|
|
|
Source: "/source",
|
|
|
|
Target: "/target",
|
|
|
|
Bind: &types.ServiceVolumeBind{Propagation: "slave"},
|
|
|
|
}
|
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, volume))
|
2017-01-24 16:53:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseVolumeWithBindOptionsWindows(t *testing.T) {
|
2017-05-09 19:21:17 -04:00
|
|
|
volume, err := ParseVolume("C:\\source\\foo:D:\\target:ro,rprivate")
|
2017-01-24 16:53:36 -05:00
|
|
|
expected := types.ServiceVolumeConfig{
|
|
|
|
Type: "bind",
|
|
|
|
Source: "C:\\source\\foo",
|
|
|
|
Target: "D:\\target",
|
|
|
|
ReadOnly: true,
|
|
|
|
Bind: &types.ServiceVolumeBind{Propagation: "rprivate"},
|
|
|
|
}
|
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, volume))
|
2017-01-24 16:53:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseVolumeWithInvalidVolumeOptions(t *testing.T) {
|
2017-05-09 19:21:17 -04:00
|
|
|
_, err := ParseVolume("name:/target:bogus")
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2017-01-24 16:53:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseVolumeWithVolumeOptions(t *testing.T) {
|
2017-05-09 19:21:17 -04:00
|
|
|
volume, err := ParseVolume("name:/target:nocopy")
|
2017-01-24 16:53:36 -05:00
|
|
|
expected := types.ServiceVolumeConfig{
|
|
|
|
Type: "volume",
|
|
|
|
Source: "name",
|
|
|
|
Target: "/target",
|
|
|
|
Volume: &types.ServiceVolumeVolume{NoCopy: true},
|
|
|
|
}
|
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, volume))
|
2017-01-24 16:53:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseVolumeWithReadOnly(t *testing.T) {
|
|
|
|
for _, path := range []string{"./foo", "/home/user"} {
|
2017-05-09 19:21:17 -04:00
|
|
|
volume, err := ParseVolume(path + ":/target:ro")
|
2017-01-24 16:53:36 -05:00
|
|
|
expected := types.ServiceVolumeConfig{
|
|
|
|
Type: "bind",
|
|
|
|
Source: path,
|
|
|
|
Target: "/target",
|
|
|
|
ReadOnly: true,
|
|
|
|
}
|
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, volume))
|
2017-01-24 16:53:36 -05:00
|
|
|
}
|
|
|
|
}
|
2017-04-06 10:32:35 -04:00
|
|
|
|
|
|
|
func TestParseVolumeWithRW(t *testing.T) {
|
|
|
|
for _, path := range []string{"./foo", "/home/user"} {
|
2017-05-09 19:21:17 -04:00
|
|
|
volume, err := ParseVolume(path + ":/target:rw")
|
2017-04-06 10:32:35 -04:00
|
|
|
expected := types.ServiceVolumeConfig{
|
|
|
|
Type: "bind",
|
|
|
|
Source: path,
|
|
|
|
Target: "/target",
|
|
|
|
ReadOnly: false,
|
|
|
|
}
|
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, volume))
|
2017-04-06 10:32:35 -04:00
|
|
|
}
|
|
|
|
}
|
2017-06-03 22:59:09 -04:00
|
|
|
|
2017-09-26 11:24:23 -04:00
|
|
|
func TestParseVolumeWindowsNamedPipe(t *testing.T) {
|
|
|
|
volume, err := ParseVolume(`\\.\pipe\docker_engine:\\.\pipe\inside`)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2017-09-26 11:24:23 -04:00
|
|
|
expected := types.ServiceVolumeConfig{
|
|
|
|
Type: "bind",
|
|
|
|
Source: `\\.\pipe\docker_engine`,
|
|
|
|
Target: `\\.\pipe\inside`,
|
|
|
|
}
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.DeepEqual(expected, volume))
|
2017-09-26 11:24:23 -04:00
|
|
|
}
|
|
|
|
|
2017-06-03 22:59:09 -04:00
|
|
|
func TestIsFilePath(t *testing.T) {
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, !isFilePath("a界"))
|
2017-06-03 22:59:09 -04:00
|
|
|
}
|
2017-05-09 19:21:17 -04:00
|
|
|
|
|
|
|
// Preserve the test cases for VolumeSplitN
|
|
|
|
func TestParseVolumeSplitCases(t *testing.T) {
|
|
|
|
for casenumber, x := range []struct {
|
|
|
|
input string
|
|
|
|
n int
|
|
|
|
expected []string
|
|
|
|
}{
|
|
|
|
{`C:\foo:d:`, -1, []string{`C:\foo`, `d:`}},
|
|
|
|
{`:C:\foo:d:`, -1, nil},
|
|
|
|
{`/foo:/bar:ro`, 3, []string{`/foo`, `/bar`, `ro`}},
|
|
|
|
{`/foo:/bar:ro`, 2, []string{`/foo`, `/bar:ro`}},
|
|
|
|
{`C:\foo\:/foo`, -1, []string{`C:\foo\`, `/foo`}},
|
|
|
|
{`d:\`, -1, []string{`d:\`}},
|
|
|
|
{`d:`, -1, []string{`d:`}},
|
|
|
|
{`d:\path`, -1, []string{`d:\path`}},
|
|
|
|
{`d:\path with space`, -1, []string{`d:\path with space`}},
|
|
|
|
{`d:\pathandmode:rw`, -1, []string{`d:\pathandmode`, `rw`}},
|
|
|
|
|
|
|
|
{`c:\:d:\`, -1, []string{`c:\`, `d:\`}},
|
|
|
|
{`c:\windows\:d:`, -1, []string{`c:\windows\`, `d:`}},
|
|
|
|
{`c:\windows:d:\s p a c e`, -1, []string{`c:\windows`, `d:\s p a c e`}},
|
|
|
|
{`c:\windows:d:\s p a c e:RW`, -1, []string{`c:\windows`, `d:\s p a c e`, `RW`}},
|
|
|
|
{`c:\program files:d:\s p a c e i n h o s t d i r`, -1, []string{`c:\program files`, `d:\s p a c e i n h o s t d i r`}},
|
|
|
|
{`0123456789name:d:`, -1, []string{`0123456789name`, `d:`}},
|
|
|
|
{`MiXeDcAsEnAmE:d:`, -1, []string{`MiXeDcAsEnAmE`, `d:`}},
|
|
|
|
{`name:D:`, -1, []string{`name`, `D:`}},
|
|
|
|
{`name:D::rW`, -1, []string{`name`, `D:`, `rW`}},
|
|
|
|
{`name:D::RW`, -1, []string{`name`, `D:`, `RW`}},
|
|
|
|
|
|
|
|
{`c:/:d:/forward/slashes/are/good/too`, -1, []string{`c:/`, `d:/forward/slashes/are/good/too`}},
|
|
|
|
{`c:\Windows`, -1, []string{`c:\Windows`}},
|
|
|
|
{`c:\Program Files (x86)`, -1, []string{`c:\Program Files (x86)`}},
|
|
|
|
{``, -1, nil},
|
|
|
|
{`.`, -1, []string{`.`}},
|
|
|
|
{`..\`, -1, []string{`..\`}},
|
|
|
|
{`c:\:..\`, -1, []string{`c:\`, `..\`}},
|
|
|
|
{`c:\:d:\:xyzzy`, -1, []string{`c:\`, `d:\`, `xyzzy`}},
|
|
|
|
// Cover directories with one-character name
|
|
|
|
{`/tmp/x/y:/foo/x/y`, -1, []string{`/tmp/x/y`, `/foo/x/y`}},
|
|
|
|
} {
|
|
|
|
parsed, _ := ParseVolume(x.input)
|
|
|
|
|
|
|
|
expected := len(x.expected) > 1
|
|
|
|
msg := fmt.Sprintf("Case %d: %s", casenumber, x.input)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal(expected, parsed.Source != ""), msg)
|
2017-05-09 19:21:17 -04:00
|
|
|
}
|
|
|
|
}
|
2017-05-09 18:35:25 -04:00
|
|
|
|
|
|
|
func TestParseVolumeInvalidEmptySpec(t *testing.T) {
|
|
|
|
_, err := ParseVolume("")
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, err, "invalid empty volume spec")
|
2017-05-09 18:35:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseVolumeInvalidSections(t *testing.T) {
|
|
|
|
_, err := ParseVolume("/foo::rw")
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, err, "invalid spec")
|
2017-05-09 18:35:25 -04:00
|
|
|
}
|