Merge pull request #16940 from coolljt0725/default_tls_host

Make default tls host work
This commit is contained in:
Tibor Vass 2015-10-21 14:02:22 -04:00
commit bf04de9ef4
2 changed files with 19 additions and 15 deletions

View file

@ -17,9 +17,12 @@ import (
// Depending of the address specified, will use the defaultTCPAddr or defaultUnixAddr // Depending of the address specified, will use the defaultTCPAddr or defaultUnixAddr
// defaultUnixAddr must be a absolute file path (no `unix://` prefix) // defaultUnixAddr must be a absolute file path (no `unix://` prefix)
// defaultTCPAddr must be the full `tcp://host:port` form // defaultTCPAddr must be the full `tcp://host:port` form
func ParseDockerDaemonHost(defaultTCPAddr, defaultUnixAddr, addr string) (string, error) { func ParseDockerDaemonHost(defaultTCPAddr, defaultTLSHost, defaultUnixAddr, defaultAddr, addr string) (string, error) {
addr = strings.TrimSpace(addr) addr = strings.TrimSpace(addr)
if addr == "" { if addr == "" {
if defaultAddr == defaultTLSHost {
return defaultTLSHost, nil
}
if runtime.GOOS != "windows" { if runtime.GOOS != "windows" {
return fmt.Sprintf("unix://%s", defaultUnixAddr), nil return fmt.Sprintf("unix://%s", defaultUnixAddr), nil
} }

View file

@ -9,9 +9,10 @@ import (
func TestParseDockerDaemonHost(t *testing.T) { func TestParseDockerDaemonHost(t *testing.T) {
var ( var (
defaultHTTPHost = "tcp://127.0.0.1:2376" defaultHTTPHost = "tcp://localhost:2375"
defaultUnix = "/var/run/docker.sock" defaultHTTPSHost = "tcp://localhost:2376"
defaultHOST = "unix:///var/run/docker.sock" defaultUnix = "/var/run/docker.sock"
defaultHOST = "unix:///var/run/docker.sock"
) )
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
defaultHOST = defaultHTTPHost defaultHOST = defaultHTTPHost
@ -28,37 +29,37 @@ func TestParseDockerDaemonHost(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:": "tcp://0.0.0.1:2375",
"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",
"[::1]:": "tcp://[::1]:2376", "[::1]:": "tcp://[::1]:2375",
"[::1]:5555/path": "tcp://[::1]:5555/path", "[::1]:5555/path": "tcp://[::1]:5555/path",
"[0:0:0:0:0:0:0:1]:": "tcp://[0:0:0:0:0:0:0:1]:2376", "[0:0:0:0:0:0:0:1]:": "tcp://[0:0:0:0:0:0:0:1]:2375",
"[0:0:0:0:0:0:0:1]:5555/path": "tcp://[0:0:0:0:0:0:0:1]:5555/path", "[0:0:0:0:0:0:0:1]:5555/path": "tcp://[0:0:0:0:0:0:0:1]:5555/path",
":6666": "tcp://127.0.0.1:6666", ":6666": "tcp://localhost:6666",
":6666/path": "tcp://127.0.0.1:6666/path", ":6666/path": "tcp://localhost:6666/path",
"": defaultHOST, "": defaultHOST,
" ": defaultHOST, " ": defaultHOST,
" ": defaultHOST, " ": defaultHOST,
"tcp://": defaultHTTPHost, "tcp://": defaultHTTPHost,
"tcp://:7777": "tcp://127.0.0.1:7777", "tcp://:7777": "tcp://localhost:7777",
"tcp://:7777/path": "tcp://127.0.0.1:7777/path", "tcp://:7777/path": "tcp://localhost:7777/path",
" tcp://:7777/path ": "tcp://127.0.0.1:7777/path", " tcp://:7777/path ": "tcp://localhost: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",
"localhost:": "tcp://localhost:2376", "localhost:": "tcp://localhost:2375",
"localhost:5555": "tcp://localhost:5555", "localhost:5555": "tcp://localhost:5555",
"localhost:5555/path": "tcp://localhost:5555/path", "localhost:5555/path": "tcp://localhost:5555/path",
} }
for invalidAddr, expectedError := range invalids { for invalidAddr, expectedError := range invalids {
if addr, err := ParseDockerDaemonHost(defaultHTTPHost, defaultUnix, invalidAddr); err == nil || err.Error() != expectedError { if addr, err := ParseDockerDaemonHost(defaultHTTPHost, defaultHTTPSHost, 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 := ParseDockerDaemonHost(defaultHTTPHost, defaultUnix, validAddr); err != nil || addr != expectedAddr { if addr, err := ParseDockerDaemonHost(defaultHTTPHost, defaultHTTPSHost, 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)
} }
} }