docker_test_vars.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. daemonKernelVersion string
  65. )
  66. const (
  67. // DefaultImage is the name of the base image for the majority of tests that
  68. // are run across suites
  69. DefaultImage = "busybox"
  70. )
  71. func init() {
  72. reexec.Init()
  73. if dockerBin := os.Getenv("DOCKER_BINARY"); dockerBin != "" {
  74. dockerBinary = dockerBin
  75. }
  76. var err error
  77. dockerBinary, err = exec.LookPath(dockerBinary)
  78. if err != nil {
  79. fmt.Printf("ERROR: couldn't resolve full path to the Docker binary (%v)\n", err)
  80. os.Exit(1)
  81. }
  82. if registryImage := os.Getenv("REGISTRY_IMAGE"); registryImage != "" {
  83. registryImageName = registryImage
  84. }
  85. if registry := os.Getenv("REGISTRY_URL"); registry != "" {
  86. privateRegistryURL = registry
  87. }
  88. workingDirectory, _ = os.Getwd()
  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. if len(os.Getenv("DOCKER_REMOTE_DAEMON")) > 0 {
  103. isLocalDaemon = false
  104. } else {
  105. isLocalDaemon = true
  106. }
  107. // TODO Windows CI. This are incorrect and need fixing into
  108. // platform specific pieces.
  109. // This is only used for a tests with local daemon true (Linux-only today)
  110. // default is "/var/lib/docker", but we'll try and ask the
  111. // /info endpoint for the specific root dir
  112. dockerBasePath = "/var/lib/docker"
  113. type Info struct {
  114. DockerRootDir string
  115. ExperimentalBuild bool
  116. KernelVersion string
  117. }
  118. var i Info
  119. status, b, err := sockRequest("GET", "/info", nil)
  120. if err == nil && status == 200 {
  121. if err = json.Unmarshal(b, &i); err == nil {
  122. dockerBasePath = i.DockerRootDir
  123. experimentalDaemon = i.ExperimentalBuild
  124. daemonKernelVersion = i.KernelVersion
  125. }
  126. }
  127. volumesConfigPath = dockerBasePath + "/volumes"
  128. containerStoragePath = dockerBasePath + "/containers"
  129. if len(os.Getenv("WINDOWS_BASE_IMAGE")) > 0 {
  130. WindowsBaseImage = os.Getenv("WINDOWS_BASE_IMAGE")
  131. fmt.Println("INFO: Windows Base image is ", WindowsBaseImage)
  132. }
  133. dest := os.Getenv("DEST")
  134. b, err = ioutil.ReadFile(filepath.Join(dest, "docker.pid"))
  135. if err == nil {
  136. if p, err := strconv.ParseInt(string(b), 10, 32); err == nil {
  137. daemonPid = int(p)
  138. }
  139. }
  140. }