Handle localhost distributed setups properly (#8577)

Fixes an issue reported by @klauspost and @vadmeste

This PR also allows users to expand their clusters
from single node XL deployment to distributed mode.
This commit is contained in:
Harshavardhana
2019-11-26 11:42:10 -08:00
committed by GitHub
parent 78eb3b78bb
commit 5d65428b29
16 changed files with 189 additions and 187 deletions

View File

@@ -81,9 +81,12 @@ func (host *Host) UnmarshalJSON(data []byte) (err error) {
// ParseHost - parses string into Host
func ParseHost(s string) (*Host, error) {
if s == "" {
return nil, errors.New("invalid argument")
}
isValidHost := func(host string) bool {
if host == "" {
return false
return true
}
if ip := net.ParseIP(host); ip != nil {

View File

@@ -137,7 +137,7 @@ func TestHostUnmarshalJSON(t *testing.T) {
{[]byte(`"12play"`), &Host{"12play", 0, false}, false},
{[]byte(`"play-minio-io"`), &Host{"play-minio-io", 0, false}, false},
{[]byte(`"play--min.io"`), &Host{"play--min.io", 0, false}, false},
{[]byte(`":9000"`), nil, true},
{[]byte(`":9000"`), &Host{"", 9000, true}, false},
{[]byte(`"[fe80::8097:76eb:b397:e067%wlp2s0]"`), &Host{"fe80::8097:76eb:b397:e067%wlp2s0", 0, false}, false},
{[]byte(`"[fe80::8097:76eb:b397:e067]:9000"`), &Host{"fe80::8097:76eb:b397:e067", 9000, true}, false},
{[]byte(`"fe80::8097:76eb:b397:e067%wlp2s0"`), nil, true},
@@ -154,20 +154,23 @@ func TestHostUnmarshalJSON(t *testing.T) {
{[]byte(`":"`), nil, true},
}
for i, testCase := range testCases {
var host Host
err := host.UnmarshalJSON(testCase.data)
expectErr := (err != nil)
for _, testCase := range testCases {
testCase := testCase
t.Run("", func(t *testing.T) {
var host Host
err := host.UnmarshalJSON(testCase.data)
expectErr := (err != nil)
if expectErr != testCase.expectErr {
t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr)
}
if !testCase.expectErr {
if !reflect.DeepEqual(&host, testCase.expectedHost) {
t.Fatalf("test %v: host: expected: %#v, got: %#v", i+1, testCase.expectedHost, host)
if expectErr != testCase.expectErr {
t.Errorf("error: expected: %v, got: %v", testCase.expectErr, expectErr)
}
}
if !testCase.expectErr {
if !reflect.DeepEqual(&host, testCase.expectedHost) {
t.Errorf("host: expected: %#v, got: %#v", testCase.expectedHost, host)
}
}
})
}
}
@@ -188,7 +191,7 @@ func TestParseHost(t *testing.T) {
{"12play", &Host{"12play", 0, false}, false},
{"play-minio-io", &Host{"play-minio-io", 0, false}, false},
{"play--min.io", &Host{"play--min.io", 0, false}, false},
{":9000", nil, true},
{":9000", &Host{"", 9000, true}, false},
{"play:", nil, true},
{"play::", nil, true},
{"play:90000", nil, true},
@@ -199,19 +202,22 @@ func TestParseHost(t *testing.T) {
{"", nil, true},
}
for i, testCase := range testCases {
host, err := ParseHost(testCase.s)
expectErr := (err != nil)
for _, testCase := range testCases {
testCase := testCase
t.Run("", func(t *testing.T) {
host, err := ParseHost(testCase.s)
expectErr := (err != nil)
if expectErr != testCase.expectErr {
t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr)
}
if !testCase.expectErr {
if !reflect.DeepEqual(host, testCase.expectedHost) {
t.Fatalf("test %v: host: expected: %#v, got: %#v", i+1, testCase.expectedHost, host)
if expectErr != testCase.expectErr {
t.Errorf("error: expected: %v, got: %v", testCase.expectErr, expectErr)
}
}
if !testCase.expectErr {
if !reflect.DeepEqual(host, testCase.expectedHost) {
t.Errorf("host: expected: %#v, got: %#v", testCase.expectedHost, host)
}
}
})
}
}

View File

@@ -31,6 +31,12 @@ func (p Port) String() string {
// ParsePort - parses string into Port
func ParsePort(s string) (p Port, err error) {
if s == "https" {
return Port(443), nil
} else if s == "http" {
return Port(80), nil
}
var i int
if i, err = strconv.Atoi(s); err != nil {
return p, errors.New("invalid port number")

View File

@@ -49,10 +49,11 @@ func TestParsePort(t *testing.T) {
{"0", Port(0), false},
{"9000", Port(9000), false},
{"65535", Port(65535), false},
{"http", Port(80), false},
{"https", Port(443), false},
{"90000", Port(0), true},
{"-10", Port(0), true},
{"", Port(0), true},
{"http", Port(0), true},
{" 1024", Port(0), true},
}

View File

@@ -126,12 +126,23 @@ func ParseURL(s string) (u *URL, err error) {
return nil, err
}
if uu.Host == "" {
if uu.Hostname() == "" {
if uu.Scheme != "" {
return nil, errors.New("scheme appears with empty host")
}
} else if _, err = ParseHost(uu.Host); err != nil {
return nil, err
} else {
portStr := uu.Port()
if portStr == "" {
switch uu.Scheme {
case "http":
portStr = "80"
case "https":
portStr = "443"
}
}
if _, err = ParseHost(net.JoinHostPort(uu.Hostname(), portStr)); err != nil {
return nil, err
}
}
// Clean path in the URL.