Skip to content

Commit

Permalink
cli/formatter: fix unbracketed IPv6 addrs
Browse files Browse the repository at this point in the history
Commit 964155c tried to enclose all IPv6 addresses within brackets but
missed some cases. This commit fixes that, and adds a few test cases.

Signed-off-by: Albin Kerouanton <albinker@gmail.com>
  • Loading branch information
akerouanton committed Sep 23, 2024
1 parent d47c36d commit 3e27146
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cli/command/formatter/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ func formGroup(key string, start, last uint16) string {
group = fmt.Sprintf("%s-%d", group, last)
}
if ip != "" {
group = fmt.Sprintf("%s:%s->%s", ip, group, group)
group = fmt.Sprintf("%s->%s", net.JoinHostPort(ip, group), group)
}
return group + "/" + groupType
}
Expand Down
61 changes: 60 additions & 1 deletion cli/command/formatter/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,17 @@ func TestDisplayablePorts(t *testing.T) {
},
expected: "4.3.2.1:8899->9988/tcp",
},
{
ports: []container.Port{
{
IP: "::1",
PrivatePort: 9988,
PublicPort: 8899,
Type: "tcp",
},
},
expected: "[::1]:8899->9988/tcp",
},
{
ports: []container.Port{
{
Expand All @@ -600,6 +611,17 @@ func TestDisplayablePorts(t *testing.T) {
},
expected: "4.3.2.1:9988->9988/tcp",
},
{
ports: []container.Port{
{
IP: "::1",
PrivatePort: 9988,
PublicPort: 9988,
Type: "tcp",
},
},
expected: "[::1]:9988->9988/tcp",
},
{
ports: []container.Port{
{
Expand Down Expand Up @@ -628,6 +650,22 @@ func TestDisplayablePorts(t *testing.T) {
},
expected: "1.2.3.4:9998-9999->9998-9999/udp",
},
{
ports: []container.Port{
{
IP: "::1",
PublicPort: 9998,
PrivatePort: 9998,
Type: "udp",
}, {
IP: "::1",
PublicPort: 9999,
PrivatePort: 9999,
Type: "udp",
},
},
expected: "[::1]:9998-9999->9998-9999/udp",
},
{
ports: []container.Port{
{
Expand All @@ -644,6 +682,22 @@ func TestDisplayablePorts(t *testing.T) {
},
expected: "1.2.3.4:8887->9998/udp, 1.2.3.4:8888->9999/udp",
},
{
ports: []container.Port{
{
IP: "::1",
PublicPort: 8887,
PrivatePort: 9998,
Type: "udp",
}, {
IP: "::1",
PublicPort: 8888,
PrivatePort: 9999,
Type: "udp",
},
},
expected: "[::1]:8887->9998/udp, [::1]:8888->9999/udp",
},
{
ports: []container.Port{
{
Expand Down Expand Up @@ -688,9 +742,14 @@ func TestDisplayablePorts(t *testing.T) {
PrivatePort: 2233,
PublicPort: 3322,
Type: "tcp",
}, {
IP: "::1",
PrivatePort: 2233,
PublicPort: 3322,
Type: "tcp",
},
},
expected: "4.3.2.1:3322->2233/tcp, 1.2.3.4:8899->9988/tcp, 1.2.3.4:8899->9988/udp",
expected: "4.3.2.1:3322->2233/tcp, [::1]:3322->2233/tcp, 1.2.3.4:8899->9988/tcp, 1.2.3.4:8899->9988/udp",
},
{
ports: []container.Port{
Expand Down

0 comments on commit 3e27146

Please sign in to comment.