github.com/kaisenlinux/docker@v0.0.0-20230510090727-ea55db55fac7/engine/integration-cli/requirements_test.go (about) 1 package main 2 3 import ( 4 "context" 5 "fmt" 6 "net/http" 7 "os" 8 "os/exec" 9 "strconv" 10 "strings" 11 "testing" 12 "time" 13 14 "github.com/docker/docker/api/types" 15 "github.com/docker/docker/api/types/swarm" 16 "github.com/docker/docker/api/types/versions" 17 "github.com/docker/docker/client" 18 "github.com/docker/docker/integration-cli/requirement" 19 "github.com/docker/docker/testutil/registry" 20 ) 21 22 func ArchitectureIsNot(arch string) bool { 23 return os.Getenv("DOCKER_ENGINE_GOARCH") != arch 24 } 25 26 func DaemonIsWindows() bool { 27 return testEnv.OSType == "windows" 28 } 29 30 func DaemonIsWindowsAtLeastBuild(buildNumber int) func() bool { 31 return func() bool { 32 if testEnv.OSType != "windows" { 33 return false 34 } 35 version := testEnv.DaemonInfo.KernelVersion 36 numVersion, _ := strconv.Atoi(strings.Split(version, " ")[1]) 37 return numVersion >= buildNumber 38 } 39 } 40 41 func DaemonIsLinux() bool { 42 return testEnv.OSType == "linux" 43 } 44 45 func MinimumAPIVersion(version string) func() bool { 46 return func() bool { 47 return versions.GreaterThanOrEqualTo(testEnv.DaemonAPIVersion(), version) 48 } 49 } 50 51 func OnlyDefaultNetworks() bool { 52 cli, err := client.NewClientWithOpts(client.FromEnv) 53 if err != nil { 54 return false 55 } 56 networks, err := cli.NetworkList(context.TODO(), types.NetworkListOptions{}) 57 if err != nil || len(networks) > 0 { 58 return false 59 } 60 return true 61 } 62 63 func IsAmd64() bool { 64 return os.Getenv("DOCKER_ENGINE_GOARCH") == "amd64" 65 } 66 67 func NotArm() bool { 68 return ArchitectureIsNot("arm") 69 } 70 71 func NotArm64() bool { 72 return ArchitectureIsNot("arm64") 73 } 74 75 func NotPpc64le() bool { 76 return ArchitectureIsNot("ppc64le") 77 } 78 79 func UnixCli() bool { 80 return isUnixCli 81 } 82 83 func Network() bool { 84 // Set a timeout on the GET at 15s 85 const timeout = 15 * time.Second 86 const url = "https://hub.docker.com" 87 88 client := http.Client{ 89 Timeout: timeout, 90 } 91 92 resp, err := client.Get(url) 93 if err != nil && strings.Contains(err.Error(), "use of closed network connection") { 94 panic(fmt.Sprintf("Timeout for GET request on %s", url)) 95 } 96 if resp != nil { 97 resp.Body.Close() 98 } 99 return err == nil 100 } 101 102 func Apparmor() bool { 103 if strings.HasPrefix(testEnv.DaemonInfo.OperatingSystem, "SUSE Linux Enterprise Server ") { 104 return false 105 } 106 buf, err := os.ReadFile("/sys/module/apparmor/parameters/enabled") 107 return err == nil && len(buf) > 1 && buf[0] == 'Y' 108 } 109 110 func Devicemapper() bool { 111 return strings.HasPrefix(testEnv.DaemonInfo.Driver, "devicemapper") 112 } 113 114 func IPv6() bool { 115 cmd := exec.Command("test", "-f", "/proc/net/if_inet6") 116 return cmd.Run() != nil 117 } 118 119 func UserNamespaceROMount() bool { 120 // quick case--userns not enabled in this test run 121 if os.Getenv("DOCKER_REMAP_ROOT") == "" { 122 return true 123 } 124 if _, _, err := dockerCmdWithError("run", "--rm", "--read-only", "busybox", "date"); err != nil { 125 return false 126 } 127 return true 128 } 129 130 func NotUserNamespace() bool { 131 root := os.Getenv("DOCKER_REMAP_ROOT") 132 return root == "" 133 } 134 135 func UserNamespaceInKernel() bool { 136 if _, err := os.Stat("/proc/self/uid_map"); os.IsNotExist(err) { 137 /* 138 * This kernel-provided file only exists if user namespaces are 139 * supported 140 */ 141 return false 142 } 143 144 // We need extra check on redhat based distributions 145 if f, err := os.Open("/sys/module/user_namespace/parameters/enable"); err == nil { 146 defer f.Close() 147 b := make([]byte, 1) 148 _, _ = f.Read(b) 149 return string(b) != "N" 150 } 151 152 return true 153 } 154 155 func IsPausable() bool { 156 if testEnv.OSType == "windows" { 157 return testEnv.DaemonInfo.Isolation == "hyperv" 158 } 159 return true 160 } 161 162 func IsolationIs(expectedIsolation string) bool { 163 return testEnv.OSType == "windows" && string(testEnv.DaemonInfo.Isolation) == expectedIsolation 164 } 165 166 func IsolationIsHyperv() bool { 167 return IsolationIs("hyperv") 168 } 169 170 func IsolationIsProcess() bool { 171 return IsolationIs("process") 172 } 173 174 // RegistryHosting returns whether the host can host a registry (v2) or not 175 func RegistryHosting() bool { 176 // for now registry binary is built only if we're running inside 177 // container through `make test`. Figure that out by testing if 178 // registry binary is in PATH. 179 _, err := exec.LookPath(registry.V2binary) 180 return err == nil 181 } 182 183 func SwarmInactive() bool { 184 return testEnv.DaemonInfo.Swarm.LocalNodeState == swarm.LocalNodeStateInactive 185 } 186 187 func TODOBuildkit() bool { 188 return os.Getenv("DOCKER_BUILDKIT") == "" 189 } 190 191 func DockerCLIVersion(t testing.TB) string { 192 out, _ := dockerCmd(t, "--version") 193 version := strings.Fields(out) 194 if len(version) < 3 { 195 t.Fatal("unknown version output", version) 196 } 197 return version[2] 198 } 199 200 // testRequires checks if the environment satisfies the requirements 201 // for the test to run or skips the tests. 202 func testRequires(t *testing.T, requirements ...requirement.Test) { 203 t.Helper() 204 requirement.Is(t, requirements...) 205 }