Browse code

Add more unit tests (thus coverage) to pkg api

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

Vincent Demeester authored on 2015/08/04 22:30:38
Showing 3 changed files
1 1
deleted file mode 100644
... ...
@@ -1,19 +0,0 @@
1
-package api
2
-
3
-import (
4
-	"testing"
5
-)
6
-
7
-func TestJsonContentType(t *testing.T) {
8
-	if !MatchesContentType("application/json", "application/json") {
9
-		t.Fail()
10
-	}
11
-
12
-	if !MatchesContentType("application/json; charset=utf-8", "application/json") {
13
-		t.Fail()
14
-	}
15
-
16
-	if MatchesContentType("dockerapplication/json", "application/json") {
17
-		t.Fail()
18
-	}
19
-}
20 1
new file mode 100644
... ...
@@ -0,0 +1,282 @@
0
+package api
1
+
2
+import (
3
+	"io/ioutil"
4
+	"path/filepath"
5
+	"testing"
6
+
7
+	"github.com/docker/docker/api/types"
8
+	"os"
9
+)
10
+
11
+type ports struct {
12
+	ports    []types.Port
13
+	expected string
14
+}
15
+
16
+// DisplayablePorts
17
+func TestDisplayablePorts(t *testing.T) {
18
+	cases := []ports{
19
+		{
20
+			[]types.Port{
21
+				{
22
+					PrivatePort: 9988,
23
+					Type:        "tcp",
24
+				},
25
+			},
26
+			"9988/tcp"},
27
+		{
28
+			[]types.Port{
29
+				{
30
+					PrivatePort: 9988,
31
+					Type:        "udp",
32
+				},
33
+			},
34
+			"9988/udp",
35
+		},
36
+		{
37
+			[]types.Port{
38
+				{
39
+					IP:          "0.0.0.0",
40
+					PrivatePort: 9988,
41
+					Type:        "tcp",
42
+				},
43
+			},
44
+			"0.0.0.0:0->9988/tcp",
45
+		},
46
+		{
47
+			[]types.Port{
48
+				{
49
+					PrivatePort: 9988,
50
+					PublicPort:  8899,
51
+					Type:        "tcp",
52
+				},
53
+			},
54
+			"9988/tcp",
55
+		},
56
+		{
57
+			[]types.Port{
58
+				{
59
+					IP:          "4.3.2.1",
60
+					PrivatePort: 9988,
61
+					PublicPort:  8899,
62
+					Type:        "tcp",
63
+				},
64
+			},
65
+			"4.3.2.1:8899->9988/tcp",
66
+		},
67
+		{
68
+			[]types.Port{
69
+				{
70
+					IP:          "4.3.2.1",
71
+					PrivatePort: 9988,
72
+					PublicPort:  9988,
73
+					Type:        "tcp",
74
+				},
75
+			},
76
+			"4.3.2.1:9988->9988/tcp",
77
+		},
78
+		{
79
+			[]types.Port{
80
+				{
81
+					PrivatePort: 9988,
82
+					Type:        "udp",
83
+				}, {
84
+					PrivatePort: 9988,
85
+					Type:        "udp",
86
+				},
87
+			},
88
+			"9988/udp, 9988/udp",
89
+		},
90
+		{
91
+			[]types.Port{
92
+				{
93
+					IP:          "1.2.3.4",
94
+					PublicPort:  9998,
95
+					PrivatePort: 9998,
96
+					Type:        "udp",
97
+				}, {
98
+					IP:          "1.2.3.4",
99
+					PublicPort:  9999,
100
+					PrivatePort: 9999,
101
+					Type:        "udp",
102
+				},
103
+			},
104
+			"1.2.3.4:9998-9999->9998-9999/udp",
105
+		},
106
+		{
107
+			[]types.Port{
108
+				{
109
+					IP:          "1.2.3.4",
110
+					PublicPort:  8887,
111
+					PrivatePort: 9998,
112
+					Type:        "udp",
113
+				}, {
114
+					IP:          "1.2.3.4",
115
+					PublicPort:  8888,
116
+					PrivatePort: 9999,
117
+					Type:        "udp",
118
+				},
119
+			},
120
+			"1.2.3.4:8887->9998/udp, 1.2.3.4:8888->9999/udp",
121
+		},
122
+		{
123
+			[]types.Port{
124
+				{
125
+					PrivatePort: 9998,
126
+					Type:        "udp",
127
+				}, {
128
+					PrivatePort: 9999,
129
+					Type:        "udp",
130
+				},
131
+			},
132
+			"9998-9999/udp",
133
+		},
134
+		{
135
+			[]types.Port{
136
+				{
137
+					IP:          "1.2.3.4",
138
+					PrivatePort: 6677,
139
+					PublicPort:  7766,
140
+					Type:        "tcp",
141
+				}, {
142
+					PrivatePort: 9988,
143
+					PublicPort:  8899,
144
+					Type:        "udp",
145
+				},
146
+			},
147
+			"9988/udp, 1.2.3.4:7766->6677/tcp",
148
+		},
149
+		{
150
+			[]types.Port{
151
+				{
152
+					IP:          "1.2.3.4",
153
+					PrivatePort: 9988,
154
+					PublicPort:  8899,
155
+					Type:        "udp",
156
+				}, {
157
+					IP:          "1.2.3.4",
158
+					PrivatePort: 9988,
159
+					PublicPort:  8899,
160
+					Type:        "tcp",
161
+				}, {
162
+					IP:          "4.3.2.1",
163
+					PrivatePort: 2233,
164
+					PublicPort:  3322,
165
+					Type:        "tcp",
166
+				},
167
+			},
168
+			"4.3.2.1:3322->2233/tcp, 1.2.3.4:8899->9988/udp, 1.2.3.4:8899->9988/tcp",
169
+		},
170
+		{
171
+			[]types.Port{
172
+				{
173
+					PrivatePort: 9988,
174
+					PublicPort:  8899,
175
+					Type:        "udp",
176
+				}, {
177
+					IP:          "1.2.3.4",
178
+					PrivatePort: 6677,
179
+					PublicPort:  7766,
180
+					Type:        "tcp",
181
+				}, {
182
+					IP:          "4.3.2.1",
183
+					PrivatePort: 2233,
184
+					PublicPort:  3322,
185
+					Type:        "tcp",
186
+				},
187
+			},
188
+			"9988/udp, 4.3.2.1:3322->2233/tcp, 1.2.3.4:7766->6677/tcp",
189
+		},
190
+	}
191
+
192
+	for _, port := range cases {
193
+		actual := DisplayablePorts(port.ports)
194
+		if port.expected != actual {
195
+			t.Fatalf("Expected %s, got %s.", port.expected, actual)
196
+		}
197
+	}
198
+}
199
+
200
+// MatchesContentType
201
+func TestJsonContentType(t *testing.T) {
202
+	if !MatchesContentType("application/json", "application/json") {
203
+		t.Fail()
204
+	}
205
+
206
+	if !MatchesContentType("application/json; charset=utf-8", "application/json") {
207
+		t.Fail()
208
+	}
209
+
210
+	if MatchesContentType("dockerapplication/json", "application/json") {
211
+		t.Fail()
212
+	}
213
+}
214
+
215
+// LoadOrCreateTrustKey
216
+func TestLoadOrCreateTrustKeyInvalidKeyFile(t *testing.T) {
217
+	tmpKeyFolderPath, err := ioutil.TempDir("", "api-trustkey-test")
218
+	if err != nil {
219
+		t.Fatal(err)
220
+	}
221
+	defer os.RemoveAll(tmpKeyFolderPath)
222
+
223
+	tmpKeyFile, err := ioutil.TempFile(tmpKeyFolderPath, "keyfile")
224
+	if err != nil {
225
+		t.Fatal(err)
226
+	}
227
+
228
+	if _, err := LoadOrCreateTrustKey(tmpKeyFile.Name()); err == nil {
229
+		t.Fatalf("expected an error, got nothing.")
230
+	}
231
+
232
+}
233
+
234
+func TestLoadOrCreateTrustKeyCreateKey(t *testing.T) {
235
+	tmpKeyFolderPath, err := ioutil.TempDir("", "api-trustkey-test")
236
+	if err != nil {
237
+		t.Fatal(err)
238
+	}
239
+	defer os.RemoveAll(tmpKeyFolderPath)
240
+
241
+	// Without the need to create the folder hierarchy
242
+	tmpKeyFile := filepath.Join(tmpKeyFolderPath, "keyfile")
243
+
244
+	if key, err := LoadOrCreateTrustKey(tmpKeyFile); err != nil || key == nil {
245
+		t.Fatalf("expected a new key file, got : %v and %v", err, key)
246
+	}
247
+
248
+	if _, err := os.Stat(tmpKeyFile); err != nil {
249
+		t.Fatalf("Expected to find a file %s, got %v", tmpKeyFile, err)
250
+	}
251
+
252
+	// With the need to create the folder hierarchy as tmpKeyFie is in a path
253
+	// where some folder do not exists.
254
+	tmpKeyFile = filepath.Join(tmpKeyFolderPath, "folder/hierarchy/keyfile")
255
+
256
+	if key, err := LoadOrCreateTrustKey(tmpKeyFile); err != nil || key == nil {
257
+		t.Fatalf("expected a new key file, got : %v and %v", err, key)
258
+	}
259
+
260
+	if _, err := os.Stat(tmpKeyFile); err != nil {
261
+		t.Fatalf("Expected to find a file %s, got %v", tmpKeyFile, err)
262
+	}
263
+
264
+	// With no path at all
265
+	defer os.Remove("keyfile")
266
+	if key, err := LoadOrCreateTrustKey("keyfile"); err != nil || key == nil {
267
+		t.Fatalf("expected a new key file, got : %v and %v", err, key)
268
+	}
269
+
270
+	if _, err := os.Stat("keyfile"); err != nil {
271
+		t.Fatalf("Expected to find a file keyfile, got %v", err)
272
+	}
273
+}
274
+
275
+func TestLoadOrCreateTrustKeyLoadValidKey(t *testing.T) {
276
+	tmpKeyFile := filepath.Join("fixtures", "keyfile")
277
+
278
+	if key, err := LoadOrCreateTrustKey(tmpKeyFile); err != nil || key == nil {
279
+		t.Fatalf("expected a key file, got : %v and %v", err, key)
280
+	}
281
+}
0 282
new file mode 100644
... ...
@@ -0,0 +1,7 @@
0
+-----BEGIN EC PRIVATE KEY-----
1
+keyID: AWX2:I27X:WQFX:IOMK:CNAK:O7PW:VYNB:ZLKC:CVAE:YJP2:SI4A:XXAY
2
+
3
+MHcCAQEEILHTRWdcpKWsnORxSFyBnndJ4ROU41hMtr/GCiLVvwBQoAoGCCqGSM49
4
+AwEHoUQDQgAElpVFbQ2V2UQKajqdE3fVxJ+/pE/YuEFOxWbOxF2be19BY209/iky
5
+NzeFFK7SLpQ4CBJ7zDVXOHsMzrkY/GquGA==
6
+-----END EC PRIVATE KEY-----