Make datastore tests pass with remote Docker containers (#587)

* Make datastore tests pass with remote Docker containers
* Make tests consume DOCKER_HOST IP address as bind host while constucting database URI.

 This fix makes datastore tests pass against
 remote Docker (with host IP different from 127.0.0.1)

Fixes: #586

* Make datastore tests pass on Go1.7.1
This commit is contained in:
Denis Makogon
2017-03-14 15:32:50 +02:00
committed by Seif Lotfy سيف لطفي
parent c38aae4bfe
commit 23360d45f3
3 changed files with 35 additions and 9 deletions

View File

@@ -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()