Browse code

Exploring the code using unit tests - these 2 functions do not work consistently

Signed-off-by: Sven Dowideit <SvenDowideit@home.org.au>

Sven Dowideit authored on 2015/08/26 18:35:16
Showing 1 changed files
... ...
@@ -1,6 +1,7 @@
1 1
 package parsers
2 2
 
3 3
 import (
4
+	"runtime"
4 5
 	"strings"
5 6
 	"testing"
6 7
 )
... ...
@@ -9,14 +10,20 @@ func TestParseHost(t *testing.T) {
9 9
 	var (
10 10
 		defaultHTTPHost = "127.0.0.1"
11 11
 		defaultUnix     = "/var/run/docker.sock"
12
+		defaultHOST     = "unix:///var/run/docker.sock"
12 13
 	)
13 14
 	invalids := map[string]string{
14
-		"0.0.0.0":              "Invalid bind address format: 0.0.0.0",
15
-		"tcp://":               "Invalid proto, expected tcp: ",
16
-		"tcp:a.b.c.d":          "Invalid bind address format: tcp:a.b.c.d",
17
-		"tcp:a.b.c.d/path":     "Invalid bind address format: tcp:a.b.c.d/path",
18
-		"udp://127.0.0.1":      "Invalid bind address format: udp://127.0.0.1",
19
-		"udp://127.0.0.1:2375": "Invalid bind address format: udp://127.0.0.1:2375",
15
+		"0.0.0.0":                       "Invalid bind address format: 0.0.0.0",
16
+		"0.0.0.0:":                      "Invalid bind address format: 0.0.0.0:",
17
+		"tcp://":                        "Invalid proto, expected tcp: ",
18
+		"tcp:a.b.c.d":                   "Invalid bind address format: tcp:a.b.c.d",
19
+		"tcp:a.b.c.d/path":              "Invalid bind address format: tcp:a.b.c.d/path",
20
+		"udp://127.0.0.1":               "Invalid bind address format: udp://127.0.0.1",
21
+		"udp://127.0.0.1:2375":          "Invalid bind address format: udp://127.0.0.1:2375",
22
+		"tcp://unix:///run/docker.sock": "Invalid bind address format: unix",
23
+		"tcp":  "Invalid bind address format: tcp",
24
+		"unix": "Invalid bind address format: unix",
25
+		"fd":   "Invalid bind address format: fd",
20 26
 	}
21 27
 	valids := map[string]string{
22 28
 		"0.0.0.1:5555":      "tcp://0.0.0.1:5555",
... ...
@@ -25,12 +32,24 @@ func TestParseHost(t *testing.T) {
25 25
 		":6666/path":        "tcp://127.0.0.1:6666/path",
26 26
 		"tcp://:7777":       "tcp://127.0.0.1:7777",
27 27
 		"tcp://:7777/path":  "tcp://127.0.0.1:7777/path",
28
-		"":                  "unix:///var/run/docker.sock",
28
+		// as there's a TrimSpace in there, we should test for it.
29
+		" tcp://:7777/path ":      "tcp://127.0.0.1:7777/path",
29 30
 		"unix:///run/docker.sock": "unix:///run/docker.sock",
30 31
 		"unix://":                 "unix:///var/run/docker.sock",
31 32
 		"fd://":                   "fd://",
32 33
 		"fd://something":          "fd://something",
33 34
 	}
35
+	if runtime.GOOS == "windows" {
36
+		defaultHOST = "Invalid bind address format: 127.0.0.1"
37
+		// SVEN: an example of the conflicted defaultHTTPHost
38
+		invalids[""] = defaultHOST
39
+		invalids[" "] = defaultHOST
40
+		invalids["  "] = defaultHOST
41
+	} else {
42
+		valids[""] = defaultHOST
43
+		valids[" "] = defaultHOST
44
+		valids["  "] = defaultHOST
45
+	}
34 46
 	for invalidAddr, expectedError := range invalids {
35 47
 		if addr, err := ParseHost(defaultHTTPHost, defaultUnix, invalidAddr); err == nil || err.Error() != expectedError {
36 48
 			t.Errorf("tcp %v address expected error %v return, got %s and addr %v", invalidAddr, expectedError, err, addr)
... ...
@@ -38,15 +57,56 @@ func TestParseHost(t *testing.T) {
38 38
 	}
39 39
 	for validAddr, expectedAddr := range valids {
40 40
 		if addr, err := ParseHost(defaultHTTPHost, defaultUnix, validAddr); err != nil || addr != expectedAddr {
41
-			t.Errorf("%v -> expected %v, got %v", validAddr, expectedAddr, addr)
41
+			t.Errorf("%v -> expected %v, got (%v) addr (%v)", validAddr, expectedAddr, err, addr)
42
+		}
43
+	}
44
+}
45
+
46
+func TestParseTCP(t *testing.T) {
47
+	var (
48
+		//SVEN if this is set to tcp://127.0.0.1, then we end up with results like 'tcp://tcp://127.0.0.1:6666'
49
+		defaultHTTPHost = "127.0.0.1"
50
+	)
51
+	invalids := map[string]string{
52
+		"0.0.0.0":              "Invalid bind address format: 0.0.0.0",
53
+		"0.0.0.0:":             "Invalid bind address format: 0.0.0.0:",
54
+		"tcp://":               "Invalid proto, expected tcp: ",
55
+		"tcp:a.b.c.d":          "Invalid bind address format: tcp:a.b.c.d",
56
+		"tcp:a.b.c.d/path":     "Invalid bind address format: tcp:a.b.c.d/path",
57
+		"udp://127.0.0.1":      "Invalid proto, expected tcp: udp://127.0.0.1",
58
+		"udp://127.0.0.1:2375": "Invalid proto, expected tcp: udp://127.0.0.1:2375",
59
+		"": "Invalid proto, expected tcp: ",
60
+	}
61
+	valids := map[string]string{
62
+		"0.0.0.1:5555":      "tcp://0.0.0.1:5555",
63
+		"0.0.0.1:5555/path": "tcp://0.0.0.1:5555/path",
64
+		":6666":             "tcp://127.0.0.1:6666",
65
+		":6666/path":        "tcp://127.0.0.1:6666/path",
66
+		"tcp://:7777":       "tcp://127.0.0.1:7777",
67
+		"tcp://:7777/path":  "tcp://127.0.0.1:7777/path",
68
+	}
69
+	for invalidAddr, expectedError := range invalids {
70
+		if addr, err := ParseTCPAddr(invalidAddr, defaultHTTPHost); err == nil || err.Error() != expectedError {
71
+			t.Errorf("tcp %v address expected error %v return, got %s and addr %v", invalidAddr, expectedError, err, addr)
72
+		}
73
+	}
74
+	for validAddr, expectedAddr := range valids {
75
+		if addr, err := ParseTCPAddr(validAddr, defaultHTTPHost); err != nil || addr != expectedAddr {
76
+			t.Errorf("%v -> expected %v, got %v and addr %v", validAddr, expectedAddr, err, addr)
42 77
 		}
43 78
 	}
44 79
 }
45 80
 
46 81
 func TestParseInvalidUnixAddrInvalid(t *testing.T) {
82
+	if _, err := ParseUnixAddr("tcp://127.0.0.1", "unix:///var/run/docker.sock"); err == nil || err.Error() != "Invalid proto, expected unix: tcp://127.0.0.1" {
83
+		t.Fatalf("Expected an error, got %v", err)
84
+	}
47 85
 	if _, err := ParseUnixAddr("unix://tcp://127.0.0.1", "unix:///var/run/docker.sock"); err == nil || err.Error() != "Invalid proto, expected unix: tcp://127.0.0.1" {
48 86
 		t.Fatalf("Expected an error, got %v", err)
49 87
 	}
88
+	if v, err := ParseUnixAddr("", "/var/run/docker.sock"); err != nil || v != "unix:///var/run/docker.sock" {
89
+		t.Fatalf("Expected an %v, got %v", v, "unix:///var/run/docker.sock")
90
+	}
50 91
 }
51 92
 
52 93
 func TestParseRepositoryTag(t *testing.T) {