From 57abf4afe9f0416666475690c63b1c4ed03dd888 Mon Sep 17 00:00:00 2001 From: Lei Jitang Date: Tue, 17 Feb 2015 07:12:02 -0800 Subject: [PATCH 1/2] Fix docker run --expose with an invalid port does not error out Signed-off-by: Lei Jitang --- runconfig/parse.go | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/runconfig/parse.go b/runconfig/parse.go index 1455cdce3..9e64f5443 100644 --- a/runconfig/parse.go +++ b/runconfig/parse.go @@ -206,21 +206,15 @@ func Parse(cmd *flag.FlagSet, args []string) (*Config, *HostConfig, *flag.FlagSe return nil, nil, cmd, fmt.Errorf("Invalid port format for --expose: %s", e) } //support two formats for expose, original format /[] or /[] - if strings.Contains(e, "-") { - proto, port := nat.SplitProtoPort(e) - //parse the start and end port and create a sequence of ports to expose - start, end, err := parsers.ParsePortRange(port) - if err != nil { - return nil, nil, cmd, fmt.Errorf("Invalid range format for --expose: %s, error: %s", e, err) - } - for i := start; i <= end; i++ { - p := nat.NewPort(proto, strconv.FormatUint(i, 10)) - if _, exists := ports[p]; !exists { - ports[p] = struct{}{} - } - } - } else { - p := nat.NewPort(nat.SplitProtoPort(e)) + proto, port := nat.SplitProtoPort(e) + //parse the start and end port and create a sequence of ports to expose + //if expose a port, the start and end port are the same + start, end, err := parsers.ParsePortRange(port) + if err != nil { + return nil, nil, cmd, fmt.Errorf("Invalid range format for --expose: %s, error: %s", e, err) + } + for i := start; i <= end; i++ { + p := nat.NewPort(proto, strconv.FormatUint(i, 10)) if _, exists := ports[p]; !exists { ports[p] = struct{}{} } From 34b7c10e3eed8bd4d71b998d06ca10766ce4c754 Mon Sep 17 00:00:00 2001 From: Lei Jitang Date: Thu, 19 Feb 2015 15:18:13 -0800 Subject: [PATCH 2/2] Add a test for expose a invalid port Signed-off-by: Lei Jitang --- integration-cli/docker_cli_run_test.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/integration-cli/docker_cli_run_test.go b/integration-cli/docker_cli_run_test.go index 491acad77..5527d4d1b 100644 --- a/integration-cli/docker_cli_run_test.go +++ b/integration-cli/docker_cli_run_test.go @@ -2718,6 +2718,20 @@ func TestRunAllowPortRangeThroughExpose(t *testing.T) { logDone("run - allow port range through --expose flag") } +// test docker run expose a invalid port +func TestRunExposePort(t *testing.T) { + runCmd := exec.Command(dockerBinary, "run", "--expose", "80000", "busybox") + out, _, err := runCommandWithOutput(runCmd) + //expose a invalid port should with a error out + if err == nil || !strings.Contains(out, "Invalid range format for --expose") { + t.Fatalf("run --expose a invalid port should with error out") + } + + deleteAllContainers() + + logDone("run - can't expose a invalid port") +} + func TestRunUnknownCommand(t *testing.T) { defer deleteAllContainers() runCmd := exec.Command(dockerBinary, "create", "busybox", "/bin/nada")