Do not print nil when hostname is provided as --address (#3053)

Fixes #3018
master
Harshavardhana 8 years ago committed by GitHub
parent 5782ec3ada
commit fe56220d1a
  1. 21
      cmd/host-to-ip.go
  2. 42
      cmd/host-to-ip_test.go
  3. 53
      cmd/server-main.go
  4. 36
      cmd/server-main_test.go

@ -17,6 +17,7 @@
package cmd package cmd
import ( import (
"fmt"
"net" "net"
"sort" "sort"
) )
@ -31,12 +32,20 @@ func (n byLastOctet) Less(i, j int) bool {
return []byte(n[i].To4())[3] < []byte(n[j].To4())[3] return []byte(n[i].To4())[3] < []byte(n[j].To4())[3]
} }
// getIPsFromHosts - returns a reverse sorted list of ips based on the last octet value. // sortIPsByOctet - returns a reverse sorted list of hosts based on the last octet value.
func getIPsFromHosts(hosts []string) (ips []net.IP) { func sortIPsByOctet(ips []string) error {
for _, host := range hosts { var nips []net.IP
ips = append(ips, net.ParseIP(host)) for _, ip := range ips {
nip := net.ParseIP(ip)
if nip == nil {
return fmt.Errorf("Unable to parse invalid ip %s", ip)
}
nips = append(nips, nip)
} }
// Reverse sort ips by their last octet. // Reverse sort ips by their last octet.
sort.Sort(sort.Reverse(byLastOctet(ips))) sort.Sort(sort.Reverse(byLastOctet(nips)))
return ips for i, nip := range nips {
ips[i] = nip.String()
}
return nil
} }

