Fix GetLocalHost testutil function for mac users (boot2docker)
This commit is contained in:
@@ -1,12 +1,27 @@
|
|||||||
package testutil
|
package testutil
|
||||||
|
|
||||||
import "os"
|
import (
|
||||||
|
"net"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
var localhost = "localhost"
|
var localhost = "localhost"
|
||||||
|
|
||||||
func GetLocalHost() string {
|
func GetLocalHost() string {
|
||||||
if dockerHostVar := os.Getenv("DOCKER_HOST"); dockerHostVar != "" {
|
if dockerHostVar := os.Getenv("DOCKER_HOST"); dockerHostVar != "" {
|
||||||
return dockerHostVar
|
u, err := url.Parse(dockerHostVar)
|
||||||
|
if err != nil {
|
||||||
|
return dockerHostVar
|
||||||
|
}
|
||||||
|
|
||||||
|
// split out the ip addr from the port
|
||||||
|
host, _, err := net.SplitHostPort(u.Host)
|
||||||
|
if err != nil {
|
||||||
|
return dockerHostVar
|
||||||
|
}
|
||||||
|
|
||||||
|
return host
|
||||||
}
|
}
|
||||||
return localhost
|
return localhost
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,4 +23,12 @@ func TestDockerHost(t *testing.T) {
|
|||||||
t.Fatalf("Host should take DOCKER_HOST value when set. Current value is [%s] and DOCKER_HOST is [%s]", host, os.Getenv("DOCKER_HOST"))
|
t.Fatalf("Host should take DOCKER_HOST value when set. Current value is [%s] and DOCKER_HOST is [%s]", host, os.Getenv("DOCKER_HOST"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
os.Setenv("DOCKER_HOST", "tcp://1.1.1.1:8080")
|
||||||
|
|
||||||
|
host = GetLocalHost()
|
||||||
|
|
||||||
|
if host != "1.1.1.1" {
|
||||||
|
t.Fatalf("Host should take DOCKER_HOST value when set. Current value is [%s] and DOCKER_HOST is [%s]", host, os.Getenv("DOCKER_HOST"))
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user