2014-02-25 16:17:48 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-04-19 22:30:59 +00:00
|
|
|
"context"
|
2014-12-01 17:16:49 +00:00
|
|
|
"encoding/json"
|
2014-07-23 18:12:42 +00:00
|
|
|
"errors"
|
2014-02-25 16:17:48 +00:00
|
|
|
"fmt"
|
2014-08-28 00:25:10 +00:00
|
|
|
"io"
|
2014-05-31 12:42:49 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2014-07-24 07:19:16 +00:00
|
|
|
"path/filepath"
|
2014-05-19 16:47:35 +00:00
|
|
|
"strconv"
|
2014-02-25 16:17:48 +00:00
|
|
|
"strings"
|
2019-09-09 21:06:12 +00:00
|
|
|
"testing"
|
2014-07-23 18:12:42 +00:00
|
|
|
"time"
|
2015-02-14 10:27:31 +00:00
|
|
|
|
2016-09-06 18:18:12 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
2017-05-24 03:56:26 +00:00
|
|
|
"github.com/docker/docker/client"
|
2017-03-23 17:35:22 +00:00
|
|
|
"github.com/docker/docker/integration-cli/cli"
|
2016-12-09 09:17:53 +00:00
|
|
|
"github.com/docker/docker/integration-cli/daemon"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/icmd"
|
|
|
|
"gotest.tools/v3/poll"
|
2014-02-25 16:17:48 +00:00
|
|
|
)
|
|
|
|
|
2014-10-07 22:39:50 +00:00
|
|
|
func deleteImages(images ...string) error {
|
2016-08-04 16:57:34 +00:00
|
|
|
args := []string{dockerBinary, "rmi", "-f"}
|
|
|
|
return icmd.RunCmd(icmd.Cmd{Command: append(args, images...)}).Error
|
2014-02-25 16:17:48 +00:00
|
|
|
}
|
2014-04-04 00:57:41 +00:00
|
|
|
|
2017-03-27 15:12:48 +00:00
|
|
|
// Deprecated: use cli.Docker or cli.DockerCmd
|
2015-07-27 18:13:25 +00:00
|
|
|
func dockerCmdWithError(args ...string) (string, int, error) {
|
2017-03-27 15:12:48 +00:00
|
|
|
result := cli.Docker(cli.Args(args...))
|
2016-08-04 16:57:34 +00:00
|
|
|
if result.Error != nil {
|
2016-08-16 21:51:38 +00:00
|
|
|
return result.Combined(), result.ExitCode, result.Compare(icmd.Success)
|
2016-03-24 16:43:04 +00:00
|
|
|
}
|
2016-08-04 16:57:34 +00:00
|
|
|
return result.Combined(), result.ExitCode, result.Error
|
2015-07-14 06:35:06 +00:00
|
|
|
}
|
|
|
|
|
2017-03-27 15:12:48 +00:00
|
|
|
// Deprecated: use cli.Docker or cli.DockerCmd
|
2019-09-09 21:45:43 +00:00
|
|
|
func dockerCmd(c testing.TB, args ...string) (string, int) {
|
2021-03-18 18:36:00 +00:00
|
|
|
c.Helper()
|
2017-03-27 15:12:48 +00:00
|
|
|
result := cli.DockerCmd(c, args...)
|
2016-08-04 16:57:34 +00:00
|
|
|
return result.Combined(), result.ExitCode
|
|
|
|
}
|
|
|
|
|
2017-03-27 15:12:48 +00:00
|
|
|
// Deprecated: use cli.Docker or cli.DockerCmd
|
2016-08-04 16:57:34 +00:00
|
|
|
func dockerCmdWithResult(args ...string) *icmd.Result {
|
2017-03-27 15:12:48 +00:00
|
|
|
return cli.Docker(cli.Args(args...))
|
2016-02-24 18:17:25 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func findContainerIP(c *testing.T, id string, network string) string {
|
2019-09-05 16:12:33 +00:00
|
|
|
c.Helper()
|
2015-10-26 12:00:49 +00:00
|
|
|
out, _ := dockerCmd(c, "inspect", fmt.Sprintf("--format='{{ .NetworkSettings.Networks.%s.IPAddress }}'", network), id)
|
2014-05-10 17:27:24 +00:00
|
|
|
return strings.Trim(out, " \r\n'")
|
|
|
|
}
|
2014-05-19 16:47:35 +00:00
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func getContainerCount(c *testing.T) int {
|
2019-09-05 16:12:33 +00:00
|
|
|
c.Helper()
|
2014-05-19 16:47:35 +00:00
|
|
|
const containers = "Containers:"
|
|
|
|
|
2017-01-16 15:39:12 +00:00
|
|
|
result := icmd.RunCommand(dockerBinary, "info")
|
|
|
|
result.Assert(c, icmd.Success)
|
2014-05-19 16:47:35 +00:00
|
|
|
|
2017-01-16 15:39:12 +00:00
|
|
|
lines := strings.Split(result.Combined(), "\n")
|
2014-05-19 16:47:35 +00:00
|
|
|
for _, line := range lines {
|
|
|
|
if strings.Contains(line, containers) {
|
2015-04-06 13:21:18 +00:00
|
|
|
output := strings.TrimSpace(line)
|
2019-08-28 17:39:50 +00:00
|
|
|
output = strings.TrimPrefix(output, containers)
|
2014-05-19 16:47:35 +00:00
|
|
|
output = strings.Trim(output, " ")
|
|
|
|
containerCount, err := strconv.Atoi(output)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2017-01-16 15:39:12 +00:00
|
|
|
return containerCount
|
2014-05-19 16:47:35 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-16 15:39:12 +00:00
|
|
|
return 0
|
2014-05-19 16:47:35 +00:00
|
|
|
}
|
2014-05-31 12:42:49 +00:00
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func inspectFieldAndUnmarshall(c *testing.T, name, field string, output interface{}) {
|
2019-09-05 16:12:33 +00:00
|
|
|
c.Helper()
|
2016-01-28 14:19:25 +00:00
|
|
|
str := inspectFieldJSON(c, name, field)
|
|
|
|
err := json.Unmarshal([]byte(str), output)
|
2022-01-28 11:48:36 +00:00
|
|
|
assert.Assert(c, err == nil, "failed to unmarshal: %v", err)
|
2015-01-07 00:04:10 +00:00
|
|
|
}
|
|
|
|
|
2022-02-23 12:19:28 +00:00
|
|
|
// Deprecated: use cli.Docker
|
2015-02-19 10:01:27 +00:00
|
|
|
func inspectFilter(name, filter string) (string, error) {
|
|
|
|
format := fmt.Sprintf("{{%s}}", filter)
|
2017-01-10 18:16:25 +00: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 19:27:26 +00:00
|
|
|
}
|
2017-01-10 18:16:25 +00:00
|
|
|
return strings.TrimSpace(result.Combined()), nil
|
2014-10-20 19:27:26 +00:00
|
|
|
}
|
|
|
|
|
2022-02-23 12:19:28 +00:00
|
|
|
// Deprecated: use cli.Docker
|
2016-01-28 14:19:25 +00:00
|
|
|
func inspectFieldWithError(name, field string) (string, error) {
|
2022-02-15 11:50:21 +00:00
|
|
|
return inspectFilter(name, "."+field)
|
2015-02-19 10:01:27 +00:00
|
|
|
}
|
|
|
|
|
2022-02-23 12:19:28 +00:00
|
|
|
// Deprecated: use cli.Docker
|
2019-09-09 21:05:55 +00:00
|
|
|
func inspectField(c *testing.T, name, field string) string {
|
2019-09-05 16:12:33 +00:00
|
|
|
c.Helper()
|
2022-02-15 11:50:21 +00:00
|
|
|
out, err := inspectFilter(name, "."+field)
|
2022-01-28 11:48:36 +00:00
|
|
|
assert.NilError(c, err)
|
2016-01-28 14:19:25 +00:00
|
|
|
return out
|
2015-02-19 10:01:27 +00:00
|
|
|
}
|
|
|
|
|
2022-02-23 12:19:28 +00:00
|
|
|
// Deprecated: use cli.Docker
|
2019-09-09 21:05:55 +00:00
|
|
|
func inspectFieldJSON(c *testing.T, name, field string) string {
|
2019-09-05 16:12:33 +00:00
|
|
|
c.Helper()
|
2022-02-15 11:50:21 +00:00
|
|
|
out, err := inspectFilter(name, "json ."+field)
|
2022-01-28 11:48:36 +00:00
|
|
|
assert.NilError(c, err)
|
2016-01-28 14:19:25 +00:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2022-02-23 12:19:28 +00:00
|
|
|
// Deprecated: use cli.Docker
|
2019-09-09 21:05:55 +00:00
|
|
|
func inspectFieldMap(c *testing.T, name, path, field string) string {
|
2019-09-05 16:12:33 +00:00
|
|
|
c.Helper()
|
2016-01-28 14:19:25 +00:00
|
|
|
out, err := inspectFilter(name, fmt.Sprintf("index .%s %q", path, field))
|
2022-01-28 11:48:36 +00:00
|
|
|
assert.NilError(c, err)
|
2016-01-28 14:19:25 +00:00
|
|
|
return out
|
2015-02-19 10:01:27 +00:00
|
|
|
}
|
|
|
|
|
2022-02-23 12:19:28 +00:00
|
|
|
// Deprecated: use cli.Docker
|
2015-06-03 19:21:38 +00:00
|
|
|
func inspectMountSourceField(name, destination string) (string, error) {
|
|
|
|
m, err := inspectMountPoint(name, destination)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return m.Source, nil
|
|
|
|
}
|
|
|
|
|
2022-06-03 09:01:25 +00:00
|
|
|
var errMountNotFound = errors.New("mount point not found")
|
|
|
|
|
2022-02-23 12:19:28 +00:00
|
|
|
// Deprecated: use cli.Docker
|
2015-06-03 19:21:38 +00:00
|
|
|
func inspectMountPoint(name, destination string) (types.MountPoint, error) {
|
2016-01-28 14:19:25 +00:00
|
|
|
out, err := inspectFilter(name, "json .Mounts")
|
2015-06-03 19:21:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return types.MountPoint{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var mp []types.MountPoint
|
2022-06-03 09:01:25 +00:00
|
|
|
if err := json.Unmarshal([]byte(out), &mp); err != nil {
|
2015-06-03 19:21:38 +00: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 12:59:24 +00:00
|
|
|
return types.MountPoint{}, errMountNotFound
|
2015-06-03 19:21:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return *m, nil
|
|
|
|
}
|
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func getIDByName(c *testing.T, name string) string {
|
2019-09-05 16:12:33 +00:00
|
|
|
c.Helper()
|
2017-01-16 10:30:14 +00:00
|
|
|
id, err := inspectFieldWithError(name, "Id")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2017-01-16 10:30:14 +00:00
|
|
|
return id
|
2014-12-04 22:06:40 +00:00
|
|
|
}
|
|
|
|
|
2022-02-23 12:19:28 +00:00
|
|
|
// Deprecated: use cli.Docker
|
2019-09-09 21:05:55 +00:00
|
|
|
func buildImageSuccessfully(c *testing.T, name string, cmdOperators ...cli.CmdOperator) {
|
2019-09-05 16:12:33 +00:00
|
|
|
c.Helper()
|
2017-01-16 10:30:14 +00:00
|
|
|
buildImage(name, cmdOperators...).Assert(c, icmd.Success)
|
2017-01-10 18:16:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-23 12:19:28 +00:00
|
|
|
// Deprecated: use cli.Docker
|
2017-03-23 17:35:22 +00:00
|
|
|
func buildImage(name string, cmdOperators ...cli.CmdOperator) *icmd.Result {
|
2022-02-23 12:19:28 +00:00
|
|
|
return cli.Docker(cli.Args("build", "-t", name), cmdOperators...)
|
2017-01-10 18:16:25 +00:00
|
|
|
}
|
|
|
|
|
2014-08-28 00:25:10 +00:00
|
|
|
// Write `content` to the file at path `dst`, creating it if necessary,
|
|
|
|
// as well as any missing directories.
|
|
|
|
// The file is truncated if it already exists.
|
2015-12-13 16:00:39 +00:00
|
|
|
// Fail the test when error occurs.
|
2019-09-09 21:05:55 +00:00
|
|
|
func writeFile(dst, content string, c *testing.T) {
|
2019-09-05 16:12:33 +00:00
|
|
|
c.Helper()
|
2014-08-28 00:25:10 +00:00
|
|
|
// Create subdirectories if necessary
|
2019-09-09 21:05:57 +00:00
|
|
|
assert.Assert(c, os.MkdirAll(path.Dir(dst), 0700) == nil)
|
2014-08-28 00:25:10 +00:00
|
|
|
f, err := os.OpenFile(dst, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0700)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-07-09 10:52:18 +00:00
|
|
|
defer f.Close()
|
2014-08-28 00:25:10 +00:00
|
|
|
// Write content (truncate if it exists)
|
2015-10-11 07:33:24 +00:00
|
|
|
_, err = io.Copy(f, strings.NewReader(content))
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2014-08-28 00:25:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the contents of file at path `src`.
|
2015-12-13 16:00:39 +00:00
|
|
|
// Fail the test when error occurs.
|
2019-09-09 21:05:55 +00:00
|
|
|
func readFile(src string, c *testing.T) (content string) {
|
2019-09-05 16:12:33 +00:00
|
|
|
c.Helper()
|
2021-08-24 10:10:50 +00:00
|
|
|
data, err := os.ReadFile(src)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-02-27 03:53:11 +00:00
|
|
|
|
2014-08-28 00:25:10 +00:00
|
|
|
return string(data)
|
|
|
|
}
|
2014-10-24 18:12:54 +00:00
|
|
|
|
2015-07-22 12:59:24 +00:00
|
|
|
func containerStorageFile(containerID, basename string) string {
|
2017-08-25 22:48:36 +00:00
|
|
|
return filepath.Join(testEnv.PlatformDefaults.ContainerStoragePath, containerID, basename)
|
2014-10-24 18:12:54 +00:00
|
|
|
}
|
|
|
|
|
2014-10-24 21:39:12 +00:00
|
|
|
// docker commands that use this function must be run with the '-d' switch.
|
2019-09-09 21:05:55 +00:00
|
|
|
func runCommandAndReadContainerFile(c *testing.T, filename string, command string, args ...string) []byte {
|
2019-09-05 16:12:33 +00:00
|
|
|
c.Helper()
|
2017-01-16 15:39:12 +00:00
|
|
|
result := icmd.RunCommand(command, args...)
|
|
|
|
result.Assert(c, icmd.Success)
|
|
|
|
contID := strings.TrimSpace(result.Combined())
|
2015-08-11 07:41:11 +00:00
|
|
|
if err := waitRun(contID); err != nil {
|
2017-01-16 15:39:12 +00:00
|
|
|
c.Fatalf("%v: %q", contID, err)
|
2015-08-11 07:41:11 +00:00
|
|
|
}
|
2017-01-16 15:39:12 +00:00
|
|
|
return readContainerFile(c, contID, filename)
|
2014-10-24 18:12:54 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func readContainerFile(c *testing.T, containerID, filename string) []byte {
|
2019-09-05 16:12:33 +00:00
|
|
|
c.Helper()
|
2015-07-22 12:59:24 +00:00
|
|
|
f, err := os.Open(containerStorageFile(containerID, filename))
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2014-10-24 18:12:54 +00:00
|
|
|
defer f.Close()
|
|
|
|
|
2021-08-24 10:10:50 +00:00
|
|
|
content, err := io.ReadAll(f)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2017-01-16 15:39:12 +00:00
|
|
|
return content
|
2014-10-24 18:12:54 +00:00
|
|
|
}
|
2015-01-13 18:46:32 +00:00
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func readContainerFileWithExec(c *testing.T, containerID, filename string) []byte {
|
2019-09-05 16:12:33 +00:00
|
|
|
c.Helper()
|
2017-01-16 15:39:12 +00:00
|
|
|
result := icmd.RunCommand(dockerBinary, "exec", containerID, "cat", filename)
|
|
|
|
result.Assert(c, icmd.Success)
|
|
|
|
return []byte(result.Combined())
|
2015-02-16 19:32:25 +00:00
|
|
|
}
|
|
|
|
|
2015-03-10 21:19:32 +00:00
|
|
|
// daemonTime provides the current time on the daemon host
|
2019-09-09 21:05:55 +00:00
|
|
|
func daemonTime(c *testing.T) time.Time {
|
2019-09-05 16:12:33 +00:00
|
|
|
c.Helper()
|
2017-08-25 22:48:36 +00:00
|
|
|
if testEnv.IsLocalDaemon() {
|
2015-03-10 21:19:32 +00:00
|
|
|
return time.Now()
|
|
|
|
}
|
2023-04-03 11:00:29 +00:00
|
|
|
apiClient, err := client.NewClientWithOpts(client.FromEnv)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2023-04-03 11:00:29 +00:00
|
|
|
defer apiClient.Close()
|
2015-03-10 21:19:32 +00:00
|
|
|
|
2023-04-03 11:00:29 +00:00
|
|
|
info, err := apiClient.Info(context.Background())
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-03-10 21:19:32 +00:00
|
|
|
|
|
|
|
dt, err := time.Parse(time.RFC3339Nano, info.SystemTime)
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, err == nil, "invalid time format in GET /info response")
|
2015-03-10 21:19:32 +00:00
|
|
|
return dt
|
|
|
|
}
|
|
|
|
|
2016-07-03 17:58:11 +00:00
|
|
|
// daemonUnixTime returns the current time on the daemon host with nanoseconds precision.
|
2016-04-11 18:52:34 +00:00
|
|
|
// It return the time formatted how the client sends timestamps to the server.
|
2019-09-09 21:05:55 +00:00
|
|
|
func daemonUnixTime(c *testing.T) string {
|
2019-09-05 16:12:33 +00:00
|
|
|
c.Helper()
|
2016-04-11 18:52:34 +00:00
|
|
|
return parseEventTime(daemonTime(c))
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseEventTime(t time.Time) string {
|
|
|
|
return fmt.Sprintf("%d.%09d", t.Unix(), int64(t.Nanosecond()))
|
|
|
|
}
|
|
|
|
|
2015-03-07 09:03:44 +00:00
|
|
|
// appendBaseEnv appends the minimum set of environment variables to exec the
|
|
|
|
// docker cli binary for testing with correct configuration to the given env
|
|
|
|
// list.
|
2016-02-24 22:59:11 +00:00
|
|
|
func appendBaseEnv(isTLS bool, env ...string) []string {
|
2015-03-07 09:03:44 +00: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 22:42:51 +00:00
|
|
|
|
|
|
|
// testing help text requires the $PATH to dockerd is set
|
|
|
|
"PATH",
|
2015-03-07 09:03:44 +00:00
|
|
|
}
|
2016-02-24 22:59:11 +00:00
|
|
|
if isTLS {
|
|
|
|
preserveList = append(preserveList, "DOCKER_TLS_VERIFY", "DOCKER_CERT_PATH")
|
|
|
|
}
|
2015-03-07 09:03:44 +00:00
|
|
|
|
|
|
|
for _, key := range preserveList {
|
|
|
|
if val := os.Getenv(key); val != "" {
|
|
|
|
env = append(env, fmt.Sprintf("%s=%s", key, val))
|
|
|
|
}
|
2015-02-12 19:20:48 +00:00
|
|
|
}
|
|
|
|
return env
|
|
|
|
}
|
2015-07-24 18:23:20 +00:00
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func createTmpFile(c *testing.T, content string) string {
|
2019-09-05 16:12:33 +00:00
|
|
|
c.Helper()
|
2021-08-24 10:10:50 +00:00
|
|
|
f, err := os.CreateTemp("", "testfile")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-07-24 18:23:20 +00:00
|
|
|
|
|
|
|
filename := f.Name()
|
|
|
|
|
2021-08-24 10:10:50 +00:00
|
|
|
err = os.WriteFile(filename, []byte(content), 0644)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-07-24 18:23:20 +00:00
|
|
|
|
|
|
|
return filename
|
|
|
|
}
|
2015-08-12 02:27:33 +00:00
|
|
|
|
2015-09-09 13:36:44 +00:00
|
|
|
// waitRun will wait for the specified container to be running, maximum 5 seconds.
|
2017-04-11 19:18:30 +00:00
|
|
|
// Deprecated: use cli.WaitFor
|
2015-09-09 13:36:44 +00:00
|
|
|
func waitRun(contID string) error {
|
2022-06-03 09:13:55 +00:00
|
|
|
return daemon.WaitInspectWithArgs(dockerBinary, contID, "{{.State.Running}}", "true", 5*time.Second)
|
2015-09-01 21:37:04 +00:00
|
|
|
}
|
|
|
|
|
2015-09-09 13:36:44 +00:00
|
|
|
// waitInspect will wait for the specified container to have the specified string
|
|
|
|
// in the inspect output. It will wait until the specified timeout (in seconds)
|
|
|
|
// is reached.
|
2017-04-11 19:18:30 +00:00
|
|
|
// Deprecated: use cli.WaitFor
|
2015-09-01 21:37:04 +00:00
|
|
|
func waitInspect(name, expr, expected string, timeout time.Duration) error {
|
2022-06-03 09:13:55 +00:00
|
|
|
return daemon.WaitInspectWithArgs(dockerBinary, name, expr, expected, timeout)
|
2015-09-09 13:36:44 +00:00
|
|
|
}
|
2015-10-30 18:57:15 +00:00
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func getInspectBody(c *testing.T, version, id string) []byte {
|
2019-09-05 16:12:33 +00:00
|
|
|
c.Helper()
|
2023-04-03 11:00:29 +00:00
|
|
|
apiClient, err := client.NewClientWithOpts(client.FromEnv, client.WithVersion(version))
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2023-04-03 11:00:29 +00:00
|
|
|
defer apiClient.Close()
|
|
|
|
_, body, err := apiClient.ContainerInspectWithRaw(context.Background(), id, false)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-10-30 18:57:15 +00:00
|
|
|
return body
|
|
|
|
}
|
2016-01-27 04:16:36 +00:00
|
|
|
|
|
|
|
// Run a long running idle task in a background container using the
|
|
|
|
// system-specific default image and command.
|
2019-09-09 21:05:55 +00:00
|
|
|
func runSleepingContainer(c *testing.T, extraArgs ...string) string {
|
2019-09-05 16:12:33 +00:00
|
|
|
c.Helper()
|
2019-07-08 16:42:08 +00:00
|
|
|
return runSleepingContainerInImage(c, "busybox", extraArgs...)
|
2016-01-27 04:16:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run a long running idle task in a background container using the specified
|
|
|
|
// image and the system-specific command.
|
2019-09-09 21:05:55 +00:00
|
|
|
func runSleepingContainerInImage(c *testing.T, image string, extraArgs ...string) string {
|
2019-09-05 16:12:33 +00:00
|
|
|
c.Helper()
|
2016-01-27 04:16:36 +00:00
|
|
|
args := []string{"run", "-d"}
|
|
|
|
args = append(args, extraArgs...)
|
|
|
|
args = append(args, image)
|
2016-08-17 22:46:28 +00:00
|
|
|
args = append(args, sleepCommandForDaemonPlatform()...)
|
2017-07-09 13:34:14 +00:00
|
|
|
return strings.TrimSpace(cli.DockerCmd(c, args...).Combined())
|
2016-01-27 04:16:36 +00:00
|
|
|
}
|
2016-02-04 21:06:12 +00:00
|
|
|
|
|
|
|
// minimalBaseImage returns the name of the minimal base image for the current
|
|
|
|
// daemon platform.
|
|
|
|
func minimalBaseImage() string {
|
2018-01-15 14:31:02 +00:00
|
|
|
return testEnv.PlatformDefaults.BaseImage
|
2016-02-04 21:06:12 +00:00
|
|
|
}
|
2016-03-14 22:52:50 +00:00
|
|
|
|
|
|
|
func getGoroutineNumber() (int, error) {
|
2023-04-03 11:00:29 +00:00
|
|
|
apiClient, err := client.NewClientWithOpts(client.FromEnv)
|
2016-03-14 22:52:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2023-04-03 11:00:29 +00:00
|
|
|
defer apiClient.Close()
|
2017-05-24 03:56:26 +00:00
|
|
|
|
2023-04-03 11:00:29 +00:00
|
|
|
info, err := apiClient.Info(context.Background())
|
2017-05-24 03:56:26 +00:00
|
|
|
if err != nil {
|
2016-03-14 22:52:50 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
2017-05-24 03:56:26 +00:00
|
|
|
return info.NGoroutines, nil
|
2016-03-14 22:52:50 +00: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 11:56:04 +00:00
|
|
|
|
|
|
|
// getErrorMessage returns the error message from an error API response
|
2019-09-09 21:05:55 +00:00
|
|
|
func getErrorMessage(c *testing.T, body []byte) string {
|
2019-09-05 16:12:33 +00:00
|
|
|
c.Helper()
|
2016-05-21 11:56:04 +00:00
|
|
|
var resp types.ErrorResponse
|
2019-09-09 21:05:57 +00:00
|
|
|
assert.Assert(c, json.Unmarshal(body, &resp) == nil)
|
2016-05-21 11:56:04 +00:00
|
|
|
return strings.TrimSpace(resp.Message)
|
|
|
|
}
|
2016-06-14 02:54:20 +00:00
|
|
|
|
2019-09-09 21:08:22 +00:00
|
|
|
type checkF func(*testing.T) (interface{}, string)
|
2016-06-14 02:54:20 +00:00
|
|
|
type reducer func(...interface{}) interface{}
|
|
|
|
|
2019-08-26 16:04:47 +00:00
|
|
|
func pollCheck(t *testing.T, f checkF, compare func(x interface{}) assert.BoolOrComparison) poll.Check {
|
2019-08-09 10:41:13 +00:00
|
|
|
return func(poll.LogT) poll.Result {
|
2019-09-05 16:12:33 +00:00
|
|
|
t.Helper()
|
2019-08-26 16:04:47 +00:00
|
|
|
v, comment := f(t)
|
2019-09-09 20:49:16 +00: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 10:41:13 +00:00
|
|
|
}
|
|
|
|
return poll.Continue(comment)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-26 16:04:47 +00:00
|
|
|
func reducedCheck(r reducer, funcs ...checkF) checkF {
|
2019-09-09 21:08:22 +00:00
|
|
|
return func(c *testing.T) (interface{}, string) {
|
2019-09-05 16:12:33 +00:00
|
|
|
c.Helper()
|
2016-06-14 02:54:20 +00:00
|
|
|
var values []interface{}
|
|
|
|
var comments []string
|
|
|
|
for _, f := range funcs {
|
2019-08-26 16:04:47 +00:00
|
|
|
v, comment := f(c)
|
2016-06-14 02:54:20 +00:00
|
|
|
values = append(values, v)
|
2019-08-26 14:45:33 +00:00
|
|
|
if len(comment) > 0 {
|
|
|
|
comments = append(comments, comment)
|
2016-06-14 02:54:20 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-09 21:08:22 +00:00
|
|
|
return r(values...), fmt.Sprintf("%v", strings.Join(comments, ", "))
|
2016-06-14 02:54:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func sumAsIntegers(vals ...interface{}) interface{} {
|
|
|
|
var s int
|
|
|
|
for _, v := range vals {
|
|
|
|
s += v.(int)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|