2014-02-25 16:17:48 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-07-23 11:34:40 +00:00
|
|
|
"bufio"
|
2014-12-01 17:16:49 +00:00
|
|
|
"bytes"
|
2015-11-12 23:55:48 +00:00
|
|
|
"crypto/tls"
|
2014-12-01 17:16:49 +00:00
|
|
|
"encoding/json"
|
2014-07-23 18:12:42 +00:00
|
|
|
"errors"
|
2014-02-25 16:17:48 +00:00
|
|
|
"fmt"
|
2014-08-28 00:25:10 +00:00
|
|
|
"io"
|
2014-05-31 12:42:49 +00:00
|
|
|
"io/ioutil"
|
2014-07-23 18:12:42 +00:00
|
|
|
"net"
|
2014-05-31 12:42:49 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2014-07-23 18:12:42 +00:00
|
|
|
"net/http/httputil"
|
2015-01-15 00:08:28 +00:00
|
|
|
"net/url"
|
2014-05-31 12:42:49 +00:00
|
|
|
"os"
|
2014-02-25 16:17:48 +00:00
|
|
|
"os/exec"
|
2014-05-31 12:42:49 +00:00
|
|
|
"path"
|
2014-07-24 07:19:16 +00:00
|
|
|
"path/filepath"
|
2014-05-19 16:47:35 +00:00
|
|
|
"strconv"
|
2014-02-25 16:17:48 +00:00
|
|
|
"strings"
|
2014-07-23 18:12:42 +00:00
|
|
|
"time"
|
2015-02-14 10:27:31 +00:00
|
|
|
|
2015-06-03 19:21:38 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
2015-04-13 14:17:14 +00:00
|
|
|
"github.com/docker/docker/opts"
|
2015-07-31 22:16:25 +00:00
|
|
|
"github.com/docker/docker/pkg/httputils"
|
2015-09-13 08:41:00 +00:00
|
|
|
"github.com/docker/docker/pkg/integration"
|
2015-04-14 02:53:54 +00:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
2015-03-24 11:25:26 +00:00
|
|
|
"github.com/docker/docker/pkg/stringutils"
|
2015-12-30 00:27:12 +00:00
|
|
|
"github.com/docker/go-connections/sockets"
|
|
|
|
"github.com/docker/go-connections/tlsconfig"
|
2015-04-18 16:46:47 +00:00
|
|
|
"github.com/go-check/check"
|
2014-02-25 16:17:48 +00:00
|
|
|
)
|
|
|
|
|
2015-10-29 03:58:55 +00:00
|
|
|
func init() {
|
|
|
|
out, err := exec.Command(dockerBinary, "images").CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
lines := strings.Split(string(out), "\n")[1:]
|
|
|
|
for _, l := range lines {
|
|
|
|
if l == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fields := strings.Fields(l)
|
|
|
|
imgTag := fields[0] + ":" + fields[1]
|
|
|
|
// just for case if we have dangling images in tested daemon
|
|
|
|
if imgTag != "<none>:<none>" {
|
|
|
|
protectedImages[imgTag] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Obtain the daemon platform so that it can be used by tests to make
|
|
|
|
// intelligent decisions about how to configure themselves, and validate
|
|
|
|
// that the target platform is valid.
|
|
|
|
res, _, err := sockRequestRaw("GET", "/version", nil, "application/json")
|
|
|
|
if err != nil || res == nil || (res != nil && res.StatusCode != http.StatusOK) {
|
|
|
|
panic(fmt.Errorf("Init failed to get version: %v. Res=%v", err.Error(), res))
|
|
|
|
}
|
|
|
|
svrHeader, _ := httputils.ParseServerHeader(res.Header.Get("Server"))
|
|
|
|
daemonPlatform = svrHeader.OS
|
|
|
|
if daemonPlatform != "linux" && daemonPlatform != "windows" {
|
|
|
|
panic("Cannot run tests against platform: " + daemonPlatform)
|
|
|
|
}
|
|
|
|
|
|
|
|
// On Windows, extract out the version as we need to make selective
|
|
|
|
// decisions during integration testing as and when features are implemented.
|
|
|
|
if daemonPlatform == "windows" {
|
|
|
|
if body, err := ioutil.ReadAll(res.Body); err == nil {
|
|
|
|
var server types.Version
|
|
|
|
if err := json.Unmarshal(body, &server); err == nil {
|
|
|
|
// eg in "10.0 10550 (10550.1000.amd64fre.branch.date-time)" we want 10550
|
|
|
|
windowsDaemonKV, _ = strconv.Atoi(strings.Split(server.KernelVersion, " ")[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we know the daemon platform, can set paths used by tests.
|
|
|
|
_, body, err := sockRequest("GET", "/info", nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var info types.Info
|
|
|
|
err = json.Unmarshal(body, &info)
|
|
|
|
dockerBasePath = info.DockerRootDir
|
|
|
|
volumesConfigPath = filepath.Join(dockerBasePath, "volumes")
|
|
|
|
containerStoragePath = filepath.Join(dockerBasePath, "containers")
|
2015-10-29 22:51:04 +00:00
|
|
|
// Make sure in context of daemon, not the local platform. Note we can't
|
|
|
|
// use filepath.FromSlash or ToSlash here as they are a no-op on Unix.
|
|
|
|
if daemonPlatform == "windows" {
|
|
|
|
volumesConfigPath = strings.Replace(volumesConfigPath, `/`, `\`, -1)
|
|
|
|
containerStoragePath = strings.Replace(containerStoragePath, `/`, `\`, -1)
|
|
|
|
} else {
|
|
|
|
volumesConfigPath = strings.Replace(volumesConfigPath, `\`, `/`, -1)
|
|
|
|
containerStoragePath = strings.Replace(containerStoragePath, `\`, `/`, -1)
|
|
|
|
}
|
2015-10-29 03:58:55 +00:00
|
|
|
}
|
|
|
|
|
2014-07-23 18:12:42 +00:00
|
|
|
// Daemon represents a Docker daemon for the testing framework.
|
|
|
|
type Daemon struct {
|
2015-05-01 14:00:43 +00:00
|
|
|
// Defaults to "daemon"
|
2015-08-07 22:24:18 +00:00
|
|
|
// Useful to set to --daemon or -d for checking backwards compatibility
|
2015-05-01 14:00:43 +00:00
|
|
|
Command string
|
|
|
|
GlobalFlags []string
|
|
|
|
|
2015-10-19 13:17:37 +00:00
|
|
|
id string
|
|
|
|
c *check.C
|
|
|
|
logFile *os.File
|
|
|
|
folder string
|
|
|
|
root string
|
|
|
|
stdin io.WriteCloser
|
|
|
|
stdout, stderr io.ReadCloser
|
|
|
|
cmd *exec.Cmd
|
|
|
|
storageDriver string
|
|
|
|
wait chan error
|
|
|
|
userlandProxy bool
|
|
|
|
useDefaultHost bool
|
|
|
|
useDefaultTLSHost bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type clientConfig struct {
|
|
|
|
transport *http.Transport
|
|
|
|
scheme string
|
|
|
|
addr string
|
2015-04-07 23:53:39 +00:00
|
|
|
}
|
|
|
|
|
2014-07-23 18:12:42 +00:00
|
|
|
// NewDaemon returns a Daemon instance to be used for testing.
|
2015-06-07 20:32:29 +00:00
|
|
|
// This will create a directory such as d123456789 in the folder specified by $DEST.
|
2014-07-23 18:12:42 +00:00
|
|
|
// The daemon will not automatically start.
|
2015-04-18 16:46:47 +00:00
|
|
|
func NewDaemon(c *check.C) *Daemon {
|
2014-07-23 18:12:42 +00:00
|
|
|
dest := os.Getenv("DEST")
|
2015-10-11 07:33:24 +00:00
|
|
|
c.Assert(dest, check.Not(check.Equals), "", check.Commentf("Please set the DEST environment variable"))
|
2014-07-23 18:12:42 +00:00
|
|
|
|
2015-05-01 14:00:43 +00:00
|
|
|
id := fmt.Sprintf("d%d", time.Now().UnixNano()%100000000)
|
|
|
|
dir := filepath.Join(dest, id)
|
2014-07-23 18:12:42 +00:00
|
|
|
daemonFolder, err := filepath.Abs(dir)
|
2015-10-11 07:33:24 +00:00
|
|
|
c.Assert(err, check.IsNil, check.Commentf("Could not make %q an absolute path", dir))
|
2015-10-08 15:51:41 +00:00
|
|
|
daemonRoot := filepath.Join(daemonFolder, "root")
|
2014-07-23 18:12:42 +00:00
|
|
|
|
2015-10-11 07:33:24 +00:00
|
|
|
c.Assert(os.MkdirAll(daemonRoot, 0755), check.IsNil, check.Commentf("Could not create daemon root %q", dir))
|
2014-07-23 18:12:42 +00:00
|
|
|
|
2015-09-16 16:55:14 +00:00
|
|
|
userlandProxy := true
|
2015-04-07 23:53:39 +00:00
|
|
|
if env := os.Getenv("DOCKER_USERLANDPROXY"); env != "" {
|
|
|
|
if val, err := strconv.ParseBool(env); err != nil {
|
|
|
|
userlandProxy = val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-23 18:12:42 +00:00
|
|
|
return &Daemon{
|
2015-05-01 14:00:43 +00:00
|
|
|
Command: "daemon",
|
|
|
|
id: id,
|
2015-04-18 16:46:47 +00:00
|
|
|
c: c,
|
2014-07-23 18:12:42 +00:00
|
|
|
folder: daemonFolder,
|
2015-10-08 15:51:41 +00:00
|
|
|
root: daemonRoot,
|
2014-07-23 18:12:42 +00:00
|
|
|
storageDriver: os.Getenv("DOCKER_GRAPHDRIVER"),
|
2015-04-07 23:53:39 +00:00
|
|
|
userlandProxy: userlandProxy,
|
2014-07-23 18:12:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-19 13:17:37 +00:00
|
|
|
func (d *Daemon) getClientConfig() (*clientConfig, error) {
|
|
|
|
var (
|
|
|
|
transport *http.Transport
|
|
|
|
scheme string
|
|
|
|
addr string
|
|
|
|
proto string
|
|
|
|
)
|
|
|
|
if d.useDefaultTLSHost {
|
|
|
|
option := &tlsconfig.Options{
|
|
|
|
CAFile: "fixtures/https/ca.pem",
|
|
|
|
CertFile: "fixtures/https/client-cert.pem",
|
|
|
|
KeyFile: "fixtures/https/client-key.pem",
|
|
|
|
}
|
|
|
|
tlsConfig, err := tlsconfig.Client(*option)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
transport = &http.Transport{
|
|
|
|
TLSClientConfig: tlsConfig,
|
|
|
|
}
|
|
|
|
addr = fmt.Sprintf("%s:%d", opts.DefaultHTTPHost, opts.DefaultTLSHTTPPort)
|
|
|
|
scheme = "https"
|
|
|
|
proto = "tcp"
|
|
|
|
} else if d.useDefaultHost {
|
|
|
|
addr = opts.DefaultUnixSocket
|
|
|
|
proto = "unix"
|
|
|
|
scheme = "http"
|
|
|
|
transport = &http.Transport{}
|
|
|
|
} else {
|
|
|
|
addr = filepath.Join(d.folder, "docker.sock")
|
|
|
|
proto = "unix"
|
|
|
|
scheme = "http"
|
|
|
|
transport = &http.Transport{}
|
|
|
|
}
|
|
|
|
|
|
|
|
sockets.ConfigureTCPTransport(transport, proto, addr)
|
|
|
|
|
|
|
|
return &clientConfig{
|
|
|
|
transport: transport,
|
|
|
|
scheme: scheme,
|
|
|
|
addr: addr,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2014-07-23 18:12:42 +00:00
|
|
|
// Start will start the daemon and return once it is ready to receive requests.
|
2014-09-04 00:12:36 +00:00
|
|
|
// You can specify additional daemon flags.
|
2014-07-23 18:12:42 +00:00
|
|
|
func (d *Daemon) Start(arg ...string) error {
|
|
|
|
dockerBinary, err := exec.LookPath(dockerBinary)
|
2015-10-11 07:33:24 +00:00
|
|
|
d.c.Assert(err, check.IsNil, check.Commentf("[%s] could not find docker binary in $PATH", d.id))
|
2014-07-23 18:12:42 +00:00
|
|
|
|
2015-05-01 14:00:43 +00:00
|
|
|
args := append(d.GlobalFlags,
|
|
|
|
d.Command,
|
2015-10-08 15:51:41 +00:00
|
|
|
"--graph", d.root,
|
2014-07-23 18:12:42 +00:00
|
|
|
"--pidfile", fmt.Sprintf("%s/docker.pid", d.folder),
|
2015-04-07 23:53:39 +00:00
|
|
|
fmt.Sprintf("--userland-proxy=%t", d.userlandProxy),
|
2015-05-01 14:00:43 +00:00
|
|
|
)
|
2015-10-19 13:17:37 +00:00
|
|
|
if !(d.useDefaultHost || d.useDefaultTLSHost) {
|
2015-10-12 08:49:25 +00:00
|
|
|
args = append(args, []string{"--host", d.sock()}...)
|
|
|
|
}
|
2015-09-18 17:41:12 +00:00
|
|
|
if root := os.Getenv("DOCKER_REMAP_ROOT"); root != "" {
|
2015-10-10 00:52:45 +00:00
|
|
|
args = append(args, []string{"--userns-remap", root}...)
|
2015-09-18 17:41:12 +00:00
|
|
|
}
|
|
|
|
|
2014-10-01 13:07:24 +00:00
|
|
|
// If we don't explicitly set the log-level or debug flag(-D) then
|
|
|
|
// turn on debug mode
|
|
|
|
foundIt := false
|
|
|
|
for _, a := range arg {
|
2015-05-01 14:00:43 +00:00
|
|
|
if strings.Contains(a, "--log-level") || strings.Contains(a, "-D") || strings.Contains(a, "--debug") {
|
2014-10-01 13:07:24 +00:00
|
|
|
foundIt = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !foundIt {
|
|
|
|
args = append(args, "--debug")
|
|
|
|
}
|
|
|
|
|
2014-09-04 00:12:36 +00:00
|
|
|
if d.storageDriver != "" {
|
|
|
|
args = append(args, "--storage-driver", d.storageDriver)
|
|
|
|
}
|
|
|
|
|
2014-07-23 18:12:42 +00:00
|
|
|
args = append(args, arg...)
|
|
|
|
d.cmd = exec.Command(dockerBinary, args...)
|
|
|
|
|
2014-10-01 13:07:24 +00:00
|
|
|
d.logFile, err = os.OpenFile(filepath.Join(d.folder, "docker.log"), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
|
2015-10-11 07:33:24 +00:00
|
|
|
d.c.Assert(err, check.IsNil, check.Commentf("[%s] Could not create %s/docker.log", d.id, d.folder))
|
2014-07-23 18:12:42 +00:00
|
|
|
|
|
|
|
d.cmd.Stdout = d.logFile
|
|
|
|
d.cmd.Stderr = d.logFile
|
|
|
|
|
|
|
|
if err := d.cmd.Start(); err != nil {
|
2015-05-01 14:00:43 +00:00
|
|
|
return fmt.Errorf("[%s] could not start daemon container: %v", d.id, err)
|
2014-07-23 18:12:42 +00:00
|
|
|
}
|
|
|
|
|
2014-09-03 22:56:58 +00:00
|
|
|
wait := make(chan error)
|
2014-07-23 18:12:42 +00:00
|
|
|
|
|
|
|
go func() {
|
2014-09-03 22:56:58 +00:00
|
|
|
wait <- d.cmd.Wait()
|
2015-05-01 14:00:43 +00:00
|
|
|
d.c.Logf("[%s] exiting daemon", d.id)
|
2014-09-03 22:56:58 +00:00
|
|
|
close(wait)
|
2014-07-23 18:12:42 +00:00
|
|
|
}()
|
|
|
|
|
2014-09-03 22:56:58 +00:00
|
|
|
d.wait = wait
|
|
|
|
|
2014-07-23 18:12:42 +00:00
|
|
|
tick := time.Tick(500 * time.Millisecond)
|
|
|
|
// make sure daemon is ready to receive requests
|
2014-10-01 13:07:24 +00:00
|
|
|
startTime := time.Now().Unix()
|
2014-07-23 18:12:42 +00:00
|
|
|
for {
|
2015-05-01 14:00:43 +00:00
|
|
|
d.c.Logf("[%s] waiting for daemon to start", d.id)
|
2014-10-01 13:07:24 +00:00
|
|
|
if time.Now().Unix()-startTime > 5 {
|
|
|
|
// After 5 seconds, give up
|
2015-05-01 14:00:43 +00:00
|
|
|
return fmt.Errorf("[%s] Daemon exited and never started", d.id)
|
2014-10-01 13:07:24 +00:00
|
|
|
}
|
2014-07-23 18:12:42 +00:00
|
|
|
select {
|
|
|
|
case <-time.After(2 * time.Second):
|
2015-05-01 14:00:43 +00:00
|
|
|
return fmt.Errorf("[%s] timeout: daemon does not respond", d.id)
|
2014-07-23 18:12:42 +00:00
|
|
|
case <-tick:
|
2015-10-19 13:17:37 +00:00
|
|
|
clientConfig, err := d.getClientConfig()
|
2014-07-23 18:12:42 +00:00
|
|
|
if err != nil {
|
2015-10-19 13:17:37 +00:00
|
|
|
return err
|
2014-07-23 18:12:42 +00:00
|
|
|
}
|
|
|
|
|
2015-10-19 13:17:37 +00:00
|
|
|
client := &http.Client{
|
|
|
|
Transport: clientConfig.transport,
|
|
|
|
}
|
2014-07-23 18:12:42 +00:00
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", "/_ping", nil)
|
2015-10-11 07:33:24 +00:00
|
|
|
d.c.Assert(err, check.IsNil, check.Commentf("[%s] could not create new request", d.id))
|
2015-10-19 13:17:37 +00:00
|
|
|
req.URL.Host = clientConfig.addr
|
|
|
|
req.URL.Scheme = clientConfig.scheme
|
2014-07-23 18:12:42 +00:00
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2015-05-01 14:00:43 +00:00
|
|
|
d.c.Logf("[%s] received status != 200 OK: %s", d.id, resp.Status)
|
2014-07-23 18:12:42 +00:00
|
|
|
}
|
2015-05-01 14:00:43 +00:00
|
|
|
d.c.Logf("[%s] daemon started", d.id)
|
2015-10-08 15:51:41 +00:00
|
|
|
d.root, err = d.queryRootDir()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("[%s] error querying daemon for root directory: %v", d.id, err)
|
|
|
|
}
|
2014-07-23 18:12:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartWithBusybox will first start the daemon with Daemon.Start()
|
|
|
|
// then save the busybox image from the main daemon and load it into this Daemon instance.
|
|
|
|
func (d *Daemon) StartWithBusybox(arg ...string) error {
|
|
|
|
if err := d.Start(arg...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
bb := filepath.Join(d.folder, "busybox.tar")
|
|
|
|
if _, err := os.Stat(bb); err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return fmt.Errorf("unexpected error on busybox.tar stat: %v", err)
|
|
|
|
}
|
|
|
|
// saving busybox image from main daemon
|
|
|
|
if err := exec.Command(dockerBinary, "save", "--output", bb, "busybox:latest").Run(); err != nil {
|
|
|
|
return fmt.Errorf("could not save busybox image: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// loading busybox image to this daemon
|
2015-12-24 15:18:23 +00:00
|
|
|
if out, err := d.Cmd("load", "--input", bb); err != nil {
|
|
|
|
return fmt.Errorf("could not load busybox image: %s", out)
|
2014-07-23 18:12:42 +00:00
|
|
|
}
|
|
|
|
if err := os.Remove(bb); err != nil {
|
2015-12-24 15:18:23 +00:00
|
|
|
d.c.Logf("could not remove %s: %v", bb, err)
|
2014-07-23 18:12:42 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop will send a SIGINT every second and wait for the daemon to stop.
|
|
|
|
// If it timeouts, a SIGKILL is sent.
|
|
|
|
// Stop will not delete the daemon directory. If a purged daemon is needed,
|
|
|
|
// instantiate a new one with NewDaemon.
|
|
|
|
func (d *Daemon) Stop() error {
|
|
|
|
if d.cmd == nil || d.wait == nil {
|
2014-10-06 14:26:55 +00:00
|
|
|
return errors.New("daemon not started")
|
2014-07-23 18:12:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
d.logFile.Close()
|
|
|
|
d.cmd = nil
|
|
|
|
}()
|
|
|
|
|
|
|
|
i := 1
|
|
|
|
tick := time.Tick(time.Second)
|
|
|
|
|
|
|
|
if err := d.cmd.Process.Signal(os.Interrupt); err != nil {
|
2014-10-06 14:26:55 +00:00
|
|
|
return fmt.Errorf("could not send signal: %v", err)
|
2014-07-23 18:12:42 +00:00
|
|
|
}
|
2014-12-23 21:15:19 +00:00
|
|
|
out1:
|
2014-07-23 18:12:42 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case err := <-d.wait:
|
|
|
|
return err
|
2014-12-23 21:15:19 +00:00
|
|
|
case <-time.After(15 * time.Second):
|
|
|
|
// time for stopping jobs and run onShutdown hooks
|
2015-04-18 16:46:47 +00:00
|
|
|
d.c.Log("timeout")
|
2014-12-23 21:15:19 +00:00
|
|
|
break out1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out2:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case err := <-d.wait:
|
|
|
|
return err
|
2014-07-23 18:12:42 +00:00
|
|
|
case <-tick:
|
2014-12-23 21:15:19 +00:00
|
|
|
i++
|
|
|
|
if i > 4 {
|
2015-04-18 16:46:47 +00:00
|
|
|
d.c.Logf("tried to interrupt daemon for %d times, now try to kill it", i)
|
2014-12-23 21:15:19 +00:00
|
|
|
break out2
|
|
|
|
}
|
2015-04-18 16:46:47 +00:00
|
|
|
d.c.Logf("Attempt #%d: daemon is still running with pid %d", i, d.cmd.Process.Pid)
|
2014-07-23 18:12:42 +00:00
|
|
|
if err := d.cmd.Process.Signal(os.Interrupt); err != nil {
|
2014-10-06 14:26:55 +00:00
|
|
|
return fmt.Errorf("could not send signal: %v", err)
|
2014-07-23 18:12:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := d.cmd.Process.Kill(); err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
d.c.Logf("Could not kill daemon: %v", err)
|
2014-07-23 18:12:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restart will restart the daemon by first stopping it and then starting it.
|
|
|
|
func (d *Daemon) Restart(arg ...string) error {
|
|
|
|
d.Stop()
|
2015-10-30 20:39:04 +00:00
|
|
|
// in the case of tests running a user namespace-enabled daemon, we have resolved
|
|
|
|
// d.root to be the actual final path of the graph dir after the "uid.gid" of
|
|
|
|
// remapped root is added--we need to subtract it from the path before calling
|
|
|
|
// start or else we will continue making subdirectories rather than truly restarting
|
|
|
|
// with the same location/root:
|
|
|
|
if root := os.Getenv("DOCKER_REMAP_ROOT"); root != "" {
|
|
|
|
d.root = filepath.Dir(d.root)
|
|
|
|
}
|
2014-07-23 18:12:42 +00:00
|
|
|
return d.Start(arg...)
|
|
|
|
}
|
|
|
|
|
2015-10-08 15:51:41 +00:00
|
|
|
func (d *Daemon) queryRootDir() (string, error) {
|
|
|
|
// update daemon root by asking /info endpoint (to support user
|
|
|
|
// namespaced daemon with root remapped uid.gid directory)
|
2015-10-19 13:17:37 +00:00
|
|
|
clientConfig, err := d.getClientConfig()
|
2015-10-08 15:51:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-10-19 13:17:37 +00:00
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: clientConfig.transport,
|
|
|
|
}
|
2015-10-08 15:51:41 +00:00
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", "/info", nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
2015-10-19 13:17:37 +00:00
|
|
|
req.URL.Host = clientConfig.addr
|
|
|
|
req.URL.Scheme = clientConfig.scheme
|
2015-10-08 15:51:41 +00:00
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
body := ioutils.NewReadCloserWrapper(resp.Body, func() error {
|
|
|
|
return resp.Body.Close()
|
|
|
|
})
|
|
|
|
|
|
|
|
type Info struct {
|
|
|
|
DockerRootDir string
|
|
|
|
}
|
|
|
|
var b []byte
|
|
|
|
var i Info
|
|
|
|
b, err = readBody(body)
|
|
|
|
if err == nil && resp.StatusCode == 200 {
|
|
|
|
// read the docker root dir
|
|
|
|
if err = json.Unmarshal(b, &i); err == nil {
|
|
|
|
return i.DockerRootDir, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2014-07-23 18:12:42 +00:00
|
|
|
func (d *Daemon) sock() string {
|
|
|
|
return fmt.Sprintf("unix://%s/docker.sock", d.folder)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cmd will execute a docker CLI command against this Daemon.
|
|
|
|
// Example: d.Cmd("version") will run docker -H unix://path/to/unix.sock version
|
|
|
|
func (d *Daemon) Cmd(name string, arg ...string) (string, error) {
|
|
|
|
args := []string{"--host", d.sock(), name}
|
|
|
|
args = append(args, arg...)
|
|
|
|
c := exec.Command(dockerBinary, args...)
|
|
|
|
b, err := c.CombinedOutput()
|
|
|
|
return string(b), err
|
|
|
|
}
|
|
|
|
|
2015-07-22 12:59:24 +00:00
|
|
|
// CmdWithArgs will execute a docker CLI command against a daemon with the
|
|
|
|
// given additional arguments
|
2015-04-20 23:21:46 +00:00
|
|
|
func (d *Daemon) CmdWithArgs(daemonArgs []string, name string, arg ...string) (string, error) {
|
|
|
|
args := append(daemonArgs, name)
|
|
|
|
args = append(args, arg...)
|
|
|
|
c := exec.Command(dockerBinary, args...)
|
|
|
|
b, err := c.CombinedOutput()
|
|
|
|
return string(b), err
|
|
|
|
}
|
|
|
|
|
2015-07-22 12:59:24 +00:00
|
|
|
// LogfileName returns the path the the daemon's log file
|
2015-03-11 14:33:06 +00:00
|
|
|
func (d *Daemon) LogfileName() string {
|
|
|
|
return d.logFile.Name()
|
|
|
|
}
|
|
|
|
|
2015-01-15 00:08:28 +00:00
|
|
|
func daemonHost() string {
|
2015-07-22 12:59:24 +00:00
|
|
|
daemonURLStr := "unix://" + opts.DefaultUnixSocket
|
2015-02-14 10:27:31 +00:00
|
|
|
if daemonHostVar := os.Getenv("DOCKER_HOST"); daemonHostVar != "" {
|
2015-07-22 12:59:24 +00:00
|
|
|
daemonURLStr = daemonHostVar
|
2015-01-15 00:08:28 +00:00
|
|
|
}
|
2015-07-22 12:59:24 +00:00
|
|
|
return daemonURLStr
|
2015-01-15 00:08:28 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 23:55:48 +00:00
|
|
|
func getTLSConfig() (*tls.Config, error) {
|
|
|
|
dockerCertPath := os.Getenv("DOCKER_CERT_PATH")
|
|
|
|
|
|
|
|
if dockerCertPath == "" {
|
|
|
|
return nil, fmt.Errorf("DOCKER_TLS_VERIFY specified, but no DOCKER_CERT_PATH environment variable")
|
|
|
|
}
|
|
|
|
|
|
|
|
option := &tlsconfig.Options{
|
|
|
|
CAFile: filepath.Join(dockerCertPath, "ca.pem"),
|
|
|
|
CertFile: filepath.Join(dockerCertPath, "cert.pem"),
|
|
|
|
KeyFile: filepath.Join(dockerCertPath, "key.pem"),
|
|
|
|
}
|
|
|
|
tlsConfig, err := tlsconfig.Client(*option)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tlsConfig, nil
|
|
|
|
}
|
|
|
|
|
2015-02-12 19:51:28 +00:00
|
|
|
func sockConn(timeout time.Duration) (net.Conn, error) {
|
2015-01-15 00:08:28 +00:00
|
|
|
daemon := daemonHost()
|
2015-07-22 12:59:24 +00:00
|
|
|
daemonURL, err := url.Parse(daemon)
|
2015-01-15 00:08:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not parse url %q: %v", daemon, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var c net.Conn
|
2015-07-22 12:59:24 +00:00
|
|
|
switch daemonURL.Scheme {
|
2015-01-15 00:08:28 +00:00
|
|
|
case "unix":
|
2015-07-22 12:59:24 +00:00
|
|
|
return net.DialTimeout(daemonURL.Scheme, daemonURL.Path, timeout)
|
2015-01-15 00:08:28 +00:00
|
|
|
case "tcp":
|
2015-11-12 23:55:48 +00:00
|
|
|
if os.Getenv("DOCKER_TLS_VERIFY") != "" {
|
|
|
|
// Setup the socket TLS configuration.
|
|
|
|
tlsConfig, err := getTLSConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dialer := &net.Dialer{Timeout: timeout}
|
|
|
|
return tls.DialWithDialer(dialer, daemonURL.Scheme, daemonURL.Host, tlsConfig)
|
|
|
|
}
|
2015-07-22 12:59:24 +00:00
|
|
|
return net.DialTimeout(daemonURL.Scheme, daemonURL.Host, timeout)
|
2015-01-15 00:08:28 +00:00
|
|
|
default:
|
2015-07-22 12:59:24 +00:00
|
|
|
return c, fmt.Errorf("unknown scheme %v (%s)", daemonURL.Scheme, daemon)
|
2015-02-12 19:51:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-11 21:49:14 +00:00
|
|
|
func sockRequest(method, endpoint string, data interface{}) (int, []byte, error) {
|
2015-02-12 19:51:28 +00:00
|
|
|
jsonData := bytes.NewBuffer(nil)
|
|
|
|
if err := json.NewEncoder(jsonData).Encode(data); err != nil {
|
2015-04-11 21:49:14 +00:00
|
|
|
return -1, nil, err
|
2015-01-15 00:08:28 +00:00
|
|
|
}
|
2015-02-12 19:51:28 +00:00
|
|
|
|
2015-04-27 16:33:08 +00:00
|
|
|
res, body, err := sockRequestRaw(method, endpoint, jsonData, "application/json")
|
2015-04-14 02:53:54 +00:00
|
|
|
if err != nil {
|
2015-07-22 23:01:32 +00:00
|
|
|
return -1, nil, err
|
2015-04-14 02:53:54 +00:00
|
|
|
}
|
2015-07-22 23:01:32 +00:00
|
|
|
b, err := readBody(body)
|
2015-04-27 16:33:08 +00:00
|
|
|
return res.StatusCode, b, err
|
2015-02-12 19:51:28 +00:00
|
|
|
}
|
|
|
|
|
2015-04-27 16:33:08 +00:00
|
|
|
func sockRequestRaw(method, endpoint string, data io.Reader, ct string) (*http.Response, io.ReadCloser, error) {
|
2015-07-23 11:34:40 +00:00
|
|
|
req, client, err := newRequestClient(method, endpoint, data, ct)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
client.Close()
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
body := ioutils.NewReadCloserWrapper(resp.Body, func() error {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
return client.Close()
|
|
|
|
})
|
|
|
|
|
|
|
|
return resp, body, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func sockRequestHijack(method, endpoint string, data io.Reader, ct string) (net.Conn, *bufio.Reader, error) {
|
|
|
|
req, client, err := newRequestClient(method, endpoint, data, ct)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
client.Do(req)
|
|
|
|
conn, br := client.Hijack()
|
|
|
|
return conn, br, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRequestClient(method, endpoint string, data io.Reader, ct string) (*http.Request, *httputil.ClientConn, error) {
|
2015-02-12 19:51:28 +00:00
|
|
|
c, err := sockConn(time.Duration(10 * time.Second))
|
2014-09-05 21:35:55 +00:00
|
|
|
if err != nil {
|
2015-04-27 16:33:08 +00:00
|
|
|
return nil, nil, fmt.Errorf("could not dial docker daemon: %v", err)
|
2014-09-05 21:35:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
client := httputil.NewClientConn(c, nil)
|
|
|
|
|
2015-01-21 19:08:19 +00:00
|
|
|
req, err := http.NewRequest(method, endpoint, data)
|
2014-09-05 21:35:55 +00:00
|
|
|
if err != nil {
|
2015-04-14 02:53:54 +00:00
|
|
|
client.Close()
|
2015-04-27 16:33:08 +00:00
|
|
|
return nil, nil, fmt.Errorf("could not create new request: %v", err)
|
2014-09-05 21:35:55 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 01:55:04 +00:00
|
|
|
if ct != "" {
|
|
|
|
req.Header.Set("Content-Type", ct)
|
2015-01-21 19:08:19 +00:00
|
|
|
}
|
2015-07-23 11:34:40 +00:00
|
|
|
return req, client, nil
|
2015-04-14 02:53:54 +00:00
|
|
|
}
|
2015-04-11 21:49:14 +00:00
|
|
|
|
2015-04-14 02:53:54 +00:00
|
|
|
func readBody(b io.ReadCloser) ([]byte, error) {
|
|
|
|
defer b.Close()
|
|
|
|
return ioutil.ReadAll(b)
|
2014-09-05 21:35:55 +00:00
|
|
|
}
|
|
|
|
|
2014-02-25 16:17:48 +00:00
|
|
|
func deleteContainer(container string) error {
|
2015-04-08 23:20:42 +00:00
|
|
|
container = strings.TrimSpace(strings.Replace(container, "\n", " ", -1))
|
2015-04-24 18:05:17 +00:00
|
|
|
rmArgs := strings.Split(fmt.Sprintf("rm -fv %v", container), " ")
|
2015-04-08 23:20:42 +00:00
|
|
|
exitCode, err := runCommand(exec.Command(dockerBinary, rmArgs...))
|
2014-02-25 16:17:48 +00:00
|
|
|
// set error manually if not set
|
|
|
|
if exitCode != 0 && err == nil {
|
|
|
|
err = fmt.Errorf("failed to remove container: `docker rm` exit is non-zero")
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAllContainers() (string, error) {
|
|
|
|
getContainersCmd := exec.Command(dockerBinary, "ps", "-q", "-a")
|
|
|
|
out, exitCode, err := runCommandWithOutput(getContainersCmd)
|
|
|
|
if exitCode != 0 && err == nil {
|
|
|
|
err = fmt.Errorf("failed to get a list of containers: %v\n", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteAllContainers() error {
|
|
|
|
containers, err := getAllContainers()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(containers)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-12-02 18:48:59 +00:00
|
|
|
if containers != "" {
|
|
|
|
if err = deleteContainer(containers); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-02-25 16:17:48 +00:00
|
|
|
}
|
|
|
|
return nil
|
2015-10-13 21:18:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func deleteAllNetworks() error {
|
|
|
|
networks, err := getAllNetworks()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var errors []string
|
|
|
|
for _, n := range networks {
|
2015-10-30 15:05:33 +00:00
|
|
|
if n.Name == "bridge" || n.Name == "none" || n.Name == "host" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
status, b, err := sockRequest("DELETE", "/networks/"+n.Name, nil)
|
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if status != http.StatusNoContent {
|
|
|
|
errors = append(errors, fmt.Sprintf("error deleting network %s: %s", n.Name, string(b)))
|
2015-10-13 21:18:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(errors) > 0 {
|
|
|
|
return fmt.Errorf(strings.Join(errors, "\n"))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAllNetworks() ([]types.NetworkResource, error) {
|
|
|
|
var networks []types.NetworkResource
|
|
|
|
_, b, err := sockRequest("GET", "/networks", nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(b, &networks); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return networks, nil
|
2014-02-25 16:17:48 +00:00
|
|
|
}
|
|
|
|
|
2015-06-12 13:25:32 +00:00
|
|
|
func deleteAllVolumes() error {
|
|
|
|
volumes, err := getAllVolumes()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var errors []string
|
|
|
|
for _, v := range volumes {
|
|
|
|
status, b, err := sockRequest("DELETE", "/volumes/"+v.Name, nil)
|
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if status != http.StatusNoContent {
|
|
|
|
errors = append(errors, fmt.Sprintf("error deleting volume %s: %s", v.Name, string(b)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(errors) > 0 {
|
|
|
|
return fmt.Errorf(strings.Join(errors, "\n"))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAllVolumes() ([]*types.Volume, error) {
|
|
|
|
var volumes types.VolumesListResponse
|
|
|
|
_, b, err := sockRequest("GET", "/volumes", nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(b, &volumes); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return volumes.Volumes, nil
|
|
|
|
}
|
|
|
|
|
2015-04-23 21:39:31 +00:00
|
|
|
var protectedImages = map[string]struct{}{}
|
|
|
|
|
|
|
|
func deleteAllImages() error {
|
|
|
|
out, err := exec.Command(dockerBinary, "images").CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
lines := strings.Split(string(out), "\n")[1:]
|
|
|
|
var imgs []string
|
|
|
|
for _, l := range lines {
|
|
|
|
if l == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fields := strings.Fields(l)
|
|
|
|
imgTag := fields[0] + ":" + fields[1]
|
|
|
|
if _, ok := protectedImages[imgTag]; !ok {
|
|
|
|
if fields[0] == "<none>" {
|
|
|
|
imgs = append(imgs, fields[2])
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
imgs = append(imgs, imgTag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(imgs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
args := append([]string{"rmi", "-f"}, imgs...)
|
|
|
|
if err := exec.Command(dockerBinary, args...).Run(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-27 16:08:39 +00:00
|
|
|
func getPausedContainers() (string, error) {
|
|
|
|
getPausedContainersCmd := exec.Command(dockerBinary, "ps", "-f", "status=paused", "-q", "-a")
|
|
|
|
out, exitCode, err := runCommandWithOutput(getPausedContainersCmd)
|
|
|
|
if exitCode != 0 && err == nil {
|
|
|
|
err = fmt.Errorf("failed to get a list of paused containers: %v\n", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, err
|
|
|
|
}
|
|
|
|
|
2015-01-31 00:23:47 +00:00
|
|
|
func getSliceOfPausedContainers() ([]string, error) {
|
|
|
|
out, err := getPausedContainers()
|
|
|
|
if err == nil {
|
2015-04-10 01:14:01 +00:00
|
|
|
if len(out) == 0 {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-01-31 00:23:47 +00:00
|
|
|
slice := strings.Split(strings.TrimSpace(out), "\n")
|
|
|
|
return slice, err
|
|
|
|
}
|
2015-03-23 19:21:37 +00:00
|
|
|
return []string{out}, err
|
2015-01-31 00:23:47 +00:00
|
|
|
}
|
|
|
|
|
2014-11-27 16:08:39 +00:00
|
|
|
func unpauseContainer(container string) error {
|
|
|
|
unpauseCmd := exec.Command(dockerBinary, "unpause", container)
|
|
|
|
exitCode, err := runCommand(unpauseCmd)
|
|
|
|
if exitCode != 0 && err == nil {
|
|
|
|
err = fmt.Errorf("failed to unpause container")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func unpauseAllContainers() error {
|
|
|
|
containers, err := getPausedContainers()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(containers)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
containers = strings.Replace(containers, "\n", " ", -1)
|
|
|
|
containers = strings.Trim(containers, " ")
|
|
|
|
containerList := strings.Split(containers, " ")
|
|
|
|
|
|
|
|
for _, value := range containerList {
|
|
|
|
if err = unpauseContainer(value); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-07 22:39:50 +00:00
|
|
|
func deleteImages(images ...string) error {
|
2015-04-23 13:20:17 +00:00
|
|
|
args := []string{"rmi", "-f"}
|
2014-11-17 16:06:48 +00:00
|
|
|
args = append(args, images...)
|
|
|
|
rmiCmd := exec.Command(dockerBinary, args...)
|
2014-02-25 16:17:48 +00:00
|
|
|
exitCode, err := runCommand(rmiCmd)
|
|
|
|
// set error manually if not set
|
|
|
|
if exitCode != 0 && err == nil {
|
|
|
|
err = fmt.Errorf("failed to remove image: `docker rmi` exit is non-zero")
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2014-04-04 00:57:41 +00:00
|
|
|
|
2014-06-30 19:22:29 +00:00
|
|
|
func imageExists(image string) error {
|
|
|
|
inspectCmd := exec.Command(dockerBinary, "inspect", image)
|
|
|
|
exitCode, err := runCommand(inspectCmd)
|
|
|
|
if exitCode != 0 && err == nil {
|
2014-10-14 17:59:38 +00:00
|
|
|
err = fmt.Errorf("couldn't find image %q", image)
|
2014-06-30 19:22:29 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-06 18:38:23 +00:00
|
|
|
func pullImageIfNotExist(image string) error {
|
2014-06-30 19:42:09 +00:00
|
|
|
if err := imageExists(image); err != nil {
|
|
|
|
pullCmd := exec.Command(dockerBinary, "pull", image)
|
|
|
|
_, exitCode, err := runCommandWithOutput(pullCmd)
|
|
|
|
|
|
|
|
if err != nil || exitCode != 0 {
|
2015-10-06 18:38:23 +00:00
|
|
|
return fmt.Errorf("image %q wasn't found locally and it couldn't be pulled: %s", image, err)
|
2014-06-30 19:42:09 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-06 18:38:23 +00:00
|
|
|
return nil
|
2014-06-30 19:42:09 +00:00
|
|
|
}
|
|
|
|
|
2015-07-27 18:13:25 +00:00
|
|
|
func dockerCmdWithError(args ...string) (string, int, error) {
|
2015-09-13 08:41:00 +00:00
|
|
|
return integration.DockerCmdWithError(dockerBinary, args...)
|
2015-07-14 06:35:06 +00:00
|
|
|
}
|
|
|
|
|
2015-07-18 02:48:28 +00:00
|
|
|
func dockerCmdWithStdoutStderr(c *check.C, args ...string) (string, string, int) {
|
2015-09-13 08:41:00 +00:00
|
|
|
return integration.DockerCmdWithStdoutStderr(dockerBinary, c, args...)
|
2015-07-18 02:48:28 +00:00
|
|
|
}
|
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func dockerCmd(c *check.C, args ...string) (string, int) {
|
2015-09-13 08:41:00 +00:00
|
|
|
return integration.DockerCmd(dockerBinary, c, args...)
|
2014-04-04 00:57:41 +00:00
|
|
|
}
|
2014-05-10 17:27:24 +00:00
|
|
|
|
2015-03-12 13:21:58 +00:00
|
|
|
// execute a docker command with a timeout
|
2014-08-13 16:02:04 +00:00
|
|
|
func dockerCmdWithTimeout(timeout time.Duration, args ...string) (string, int, error) {
|
2015-09-13 08:41:00 +00:00
|
|
|
return integration.DockerCmdWithTimeout(dockerBinary, timeout, args...)
|
2014-08-13 16:02:04 +00:00
|
|
|
}
|
|
|
|
|
2014-07-22 22:54:56 +00:00
|
|
|
// execute a docker command in a directory
|
2015-04-18 16:46:47 +00:00
|
|
|
func dockerCmdInDir(c *check.C, path string, args ...string) (string, int, error) {
|
2015-09-13 08:41:00 +00:00
|
|
|
return integration.DockerCmdInDir(dockerBinary, path, args...)
|
2014-08-13 16:02:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// execute a docker command in a directory with a timeout
|
|
|
|
func dockerCmdInDirWithTimeout(timeout time.Duration, path string, args ...string) (string, int, error) {
|
2015-09-13 08:41:00 +00:00
|
|
|
return integration.DockerCmdInDirWithTimeout(dockerBinary, timeout, path, args...)
|
2014-07-22 22:54:56 +00:00
|
|
|
}
|
|
|
|
|
2015-07-29 12:21:16 +00:00
|
|
|
// find the State.ExitCode in container metadata
|
|
|
|
func findContainerExitCode(c *check.C, name string, vargs ...string) string {
|
|
|
|
args := append(vargs, "inspect", "--format='{{ .State.ExitCode }} {{ .State.Error }}'", name)
|
|
|
|
cmd := exec.Command(dockerBinary, args...)
|
|
|
|
out, _, err := runCommandWithOutput(cmd)
|
|
|
|
if err != nil {
|
|
|
|
c.Fatal(err, out)
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2015-10-26 12:00:49 +00:00
|
|
|
func findContainerIP(c *check.C, id string, network string) string {
|
|
|
|
out, _ := dockerCmd(c, "inspect", fmt.Sprintf("--format='{{ .NetworkSettings.Networks.%s.IPAddress }}'", network), id)
|
2014-05-10 17:27:24 +00:00
|
|
|
return strings.Trim(out, " \r\n'")
|
|
|
|
}
|
2014-05-19 16:47:35 +00:00
|
|
|
|
2015-04-28 23:17:00 +00:00
|
|
|
func (d *Daemon) findContainerIP(id string) string {
|
2015-10-27 02:35:49 +00:00
|
|
|
out, err := d.Cmd("inspect", fmt.Sprintf("--format='{{ .NetworkSettings.Networks.bridge.IPAddress }}'"), id)
|
|
|
|
if err != nil {
|
|
|
|
d.c.Log(err)
|
|
|
|
}
|
|
|
|
return strings.Trim(out, " \r\n'")
|
2015-04-27 20:16:33 +00:00
|
|
|
}
|
|
|
|
|
2014-05-19 16:47:35 +00:00
|
|
|
func getContainerCount() (int, error) {
|
|
|
|
const containers = "Containers:"
|
|
|
|
|
|
|
|
cmd := exec.Command(dockerBinary, "info")
|
|
|
|
out, _, err := runCommandWithOutput(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
lines := strings.Split(out, "\n")
|
|
|
|
for _, line := range lines {
|
|
|
|
if strings.Contains(line, containers) {
|
2015-04-06 13:21:18 +00:00
|
|
|
output := strings.TrimSpace(line)
|
2014-05-19 16:47:35 +00:00
|
|
|
output = strings.TrimLeft(output, containers)
|
|
|
|
output = strings.Trim(output, " ")
|
|
|
|
containerCount, err := strconv.Atoi(output)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return containerCount, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0, fmt.Errorf("couldn't find the Container count in the output")
|
|
|
|
}
|
2014-05-31 12:42:49 +00:00
|
|
|
|
2015-07-22 12:59:24 +00:00
|
|
|
// FakeContext creates directories that can be used as a build context
|
2014-05-31 12:42:49 +00:00
|
|
|
type FakeContext struct {
|
|
|
|
Dir string
|
|
|
|
}
|
|
|
|
|
2015-07-22 12:59:24 +00:00
|
|
|
// Add a file at a path, creating directories where necessary
|
2014-05-31 12:42:49 +00:00
|
|
|
func (f *FakeContext) Add(file, content string) error {
|
2015-04-04 14:54:43 +00:00
|
|
|
return f.addFile(file, []byte(content))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FakeContext) addFile(file string, content []byte) error {
|
2014-05-31 12:42:49 +00:00
|
|
|
filepath := path.Join(f.Dir, file)
|
|
|
|
dirpath := path.Dir(filepath)
|
|
|
|
if dirpath != "." {
|
|
|
|
if err := os.MkdirAll(dirpath, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-04-04 14:54:43 +00:00
|
|
|
return ioutil.WriteFile(filepath, content, 0644)
|
|
|
|
|
2014-05-31 12:42:49 +00:00
|
|
|
}
|
|
|
|
|
2015-07-22 12:59:24 +00:00
|
|
|
// Delete a file at a path
|
2014-05-31 12:42:49 +00:00
|
|
|
func (f *FakeContext) Delete(file string) error {
|
|
|
|
filepath := path.Join(f.Dir, file)
|
|
|
|
return os.RemoveAll(filepath)
|
|
|
|
}
|
|
|
|
|
2015-07-22 12:59:24 +00:00
|
|
|
// Close deletes the context
|
2014-05-31 12:42:49 +00:00
|
|
|
func (f *FakeContext) Close() error {
|
|
|
|
return os.RemoveAll(f.Dir)
|
|
|
|
}
|
|
|
|
|
2015-04-04 14:54:43 +00:00
|
|
|
func fakeContextFromNewTempDir() (*FakeContext, error) {
|
2014-05-31 12:42:49 +00:00
|
|
|
tmp, err := ioutil.TempDir("", "fake-context")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-09-24 12:42:17 +00:00
|
|
|
if err := os.Chmod(tmp, 0755); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-04-04 14:54:43 +00:00
|
|
|
return fakeContextFromDir(tmp), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func fakeContextFromDir(dir string) *FakeContext {
|
|
|
|
return &FakeContext{dir}
|
|
|
|
}
|
2015-03-10 03:53:28 +00:00
|
|
|
|
2015-04-04 14:54:43 +00:00
|
|
|
func fakeContextWithFiles(files map[string]string) (*FakeContext, error) {
|
|
|
|
ctx, err := fakeContextFromNewTempDir()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-05-31 12:42:49 +00:00
|
|
|
for file, content := range files {
|
|
|
|
if err := ctx.Add(file, content); err != nil {
|
|
|
|
ctx.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2015-03-10 03:53:28 +00:00
|
|
|
return ctx, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func fakeContextAddDockerfile(ctx *FakeContext, dockerfile string) error {
|
2014-05-31 12:42:49 +00:00
|
|
|
if err := ctx.Add("Dockerfile", dockerfile); err != nil {
|
|
|
|
ctx.Close()
|
2015-03-10 03:53:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func fakeContext(dockerfile string, files map[string]string) (*FakeContext, error) {
|
|
|
|
ctx, err := fakeContextWithFiles(files)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := fakeContextAddDockerfile(ctx, dockerfile); err != nil {
|
2014-05-31 12:42:49 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ctx, nil
|
|
|
|
}
|
|
|
|
|
2015-02-19 10:01:27 +00:00
|
|
|
// FakeStorage is a static file server. It might be running locally or remotely
|
|
|
|
// on test host.
|
|
|
|
type FakeStorage interface {
|
|
|
|
Close() error
|
|
|
|
URL() string
|
|
|
|
CtxDir() string
|
|
|
|
}
|
|
|
|
|
2015-04-04 14:54:43 +00:00
|
|
|
func fakeBinaryStorage(archives map[string]*bytes.Buffer) (FakeStorage, error) {
|
|
|
|
ctx, err := fakeContextFromNewTempDir()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for name, content := range archives {
|
|
|
|
if err := ctx.addFile(name, content.Bytes()); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fakeStorageWithContext(ctx)
|
|
|
|
}
|
|
|
|
|
2015-02-19 10:01:27 +00:00
|
|
|
// fakeStorage returns either a local or remote (at daemon machine) file server
|
|
|
|
func fakeStorage(files map[string]string) (FakeStorage, error) {
|
2015-03-10 03:53:28 +00:00
|
|
|
ctx, err := fakeContextWithFiles(files)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return fakeStorageWithContext(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// fakeStorageWithContext returns either a local or remote (at daemon machine) file server
|
|
|
|
func fakeStorageWithContext(ctx *FakeContext) (FakeStorage, error) {
|
2015-02-19 10:01:27 +00:00
|
|
|
if isLocalDaemon {
|
2015-03-10 03:53:28 +00:00
|
|
|
return newLocalFakeStorage(ctx)
|
2015-02-19 10:01:27 +00:00
|
|
|
}
|
2015-03-10 03:53:28 +00:00
|
|
|
return newRemoteFileServer(ctx)
|
2015-02-19 10:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// localFileStorage is a file storage on the running machine
|
|
|
|
type localFileStorage struct {
|
2014-05-31 12:42:49 +00:00
|
|
|
*FakeContext
|
|
|
|
*httptest.Server
|
|
|
|
}
|
|
|
|
|
2015-02-19 10:01:27 +00:00
|
|
|
func (s *localFileStorage) URL() string {
|
|
|
|
return s.Server.URL
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *localFileStorage) CtxDir() string {
|
|
|
|
return s.FakeContext.Dir
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *localFileStorage) Close() error {
|
|
|
|
defer s.Server.Close()
|
|
|
|
return s.FakeContext.Close()
|
2014-05-31 12:42:49 +00:00
|
|
|
}
|
|
|
|
|
2015-03-10 03:53:28 +00:00
|
|
|
func newLocalFakeStorage(ctx *FakeContext) (*localFileStorage, error) {
|
2014-05-31 12:42:49 +00:00
|
|
|
handler := http.FileServer(http.Dir(ctx.Dir))
|
|
|
|
server := httptest.NewServer(handler)
|
2015-02-19 10:01:27 +00:00
|
|
|
return &localFileStorage{
|
2014-05-31 12:42:49 +00:00
|
|
|
FakeContext: ctx,
|
|
|
|
Server: server,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-02-19 10:01:27 +00:00
|
|
|
// remoteFileServer is a containerized static file server started on the remote
|
|
|
|
// testing machine to be used in URL-accepting docker build functionality.
|
|
|
|
type remoteFileServer struct {
|
|
|
|
host string // hostname/port web server is listening to on docker host e.g. 0.0.0.0:43712
|
|
|
|
container string
|
|
|
|
image string
|
|
|
|
ctx *FakeContext
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *remoteFileServer) URL() string {
|
|
|
|
u := url.URL{
|
|
|
|
Scheme: "http",
|
|
|
|
Host: f.host}
|
|
|
|
return u.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *remoteFileServer) CtxDir() string {
|
|
|
|
return f.ctx.Dir
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *remoteFileServer) Close() error {
|
|
|
|
defer func() {
|
|
|
|
if f.ctx != nil {
|
|
|
|
f.ctx.Close()
|
|
|
|
}
|
|
|
|
if f.image != "" {
|
|
|
|
deleteImages(f.image)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if f.container == "" {
|
|
|
|
return nil
|
2014-05-31 12:42:49 +00:00
|
|
|
}
|
2015-02-19 10:01:27 +00:00
|
|
|
return deleteContainer(f.container)
|
2014-05-31 12:42:49 +00:00
|
|
|
}
|
2014-07-08 20:25:22 +00:00
|
|
|
|
2015-03-10 03:53:28 +00:00
|
|
|
func newRemoteFileServer(ctx *FakeContext) (*remoteFileServer, error) {
|
2015-02-19 10:01:27 +00:00
|
|
|
var (
|
2015-03-24 11:25:26 +00:00
|
|
|
image = fmt.Sprintf("fileserver-img-%s", strings.ToLower(stringutils.GenerateRandomAlphaOnlyString(10)))
|
|
|
|
container = fmt.Sprintf("fileserver-cnt-%s", strings.ToLower(stringutils.GenerateRandomAlphaOnlyString(10)))
|
2015-02-19 10:01:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Build the image
|
2015-03-10 03:53:28 +00:00
|
|
|
if err := fakeContextAddDockerfile(ctx, `FROM httpserver
|
|
|
|
COPY . /static`); err != nil {
|
|
|
|
return nil, fmt.Errorf("Cannot add Dockerfile to context: %v", err)
|
|
|
|
}
|
2015-02-19 10:01:27 +00:00
|
|
|
if _, err := buildImageFromContext(image, ctx, false); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed building file storage container image: %v", err)
|
2014-07-08 20:25:22 +00:00
|
|
|
}
|
2015-02-19 10:01:27 +00:00
|
|
|
|
|
|
|
// Start the container
|
|
|
|
runCmd := exec.Command(dockerBinary, "run", "-d", "-P", "--name", container, image)
|
|
|
|
if out, ec, err := runCommandWithOutput(runCmd); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to start file storage container. ec=%v\nout=%s\nerr=%v", ec, out, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find out the system assigned port
|
|
|
|
out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "port", container, "80/tcp"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to find container port: err=%v\nout=%s", err, out)
|
|
|
|
}
|
|
|
|
|
2015-07-27 18:08:40 +00:00
|
|
|
fileserverHostPort := strings.Trim(out, "\n")
|
|
|
|
_, port, err := net.SplitHostPort(fileserverHostPort)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to parse file server host:port: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dockerHostURL, err := url.Parse(daemonHost())
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to parse daemon host URL: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
host, _, err := net.SplitHostPort(dockerHostURL.Host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to parse docker daemon host:port: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-02-19 10:01:27 +00:00
|
|
|
return &remoteFileServer{
|
|
|
|
container: container,
|
|
|
|
image: image,
|
2015-07-27 18:08:40 +00:00
|
|
|
host: fmt.Sprintf("%s:%s", host, port),
|
2015-02-19 10:01:27 +00:00
|
|
|
ctx: ctx}, nil
|
2014-07-08 20:25:22 +00:00
|
|
|
}
|
2014-05-31 12:42:49 +00:00
|
|
|
|
2015-01-07 00:04:10 +00:00
|
|
|
func inspectFieldAndMarshall(name, field string, output interface{}) error {
|
|
|
|
str, err := inspectFieldJSON(name, field)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.Unmarshal([]byte(str), output)
|
|
|
|
}
|
|
|
|
|
2015-02-19 10:01:27 +00:00
|
|
|
func inspectFilter(name, filter string) (string, error) {
|
|
|
|
format := fmt.Sprintf("{{%s}}", filter)
|
2014-10-20 19:27:26 +00:00
|
|
|
inspectCmd := exec.Command(dockerBinary, "inspect", "-f", format, name)
|
|
|
|
out, exitCode, err := runCommandWithOutput(inspectCmd)
|
|
|
|
if err != nil || exitCode != 0 {
|
2015-02-19 10:01:27 +00:00
|
|
|
return "", fmt.Errorf("failed to inspect container %s: %s", name, out)
|
2014-10-20 19:27:26 +00:00
|
|
|
}
|
|
|
|
return strings.TrimSpace(out), nil
|
|
|
|
}
|
|
|
|
|
2015-02-19 10:01:27 +00:00
|
|
|
func inspectField(name, field string) (string, error) {
|
|
|
|
return inspectFilter(name, fmt.Sprintf(".%s", field))
|
|
|
|
}
|
|
|
|
|
|
|
|
func inspectFieldJSON(name, field string) (string, error) {
|
|
|
|
return inspectFilter(name, fmt.Sprintf("json .%s", field))
|
|
|
|
}
|
|
|
|
|
|
|
|
func inspectFieldMap(name, path, field string) (string, error) {
|
|
|
|
return inspectFilter(name, fmt.Sprintf("index .%s %q", path, field))
|
|
|
|
}
|
|
|
|
|
2015-06-03 19:21:38 +00:00
|
|
|
func inspectMountSourceField(name, destination string) (string, error) {
|
|
|
|
m, err := inspectMountPoint(name, destination)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return m.Source, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func inspectMountPoint(name, destination string) (types.MountPoint, error) {
|
|
|
|
out, err := inspectFieldJSON(name, "Mounts")
|
|
|
|
if err != nil {
|
|
|
|
return types.MountPoint{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return inspectMountPointJSON(out, destination)
|
|
|
|
}
|
|
|
|
|
2015-07-22 12:59:24 +00:00
|
|
|
var errMountNotFound = errors.New("mount point not found")
|
2015-06-03 19:21:38 +00:00
|
|
|
|
|
|
|
func inspectMountPointJSON(j, destination string) (types.MountPoint, error) {
|
|
|
|
var mp []types.MountPoint
|
|
|
|
if err := unmarshalJSON([]byte(j), &mp); err != nil {
|
|
|
|
return types.MountPoint{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var m *types.MountPoint
|
|
|
|
for _, c := range mp {
|
|
|
|
if c.Destination == destination {
|
|
|
|
m = &c
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if m == nil {
|
2015-07-22 12:59:24 +00:00
|
|
|
return types.MountPoint{}, errMountNotFound
|
2015-06-03 19:21:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return *m, nil
|
|
|
|
}
|
|
|
|
|
2014-05-31 12:42:49 +00:00
|
|
|
func getIDByName(name string) (string, error) {
|
|
|
|
return inspectField(name, "Id")
|
|
|
|
}
|
|
|
|
|
2014-08-13 14:30:45 +00:00
|
|
|
// getContainerState returns the exit code of the container
|
|
|
|
// and true if it's running
|
|
|
|
// the exit code should be ignored if it's running
|
2015-04-18 16:46:47 +00:00
|
|
|
func getContainerState(c *check.C, id string) (int, bool, error) {
|
2014-08-13 14:30:45 +00:00
|
|
|
var (
|
|
|
|
exitStatus int
|
|
|
|
running bool
|
|
|
|
)
|
2015-04-18 16:46:47 +00:00
|
|
|
out, exitCode := dockerCmd(c, "inspect", "--format={{.State.Running}} {{.State.ExitCode}}", id)
|
2015-04-14 05:16:19 +00:00
|
|
|
if exitCode != 0 {
|
|
|
|
return 0, false, fmt.Errorf("%q doesn't exist: %s", id, out)
|
2014-08-13 14:30:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
out = strings.Trim(out, "\n")
|
|
|
|
splitOutput := strings.Split(out, " ")
|
|
|
|
if len(splitOutput) != 2 {
|
|
|
|
return 0, false, fmt.Errorf("failed to get container state: output is broken")
|
|
|
|
}
|
|
|
|
if splitOutput[0] == "true" {
|
|
|
|
running = true
|
|
|
|
}
|
|
|
|
if n, err := strconv.Atoi(splitOutput[1]); err == nil {
|
|
|
|
exitStatus = n
|
|
|
|
} else {
|
|
|
|
return 0, false, fmt.Errorf("failed to get container state: couldn't parse integer")
|
|
|
|
}
|
|
|
|
|
|
|
|
return exitStatus, running, nil
|
|
|
|
}
|
|
|
|
|
2014-11-14 18:59:14 +00:00
|
|
|
func buildImageCmd(name, dockerfile string, useCache bool, buildFlags ...string) *exec.Cmd {
|
2015-07-23 12:12:36 +00:00
|
|
|
args := []string{"-D", "build", "-t", name}
|
2014-05-31 12:42:49 +00:00
|
|
|
if !useCache {
|
|
|
|
args = append(args, "--no-cache")
|
|
|
|
}
|
2014-11-14 18:59:14 +00:00
|
|
|
args = append(args, buildFlags...)
|
2014-05-31 12:42:49 +00:00
|
|
|
args = append(args, "-")
|
|
|
|
buildCmd := exec.Command(dockerBinary, args...)
|
|
|
|
buildCmd.Stdin = strings.NewReader(dockerfile)
|
2015-07-23 12:12:36 +00:00
|
|
|
return buildCmd
|
|
|
|
}
|
|
|
|
|
2014-11-14 18:59:14 +00:00
|
|
|
func buildImageWithOut(name, dockerfile string, useCache bool, buildFlags ...string) (string, string, error) {
|
|
|
|
buildCmd := buildImageCmd(name, dockerfile, useCache, buildFlags...)
|
2014-05-31 12:42:49 +00:00
|
|
|
out, exitCode, err := runCommandWithOutput(buildCmd)
|
|
|
|
if err != nil || exitCode != 0 {
|
2014-08-18 10:13:43 +00:00
|
|
|
return "", out, fmt.Errorf("failed to build the image: %s", out)
|
2014-05-31 12:42:49 +00:00
|
|
|
}
|
2014-08-18 10:13:43 +00:00
|
|
|
id, err := getIDByName(name)
|
|
|
|
if err != nil {
|
|
|
|
return "", out, err
|
|
|
|
}
|
|
|
|
return id, out, nil
|
|
|
|
}
|
|
|
|
|
2014-11-14 18:59:14 +00:00
|
|
|
func buildImageWithStdoutStderr(name, dockerfile string, useCache bool, buildFlags ...string) (string, string, string, error) {
|
|
|
|
buildCmd := buildImageCmd(name, dockerfile, useCache, buildFlags...)
|
2014-12-04 22:06:40 +00:00
|
|
|
stdout, stderr, exitCode, err := runCommandWithStdoutStderr(buildCmd)
|
|
|
|
if err != nil || exitCode != 0 {
|
|
|
|
return "", stdout, stderr, fmt.Errorf("failed to build the image: %s", stdout)
|
|
|
|
}
|
|
|
|
id, err := getIDByName(name)
|
|
|
|
if err != nil {
|
|
|
|
return "", stdout, stderr, err
|
|
|
|
}
|
|
|
|
return id, stdout, stderr, nil
|
|
|
|
}
|
|
|
|
|
2014-11-14 18:59:14 +00:00
|
|
|
func buildImage(name, dockerfile string, useCache bool, buildFlags ...string) (string, error) {
|
|
|
|
id, _, err := buildImageWithOut(name, dockerfile, useCache, buildFlags...)
|
2014-08-18 10:13:43 +00:00
|
|
|
return id, err
|
2014-05-31 12:42:49 +00:00
|
|
|
}
|
|
|
|
|
2014-11-14 18:59:14 +00:00
|
|
|
func buildImageFromContext(name string, ctx *FakeContext, useCache bool, buildFlags ...string) (string, error) {
|
2015-11-04 21:42:08 +00:00
|
|
|
id, _, err := buildImageFromContextWithOut(name, ctx, useCache, buildFlags...)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildImageFromContextWithOut(name string, ctx *FakeContext, useCache bool, buildFlags ...string) (string, string, error) {
|
2014-05-31 12:42:49 +00:00
|
|
|
args := []string{"build", "-t", name}
|
|
|
|
if !useCache {
|
|
|
|
args = append(args, "--no-cache")
|
|
|
|
}
|
2014-11-14 18:59:14 +00:00
|
|
|
args = append(args, buildFlags...)
|
2014-05-31 12:42:49 +00:00
|
|
|
args = append(args, ".")
|
|
|
|
buildCmd := exec.Command(dockerBinary, args...)
|
|
|
|
buildCmd.Dir = ctx.Dir
|
|
|
|
out, exitCode, err := runCommandWithOutput(buildCmd)
|
|
|
|
if err != nil || exitCode != 0 {
|
2015-11-04 21:42:08 +00:00
|
|
|
return "", "", fmt.Errorf("failed to build the image: %s", out)
|
2014-05-31 12:42:49 +00:00
|
|
|
}
|
2015-11-04 21:42:08 +00:00
|
|
|
id, err := getIDByName(name)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
return id, out, nil
|
2014-05-31 12:42:49 +00:00
|
|
|
}
|
2014-07-24 07:19:16 +00:00
|
|
|
|
2015-10-28 00:29:21 +00:00
|
|
|
func buildImageFromContextWithStdoutStderr(name string, ctx *FakeContext, useCache bool, buildFlags ...string) (string, string, string, error) {
|
|
|
|
args := []string{"build", "-t", name}
|
|
|
|
if !useCache {
|
|
|
|
args = append(args, "--no-cache")
|
|
|
|
}
|
|
|
|
args = append(args, buildFlags...)
|
|
|
|
args = append(args, ".")
|
|
|
|
buildCmd := exec.Command(dockerBinary, args...)
|
|
|
|
buildCmd.Dir = ctx.Dir
|
|
|
|
|
|
|
|
stdout, stderr, exitCode, err := runCommandWithStdoutStderr(buildCmd)
|
|
|
|
if err != nil || exitCode != 0 {
|
|
|
|
return "", stdout, stderr, fmt.Errorf("failed to build the image: %s", stdout)
|
|
|
|
}
|
|
|
|
id, err := getIDByName(name)
|
|
|
|
if err != nil {
|
|
|
|
return "", stdout, stderr, err
|
|
|
|
}
|
|
|
|
return id, stdout, stderr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildImageFromGitWithStdoutStderr(name string, ctx *fakeGit, useCache bool, buildFlags ...string) (string, string, string, error) {
|
|
|
|
args := []string{"build", "-t", name}
|
|
|
|
if !useCache {
|
|
|
|
args = append(args, "--no-cache")
|
|
|
|
}
|
|
|
|
args = append(args, buildFlags...)
|
|
|
|
args = append(args, ctx.RepoURL)
|
|
|
|
buildCmd := exec.Command(dockerBinary, args...)
|
|
|
|
|
|
|
|
stdout, stderr, exitCode, err := runCommandWithStdoutStderr(buildCmd)
|
|
|
|
if err != nil || exitCode != 0 {
|
|
|
|
return "", stdout, stderr, fmt.Errorf("failed to build the image: %s", stdout)
|
|
|
|
}
|
|
|
|
id, err := getIDByName(name)
|
|
|
|
if err != nil {
|
|
|
|
return "", stdout, stderr, err
|
|
|
|
}
|
|
|
|
return id, stdout, stderr, nil
|
|
|
|
}
|
|
|
|
|
2014-11-14 18:59:14 +00:00
|
|
|
func buildImageFromPath(name, path string, useCache bool, buildFlags ...string) (string, error) {
|
2014-07-24 07:19:16 +00:00
|
|
|
args := []string{"build", "-t", name}
|
|
|
|
if !useCache {
|
|
|
|
args = append(args, "--no-cache")
|
|
|
|
}
|
2014-11-14 18:59:14 +00:00
|
|
|
args = append(args, buildFlags...)
|
2014-07-24 07:19:16 +00:00
|
|
|
args = append(args, path)
|
|
|
|
buildCmd := exec.Command(dockerBinary, args...)
|
|
|
|
out, exitCode, err := runCommandWithOutput(buildCmd)
|
|
|
|
if err != nil || exitCode != 0 {
|
|
|
|
return "", fmt.Errorf("failed to build the image: %s", out)
|
|
|
|
}
|
|
|
|
return getIDByName(name)
|
|
|
|
}
|
|
|
|
|
2015-07-22 12:59:24 +00:00
|
|
|
type gitServer interface {
|
2015-03-10 03:53:28 +00:00
|
|
|
URL() string
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type localGitServer struct {
|
2014-07-24 07:19:16 +00:00
|
|
|
*httptest.Server
|
2015-03-10 03:53:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *localGitServer) Close() error {
|
|
|
|
r.Server.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *localGitServer) URL() string {
|
|
|
|
return r.Server.URL
|
|
|
|
}
|
|
|
|
|
2015-07-22 12:59:24 +00:00
|
|
|
type fakeGit struct {
|
2015-03-10 03:53:28 +00:00
|
|
|
root string
|
2015-07-22 12:59:24 +00:00
|
|
|
server gitServer
|
2014-07-24 07:19:16 +00:00
|
|
|
RepoURL string
|
|
|
|
}
|
|
|
|
|
2015-07-22 12:59:24 +00:00
|
|
|
func (g *fakeGit) Close() {
|
2015-03-10 03:53:28 +00:00
|
|
|
g.server.Close()
|
|
|
|
os.RemoveAll(g.root)
|
2014-07-24 07:19:16 +00:00
|
|
|
}
|
|
|
|
|
2015-07-22 12:59:24 +00:00
|
|
|
func newFakeGit(name string, files map[string]string, enforceLocalServer bool) (*fakeGit, error) {
|
2015-03-10 03:53:28 +00:00
|
|
|
ctx, err := fakeContextWithFiles(files)
|
2014-07-24 07:19:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer ctx.Close()
|
|
|
|
curdir, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer os.Chdir(curdir)
|
|
|
|
|
|
|
|
if output, err := exec.Command("git", "init", ctx.Dir).CombinedOutput(); err != nil {
|
2014-10-06 14:26:55 +00:00
|
|
|
return nil, fmt.Errorf("error trying to init repo: %s (%s)", err, output)
|
2014-07-24 07:19:16 +00:00
|
|
|
}
|
|
|
|
err = os.Chdir(ctx.Dir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-02-14 10:27:31 +00:00
|
|
|
if output, err := exec.Command("git", "config", "user.name", "Fake User").CombinedOutput(); err != nil {
|
|
|
|
return nil, fmt.Errorf("error trying to set 'user.name': %s (%s)", err, output)
|
|
|
|
}
|
|
|
|
if output, err := exec.Command("git", "config", "user.email", "fake.user@example.com").CombinedOutput(); err != nil {
|
|
|
|
return nil, fmt.Errorf("error trying to set 'user.email': %s (%s)", err, output)
|
|
|
|
}
|
2014-07-24 07:19:16 +00:00
|
|
|
if output, err := exec.Command("git", "add", "*").CombinedOutput(); err != nil {
|
2014-10-06 14:26:55 +00:00
|
|
|
return nil, fmt.Errorf("error trying to add files to repo: %s (%s)", err, output)
|
2014-07-24 07:19:16 +00:00
|
|
|
}
|
|
|
|
if output, err := exec.Command("git", "commit", "-a", "-m", "Initial commit").CombinedOutput(); err != nil {
|
2014-10-06 14:26:55 +00:00
|
|
|
return nil, fmt.Errorf("error trying to commit to repo: %s (%s)", err, output)
|
2014-07-24 07:19:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
root, err := ioutil.TempDir("", "docker-test-git-repo")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
repoPath := filepath.Join(root, name+".git")
|
|
|
|
if output, err := exec.Command("git", "clone", "--bare", ctx.Dir, repoPath).CombinedOutput(); err != nil {
|
|
|
|
os.RemoveAll(root)
|
2014-10-06 14:26:55 +00:00
|
|
|
return nil, fmt.Errorf("error trying to clone --bare: %s (%s)", err, output)
|
2014-07-24 07:19:16 +00:00
|
|
|
}
|
|
|
|
err = os.Chdir(repoPath)
|
|
|
|
if err != nil {
|
|
|
|
os.RemoveAll(root)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if output, err := exec.Command("git", "update-server-info").CombinedOutput(); err != nil {
|
|
|
|
os.RemoveAll(root)
|
2014-10-06 14:26:55 +00:00
|
|
|
return nil, fmt.Errorf("error trying to git update-server-info: %s (%s)", err, output)
|
2014-07-24 07:19:16 +00:00
|
|
|
}
|
|
|
|
err = os.Chdir(curdir)
|
|
|
|
if err != nil {
|
|
|
|
os.RemoveAll(root)
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-03-10 03:53:28 +00:00
|
|
|
|
2015-07-22 12:59:24 +00:00
|
|
|
var server gitServer
|
2015-03-10 03:53:28 +00:00
|
|
|
if !enforceLocalServer {
|
|
|
|
// use fakeStorage server, which might be local or remote (at test daemon)
|
|
|
|
server, err = fakeStorageWithContext(fakeContextFromDir(root))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot start fake storage: %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
2015-12-13 16:00:39 +00:00
|
|
|
// always start a local http server on CLI test machine
|
2015-03-10 03:53:28 +00:00
|
|
|
httpServer := httptest.NewServer(http.FileServer(http.Dir(root)))
|
|
|
|
server = &localGitServer{httpServer}
|
|
|
|
}
|
2015-07-22 12:59:24 +00:00
|
|
|
return &fakeGit{
|
2015-03-10 03:53:28 +00:00
|
|
|
root: root,
|
|
|
|
server: server,
|
|
|
|
RepoURL: fmt.Sprintf("%s/%s.git", server.URL(), name),
|
2014-07-24 07:19:16 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2014-08-28 00:25:10 +00:00
|
|
|
|
|
|
|
// Write `content` to the file at path `dst`, creating it if necessary,
|
|
|
|
// as well as any missing directories.
|
|
|
|
// The file is truncated if it already exists.
|
2015-12-13 16:00:39 +00:00
|
|
|
// Fail the test when error occurs.
|
2015-04-18 16:46:47 +00:00
|
|
|
func writeFile(dst, content string, c *check.C) {
|
2014-08-28 00:25:10 +00:00
|
|
|
// Create subdirectories if necessary
|
2015-10-11 07:33:24 +00:00
|
|
|
c.Assert(os.MkdirAll(path.Dir(dst), 0700), check.IsNil)
|
2014-08-28 00:25:10 +00:00
|
|
|
f, err := os.OpenFile(dst, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0700)
|
2015-10-11 07:33:24 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2015-07-09 10:52:18 +00:00
|
|
|
defer f.Close()
|
2014-08-28 00:25:10 +00:00
|
|
|
// Write content (truncate if it exists)
|
2015-10-11 07:33:24 +00:00
|
|
|
_, err = io.Copy(f, strings.NewReader(content))
|
|
|
|
c.Assert(err, check.IsNil)
|
2014-08-28 00:25:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the contents of file at path `src`.
|
2015-12-13 16:00:39 +00:00
|
|
|
// Fail the test when error occurs.
|
2015-04-18 16:46:47 +00:00
|
|
|
func readFile(src string, c *check.C) (content string) {
|
2015-02-27 03:53:11 +00:00
|
|
|
data, err := ioutil.ReadFile(src)
|
2015-10-11 07:33:24 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2015-02-27 03:53:11 +00:00
|
|
|
|
2014-08-28 00:25:10 +00:00
|
|
|
return string(data)
|
|
|
|
}
|
2014-10-24 18:12:54 +00:00
|
|
|
|
2015-07-22 12:59:24 +00:00
|
|
|
func containerStorageFile(containerID, basename string) string {
|
2015-10-08 15:51:41 +00:00
|
|
|
return filepath.Join(containerStoragePath, containerID, basename)
|
2014-10-24 18:12:54 +00:00
|
|
|
}
|
|
|
|
|
2014-10-24 21:39:12 +00:00
|
|
|
// docker commands that use this function must be run with the '-d' switch.
|
2014-10-24 18:12:54 +00:00
|
|
|
func runCommandAndReadContainerFile(filename string, cmd *exec.Cmd) ([]byte, error) {
|
|
|
|
out, _, err := runCommandWithOutput(cmd)
|
|
|
|
if err != nil {
|
2014-10-24 21:39:12 +00:00
|
|
|
return nil, fmt.Errorf("%v: %q", err, out)
|
2014-10-24 18:12:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
contID := strings.TrimSpace(out)
|
|
|
|
|
2015-08-11 07:41:11 +00:00
|
|
|
if err := waitRun(contID); err != nil {
|
|
|
|
return nil, fmt.Errorf("%v: %q", contID, err)
|
|
|
|
}
|
|
|
|
|
2014-10-24 18:12:54 +00:00
|
|
|
return readContainerFile(contID, filename)
|
|
|
|
}
|
|
|
|
|
2015-07-22 12:59:24 +00:00
|
|
|
func readContainerFile(containerID, filename string) ([]byte, error) {
|
|
|
|
f, err := os.Open(containerStorageFile(containerID, filename))
|
2014-10-24 18:12:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
content, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return content, nil
|
|
|
|
}
|
2015-01-13 18:46:32 +00:00
|
|
|
|
2015-07-22 12:59:24 +00:00
|
|
|
func readContainerFileWithExec(containerID, filename string) ([]byte, error) {
|
|
|
|
out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "exec", containerID, "cat", filename))
|
2015-02-16 19:32:25 +00:00
|
|
|
return []byte(out), err
|
|
|
|
}
|
|
|
|
|
2015-03-10 21:19:32 +00:00
|
|
|
// daemonTime provides the current time on the daemon host
|
2015-04-18 16:46:47 +00:00
|
|
|
func daemonTime(c *check.C) time.Time {
|
2015-03-10 21:19:32 +00:00
|
|
|
if isLocalDaemon {
|
|
|
|
return time.Now()
|
|
|
|
}
|
|
|
|
|
2015-04-20 21:03:56 +00:00
|
|
|
status, body, err := sockRequest("GET", "/info", nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
2015-10-11 07:33:24 +00:00
|
|
|
c.Assert(status, check.Equals, http.StatusOK)
|
2015-03-10 21:19:32 +00:00
|
|
|
|
|
|
|
type infoJSON struct {
|
|
|
|
SystemTime string
|
|
|
|
}
|
|
|
|
var info infoJSON
|
2015-10-11 07:33:24 +00:00
|
|
|
err = json.Unmarshal(body, &info)
|
|
|
|
c.Assert(err, check.IsNil, check.Commentf("unable to unmarshal GET /info response"))
|
2015-03-10 21:19:32 +00:00
|
|
|
|
|
|
|
dt, err := time.Parse(time.RFC3339Nano, info.SystemTime)
|
2015-10-11 07:33:24 +00:00
|
|
|
c.Assert(err, check.IsNil, check.Commentf("invalid time format in GET /info response"))
|
2015-03-10 21:19:32 +00:00
|
|
|
return dt
|
|
|
|
}
|
|
|
|
|
2015-04-24 21:16:56 +00:00
|
|
|
func setupRegistry(c *check.C) *testRegistryV2 {
|
2015-04-18 16:46:47 +00:00
|
|
|
testRequires(c, RegistryHosting)
|
|
|
|
reg, err := newTestRegistryV2(c)
|
2015-10-11 07:33:24 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2015-01-31 19:28:11 +00:00
|
|
|
|
|
|
|
// Wait for registry to be ready to serve requests.
|
2015-11-20 22:12:12 +00:00
|
|
|
for i := 0; i != 50; i++ {
|
2015-01-31 19:28:11 +00:00
|
|
|
if err = reg.Ping(); err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
2015-10-11 07:33:24 +00:00
|
|
|
c.Assert(err, check.IsNil, check.Commentf("Timeout waiting for test registry to become available"))
|
2015-04-24 21:16:56 +00:00
|
|
|
return reg
|
2015-01-13 18:46:32 +00:00
|
|
|
}
|
2015-02-12 19:20:48 +00:00
|
|
|
|
2015-07-20 05:56:10 +00:00
|
|
|
func setupNotary(c *check.C) *testNotary {
|
|
|
|
testRequires(c, NotaryHosting)
|
|
|
|
ts, err := newTestNotary(c)
|
2015-10-11 07:33:24 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2015-07-20 05:56:10 +00:00
|
|
|
|
|
|
|
return ts
|
|
|
|
}
|
|
|
|
|
2015-03-07 09:03:44 +00:00
|
|
|
// appendBaseEnv appends the minimum set of environment variables to exec the
|
|
|
|
// docker cli binary for testing with correct configuration to the given env
|
|
|
|
// list.
|
|
|
|
func appendBaseEnv(env []string) []string {
|
|
|
|
preserveList := []string{
|
|
|
|
// preserve remote test host
|
|
|
|
"DOCKER_HOST",
|
|
|
|
|
|
|
|
// windows: requires preserving SystemRoot, otherwise dial tcp fails
|
|
|
|
// with "GetAddrInfoW: A non-recoverable error occurred during a database lookup."
|
|
|
|
"SystemRoot",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, key := range preserveList {
|
|
|
|
if val := os.Getenv(key); val != "" {
|
|
|
|
env = append(env, fmt.Sprintf("%s=%s", key, val))
|
|
|
|
}
|
2015-02-12 19:20:48 +00:00
|
|
|
}
|
|
|
|
return env
|
|
|
|
}
|
2015-07-24 18:23:20 +00:00
|
|
|
|
|
|
|
func createTmpFile(c *check.C, content string) string {
|
|
|
|
f, err := ioutil.TempFile("", "testfile")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
filename := f.Name()
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(filename, []byte(content), 0644)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
return filename
|
|
|
|
}
|
2015-08-12 02:27:33 +00:00
|
|
|
|
2015-08-24 17:57:39 +00:00
|
|
|
func buildImageWithOutInDamon(socket string, name, dockerfile string, useCache bool) (string, error) {
|
|
|
|
args := []string{"--host", socket}
|
2015-08-12 02:27:33 +00:00
|
|
|
buildCmd := buildImageCmdArgs(args, name, dockerfile, useCache)
|
|
|
|
out, exitCode, err := runCommandWithOutput(buildCmd)
|
|
|
|
if err != nil || exitCode != 0 {
|
2015-08-24 17:57:39 +00:00
|
|
|
return out, fmt.Errorf("failed to build the image: %s, error: %v", out, err)
|
2015-08-12 02:27:33 +00:00
|
|
|
}
|
2015-08-24 17:57:39 +00:00
|
|
|
return out, nil
|
2015-08-12 02:27:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func buildImageCmdArgs(args []string, name, dockerfile string, useCache bool) *exec.Cmd {
|
|
|
|
args = append(args, []string{"-D", "build", "-t", name}...)
|
|
|
|
if !useCache {
|
|
|
|
args = append(args, "--no-cache")
|
|
|
|
}
|
|
|
|
args = append(args, "-")
|
|
|
|
buildCmd := exec.Command(dockerBinary, args...)
|
|
|
|
buildCmd.Stdin = strings.NewReader(dockerfile)
|
|
|
|
return buildCmd
|
|
|
|
|
|
|
|
}
|
2015-09-09 13:36:44 +00:00
|
|
|
|
|
|
|
func waitForContainer(contID string, args ...string) error {
|
|
|
|
args = append([]string{"run", "--name", contID}, args...)
|
|
|
|
cmd := exec.Command(dockerBinary, args...)
|
|
|
|
if _, err := runCommand(cmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := waitRun(contID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// waitRun will wait for the specified container to be running, maximum 5 seconds.
|
|
|
|
func waitRun(contID string) error {
|
2015-09-01 21:37:04 +00:00
|
|
|
return waitInspect(contID, "{{.State.Running}}", "true", 5*time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
// waitExited will wait for the specified container to state exit, subject
|
|
|
|
// to a maximum time limit in seconds supplied by the caller
|
|
|
|
func waitExited(contID string, duration time.Duration) error {
|
|
|
|
return waitInspect(contID, "{{.State.Status}}", "exited", duration)
|
2015-09-09 13:36:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// waitInspect will wait for the specified container to have the specified string
|
|
|
|
// in the inspect output. It will wait until the specified timeout (in seconds)
|
|
|
|
// is reached.
|
2015-09-01 21:37:04 +00:00
|
|
|
func waitInspect(name, expr, expected string, timeout time.Duration) error {
|
|
|
|
after := time.After(timeout)
|
2015-09-09 13:36:44 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
cmd := exec.Command(dockerBinary, "inspect", "-f", expr, name)
|
|
|
|
out, _, err := runCommandWithOutput(cmd)
|
|
|
|
if err != nil {
|
|
|
|
if !strings.Contains(out, "No such") {
|
|
|
|
return fmt.Errorf("error executing docker inspect: %v\n%s", err, out)
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-after:
|
|
|
|
return err
|
|
|
|
default:
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out = strings.TrimSpace(out)
|
|
|
|
if out == expected {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-after:
|
|
|
|
return fmt.Errorf("condition \"%q == %q\" not true in time", out, expected)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-10-30 18:57:15 +00:00
|
|
|
|
|
|
|
func getInspectBody(c *check.C, version, id string) []byte {
|
|
|
|
endpoint := fmt.Sprintf("/%s/containers/%s/json", version, id)
|
|
|
|
status, body, err := sockRequest("GET", endpoint, nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(status, check.Equals, http.StatusOK)
|
|
|
|
return body
|
|
|
|
}
|