Allow to use -H unix:// like -H tcp://

This commit is contained in:
Guillaume J. Charmes 2013-12-16 16:30:23 -08:00
Родитель 64136071c6
Коммит 20605eb310
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B33E4642CB6E3FF3
3 изменённых файлов: 38 добавлений и 12 удалений

Просмотреть файл

@ -129,7 +129,7 @@ func ValidateEnv(val string) (string, error) {
}
func ValidateHost(val string) (string, error) {
host, err := utils.ParseHost(DEFAULTHTTPHOST, DEFAULTHTTPPORT, val)
host, err := utils.ParseHost(DEFAULTHTTPHOST, DEFAULTHTTPPORT, DEFAULTUNIXSOCKET, val)
if err != nil {
return val, err
}

Просмотреть файл

@ -776,11 +776,21 @@ func GetNameserversAsCIDR(resolvConf []byte) []string {
return nameservers
}
func ParseHost(host string, port int, addr string) (string, error) {
var proto string
// FIXME: Change this not to receive default value as parameter
func ParseHost(defaultHost string, defaultPort int, defaultUnix, addr string) (string, error) {
var (
proto string
host string
port int
)
switch {
case strings.HasPrefix(addr, "unix://"):
return addr, nil
proto = "unix"
addr = strings.TrimPrefix(addr, "unix://")
if addr == "" {
addr = defaultUnix
}
case strings.HasPrefix(addr, "tcp://"):
proto = "tcp"
addr = strings.TrimPrefix(addr, "tcp://")
@ -791,19 +801,29 @@ func ParseHost(host string, port int, addr string) (string, error) {
proto = "tcp"
}
if strings.Contains(addr, ":") {
if proto != "unix" && strings.Contains(addr, ":") {
hostParts := strings.Split(addr, ":")
if len(hostParts) != 2 {
return "", fmt.Errorf("Invalid bind address format: %s", addr)
}
if hostParts[0] != "" {
host = hostParts[0]
} else {
host = defaultHost
}
if p, err := strconv.Atoi(hostParts[1]); err == nil {
if p, err := strconv.Atoi(hostParts[1]); err == nil && p != 0 {
port = p
} else {
port = defaultPort
}
} else {
host = addr
port = defaultPort
}
if proto == "unix" {
return fmt.Sprintf("%s://%s", proto, host), nil
}
return fmt.Sprintf("%s://%s:%d", proto, host, port), nil
}

Просмотреть файл

@ -299,22 +299,28 @@ func assertRAMInBytes(t *testing.T, size string, expectError bool, expectedBytes
}
func TestParseHost(t *testing.T) {
if addr, err := ParseHost("127.0.0.1", 4243, "0.0.0.0"); err != nil || addr != "tcp://0.0.0.0:4243" {
if addr, err := ParseHost("127.0.0.1", 4243, "/var/run/docker.sock", "0.0.0.0"); err != nil || addr != "tcp://0.0.0.0:4243" {
t.Errorf("0.0.0.0 -> expected tcp://0.0.0.0:4243, got %s", addr)
}
if addr, err := ParseHost("127.0.0.1", 4243, "0.0.0.1:5555"); err != nil || addr != "tcp://0.0.0.1:5555" {
if addr, err := ParseHost("127.0.0.1", 4243, "/var/run/docker.sock", "0.0.0.1:5555"); err != nil || addr != "tcp://0.0.0.1:5555" {
t.Errorf("0.0.0.1:5555 -> expected tcp://0.0.0.1:5555, got %s", addr)
}
if addr, err := ParseHost("127.0.0.1", 4243, ":6666"); err != nil || addr != "tcp://127.0.0.1:6666" {
if addr, err := ParseHost("127.0.0.1", 4243, "/var/run/docker.sock", ":6666"); err != nil || addr != "tcp://127.0.0.1:6666" {
t.Errorf(":6666 -> expected tcp://127.0.0.1:6666, got %s", addr)
}
if addr, err := ParseHost("127.0.0.1", 4243, "tcp://:7777"); err != nil || addr != "tcp://127.0.0.1:7777" {
if addr, err := ParseHost("127.0.0.1", 4243, "/var/run/docker.sock", "tcp://:7777"); err != nil || addr != "tcp://127.0.0.1:7777" {
t.Errorf("tcp://:7777 -> expected tcp://127.0.0.1:7777, got %s", addr)
}
if addr, err := ParseHost("127.0.0.1", 4243, "unix:///var/run/docker.sock"); err != nil || addr != "unix:///var/run/docker.sock" {
if addr, err := ParseHost("127.0.0.1", 4243, "/var/run/docker.sock", "unix:///var/run/docker.sock"); err != nil || addr != "unix:///var/run/docker.sock" {
t.Errorf("unix:///var/run/docker.sock -> expected unix:///var/run/docker.sock, got %s", addr)
}
if addr, err := ParseHost("127.0.0.1", 4243, "udp://127.0.0.1"); err == nil {
if addr, err := ParseHost("127.0.0.1", 4243, "/var/run/docker.sock", "unix://"); err != nil || addr != "unix:///var/run/docker.sock" {
t.Errorf("unix:///var/run/docker.sock -> expected unix:///var/run/docker.sock, got %s", addr)
}
if addr, err := ParseHost("127.0.0.1", 4243, "/var/run/docker.sock", "udp://127.0.0.1"); err == nil {
t.Errorf("udp protocol address expected error return, but err == nil. Got %s", addr)
}
if addr, err := ParseHost("127.0.0.1", 4243, "/var/run/docker.sock", "udp://127.0.0.1:4243"); err == nil {
t.Errorf("udp protocol address expected error return, but err == nil. Got %s", addr)
}
}