docker_test_vars.go 4.3 KB

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