From 03c520798e37b694d2e339d3919d202513ca471d Mon Sep 17 00:00:00 2001 From: Cameron Sparr Date: Mon, 3 Aug 2015 17:16:02 -0600 Subject: [PATCH] Fix GetLocalHost testutil function for mac users (boot2docker) --- testutil/testutil.go | 19 +++++++++++++++++-- testutil/testutil_test.go | 8 ++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/testutil/testutil.go b/testutil/testutil.go index 71c3ce0c2aaec..8735e882dbba3 100644 --- a/testutil/testutil.go +++ b/testutil/testutil.go @@ -1,12 +1,27 @@ package testutil -import "os" +import ( + "net" + "net/url" + "os" +) var localhost = "localhost" func GetLocalHost() string { 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 } diff --git a/testutil/testutil_test.go b/testutil/testutil_test.go index 39f548c0f645a..52a807514c94d 100644 --- a/testutil/testutil_test.go +++ b/testutil/testutil_test.go @@ -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")) } + 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")) + } + }