Browse code

Add test coverage to pkg/parsers

Signed-off-by: Vincent Demeester <vincent@sbr.pm>

Vincent Demeester authored on 2015/06/30 00:01:20
Showing 3 changed files
... ...
@@ -30,33 +30,75 @@ func TestParseArgs(t *testing.T) {
30 30
 	}
31 31
 }
32 32
 
33
-func TestParam(t *testing.T) {
33
+func TestParseArgsEdgeCase(t *testing.T) {
34
+	var filters Args
35
+	args, err := ParseFlag("", filters)
36
+	if err != nil {
37
+		t.Fatal(err)
38
+	}
39
+	if args == nil || len(args) != 0 {
40
+		t.Fatalf("Expected an empty Args (map), got %v", args)
41
+	}
42
+	if args, err = ParseFlag("anything", args); err == nil || err != ErrorBadFormat {
43
+		t.Fatalf("Expected ErrorBadFormat, got %v", err)
44
+	}
45
+}
46
+
47
+func TestToParam(t *testing.T) {
34 48
 	a := Args{
35 49
 		"created":    []string{"today"},
36 50
 		"image.name": []string{"ubuntu*", "*untu"},
37 51
 	}
38 52
 
39
-	v, err := ToParam(a)
53
+	_, err := ToParam(a)
40 54
 	if err != nil {
41 55
 		t.Errorf("failed to marshal the filters: %s", err)
42 56
 	}
43
-	v1, err := FromParam(v)
44
-	if err != nil {
45
-		t.Errorf("%s", err)
57
+}
58
+
59
+func TestFromParam(t *testing.T) {
60
+	invalids := []string{
61
+		"anything",
62
+		"['a','list']",
63
+		"{'key': 'value'}",
64
+		`{"key": "value"}`,
46 65
 	}
47
-	for key, vals := range v1 {
48
-		if _, ok := a[key]; !ok {
49
-			t.Errorf("could not find key %s in original set", key)
66
+	valids := map[string]Args{
67
+		`{"key": ["value"]}`: {
68
+			"key": {"value"},
69
+		},
70
+		`{"key": ["value1", "value2"]}`: {
71
+			"key": {"value1", "value2"},
72
+		},
73
+		`{"key1": ["value1"], "key2": ["value2"]}`: {
74
+			"key1": {"value1"},
75
+			"key2": {"value2"},
76
+		},
77
+	}
78
+	for _, invalid := range invalids {
79
+		if _, err := FromParam(invalid); err == nil {
80
+			t.Fatalf("Expected an error with %v, got nothing", invalid)
50 81
 		}
51
-		sort.Strings(vals)
52
-		sort.Strings(a[key])
53
-		if len(vals) != len(a[key]) {
54
-			t.Errorf("value lengths ought to match")
55
-			continue
82
+	}
83
+	for json, expectedArgs := range valids {
84
+		args, err := FromParam(json)
85
+		if err != nil {
86
+			t.Fatal(err)
87
+		}
88
+		if len(args) != len(expectedArgs) {
89
+			t.Fatalf("Expected %v, go %v", expectedArgs, args)
56 90
 		}
57
-		for i := range vals {
58
-			if vals[i] != a[key][i] {
59
-				t.Errorf("expected %s, but got %s", a[key][i], vals[i])
91
+		for key, expectedValues := range expectedArgs {
92
+			values := args[key]
93
+			sort.Strings(values)
94
+			sort.Strings(expectedValues)
95
+			if len(values) != len(expectedValues) {
96
+				t.Fatalf("Expected %v, go %v", expectedArgs, args)
97
+			}
98
+			for index, expectedValue := range expectedValues {
99
+				if values[index] != expectedValue {
100
+					t.Fatalf("Expected %v, go %v", expectedArgs, args)
101
+				}
60 102
 			}
61 103
 		}
62 104
 	}
... ...
@@ -76,3 +118,101 @@ func TestEmpty(t *testing.T) {
76 76
 		t.Errorf("these should both be empty sets")
77 77
 	}
78 78
 }
79
+
80
+func TestArgsMatchKVList(t *testing.T) {
81
+	// empty sources
82
+	args := Args{
83
+		"created": []string{"today"},
84
+	}
85
+	if args.MatchKVList("created", map[string]string{}) {
86
+		t.Fatalf("Expected false for (%v,created), got true", args)
87
+	}
88
+	// Not empty sources
89
+	sources := map[string]string{
90
+		"key1": "value1",
91
+		"key2": "value2",
92
+		"key3": "value3",
93
+	}
94
+	matches := map[*Args]string{
95
+		&Args{}: "field",
96
+		&Args{
97
+			"created": []string{"today"},
98
+			"labels":  []string{"key1"},
99
+		}: "labels",
100
+		&Args{
101
+			"created": []string{"today"},
102
+			"labels":  []string{"key1=value1"},
103
+		}: "labels",
104
+	}
105
+	differs := map[*Args]string{
106
+		&Args{
107
+			"created": []string{"today"},
108
+		}: "created",
109
+		&Args{
110
+			"created": []string{"today"},
111
+			"labels":  []string{"key4"},
112
+		}: "labels",
113
+		&Args{
114
+			"created": []string{"today"},
115
+			"labels":  []string{"key1=value3"},
116
+		}: "labels",
117
+	}
118
+	for args, field := range matches {
119
+		if args.MatchKVList(field, sources) != true {
120
+			t.Fatalf("Expected true for %v on %v, got false", sources, args)
121
+		}
122
+	}
123
+	for args, field := range differs {
124
+		if args.MatchKVList(field, sources) != false {
125
+			t.Fatalf("Expected false for %v on %v, got true", sources, args)
126
+		}
127
+	}
128
+}
129
+
130
+func TestArgsMatch(t *testing.T) {
131
+	source := "today"
132
+	matches := map[*Args]string{
133
+		&Args{}: "field",
134
+		&Args{
135
+			"created": []string{"today"},
136
+			"labels":  []string{"key1"},
137
+		}: "today",
138
+		&Args{
139
+			"created": []string{"to*"},
140
+		}: "created",
141
+		&Args{
142
+			"created": []string{"to(.*)"},
143
+		}: "created",
144
+		&Args{
145
+			"created": []string{"tod"},
146
+		}: "created",
147
+		&Args{
148
+			"created": []string{"anything", "to*"},
149
+		}: "created",
150
+	}
151
+	differs := map[*Args]string{
152
+		&Args{
153
+			"created": []string{"tomorrow"},
154
+		}: "created",
155
+		&Args{
156
+			"created": []string{"to(day"},
157
+		}: "created",
158
+		&Args{
159
+			"created": []string{"tom(.*)"},
160
+		}: "created",
161
+		&Args{
162
+			"created": []string{"today1"},
163
+			"labels":  []string{"today"},
164
+		}: "created",
165
+	}
166
+	for args, field := range matches {
167
+		if args.Match(field, source) != true {
168
+			t.Fatalf("Expected true for %v on %v, got false", source, args)
169
+		}
170
+	}
171
+	for args, field := range differs {
172
+		if args.Match(field, source) != false {
173
+			t.Fatalf("Expected false for %v on %v, got true", source, args)
174
+		}
175
+	}
176
+}
... ...
@@ -1,6 +1,7 @@
1 1
 package kernel
2 2
 
3 3
 import (
4
+	"fmt"
4 5
 	"testing"
5 6
 )
6 7
 
... ...
@@ -11,7 +12,7 @@ func assertParseRelease(t *testing.T, release string, b *KernelVersionInfo, resu
11 11
 	a, _ = ParseRelease(release)
12 12
 
13 13
 	if r := CompareKernelVersion(a, b); r != result {
14
-		t.Fatalf("Unexpected kernel version comparison result. Found %d, expected %d", r, result)
14
+		t.Fatalf("Unexpected kernel version comparison result for (%v,%v). Found %d, expected %d", release, b, r, result)
15 15
 	}
16 16
 	if a.Flavor != b.Flavor {
17 17
 		t.Fatalf("Unexpected parsed kernel flavor.  Found %s, expected %s", a.Flavor, b.Flavor)
... ...
@@ -25,6 +26,20 @@ func TestParseRelease(t *testing.T) {
25 25
 	assertParseRelease(t, "3.8.0-19-generic", &KernelVersionInfo{Kernel: 3, Major: 8, Minor: 0, Flavor: "-19-generic"}, 0)
26 26
 	assertParseRelease(t, "3.12.8tag", &KernelVersionInfo{Kernel: 3, Major: 12, Minor: 8, Flavor: "tag"}, 0)
27 27
 	assertParseRelease(t, "3.12-1-amd64", &KernelVersionInfo{Kernel: 3, Major: 12, Minor: 0, Flavor: "-1-amd64"}, 0)
28
+	assertParseRelease(t, "3.8.0", &KernelVersionInfo{Kernel: 4, Major: 8, Minor: 0}, -1)
29
+	// Errors
30
+	invalids := []string{
31
+		"3",
32
+		"a",
33
+		"a.a",
34
+		"a.a.a-a",
35
+	}
36
+	for _, invalid := range invalids {
37
+		expectedMessage := fmt.Sprintf("Can't parse kernel version %v", invalid)
38
+		if _, err := ParseRelease(invalid); err == nil || err.Error() != expectedMessage {
39
+
40
+		}
41
+	}
28 42
 }
29 43
 
30 44
 func assertKernelVersion(t *testing.T, a, b *KernelVersionInfo, result int) {
... ...
@@ -58,4 +73,20 @@ func TestCompareKernelVersion(t *testing.T) {
58 58
 		&KernelVersionInfo{Kernel: 3, Major: 0, Minor: 20},
59 59
 		&KernelVersionInfo{Kernel: 3, Major: 8, Minor: 0},
60 60
 		-1)
61
+	assertKernelVersion(t,
62
+		&KernelVersionInfo{Kernel: 3, Major: 7, Minor: 20},
63
+		&KernelVersionInfo{Kernel: 3, Major: 8, Minor: 0},
64
+		-1)
65
+	assertKernelVersion(t,
66
+		&KernelVersionInfo{Kernel: 3, Major: 8, Minor: 20},
67
+		&KernelVersionInfo{Kernel: 3, Major: 7, Minor: 0},
68
+		1)
69
+	assertKernelVersion(t,
70
+		&KernelVersionInfo{Kernel: 3, Major: 8, Minor: 20},
71
+		&KernelVersionInfo{Kernel: 3, Major: 8, Minor: 0},
72
+		1)
73
+	assertKernelVersion(t,
74
+		&KernelVersionInfo{Kernel: 3, Major: 8, Minor: 0},
75
+		&KernelVersionInfo{Kernel: 3, Major: 8, Minor: 20},
76
+		-1)
61 77
 }
... ...
@@ -10,35 +10,38 @@ func TestParseHost(t *testing.T) {
10 10
 		defaultHttpHost = "127.0.0.1"
11 11
 		defaultUnix     = "/var/run/docker.sock"
12 12
 	)
13
-	if addr, err := ParseHost(defaultHttpHost, defaultUnix, "0.0.0.0"); err == nil {
14
-		t.Errorf("tcp 0.0.0.0 address expected error return, but err == nil, got %s", addr)
13
+	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
+		"udp://127.0.0.1":      "Invalid bind address format: udp://127.0.0.1",
18
+		"udp://127.0.0.1:2375": "Invalid bind address format: udp://127.0.0.1:2375",
19
+	}
20
+	valids := map[string]string{
21
+		"0.0.0.1:5555": "tcp://0.0.0.1:5555",
22
+		":6666":        "tcp://127.0.0.1:6666",
23
+		"tcp://:7777":  "tcp://127.0.0.1:7777",
24
+		"":             "unix:///var/run/docker.sock",
25
+		"unix:///run/docker.sock": "unix:///run/docker.sock",
26
+		"unix://":                 "unix:///var/run/docker.sock",
27
+		"fd://":                   "fd://",
28
+		"fd://something":          "fd://something",
29
+	}
30
+	for invalidAddr, expectedError := range invalids {
31
+		if addr, err := ParseHost(defaultHttpHost, defaultUnix, invalidAddr); err == nil || err.Error() != expectedError {
32
+			t.Errorf("tcp %v address expected error %v return, got %s and addr %v", invalidAddr, expectedError, err, addr)
33
+		}
34
+	}
35
+	for validAddr, expectedAddr := range valids {
36
+		if addr, err := ParseHost(defaultHttpHost, defaultUnix, validAddr); err != nil || addr != expectedAddr {
37
+			t.Errorf("%v -> expected %v, got %v", validAddr, expectedAddr, addr)
38
+		}
15 39
 	}
16
-	if addr, err := ParseHost(defaultHttpHost, defaultUnix, "tcp://"); err == nil {
17
-		t.Errorf("default tcp:// address expected error return, but err == nil, got %s", addr)
18
-	}
19
-	if addr, err := ParseHost(defaultHttpHost, defaultUnix, "0.0.0.1:5555"); err != nil || addr != "tcp://0.0.0.1:5555" {
20
-		t.Errorf("0.0.0.1:5555 -> expected tcp://0.0.0.1:5555, got %s", addr)
21
-	}
22
-	if addr, err := ParseHost(defaultHttpHost, defaultUnix, ":6666"); err != nil || addr != "tcp://127.0.0.1:6666" {
23
-		t.Errorf(":6666 -> expected tcp://127.0.0.1:6666, got %s", addr)
24
-	}
25
-	if addr, err := ParseHost(defaultHttpHost, defaultUnix, "tcp://:7777"); err != nil || addr != "tcp://127.0.0.1:7777" {
26
-		t.Errorf("tcp://:7777 -> expected tcp://127.0.0.1:7777, got %s", addr)
27
-	}
28
-	if addr, err := ParseHost(defaultHttpHost, defaultUnix, ""); err != nil || addr != "unix:///var/run/docker.sock" {
29
-		t.Errorf("empty argument -> expected unix:///var/run/docker.sock, got %s", addr)
30
-	}
31
-	if addr, err := ParseHost(defaultHttpHost, defaultUnix, "unix:///var/run/docker.sock"); err != nil || addr != "unix:///var/run/docker.sock" {
32
-		t.Errorf("unix:///var/run/docker.sock -> expected unix:///var/run/docker.sock, got %s", addr)
33
-	}
34
-	if addr, err := ParseHost(defaultHttpHost, defaultUnix, "unix://"); err != nil || addr != "unix:///var/run/docker.sock" {
35
-		t.Errorf("unix:///var/run/docker.sock -> expected unix:///var/run/docker.sock, got %s", addr)
36
-	}
37
-	if addr, err := ParseHost(defaultHttpHost, defaultUnix, "udp://127.0.0.1"); err == nil {
38
-		t.Errorf("udp protocol address expected error return, but err == nil. Got %s", addr)
39
-	}
40
-	if addr, err := ParseHost(defaultHttpHost, defaultUnix, "udp://127.0.0.1:2375"); err == nil {
41
-		t.Errorf("udp protocol address expected error return, but err == nil. Got %s", addr)
40
+}
41
+
42
+func TestParseInvalidUnixAddrInvalid(t *testing.T) {
43
+	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" {
44
+		t.Fatalf("Expected an error, got %v", err)
42 45
 	}
43 46
 }
44 47
 
... ...
@@ -73,6 +76,9 @@ func TestParseRepositoryTag(t *testing.T) {
73 73
 }
74 74
 
75 75
 func TestParsePortMapping(t *testing.T) {
76
+	if _, err := PartParser("ip:public:private", "192.168.1.1:80"); err == nil {
77
+		t.Fatalf("Expected an error, got %v", err)
78
+	}
76 79
 	data, err := PartParser("ip:public:private", "192.168.1.1:80:8080")
77 80
 	if err != nil {
78 81
 		t.Fatal(err)
... ...
@@ -92,12 +98,55 @@ func TestParsePortMapping(t *testing.T) {
92 92
 	}
93 93
 }
94 94
 
95
+func TestParseKeyValueOpt(t *testing.T) {
96
+	invalids := map[string]string{
97
+		"":    "Unable to parse key/value option: ",
98
+		"key": "Unable to parse key/value option: key",
99
+	}
100
+	for invalid, expectedError := range invalids {
101
+		if _, _, err := ParseKeyValueOpt(invalid); err == nil || err.Error() != expectedError {
102
+			t.Fatalf("Expected error %v for %v, got %v", expectedError, invalid, err)
103
+		}
104
+	}
105
+	valids := map[string][]string{
106
+		"key=value":               {"key", "value"},
107
+		" key = value ":           {"key", "value"},
108
+		"key=value1=value2":       {"key", "value1=value2"},
109
+		" key = value1 = value2 ": {"key", "value1 = value2"},
110
+	}
111
+	for valid, expectedKeyValue := range valids {
112
+		key, value, err := ParseKeyValueOpt(valid)
113
+		if err != nil {
114
+			t.Fatal(err)
115
+		}
116
+		if key != expectedKeyValue[0] || value != expectedKeyValue[1] {
117
+			t.Fatalf("Expected {%v: %v} got {%v: %v}", expectedKeyValue[0], expectedKeyValue[1], key, value)
118
+		}
119
+	}
120
+}
121
+
95 122
 func TestParsePortRange(t *testing.T) {
96 123
 	if start, end, err := ParsePortRange("8000-8080"); err != nil || start != 8000 || end != 8080 {
97 124
 		t.Fatalf("Error: %s or Expecting {start,end} values {8000,8080} but found {%d,%d}.", err, start, end)
98 125
 	}
99 126
 }
100 127
 
128
+func TestParsePortRangeEmpty(t *testing.T) {
129
+	if _, _, err := ParsePortRange(""); err == nil || err.Error() != "Empty string specified for ports." {
130
+		t.Fatalf("Expected error 'Empty string specified for ports.', got %v", err)
131
+	}
132
+}
133
+
134
+func TestParsePortRangeWithNoRange(t *testing.T) {
135
+	start, end, err := ParsePortRange("8080")
136
+	if err != nil {
137
+		t.Fatal(err)
138
+	}
139
+	if start != 8080 || end != 8080 {
140
+		t.Fatalf("Expected start and end to be the same and equal to 8080, but were %v and %v", start, end)
141
+	}
142
+}
143
+
101 144
 func TestParsePortRangeIncorrectRange(t *testing.T) {
102 145
 	if _, _, err := ParsePortRange("9000-8080"); err == nil || !strings.Contains(err.Error(), "Invalid range specified for the Port") {
103 146
 		t.Fatalf("Expecting error 'Invalid range specified for the Port' but received %s.", err)