docker_test_vars.go 4.6 KB

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