From b66580da75c0cd8e34e10328a46e63e16694670a Mon Sep 17 00:00:00 2001 From: Garry Hill Date: Wed, 23 Nov 2016 11:19:28 +0000 Subject: [PATCH] nsqadmin: Fix handling of IPv6 broadcast addresses Fixes #815 --- internal/clusterinfo/producer_test.go | 47 +++++++++++++++++++++++++++ internal/clusterinfo/types.go | 6 ++-- 2 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 internal/clusterinfo/producer_test.go diff --git a/internal/clusterinfo/producer_test.go b/internal/clusterinfo/producer_test.go new file mode 100644 index 000000000..99e6ca29a --- /dev/null +++ b/internal/clusterinfo/producer_test.go @@ -0,0 +1,47 @@ +package clusterinfo + +import "testing" + +func TestHostNameAddresses(t *testing.T) { + p := &Producer{ + BroadcastAddress: "host.domain.com", + TCPPort: 4150, + HTTPPort: 4151, + } + + if p.HTTPAddress() != "host.domain.com:4151" { + t.Errorf("Incorrect HTTPAddress: %s", p.HTTPAddress()) + } + if p.TCPAddress() != "host.domain.com:4150" { + t.Errorf("Incorrect TCPAddress: %s", p.TCPAddress()) + } +} + +func TestIPv4Addresses(t *testing.T) { + p := &Producer{ + BroadcastAddress: "192.168.1.17", + TCPPort: 4150, + HTTPPort: 4151, + } + + if p.HTTPAddress() != "192.168.1.17:4151" { + t.Errorf("Incorrect IPv4 HTTPAddress: %s", p.HTTPAddress()) + } + if p.TCPAddress() != "192.168.1.17:4150" { + t.Errorf("Incorrect IPv4 TCPAddress: %s", p.TCPAddress()) + } +} + +func TestIPv6Addresses(t *testing.T) { + p := &Producer{ + BroadcastAddress: "fd4a:622f:d2f2::1", + TCPPort: 4150, + HTTPPort: 4151, + } + if p.HTTPAddress() != "[fd4a:622f:d2f2::1]:4151" { + t.Errorf("Incorrect IPv6 HTTPAddress: %s", p.HTTPAddress()) + } + if p.TCPAddress() != "[fd4a:622f:d2f2::1]:4150" { + t.Errorf("Incorrect IPv6 TCPAddress: %s", p.TCPAddress()) + } +} diff --git a/internal/clusterinfo/types.go b/internal/clusterinfo/types.go index 2630eccb3..8c0c3b625 100644 --- a/internal/clusterinfo/types.go +++ b/internal/clusterinfo/types.go @@ -3,7 +3,9 @@ package clusterinfo import ( "encoding/json" "fmt" + "net" "sort" + "strconv" "strings" "time" @@ -77,11 +79,11 @@ func (p *Producer) Address() string { } func (p *Producer) HTTPAddress() string { - return fmt.Sprintf("%s:%d", p.BroadcastAddress, p.HTTPPort) + return net.JoinHostPort(p.BroadcastAddress, strconv.Itoa(p.HTTPPort)) } func (p *Producer) TCPAddress() string { - return fmt.Sprintf("%s:%d", p.BroadcastAddress, p.TCPPort) + return net.JoinHostPort(p.BroadcastAddress, strconv.Itoa(p.TCPPort)) } // IsInconsistent checks for cases where an unexpected number of nsqd connections are