requirements.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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/utils"
  11. "github.com/go-check/check"
  12. )
  13. type testCondition func() bool
  14. type testRequirement struct {
  15. Condition testCondition
  16. SkipMessage string
  17. }
  18. // List test requirements
  19. var (
  20. DaemonIsWindows = testRequirement{
  21. func() bool { return daemonPlatform == "windows" },
  22. "Test requires a Windows daemon",
  23. }
  24. DaemonIsLinux = testRequirement{
  25. func() bool { return daemonPlatform == "linux" },
  26. "Test requires a Linux daemon",
  27. }
  28. ExperimentalDaemon = testRequirement{
  29. func() bool { return utils.ExperimentalBuild() },
  30. "Test requires an experimental daemon",
  31. }
  32. NotExperimentalDaemon = testRequirement{
  33. func() bool { return !utils.ExperimentalBuild() },
  34. "Test requires a non experimental daemon",
  35. }
  36. IsAmd64 = testRequirement{
  37. func() bool { return os.Getenv("DOCKER_ENGINE_GOARCH") == "amd64" },
  38. "Test requires a daemon running on amd64",
  39. }
  40. NotArm = testRequirement{
  41. func() bool { return os.Getenv("DOCKER_ENGINE_GOARCH") != "arm" },
  42. "Test requires a daemon not running on ARM",
  43. }
  44. NotArm64 = testRequirement{
  45. func() bool { return os.Getenv("DOCKER_ENGINE_GOARCH") != "arm64" },
  46. "Test requires a daemon not running on arm64",
  47. }
  48. NotPpc64le = testRequirement{
  49. func() bool { return os.Getenv("DOCKER_ENGINE_GOARCH") != "ppc64le" },
  50. "Test requires a daemon not running on ppc64le",
  51. }
  52. NotS390X = testRequirement{
  53. func() bool { return os.Getenv("DOCKER_ENGINE_GOARCH") != "s390x" },
  54. "Test requires a daemon not running on s390x",
  55. }
  56. SameHostDaemon = testRequirement{
  57. func() bool { return isLocalDaemon },
  58. "Test requires docker daemon to run on the same machine as CLI",
  59. }
  60. UnixCli = testRequirement{
  61. func() bool { return isUnixCli },
  62. "Test requires posix utilities or functionality to run.",
  63. }
  64. ExecSupport = testRequirement{
  65. func() bool { return supportsExec },
  66. "Test requires 'docker exec' capabilities on the tested daemon.",
  67. }
  68. Network = testRequirement{
  69. func() bool {
  70. // Set a timeout on the GET at 15s
  71. var timeout = time.Duration(15 * time.Second)
  72. var url = "https://hub.docker.com"
  73. client := http.Client{
  74. Timeout: timeout,
  75. }
  76. resp, err := client.Get(url)
  77. if err != nil && strings.Contains(err.Error(), "use of closed network connection") {
  78. panic(fmt.Sprintf("Timeout for GET request on %s", url))
  79. }
  80. if resp != nil {
  81. resp.Body.Close()
  82. }
  83. return err == nil
  84. },
  85. "Test requires network availability, environment variable set to none to run in a non-network enabled mode.",
  86. }
  87. Apparmor = testRequirement{
  88. func() bool {
  89. buf, err := ioutil.ReadFile("/sys/module/apparmor/parameters/enabled")
  90. return err == nil && len(buf) > 1 && buf[0] == 'Y'
  91. },
  92. "Test requires apparmor is enabled.",
  93. }
  94. RegistryHosting = testRequirement{
  95. func() bool {
  96. // for now registry binary is built only if we're running inside
  97. // container through `make test`. Figure that out by testing if
  98. // registry binary is in PATH.
  99. _, err := exec.LookPath(v2binary)
  100. return err == nil
  101. },
  102. fmt.Sprintf("Test requires an environment that can host %s in the same host", v2binary),
  103. }
  104. NotaryHosting = testRequirement{
  105. func() bool {
  106. // for now notary binary is built only if we're running inside
  107. // container through `make test`. Figure that out by testing if
  108. // notary-server binary is in PATH.
  109. _, err := exec.LookPath(notaryServerBinary)
  110. return err == nil
  111. },
  112. fmt.Sprintf("Test requires an environment that can host %s in the same host", notaryServerBinary),
  113. }
  114. NotaryServerHosting = testRequirement{
  115. func() bool {
  116. // for now notary-server binary is built only if we're running inside
  117. // container through `make test`. Figure that out by testing if
  118. // notary-server binary is in PATH.
  119. _, err := exec.LookPath(notaryServerBinary)
  120. return err == nil
  121. },
  122. fmt.Sprintf("Test requires an environment that can host %s in the same host", notaryServerBinary),
  123. }
  124. NotOverlay = testRequirement{
  125. func() bool {
  126. return !strings.HasPrefix(daemonStorageDriver, "overlay")
  127. },
  128. "Test requires underlying root filesystem not be backed by overlay.",
  129. }
  130. Devicemapper = testRequirement{
  131. func() bool {
  132. return strings.HasPrefix(daemonStorageDriver, "devicemapper")
  133. },
  134. "Test requires underlying root filesystem to be backed by devicemapper.",
  135. }
  136. IPv6 = testRequirement{
  137. func() bool {
  138. cmd := exec.Command("test", "-f", "/proc/net/if_inet6")
  139. if err := cmd.Run(); err != nil {
  140. return true
  141. }
  142. return false
  143. },
  144. "Test requires support for IPv6",
  145. }
  146. NotGCCGO = testRequirement{
  147. func() bool {
  148. out, err := exec.Command("go", "version").Output()
  149. if err == nil && strings.Contains(string(out), "gccgo") {
  150. return false
  151. }
  152. return true
  153. },
  154. "Test requires native Golang compiler instead of GCCGO",
  155. }
  156. UserNamespaceInKernel = testRequirement{
  157. func() bool {
  158. if _, err := os.Stat("/proc/self/uid_map"); os.IsNotExist(err) {
  159. /*
  160. * This kernel-provided file only exists if user namespaces are
  161. * supported
  162. */
  163. return false
  164. }
  165. // We need extra check on redhat based distributions
  166. if f, err := os.Open("/sys/module/user_namespace/parameters/enable"); err == nil {
  167. b := make([]byte, 1)
  168. _, _ = f.Read(b)
  169. if string(b) == "N" {
  170. return false
  171. }
  172. return true
  173. }
  174. return true
  175. },
  176. "Kernel must have user namespaces configured and enabled.",
  177. }
  178. NotUserNamespace = testRequirement{
  179. func() bool {
  180. root := os.Getenv("DOCKER_REMAP_ROOT")
  181. if root != "" {
  182. return false
  183. }
  184. return true
  185. },
  186. "Test cannot be run when remapping root",
  187. }
  188. )
  189. // testRequires checks if the environment satisfies the requirements
  190. // for the test to run or skips the tests.
  191. func testRequires(c *check.C, requirements ...testRequirement) {
  192. for _, r := range requirements {
  193. if !r.Condition() {
  194. c.Skip(r.SkipMessage)
  195. }
  196. }
  197. }