From 8ebdf4f75c70d516a6a48bd670736752afc532fd Mon Sep 17 00:00:00 2001 From: Martin Englund Date: Mon, 20 Feb 2017 13:37:54 -0800 Subject: [PATCH 1/2] core: add support to connect to docker-machine Signed-off-by: Martin Englund --- dockertest.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/dockertest.go b/dockertest.go index b4d7d158..1329697e 100644 --- a/dockertest.go +++ b/dockertest.go @@ -2,12 +2,13 @@ package dockertest import ( "fmt" + "os" + "runtime" + "time" + "github.com/cenk/backoff" dc "github.com/fsouza/go-dockerclient" "github.com/pkg/errors" - "time" - "runtime" - "os" ) // Pool represents a connection to the docker API and is used to create and remove docker images. @@ -40,11 +41,18 @@ func (r *Resource) GetPort(id string) string { } // NewPool creates a new pool. You can pass an empty string to use the default, which is taken from the environment -// variable DOCKER_URL or if that is not defined a sensible default for the operating system you are on. +// variable DOCKER_URL, or from docker-machine if the environment variable DOCKER_MACHINE_NAME is set, +// or if neither is defined a sensible default for the operating system you are on. func NewPool(endpoint string) (*Pool, error) { + var client *dc.Client + var err error + if endpoint == "" { if os.Getenv("DOCKER_URL") != "" { endpoint = os.Getenv("DOCKER_URL") + } else if os.Getenv("DOCKER_MACHINE_NAME") != "" { + client, err = dc.NewClientFromEnv() + goto error_check } else if runtime.GOOS == "windows" { endpoint = "http://localhost:2375" } else { @@ -52,7 +60,8 @@ func NewPool(endpoint string) (*Pool, error) { } } - client, err := dc.NewClient(endpoint) + client, err = dc.NewClient(endpoint) +error_check: if err != nil { return nil, errors.Wrap(err, "") } @@ -107,7 +116,6 @@ func (d *Pool) Run(repository, tag string, env []string) (*Resource, error) { }, nil } - // Purge removes a container and linked volumes from docker. func (d *Pool) Purge(r *Resource) error { if err := d.Client.KillContainer(dc.KillContainerOptions{ID: r.Container.ID}); err != nil { From 8ea2c2da6d6689c8d3a97f146c7cc53f26677b5f Mon Sep 17 00:00:00 2001 From: Martin Englund Date: Tue, 21 Feb 2017 20:57:44 -0800 Subject: [PATCH 2/2] core: handle everything for docker-machine within the if else Signed-off-by: Martin Englund --- dockertest.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/dockertest.go b/dockertest.go index 1329697e..9e2fe59d 100644 --- a/dockertest.go +++ b/dockertest.go @@ -44,15 +44,16 @@ func (r *Resource) GetPort(id string) string { // variable DOCKER_URL, or from docker-machine if the environment variable DOCKER_MACHINE_NAME is set, // or if neither is defined a sensible default for the operating system you are on. func NewPool(endpoint string) (*Pool, error) { - var client *dc.Client - var err error - if endpoint == "" { if os.Getenv("DOCKER_URL") != "" { endpoint = os.Getenv("DOCKER_URL") } else if os.Getenv("DOCKER_MACHINE_NAME") != "" { - client, err = dc.NewClientFromEnv() - goto error_check + client, err := dc.NewClientFromEnv() + if err != nil { + return nil, errors.Wrap(err, "") + } + + return &Pool{Client: client}, nil } else if runtime.GOOS == "windows" { endpoint = "http://localhost:2375" } else { @@ -60,8 +61,7 @@ func NewPool(endpoint string) (*Pool, error) { } } - client, err = dc.NewClient(endpoint) -error_check: + client, err := dc.NewClient(endpoint) if err != nil { return nil, errors.Wrap(err, "") }