Merge pull request #15927 from SvenDowideit/clean-up-parsehost

Change ParseTCPAddr to use tcp://127.0.0.0:2375 format as default consistently
This commit is contained in:
Doug Davis 2015-09-01 08:25:59 -04:00
commit 963f2ee064
2 changed files with 50 additions and 45 deletions

View file

@ -12,17 +12,17 @@ import (
"strings"
)
// ParseHost parses the specified address and returns an address that will be used as the host.
// ParseDockerDaemonHost parses the specified address and returns an address that will be used as the host.
// Depending of the address specified, will use the defaultTCPAddr or defaultUnixAddr
func ParseHost(defaultTCPAddr, defaultUnixAddr, addr string) (string, error) {
// defaultUnixAddr must be a absolute file path (no `unix://` prefix)
// defaultTCPAddr must be the full `tcp://host:port` form
func ParseDockerDaemonHost(defaultTCPAddr, defaultUnixAddr, addr string) (string, error) {
addr = strings.TrimSpace(addr)
if addr == "" {
if runtime.GOOS != "windows" {
addr = fmt.Sprintf("unix://%s", defaultUnixAddr)
} else {
// Note - defaultTCPAddr already includes tcp:// prefix
addr = defaultTCPAddr
return fmt.Sprintf("unix://%s", defaultUnixAddr), nil
}
return defaultTCPAddr, nil
}
addrParts := strings.Split(addr, "://")
if len(addrParts) == 1 {
@ -58,11 +58,16 @@ func ParseUnixAddr(addr string, defaultAddr string) (string, error) {
// ParseTCPAddr parses and validates that the specified address is a valid TCP
// address. It returns a formatted TCP address, either using the address parsed
// from addr, or the contents of defaultAddr if addr is a blank string.
func ParseTCPAddr(addr string, defaultAddr string) (string, error) {
addr = strings.TrimPrefix(addr, "tcp://")
// from tryAddr, or the contents of defaultAddr if tryAddr is a blank string.
// tryAddr is expected to have already been Trim()'d
// defaultAddr must be in the full `tcp://host:port` form
func ParseTCPAddr(tryAddr string, defaultAddr string) (string, error) {
if tryAddr == "" || tryAddr == "tcp://" {
return defaultAddr, nil
}
addr := strings.TrimPrefix(tryAddr, "tcp://")
if strings.Contains(addr, "://") || addr == "" {
return "", fmt.Errorf("Invalid proto, expected tcp: %s", addr)
return "", fmt.Errorf("Invalid proto, expected tcp: %s", tryAddr)
}
u, err := url.Parse("tcp://" + addr)
@ -71,16 +76,23 @@ func ParseTCPAddr(addr string, defaultAddr string) (string, error) {
}
hostParts := strings.Split(u.Host, ":")
if len(hostParts) != 2 {
return "", fmt.Errorf("Invalid bind address format: %s", addr)
return "", fmt.Errorf("Invalid bind address format: %s", tryAddr)
}
host := hostParts[0]
if host == "" {
host = defaultAddr
defaults := strings.Split(defaultAddr, ":")
if len(defaults) != 3 {
return "", fmt.Errorf("Invalid defaults address format: %s", defaultAddr)
}
host := hostParts[0]
if host == "" {
host = strings.TrimPrefix(defaults[1], "//")
}
if hostParts[1] == "" {
hostParts[1] = defaults[2]
}
p, err := strconv.Atoi(hostParts[1])
if err != nil && p == 0 {
return "", fmt.Errorf("Invalid bind address format: %s", addr)
return "", fmt.Errorf("Invalid bind address format: %s", tryAddr)
}
return fmt.Sprintf("tcp://%s:%d%s", host, p, u.Path), nil
}

View file

@ -6,16 +6,17 @@ import (
"testing"
)
func TestParseHost(t *testing.T) {
func TestParseDockerDaemonHost(t *testing.T) {
var (
defaultHTTPHost = "127.0.0.1"
defaultHTTPHost = "tcp://127.0.0.1:2376"
defaultUnix = "/var/run/docker.sock"
defaultHOST = "unix:///var/run/docker.sock"
)
if runtime.GOOS == "windows" {
defaultHOST = defaultHTTPHost
}
invalids := map[string]string{
"0.0.0.0": "Invalid bind address format: 0.0.0.0",
"0.0.0.0:": "Invalid bind address format: 0.0.0.0:",
"tcp://": "Invalid proto, expected tcp: ",
"tcp:a.b.c.d": "Invalid bind address format: tcp:a.b.c.d",
"tcp:a.b.c.d/path": "Invalid bind address format: tcp:a.b.c.d/path",
"udp://127.0.0.1": "Invalid bind address format: udp://127.0.0.1",
@ -26,37 +27,30 @@ func TestParseHost(t *testing.T) {
"fd": "Invalid bind address format: fd",
}
valids := map[string]string{
"0.0.0.1:5555": "tcp://0.0.0.1:5555",
"0.0.0.1:5555/path": "tcp://0.0.0.1:5555/path",
":6666": "tcp://127.0.0.1:6666",
":6666/path": "tcp://127.0.0.1:6666/path",
"tcp://:7777": "tcp://127.0.0.1:7777",
"tcp://:7777/path": "tcp://127.0.0.1:7777/path",
// as there's a TrimSpace in there, we should test for it.
"0.0.0.1:": "tcp://0.0.0.1:2376",
"0.0.0.1:5555": "tcp://0.0.0.1:5555",
"0.0.0.1:5555/path": "tcp://0.0.0.1:5555/path",
":6666": "tcp://127.0.0.1:6666",
":6666/path": "tcp://127.0.0.1:6666/path",
"": defaultHOST,
" ": defaultHOST,
" ": defaultHOST,
"tcp://": defaultHTTPHost,
"tcp://:7777": "tcp://127.0.0.1:7777",
"tcp://:7777/path": "tcp://127.0.0.1:7777/path",
" tcp://:7777/path ": "tcp://127.0.0.1:7777/path",
"unix:///run/docker.sock": "unix:///run/docker.sock",
"unix://": "unix:///var/run/docker.sock",
"fd://": "fd://",
"fd://something": "fd://something",
}
if runtime.GOOS == "windows" {
defaultHOST = "Invalid bind address format: 127.0.0.1"
// SVEN: an example of the conflicted defaultHTTPHost
invalids[""] = defaultHOST
invalids[" "] = defaultHOST
invalids[" "] = defaultHOST
} else {
valids[""] = defaultHOST
valids[" "] = defaultHOST
valids[" "] = defaultHOST
}
for invalidAddr, expectedError := range invalids {
if addr, err := ParseHost(defaultHTTPHost, defaultUnix, invalidAddr); err == nil || err.Error() != expectedError {
if addr, err := ParseDockerDaemonHost(defaultHTTPHost, defaultUnix, invalidAddr); err == nil || err.Error() != expectedError {
t.Errorf("tcp %v address expected error %v return, got %s and addr %v", invalidAddr, expectedError, err, addr)
}
}
for validAddr, expectedAddr := range valids {
if addr, err := ParseHost(defaultHTTPHost, defaultUnix, validAddr); err != nil || addr != expectedAddr {
if addr, err := ParseDockerDaemonHost(defaultHTTPHost, defaultUnix, validAddr); err != nil || addr != expectedAddr {
t.Errorf("%v -> expected %v, got (%v) addr (%v)", validAddr, expectedAddr, err, addr)
}
}
@ -64,20 +58,19 @@ func TestParseHost(t *testing.T) {
func TestParseTCP(t *testing.T) {
var (
//SVEN if this is set to tcp://127.0.0.1, then we end up with results like 'tcp://tcp://127.0.0.1:6666'
defaultHTTPHost = "127.0.0.1"
defaultHTTPHost = "tcp://127.0.0.1:2376"
)
invalids := map[string]string{
"0.0.0.0": "Invalid bind address format: 0.0.0.0",
"0.0.0.0:": "Invalid bind address format: 0.0.0.0:",
"tcp://": "Invalid proto, expected tcp: ",
"tcp:a.b.c.d": "Invalid bind address format: tcp:a.b.c.d",
"tcp:a.b.c.d/path": "Invalid bind address format: tcp:a.b.c.d/path",
"udp://127.0.0.1": "Invalid proto, expected tcp: udp://127.0.0.1",
"udp://127.0.0.1:2375": "Invalid proto, expected tcp: udp://127.0.0.1:2375",
"": "Invalid proto, expected tcp: ",
}
valids := map[string]string{
"": defaultHTTPHost,
"tcp://": defaultHTTPHost,
"0.0.0.1:": "tcp://0.0.0.1:2376",
"0.0.0.1:5555": "tcp://0.0.0.1:5555",
"0.0.0.1:5555/path": "tcp://0.0.0.1:5555/path",
":6666": "tcp://127.0.0.1:6666",
@ -101,7 +94,7 @@ func TestParseInvalidUnixAddrInvalid(t *testing.T) {
if _, err := ParseUnixAddr("tcp://127.0.0.1", "unix:///var/run/docker.sock"); err == nil || err.Error() != "Invalid proto, expected unix: tcp://127.0.0.1" {
t.Fatalf("Expected an error, got %v", err)
}
if _, err := ParseUnixAddr("unix://tcp://127.0.0.1", "unix:///var/run/docker.sock"); err == nil || err.Error() != "Invalid proto, expected unix: tcp://127.0.0.1" {
if _, err := ParseUnixAddr("unix://tcp://127.0.0.1", "/var/run/docker.sock"); err == nil || err.Error() != "Invalid proto, expected unix: tcp://127.0.0.1" {
t.Fatalf("Expected an error, got %v", err)
}
if v, err := ParseUnixAddr("", "/var/run/docker.sock"); err != nil || v != "unix:///var/run/docker.sock" {