requirements_test.go 4.7 KB

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