From 3e271461e675d3f7a7814ed2bbcef86ccd159ab3 Mon Sep 17 00:00:00 2001 From: Albin Kerouanton Date: Mon, 23 Sep 2024 12:16:58 +0200 Subject: [PATCH] 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 --- cli/command/formatter/container.go | 2 +- cli/command/formatter/container_test.go | 61 ++++++++++++++++++++++++- 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/cli/command/formatter/container.go b/cli/command/formatter/container.go index 203e437c6997..a62dbd822a3b 100644 --- a/cli/command/formatter/container.go +++ b/cli/command/formatter/container.go @@ -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 } diff --git a/cli/command/formatter/container_test.go b/cli/command/formatter/container_test.go index ae3f7317ef7c..3c8552f6663f 100644 --- a/cli/command/formatter/container_test.go +++ b/cli/command/formatter/container_test.go @@ -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{ { @@ -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{ { @@ -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{ { @@ -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{ { @@ -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{