docker_test_vars.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "os/exec"
  8. "path/filepath"
  9. "strconv"
  10. "github.com/docker/docker/api/types/container"
  11. "github.com/docker/docker/pkg/reexec"
  12. )
  13. var (
  14. // the docker client binary to use
  15. dockerBinary = "docker"
  16. // the docker daemon binary to use
  17. dockerdBinary = "dockerd"
  18. // path to containerd's ctr binary
  19. ctrBinary = "docker-containerd-ctr"
  20. // the private registry image to use for tests involving the registry
  21. registryImageName = "registry"
  22. // the private registry to use for tests
  23. privateRegistryURL = "127.0.0.1:5000"
  24. // TODO Windows CI. These are incorrect and need fixing into
  25. // platform specific pieces.
  26. runtimePath = "/var/run/docker"
  27. workingDirectory string
  28. // isLocalDaemon is true if the daemon under test is on the same
  29. // host as the CLI.
  30. isLocalDaemon bool
  31. // daemonPlatform is held globally so that tests can make intelligent
  32. // decisions on how to configure themselves according to the platform
  33. // of the daemon. This is initialized in docker_utils by sending
  34. // a version call to the daemon and examining the response header.
  35. daemonPlatform string
  36. // windowsDaemonKV is used on Windows to distinguish between different
  37. // versions. This is necessary to enable certain tests based on whether
  38. // the platform supports it. For example, Windows Server 2016 TP3 did
  39. // not support volumes, but TP4 did.
  40. windowsDaemonKV int
  41. // daemonDefaultImage is the name of the default image to use when running
  42. // tests. This is platform dependent.
  43. daemonDefaultImage string
  44. // For a local daemon on Linux, these values will be used for testing
  45. // user namespace support as the standard graph path(s) will be
  46. // appended with the root remapped uid.gid prefix
  47. dockerBasePath string
  48. volumesConfigPath string
  49. containerStoragePath string
  50. // experimentalDaemon tell whether the main daemon has
  51. // experimental features enabled or not
  52. experimentalDaemon bool
  53. // daemonStorageDriver is held globally so that tests can know the storage
  54. // driver of the daemon. This is initialized in docker_utils by sending
  55. // a version call to the daemon and examining the response header.
  56. daemonStorageDriver string
  57. // WindowsBaseImage is the name of the base image for Windows testing
  58. // Environment variable WINDOWS_BASE_IMAGE can override this
  59. WindowsBaseImage = "microsoft/windowsservercore"
  60. // isolation is the isolation mode of the daemon under test
  61. isolation container.Isolation
  62. // daemonPid is the pid of the main test daemon
  63. daemonPid int
  64. )
  65. const (
  66. // DefaultImage is the name of the base image for the majority of tests that
  67. // are run across suites
  68. DefaultImage = "busybox"
  69. )
  70. func init() {
  71. reexec.Init()
  72. if dockerBin := os.Getenv("DOCKER_BINARY"); dockerBin != "" {
  73. dockerBinary = dockerBin
  74. }
  75. var err error
  76. dockerBinary, err = exec.LookPath(dockerBinary)
  77. if err != nil {
  78. fmt.Printf("ERROR: couldn't resolve full path to the Docker binary (%v)\n", err)
  79. os.Exit(1)
  80. }
  81. if registryImage := os.Getenv("REGISTRY_IMAGE"); registryImage != "" {
  82. registryImageName = registryImage
  83. }
  84. if registry := os.Getenv("REGISTRY_URL"); registry != "" {
  85. privateRegistryURL = registry
  86. }
  87. workingDirectory, _ = os.Getwd()
  88. // Deterministically working out the environment in which CI is running
  89. // to evaluate whether the daemon is local or remote is not possible through
  90. // a build tag.
  91. //
  92. // For example Windows to Linux CI under Jenkins tests the 64-bit
  93. // Windows binary build with the daemon build tag, but calls a remote
  94. // Linux daemon.
  95. //
  96. // We can't just say if Windows then assume the daemon is local as at
  97. // some point, we will be testing the Windows CLI against a Windows daemon.
  98. //
  99. // Similarly, it will be perfectly valid to also run CLI tests from
  100. // a Linux CLI (built with the daemon tag) against a Windows daemon.
  101. if len(os.Getenv("DOCKER_REMOTE_DAEMON")) > 0 {
  102. isLocalDaemon = false
  103. } else {
  104. isLocalDaemon = true
  105. }
  106. // TODO Windows CI. This are incorrect and need fixing into
  107. // platform specific pieces.
  108. // This is only used for a tests with local daemon true (Linux-only today)
  109. // default is "/var/lib/docker", but we'll try and ask the
  110. // /info endpoint for the specific root dir
  111. dockerBasePath = "/var/lib/docker"
  112. type Info struct {
  113. DockerRootDir string
  114. ExperimentalBuild bool
  115. }
  116. var i Info
  117. status, b, err := sockRequest("GET", "/info", nil)
  118. if err == nil && status == 200 {
  119. if err = json.Unmarshal(b, &i); err == nil {
  120. dockerBasePath = i.DockerRootDir
  121. experimentalDaemon = i.ExperimentalBuild
  122. }
  123. }
  124. volumesConfigPath = dockerBasePath + "/volumes"
  125. containerStoragePath = dockerBasePath + "/containers"
  126. if len(os.Getenv("WINDOWS_BASE_IMAGE")) > 0 {
  127. WindowsBaseImage = os.Getenv("WINDOWS_BASE_IMAGE")
  128. fmt.Println("INFO: Windows Base image is ", WindowsBaseImage)
  129. }
  130. dest := os.Getenv("DEST")
  131. b, err = ioutil.ReadFile(filepath.Join(dest, "docker.pid"))
  132. if err == nil {
  133. if p, err := strconv.ParseInt(string(b), 10, 32); err == nil {
  134. daemonPid = int(p)
  135. }
  136. }
  137. }