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
|
|
|
"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"
|
2019-09-09 17:06:12 -04:00
|
|
|
"testing"
|
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"
|
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"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/icmd"
|
|
|
|
"gotest.tools/v3/poll"
|
2014-02-25 11:17:48 -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
|
2019-09-09 17:45:43 -04:00
|
|
|
func dockerCmd(c testing.TB, args ...string) (string, int) {
|
2021-03-18 14:36:00 -04:00
|
|
|
c.Helper()
|
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
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func findContainerIP(c *testing.T, id string, network string) string {
|
2019-09-05 12:12:33 -04:00
|
|
|
c.Helper()
|
2015-10-26 08:00:49 -04:00
|
|
|
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
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func getContainerCount(c *testing.T) int {
|
2019-09-05 12:12:33 -04:00
|
|
|
c.Helper()
|
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)
|
2019-08-28 13:39:50 -04:00
|
|
|
output = strings.TrimPrefix(output, containers)
|
2014-05-19 12:47:35 -04:00
|
|
|
output = strings.Trim(output, " ")
|
|
|
|
containerCount, err := strconv.Atoi(output)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2017-01-16 10:39:12 -05:00
|
|
|
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
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func inspectFieldAndUnmarshall(c *testing.T, name, field string, output interface{}) {
|
2019-09-05 12:12:33 -04:00
|
|
|
c.Helper()
|
2016-01-28 09:19:25 -05:00
|
|
|
str := inspectFieldJSON(c, name, field)
|
|
|
|
err := json.Unmarshal([]byte(str), output)
|
2022-01-28 06:48:36 -05:00
|
|
|
assert.Assert(c, err == nil, "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) {
|
2022-02-15 06:50:21 -05:00
|
|
|
return inspectFilter(name, "."+field)
|
2015-02-19 05:01:27 -05:00
|
|
|
}
|
|
|
|
|
2017-03-27 11:12:48 -04:00
|
|
|
// Deprecated: use cli.Inspect
|
2019-09-09 17:05:55 -04:00
|
|
|
func inspectField(c *testing.T, name, field string) string {
|
2019-09-05 12:12:33 -04:00
|
|
|
c.Helper()
|
2022-02-15 06:50:21 -05:00
|
|
|
out, err := inspectFilter(name, "."+field)
|
2022-01-28 06:48:36 -05:00
|
|
|
assert.NilError(c, err)
|
2016-01-28 09:19:25 -05:00
|
|
|
return out
|
2015-02-19 05:01:27 -05:00
|
|
|
}
|
|
|
|
|
2017-03-27 11:12:48 -04:00
|
|
|
// Deprecated: use cli.Inspect
|
2019-09-09 17:05:55 -04:00
|
|
|
func inspectFieldJSON(c *testing.T, name, field string) string {
|
2019-09-05 12:12:33 -04:00
|
|
|
c.Helper()
|
2022-02-15 06:50:21 -05:00
|
|
|
out, err := inspectFilter(name, "json ."+field)
|
2022-01-28 06:48:36 -05:00
|
|
|
assert.NilError(c, err)
|
2016-01-28 09:19:25 -05:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2017-03-27 11:12:48 -04:00
|
|
|
// Deprecated: use cli.Inspect
|
2019-09-09 17:05:55 -04:00
|
|
|
func inspectFieldMap(c *testing.T, name, path, field string) string {
|
2019-09-05 12:12:33 -04:00
|
|
|
c.Helper()
|
2016-01-28 09:19:25 -05:00
|
|
|
out, err := inspectFilter(name, fmt.Sprintf("index .%s %q", path, field))
|
2022-01-28 06:48:36 -05:00
|
|
|
assert.NilError(c, err)
|
2016-01-28 09:19:25 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func getIDByName(c *testing.T, name string) string {
|
2019-09-05 12:12:33 -04:00
|
|
|
c.Helper()
|
2017-01-16 05:30:14 -05:00
|
|
|
id, err := inspectFieldWithError(name, "Id")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2017-01-16 05:30:14 -05:00
|
|
|
return id
|
2014-12-04 17:06:40 -05:00
|
|
|
}
|
|
|
|
|
2017-03-23 13:35:22 -04:00
|
|
|
// Deprecated: use cli.Build
|
2019-09-09 17:05:55 -04:00
|
|
|
func buildImageSuccessfully(c *testing.T, name string, cmdOperators ...cli.CmdOperator) {
|
2019-09-05 12:12:33 -04:00
|
|
|
c.Helper()
|
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.
|
2019-09-09 17:05:55 -04:00
|
|
|
func writeFile(dst, content string, c *testing.T) {
|
2019-09-05 12:12:33 -04:00
|
|
|
c.Helper()
|
2014-08-27 20:25:10 -04:00
|
|
|
// Create subdirectories if necessary
|
2019-09-09 17:05:57 -04:00
|
|
|
assert.Assert(c, os.MkdirAll(path.Dir(dst), 0700) == nil)
|
2014-08-27 20:25:10 -04:00
|
|
|
f, err := os.OpenFile(dst, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0700)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
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))
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
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.
|
2019-09-09 17:05:55 -04:00
|
|
|
func readFile(src string, c *testing.T) (content string) {
|
2019-09-05 12:12:33 -04:00
|
|
|
c.Helper()
|
2021-08-24 06:10:50 -04:00
|
|
|
data, err := os.ReadFile(src)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
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.
|
2019-09-09 17:05:55 -04:00
|
|
|
func runCommandAndReadContainerFile(c *testing.T, filename string, command string, args ...string) []byte {
|
2019-09-05 12:12:33 -04:00
|
|
|
c.Helper()
|
2017-01-16 10:39:12 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func readContainerFile(c *testing.T, containerID, filename string) []byte {
|
2019-09-05 12:12:33 -04:00
|
|
|
c.Helper()
|
2015-07-22 08:59:24 -04:00
|
|
|
f, err := os.Open(containerStorageFile(containerID, filename))
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2014-10-24 14:12:54 -04:00
|
|
|
defer f.Close()
|
|
|
|
|
2021-08-24 06:10:50 -04:00
|
|
|
content, err := io.ReadAll(f)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2017-01-16 10:39:12 -05:00
|
|
|
return content
|
2014-10-24 14:12:54 -04:00
|
|
|
}
|
2015-01-13 13:46:32 -05:00
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func readContainerFileWithExec(c *testing.T, containerID, filename string) []byte {
|
2019-09-05 12:12:33 -04:00
|
|
|
c.Helper()
|
2017-01-16 10:39:12 -05:00
|
|
|
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
|
2019-09-09 17:05:55 -04:00
|
|
|
func daemonTime(c *testing.T) time.Time {
|
2019-09-05 12:12:33 -04:00
|
|
|
c.Helper()
|
2017-08-25 18:48:36 -04:00
|
|
|
if testEnv.IsLocalDaemon() {
|
2015-03-10 17:19:32 -04:00
|
|
|
return time.Now()
|
|
|
|
}
|
2019-01-03 16:49:00 -05:00
|
|
|
cli, err := client.NewClientWithOpts(client.FromEnv)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
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())
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-03-10 17:19:32 -04:00
|
|
|
|
|
|
|
dt, err := time.Parse(time.RFC3339Nano, info.SystemTime)
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Assert(c, err == nil, "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.
|
2019-09-09 17:05:55 -04:00
|
|
|
func daemonUnixTime(c *testing.T) string {
|
2019-09-05 12:12:33 -04:00
|
|
|
c.Helper()
|
2016-04-11 14:52:34 -04:00
|
|
|
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
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func createTmpFile(c *testing.T, content string) string {
|
2019-09-05 12:12:33 -04:00
|
|
|
c.Helper()
|
2021-08-24 06:10:50 -04:00
|
|
|
f, err := os.CreateTemp("", "testfile")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-07-24 14:23:20 -04:00
|
|
|
|
|
|
|
filename := f.Name()
|
|
|
|
|
2021-08-24 06:10:50 -04:00
|
|
|
err = os.WriteFile(filename, []byte(content), 0644)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-07-24 14:23:20 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func getInspectBody(c *testing.T, version, id string) []byte {
|
2019-09-05 12:12:33 -04:00
|
|
|
c.Helper()
|
2018-01-31 18:01:29 -05:00
|
|
|
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithVersion(version))
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2017-05-23 23:56:26 -04:00
|
|
|
defer cli.Close()
|
|
|
|
_, body, err := cli.ContainerInspectWithRaw(context.Background(), id, false)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-10-30 14:57:15 -04:00
|
|
|
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.
|
2019-09-09 17:05:55 -04:00
|
|
|
func runSleepingContainer(c *testing.T, extraArgs ...string) string {
|
2019-09-05 12:12:33 -04:00
|
|
|
c.Helper()
|
2019-07-08 12:42:08 -04:00
|
|
|
return runSleepingContainerInImage(c, "busybox", extraArgs...)
|
2016-01-26 23:16:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run a long running idle task in a background container using the specified
|
|
|
|
// image and the system-specific command.
|
2019-09-09 17:05:55 -04:00
|
|
|
func runSleepingContainerInImage(c *testing.T, image string, extraArgs ...string) string {
|
2019-09-05 12:12:33 -04:00
|
|
|
c.Helper()
|
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) {
|
2019-01-03 16:49:00 -05:00
|
|
|
cli, err := client.NewClientWithOpts(client.FromEnv)
|
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
|
2019-09-09 17:05:55 -04:00
|
|
|
func getErrorMessage(c *testing.T, body []byte) string {
|
2019-09-05 12:12:33 -04:00
|
|
|
c.Helper()
|
2016-05-21 07:56:04 -04:00
|
|
|
var resp types.ErrorResponse
|
2019-09-09 17:05:57 -04:00
|
|
|
assert.Assert(c, json.Unmarshal(body, &resp) == nil)
|
2016-05-21 07:56:04 -04:00
|
|
|
return strings.TrimSpace(resp.Message)
|
|
|
|
}
|
2016-06-13 22:54:20 -04:00
|
|
|
|
2019-09-09 17:08:22 -04:00
|
|
|
type checkF func(*testing.T) (interface{}, string)
|
2016-06-13 22:54:20 -04:00
|
|
|
type reducer func(...interface{}) interface{}
|
|
|
|
|
2019-08-26 12:04:47 -04:00
|
|
|
func pollCheck(t *testing.T, f checkF, compare func(x interface{}) assert.BoolOrComparison) poll.Check {
|
2019-08-09 06:41:13 -04:00
|
|
|
return func(poll.LogT) poll.Result {
|
2019-09-05 12:12:33 -04:00
|
|
|
t.Helper()
|
2019-08-26 12:04:47 -04:00
|
|
|
v, comment := f(t)
|
2019-09-09 16:49:16 -04:00
|
|
|
r := compare(v)
|
|
|
|
switch r := r.(type) {
|
|
|
|
case bool:
|
|
|
|
if r {
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
case cmp.Comparison:
|
|
|
|
if r().Success() {
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("pollCheck: type %T not implemented", r))
|
2019-08-09 06:41:13 -04:00
|
|
|
}
|
|
|
|
return poll.Continue(comment)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-26 12:04:47 -04:00
|
|
|
func reducedCheck(r reducer, funcs ...checkF) checkF {
|
2019-09-09 17:08:22 -04:00
|
|
|
return func(c *testing.T) (interface{}, string) {
|
2019-09-05 12:12:33 -04:00
|
|
|
c.Helper()
|
2016-06-13 22:54:20 -04:00
|
|
|
var values []interface{}
|
|
|
|
var comments []string
|
|
|
|
for _, f := range funcs {
|
2019-08-26 12:04:47 -04:00
|
|
|
v, comment := f(c)
|
2016-06-13 22:54:20 -04:00
|
|
|
values = append(values, v)
|
2019-08-26 10:45:33 -04:00
|
|
|
if len(comment) > 0 {
|
|
|
|
comments = append(comments, comment)
|
2016-06-13 22:54:20 -04:00
|
|
|
}
|
|
|
|
}
|
2019-09-09 17:08:22 -04:00
|
|
|
return r(values...), fmt.Sprintf("%v", strings.Join(comments, ", "))
|
2016-06-13 22:54:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func sumAsIntegers(vals ...interface{}) interface{} {
|
|
|
|
var s int
|
|
|
|
for _, v := range vals {
|
|
|
|
s += v.(int)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|