requirements_test.go 4.3 KB

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