2017-09-01 15:30:33 -04:00
|
|
|
package environment
|
|
|
|
|
|
|
|
import (
|
2019-07-30 13:10:03 -04:00
|
|
|
"fmt"
|
2017-09-01 15:30:33 -04:00
|
|
|
"os"
|
2018-05-17 07:11:59 -04:00
|
|
|
"strings"
|
2018-06-18 08:09:23 -04:00
|
|
|
"testing"
|
2017-09-01 15:30:33 -04:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/icmd"
|
|
|
|
"gotest.tools/v3/poll"
|
|
|
|
"gotest.tools/v3/skip"
|
2017-09-01 15:30:33 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Setup a new environment
|
|
|
|
func Setup() error {
|
|
|
|
dockerHost := os.Getenv("TEST_DOCKER_HOST")
|
|
|
|
if dockerHost == "" {
|
|
|
|
return errors.New("$TEST_DOCKER_HOST must be set")
|
|
|
|
}
|
2018-05-17 07:11:59 -04:00
|
|
|
if err := os.Setenv("DOCKER_HOST", dockerHost); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if dockerCertPath := os.Getenv("TEST_DOCKER_CERT_PATH"); dockerCertPath != "" {
|
|
|
|
if err := os.Setenv("DOCKER_CERT_PATH", dockerCertPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.Setenv("DOCKER_TLS_VERIFY", "1"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if val := boolFromString(os.Getenv("TEST_REMOTE_DAEMON")); val {
|
|
|
|
if err := os.Setenv("REMOTE_DAEMON", "1"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if val := boolFromString(os.Getenv("TEST_SKIP_PLUGIN_TESTS")); val {
|
|
|
|
if err := os.Setenv("SKIP_PLUGIN_TESTS", "1"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoteDaemon returns true if running against a remote daemon
|
|
|
|
func RemoteDaemon() bool {
|
|
|
|
return os.Getenv("REMOTE_DAEMON") != ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// SkipPluginTests returns if plugin tests should be skipped
|
|
|
|
func SkipPluginTests() bool {
|
|
|
|
return os.Getenv("SKIP_PLUGIN_TESTS") != ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// boolFromString determines boolean value from string
|
|
|
|
func boolFromString(val string) bool {
|
|
|
|
switch strings.ToLower(val) {
|
|
|
|
case "true", "1":
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
2017-09-01 15:30:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultPollSettings used with gotestyourself/poll
|
|
|
|
var DefaultPollSettings = poll.WithDelay(100 * time.Millisecond)
|
2018-06-18 08:09:23 -04:00
|
|
|
|
|
|
|
// SkipIfNotExperimentalDaemon returns whether the test docker daemon is in experimental mode
|
|
|
|
func SkipIfNotExperimentalDaemon(t *testing.T) {
|
|
|
|
t.Helper()
|
2018-09-06 01:38:01 -04:00
|
|
|
result := icmd.RunCmd(icmd.Command("docker", "info", "--format", "{{.ExperimentalBuild}}"))
|
|
|
|
result.Assert(t, icmd.Expected{Err: icmd.None})
|
|
|
|
experimentalBuild := strings.TrimSpace(result.Stdout()) == "true"
|
|
|
|
skip.If(t, !experimentalBuild, "running against a non-experimental daemon")
|
2018-06-18 08:09:23 -04:00
|
|
|
}
|
2019-07-30 13:10:03 -04:00
|
|
|
|
|
|
|
// SkipIfDaemonNotLinux skips the test unless the running docker daemon is on Linux
|
|
|
|
func SkipIfDaemonNotLinux(t *testing.T) {
|
|
|
|
t.Helper()
|
|
|
|
result := icmd.RunCmd(icmd.Command("docker", "info", "--format", "{{.OSType}}"))
|
|
|
|
result.Assert(t, icmd.Expected{Err: icmd.None})
|
|
|
|
isLinux := strings.TrimSpace(result.Stdout()) == "linux"
|
|
|
|
skip.If(t, !isLinux, "running against a Linux daemon")
|
|
|
|
}
|
|
|
|
|
|
|
|
// SkipIfCgroupNamespacesNotSupported skips the test if the running docker daemon doesn't support cgroup namespaces
|
|
|
|
func SkipIfCgroupNamespacesNotSupported(t *testing.T) {
|
|
|
|
t.Helper()
|
|
|
|
result := icmd.RunCmd(icmd.Command("docker", "info", "--format", "{{.SecurityOptions}}"))
|
|
|
|
result.Assert(t, icmd.Expected{Err: icmd.None})
|
|
|
|
cgroupNsFound := strings.Contains(result.Stdout(), "name=cgroupns")
|
|
|
|
|
|
|
|
skip.If(t, !cgroupNsFound, fmt.Sprintf("running against a daemon that doesn't support cgroup namespaces (security options: %s)", result.Stdout()))
|
|
|
|
}
|