Skip to content

Commit

Permalink
Fix GetLocalHost testutil function for mac users (boot2docker)
Browse files Browse the repository at this point in the history
  • Loading branch information
sparrc committed Aug 3, 2015
1 parent a4d0c47 commit 25a1f5d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
19 changes: 17 additions & 2 deletions testutil/testutil.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
package testutil

import "os"
import (
"os"
"net"
"net/url"
)

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
}
8 changes: 8 additions & 0 deletions testutil/testutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}

}

0 comments on commit 25a1f5d

Please sign in to comment.