...
1 package environment
2
3 import (
4 "fmt"
5 "os"
6 "strings"
7 "testing"
8 "time"
9
10 "github.com/docker/docker/client"
11 "github.com/pkg/errors"
12 "gotest.tools/v3/icmd"
13 "gotest.tools/v3/poll"
14 "gotest.tools/v3/skip"
15 )
16
17
18 func Setup() error {
19 dockerHost := os.Getenv("TEST_DOCKER_HOST")
20 if dockerHost == "" {
21 return errors.New("$TEST_DOCKER_HOST must be set")
22 }
23 if err := os.Setenv(client.EnvOverrideHost, dockerHost); err != nil {
24 return err
25 }
26
27 if dockerCertPath := os.Getenv("TEST_DOCKER_CERT_PATH"); dockerCertPath != "" {
28 if err := os.Setenv(client.EnvOverrideCertPath, dockerCertPath); err != nil {
29 return err
30 }
31 if err := os.Setenv(client.EnvTLSVerify, "1"); err != nil {
32 return err
33 }
34 }
35
36 if val := boolFromString(os.Getenv("TEST_REMOTE_DAEMON")); val {
37 if err := os.Setenv("REMOTE_DAEMON", "1"); err != nil {
38 return err
39 }
40 }
41
42 if val := boolFromString(os.Getenv("TEST_SKIP_PLUGIN_TESTS")); val {
43 if err := os.Setenv("SKIP_PLUGIN_TESTS", "1"); err != nil {
44 return err
45 }
46 }
47
48 return nil
49 }
50
51
52 func RemoteDaemon() bool {
53 return os.Getenv("REMOTE_DAEMON") != ""
54 }
55
56
57 func SkipPluginTests() bool {
58 return os.Getenv("SKIP_PLUGIN_TESTS") != ""
59 }
60
61
62 func boolFromString(val string) bool {
63 switch strings.ToLower(val) {
64 case "true", "1":
65 return true
66 default:
67 return false
68 }
69 }
70
71
72 var DefaultPollSettings = poll.WithDelay(100 * time.Millisecond)
73
74
75 func SkipIfNotExperimentalDaemon(t *testing.T) {
76 t.Helper()
77 result := icmd.RunCmd(icmd.Command("docker", "info", "--format", "{{.ExperimentalBuild}}"))
78 result.Assert(t, icmd.Expected{Err: icmd.None})
79 experimentalBuild := strings.TrimSpace(result.Stdout()) == "true"
80 skip.If(t, !experimentalBuild, "running against a non-experimental daemon")
81 }
82
83
84 func SkipIfDaemonNotLinux(t *testing.T) {
85 t.Helper()
86 result := icmd.RunCmd(icmd.Command("docker", "info", "--format", "{{.OSType}}"))
87 result.Assert(t, icmd.Expected{Err: icmd.None})
88 isLinux := strings.TrimSpace(result.Stdout()) == "linux"
89 skip.If(t, !isLinux, "running against a Linux daemon")
90 }
91
92
93 func SkipIfCgroupNamespacesNotSupported(t *testing.T) {
94 t.Helper()
95 result := icmd.RunCmd(icmd.Command("docker", "info", "--format", "{{.SecurityOptions}}"))
96 result.Assert(t, icmd.Expected{Err: icmd.None})
97 cgroupNsFound := strings.Contains(result.Stdout(), "name=cgroupns")
98
99 skip.If(t, !cgroupNsFound, fmt.Sprintf("running against a daemon that doesn't support cgroup namespaces (security options: %s)", result.Stdout()))
100 }
101
102
103
104 func SkipIfNotPlatform(t *testing.T, platform string) {
105 t.Helper()
106 result := icmd.RunCmd(icmd.Command("docker", "version", "--format", "{{.Server.Os}}/{{.Server.Arch}}"))
107 result.Assert(t, icmd.Expected{Err: icmd.None})
108 daemonPlatform := strings.TrimSpace(result.Stdout())
109 skip.If(t, daemonPlatform != platform, "running against a non %s daemon", platform)
110 }
111
112
113 func DaemonAPIVersion(t *testing.T) string {
114 t.Helper()
115
116
117
118 result := icmd.RunCmd(icmd.Command("docker", "version", "--format", "{{.Client.APIVersion}}"))
119 result.Assert(t, icmd.Expected{Err: icmd.None})
120 return strings.TrimSpace(result.Stdout())
121 }
122
View as plain text