File: testenv.go

package info (click to toggle)
docker.io 20.10.24%2Bdfsg1-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-proposed-updates
  • size: 60,824 kB
  • sloc: sh: 5,621; makefile: 593; ansic: 179; python: 162; asm: 7
file content (110 lines) | stat: -rw-r--r-- 3,237 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package environment

import (
	"fmt"
	"os"
	"strings"
	"testing"
	"time"

	"github.com/pkg/errors"
	"gotest.tools/v3/icmd"
	"gotest.tools/v3/poll"
	"gotest.tools/v3/skip"
)

// Setup a new environment
func Setup() error {
	dockerHost := os.Getenv("TEST_DOCKER_HOST")
	if dockerHost == "" {
		return errors.New("$TEST_DOCKER_HOST must be set")
	}
	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 kubeConfig := os.Getenv("TEST_KUBECONFIG"); kubeConfig != "" {
		if err := os.Setenv("KUBECONFIG", kubeConfig); 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
}

// KubernetesEnabled returns if Kubernetes testing is enabled
func KubernetesEnabled() bool {
	return os.Getenv("KUBECONFIG") != ""
}

// 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
	}
}

// DefaultPollSettings used with gotestyourself/poll
var DefaultPollSettings = poll.WithDelay(100 * time.Millisecond)

// SkipIfNotExperimentalDaemon returns whether the test docker daemon is in experimental mode
func SkipIfNotExperimentalDaemon(t *testing.T) {
	t.Helper()
	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")
}

// 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()))
}