requirements.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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/go-check/check"
  11. )
  12. type testCondition func() bool
  13. type testRequirement struct {
  14. Condition testCondition
  15. SkipMessage string
  16. }
  17. // List test requirements
  18. var (
  19. DaemonIsWindows = testRequirement{
  20. func() bool { return daemonPlatform == "windows" },
  21. "Test requires a Windows daemon",
  22. }
  23. DaemonIsLinux = testRequirement{
  24. func() bool { return daemonPlatform == "linux" },
  25. "Test requires a Linux daemon",
  26. }
  27. NotArm = testRequirement{
  28. func() bool { return os.Getenv("DOCKER_ENGINE_GOARCH") != "arm" },
  29. "Test requires a daemon not running on ARM",
  30. }
  31. SameHostDaemon = testRequirement{
  32. func() bool { return isLocalDaemon },
  33. "Test requires docker daemon to run on the same machine as CLI",
  34. }
  35. UnixCli = testRequirement{
  36. func() bool { return isUnixCli },
  37. "Test requires posix utilities or functionality to run.",
  38. }
  39. ExecSupport = testRequirement{
  40. func() bool { return supportsExec },
  41. "Test requires 'docker exec' capabilities on the tested daemon.",
  42. }
  43. Network = testRequirement{
  44. func() bool {
  45. // Set a timeout on the GET at 15s
  46. var timeout = time.Duration(15 * time.Second)
  47. var url = "https://hub.docker.com"
  48. client := http.Client{
  49. Timeout: timeout,
  50. }
  51. resp, err := client.Get(url)
  52. if err != nil && strings.Contains(err.Error(), "use of closed network connection") {
  53. panic(fmt.Sprintf("Timeout for GET request on %s", url))
  54. }
  55. if resp != nil {
  56. resp.Body.Close()
  57. }
  58. return err == nil
  59. },
  60. "Test requires network availability, environment variable set to none to run in a non-network enabled mode.",
  61. }
  62. Apparmor = testRequirement{
  63. func() bool {
  64. buf, err := ioutil.ReadFile("/sys/module/apparmor/parameters/enabled")
  65. return err == nil && len(buf) > 1 && buf[0] == 'Y'
  66. },
  67. "Test requires apparmor is enabled.",
  68. }
  69. RegistryHosting = testRequirement{
  70. func() bool {
  71. // for now registry binary is built only if we're running inside
  72. // container through `make test`. Figure that out by testing if
  73. // registry binary is in PATH.
  74. _, err := exec.LookPath(v2binary)
  75. return err == nil
  76. },
  77. fmt.Sprintf("Test requires an environment that can host %s in the same host", v2binary),
  78. }
  79. NotaryHosting = testRequirement{
  80. func() bool {
  81. // for now notary binary is built only if we're running inside
  82. // container through `make test`. Figure that out by testing if
  83. // notary-server binary is in PATH.
  84. _, err := exec.LookPath(notaryBinary)
  85. return err == nil
  86. },
  87. fmt.Sprintf("Test requires an environment that can host %s in the same host", notaryBinary),
  88. }
  89. NotOverlay = testRequirement{
  90. func() bool {
  91. cmd := exec.Command("grep", "^overlay / overlay", "/proc/mounts")
  92. if err := cmd.Run(); err != nil {
  93. return true
  94. }
  95. return false
  96. },
  97. "Test requires underlying root filesystem not be backed by overlay.",
  98. }
  99. IPv6 = testRequirement{
  100. func() bool {
  101. cmd := exec.Command("test", "-f", "/proc/net/if_inet6")
  102. if err := cmd.Run(); err != nil {
  103. return true
  104. }
  105. return false
  106. },
  107. "Test requires support for IPv6",
  108. }
  109. NotGCCGO = testRequirement{
  110. func() bool {
  111. out, err := exec.Command("go", "version").Output()
  112. if err == nil && strings.Contains(string(out), "gccgo") {
  113. return false
  114. }
  115. return true
  116. },
  117. "Test requires native Golang compiler instead of GCCGO",
  118. }
  119. NotUserNamespace = testRequirement{
  120. func() bool {
  121. root := os.Getenv("DOCKER_REMAP_ROOT")
  122. if root != "" {
  123. return false
  124. }
  125. return true
  126. },
  127. "Test cannot be run when remapping root",
  128. }
  129. )
  130. // testRequires checks if the environment satisfies the requirements
  131. // for the test to run or skips the tests.
  132. func testRequires(c *check.C, requirements ...testRequirement) {
  133. for _, r := range requirements {
  134. if !r.Condition() {
  135. c.Skip(r.SkipMessage)
  136. }
  137. }
  138. }