Browse code

Change sorting method and add test to DisplayablePorts

Signed-off-by: Garrett Barboza <garrett@garrettbarboza.com>

Garrett Barboza authored on 2015/10/20 00:38:54
Showing 2 changed files
... ...
@@ -27,12 +27,26 @@ const (
27 27
 	DefaultDockerfileName string = "Dockerfile"
28 28
 )
29 29
 
30
-// byPrivatePort is temporary type used to sort types.Port by PrivatePort
31
-type byPrivatePort []types.Port
30
+// byPortInfo is a temporary type used to sort types.Port by its fields
31
+type byPortInfo []types.Port
32 32
 
33
-func (r byPrivatePort) Len() int           { return len(r) }
34
-func (r byPrivatePort) Swap(i, j int)      { r[i], r[j] = r[j], r[i] }
35
-func (r byPrivatePort) Less(i, j int) bool { return r[i].PrivatePort < r[j].PrivatePort }
33
+func (r byPortInfo) Len() int      { return len(r) }
34
+func (r byPortInfo) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
35
+func (r byPortInfo) Less(i, j int) bool {
36
+	if r[i].PrivatePort != r[j].PrivatePort {
37
+		return r[i].PrivatePort < r[j].PrivatePort
38
+	}
39
+
40
+	if r[i].IP != r[j].IP {
41
+		return r[i].IP < r[j].IP
42
+	}
43
+
44
+	if r[i].PublicPort != r[j].PublicPort {
45
+		return r[i].PublicPort < r[j].PublicPort
46
+	}
47
+
48
+	return r[i].Type < r[j].Type
49
+}
36 50
 
37 51
 // DisplayablePorts returns formatted string representing open ports of container
38 52
 // e.g. "0.0.0.0:80->9090/tcp, 9988/tcp"
... ...
@@ -45,7 +59,8 @@ func DisplayablePorts(ports []types.Port) string {
45 45
 	groupMap := make(map[string]*portGroup)
46 46
 	var result []string
47 47
 	var hostMappings []string
48
-	sort.Sort(byPrivatePort(ports))
48
+	var groupMapKeys []string
49
+	sort.Sort(byPortInfo(ports))
49 50
 	for _, port := range ports {
50 51
 		current := port.PrivatePort
51 52
 		portKey := port.Type
... ...
@@ -60,6 +75,8 @@ func DisplayablePorts(ports []types.Port) string {
60 60
 
61 61
 		if group == nil {
62 62
 			groupMap[portKey] = &portGroup{first: current, last: current}
63
+			// record order that groupMap keys are created
64
+			groupMapKeys = append(groupMapKeys, portKey)
63 65
 			continue
64 66
 		}
65 67
 		if current == (group.last + 1) {
... ...
@@ -70,7 +87,8 @@ func DisplayablePorts(ports []types.Port) string {
70 70
 		result = append(result, formGroup(portKey, group.first, group.last))
71 71
 		groupMap[portKey] = &portGroup{first: current, last: current}
72 72
 	}
73
-	for portKey, g := range groupMap {
73
+	for _, portKey := range groupMapKeys {
74
+		g := groupMap[portKey]
74 75
 		result = append(result, formGroup(portKey, g.first, g.last))
75 76
 	}
76 77
 	result = append(result, hostMappings...)
... ...
@@ -166,7 +166,7 @@ func TestDisplayablePorts(t *testing.T) {
166 166
 					Type:        "tcp",
167 167
 				},
168 168
 			},
169
-			"4.3.2.1:3322->2233/tcp, 1.2.3.4:8899->9988/udp, 1.2.3.4:8899->9988/tcp",
169
+			"4.3.2.1:3322->2233/tcp, 1.2.3.4:8899->9988/tcp, 1.2.3.4:8899->9988/udp",
170 170
 		},
171 171
 		{
172 172
 			[]types.Port{
... ...
@@ -188,6 +188,64 @@ func TestDisplayablePorts(t *testing.T) {
188 188
 			},
189 189
 			"9988/udp, 4.3.2.1:3322->2233/tcp, 1.2.3.4:7766->6677/tcp",
190 190
 		},
191
+		{
192
+			[]types.Port{
193
+				{
194
+					PrivatePort: 80,
195
+					Type:        "tcp",
196
+				}, {
197
+					PrivatePort: 1024,
198
+					Type:        "tcp",
199
+				}, {
200
+					PrivatePort: 80,
201
+					Type:        "udp",
202
+				}, {
203
+					PrivatePort: 1024,
204
+					Type:        "udp",
205
+				}, {
206
+					IP:          "1.1.1.1",
207
+					PublicPort:  80,
208
+					PrivatePort: 1024,
209
+					Type:        "tcp",
210
+				}, {
211
+					IP:          "1.1.1.1",
212
+					PublicPort:  80,
213
+					PrivatePort: 1024,
214
+					Type:        "udp",
215
+				}, {
216
+					IP:          "1.1.1.1",
217
+					PublicPort:  1024,
218
+					PrivatePort: 80,
219
+					Type:        "tcp",
220
+				}, {
221
+					IP:          "1.1.1.1",
222
+					PublicPort:  1024,
223
+					PrivatePort: 80,
224
+					Type:        "udp",
225
+				}, {
226
+					IP:          "2.1.1.1",
227
+					PublicPort:  80,
228
+					PrivatePort: 1024,
229
+					Type:        "tcp",
230
+				}, {
231
+					IP:          "2.1.1.1",
232
+					PublicPort:  80,
233
+					PrivatePort: 1024,
234
+					Type:        "udp",
235
+				}, {
236
+					IP:          "2.1.1.1",
237
+					PublicPort:  1024,
238
+					PrivatePort: 80,
239
+					Type:        "tcp",
240
+				}, {
241
+					IP:          "2.1.1.1",
242
+					PublicPort:  1024,
243
+					PrivatePort: 80,
244
+					Type:        "udp",
245
+				},
246
+			},
247
+			"80/tcp, 80/udp, 1024/tcp, 1024/udp, 1.1.1.1:1024->80/tcp, 1.1.1.1:1024->80/udp, 2.1.1.1:1024->80/tcp, 2.1.1.1:1024->80/udp, 1.1.1.1:80->1024/tcp, 1.1.1.1:80->1024/udp, 2.1.1.1:80->1024/tcp, 2.1.1.1:80->1024/udp",
248
+		},
191 249
 	}
192 250
 
193 251
 	for _, port := range cases {