2014-02-25 11:17:48 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-04-19 18:30:59 -04:00
|
|
|
"context"
|
2014-12-01 12:16:49 -05:00
|
|
|
"encoding/json"
|
2014-07-23 14:12:42 -04:00
|
|
|
"errors"
|
2014-02-25 11:17:48 -05:00
|
|
|
"fmt"
|
2014-08-27 20:25:10 -04:00
|
|
|
"io"
|
2014-05-31 08:42:49 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
2014-07-24 03:19:16 -04:00
|
|
|
"path/filepath"
|
2014-05-19 12:47:35 -04:00
|
|
|
"strconv"
|
2014-02-25 11:17:48 -05:00
|
|
|
"strings"
|
2014-07-23 14:12:42 -04:00
|
|
|
"time"
|
2015-02-14 05:27:31 -05:00
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2017-05-23 23:56:26 -04:00
|
|
|
"github.com/docker/docker/client"
|
2016-12-30 12:23:00 -05:00
|
|
|
"github.com/docker/docker/integration-cli/checker"
|
2017-03-23 13:35:22 -04:00
|
|
|
"github.com/docker/docker/integration-cli/cli"
|
2016-12-09 04:17:53 -05:00
|
|
|
"github.com/docker/docker/integration-cli/daemon"
|
2018-04-17 04:22:04 -04:00
|
|
|
"github.com/docker/docker/internal/test/request"
|
2015-04-18 12:46:47 -04:00
|
|
|
"github.com/go-check/check"
|
2018-06-11 09:32:11 -04:00
|
|
|
"gotest.tools/icmd"
|
2014-02-25 11:17:48 -05:00
|
|
|
)
|
|
|
|
|
2017-03-01 12:45:04 -05:00
|
|
|
// Deprecated
|
2015-01-14 19:08:28 -05:00
|
|
|
func daemonHost() string {
|
2017-03-06 10:35:27 -05:00
|
|
|
return request.DaemonHost()
|
2015-01-14 19:08:28 -05:00
|
|
|
}
|
|
|
|
|
2014-10-07 18:39:50 -04:00
|
|
|
func deleteImages(images ...string) error {
|
2016-08-04 12:57:34 -04:00
|
|
|
args := []string{dockerBinary, "rmi", "-f"}
|
|
|
|
return icmd.RunCmd(icmd.Cmd{Command: append(args, images...)}).Error
|
2014-02-25 11:17:48 -05:00
|
|
|
}
|
2014-04-03 20:57:41 -04:00
|
|
|
|
2017-03-27 11:12:48 -04:00
|
|
|
// Deprecated: use cli.Docker or cli.DockerCmd
|
2015-07-27 14:13:25 -04:00
|
|
|
func dockerCmdWithError(args ...string) (string, int, error) {
|
2017-03-27 11:12:48 -04:00
|
|
|
result := cli.Docker(cli.Args(args...))
|
2016-08-04 12:57:34 -04:00
|
|
|
if result.Error != nil {
|
2016-08-16 17:51:38 -04:00
|
|
|
return result.Combined(), result.ExitCode, result.Compare(icmd.Success)
|
2016-03-24 12:43:04 -04:00
|
|
|
}
|
2016-08-04 12:57:34 -04:00
|
|
|
return result.Combined(), result.ExitCode, result.Error
|
2015-07-14 02:35:06 -04:00
|
|
|
}
|
|
|
|
|
2017-03-27 11:12:48 -04:00
|
|
|
// Deprecated: use cli.Docker or cli.DockerCmd
|
2015-04-18 12:46:47 -04:00
|
|
|
func dockerCmd(c *check.C, args ...string) (string, int) {
|
2017-03-27 11:12:48 -04:00
|
|
|
result := cli.DockerCmd(c, args...)
|
2016-08-04 12:57:34 -04:00
|
|
|
return result.Combined(), result.ExitCode
|
|
|
|
}
|
|
|
|
|
2017-03-27 11:12:48 -04:00
|
|
|
// Deprecated: use cli.Docker or cli.DockerCmd
|
2016-08-04 12:57:34 -04:00
|
|
|
func dockerCmdWithResult(args ...string) *icmd.Result {
|
2017-03-27 11:12:48 -04:00
|
|
|
return cli.Docker(cli.Args(args...))
|
2016-02-24 13:17:25 -05:00
|
|
|
}
|
|
|
|
|
2015-10-26 08:00:49 -04: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 13:27:24 -04:00
|
|
|
return strings.Trim(out, " \r\n'")
|
|
|
|
}
|
2014-05-19 12:47:35 -04:00
|
|
|
|
2017-01-16 10:39:12 -05:00
|
|
|
func getContainerCount(c *check.C) int {
|
2014-05-19 12:47:35 -04:00
|
|
|
const containers = "Containers:"
|
|
|
|
|
2017-01-16 10:39:12 -05:00
|
|
|
result := icmd.RunCommand(dockerBinary, "info")
|
|
|
|
result.Assert(c, icmd.Success)
|
2014-05-19 12:47:35 -04:00
|
|
|
|
2017-01-16 10:39:12 -05:00
|
|
|
lines := strings.Split(result.Combined(), "\n")
|
2014-05-19 12:47:35 -04:00
|
|
|
for _, line := range lines {
|
|
|
|
if strings.Contains(line, containers) {
|
2015-04-06 09:21:18 -04:00
|
|
|
output := strings.TrimSpace(line)
|
2014-05-19 12:47:35 -04:00
|
|
|
output = strings.TrimLeft(output, containers)
|
|
|
|
output = strings.Trim(output, " ")
|
|
|
|
containerCount, err := strconv.Atoi(output)
|
2017-01-16 10:39:12 -05:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
return containerCount
|
2014-05-19 12:47:35 -04:00
|
|
|
}
|
|
|
|
}
|
2017-01-16 10:39:12 -05:00
|
|
|
return 0
|
2014-05-19 12:47:35 -04:00
|
|
|
}
|
2014-05-31 08:42:49 -04:00
|
|
|
|
2017-01-10 13:16:25 -05:00
|
|
|
func inspectFieldAndUnmarshall(c *check.C, name, field string, output interface{}) {
|
2016-01-28 09:19:25 -05:00
|
|
|
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))
|
2015-01-06 19:04:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-27 11:12:48 -04:00
|
|
|
// Deprecated: use cli.Inspect
|
2015-02-19 05:01:27 -05:00
|
|
|
func inspectFilter(name, filter string) (string, error) {
|
|
|
|
format := fmt.Sprintf("{{%s}}", filter)
|
2017-01-10 13:16:25 -05:00
|
|
|
result := icmd.RunCommand(dockerBinary, "inspect", "-f", format, name)
|
|
|
|
if result.Error != nil || result.ExitCode != 0 {
|
|
|
|
return "", fmt.Errorf("failed to inspect %s: %s", name, result.Combined())
|
2014-10-20 15:27:26 -04:00
|
|
|
}
|
2017-01-10 13:16:25 -05:00
|
|
|
return strings.TrimSpace(result.Combined()), nil
|
2014-10-20 15:27:26 -04:00
|
|
|
}
|
|
|
|
|
2017-03-27 11:12:48 -04:00
|
|
|
// Deprecated: use cli.Inspect
|
2016-01-28 09:19:25 -05:00
|
|
|
func inspectFieldWithError(name, field string) (string, error) {
|
2015-02-19 05:01:27 -05:00
|
|
|
return inspectFilter(name, fmt.Sprintf(".%s", field))
|
|
|
|
}
|
|
|
|
|
2017-03-27 11:12:48 -04:00
|
|
|
// Deprecated: use cli.Inspect
|
2016-01-28 09:19:25 -05:00
|
|
|
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
|
2015-02-19 05:01:27 -05:00
|
|
|
}
|
|
|
|
|
2017-03-27 11:12:48 -04:00
|
|
|
// Deprecated: use cli.Inspect
|
2016-01-28 09:19:25 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-03-27 11:12:48 -04:00
|
|
|
// Deprecated: use cli.Inspect
|
2016-01-28 09:19:25 -05:00
|
|
|
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
|
2015-02-19 05:01:27 -05:00
|
|
|
}
|
|
|
|
|
2017-03-27 11:12:48 -04:00
|
|
|
// Deprecated: use cli.Inspect
|
2015-06-03 15:21:38 -04:00
|
|
|
func inspectMountSourceField(name, destination string) (string, error) {
|
|
|
|
m, err := inspectMountPoint(name, destination)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return m.Source, nil
|
|
|
|
}
|
|
|
|
|
2017-03-27 11:12:48 -04:00
|
|
|
// Deprecated: use cli.Inspect
|
2015-06-03 15:21:38 -04:00
|
|
|
func inspectMountPoint(name, destination string) (types.MountPoint, error) {
|
2016-01-28 09:19:25 -05:00
|
|
|
out, err := inspectFilter(name, "json .Mounts")
|
2015-06-03 15:21:38 -04:00
|
|
|
if err != nil {
|
|
|
|
return types.MountPoint{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return inspectMountPointJSON(out, destination)
|
|
|
|
}
|
|
|
|
|
2015-07-22 08:59:24 -04:00
|
|
|
var errMountNotFound = errors.New("mount point not found")
|
2015-06-03 15:21:38 -04:00
|
|
|
|
2017-03-27 11:12:48 -04:00
|
|
|
// Deprecated: use cli.Inspect
|
2015-06-03 15:21:38 -04:00
|
|
|
func inspectMountPointJSON(j, destination string) (types.MountPoint, error) {
|
|
|
|
var mp []types.MountPoint
|
2016-08-04 12:17:37 -04:00
|
|
|
if err := json.Unmarshal([]byte(j), &mp); err != nil {
|
2015-06-03 15:21:38 -04:00
|
|
|
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 08:59:24 -04:00
|
|
|
return types.MountPoint{}, errMountNotFound
|
2015-06-03 15:21:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return *m, nil
|
|
|
|
}
|
|
|
|
|
2017-03-27 11:12:48 -04:00
|
|
|
// Deprecated: use cli.Inspect
|
2017-01-16 10:39:12 -05:00
|
|
|
func inspectImage(c *check.C, name, filter string) string {
|
2016-03-30 17:26:02 -04:00
|
|
|
args := []string{"inspect", "--type", "image"}
|
|
|
|
if filter != "" {
|
|
|
|
format := fmt.Sprintf("{{%s}}", filter)
|
|
|
|
args = append(args, "-f", format)
|
|
|
|
}
|
|
|
|
args = append(args, name)
|
2017-01-16 10:39:12 -05:00
|
|
|
result := icmd.RunCommand(dockerBinary, args...)
|
|
|
|
result.Assert(c, icmd.Success)
|
|
|
|
return strings.TrimSpace(result.Combined())
|
2016-03-30 17:26:02 -04:00
|
|
|
}
|
|
|
|
|
2017-01-16 05:30:14 -05:00
|
|
|
func getIDByName(c *check.C, name string) string {
|
|
|
|
id, err := inspectFieldWithError(name, "Id")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
return id
|
2014-12-04 17:06:40 -05:00
|
|
|
}
|
|
|
|
|
2017-03-23 13:35:22 -04:00
|
|
|
// Deprecated: use cli.Build
|
|
|
|
func buildImageSuccessfully(c *check.C, name string, cmdOperators ...cli.CmdOperator) {
|
2017-01-16 05:30:14 -05:00
|
|
|
buildImage(name, cmdOperators...).Assert(c, icmd.Success)
|
2017-01-10 13:16:25 -05:00
|
|
|
}
|
|
|
|
|
2017-03-23 13:35:22 -04:00
|
|
|
// Deprecated: use cli.Build
|
|
|
|
func buildImage(name string, cmdOperators ...cli.CmdOperator) *icmd.Result {
|
|
|
|
return cli.Docker(cli.Build(name), cmdOperators...)
|
2017-01-10 13:16:25 -05:00
|
|
|
}
|
|
|
|
|
2014-08-27 20:25:10 -04: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 11:00:39 -05:00
|
|
|
// Fail the test when error occurs.
|
2015-04-18 12:46:47 -04:00
|
|
|
func writeFile(dst, content string, c *check.C) {
|
2014-08-27 20:25:10 -04:00
|
|
|
// Create subdirectories if necessary
|
2015-10-11 03:33:24 -04:00
|
|
|
c.Assert(os.MkdirAll(path.Dir(dst), 0700), check.IsNil)
|
2014-08-27 20:25:10 -04:00
|
|
|
f, err := os.OpenFile(dst, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0700)
|
2015-10-11 03:33:24 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2015-07-09 06:52:18 -04:00
|
|
|
defer f.Close()
|
2014-08-27 20:25:10 -04:00
|
|
|
// Write content (truncate if it exists)
|
2015-10-11 03:33:24 -04:00
|
|
|
_, err = io.Copy(f, strings.NewReader(content))
|
|
|
|
c.Assert(err, check.IsNil)
|
2014-08-27 20:25:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the contents of file at path `src`.
|
2015-12-13 11:00:39 -05:00
|
|
|
// Fail the test when error occurs.
|
2015-04-18 12:46:47 -04:00
|
|
|
func readFile(src string, c *check.C) (content string) {
|
2015-02-26 22:53:11 -05:00
|
|
|
data, err := ioutil.ReadFile(src)
|
2015-10-11 03:33:24 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2015-02-26 22:53:11 -05:00
|
|
|
|
2014-08-27 20:25:10 -04:00
|
|
|
return string(data)
|
|
|
|
}
|
2014-10-24 14:12:54 -04:00
|
|
|
|
2015-07-22 08:59:24 -04:00
|
|
|
func containerStorageFile(containerID, basename string) string {
|
2017-08-25 18:48:36 -04:00
|
|
|
return filepath.Join(testEnv.PlatformDefaults.ContainerStoragePath, containerID, basename)
|
2014-10-24 14:12:54 -04:00
|
|
|
}
|
|
|
|
|
2014-10-24 17:39:12 -04:00
|
|
|
// docker commands that use this function must be run with the '-d' switch.
|
2017-01-16 10:39:12 -05:00
|
|
|
func runCommandAndReadContainerFile(c *check.C, filename string, command string, args ...string) []byte {
|
|
|
|
result := icmd.RunCommand(command, args...)
|
|
|
|
result.Assert(c, icmd.Success)
|
|
|
|
contID := strings.TrimSpace(result.Combined())
|
2015-08-11 03:41:11 -04:00
|
|
|
if err := waitRun(contID); err != nil {
|
2017-01-16 10:39:12 -05:00
|
|
|
c.Fatalf("%v: %q", contID, err)
|
2015-08-11 03:41:11 -04:00
|
|
|
}
|
2017-01-16 10:39:12 -05:00
|
|
|
return readContainerFile(c, contID, filename)
|
2014-10-24 14:12:54 -04:00
|
|
|
}
|
|
|
|
|
2017-01-16 10:39:12 -05:00
|
|
|
func readContainerFile(c *check.C, containerID, filename string) []byte {
|
2015-07-22 08:59:24 -04:00
|
|
|
f, err := os.Open(containerStorageFile(containerID, filename))
|
2017-01-16 10:39:12 -05:00
|
|
|
c.Assert(err, checker.IsNil)
|
2014-10-24 14:12:54 -04:00
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
content, err := ioutil.ReadAll(f)
|
2017-01-16 10:39:12 -05:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
return content
|
2014-10-24 14:12:54 -04:00
|
|
|
}
|
2015-01-13 13:46:32 -05:00
|
|
|
|
2017-01-16 10:39:12 -05:00
|
|
|
func readContainerFileWithExec(c *check.C, containerID, filename string) []byte {
|
|
|
|
result := icmd.RunCommand(dockerBinary, "exec", containerID, "cat", filename)
|
|
|
|
result.Assert(c, icmd.Success)
|
|
|
|
return []byte(result.Combined())
|
2015-02-16 14:32:25 -05:00
|
|
|
}
|
|
|
|
|
2015-03-10 17:19:32 -04:00
|
|
|
// daemonTime provides the current time on the daemon host
|
2015-04-18 12:46:47 -04:00
|
|
|
func daemonTime(c *check.C) time.Time {
|
2017-08-25 18:48:36 -04:00
|
|
|
if testEnv.IsLocalDaemon() {
|
2015-03-10 17:19:32 -04:00
|
|
|
return time.Now()
|
|
|
|
}
|
2017-05-23 23:56:26 -04:00
|
|
|
cli, err := client.NewEnvClient()
|
2015-04-20 17:03:56 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2017-05-23 23:56:26 -04:00
|
|
|
defer cli.Close()
|
2015-03-10 17:19:32 -04:00
|
|
|
|
2017-05-23 23:56:26 -04:00
|
|
|
info, err := cli.Info(context.Background())
|
|
|
|
c.Assert(err, check.IsNil)
|
2015-03-10 17:19:32 -04:00
|
|
|
|
|
|
|
dt, err := time.Parse(time.RFC3339Nano, info.SystemTime)
|
2015-10-11 03:33:24 -04:00
|
|
|
c.Assert(err, check.IsNil, check.Commentf("invalid time format in GET /info response"))
|
2015-03-10 17:19:32 -04:00
|
|
|
return dt
|
|
|
|
}
|
|
|
|
|
2016-07-03 13:58:11 -04:00
|
|
|
// daemonUnixTime returns the current time on the daemon host with nanoseconds precision.
|
2016-04-11 14:52:34 -04:00
|
|
|
// 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()))
|
|
|
|
}
|
|
|
|
|
2015-03-07 04:03:44 -05: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.
|
2016-02-24 17:59:11 -05:00
|
|
|
func appendBaseEnv(isTLS bool, env ...string) []string {
|
2015-03-07 04:03:44 -05:00
|
|
|
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",
|
2016-02-19 17:42:51 -05:00
|
|
|
|
|
|
|
// testing help text requires the $PATH to dockerd is set
|
|
|
|
"PATH",
|
2015-03-07 04:03:44 -05:00
|
|
|
}
|
2016-02-24 17:59:11 -05:00
|
|
|
if isTLS {
|
|
|
|
preserveList = append(preserveList, "DOCKER_TLS_VERIFY", "DOCKER_CERT_PATH")
|
|
|
|
}
|
2015-03-07 04:03:44 -05:00
|
|
|
|
|
|
|
for _, key := range preserveList {
|
|
|
|
if val := os.Getenv(key); val != "" {
|
|
|
|
env = append(env, fmt.Sprintf("%s=%s", key, val))
|
|
|
|
}
|
2015-02-12 14:20:48 -05:00
|
|
|
}
|
|
|
|
return env
|
|
|
|
}
|
2015-07-24 14:23:20 -04: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-11 22:27:33 -04:00
|
|
|
|
2015-09-09 09:36:44 -04:00
|
|
|
// waitRun will wait for the specified container to be running, maximum 5 seconds.
|
2017-04-11 15:18:30 -04:00
|
|
|
// Deprecated: use cli.WaitFor
|
2015-09-09 09:36:44 -04:00
|
|
|
func waitRun(contID string) error {
|
2015-09-01 17:37:04 -04:00
|
|
|
return waitInspect(contID, "{{.State.Running}}", "true", 5*time.Second)
|
|
|
|
}
|
|
|
|
|
2015-09-09 09:36:44 -04: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.
|
2017-04-11 15:18:30 -04:00
|
|
|
// Deprecated: use cli.WaitFor
|
2015-09-01 17:37:04 -04:00
|
|
|
func waitInspect(name, expr, expected string, timeout time.Duration) error {
|
2016-02-03 16:03:32 -05:00
|
|
|
return waitInspectWithArgs(name, expr, expected, timeout)
|
|
|
|
}
|
|
|
|
|
2017-04-11 15:18:30 -04:00
|
|
|
// Deprecated: use cli.WaitFor
|
2016-02-03 16:03:32 -05:00
|
|
|
func waitInspectWithArgs(name, expr, expected string, timeout time.Duration, arg ...string) error {
|
2016-12-09 04:17:53 -05:00
|
|
|
return daemon.WaitInspectWithArgs(dockerBinary, name, expr, expected, timeout, arg...)
|
2015-09-09 09:36:44 -04:00
|
|
|
}
|
2015-10-30 14:57:15 -04:00
|
|
|
|
|
|
|
func getInspectBody(c *check.C, version, id string) []byte {
|
2018-01-31 18:01:29 -05:00
|
|
|
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithVersion(version))
|
2017-05-23 23:56:26 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
defer cli.Close()
|
|
|
|
_, body, err := cli.ContainerInspectWithRaw(context.Background(), id, false)
|
2015-10-30 14:57:15 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
return body
|
|
|
|
}
|
2016-01-26 23:16:36 -05:00
|
|
|
|
|
|
|
// Run a long running idle task in a background container using the
|
|
|
|
// system-specific default image and command.
|
2017-04-16 17:39:30 -04:00
|
|
|
func runSleepingContainer(c *check.C, extraArgs ...string) string {
|
2016-01-26 23:16:36 -05:00
|
|
|
return runSleepingContainerInImage(c, defaultSleepImage, extraArgs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run a long running idle task in a background container using the specified
|
|
|
|
// image and the system-specific command.
|
2017-04-16 17:39:30 -04:00
|
|
|
func runSleepingContainerInImage(c *check.C, image string, extraArgs ...string) string {
|
2016-01-26 23:16:36 -05:00
|
|
|
args := []string{"run", "-d"}
|
|
|
|
args = append(args, extraArgs...)
|
|
|
|
args = append(args, image)
|
2016-08-17 18:46:28 -04:00
|
|
|
args = append(args, sleepCommandForDaemonPlatform()...)
|
2017-07-09 09:34:14 -04:00
|
|
|
return strings.TrimSpace(cli.DockerCmd(c, args...).Combined())
|
2016-01-26 23:16:36 -05:00
|
|
|
}
|
2016-02-04 16:06:12 -05:00
|
|
|
|
|
|
|
// minimalBaseImage returns the name of the minimal base image for the current
|
|
|
|
// daemon platform.
|
|
|
|
func minimalBaseImage() string {
|
2018-01-15 09:31:02 -05:00
|
|
|
return testEnv.PlatformDefaults.BaseImage
|
2016-02-04 16:06:12 -05:00
|
|
|
}
|
2016-03-14 18:52:50 -04:00
|
|
|
|
|
|
|
func getGoroutineNumber() (int, error) {
|
2017-05-23 23:56:26 -04:00
|
|
|
cli, err := client.NewEnvClient()
|
2016-03-14 18:52:50 -04:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2017-05-23 23:56:26 -04:00
|
|
|
defer cli.Close()
|
|
|
|
|
|
|
|
info, err := cli.Info(context.Background())
|
|
|
|
if err != nil {
|
2016-03-14 18:52:50 -04:00
|
|
|
return 0, err
|
|
|
|
}
|
2017-05-23 23:56:26 -04:00
|
|
|
return info.NGoroutines, nil
|
2016-03-14 18:52:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-21 07:56:04 -04:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
2016-06-13 22:54:20 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|