requirements_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "os"
  7. "os/exec"
  8. "strings"
  9. "testing"
  10. "time"
  11. "github.com/containerd/containerd/plugin"
  12. "github.com/docker/docker/api/types"
  13. "github.com/docker/docker/api/types/swarm"
  14. "github.com/docker/docker/api/types/versions"
  15. "github.com/docker/docker/client"
  16. "github.com/docker/docker/integration-cli/requirement"
  17. "github.com/docker/docker/testutil/registry"
  18. )
  19. func ArchitectureIsNot(arch string) bool {
  20. return os.Getenv("DOCKER_ENGINE_GOARCH") != arch
  21. }
  22. func DaemonIsWindows() bool {
  23. return testEnv.OSType == "windows"
  24. }
  25. func DaemonIsLinux() bool {
  26. return testEnv.OSType == "linux"
  27. }
  28. func MinimumAPIVersion(version string) func() bool {
  29. return func() bool {
  30. return versions.GreaterThanOrEqualTo(testEnv.DaemonAPIVersion(), version)
  31. }
  32. }
  33. func OnlyDefaultNetworks() bool {
  34. cli, err := client.NewClientWithOpts(client.FromEnv)
  35. if err != nil {
  36. return false
  37. }
  38. networks, err := cli.NetworkList(context.TODO(), types.NetworkListOptions{})
  39. if err != nil || len(networks) > 0 {
  40. return false
  41. }
  42. return true
  43. }
  44. func IsAmd64() bool {
  45. return os.Getenv("DOCKER_ENGINE_GOARCH") == "amd64"
  46. }
  47. func NotArm() bool {
  48. return ArchitectureIsNot("arm")
  49. }
  50. func NotArm64() bool {
  51. return ArchitectureIsNot("arm64")
  52. }
  53. func NotPpc64le() bool {
  54. return ArchitectureIsNot("ppc64le")
  55. }
  56. func UnixCli() bool {
  57. return isUnixCli
  58. }
  59. func GitHubActions() bool {
  60. return os.Getenv("GITHUB_ACTIONS") != ""
  61. }
  62. func Network() bool {
  63. // Set a timeout on the GET at 15s
  64. const timeout = 15 * time.Second
  65. const url = "https://hub.docker.com"
  66. client := http.Client{
  67. Timeout: timeout,
  68. }
  69. resp, err := client.Get(url)
  70. if err != nil && strings.Contains(err.Error(), "use of closed network connection") {
  71. panic(fmt.Sprintf("Timeout for GET request on %s", url))
  72. }
  73. if resp != nil {
  74. resp.Body.Close()
  75. }
  76. return err == nil
  77. }
  78. func Apparmor() bool {
  79. if strings.HasPrefix(testEnv.DaemonInfo.OperatingSystem, "SUSE Linux Enterprise Server ") {
  80. return false
  81. }
  82. buf, err := os.ReadFile("/sys/module/apparmor/parameters/enabled")
  83. return err == nil && len(buf) > 1 && buf[0] == 'Y'
  84. }
  85. func Devicemapper() bool {
  86. return strings.HasPrefix(testEnv.DaemonInfo.Driver, "devicemapper")
  87. }
  88. // containerdSnapshotterEnabled checks if the daemon in the test-environment is
  89. // configured with containerd-snapshotters enabled.
  90. func containerdSnapshotterEnabled() bool {
  91. for _, v := range testEnv.DaemonInfo.DriverStatus {
  92. if v[0] == "driver-type" {
  93. return v[1] == string(plugin.SnapshotPlugin)
  94. }
  95. }
  96. return false
  97. }
  98. func IPv6() bool {
  99. cmd := exec.Command("test", "-f", "/proc/net/if_inet6")
  100. return cmd.Run() != nil
  101. }
  102. func UserNamespaceROMount() bool {
  103. // quick case--userns not enabled in this test run
  104. if os.Getenv("DOCKER_REMAP_ROOT") == "" {
  105. return true
  106. }
  107. if _, _, err := dockerCmdWithError("run", "--rm", "--read-only", "busybox", "date"); err != nil {
  108. return false
  109. }
  110. return true
  111. }
  112. func NotUserNamespace() bool {
  113. root := os.Getenv("DOCKER_REMAP_ROOT")
  114. return root == ""
  115. }
  116. func UserNamespaceInKernel() bool {
  117. if _, err := os.Stat("/proc/self/uid_map"); os.IsNotExist(err) {
  118. /*
  119. * This kernel-provided file only exists if user namespaces are
  120. * supported
  121. */
  122. return false
  123. }
  124. // We need extra check on redhat based distributions
  125. if f, err := os.Open("/sys/module/user_namespace/parameters/enable"); err == nil {
  126. defer f.Close()
  127. b := make([]byte, 1)
  128. _, _ = f.Read(b)
  129. return string(b) != "N"
  130. }
  131. return true
  132. }
  133. func IsPausable() bool {
  134. if testEnv.OSType == "windows" {
  135. return testEnv.DaemonInfo.Isolation.IsHyperV()
  136. }
  137. return true
  138. }
  139. // RegistryHosting returns whether the host can host a registry (v2) or not
  140. func RegistryHosting() bool {
  141. // for now registry binary is built only if we're running inside
  142. // container through `make test`. Figure that out by testing if
  143. // registry binary is in PATH.
  144. _, err := exec.LookPath(registry.V2binary)
  145. return err == nil
  146. }
  147. func RuntimeIsWindowsContainerd() bool {
  148. return os.Getenv("DOCKER_WINDOWS_CONTAINERD_RUNTIME") == "1"
  149. }
  150. func SwarmInactive() bool {
  151. return testEnv.DaemonInfo.Swarm.LocalNodeState == swarm.LocalNodeStateInactive
  152. }
  153. func TODOBuildkit() bool {
  154. return os.Getenv("DOCKER_BUILDKIT") == ""
  155. }
  156. func DockerCLIVersion(t testing.TB) string {
  157. out, _ := dockerCmd(t, "--version")
  158. version := strings.Fields(out)
  159. if len(version) < 3 {
  160. t.Fatal("unknown version output", version)
  161. }
  162. return version[2]
  163. }
  164. // testRequires checks if the environment satisfies the requirements
  165. // for the test to run or skips the tests.
  166. func testRequires(t *testing.T, requirements ...requirement.Test) {
  167. t.Helper()
  168. requirement.Is(t, requirements...)
  169. }