Merge pull request #7187 from akram/fix_port_forward_listner_parsing_ipv4_ipv6_2

Fixes port_forward unit tests which fails on machine which ::1 does not resolve to localhost
pull/6/head
Fabio Yeon 2015-04-23 09:49:46 -07:00
commit eb11d78b58
1 changed files with 22 additions and 12 deletions

View File

@ -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})