cli/formatter: fix unbracketed IPv6 addrs

Commit 964155cd 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>
This commit is contained in:
Albin Kerouanton 2024-09-23 12:16:58 +02:00
parent d47c36debb
commit 3e271461e6
2 changed files with 61 additions and 2 deletions

View File

@ -375,7 +375,7 @@ func formGroup(key string, start, last uint16) string {
group = fmt.Sprintf("%s-%d", group, last) group = fmt.Sprintf("%s-%d", group, last)
} }
if ip != "" { if ip != "" {
group = fmt.Sprintf("%s:%s->%s", ip, group, group) group = fmt.Sprintf("%s->%s", net.JoinHostPort(ip, group), group)
} }
return group + "/" + groupType return group + "/" + groupType
} }

View File

@ -589,6 +589,17 @@ func TestDisplayablePorts(t *testing.T) {
}, },
expected: "4.3.2.1:8899->9988/tcp", 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{ ports: []container.Port{
{ {
@ -600,6 +611,17 @@ func TestDisplayablePorts(t *testing.T) {
}, },
expected: "4.3.2.1:9988->9988/tcp", 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{ ports: []container.Port{
{ {
@ -628,6 +650,22 @@ func TestDisplayablePorts(t *testing.T) {
}, },
expected: "1.2.3.4:9998-9999->9998-9999/udp", 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{ ports: []container.Port{
{ {
@ -644,6 +682,22 @@ func TestDisplayablePorts(t *testing.T) {
}, },
expected: "1.2.3.4:8887->9998/udp, 1.2.3.4:8888->9999/udp", 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{ ports: []container.Port{
{ {
@ -688,9 +742,14 @@ func TestDisplayablePorts(t *testing.T) {
PrivatePort: 2233, PrivatePort: 2233,
PublicPort: 3322, PublicPort: 3322,
Type: "tcp", 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{ ports: []container.Port{