1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge pull request #10856 from coolljt0725/fix_expose

Fix docker run --expose with an invalid port does not error out
This commit is contained in:
Jessie Frazelle 2015-02-19 16:01:15 -08:00
commit 1402937347
2 changed files with 23 additions and 15 deletions

View file

@ -2719,6 +2719,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")

View file

@ -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 <portnum>/[<proto>] or <startport-endport>/[<proto>]
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{}{}
}