diff --git a/api/datastore/internal/datastoretest/test.go b/api/datastore/internal/datastoretest/test.go index 335c261a2..799098da4 100644 --- a/api/datastore/internal/datastoretest/test.go +++ b/api/datastore/internal/datastoretest/test.go @@ -12,6 +12,9 @@ import ( "github.com/gin-gonic/gin" "reflect" "net/http" + "os" + "net/url" + "strings" ) func setLogBuffer() *bytes.Buffer { @@ -24,6 +27,28 @@ func setLogBuffer() *bytes.Buffer { return &buf } +// workaround for parts.Hostname() that doesn't work on Go1.7.1 +// TODO(denismakogon): remove this after switching to Go 1.8 +func stripPort(hostport string) string { + colon := strings.IndexByte(hostport, ':') + if colon == -1 { + return hostport + } + if i := strings.IndexByte(hostport, ']'); i != -1 { + return strings.TrimPrefix(hostport[:i], "[") + } + return hostport[:colon] +} + +func GetContainerHostIP() string { + dockerHost := os.Getenv("DOCKER_HOST") + if dockerHost == "" { + return "127.0.0.1" + } + parts, _ := url.Parse(dockerHost) + return stripPort(parts.Host) +} + func Test(t *testing.T, ds models.Datastore) { buf := setLogBuffer() diff --git a/api/datastore/postgres/postgres_test.go b/api/datastore/postgres/postgres_test.go index 606cd1e19..e2f2bef19 100644 --- a/api/datastore/postgres/postgres_test.go +++ b/api/datastore/postgres/postgres_test.go @@ -12,7 +12,7 @@ import ( "github.com/iron-io/functions/api/datastore/internal/datastoretest" ) -const tmpPostgres = "postgres://postgres@127.0.0.1:15432/funcs?sslmode=disable" +const tmpPostgres = "postgres://postgres@%v:15432/funcs?sslmode=disable" func preparePostgresTest(logf, fatalf func(string, ...interface{})) (func(), func()) { fmt.Println("initializing postgres for test") @@ -21,7 +21,8 @@ func preparePostgresTest(logf, fatalf func(string, ...interface{})) (func(), fun wait := 1 * time.Second for { - db, err := sql.Open("postgres", "postgres://postgres@127.0.0.1:15432?sslmode=disable") + db, err := sql.Open("postgres", fmt.Sprintf("postgres://postgres@%v:15432?sslmode=disable", + datastoretest.GetContainerHostIP())) if err != nil { fmt.Println("failed to connect to postgres:", err) fmt.Println("retrying in:", wait) @@ -49,7 +50,7 @@ func preparePostgresTest(logf, fatalf func(string, ...interface{})) (func(), fun } fmt.Println("postgres for test ready") return func() { - db, err := sql.Open("postgres", tmpPostgres) + db, err := sql.Open("postgres", fmt.Sprintf(tmpPostgres, datastoretest.GetContainerHostIP())) if err != nil { fatalf("failed to connect for truncation: %s\n", err) } @@ -69,13 +70,13 @@ func TestDatastore(t *testing.T) { _, close := preparePostgresTest(t.Logf, t.Fatalf) defer close() - u, err := url.Parse(tmpPostgres) + u, err := url.Parse(fmt.Sprintf(tmpPostgres, datastoretest.GetContainerHostIP())) if err != nil { - t.Fatalf("failed to parse url:", err) + t.Fatalf("failed to parse url: %v", err) } ds, err := New(u) if err != nil { - t.Fatalf("failed to create postgres datastore:", err) + t.Fatalf("failed to create postgres datastore: %v", err) } datastoretest.Test(t, ds) diff --git a/api/datastore/redis/redis_test.go b/api/datastore/redis/redis_test.go index 6d27d9c99..faaab87a2 100644 --- a/api/datastore/redis/redis_test.go +++ b/api/datastore/redis/redis_test.go @@ -13,7 +13,7 @@ import ( "github.com/iron-io/functions/api/datastore/internal/datastoretest" ) -const tmpRedis = "redis://127.0.0.1:6301/" +const tmpRedis = "redis://%v:6301/" func prepareRedisTest(logf, fatalf func(string, ...interface{})) (func(), func()) { fmt.Println("initializing redis for test") @@ -22,7 +22,7 @@ func prepareRedisTest(logf, fatalf func(string, ...interface{})) (func(), func() timeout := time.After(20 * time.Second) for { - c, err := redis.DialURL(tmpRedis) + c, err := redis.DialURL(fmt.Sprintf(tmpRedis, datastoretest.GetContainerHostIP())) if err == nil { _, err = c.Do("PING") c.Close() @@ -49,7 +49,7 @@ func TestDatastore(t *testing.T) { _, close := prepareRedisTest(t.Logf, t.Fatalf) defer close() - u, err := url.Parse(tmpRedis) + u, err := url.Parse(fmt.Sprintf(tmpRedis, datastoretest.GetContainerHostIP())) if err != nil { t.Fatal("failed to parse url: ", err) }