Browse code

runconfig/parse: add test for parseNetMode

Docker-DCO-1.1-Signed-off-by: Johan Euphrosine <proppy@google.com> (github: proppy)

Johan Euphrosine authored on 2014/05/02 17:47:12
Showing 2 changed files
... ...
@@ -284,16 +284,17 @@ func parseKeyValueOpts(opts opts.ListOpts) ([]utils.KeyValuePair, error) {
284 284
 
285 285
 func parseNetMode(netMode string) (string, string, error) {
286 286
 	parts := strings.Split(netMode, ":")
287
-	if len(parts) < 1 {
288
-		return "", "", fmt.Errorf("'netmode' cannot be empty", netMode)
289
-	}
290
-	mode := parts[0]
291
-	var container string
292
-	if mode == "container" {
293
-		if len(parts) < 2 {
287
+	switch mode := parts[0]; mode {
288
+	case "bridge", "disable":
289
+		return mode, "", nil
290
+	case "container":
291
+		var container string
292
+		if len(parts) < 2 || parts[1] == "" {
294 293
 			return "", "", fmt.Errorf("'container:' netmode requires a container id or name", netMode)
295 294
 		}
296 295
 		container = parts[1]
296
+		return mode, container, nil
297
+	default:
298
+		return "", "", fmt.Errorf("invalid netmode: %q", netMode)
297 299
 	}
298
-	return mode, container, nil
299 300
 }
... ...
@@ -1,8 +1,9 @@
1 1
 package runconfig
2 2
 
3 3
 import (
4
-	"github.com/dotcloud/docker/utils"
5 4
 	"testing"
5
+
6
+	"github.com/dotcloud/docker/utils"
6 7
 )
7 8
 
8 9
 func TestParseLxcConfOpt(t *testing.T) {
... ...
@@ -21,3 +22,33 @@ func TestParseLxcConfOpt(t *testing.T) {
21 21
 		}
22 22
 	}
23 23
 }
24
+
25
+func TestParseNetMode(t *testing.T) {
26
+	testFlags := []struct {
27
+		flag      string
28
+		mode      string
29
+		container string
30
+		err       bool
31
+	}{
32
+		{"", "", "", true},
33
+		{"bridge", "bridge", "", false},
34
+		{"disable", "disable", "", false},
35
+		{"container:foo", "container", "foo", false},
36
+		{"container:", "", "", true},
37
+		{"container", "", "", true},
38
+		{"unknown", "", "", true},
39
+	}
40
+
41
+	for _, to := range testFlags {
42
+		mode, container, err := parseNetMode(to.flag)
43
+		if mode != to.mode {
44
+			t.Fatalf("-net %s: expected net mode: %q, got: %q", to.flag, to.mode, mode)
45
+		}
46
+		if container != to.container {
47
+			t.Fatalf("-net %s: expected net container: %q, got: %q", to.flag, to.container, container)
48
+		}
49
+		if (err != nil) != to.err {
50
+			t.Fatal("-net %s: expected an error got none", to.flag)
51
+		}
52
+	}
53
+}