Skip to content

Commit

Permalink
Merge pull request gravitational#464 from gravitational/dmitri/fix-ut…
Browse files Browse the repository at this point in the history
…ils-addr-edge-cases

Cover the missing edge cases to replicate the old behavior
  • Loading branch information
klizhentas authored Jun 25, 2016
2 parents a9e2e94 + 43ba205 commit d8293ac
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
5 changes: 4 additions & 1 deletion lib/utils/addr.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,11 @@ func guessHostIP(ifaces []netInterface) (ip net.IP) {
}
}
}
// did not find anything? return loopback
if ip == nil {
if len(ips) > 0 {
return ips[0]
}
// fallback to loopback
ip = net.IPv4(127, 0, 0, 1)
}
return ip
Expand Down
15 changes: 13 additions & 2 deletions lib/utils/addr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,25 @@ func (s *AddrTestSuite) TestGuessesIPAddress(c *C) {
ifaces: []netInterface{
iface{
addrs: []net.Addr{
&net.IPAddr{IP: net.ParseIP("172.192.12.1")},
&net.IPAddr{IP: net.ParseIP("52.35.21.180")},
&net.IPAddr{IP: net.ParseIP("fe80::af:6dff:fefd:150f")},
},
},
},
expected: net.ParseIP("172.192.12.1"),
expected: net.ParseIP("52.35.21.180"),
comment: "ignores IPv6",
},
{
ifaces: []netInterface{
iface{
addrs: []net.Addr{
&net.IPAddr{IP: net.ParseIP("fe80::af:6dff:fefd:150f")},
},
},
},
expected: net.ParseIP("127.0.0.1"),
comment: "falls back to loopback",
},
}

for _, testCase := range testCases {
Expand Down

0 comments on commit d8293ac

Please sign in to comment.