requirements_test.go 4.6 KB

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