2016-03-26 00:10:08 +00:00
|
|
|
package testutil
|
|
|
|
|
|
|
|
import (
|
2017-10-19 23:55:10 +00:00
|
|
|
"runtime"
|
2016-03-26 00:10:08 +00:00
|
|
|
"testing"
|
2017-02-15 01:34:05 +00:00
|
|
|
|
|
|
|
docker "github.com/fsouza/go-dockerclient"
|
|
|
|
"github.com/hashicorp/nomad/testutil"
|
2016-03-26 00:10:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// DockerIsConnected checks to see if a docker daemon is available (local or remote)
|
|
|
|
func DockerIsConnected(t *testing.T) bool {
|
2017-02-15 01:34:05 +00:00
|
|
|
// We have docker on travis so we should try to test
|
2019-01-07 13:27:06 +00:00
|
|
|
if testutil.IsTravis() {
|
|
|
|
// Travis supports Docker on Linux only; MacOS setup does not support Docker
|
|
|
|
return runtime.GOOS == "linux"
|
2017-02-15 01:34:05 +00:00
|
|
|
}
|
|
|
|
|
2016-03-26 00:10:08 +00:00
|
|
|
client, err := docker.NewClientFromEnv()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creating a client doesn't actually connect, so make sure we do something
|
|
|
|
// like call Version() on it.
|
|
|
|
env, err := client.Version()
|
|
|
|
if err != nil {
|
|
|
|
t.Logf("Failed to connect to docker daemon: %s", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("Successfully connected to docker daemon running version %s", env.Get("Version"))
|
|
|
|
return true
|
|
|
|
}
|
2019-01-07 13:27:06 +00:00
|
|
|
|
|
|
|
// DockerCompatible skips tests if docker is not present
|
|
|
|
func DockerCompatible(t *testing.T) {
|
|
|
|
if !DockerIsConnected(t) {
|
|
|
|
t.Skip("Docker not connected")
|
|
|
|
}
|
|
|
|
}
|