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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
package main
import (
"context"
"fmt"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
"testing"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/api/types/versions"
"github.com/docker/docker/client"
"github.com/docker/docker/integration-cli/requirement"
"github.com/docker/docker/testutil/registry"
)
func ArchitectureIsNot(arch string) bool {
return os.Getenv("DOCKER_ENGINE_GOARCH") != arch
}
func DaemonIsWindows() bool {
return testEnv.OSType == "windows"
}
func DaemonIsWindowsAtLeastBuild(buildNumber int) func() bool {
return func() bool {
if testEnv.OSType != "windows" {
return false
}
version := testEnv.DaemonInfo.KernelVersion
numVersion, _ := strconv.Atoi(strings.Split(version, " ")[1])
return numVersion >= buildNumber
}
}
func DaemonIsLinux() bool {
return testEnv.OSType == "linux"
}
func MinimumAPIVersion(version string) func() bool {
return func() bool {
return versions.GreaterThanOrEqualTo(testEnv.DaemonAPIVersion(), version)
}
}
func OnlyDefaultNetworks() bool {
cli, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
return false
}
networks, err := cli.NetworkList(context.TODO(), types.NetworkListOptions{})
if err != nil || len(networks) > 0 {
return false
}
return true
}
func IsAmd64() bool {
return os.Getenv("DOCKER_ENGINE_GOARCH") == "amd64"
}
func NotArm() bool {
return ArchitectureIsNot("arm")
}
func NotArm64() bool {
return ArchitectureIsNot("arm64")
}
func NotPpc64le() bool {
return ArchitectureIsNot("ppc64le")
}
func UnixCli() bool {
return isUnixCli
}
func Network() bool {
// Set a timeout on the GET at 15s
const timeout = 15 * time.Second
const url = "https://hub.docker.com"
client := http.Client{
Timeout: timeout,
}
resp, err := client.Get(url)
if err != nil && strings.Contains(err.Error(), "use of closed network connection") {
panic(fmt.Sprintf("Timeout for GET request on %s", url))
}
if resp != nil {
resp.Body.Close()
}
return err == nil
}
func Apparmor() bool {
if strings.HasPrefix(testEnv.DaemonInfo.OperatingSystem, "SUSE Linux Enterprise Server ") {
return false
}
buf, err := os.ReadFile("/sys/module/apparmor/parameters/enabled")
return err == nil && len(buf) > 1 && buf[0] == 'Y'
}
func Devicemapper() bool {
return strings.HasPrefix(testEnv.DaemonInfo.Driver, "devicemapper")
}
func IPv6() bool {
cmd := exec.Command("test", "-f", "/proc/net/if_inet6")
return cmd.Run() != nil
}
func UserNamespaceROMount() bool {
// quick case--userns not enabled in this test run
if os.Getenv("DOCKER_REMAP_ROOT") == "" {
return true
}
if _, _, err := dockerCmdWithError("run", "--rm", "--read-only", "busybox", "date"); err != nil {
return false
}
return true
}
func NotUserNamespace() bool {
root := os.Getenv("DOCKER_REMAP_ROOT")
return root == ""
}
func UserNamespaceInKernel() bool {
if _, err := os.Stat("/proc/self/uid_map"); os.IsNotExist(err) {
/*
* This kernel-provided file only exists if user namespaces are
* supported
*/
return false
}
// We need extra check on redhat based distributions
if f, err := os.Open("/sys/module/user_namespace/parameters/enable"); err == nil {
defer f.Close()
b := make([]byte, 1)
_, _ = f.Read(b)
return string(b) != "N"
}
return true
}
func IsPausable() bool {
if testEnv.OSType == "windows" {
return testEnv.DaemonInfo.Isolation == "hyperv"
}
return true
}
func IsolationIs(expectedIsolation string) bool {
return testEnv.OSType == "windows" && string(testEnv.DaemonInfo.Isolation) == expectedIsolation
}
func IsolationIsHyperv() bool {
return IsolationIs("hyperv")
}
func IsolationIsProcess() bool {
return IsolationIs("process")
}
// RegistryHosting returns whether the host can host a registry (v2) or not
func RegistryHosting() bool {
// for now registry binary is built only if we're running inside
// container through `make test`. Figure that out by testing if
// registry binary is in PATH.
_, err := exec.LookPath(registry.V2binary)
return err == nil
}
func SwarmInactive() bool {
return testEnv.DaemonInfo.Swarm.LocalNodeState == swarm.LocalNodeStateInactive
}
func TODOBuildkit() bool {
return os.Getenv("DOCKER_BUILDKIT") == ""
}
func DockerCLIVersion(t testing.TB) string {
out, _ := dockerCmd(t, "--version")
version := strings.Fields(out)
if len(version) < 3 {
t.Fatal("unknown version output", version)
}
return version[2]
}
// testRequires checks if the environment satisfies the requirements
// for the test to run or skips the tests.
func testRequires(t *testing.T, requirements ...requirement.Test) {
t.Helper()
requirement.Is(t, requirements...)
}
|