From c4c6d33bbcacab113f874b7407499c5f71cf72a0 Mon Sep 17 00:00:00 2001 From: Garrett Barboza Date: Mon, 19 Oct 2015 10:38:54 -0500 Subject: [PATCH] Change sorting method and add test to DisplayablePorts Signed-off-by: Garrett Barboza --- api/common.go | 32 +++++++++++++++++++------ api/common_test.go | 60 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 84 insertions(+), 8 deletions(-) diff --git a/api/common.go b/api/common.go index 44225e4e9f..3a845028d8 100644 --- a/api/common.go +++ b/api/common.go @@ -27,12 +27,26 @@ const ( DefaultDockerfileName string = "Dockerfile" ) -// byPrivatePort is temporary type used to sort types.Port by PrivatePort -type byPrivatePort []types.Port +// byPortInfo is a temporary type used to sort types.Port by its fields +type byPortInfo []types.Port -func (r byPrivatePort) Len() int { return len(r) } -func (r byPrivatePort) Swap(i, j int) { r[i], r[j] = r[j], r[i] } -func (r byPrivatePort) Less(i, j int) bool { return r[i].PrivatePort < r[j].PrivatePort } +func (r byPortInfo) Len() int { return len(r) } +func (r byPortInfo) Swap(i, j int) { r[i], r[j] = r[j], r[i] } +func (r byPortInfo) Less(i, j int) bool { + if r[i].PrivatePort != r[j].PrivatePort { + return r[i].PrivatePort < r[j].PrivatePort + } + + if r[i].IP != r[j].IP { + return r[i].IP < r[j].IP + } + + if r[i].PublicPort != r[j].PublicPort { + return r[i].PublicPort < r[j].PublicPort + } + + return r[i].Type < r[j].Type +} // DisplayablePorts returns formatted string representing open ports of container // e.g. "0.0.0.0:80->9090/tcp, 9988/tcp" @@ -45,7 +59,8 @@ func DisplayablePorts(ports []types.Port) string { groupMap := make(map[string]*portGroup) var result []string var hostMappings []string - sort.Sort(byPrivatePort(ports)) + var groupMapKeys []string + sort.Sort(byPortInfo(ports)) for _, port := range ports { current := port.PrivatePort portKey := port.Type @@ -60,6 +75,8 @@ func DisplayablePorts(ports []types.Port) string { if group == nil { groupMap[portKey] = &portGroup{first: current, last: current} + // record order that groupMap keys are created + groupMapKeys = append(groupMapKeys, portKey) continue } if current == (group.last + 1) { @@ -70,7 +87,8 @@ func DisplayablePorts(ports []types.Port) string { result = append(result, formGroup(portKey, group.first, group.last)) groupMap[portKey] = &portGroup{first: current, last: current} } - for portKey, g := range groupMap { + for _, portKey := range groupMapKeys { + g := groupMap[portKey] result = append(result, formGroup(portKey, g.first, g.last)) } result = append(result, hostMappings...) diff --git a/api/common_test.go b/api/common_test.go index 39ea93bd64..aa30099d03 100644 --- a/api/common_test.go +++ b/api/common_test.go @@ -166,7 +166,7 @@ func TestDisplayablePorts(t *testing.T) { Type: "tcp", }, }, - "4.3.2.1:3322->2233/tcp, 1.2.3.4:8899->9988/udp, 1.2.3.4:8899->9988/tcp", + "4.3.2.1:3322->2233/tcp, 1.2.3.4:8899->9988/tcp, 1.2.3.4:8899->9988/udp", }, { []types.Port{ @@ -188,6 +188,64 @@ func TestDisplayablePorts(t *testing.T) { }, "9988/udp, 4.3.2.1:3322->2233/tcp, 1.2.3.4:7766->6677/tcp", }, + { + []types.Port{ + { + PrivatePort: 80, + Type: "tcp", + }, { + PrivatePort: 1024, + Type: "tcp", + }, { + PrivatePort: 80, + Type: "udp", + }, { + PrivatePort: 1024, + Type: "udp", + }, { + IP: "1.1.1.1", + PublicPort: 80, + PrivatePort: 1024, + Type: "tcp", + }, { + IP: "1.1.1.1", + PublicPort: 80, + PrivatePort: 1024, + Type: "udp", + }, { + IP: "1.1.1.1", + PublicPort: 1024, + PrivatePort: 80, + Type: "tcp", + }, { + IP: "1.1.1.1", + PublicPort: 1024, + PrivatePort: 80, + Type: "udp", + }, { + IP: "2.1.1.1", + PublicPort: 80, + PrivatePort: 1024, + Type: "tcp", + }, { + IP: "2.1.1.1", + PublicPort: 80, + PrivatePort: 1024, + Type: "udp", + }, { + IP: "2.1.1.1", + PublicPort: 1024, + PrivatePort: 80, + Type: "tcp", + }, { + IP: "2.1.1.1", + PublicPort: 1024, + PrivatePort: 80, + Type: "udp", + }, + }, + "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", + }, } for _, port := range cases {