requirements.go 4.1 KB

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