requirements_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "os"
  8. "os/exec"
  9. "strconv"
  10. "strings"
  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/internal/test/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.NewEnvClient()
  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 SameHostDaemon() bool {
  67. return testEnv.IsLocalDaemon()
  68. }
  69. func UnixCli() bool {
  70. return isUnixCli
  71. }
  72. func ExecSupport() bool {
  73. return supportsExec
  74. }
  75. func Network() bool {
  76. // Set a timeout on the GET at 15s
  77. var timeout = time.Duration(15 * time.Second)
  78. var url = "https://hub.docker.com"
  79. client := http.Client{
  80. Timeout: timeout,
  81. }
  82. resp, err := client.Get(url)
  83. if err != nil && strings.Contains(err.Error(), "use of closed network connection") {
  84. panic(fmt.Sprintf("Timeout for GET request on %s", url))
  85. }
  86. if resp != nil {
  87. resp.Body.Close()
  88. }
  89. return err == nil
  90. }
  91. func Apparmor() bool {
  92. if strings.HasPrefix(testEnv.DaemonInfo.OperatingSystem, "SUSE Linux Enterprise Server ") {
  93. return false
  94. }
  95. buf, err := ioutil.ReadFile("/sys/module/apparmor/parameters/enabled")
  96. return err == nil && len(buf) > 1 && buf[0] == 'Y'
  97. }
  98. func Devicemapper() bool {
  99. return strings.HasPrefix(testEnv.DaemonInfo.Driver, "devicemapper")
  100. }
  101. func IPv6() bool {
  102. cmd := exec.Command("test", "-f", "/proc/net/if_inet6")
  103. return cmd.Run() != nil
  104. }
  105. func UserNamespaceROMount() bool {
  106. // quick case--userns not enabled in this test run
  107. if os.Getenv("DOCKER_REMAP_ROOT") == "" {
  108. return true
  109. }
  110. if _, _, err := dockerCmdWithError("run", "--rm", "--read-only", "busybox", "date"); err != nil {
  111. return false
  112. }
  113. return true
  114. }
  115. func NotUserNamespace() bool {
  116. root := os.Getenv("DOCKER_REMAP_ROOT")
  117. return root == ""
  118. }
  119. func UserNamespaceInKernel() bool {
  120. if _, err := os.Stat("/proc/self/uid_map"); os.IsNotExist(err) {
  121. /*
  122. * This kernel-provided file only exists if user namespaces are
  123. * supported
  124. */
  125. return false
  126. }
  127. // We need extra check on redhat based distributions
  128. if f, err := os.Open("/sys/module/user_namespace/parameters/enable"); err == nil {
  129. defer f.Close()
  130. b := make([]byte, 1)
  131. _, _ = f.Read(b)
  132. return string(b) != "N"
  133. }
  134. return true
  135. }
  136. func IsPausable() bool {
  137. if testEnv.OSType == "windows" {
  138. return testEnv.DaemonInfo.Isolation == "hyperv"
  139. }
  140. return true
  141. }
  142. func IsolationIs(expectedIsolation string) bool {
  143. return testEnv.OSType == "windows" && string(testEnv.DaemonInfo.Isolation) == expectedIsolation
  144. }
  145. func IsolationIsHyperv() bool {
  146. return IsolationIs("hyperv")
  147. }
  148. func IsolationIsProcess() bool {
  149. return IsolationIs("process")
  150. }
  151. // RegistryHosting returns whether the host can host a registry (v2) or not
  152. func RegistryHosting() bool {
  153. // for now registry binary is built only if we're running inside
  154. // container through `make test`. Figure that out by testing if
  155. // registry binary is in PATH.
  156. _, err := exec.LookPath(registry.V2binary)
  157. return err == nil
  158. }
  159. func SwarmInactive() bool {
  160. return testEnv.DaemonInfo.Swarm.LocalNodeState == swarm.LocalNodeStateInactive
  161. }
  162. func TODOBuildkit() bool {
  163. return os.Getenv("DOCKER_BUILDKIT") == ""
  164. }
  165. // testRequires checks if the environment satisfies the requirements
  166. // for the test to run or skips the tests.
  167. func testRequires(c requirement.SkipT, requirements ...requirement.Test) {
  168. requirement.Is(c, requirements...)
  169. }