Change sorting method and add test to DisplayablePorts
Signed-off-by: Garrett Barboza <garrett@garrettbarboza.com>
This commit is contained in:
parent
8005dbd748
commit
c4c6d33bbc
2 changed files with 84 additions and 8 deletions
|
@ -27,12 +27,26 @@ const (
|
||||||
DefaultDockerfileName string = "Dockerfile"
|
DefaultDockerfileName string = "Dockerfile"
|
||||||
)
|
)
|
||||||
|
|
||||||
// byPrivatePort is temporary type used to sort types.Port by PrivatePort
|
// byPortInfo is a temporary type used to sort types.Port by its fields
|
||||||
type byPrivatePort []types.Port
|
type byPortInfo []types.Port
|
||||||
|
|
||||||
func (r byPrivatePort) Len() int { return len(r) }
|
func (r byPortInfo) Len() int { return len(r) }
|
||||||
func (r byPrivatePort) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
|
func (r byPortInfo) 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) 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
|
// DisplayablePorts returns formatted string representing open ports of container
|
||||||
// e.g. "0.0.0.0:80->9090/tcp, 9988/tcp"
|
// 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)
|
groupMap := make(map[string]*portGroup)
|
||||||
var result []string
|
var result []string
|
||||||
var hostMappings []string
|
var hostMappings []string
|
||||||
sort.Sort(byPrivatePort(ports))
|
var groupMapKeys []string
|
||||||
|
sort.Sort(byPortInfo(ports))
|
||||||
for _, port := range ports {
|
for _, port := range ports {
|
||||||
current := port.PrivatePort
|
current := port.PrivatePort
|
||||||
portKey := port.Type
|
portKey := port.Type
|
||||||
|
@ -60,6 +75,8 @@ func DisplayablePorts(ports []types.Port) string {
|
||||||
|
|
||||||
if group == nil {
|
if group == nil {
|
||||||
groupMap[portKey] = &portGroup{first: current, last: current}
|
groupMap[portKey] = &portGroup{first: current, last: current}
|
||||||
|
// record order that groupMap keys are created
|
||||||
|
groupMapKeys = append(groupMapKeys, portKey)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if current == (group.last + 1) {
|
if current == (group.last + 1) {
|
||||||
|
@ -70,7 +87,8 @@ func DisplayablePorts(ports []types.Port) string {
|
||||||
result = append(result, formGroup(portKey, group.first, group.last))
|
result = append(result, formGroup(portKey, group.first, group.last))
|
||||||
groupMap[portKey] = &portGroup{first: current, last: current}
|
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, formGroup(portKey, g.first, g.last))
|
||||||
}
|
}
|
||||||
result = append(result, hostMappings...)
|
result = append(result, hostMappings...)
|
||||||
|
|
|
@ -166,7 +166,7 @@ func TestDisplayablePorts(t *testing.T) {
|
||||||
Type: "tcp",
|
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{
|
[]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",
|
"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 {
|
for _, port := range cases {
|
||||||
|
|
Loading…
Reference in a new issue