requirements_test.go 4.5 KB

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