@ -16,18 +16,23 @@
package cmd package cmd
import "testing" import (
"fmt"
"testing"
)
// Tests sorted list generated from hosts to ip. // Tests sorted list generated from hosts to ip.
func TestHostToIP(t *testing.T) { func TestHostToIP(t *testing.T) {
// Collection of test cases to validate last octet sorting. // Collection of test cases to validate last octet sorting.
testCases := []struct { testCases := []struct {
hosts []string ips []string
sortedHosts []string sortedIPs []string
err error
shouldPass bool
}{ }{
{ {
// List of ip addresses that need to be sorted. // List of ip addresses that need to be sorted.
[]string{ ips: []string{
"129.95.30.40", "129.95.30.40",
"5.24.69.2", "5.24.69.2",
"19.20.203.5", "19.20.203.5",
@ -37,7 +42,7 @@ func TestHostToIP(t *testing.T) {
"5.220.100.50", "5.220.100.50",
}, },
// Numerical sorting result based on the last octet. // Numerical sorting result based on the last octet.
[]string{ sortedIPs: []string{
"5.220.100.50", "5.220.100.50",
"129.95.30.40", "129.95.30.40",
"19.20.21.22", "19.20.21.22",
@ -46,15 +51,34 @@ func TestHostToIP(t *testing.T) {
"5.24.69.2", "5.24.69.2",
"127.0.0.1", "127.0.0.1",
}, },
err: nil,
shouldPass: true,
},
{
ips: []string{
"localhost",
},
sortedIPs: []string{},
err: fmt.Errorf("Unable to parse invalid ip localhost"),
shouldPass: false,
}, },
} }
// Tests the correct sorting behavior of getIPsFromHosts. // Tests the correct sorting behavior of getIPsFromHosts.
for j, testCase := range testCases { for j, testCase := range testCases {
ips := getIPsFromHosts(testCase.hosts) err := sortIPsByOctet(testCase.ips)
for i, ip := range ips { if !testCase.shouldPass && testCase.err.Error() != err.Error() {
if ip.String() != testCase.sortedHosts[i] { t.Fatalf("Test %d: Expected error %s, got %s", j+1, testCase.err, err)
t.Fatalf("Test %d expected to pass but failed. Wanted ip %s, but got %s", j+1, testCase.sortedHosts[i], ip.String()) }
if testCase.shouldPass && err != nil {
t.Fatalf("Test %d: Expected error %s", j+1, err)
}
if testCase.shouldPass {
for i, ip := range testCase.ips {
if ip == testCase.sortedIPs[i] {
continue
}
t.Errorf("Test %d expected to pass but failed. Wanted ip %s, but got %s", j+1, testCase.sortedIPs[i], ip)
} }
} }
} }

@ -215,43 +215,52 @@ func parseStorageEndPoints(eps []string, defaultPort int) (endpoints []storageEn
} }
// getListenIPs - gets all the ips to listen on. // getListenIPs - gets all the ips to listen on.
func getListenIPs(httpServerConf *http.Server) (hosts []string, port string) { func getListenIPs(httpServerConf *http.Server) (hosts []string, port string, err error) {
host, port, err := net.SplitHostPort(httpServerConf.Addr) var host string
fatalIf(err, "Unable to parse host address.", httpServerConf.Addr) host, port, err = net.SplitHostPort(httpServerConf.Addr)
if err != nil {
if host != "" { return nil, port, fmt.Errorf("Unable to parse host address %s", err)
hosts = append(hosts, host)
return hosts, port
} }
addrs, err := net.InterfaceAddrs() if host == "" {
fatalIf(err, "Unable to determine network interface address.") var addrs []net.Addr
for _, addr := range addrs { addrs, err = net.InterfaceAddrs()
if addr.Network() == "ip+net" { if err != nil {
host := strings.Split(addr.String(), "/")[0] return nil, port, fmt.Errorf("Unable to determine network interface address. %s", err)
if ip := net.ParseIP(host); ip.To4() != nil { }
hosts = append(hosts, host) for _, addr := range addrs {
if addr.Network() == "ip+net" {
hostname := strings.Split(addr.String(), "/")[0]
if ip := net.ParseIP(hostname); ip.To4() != nil {
hosts = append(hosts, hostname)
}
} }
} }
} err = sortIPsByOctet(hosts)
return hosts, port if err != nil {
return nil, port, fmt.Errorf("Unable reverse sorted ips from hosts %s", err)
}
return hosts, port, nil
} // if host != "" {
// Proceed to append itself, since user requested a specific endpoint.
hosts = append(hosts, host)
return hosts, port, nil
} }
// Finalizes the endpoints based on the host list and port. // Finalizes the endpoints based on the host list and port.
func finalizeEndpoints(tls bool, apiServer *http.Server) (endPoints []string) { func finalizeEndpoints(tls bool, apiServer *http.Server) (endPoints []string) {
// Get list of listen ips and port.
hosts, port := getListenIPs(apiServer)
// Verify current scheme. // Verify current scheme.
scheme := "http" scheme := "http"
if tls { if tls {
scheme = "https" scheme = "https"
} }
ips := getIPsFromHosts(hosts) // Get list of listen ips and port.
hosts, port, err := getListenIPs(apiServer)
fatalIf(err, "Unable to get list of ips to listen on")
// Construct proper endpoints. // Construct proper endpoints.
for _, ip := range ips { for _, host := range hosts {
endPoints = append(endPoints, fmt.Sprintf("%s://%s:%s", scheme, ip.String(), port)) endPoints = append(endPoints, fmt.Sprintf("%s://%s:%s", scheme, host, port))
} }
// Success. // Success.

@ -28,15 +28,39 @@ import (
func TestGetListenIPs(t *testing.T) { func TestGetListenIPs(t *testing.T) {
testCases := []struct { testCases := []struct {
addr string addr string
port string port string
shouldPass bool
}{ }{
{"localhost", ":80"}, {"localhost", "9000", true},
{"", ":80"}, {"", "9000", true},
{"", "", false},
} }
for _, test := range testCases { for _, test := range testCases {
ts := &http.Server{Addr: test.addr + test.port} var addr string
getListenIPs(ts) // Please keep this we need to do this because
// of odd https://play.golang.org/p/4dMPtM6Wdd
// implementation issue.
if test.port != "" {
addr = test.addr + ":" + test.port
}
hosts, port, err := getListenIPs(&http.Server{
Addr: addr,
})
if !test.shouldPass && err == nil {
t.Fatalf("Test should fail but succeeded %s", err)
}
if test.shouldPass && err != nil {
t.Fatalf("Test should succeed but failed %s", err)
}
if test.shouldPass {
if port != test.port {
t.Errorf("Test expected %s, got %s", test.port, port)
}
if len(hosts) == 0 {
t.Errorf("Test unexpected value hosts cannot be empty %#v", test)
}
}
} }
} }

Loading…
Cancel
Save