requirements_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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.DaemonInfo.OSType == "windows"
  24. }
  25. func DaemonIsLinux() bool {
  26. return testEnv.DaemonInfo.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. apiClient, err := client.NewClientWithOpts(client.FromEnv)
  35. if err != nil {
  36. return false
  37. }
  38. networks, err := apiClient.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. c := http.Client{
  67. Timeout: timeout,
  68. }
  69. resp, err := c.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. // containerdSnapshotterEnabled checks if the daemon in the test-environment is
  86. // configured with containerd-snapshotters enabled.
  87. func containerdSnapshotterEnabled() bool {
  88. for _, v := range testEnv.DaemonInfo.DriverStatus {
  89. if v[0] == "driver-type" {
  90. return v[1] == string(plugin.SnapshotPlugin)
  91. }
  92. }
  93. return false
  94. }
  95. func IPv6() bool {
  96. cmd := exec.Command("test", "-f", "/proc/net/if_inet6")
  97. return cmd.Run() != nil
  98. }
  99. func UserNamespaceROMount() bool {
  100. // quick case--userns not enabled in this test run
  101. if os.Getenv("DOCKER_REMAP_ROOT") == "" {
  102. return true
  103. }
  104. if _, _, err := dockerCmdWithError("run", "--rm", "--read-only", "busybox", "date"); err != nil {
  105. return false
  106. }
  107. return true
  108. }
  109. func NotUserNamespace() bool {
  110. root := os.Getenv("DOCKER_REMAP_ROOT")
  111. return root == ""
  112. }
  113. func UserNamespaceInKernel() bool {
  114. if _, err := os.Stat("/proc/self/uid_map"); os.IsNotExist(err) {
  115. /*
  116. * This kernel-provided file only exists if user namespaces are
  117. * supported
  118. */
  119. return false
  120. }
  121. // We need extra check on redhat based distributions
  122. if f, err := os.Open("/sys/module/user_namespace/parameters/enable"); err == nil {
  123. defer f.Close()
  124. b := make([]byte, 1)
  125. _, _ = f.Read(b)
  126. return string(b) != "N"
  127. }
  128. return true
  129. }
  130. func IsPausable() bool {
  131. if testEnv.DaemonInfo.OSType == "windows" {
  132. return testEnv.DaemonInfo.Isolation.IsHyperV()
  133. }
  134. return true
  135. }
  136. // RegistryHosting returns whether the host can host a registry (v2) or not
  137. func RegistryHosting() bool {
  138. // for now registry binary is built only if we're running inside
  139. // container through `make test`. Figure that out by testing if
  140. // registry binary is in PATH.
  141. _, err := exec.LookPath(registry.V2binary)
  142. return err == nil
  143. }
  144. func RuntimeIsWindowsContainerd() bool {
  145. return os.Getenv("DOCKER_WINDOWS_CONTAINERD_RUNTIME") == "1"
  146. }
  147. func SwarmInactive() bool {
  148. return testEnv.DaemonInfo.Swarm.LocalNodeState == swarm.LocalNodeStateInactive
  149. }
  150. func TODOBuildkit() bool {
  151. return os.Getenv("DOCKER_BUILDKIT") == ""
  152. }
  153. func DockerCLIVersion(t testing.TB) string {
  154. out, _ := dockerCmd(t, "--version")
  155. version := strings.Fields(out)
  156. if len(version) < 3 {
  157. t.Fatal("unknown version output", version)
  158. }
  159. return version[2]
  160. }
  161. // testRequires checks if the environment satisfies the requirements
  162. // for the test to run or skips the tests.
  163. func testRequires(t *testing.T, requirements ...requirement.Test) {
  164. t.Helper()
  165. requirement.Is(t, requirements...)
  166. }