runconfig/parse: add test for parseNetMode

Docker-DCO-1.1-Signed-off-by: Johan Euphrosine <proppy@google.com> (github: proppy)
This commit is contained in:
Johan Euphrosine 2014-05-02 01:47:12 -07:00 committed by Michael Crosby
parent a60159f3b1
commit 7118416aee
2 changed files with 41 additions and 9 deletions

View file

@ -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
}

View file

@ -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)
}
}
}