From 0883bfa6d36090f1aba0a5b8107e40224242f83a Mon Sep 17 00:00:00 2001 From: Akram Ben Aissi Date: Wed, 22 Apr 2015 22:06:20 +0200 Subject: [PATCH] Fixes an issue with hosts having an IPv6 address on localhost - When 'getent hosts localhost' returns '::1' the creation of the listener fails because of the port parsing which uses ":" as a separator - Use of net.SplitHostPort() to do the job - Adding unit tests to ensure that the creation succeeds - On docker.go: adds a test on the presence the socat command which was failing silenty if not installed - Code Review 1 - Fixed typo on Expected - The UT now fails if the PortForwarder could not be created - Code Review 2 - Simplify socat error message - Changing t.Fatal to to.Error on unit tests - Code Review 3 - Removing useless uses cases in unit tests - Code Review 4 - Removing useless initiliasiation of PortForwarder - Changing error message - Code Review 5 - Simplifying TestCast struct - Adding addition test in one test case - Closing the listener - Code Review 6 - Improving unit test --- pkg/client/portforward/portforward_test.go | 34 ++++++++++++++-------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/pkg/client/portforward/portforward_test.go b/pkg/client/portforward/portforward_test.go index 00132db46d8..29f0a826c02 100644 --- a/pkg/client/portforward/portforward_test.go +++ b/pkg/client/portforward/portforward_test.go @@ -209,14 +209,16 @@ func (s *fakeUpgradeStream) Headers() http.Header { return http.Header{} } +type TestCase struct { + Hostname string + Protocol string + ShouldRaiseError bool + ExpectedListenerAddress string +} + func TestGetListener(t *testing.T) { var pf PortForwarder - testCases := []struct { - Hostname string - Protocol string - ShouldRaiseError bool - ExpectedListenerAddress string - }{ + testCases := []TestCase{ { Hostname: "localhost", Protocol: "tcp4", @@ -235,12 +237,6 @@ func TestGetListener(t *testing.T) { ShouldRaiseError: false, ExpectedListenerAddress: "::1", }, - { - Hostname: "localhost", - Protocol: "tcp6", - ShouldRaiseError: false, - ExpectedListenerAddress: "::1", - }, { Hostname: "[::1]", Protocol: "tcp4", @@ -253,6 +249,20 @@ func TestGetListener(t *testing.T) { }, } + // On some linux systems, ::1 does not resolve to localhost but to localhost6 or + // ip6-localhost. To make the test case portable, we need to do a reverse lookup on ::1 and + // trying to bind a port with the name. + names, err := net.LookupAddr("::1") + if err == nil && len(names) > 0 { + ipv6TestCase := TestCase{ + Hostname: names[0], + Protocol: "tcp6", + ShouldRaiseError: false, + ExpectedListenerAddress: "::1", + } + testCases = append(testCases, ipv6TestCase) + } + for i, testCase := range testCases { expectedListenerPort := "12345" listener, err := pf.getListener(testCase.Protocol, testCase.Hostname, &ForwardedPort{12345, 12345})