requirements_test.go 4.3 KB

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