mirror of https://github.com/docker/docs.git
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:
parent
a60159f3b1
commit
7118416aee
|
@ -284,16 +284,17 @@ func parseKeyValueOpts(opts opts.ListOpts) ([]utils.KeyValuePair, error) {
|
||||||
|
|
||||||
func parseNetMode(netMode string) (string, string, error) {
|
func parseNetMode(netMode string) (string, string, error) {
|
||||||
parts := strings.Split(netMode, ":")
|
parts := strings.Split(netMode, ":")
|
||||||
if len(parts) < 1 {
|
switch mode := parts[0]; mode {
|
||||||
return "", "", fmt.Errorf("'netmode' cannot be empty", netMode)
|
case "bridge", "disable":
|
||||||
}
|
return mode, "", nil
|
||||||
mode := parts[0]
|
case "container":
|
||||||
var container string
|
var container string
|
||||||
if mode == "container" {
|
if len(parts) < 2 || parts[1] == "" {
|
||||||
if len(parts) < 2 {
|
|
||||||
return "", "", fmt.Errorf("'container:' netmode requires a container id or name", netMode)
|
return "", "", fmt.Errorf("'container:' netmode requires a container id or name", netMode)
|
||||||
}
|
}
|
||||||
container = parts[1]
|
container = parts[1]
|
||||||
|
return mode, container, nil
|
||||||
|
default:
|
||||||
|
return "", "", fmt.Errorf("invalid netmode: %q", netMode)
|
||||||
}
|
}
|
||||||
return mode, container, nil
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
package runconfig
|
package runconfig
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/dotcloud/docker/utils"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/dotcloud/docker/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestParseLxcConfOpt(t *testing.T) {
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue