environment.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package environment // import "github.com/docker/docker/testutil/environment"
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "testing"
  9. "github.com/docker/docker/api/types"
  10. "github.com/docker/docker/api/types/filters"
  11. "github.com/docker/docker/api/types/system"
  12. "github.com/docker/docker/client"
  13. "github.com/docker/docker/testutil/fixtures/load"
  14. "github.com/pkg/errors"
  15. "gotest.tools/v3/assert"
  16. )
  17. // Execution contains information about the current test execution and daemon
  18. // under test
  19. type Execution struct {
  20. client client.APIClient
  21. DaemonInfo system.Info
  22. PlatformDefaults PlatformDefaults
  23. protectedElements protectedElements
  24. }
  25. // PlatformDefaults are defaults values for the platform of the daemon under test
  26. type PlatformDefaults struct {
  27. BaseImage string
  28. VolumesConfigPath string
  29. ContainerStoragePath string
  30. }
  31. // New creates a new Execution struct
  32. // This is configured using the env client (see client.FromEnv)
  33. func New() (*Execution, error) {
  34. c, err := client.NewClientWithOpts(client.FromEnv)
  35. if err != nil {
  36. return nil, errors.Wrapf(err, "failed to create client")
  37. }
  38. return FromClient(c)
  39. }
  40. // FromClient creates a new Execution environment from the passed in client
  41. func FromClient(c *client.Client) (*Execution, error) {
  42. info, err := c.Info(context.Background())
  43. if err != nil {
  44. return nil, errors.Wrapf(err, "failed to get info from daemon")
  45. }
  46. return &Execution{
  47. client: c,
  48. DaemonInfo: info,
  49. PlatformDefaults: getPlatformDefaults(info),
  50. protectedElements: newProtectedElements(),
  51. }, nil
  52. }
  53. func getPlatformDefaults(info system.Info) PlatformDefaults {
  54. volumesPath := filepath.Join(info.DockerRootDir, "volumes")
  55. containersPath := filepath.Join(info.DockerRootDir, "containers")
  56. switch info.OSType {
  57. case "linux":
  58. return PlatformDefaults{
  59. BaseImage: "scratch",
  60. VolumesConfigPath: toSlash(volumesPath),
  61. ContainerStoragePath: toSlash(containersPath),
  62. }
  63. case "windows":
  64. baseImage := "microsoft/windowsservercore"
  65. if overrideBaseImage := os.Getenv("WINDOWS_BASE_IMAGE"); overrideBaseImage != "" {
  66. baseImage = overrideBaseImage
  67. if overrideBaseImageTag := os.Getenv("WINDOWS_BASE_IMAGE_TAG"); overrideBaseImageTag != "" {
  68. baseImage = baseImage + ":" + overrideBaseImageTag
  69. }
  70. }
  71. fmt.Println("INFO: Windows Base image is ", baseImage)
  72. return PlatformDefaults{
  73. BaseImage: baseImage,
  74. VolumesConfigPath: filepath.FromSlash(volumesPath),
  75. ContainerStoragePath: filepath.FromSlash(containersPath),
  76. }
  77. default:
  78. panic(fmt.Sprintf("unknown OSType for daemon: %s", info.OSType))
  79. }
  80. }
  81. // Make sure in context of daemon, not the local platform. Note we can't
  82. // use filepath.ToSlash here as that is a no-op on Unix.
  83. func toSlash(path string) string {
  84. return strings.ReplaceAll(path, `\`, `/`)
  85. }
  86. // IsLocalDaemon is true if the daemon under test is on the same
  87. // host as the test process.
  88. //
  89. // Deterministically working out the environment in which CI is running
  90. // to evaluate whether the daemon is local or remote is not possible through
  91. // a build tag.
  92. //
  93. // For example Windows to Linux CI under Jenkins tests the 64-bit
  94. // Windows binary build with the daemon build tag, but calls a remote
  95. // Linux daemon.
  96. //
  97. // We can't just say if Windows then assume the daemon is local as at
  98. // some point, we will be testing the Windows CLI against a Windows daemon.
  99. //
  100. // Similarly, it will be perfectly valid to also run CLI tests from
  101. // a Linux CLI (built with the daemon tag) against a Windows daemon.
  102. func (e *Execution) IsLocalDaemon() bool {
  103. return os.Getenv("DOCKER_REMOTE_DAEMON") == ""
  104. }
  105. // IsRemoteDaemon is true if the daemon under test is on different host
  106. // as the test process.
  107. func (e *Execution) IsRemoteDaemon() bool {
  108. return !e.IsLocalDaemon()
  109. }
  110. // DaemonAPIVersion returns the negotiated daemon api version
  111. func (e *Execution) DaemonAPIVersion() string {
  112. version, err := e.APIClient().ServerVersion(context.TODO())
  113. if err != nil {
  114. return ""
  115. }
  116. return version.APIVersion
  117. }
  118. // Print the execution details to stdout
  119. // TODO: print everything
  120. func (e *Execution) Print() {
  121. if e.IsLocalDaemon() {
  122. fmt.Println("INFO: Testing against a local daemon")
  123. } else {
  124. fmt.Println("INFO: Testing against a remote daemon")
  125. }
  126. }
  127. // APIClient returns an APIClient connected to the daemon under test
  128. func (e *Execution) APIClient() client.APIClient {
  129. return e.client
  130. }
  131. // IsUserNamespace returns whether the user namespace remapping is enabled
  132. func (e *Execution) IsUserNamespace() bool {
  133. root := os.Getenv("DOCKER_REMAP_ROOT")
  134. return root != ""
  135. }
  136. // RuntimeIsWindowsContainerd returns whether containerd runtime is used on Windows
  137. func (e *Execution) RuntimeIsWindowsContainerd() bool {
  138. return os.Getenv("DOCKER_WINDOWS_CONTAINERD_RUNTIME") == "1"
  139. }
  140. // IsRootless returns whether the rootless mode is enabled
  141. func (e *Execution) IsRootless() bool {
  142. return os.Getenv("DOCKER_ROOTLESS") != ""
  143. }
  144. // IsUserNamespaceInKernel returns whether the kernel supports user namespaces
  145. func (e *Execution) IsUserNamespaceInKernel() bool {
  146. if _, err := os.Stat("/proc/self/uid_map"); os.IsNotExist(err) {
  147. /*
  148. * This kernel-provided file only exists if user namespaces are
  149. * supported
  150. */
  151. return false
  152. }
  153. // We need extra check on redhat based distributions
  154. if f, err := os.Open("/sys/module/user_namespace/parameters/enable"); err == nil {
  155. defer f.Close()
  156. b := make([]byte, 1)
  157. _, _ = f.Read(b)
  158. return string(b) != "N"
  159. }
  160. return true
  161. }
  162. // UsingSnapshotter returns whether containerd snapshotters are used for the
  163. // tests by checking if the "TEST_INTEGRATION_USE_SNAPSHOTTER" is set to a
  164. // non-empty value.
  165. func (e *Execution) UsingSnapshotter() bool {
  166. return os.Getenv("TEST_INTEGRATION_USE_SNAPSHOTTER") != ""
  167. }
  168. // HasExistingImage checks whether there is an image with the given reference.
  169. // Note that this is done by filtering and then checking whether there were any
  170. // results -- so ambiguous references might result in false-positives.
  171. func (e *Execution) HasExistingImage(t testing.TB, reference string) bool {
  172. imageList, err := e.APIClient().ImageList(context.Background(), types.ImageListOptions{
  173. All: true,
  174. Filters: filters.NewArgs(
  175. filters.Arg("dangling", "false"),
  176. filters.Arg("reference", reference),
  177. ),
  178. })
  179. assert.NilError(t, err, "failed to list images")
  180. return len(imageList) > 0
  181. }
  182. // EnsureFrozenImagesLinux loads frozen test images into the daemon
  183. // if they aren't already loaded
  184. func EnsureFrozenImagesLinux(testEnv *Execution) error {
  185. if testEnv.DaemonInfo.OSType == "linux" {
  186. err := load.FrozenImagesLinux(testEnv.APIClient(), frozenImages...)
  187. if err != nil {
  188. return errors.Wrap(err, "error loading frozen images")
  189. }
  190. }
  191. return nil
  192. }
  193. // GitHubActions is true if test is executed on a GitHub Runner.
  194. func (e *Execution) GitHubActions() bool {
  195. return os.Getenv("GITHUB_ACTIONS") != ""
  196. }