From fe7afb700f833a023a4bf78d89fd2c8adf92f8b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Gronowski?= Date: Tue, 11 Jul 2023 17:48:35 +0200 Subject: [PATCH] cli/container: Don't ignore error when parsing volume spec MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Paweł Gronowski --- cli/command/container/opts.go | 5 ++++- cli/compose/loader/volume_test.go | 5 +++++ e2e/container/create_test.go | 16 ++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/cli/command/container/opts.go b/cli/command/container/opts.go index 19c5d3bc07..53dc4daae0 100644 --- a/cli/command/container/opts.go +++ b/cli/command/container/opts.go @@ -358,7 +358,10 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con volumes := copts.volumes.GetMap() // add any bind targets to the list of container volumes for bind := range copts.volumes.GetMap() { - parsed, _ := loader.ParseVolume(bind) + parsed, err := loader.ParseVolume(bind) + if err != nil { + return nil, err + } if parsed.Source != "" { toBind := bind diff --git a/cli/compose/loader/volume_test.go b/cli/compose/loader/volume_test.go index b42be1fc52..54dc200fe8 100644 --- a/cli/compose/loader/volume_test.go +++ b/cli/compose/loader/volume_test.go @@ -223,3 +223,8 @@ func TestParseVolumeInvalidSections(t *testing.T) { _, err := ParseVolume("/foo::rw") assert.ErrorContains(t, err, "invalid spec") } + +func TestParseVolumeWithEmptySource(t *testing.T) { + _, err := ParseVolume(":/vol") + assert.ErrorContains(t, err, "empty section between colons") +} diff --git a/e2e/container/create_test.go b/e2e/container/create_test.go index 3c519df12e..daf8e092f6 100644 --- a/e2e/container/create_test.go +++ b/e2e/container/create_test.go @@ -101,3 +101,19 @@ func TestTrustedCreateFromBadTrustServer(t *testing.T) { Err: "could not rotate trust to a new trusted root", }) } + +func TestCreateWithEmptySourceVolume(t *testing.T) { + icmd.RunCmd(icmd.Command("docker", "create", "-v", ":/volume", fixtures.AlpineImage)). + Assert(t, icmd.Expected{ + ExitCode: 125, + Err: "empty section between colons", + }) +} + +func TestCreateWithEmptyVolumeSpec(t *testing.T) { + icmd.RunCmd(icmd.Command("docker", "create", "-v", "", fixtures.AlpineImage)). + Assert(t, icmd.Expected{ + ExitCode: 125, + Err: "invalid empty volume spec", + }) +}