diff --git a/runconfig/parse.go b/runconfig/parse.go index 06e380d4fa..262a04eb4b 100644 --- a/runconfig/parse.go +++ b/runconfig/parse.go @@ -284,16 +284,17 @@ func parseKeyValueOpts(opts opts.ListOpts) ([]utils.KeyValuePair, error) { func parseNetMode(netMode string) (string, string, error) { parts := strings.Split(netMode, ":") - if len(parts) < 1 { - return "", "", fmt.Errorf("'netmode' cannot be empty", netMode) - } - mode := parts[0] - var container string - if mode == "container" { - if len(parts) < 2 { + switch mode := parts[0]; mode { + case "bridge", "disable": + return mode, "", nil + case "container": + var container string + if len(parts) < 2 || parts[1] == "" { return "", "", fmt.Errorf("'container:' netmode requires a container id or name", netMode) } container = parts[1] + return mode, container, nil + default: + return "", "", fmt.Errorf("invalid netmode: %q", netMode) } - return mode, container, nil } diff --git a/runconfig/parse_test.go b/runconfig/parse_test.go index fd28c4593e..9ac925f2ac 100644 --- a/runconfig/parse_test.go +++ b/runconfig/parse_test.go @@ -1,8 +1,9 @@ package runconfig import ( - "github.com/dotcloud/docker/utils" "testing" + + "github.com/dotcloud/docker/utils" ) func TestParseLxcConfOpt(t *testing.T) { @@ -21,3 +22,33 @@ func TestParseLxcConfOpt(t *testing.T) { } } } + +func TestParseNetMode(t *testing.T) { + testFlags := []struct { + flag string + mode string + container string + err bool + }{ + {"", "", "", true}, + {"bridge", "bridge", "", false}, + {"disable", "disable", "", false}, + {"container:foo", "container", "foo", false}, + {"container:", "", "", true}, + {"container", "", "", true}, + {"unknown", "", "", true}, + } + + for _, to := range testFlags { + mode, container, err := parseNetMode(to.flag) + if mode != to.mode { + t.Fatalf("-net %s: expected net mode: %q, got: %q", to.flag, to.mode, mode) + } + if container != to.container { + t.Fatalf("-net %s: expected net container: %q, got: %q", to.flag, to.container, container) + } + if (err != nil) != to.err { + t.Fatal("-net %s: expected an error got none", to.flag) + } + } +}