12 "github.com/docker/docker/api/types"
13 "github.com/docker/docker/api/types/container"
14 "github.com/docker/docker/client"
15 "github.com/docker/docker/opts"
16 "golang.org/x/net/context"
20 // DefaultClientBinary is the name of the docker binary
21 DefaultClientBinary = os.Getenv("TEST_CLIENT_BINARY")
25 if DefaultClientBinary == "" {
26 // TODO: to be removed once we no longer depend on the docker cli for integration tests
27 //panic("TEST_CLIENT_BINARY must be set")
28 DefaultClientBinary = "docker"
32 // Execution holds informations about the test execution environment.
33 type Execution struct {
36 experimentalDaemon bool
37 daemonStorageDriver string
38 isolation container.Isolation
40 daemonKernelVersion string
41 // For a local daemon on Linux, these values will be used for testing
42 // user namespace support as the standard graph path(s) will be
43 // appended with the root remapped uid.gid prefix
45 volumesConfigPath string
46 containerStoragePath string
47 // baseImage is the name of the base image for testing
48 // Environment variable WINDOWS_BASE_IMAGE can override this
52 protectedElements protectedElements
55 // New creates a new Execution struct
56 func New() (*Execution, error) {
58 // Deterministically working out the environment in which CI is running
59 // to evaluate whether the daemon is local or remote is not possible through
62 // For example Windows to Linux CI under Jenkins tests the 64-bit
63 // Windows binary build with the daemon build tag, but calls a remote
66 // We can't just say if Windows then assume the daemon is local as at
67 // some point, we will be testing the Windows CLI against a Windows daemon.
69 // Similarly, it will be perfectly valid to also run CLI tests from
70 // a Linux CLI (built with the daemon tag) against a Windows daemon.
71 if len(os.Getenv("DOCKER_REMOTE_DAEMON")) > 0 {
74 info, err := getDaemonDockerInfo()
78 daemonPlatform := info.OSType
79 if daemonPlatform != "linux" && daemonPlatform != "windows" {
80 return nil, fmt.Errorf("Cannot run tests against platform: %s", daemonPlatform)
82 baseImage := "scratch"
83 volumesConfigPath := filepath.Join(info.DockerRootDir, "volumes")
84 containerStoragePath := filepath.Join(info.DockerRootDir, "containers")
85 // Make sure in context of daemon, not the local platform. Note we can't
86 // use filepath.FromSlash or ToSlash here as they are a no-op on Unix.
87 if daemonPlatform == "windows" {
88 volumesConfigPath = strings.Replace(volumesConfigPath, `/`, `\`, -1)
89 containerStoragePath = strings.Replace(containerStoragePath, `/`, `\`, -1)
91 baseImage = "microsoft/windowsservercore"
92 if len(os.Getenv("WINDOWS_BASE_IMAGE")) > 0 {
93 baseImage = os.Getenv("WINDOWS_BASE_IMAGE")
94 fmt.Println("INFO: Windows Base image is ", baseImage)
97 volumesConfigPath = strings.Replace(volumesConfigPath, `\`, `/`, -1)
98 containerStoragePath = strings.Replace(containerStoragePath, `\`, `/`, -1)
102 dest := os.Getenv("DEST")
103 b, err := ioutil.ReadFile(filepath.Join(dest, "docker.pid"))
105 if p, err := strconv.ParseInt(string(b), 10, 32); err == nil {
110 dockerBinary, err := exec.LookPath(DefaultClientBinary)
116 localDaemon: localDaemon,
117 daemonPlatform: daemonPlatform,
118 daemonStorageDriver: info.Driver,
119 daemonKernelVersion: info.KernelVersion,
120 dockerBasePath: info.DockerRootDir,
121 volumesConfigPath: volumesConfigPath,
122 containerStoragePath: containerStoragePath,
123 isolation: info.Isolation,
124 daemonPid: daemonPid,
125 experimentalDaemon: info.ExperimentalBuild,
126 baseImage: baseImage,
127 dockerBinary: dockerBinary,
128 protectedElements: protectedElements{
129 images: map[string]struct{}{},
133 func getDaemonDockerInfo() (types.Info, error) {
134 // FIXME(vdemeester) should be safe to use as is
135 client, err := client.NewEnvClient()
137 return types.Info{}, err
139 return client.Info(context.Background())
142 // LocalDaemon is true if the daemon under test is on the same
144 func (e *Execution) LocalDaemon() bool {
148 // DaemonPlatform is held globally so that tests can make intelligent
149 // decisions on how to configure themselves according to the platform
150 // of the daemon. This is initialized in docker_utils by sending
151 // a version call to the daemon and examining the response header.
152 func (e *Execution) DaemonPlatform() string {
153 return e.daemonPlatform
156 // DockerBasePath is the base path of the docker folder (by default it is -/var/run/docker)
157 func (e *Execution) DockerBasePath() string {
158 return e.dockerBasePath
161 // VolumesConfigPath is the path of the volume configuration for the testing daemon
162 func (e *Execution) VolumesConfigPath() string {
163 return e.volumesConfigPath
166 // ContainerStoragePath is the path where the container are stored for the testing daemon
167 func (e *Execution) ContainerStoragePath() string {
168 return e.containerStoragePath
171 // DaemonStorageDriver is held globally so that tests can know the storage
172 // driver of the daemon. This is initialized in docker_utils by sending
173 // a version call to the daemon and examining the response header.
174 func (e *Execution) DaemonStorageDriver() string {
175 return e.daemonStorageDriver
178 // Isolation is the isolation mode of the daemon under test
179 func (e *Execution) Isolation() container.Isolation {
183 // DaemonPID is the pid of the main test daemon
184 func (e *Execution) DaemonPID() int {
188 // ExperimentalDaemon tell whether the main daemon has
189 // experimental features enabled or not
190 func (e *Execution) ExperimentalDaemon() bool {
191 return e.experimentalDaemon
194 // MinimalBaseImage is the image used for minimal builds (it depends on the platform)
195 func (e *Execution) MinimalBaseImage() string {
199 // DaemonKernelVersion is the kernel version of the daemon as a string, as returned
200 // by an INFO call to the daemon.
201 func (e *Execution) DaemonKernelVersion() string {
202 return e.daemonKernelVersion
205 // DaemonKernelVersionNumeric is the kernel version of the daemon as an integer.
206 // Mostly useful on Windows where DaemonKernelVersion holds the full string such
207 // as `10.0 14393 (14393.447.amd64fre.rs1_release_inmarket.161102-0100)`, but
208 // integration tests really only need the `14393` piece to make decisions.
209 func (e *Execution) DaemonKernelVersionNumeric() int {
210 if e.daemonPlatform != "windows" {
213 v, _ := strconv.Atoi(strings.Split(e.daemonKernelVersion, " ")[1])
217 // DockerBinary returns the docker binary for this testing environment
218 func (e *Execution) DockerBinary() string {
219 return e.dockerBinary
222 // DaemonHost return the daemon host string for this test execution
223 func DaemonHost() string {
224 daemonURLStr := "unix://" + opts.DefaultUnixSocket
225 if daemonHostVar := os.Getenv("DOCKER_HOST"); daemonHostVar != "" {
226 daemonURLStr = daemonHostVar