Change ParseTCPAddr to use tcp://127.0.0.0:2375 format as default consistently
Signed-off-by: Sven Dowideit <SvenDowideit@home.org.au>
This commit is contained in:
parent
0ffa6f1bb6
commit
aca9c7cc9c
2 changed files with 50 additions and 45 deletions
|
@ -12,17 +12,17 @@ import (
|
||||||
"strings"
|
"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
|
// 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)
|
addr = strings.TrimSpace(addr)
|
||||||
if addr == "" {
|
if addr == "" {
|
||||||
if runtime.GOOS != "windows" {
|
if runtime.GOOS != "windows" {
|
||||||
addr = fmt.Sprintf("unix://%s", defaultUnixAddr)
|
return fmt.Sprintf("unix://%s", defaultUnixAddr), nil
|
||||||
} else {
|
|
||||||
// Note - defaultTCPAddr already includes tcp:// prefix
|
|
||||||
addr = defaultTCPAddr
|
|
||||||
}
|
}
|
||||||
|
return defaultTCPAddr, nil
|
||||||
}
|
}
|
||||||
addrParts := strings.Split(addr, "://")
|
addrParts := strings.Split(addr, "://")
|
||||||
if len(addrParts) == 1 {
|
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
|
// ParseTCPAddr parses and validates that the specified address is a valid TCP
|
||||||
// address. It returns a formatted TCP address, either using the address parsed
|
// 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.
|
// from tryAddr, or the contents of defaultAddr if tryAddr is a blank string.
|
||||||
func ParseTCPAddr(addr string, defaultAddr string) (string, error) {
|
// tryAddr is expected to have already been Trim()'d
|
||||||
addr = strings.TrimPrefix(addr, "tcp://")
|
// 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 == "" {
|
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)
|
u, err := url.Parse("tcp://" + addr)
|
||||||
|
@ -71,16 +76,23 @@ func ParseTCPAddr(addr string, defaultAddr string) (string, error) {
|
||||||
}
|
}
|
||||||
hostParts := strings.Split(u.Host, ":")
|
hostParts := strings.Split(u.Host, ":")
|
||||||
if len(hostParts) != 2 {
|
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]
|
defaults := strings.Split(defaultAddr, ":")
|
||||||
if host == "" {
|
if len(defaults) != 3 {
|
||||||
host = defaultAddr
|
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])
|
p, err := strconv.Atoi(hostParts[1])
|
||||||
if err != nil && p == 0 {
|
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
|
return fmt.Sprintf("tcp://%s:%d%s", host, p, u.Path), nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,16 +6,17 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestParseHost(t *testing.T) {
|
func TestParseDockerDaemonHost(t *testing.T) {
|
||||||
var (
|
var (
|
||||||
defaultHTTPHost = "127.0.0.1"
|
defaultHTTPHost = "tcp://127.0.0.1:2376"
|
||||||
defaultUnix = "/var/run/docker.sock"
|
defaultUnix = "/var/run/docker.sock"
|
||||||
defaultHOST = "unix:///var/run/docker.sock"
|
defaultHOST = "unix:///var/run/docker.sock"
|
||||||
)
|
)
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
defaultHOST = defaultHTTPHost
|
||||||
|
}
|
||||||
invalids := map[string]string{
|
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",
|
||||||
"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": "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",
|
"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",
|
"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",
|
"fd": "Invalid bind address format: fd",
|
||||||
}
|
}
|
||||||
valids := map[string]string{
|
valids := map[string]string{
|
||||||
|
"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": "tcp://0.0.0.1:5555",
|
||||||
"0.0.0.1:5555/path": "tcp://0.0.0.1:5555/path",
|
"0.0.0.1:5555/path": "tcp://0.0.0.1:5555/path",
|
||||||
":6666": "tcp://127.0.0.1:6666",
|
":6666": "tcp://127.0.0.1:6666",
|
||||||
":6666/path": "tcp://127.0.0.1:6666/path",
|
":6666/path": "tcp://127.0.0.1:6666/path",
|
||||||
|
"": defaultHOST,
|
||||||
|
" ": defaultHOST,
|
||||||
|
" ": defaultHOST,
|
||||||
|
"tcp://": defaultHTTPHost,
|
||||||
"tcp://:7777": "tcp://127.0.0.1:7777",
|
"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",
|
||||||
// as there's a TrimSpace in there, we should test for it.
|
|
||||||
" 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:///run/docker.sock": "unix:///run/docker.sock",
|
||||||
"unix://": "unix:///var/run/docker.sock",
|
"unix://": "unix:///var/run/docker.sock",
|
||||||
"fd://": "fd://",
|
"fd://": "fd://",
|
||||||
"fd://something": "fd://something",
|
"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 {
|
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)
|
t.Errorf("tcp %v address expected error %v return, got %s and addr %v", invalidAddr, expectedError, err, addr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for validAddr, expectedAddr := range valids {
|
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)
|
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) {
|
func TestParseTCP(t *testing.T) {
|
||||||
var (
|
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 = "tcp://127.0.0.1:2376"
|
||||||
defaultHTTPHost = "127.0.0.1"
|
|
||||||
)
|
)
|
||||||
invalids := map[string]string{
|
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",
|
||||||
"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": "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",
|
"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": "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",
|
"udp://127.0.0.1:2375": "Invalid proto, expected tcp: udp://127.0.0.1:2375",
|
||||||
"": "Invalid proto, expected tcp: ",
|
|
||||||
}
|
}
|
||||||
valids := map[string]string{
|
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": "tcp://0.0.0.1:5555",
|
||||||
"0.0.0.1:5555/path": "tcp://0.0.0.1:5555/path",
|
"0.0.0.1:5555/path": "tcp://0.0.0.1:5555/path",
|
||||||
":6666": "tcp://127.0.0.1:6666",
|
":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" {
|
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)
|
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)
|
t.Fatalf("Expected an error, got %v", err)
|
||||||
}
|
}
|
||||||
if v, err := ParseUnixAddr("", "/var/run/docker.sock"); err != nil || v != "unix:///var/run/docker.sock" {
|
if v, err := ParseUnixAddr("", "/var/run/docker.sock"); err != nil || v != "unix:///var/run/docker.sock" {
|
||||||
|
|
Loading…
Reference in a new issue