6db32fde |
package main
import ( |
ba5e0980 |
"bufio" |
c8a3d313 |
"bytes" |
f6a037d4 |
"crypto/tls" |
c8a3d313 |
"encoding/json" |
054b921a |
"errors" |
6db32fde |
"fmt" |
5b27fbc0 |
"io" |
2e855688 |
"io/ioutil" |
054b921a |
"net" |
2e855688 |
"net/http"
"net/http/httptest" |
054b921a |
"net/http/httputil" |
e1ef3344 |
"net/url" |
2e855688 |
"os" |
6db32fde |
"os/exec" |
2e855688 |
"path" |
5b0d4cf2 |
"path/filepath" |
77f54252 |
"strconv" |
6db32fde |
"strings" |
054b921a |
"time" |
ed345fb1 |
|
4b9fe9c2 |
"github.com/docker/docker/opts" |
da44d0fc |
"github.com/docker/docker/pkg/httputils" |
7efa0f37 |
"github.com/docker/docker/pkg/integration" |
8232cc77 |
"github.com/docker/docker/pkg/ioutils" |
b80fae73 |
"github.com/docker/docker/pkg/stringutils" |
907407d0 |
"github.com/docker/engine-api/types" |
8e034802 |
"github.com/docker/go-connections/tlsconfig" |
07184599 |
"github.com/docker/go-units" |
dc944ea7 |
"github.com/go-check/check" |
6db32fde |
)
|
44da8617 |
func init() { |
50d5d55f |
cmd := exec.Command(dockerBinary, "images", "-f", "dangling=false", "--format", "{{.Repository}}:{{.Tag}}") |
f4a1e3db |
cmd.Env = appendBaseEnv(true)
out, err := cmd.CombinedOutput() |
44da8617 |
if err != nil { |
f4a1e3db |
panic(fmt.Errorf("err=%v\nout=%s\n", err, out)) |
44da8617 |
} |
50d5d55f |
images := strings.Split(strings.TrimSpace(string(out)), "\n")
for _, img := range images {
protectedImages[img] = struct{}{} |
44da8617 |
}
|
50d5d55f |
res, body, err := sockRequestRaw("GET", "/info", nil, "application/json")
if err != nil {
panic(fmt.Errorf("Init failed to get /info: %v", err))
}
defer body.Close()
if res.StatusCode != http.StatusOK {
panic(fmt.Errorf("Init failed to get /info. Res=%v", res)) |
44da8617 |
} |
50d5d55f |
|
44da8617 |
svrHeader, _ := httputils.ParseServerHeader(res.Header.Get("Server"))
daemonPlatform = svrHeader.OS
if daemonPlatform != "linux" && daemonPlatform != "windows" {
panic("Cannot run tests against platform: " + daemonPlatform)
}
// Now we know the daemon platform, can set paths used by tests. |
50d5d55f |
var info types.Info
err = json.NewDecoder(body).Decode(&info) |
44da8617 |
if err != nil { |
50d5d55f |
panic(fmt.Errorf("Init failed to unmarshal docker info: %v", err)) |
44da8617 |
}
|
90f51242 |
daemonStorageDriver = info.Driver |
44da8617 |
dockerBasePath = info.DockerRootDir
volumesConfigPath = filepath.Join(dockerBasePath, "volumes")
containerStoragePath = filepath.Join(dockerBasePath, "containers") |
1847641b |
// 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) |
50d5d55f |
// On Windows, extract out the version as we need to make selective
// decisions during integration testing as and when features are implemented.
// eg in "10.0 10550 (10550.1000.amd64fre.branch.date-time)" we want 10550
windowsDaemonKV, _ = strconv.Atoi(strings.Split(info.KernelVersion, " ")[1]) |
1847641b |
} else {
volumesConfigPath = strings.Replace(volumesConfigPath, `\`, `/`, -1)
containerStoragePath = strings.Replace(containerStoragePath, `\`, `/`, -1)
} |
44da8617 |
}
|
07184599 |
func convertBasesize(basesizeBytes int64) (int64, error) {
basesize := units.HumanSize(float64(basesizeBytes))
basesize = strings.Trim(basesize, " ")[:len(basesize)-3]
basesizeFloat, err := strconv.ParseFloat(strings.Trim(basesize, " "), 64)
if err != nil {
return 0, err
}
return int64(basesizeFloat) * 1024 * 1024 * 1024, nil
}
|
e1ef3344 |
func daemonHost() string { |
6b3c9281 |
daemonURLStr := "unix://" + opts.DefaultUnixSocket |
ed345fb1 |
if daemonHostVar := os.Getenv("DOCKER_HOST"); daemonHostVar != "" { |
6b3c9281 |
daemonURLStr = daemonHostVar |
e1ef3344 |
} |
6b3c9281 |
return daemonURLStr |
e1ef3344 |
}
|
f6a037d4 |
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
}
|
0d88d5b6 |
func sockConn(timeout time.Duration, daemon string) (net.Conn, error) {
if daemon == "" {
daemon = daemonHost()
} |
6b3c9281 |
daemonURL, err := url.Parse(daemon) |
e1ef3344 |
if err != nil {
return nil, fmt.Errorf("could not parse url %q: %v", daemon, err)
}
var c net.Conn |
6b3c9281 |
switch daemonURL.Scheme { |
08b65e7d |
case "npipe":
return npipeDial(daemonURL.Path, timeout) |
e1ef3344 |
case "unix": |
6b3c9281 |
return net.DialTimeout(daemonURL.Scheme, daemonURL.Path, timeout) |
e1ef3344 |
case "tcp": |
f6a037d4 |
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)
} |
6b3c9281 |
return net.DialTimeout(daemonURL.Scheme, daemonURL.Host, timeout) |
e1ef3344 |
default: |
6b3c9281 |
return c, fmt.Errorf("unknown scheme %v (%s)", daemonURL.Scheme, daemon) |
ac6cb41d |
}
}
|
91bfed60 |
func sockRequest(method, endpoint string, data interface{}) (int, []byte, error) { |
ac6cb41d |
jsonData := bytes.NewBuffer(nil)
if err := json.NewEncoder(jsonData).Encode(data); err != nil { |
91bfed60 |
return -1, nil, err |
e1ef3344 |
} |
ac6cb41d |
|
bb1c576e |
res, body, err := sockRequestRaw(method, endpoint, jsonData, "application/json") |
8232cc77 |
if err != nil { |
09b86c46 |
return -1, nil, err |
8232cc77 |
} |
09b86c46 |
b, err := readBody(body) |
bb1c576e |
return res.StatusCode, b, err |
ac6cb41d |
}
|
bb1c576e |
func sockRequestRaw(method, endpoint string, data io.Reader, ct string) (*http.Response, io.ReadCloser, error) { |
0d88d5b6 |
return sockRequestRawToDaemon(method, endpoint, data, ct, "")
}
func sockRequestRawToDaemon(method, endpoint string, data io.Reader, ct, daemon string) (*http.Response, io.ReadCloser, error) {
req, client, err := newRequestClient(method, endpoint, data, ct, daemon) |
ba5e0980 |
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) { |
0d88d5b6 |
req, client, err := newRequestClient(method, endpoint, data, ct, "") |
ba5e0980 |
if err != nil {
return nil, nil, err
}
client.Do(req)
conn, br := client.Hijack()
return conn, br, nil
}
|
0d88d5b6 |
func newRequestClient(method, endpoint string, data io.Reader, ct, daemon string) (*http.Request, *httputil.ClientConn, error) {
c, err := sockConn(time.Duration(10*time.Second), daemon) |
f49c3f28 |
if err != nil { |
bb1c576e |
return nil, nil, fmt.Errorf("could not dial docker daemon: %v", err) |
f49c3f28 |
}
client := httputil.NewClientConn(c, nil)
|
198ff76d |
req, err := http.NewRequest(method, endpoint, data) |
f49c3f28 |
if err != nil { |
8232cc77 |
client.Close() |
bb1c576e |
return nil, nil, fmt.Errorf("could not create new request: %v", err) |
f49c3f28 |
}
|
5dc02a2f |
if ct != "" {
req.Header.Set("Content-Type", ct) |
198ff76d |
} |
ba5e0980 |
return req, client, nil |
8232cc77 |
} |
91bfed60 |
|
8232cc77 |
func readBody(b io.ReadCloser) ([]byte, error) {
defer b.Close()
return ioutil.ReadAll(b) |
f49c3f28 |
}
|
6db32fde |
func deleteContainer(container string) error { |
255b8444 |
container = strings.TrimSpace(strings.Replace(container, "\n", " ", -1)) |
887ad57c |
rmArgs := strings.Split(fmt.Sprintf("rm -fv %v", container), " ") |
255b8444 |
exitCode, err := runCommand(exec.Command(dockerBinary, rmArgs...)) |
6db32fde |
// 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
}
|
bfa5027e |
if containers != "" {
if err = deleteContainer(containers); err != nil {
return err
} |
6db32fde |
}
return nil |
28ad7c58 |
}
func deleteAllNetworks() error {
networks, err := getAllNetworks()
if err != nil {
return err
}
var errors []string
for _, n := range networks { |
5970e940 |
if n.Name == "bridge" || n.Name == "none" || n.Name == "host" {
continue
} |
d6ab2ad3 |
if daemonPlatform == "windows" && strings.ToLower(n.Name) == "nat" {
// nat is a pre-defined network on Windows and cannot be removed
continue
} |
5970e940 |
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))) |
28ad7c58 |
}
}
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 |
6db32fde |
}
|
b3b7eb27 |
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
}
|
a9688cdc |
var protectedImages = map[string]struct{}{}
func deleteAllImages() error { |
f4a1e3db |
cmd := exec.Command(dockerBinary, "images")
cmd.Env = appendBaseEnv(true)
out, err := cmd.CombinedOutput() |
a9688cdc |
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
}
|
1bb02117 |
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
}
|
0ce42dcc |
func getSliceOfPausedContainers() ([]string, error) {
out, err := getPausedContainers()
if err == nil { |
8636a219 |
if len(out) == 0 {
return nil, err
} |
0ce42dcc |
slice := strings.Split(strings.TrimSpace(out), "\n")
return slice, err
} |
664ef0cb |
return []string{out}, err |
0ce42dcc |
}
|
1bb02117 |
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")
}
|
0687d76a |
return err |
1bb02117 |
}
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
}
|
50fa9dff |
func deleteImages(images ...string) error { |
563708d7 |
args := []string{"rmi", "-f"} |
bbb245de |
args = append(args, images...)
rmiCmd := exec.Command(dockerBinary, args...) |
6db32fde |
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
} |
30f22ee9 |
|
eb1e8e88 |
func imageExists(image string) error {
inspectCmd := exec.Command(dockerBinary, "inspect", image)
exitCode, err := runCommand(inspectCmd)
if exitCode != 0 && err == nil { |
949ab477 |
err = fmt.Errorf("couldn't find image %q", image) |
eb1e8e88 |
}
return err
}
|
b7009a4b |
func pullImageIfNotExist(image string) error { |
dad47680 |
if err := imageExists(image); err != nil {
pullCmd := exec.Command(dockerBinary, "pull", image)
_, exitCode, err := runCommandWithOutput(pullCmd)
if err != nil || exitCode != 0 { |
b7009a4b |
return fmt.Errorf("image %q wasn't found locally and it couldn't be pulled: %s", image, err) |
dad47680 |
}
} |
b7009a4b |
return nil |
dad47680 |
}
|
693ba98c |
func dockerCmdWithError(args ...string) (string, int, error) { |
6a931c35 |
if err := validateArgs(args...); err != nil {
return "", 0, err
} |
42df9edc |
out, code, err := integration.DockerCmdWithError(dockerBinary, args...)
if err != nil {
err = fmt.Errorf("%v: %s", err, out)
}
return out, code, err |
4290bdef |
}
|
c6cde91b |
func dockerCmdWithStdoutStderr(c *check.C, args ...string) (string, string, int) { |
6a931c35 |
if err := validateArgs(args...); err != nil {
c.Fatalf(err.Error())
} |
7efa0f37 |
return integration.DockerCmdWithStdoutStderr(dockerBinary, c, args...) |
c6cde91b |
}
|
dc944ea7 |
func dockerCmd(c *check.C, args ...string) (string, int) { |
6a931c35 |
if err := validateArgs(args...); err != nil {
c.Fatalf(err.Error())
} |
7efa0f37 |
return integration.DockerCmd(dockerBinary, c, args...) |
30f22ee9 |
} |
7cc27b20 |
|
a0225657 |
// execute a docker command with a timeout |
c6965e3e |
func dockerCmdWithTimeout(timeout time.Duration, args ...string) (string, int, error) { |
6a931c35 |
if err := validateArgs(args...); err != nil {
return "", 0, err
} |
7efa0f37 |
return integration.DockerCmdWithTimeout(dockerBinary, timeout, args...) |
c6965e3e |
}
|
f60c8e9e |
// execute a docker command in a directory |
dc944ea7 |
func dockerCmdInDir(c *check.C, path string, args ...string) (string, int, error) { |
6a931c35 |
if err := validateArgs(args...); err != nil {
c.Fatalf(err.Error())
} |
7efa0f37 |
return integration.DockerCmdInDir(dockerBinary, path, args...) |
c6965e3e |
}
// execute a docker command in a directory with a timeout
func dockerCmdInDirWithTimeout(timeout time.Duration, path string, args ...string) (string, int, error) { |
6a931c35 |
if err := validateArgs(args...); err != nil {
return "", 0, err
} |
7efa0f37 |
return integration.DockerCmdInDirWithTimeout(dockerBinary, timeout, path, args...) |
f60c8e9e |
}
|
6a931c35 |
// validateArgs is a checker to ensure tests are not running commands which are
// not supported on platforms. Specifically on Windows this is 'busybox top'.
func validateArgs(args ...string) error {
if daemonPlatform != "windows" {
return nil
}
foundBusybox := -1
for key, value := range args {
if strings.ToLower(value) == "busybox" {
foundBusybox = key
}
if (foundBusybox != -1) && (key == foundBusybox+1) && (strings.ToLower(value) == "top") {
return errors.New("Cannot use 'busybox top' in tests on Windows. Use runSleepingContainer()")
}
}
return nil
}
|
41de7a18 |
// 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
}
|
1b9a08e7 |
func findContainerIP(c *check.C, id string, network string) string {
out, _ := dockerCmd(c, "inspect", fmt.Sprintf("--format='{{ .NetworkSettings.Networks.%s.IPAddress }}'", network), id) |
7cc27b20 |
return strings.Trim(out, " \r\n'")
} |
77f54252 |
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) { |
475c6531 |
output := strings.TrimSpace(line) |
77f54252 |
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")
} |
2e855688 |
|
6b3c9281 |
// FakeContext creates directories that can be used as a build context |
2e855688 |
type FakeContext struct {
Dir string
}
|
6b3c9281 |
// Add a file at a path, creating directories where necessary |
2e855688 |
func (f *FakeContext) Add(file, content string) error { |
d48bface |
return f.addFile(file, []byte(content))
}
func (f *FakeContext) addFile(file string, content []byte) error { |
2e855688 |
filepath := path.Join(f.Dir, file)
dirpath := path.Dir(filepath)
if dirpath != "." {
if err := os.MkdirAll(dirpath, 0755); err != nil {
return err
}
} |
d48bface |
return ioutil.WriteFile(filepath, content, 0644)
|
2e855688 |
}
|
6b3c9281 |
// Delete a file at a path |
2e855688 |
func (f *FakeContext) Delete(file string) error {
filepath := path.Join(f.Dir, file)
return os.RemoveAll(filepath)
}
|
6b3c9281 |
// Close deletes the context |
2e855688 |
func (f *FakeContext) Close() error {
return os.RemoveAll(f.Dir)
}
|
d48bface |
func fakeContextFromNewTempDir() (*FakeContext, error) { |
2e855688 |
tmp, err := ioutil.TempDir("", "fake-context")
if err != nil {
return nil, err
} |
51a56399 |
if err := os.Chmod(tmp, 0755); err != nil {
return nil, err
} |
d48bface |
return fakeContextFromDir(tmp), nil
}
func fakeContextFromDir(dir string) *FakeContext {
return &FakeContext{dir}
} |
44ffb199 |
|
d48bface |
func fakeContextWithFiles(files map[string]string) (*FakeContext, error) {
ctx, err := fakeContextFromNewTempDir()
if err != nil {
return nil, err
} |
2e855688 |
for file, content := range files {
if err := ctx.Add(file, content); err != nil {
ctx.Close()
return nil, err
}
} |
44ffb199 |
return ctx, nil
}
func fakeContextAddDockerfile(ctx *FakeContext, dockerfile string) error { |
2e855688 |
if err := ctx.Add("Dockerfile", dockerfile); err != nil {
ctx.Close() |
44ffb199 |
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 { |
2e855688 |
return nil, err
}
return ctx, nil
}
|
2e95bb5f |
// 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
}
|
d48bface |
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)
}
|
2e95bb5f |
// fakeStorage returns either a local or remote (at daemon machine) file server
func fakeStorage(files map[string]string) (FakeStorage, error) { |
44ffb199 |
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) { |
2e95bb5f |
if isLocalDaemon { |
44ffb199 |
return newLocalFakeStorage(ctx) |
2e95bb5f |
} |
44ffb199 |
return newRemoteFileServer(ctx) |
2e95bb5f |
}
// localFileStorage is a file storage on the running machine
type localFileStorage struct { |
2e855688 |
*FakeContext
*httptest.Server
}
|
2e95bb5f |
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() |
2e855688 |
}
|
44ffb199 |
func newLocalFakeStorage(ctx *FakeContext) (*localFileStorage, error) { |
2e855688 |
handler := http.FileServer(http.Dir(ctx.Dir))
server := httptest.NewServer(handler) |
2e95bb5f |
return &localFileStorage{ |
2e855688 |
FakeContext: ctx,
Server: server,
}, nil
}
|
2e95bb5f |
// 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 |
2e855688 |
} |
2e95bb5f |
return deleteContainer(f.container) |
2e855688 |
} |
e5e8669c |
|
44ffb199 |
func newRemoteFileServer(ctx *FakeContext) (*remoteFileServer, error) { |
2e95bb5f |
var ( |
b80fae73 |
image = fmt.Sprintf("fileserver-img-%s", strings.ToLower(stringutils.GenerateRandomAlphaOnlyString(10)))
container = fmt.Sprintf("fileserver-cnt-%s", strings.ToLower(stringutils.GenerateRandomAlphaOnlyString(10))) |
2e95bb5f |
)
// Build the image |
44ffb199 |
if err := fakeContextAddDockerfile(ctx, `FROM httpserver
COPY . /static`); err != nil {
return nil, fmt.Errorf("Cannot add Dockerfile to context: %v", err)
} |
2e95bb5f |
if _, err := buildImageFromContext(image, ctx, false); err != nil {
return nil, fmt.Errorf("failed building file storage container image: %v", err) |
e5e8669c |
} |
2e95bb5f |
// 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)
}
|
d06ffd0a |
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)
}
|
2e95bb5f |
return &remoteFileServer{
container: container,
image: image, |
d06ffd0a |
host: fmt.Sprintf("%s:%s", host, port), |
2e95bb5f |
ctx: ctx}, nil |
e5e8669c |
} |
2e855688 |
|
62a856e9 |
func inspectFieldAndMarshall(c *check.C, name, field string, output interface{}) {
str := inspectFieldJSON(c, name, field)
err := json.Unmarshal([]byte(str), output)
if c != nil {
c.Assert(err, check.IsNil, check.Commentf("failed to unmarshal: %v", err)) |
abb5e9a0 |
}
}
|
2e95bb5f |
func inspectFilter(name, filter string) (string, error) {
format := fmt.Sprintf("{{%s}}", filter) |
964f9965 |
inspectCmd := exec.Command(dockerBinary, "inspect", "-f", format, name)
out, exitCode, err := runCommandWithOutput(inspectCmd)
if err != nil || exitCode != 0 { |
62a856e9 |
return "", fmt.Errorf("failed to inspect %s: %s", name, out) |
964f9965 |
}
return strings.TrimSpace(out), nil
}
|
62a856e9 |
func inspectFieldWithError(name, field string) (string, error) { |
2e95bb5f |
return inspectFilter(name, fmt.Sprintf(".%s", field))
}
|
62a856e9 |
func inspectField(c *check.C, name, field string) string {
out, err := inspectFilter(name, fmt.Sprintf(".%s", field))
if c != nil {
c.Assert(err, check.IsNil)
}
return out |
2e95bb5f |
}
|
62a856e9 |
func inspectFieldJSON(c *check.C, name, field string) string {
out, err := inspectFilter(name, fmt.Sprintf("json .%s", field))
if c != nil {
c.Assert(err, check.IsNil)
}
return out
}
func inspectFieldMap(c *check.C, name, path, field string) string {
out, err := inspectFilter(name, fmt.Sprintf("index .%s %q", path, field))
if c != nil {
c.Assert(err, check.IsNil)
}
return out |
2e95bb5f |
}
|
1c3cb2d3 |
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) { |
62a856e9 |
out, err := inspectFilter(name, "json .Mounts") |
1c3cb2d3 |
if err != nil {
return types.MountPoint{}, err
}
return inspectMountPointJSON(out, destination)
}
|
6b3c9281 |
var errMountNotFound = errors.New("mount point not found") |
1c3cb2d3 |
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 { |
6b3c9281 |
return types.MountPoint{}, errMountNotFound |
1c3cb2d3 |
}
return *m, nil
}
|
1a85c8eb |
func inspectImage(name, filter string) (string, error) {
args := []string{"inspect", "--type", "image"}
if filter != "" {
format := fmt.Sprintf("{{%s}}", filter)
args = append(args, "-f", format)
}
args = append(args, name)
inspectCmd := exec.Command(dockerBinary, args...)
out, exitCode, err := runCommandWithOutput(inspectCmd)
if err != nil || exitCode != 0 {
return "", fmt.Errorf("failed to inspect %s: %s", name, out)
}
return strings.TrimSpace(out), nil
}
|
2e855688 |
func getIDByName(name string) (string, error) { |
62a856e9 |
return inspectFieldWithError(name, "Id") |
2e855688 |
}
|
5d1cb9d0 |
// getContainerState returns the exit code of the container
// and true if it's running
// the exit code should be ignored if it's running |
dc944ea7 |
func getContainerState(c *check.C, id string) (int, bool, error) { |
5d1cb9d0 |
var (
exitStatus int
running bool
) |
dc944ea7 |
out, exitCode := dockerCmd(c, "inspect", "--format={{.State.Running}} {{.State.ExitCode}}", id) |
621b601b |
if exitCode != 0 {
return 0, false, fmt.Errorf("%q doesn't exist: %s", id, out) |
5d1cb9d0 |
}
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
}
|
54240f8d |
func buildImageCmd(name, dockerfile string, useCache bool, buildFlags ...string) *exec.Cmd { |
a8d01349 |
return buildImageCmdWithHost(name, dockerfile, "", useCache, buildFlags...)
}
func buildImageCmdWithHost(name, dockerfile, host string, useCache bool, buildFlags ...string) *exec.Cmd {
args := []string{}
if host != "" {
args = append(args, "--host", host)
}
args = append(args, "build", "-t", name) |
2e855688 |
if !useCache {
args = append(args, "--no-cache")
} |
54240f8d |
args = append(args, buildFlags...) |
2e855688 |
args = append(args, "-")
buildCmd := exec.Command(dockerBinary, args...)
buildCmd.Stdin = strings.NewReader(dockerfile) |
871d2b96 |
return buildCmd
}
|
54240f8d |
func buildImageWithOut(name, dockerfile string, useCache bool, buildFlags ...string) (string, string, error) {
buildCmd := buildImageCmd(name, dockerfile, useCache, buildFlags...) |
2e855688 |
out, exitCode, err := runCommandWithOutput(buildCmd)
if err != nil || exitCode != 0 { |
075e1bc1 |
return "", out, fmt.Errorf("failed to build the image: %s", out) |
2e855688 |
} |
075e1bc1 |
id, err := getIDByName(name)
if err != nil {
return "", out, err
}
return id, out, nil
}
|
54240f8d |
func buildImageWithStdoutStderr(name, dockerfile string, useCache bool, buildFlags ...string) (string, string, string, error) {
buildCmd := buildImageCmd(name, dockerfile, useCache, buildFlags...) |
5c91bb93 |
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
}
|
54240f8d |
func buildImage(name, dockerfile string, useCache bool, buildFlags ...string) (string, error) {
id, _, err := buildImageWithOut(name, dockerfile, useCache, buildFlags...) |
075e1bc1 |
return id, err |
2e855688 |
}
|
54240f8d |
func buildImageFromContext(name string, ctx *FakeContext, useCache bool, buildFlags ...string) (string, error) { |
47da59f7 |
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) { |
2e855688 |
args := []string{"build", "-t", name}
if !useCache {
args = append(args, "--no-cache")
} |
54240f8d |
args = append(args, buildFlags...) |
2e855688 |
args = append(args, ".")
buildCmd := exec.Command(dockerBinary, args...)
buildCmd.Dir = ctx.Dir
out, exitCode, err := runCommandWithOutput(buildCmd)
if err != nil || exitCode != 0 { |
47da59f7 |
return "", "", fmt.Errorf("failed to build the image: %s", out) |
2e855688 |
} |
47da59f7 |
id, err := getIDByName(name)
if err != nil {
return "", "", err
}
return id, out, nil |
2e855688 |
} |
5b0d4cf2 |
|
60b4db7e |
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
}
|
54240f8d |
func buildImageFromPath(name, path string, useCache bool, buildFlags ...string) (string, error) { |
5b0d4cf2 |
args := []string{"build", "-t", name}
if !useCache {
args = append(args, "--no-cache")
} |
54240f8d |
args = append(args, buildFlags...) |
5b0d4cf2 |
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)
}
|
6b3c9281 |
type gitServer interface { |
44ffb199 |
URL() string
Close() error
}
type localGitServer struct { |
5b0d4cf2 |
*httptest.Server |
44ffb199 |
}
func (r *localGitServer) Close() error {
r.Server.Close()
return nil
}
func (r *localGitServer) URL() string {
return r.Server.URL
}
|
6b3c9281 |
type fakeGit struct { |
44ffb199 |
root string |
6b3c9281 |
server gitServer |
5b0d4cf2 |
RepoURL string
}
|
6b3c9281 |
func (g *fakeGit) Close() { |
44ffb199 |
g.server.Close()
os.RemoveAll(g.root) |
5b0d4cf2 |
}
|
6b3c9281 |
func newFakeGit(name string, files map[string]string, enforceLocalServer bool) (*fakeGit, error) { |
44ffb199 |
ctx, err := fakeContextWithFiles(files) |
5b0d4cf2 |
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 { |
c0e63224 |
return nil, fmt.Errorf("error trying to init repo: %s (%s)", err, output) |
5b0d4cf2 |
}
err = os.Chdir(ctx.Dir)
if err != nil {
return nil, err
} |
ed345fb1 |
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)
} |
5b0d4cf2 |
if output, err := exec.Command("git", "add", "*").CombinedOutput(); err != nil { |
c0e63224 |
return nil, fmt.Errorf("error trying to add files to repo: %s (%s)", err, output) |
5b0d4cf2 |
}
if output, err := exec.Command("git", "commit", "-a", "-m", "Initial commit").CombinedOutput(); err != nil { |
c0e63224 |
return nil, fmt.Errorf("error trying to commit to repo: %s (%s)", err, output) |
5b0d4cf2 |
}
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) |
c0e63224 |
return nil, fmt.Errorf("error trying to clone --bare: %s (%s)", err, output) |
5b0d4cf2 |
}
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) |
c0e63224 |
return nil, fmt.Errorf("error trying to git update-server-info: %s (%s)", err, output) |
5b0d4cf2 |
}
err = os.Chdir(curdir)
if err != nil {
os.RemoveAll(root)
return nil, err
} |
44ffb199 |
|
6b3c9281 |
var server gitServer |
44ffb199 |
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 { |
927b334e |
// always start a local http server on CLI test machine |
44ffb199 |
httpServer := httptest.NewServer(http.FileServer(http.Dir(root)))
server = &localGitServer{httpServer}
} |
6b3c9281 |
return &fakeGit{ |
44ffb199 |
root: root,
server: server,
RepoURL: fmt.Sprintf("%s/%s.git", server.URL(), name), |
5b0d4cf2 |
}, nil
} |
5b27fbc0 |
// 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. |
927b334e |
// Fail the test when error occurs. |
dc944ea7 |
func writeFile(dst, content string, c *check.C) { |
5b27fbc0 |
// Create subdirectories if necessary |
a1d4b7dd |
c.Assert(os.MkdirAll(path.Dir(dst), 0700), check.IsNil) |
5b27fbc0 |
f, err := os.OpenFile(dst, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0700) |
a1d4b7dd |
c.Assert(err, check.IsNil) |
d28b0ba1 |
defer f.Close() |
5b27fbc0 |
// Write content (truncate if it exists) |
a1d4b7dd |
_, err = io.Copy(f, strings.NewReader(content))
c.Assert(err, check.IsNil) |
5b27fbc0 |
}
// Return the contents of file at path `src`. |
927b334e |
// Fail the test when error occurs. |
dc944ea7 |
func readFile(src string, c *check.C) (content string) { |
d5ea4bae |
data, err := ioutil.ReadFile(src) |
a1d4b7dd |
c.Assert(err, check.IsNil) |
d5ea4bae |
|
5b27fbc0 |
return string(data)
} |
20575d20 |
|
6b3c9281 |
func containerStorageFile(containerID, basename string) string { |
442b4562 |
return filepath.Join(containerStoragePath, containerID, basename) |
20575d20 |
}
|
68bc8de1 |
// docker commands that use this function must be run with the '-d' switch. |
20575d20 |
func runCommandAndReadContainerFile(filename string, cmd *exec.Cmd) ([]byte, error) {
out, _, err := runCommandWithOutput(cmd)
if err != nil { |
68bc8de1 |
return nil, fmt.Errorf("%v: %q", err, out) |
20575d20 |
}
contID := strings.TrimSpace(out)
|
799d9605 |
if err := waitRun(contID); err != nil {
return nil, fmt.Errorf("%v: %q", contID, err)
}
|
20575d20 |
return readContainerFile(contID, filename)
}
|
6b3c9281 |
func readContainerFile(containerID, filename string) ([]byte, error) {
f, err := os.Open(containerStorageFile(containerID, filename)) |
20575d20 |
if err != nil {
return nil, err
}
defer f.Close()
content, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
return content, nil
} |
dbec2317 |
|
6b3c9281 |
func readContainerFileWithExec(containerID, filename string) ([]byte, error) {
out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "exec", containerID, "cat", filename)) |
6bbb4561 |
return []byte(out), err
}
|
e424c54d |
// daemonTime provides the current time on the daemon host |
dc944ea7 |
func daemonTime(c *check.C) time.Time { |
e424c54d |
if isLocalDaemon {
return time.Now()
}
|
c7845e27 |
status, body, err := sockRequest("GET", "/info", nil)
c.Assert(err, check.IsNil) |
a1d4b7dd |
c.Assert(status, check.Equals, http.StatusOK) |
e424c54d |
type infoJSON struct {
SystemTime string
}
var info infoJSON |
a1d4b7dd |
err = json.Unmarshal(body, &info)
c.Assert(err, check.IsNil, check.Commentf("unable to unmarshal GET /info response")) |
e424c54d |
dt, err := time.Parse(time.RFC3339Nano, info.SystemTime) |
a1d4b7dd |
c.Assert(err, check.IsNil, check.Commentf("invalid time format in GET /info response")) |
e424c54d |
return dt
}
|
55053d35 |
// daemonUnixTime returns the current time on the daemon host with nanoseconds precission.
// It return the time formatted how the client sends timestamps to the server.
func daemonUnixTime(c *check.C) string {
return parseEventTime(daemonTime(c))
}
func parseEventTime(t time.Time) string {
return fmt.Sprintf("%d.%09d", t.Unix(), int64(t.Nanosecond()))
}
|
1b5c2e1d |
func setupRegistry(c *check.C, schema1 bool, auth, tokenURL string) *testRegistryV2 {
reg, err := newTestRegistryV2(c, schema1, auth, tokenURL) |
a1d4b7dd |
c.Assert(err, check.IsNil) |
de8ea06d |
// Wait for registry to be ready to serve requests. |
88c1bc10 |
for i := 0; i != 50; i++ { |
de8ea06d |
if err = reg.Ping(); err == nil {
break
}
time.Sleep(100 * time.Millisecond)
}
|
55cec657 |
c.Assert(err, check.IsNil, check.Commentf("Timeout waiting for test registry to become available: %v", err)) |
f696b107 |
return reg |
dbec2317 |
} |
4f377fbe |
|
58a1de9b |
func setupNotary(c *check.C) *testNotary {
ts, err := newTestNotary(c) |
a1d4b7dd |
c.Assert(err, check.IsNil) |
58a1de9b |
return ts
}
|
d18689df |
// appendBaseEnv appends the minimum set of environment variables to exec the
// docker cli binary for testing with correct configuration to the given env
// list. |
f4a1e3db |
func appendBaseEnv(isTLS bool, env ...string) []string { |
d18689df |
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", |
9e7651db |
// testing help text requires the $PATH to dockerd is set
"PATH", |
d18689df |
} |
f4a1e3db |
if isTLS {
preserveList = append(preserveList, "DOCKER_TLS_VERIFY", "DOCKER_CERT_PATH")
} |
d18689df |
for _, key := range preserveList {
if val := os.Getenv(key); val != "" {
env = append(env, fmt.Sprintf("%s=%s", key, val))
} |
4f377fbe |
}
return env
} |
38295d4b |
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
} |
6b8129d1 |
|
6549d651 |
func buildImageWithOutInDamon(socket string, name, dockerfile string, useCache bool) (string, error) {
args := []string{"--host", socket} |
6b8129d1 |
buildCmd := buildImageCmdArgs(args, name, dockerfile, useCache)
out, exitCode, err := runCommandWithOutput(buildCmd)
if err != nil || exitCode != 0 { |
6549d651 |
return out, fmt.Errorf("failed to build the image: %s, error: %v", out, err) |
6b8129d1 |
} |
6549d651 |
return out, nil |
6b8129d1 |
}
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
} |
51090717 |
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 { |
8a5ab83d |
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) |
51090717 |
}
// 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. |
8a5ab83d |
func waitInspect(name, expr, expected string, timeout time.Duration) error { |
76a76514 |
return waitInspectWithArgs(name, expr, expected, timeout)
}
func waitInspectWithArgs(name, expr, expected string, timeout time.Duration, arg ...string) error { |
8a5ab83d |
after := time.After(timeout) |
51090717 |
|
76a76514 |
args := append(arg, "inspect", "-f", expr, name) |
51090717 |
for { |
76a76514 |
cmd := exec.Command(dockerBinary, args...) |
51090717 |
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
} |
f301c576 |
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
} |
777ee34b |
// Run a long running idle task in a background container using the
// system-specific default image and command.
func runSleepingContainer(c *check.C, extraArgs ...string) (string, int) {
return runSleepingContainerInImage(c, defaultSleepImage, extraArgs...)
}
// Run a long running idle task in a background container using the specified
// image and the system-specific command.
func runSleepingContainerInImage(c *check.C, image string, extraArgs ...string) (string, int) {
args := []string{"run", "-d"}
args = append(args, extraArgs...)
args = append(args, image)
args = append(args, defaultSleepCommand...)
return dockerCmd(c, args...)
} |
96c52216 |
|
40be5dba |
func getRootUIDGID() (int, int, error) {
uidgid := strings.Split(filepath.Base(dockerBasePath), ".")
if len(uidgid) == 1 {
//user namespace remapping is not turned on; return 0
return 0, 0, nil
}
uid, err := strconv.Atoi(uidgid[0])
if err != nil {
return 0, 0, err
}
gid, err := strconv.Atoi(uidgid[1])
if err != nil {
return 0, 0, err
}
return uid, gid, nil
}
|
96c52216 |
// minimalBaseImage returns the name of the minimal base image for the current
// daemon platform.
func minimalBaseImage() string {
if daemonPlatform == "windows" {
return WindowsBaseImage
}
return "scratch"
} |
0c7c9df8 |
func getGoroutineNumber() (int, error) {
i := struct {
NGoroutines int
}{}
status, b, err := sockRequest("GET", "/info", nil)
if err != nil {
return 0, err
}
if status != http.StatusOK {
return 0, fmt.Errorf("http status code: %d", status)
}
if err := json.Unmarshal(b, &i); err != nil {
return 0, err
}
return i.NGoroutines, nil
}
func waitForGoroutines(expected int) error {
t := time.After(30 * time.Second)
for {
select {
case <-t:
n, err := getGoroutineNumber()
if err != nil {
return err
}
if n > expected {
return fmt.Errorf("leaked goroutines: expected less than or equal to %d, got: %d", expected, n)
}
default:
n, err := getGoroutineNumber()
if err != nil {
return err
}
if n <= expected {
return nil
}
time.Sleep(200 * time.Millisecond)
}
}
} |
322e2a7d |
// getErrorMessage returns the error message from an error API response
func getErrorMessage(c *check.C, body []byte) string {
var resp types.ErrorResponse
c.Assert(json.Unmarshal(body, &resp), check.IsNil)
return strings.TrimSpace(resp.Message)
} |
0d88d5b6 |
func waitAndAssert(c *check.C, timeout time.Duration, f checkF, checker check.Checker, args ...interface{}) {
after := time.After(timeout)
for {
v, comment := f(c)
assert, _ := checker.Check(append([]interface{}{v}, args...), checker.Info().Params)
select {
case <-after:
assert = true
default:
}
if assert {
if comment != nil {
args = append(args, comment)
}
c.Assert(v, checker, args...)
return
}
time.Sleep(100 * time.Millisecond)
}
}
type checkF func(*check.C) (interface{}, check.CommentInterface)
type reducer func(...interface{}) interface{}
func reducedCheck(r reducer, funcs ...checkF) checkF {
return func(c *check.C) (interface{}, check.CommentInterface) {
var values []interface{}
var comments []string
for _, f := range funcs {
v, comment := f(c)
values = append(values, v)
if comment != nil {
comments = append(comments, comment.CheckCommentString())
}
}
return r(values...), check.Commentf("%v", strings.Join(comments, ", "))
}
}
func sumAsIntegers(vals ...interface{}) interface{} {
var s int
for _, v := range vals {
s += v.(int)
}
return s
} |