2014-02-25 11:17:48 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-12-01 12:16:49 -05:00
|
|
|
"bytes"
|
|
|
|
"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"
|
2014-07-23 14:12:42 -04:00
|
|
|
"net"
|
2014-05-31 08:42:49 -04:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2015-01-14 19:08:28 -05:00
|
|
|
"net/url"
|
2014-05-31 08:42:49 -04:00
|
|
|
"os"
|
2014-02-25 11:17:48 -05:00
|
|
|
"os/exec"
|
2014-05-31 08:42:49 -04:00
|
|
|
"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"
|
2016-10-14 16:20:13 -04:00
|
|
|
volumetypes "github.com/docker/docker/api/types/volume"
|
2016-12-30 12:23:00 -05:00
|
|
|
"github.com/docker/docker/integration-cli/checker"
|
2016-12-09 04:17:53 -05:00
|
|
|
"github.com/docker/docker/integration-cli/daemon"
|
2016-12-30 13:10:04 -05:00
|
|
|
"github.com/docker/docker/integration-cli/registry"
|
2016-12-30 04:49:36 -05:00
|
|
|
"github.com/docker/docker/integration-cli/request"
|
2015-04-13 10:17:14 -04:00
|
|
|
"github.com/docker/docker/opts"
|
2015-03-24 07:25:26 -04:00
|
|
|
"github.com/docker/docker/pkg/stringutils"
|
2016-12-30 12:23:00 -05:00
|
|
|
icmd "github.com/docker/docker/pkg/testutil/cmd"
|
2015-04-18 12:46:47 -04:00
|
|
|
"github.com/go-check/check"
|
2014-02-25 11:17:48 -05:00
|
|
|
)
|
|
|
|
|
2015-01-14 19:08:28 -05:00
|
|
|
func daemonHost() string {
|
2015-07-22 08:59:24 -04:00
|
|
|
daemonURLStr := "unix://" + opts.DefaultUnixSocket
|
2015-02-14 05:27:31 -05:00
|
|
|
if daemonHostVar := os.Getenv("DOCKER_HOST"); daemonHostVar != "" {
|
2015-07-22 08:59:24 -04:00
|
|
|
daemonURLStr = daemonHostVar
|
2015-01-14 19:08:28 -05:00
|
|
|
}
|
2015-07-22 08:59:24 -04:00
|
|
|
return daemonURLStr
|
2015-01-14 19:08:28 -05:00
|
|
|
}
|
|
|
|
|
2017-01-02 13:21:52 -05:00
|
|
|
// FIXME(vdemeester) move this away are remove ignoreNoSuchContainer bool
|
|
|
|
func deleteContainer(ignoreNoSuchContainer bool, container ...string) error {
|
2016-08-16 17:51:38 -04:00
|
|
|
result := icmd.RunCommand(dockerBinary, append([]string{"rm", "-fv"}, container...)...)
|
2017-01-02 13:21:52 -05:00
|
|
|
if ignoreNoSuchContainer && result.Error != nil {
|
|
|
|
// If the error is "No such container: ..." this means the container doesn't exists anymore,
|
|
|
|
// we can safely ignore that one.
|
2017-01-03 08:45:39 -05:00
|
|
|
if strings.Contains(result.Stderr(), "No such container") {
|
2017-01-02 13:21:52 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2016-08-16 17:51:38 -04:00
|
|
|
return result.Compare(icmd.Success)
|
2014-02-25 11:17:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-12-08 04:56:29 -05:00
|
|
|
func deleteAllContainers(c *check.C) {
|
2014-02-25 11:17:48 -05:00
|
|
|
containers, err := getAllContainers()
|
2016-12-08 04:56:29 -05:00
|
|
|
c.Assert(err, checker.IsNil, check.Commentf("containers: %v", containers))
|
2014-02-25 11:17:48 -05:00
|
|
|
|
2016-12-08 04:56:29 -05:00
|
|
|
if containers != "" {
|
2017-01-02 13:21:52 -05:00
|
|
|
err = deleteContainer(true, strings.Split(strings.TrimSpace(containers), "\n")...)
|
2016-12-08 04:56:29 -05:00
|
|
|
c.Assert(err, checker.IsNil)
|
2014-02-25 11:17:48 -05:00
|
|
|
}
|
2015-10-13 17:18:05 -04:00
|
|
|
}
|
|
|
|
|
2016-12-08 04:56:29 -05:00
|
|
|
func deleteAllNetworks(c *check.C) {
|
2015-10-13 17:18:05 -04:00
|
|
|
networks, err := getAllNetworks()
|
2016-12-08 04:56:29 -05:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
var errs []string
|
2015-10-13 17:18:05 -04:00
|
|
|
for _, n := range networks {
|
2015-10-30 11:05:33 -04:00
|
|
|
if n.Name == "bridge" || n.Name == "none" || n.Name == "host" {
|
|
|
|
continue
|
|
|
|
}
|
2017-01-13 11:23:28 -05:00
|
|
|
if testEnv.DaemonPlatform() == "windows" && strings.ToLower(n.Name) == "nat" {
|
2016-04-05 22:29:43 -04:00
|
|
|
// nat is a pre-defined network on Windows and cannot be removed
|
|
|
|
continue
|
|
|
|
}
|
2016-12-30 04:49:36 -05:00
|
|
|
status, b, err := request.SockRequest("DELETE", "/networks/"+n.Name, nil, daemonHost())
|
2015-10-30 11:05:33 -04:00
|
|
|
if err != nil {
|
2016-12-08 04:56:29 -05:00
|
|
|
errs = append(errs, err.Error())
|
2015-10-30 11:05:33 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if status != http.StatusNoContent {
|
2016-12-08 04:56:29 -05:00
|
|
|
errs = append(errs, fmt.Sprintf("error deleting network %s: %s", n.Name, string(b)))
|
2015-10-13 17:18:05 -04:00
|
|
|
}
|
|
|
|
}
|
2016-12-08 04:56:29 -05:00
|
|
|
c.Assert(errs, checker.HasLen, 0, check.Commentf(strings.Join(errs, "\n")))
|
2015-10-13 17:18:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func getAllNetworks() ([]types.NetworkResource, error) {
|
|
|
|
var networks []types.NetworkResource
|
2016-12-30 04:49:36 -05:00
|
|
|
_, b, err := request.SockRequest("GET", "/networks", nil, daemonHost())
|
2015-10-13 17:18:05 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(b, &networks); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return networks, nil
|
2014-02-25 11:17:48 -05:00
|
|
|
}
|
|
|
|
|
2016-12-08 04:56:29 -05:00
|
|
|
func deleteAllPlugins(c *check.C) {
|
2016-11-01 14:17:33 -04:00
|
|
|
plugins, err := getAllPlugins()
|
2016-12-08 04:56:29 -05:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
var errs []string
|
2016-11-01 14:17:33 -04:00
|
|
|
for _, p := range plugins {
|
2016-12-08 04:56:29 -05:00
|
|
|
pluginName := p.Name
|
2016-12-30 04:49:36 -05:00
|
|
|
status, b, err := request.SockRequest("DELETE", "/plugins/"+pluginName+"?force=1", nil, daemonHost())
|
2016-11-01 14:17:33 -04:00
|
|
|
if err != nil {
|
2016-12-08 04:56:29 -05:00
|
|
|
errs = append(errs, err.Error())
|
2016-11-01 14:17:33 -04:00
|
|
|
continue
|
|
|
|
}
|
2016-12-08 04:56:29 -05:00
|
|
|
if status != http.StatusOK {
|
|
|
|
errs = append(errs, fmt.Sprintf("error deleting plugin %s: %s", p.Name, string(b)))
|
2016-11-01 14:17:33 -04:00
|
|
|
}
|
|
|
|
}
|
2016-12-08 04:56:29 -05:00
|
|
|
c.Assert(errs, checker.HasLen, 0, check.Commentf(strings.Join(errs, "\n")))
|
2016-11-01 14:17:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func getAllPlugins() (types.PluginsListResponse, error) {
|
|
|
|
var plugins types.PluginsListResponse
|
2016-12-30 04:49:36 -05:00
|
|
|
_, b, err := request.SockRequest("GET", "/plugins", nil, daemonHost())
|
2016-11-01 14:17:33 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(b, &plugins); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return plugins, nil
|
|
|
|
}
|
|
|
|
|
2016-12-08 04:56:29 -05:00
|
|
|
func deleteAllVolumes(c *check.C) {
|
2015-06-12 09:25:32 -04:00
|
|
|
volumes, err := getAllVolumes()
|
2016-12-08 04:56:29 -05:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
var errs []string
|
2015-06-12 09:25:32 -04:00
|
|
|
for _, v := range volumes {
|
2016-12-30 04:49:36 -05:00
|
|
|
status, b, err := request.SockRequest("DELETE", "/volumes/"+v.Name, nil, daemonHost())
|
2015-06-12 09:25:32 -04:00
|
|
|
if err != nil {
|
2016-12-08 04:56:29 -05:00
|
|
|
errs = append(errs, err.Error())
|
2015-06-12 09:25:32 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if status != http.StatusNoContent {
|
2016-12-08 04:56:29 -05:00
|
|
|
errs = append(errs, fmt.Sprintf("error deleting volume %s: %s", v.Name, string(b)))
|
2015-06-12 09:25:32 -04:00
|
|
|
}
|
|
|
|
}
|
2016-12-08 04:56:29 -05:00
|
|
|
c.Assert(errs, checker.HasLen, 0, check.Commentf(strings.Join(errs, "\n")))
|
2015-06-12 09:25:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func getAllVolumes() ([]*types.Volume, error) {
|
2016-10-04 11:40:17 -04:00
|
|
|
var volumes volumetypes.VolumesListOKBody
|
2016-12-30 04:49:36 -05:00
|
|
|
_, b, err := request.SockRequest("GET", "/volumes", nil, daemonHost())
|
2015-06-12 09:25:32 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(b, &volumes); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return volumes.Volumes, nil
|
|
|
|
}
|
|
|
|
|
2016-12-08 04:56:29 -05:00
|
|
|
func deleteAllImages(c *check.C) {
|
2016-12-08 10:28:06 -05:00
|
|
|
cmd := exec.Command(dockerBinary, "images", "--digests")
|
2016-02-24 17:59:11 -05:00
|
|
|
cmd.Env = appendBaseEnv(true)
|
|
|
|
out, err := cmd.CombinedOutput()
|
2016-12-08 04:56:29 -05:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-04-23 17:39:31 -04:00
|
|
|
lines := strings.Split(string(out), "\n")[1:]
|
2016-12-08 10:28:06 -05:00
|
|
|
imgMap := map[string]struct{}{}
|
2015-04-23 17:39:31 -04:00
|
|
|
for _, l := range lines {
|
|
|
|
if l == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fields := strings.Fields(l)
|
|
|
|
imgTag := fields[0] + ":" + fields[1]
|
|
|
|
if _, ok := protectedImages[imgTag]; !ok {
|
2016-12-08 04:56:29 -05:00
|
|
|
if fields[0] == "<none>" || fields[1] == "<none>" {
|
2016-12-08 10:28:06 -05:00
|
|
|
if fields[2] != "<none>" {
|
|
|
|
imgMap[fields[0]+"@"+fields[2]] = struct{}{}
|
|
|
|
} else {
|
|
|
|
imgMap[fields[3]] = struct{}{}
|
|
|
|
}
|
|
|
|
// continue
|
|
|
|
} else {
|
|
|
|
imgMap[imgTag] = struct{}{}
|
2015-04-23 17:39:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-08 10:28:06 -05:00
|
|
|
if len(imgMap) != 0 {
|
|
|
|
imgs := make([]string, 0, len(imgMap))
|
|
|
|
for k := range imgMap {
|
|
|
|
imgs = append(imgs, k)
|
|
|
|
}
|
2016-12-08 04:56:29 -05:00
|
|
|
dockerCmd(c, append([]string{"rmi", "-f"}, imgs...)...)
|
2015-04-23 17:39:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-20 16:26:55 -05:00
|
|
|
func getPausedContainers() ([]string, error) {
|
2014-11-27 11:08:39 -05:00
|
|
|
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)
|
|
|
|
}
|
2016-12-20 16:26:55 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-01-30 19:23:47 -05:00
|
|
|
}
|
2016-12-20 16:26:55 -05:00
|
|
|
|
|
|
|
return strings.Fields(out), nil
|
2015-01-30 19:23:47 -05:00
|
|
|
}
|
|
|
|
|
2016-12-08 04:56:29 -05:00
|
|
|
func unpauseContainer(c *check.C, container string) {
|
|
|
|
dockerCmd(c, "unpause", container)
|
2014-11-27 11:08:39 -05:00
|
|
|
}
|
|
|
|
|
2016-12-08 04:56:29 -05:00
|
|
|
func unpauseAllContainers(c *check.C) {
|
2014-11-27 11:08:39 -05:00
|
|
|
containers, err := getPausedContainers()
|
2016-12-08 04:56:29 -05:00
|
|
|
c.Assert(err, checker.IsNil, check.Commentf("containers: %v", containers))
|
2016-12-20 16:26:55 -05:00
|
|
|
for _, value := range containers {
|
2016-12-08 04:56:29 -05:00
|
|
|
unpauseContainer(c, value)
|
2014-11-27 11:08:39 -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
|
|
|
|
2015-07-27 14:13:25 -04:00
|
|
|
func dockerCmdWithError(args ...string) (string, int, error) {
|
2016-02-24 13:17:25 -05:00
|
|
|
if err := validateArgs(args...); err != nil {
|
|
|
|
return "", 0, err
|
|
|
|
}
|
2016-08-04 12:57:34 -04:00
|
|
|
result := icmd.RunCommand(dockerBinary, args...)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-07-17 22:48:28 -04:00
|
|
|
func dockerCmdWithStdoutStderr(c *check.C, args ...string) (string, string, int) {
|
2016-02-24 13:17:25 -05:00
|
|
|
if err := validateArgs(args...); err != nil {
|
|
|
|
c.Fatalf(err.Error())
|
|
|
|
}
|
2016-08-04 12:57:34 -04:00
|
|
|
|
|
|
|
result := icmd.RunCommand(dockerBinary, args...)
|
2016-12-08 04:56:29 -05:00
|
|
|
c.Assert(result, icmd.Matches, icmd.Success)
|
2016-08-04 12:57:34 -04:00
|
|
|
return result.Stdout(), result.Stderr(), result.ExitCode
|
2015-07-17 22:48:28 -04:00
|
|
|
}
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func dockerCmd(c *check.C, args ...string) (string, int) {
|
2016-02-24 13:17:25 -05:00
|
|
|
if err := validateArgs(args...); err != nil {
|
|
|
|
c.Fatalf(err.Error())
|
|
|
|
}
|
2016-08-04 12:57:34 -04:00
|
|
|
result := icmd.RunCommand(dockerBinary, args...)
|
2016-08-16 17:51:38 -04:00
|
|
|
c.Assert(result, icmd.Matches, icmd.Success)
|
2016-08-04 12:57:34 -04:00
|
|
|
return result.Combined(), result.ExitCode
|
|
|
|
}
|
|
|
|
|
|
|
|
func dockerCmdWithResult(args ...string) *icmd.Result {
|
|
|
|
return icmd.RunCommand(dockerBinary, args...)
|
|
|
|
}
|
|
|
|
|
2016-08-16 17:51:38 -04:00
|
|
|
func binaryWithArgs(args ...string) []string {
|
2016-08-04 12:57:34 -04:00
|
|
|
return append([]string{dockerBinary}, args...)
|
2014-04-03 20:57:41 -04:00
|
|
|
}
|
2014-05-10 13:27:24 -04:00
|
|
|
|
2015-03-12 09:21:58 -04:00
|
|
|
// execute a docker command with a timeout
|
2016-08-04 12:57:34 -04:00
|
|
|
func dockerCmdWithTimeout(timeout time.Duration, args ...string) *icmd.Result {
|
2016-02-24 13:17:25 -05:00
|
|
|
if err := validateArgs(args...); err != nil {
|
2016-08-04 12:57:34 -04:00
|
|
|
return &icmd.Result{Error: err}
|
2016-02-24 13:17:25 -05:00
|
|
|
}
|
2016-08-16 17:51:38 -04:00
|
|
|
return icmd.RunCmd(icmd.Cmd{Command: binaryWithArgs(args...), Timeout: timeout})
|
2014-08-13 12:02:04 -04:00
|
|
|
}
|
|
|
|
|
2014-07-22 18:54:56 -04:00
|
|
|
// execute a docker command in a directory
|
2015-04-18 12:46:47 -04:00
|
|
|
func dockerCmdInDir(c *check.C, path string, args ...string) (string, int, error) {
|
2016-02-24 13:17:25 -05:00
|
|
|
if err := validateArgs(args...); err != nil {
|
|
|
|
c.Fatalf(err.Error())
|
|
|
|
}
|
2016-08-16 17:51:38 -04:00
|
|
|
result := icmd.RunCmd(icmd.Cmd{Command: binaryWithArgs(args...), Dir: path})
|
2016-08-04 12:57:34 -04:00
|
|
|
return result.Combined(), result.ExitCode, result.Error
|
2014-08-13 12:02:04 -04:00
|
|
|
}
|
|
|
|
|
2016-02-24 13:17:25 -05:00
|
|
|
// 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 {
|
2017-01-13 11:23:28 -05:00
|
|
|
if testEnv.DaemonPlatform() != "windows" {
|
2016-02-24 13:17:25 -05:00
|
|
|
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") {
|
2016-08-31 23:22:35 -04:00
|
|
|
return errors.New("cannot use 'busybox top' in tests on Windows. Use runSleepingContainer()")
|
2016-02-24 13:17:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
func getContainerCount() (int, error) {
|
|
|
|
const containers = "Containers:"
|
|
|
|
|
|
|
|
cmd := exec.Command(dockerBinary, "info")
|
|
|
|
out, _, err := runCommandWithOutput(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
lines := strings.Split(out, "\n")
|
|
|
|
for _, line := range lines {
|
|
|
|
if strings.Contains(line, containers) {
|
2015-04-06 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)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return containerCount, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0, fmt.Errorf("couldn't find the Container count in the output")
|
|
|
|
}
|
2014-05-31 08:42:49 -04:00
|
|
|
|
2015-07-22 08:59:24 -04:00
|
|
|
// FakeContext creates directories that can be used as a build context
|
2014-05-31 08:42:49 -04:00
|
|
|
type FakeContext struct {
|
|
|
|
Dir string
|
|
|
|
}
|
|
|
|
|
2015-07-22 08:59:24 -04:00
|
|
|
// Add a file at a path, creating directories where necessary
|
2014-05-31 08:42:49 -04:00
|
|
|
func (f *FakeContext) Add(file, content string) error {
|
2015-04-04 10:54:43 -04:00
|
|
|
return f.addFile(file, []byte(content))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FakeContext) addFile(file string, content []byte) error {
|
2016-10-26 19:26:31 -04:00
|
|
|
fp := filepath.Join(f.Dir, filepath.FromSlash(file))
|
|
|
|
dirpath := filepath.Dir(fp)
|
2014-05-31 08:42:49 -04:00
|
|
|
if dirpath != "." {
|
|
|
|
if err := os.MkdirAll(dirpath, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-10-26 19:26:31 -04:00
|
|
|
return ioutil.WriteFile(fp, content, 0644)
|
2015-04-04 10:54:43 -04:00
|
|
|
|
2014-05-31 08:42:49 -04:00
|
|
|
}
|
|
|
|
|
2015-07-22 08:59:24 -04:00
|
|
|
// Delete a file at a path
|
2014-05-31 08:42:49 -04:00
|
|
|
func (f *FakeContext) Delete(file string) error {
|
2016-10-26 19:26:31 -04:00
|
|
|
fp := filepath.Join(f.Dir, filepath.FromSlash(file))
|
|
|
|
return os.RemoveAll(fp)
|
2014-05-31 08:42:49 -04:00
|
|
|
}
|
|
|
|
|
2015-07-22 08:59:24 -04:00
|
|
|
// Close deletes the context
|
2014-05-31 08:42:49 -04:00
|
|
|
func (f *FakeContext) Close() error {
|
|
|
|
return os.RemoveAll(f.Dir)
|
|
|
|
}
|
|
|
|
|
2017-01-16 05:30:14 -05:00
|
|
|
func fakeContextFromNewTempDir(c *check.C) *FakeContext {
|
2014-05-31 08:42:49 -04:00
|
|
|
tmp, err := ioutil.TempDir("", "fake-context")
|
2017-01-16 05:30:14 -05:00
|
|
|
c.Assert(err, checker.IsNil)
|
2014-09-24 08:42:17 -04:00
|
|
|
if err := os.Chmod(tmp, 0755); err != nil {
|
2017-01-16 05:30:14 -05:00
|
|
|
c.Fatal(err)
|
2014-09-24 08:42:17 -04:00
|
|
|
}
|
2017-01-16 05:30:14 -05:00
|
|
|
return fakeContextFromDir(tmp)
|
2015-04-04 10:54:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func fakeContextFromDir(dir string) *FakeContext {
|
|
|
|
return &FakeContext{dir}
|
|
|
|
}
|
2015-03-09 23:53:28 -04:00
|
|
|
|
2017-01-16 05:30:14 -05:00
|
|
|
func fakeContextWithFiles(c *check.C, files map[string]string) *FakeContext {
|
|
|
|
ctx := fakeContextFromNewTempDir(c)
|
2014-05-31 08:42:49 -04:00
|
|
|
for file, content := range files {
|
|
|
|
if err := ctx.Add(file, content); err != nil {
|
|
|
|
ctx.Close()
|
2017-01-16 05:30:14 -05:00
|
|
|
c.Fatal(err)
|
2014-05-31 08:42:49 -04:00
|
|
|
}
|
|
|
|
}
|
2017-01-16 05:30:14 -05:00
|
|
|
return ctx
|
2015-03-09 23:53:28 -04:00
|
|
|
}
|
|
|
|
|
2017-01-16 05:30:14 -05:00
|
|
|
func fakeContextAddDockerfile(c *check.C, ctx *FakeContext, dockerfile string) {
|
2014-05-31 08:42:49 -04:00
|
|
|
if err := ctx.Add("Dockerfile", dockerfile); err != nil {
|
|
|
|
ctx.Close()
|
2017-01-16 05:30:14 -05:00
|
|
|
c.Fatal(err)
|
2015-03-09 23:53:28 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-16 05:30:14 -05:00
|
|
|
func fakeContext(c *check.C, dockerfile string, files map[string]string) *FakeContext {
|
|
|
|
ctx := fakeContextWithFiles(c, files)
|
|
|
|
fakeContextAddDockerfile(c, ctx, dockerfile)
|
|
|
|
return ctx
|
2014-05-31 08:42:49 -04:00
|
|
|
}
|
|
|
|
|
2015-02-19 05:01:27 -05:00
|
|
|
// FakeStorage is a static file server. It might be running locally or remotely
|
|
|
|
// on test host.
|
|
|
|
type FakeStorage interface {
|
|
|
|
Close() error
|
|
|
|
URL() string
|
|
|
|
CtxDir() string
|
|
|
|
}
|
|
|
|
|
2017-01-16 05:30:14 -05:00
|
|
|
func fakeBinaryStorage(c *check.C, archives map[string]*bytes.Buffer) FakeStorage {
|
|
|
|
ctx := fakeContextFromNewTempDir(c)
|
2015-04-04 10:54:43 -04:00
|
|
|
for name, content := range archives {
|
|
|
|
if err := ctx.addFile(name, content.Bytes()); err != nil {
|
2017-01-16 05:30:14 -05:00
|
|
|
c.Fatal(err)
|
2015-04-04 10:54:43 -04:00
|
|
|
}
|
|
|
|
}
|
2017-01-16 05:30:14 -05:00
|
|
|
return fakeStorageWithContext(c, ctx)
|
2015-04-04 10:54:43 -04:00
|
|
|
}
|
|
|
|
|
2015-02-19 05:01:27 -05:00
|
|
|
// fakeStorage returns either a local or remote (at daemon machine) file server
|
2017-01-16 05:30:14 -05:00
|
|
|
func fakeStorage(c *check.C, files map[string]string) FakeStorage {
|
|
|
|
ctx := fakeContextWithFiles(c, files)
|
|
|
|
return fakeStorageWithContext(c, ctx)
|
2015-03-09 23:53:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// fakeStorageWithContext returns either a local or remote (at daemon machine) file server
|
2017-01-16 05:30:14 -05:00
|
|
|
func fakeStorageWithContext(c *check.C, ctx *FakeContext) FakeStorage {
|
2017-01-13 11:23:28 -05:00
|
|
|
if testEnv.LocalDaemon() {
|
2017-01-16 05:30:14 -05:00
|
|
|
return newLocalFakeStorage(c, ctx)
|
2015-02-19 05:01:27 -05:00
|
|
|
}
|
2017-01-16 05:30:14 -05:00
|
|
|
return newRemoteFileServer(c, ctx)
|
2015-02-19 05:01:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// localFileStorage is a file storage on the running machine
|
|
|
|
type localFileStorage struct {
|
2014-05-31 08:42:49 -04:00
|
|
|
*FakeContext
|
|
|
|
*httptest.Server
|
|
|
|
}
|
|
|
|
|
2015-02-19 05:01:27 -05:00
|
|
|
func (s *localFileStorage) URL() string {
|
|
|
|
return s.Server.URL
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *localFileStorage) CtxDir() string {
|
|
|
|
return s.FakeContext.Dir
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *localFileStorage) Close() error {
|
|
|
|
defer s.Server.Close()
|
|
|
|
return s.FakeContext.Close()
|
2014-05-31 08:42:49 -04:00
|
|
|
}
|
|
|
|
|
2017-01-16 05:30:14 -05:00
|
|
|
func newLocalFakeStorage(c *check.C, ctx *FakeContext) *localFileStorage {
|
2014-05-31 08:42:49 -04:00
|
|
|
handler := http.FileServer(http.Dir(ctx.Dir))
|
|
|
|
server := httptest.NewServer(handler)
|
2015-02-19 05:01:27 -05:00
|
|
|
return &localFileStorage{
|
2014-05-31 08:42:49 -04:00
|
|
|
FakeContext: ctx,
|
|
|
|
Server: server,
|
2017-01-16 05:30:14 -05:00
|
|
|
}
|
2014-05-31 08:42:49 -04:00
|
|
|
}
|
|
|
|
|
2015-02-19 05:01:27 -05:00
|
|
|
// remoteFileServer is a containerized static file server started on the remote
|
|
|
|
// testing machine to be used in URL-accepting docker build functionality.
|
|
|
|
type remoteFileServer struct {
|
|
|
|
host string // hostname/port web server is listening to on docker host e.g. 0.0.0.0:43712
|
|
|
|
container string
|
|
|
|
image string
|
|
|
|
ctx *FakeContext
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *remoteFileServer) URL() string {
|
|
|
|
u := url.URL{
|
|
|
|
Scheme: "http",
|
|
|
|
Host: f.host}
|
|
|
|
return u.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *remoteFileServer) CtxDir() string {
|
|
|
|
return f.ctx.Dir
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *remoteFileServer) Close() error {
|
|
|
|
defer func() {
|
|
|
|
if f.ctx != nil {
|
|
|
|
f.ctx.Close()
|
|
|
|
}
|
|
|
|
if f.image != "" {
|
|
|
|
deleteImages(f.image)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if f.container == "" {
|
|
|
|
return nil
|
2014-05-31 08:42:49 -04:00
|
|
|
}
|
2017-01-02 13:21:52 -05:00
|
|
|
return deleteContainer(false, f.container)
|
2014-05-31 08:42:49 -04:00
|
|
|
}
|
2014-07-08 16:25:22 -04:00
|
|
|
|
2017-01-16 05:30:14 -05:00
|
|
|
func newRemoteFileServer(c *check.C, ctx *FakeContext) *remoteFileServer {
|
2015-02-19 05:01:27 -05:00
|
|
|
var (
|
2015-03-24 07:25:26 -04:00
|
|
|
image = fmt.Sprintf("fileserver-img-%s", strings.ToLower(stringutils.GenerateRandomAlphaOnlyString(10)))
|
|
|
|
container = fmt.Sprintf("fileserver-cnt-%s", strings.ToLower(stringutils.GenerateRandomAlphaOnlyString(10)))
|
2015-02-19 05:01:27 -05:00
|
|
|
)
|
|
|
|
|
2017-01-16 05:30:14 -05:00
|
|
|
c.Assert(ensureHTTPServerImage(), checker.IsNil)
|
2016-08-31 15:31:06 -04:00
|
|
|
|
2015-02-19 05:01:27 -05:00
|
|
|
// Build the image
|
2017-01-16 05:30:14 -05:00
|
|
|
fakeContextAddDockerfile(c, ctx, `FROM httpserver
|
|
|
|
COPY . /static`)
|
|
|
|
buildImageSuccessfully(c, image, withoutCache, withExternalBuildContext(ctx))
|
2015-02-19 05:01:27 -05:00
|
|
|
|
|
|
|
// Start the container
|
2017-01-16 05:30:14 -05:00
|
|
|
dockerCmd(c, "run", "-d", "-P", "--name", container, image)
|
2015-02-19 05:01:27 -05:00
|
|
|
|
|
|
|
// Find out the system assigned port
|
2017-01-16 05:30:14 -05:00
|
|
|
out, _ := dockerCmd(c, "port", container, "80/tcp")
|
2015-07-27 14:08:40 -04:00
|
|
|
fileserverHostPort := strings.Trim(out, "\n")
|
|
|
|
_, port, err := net.SplitHostPort(fileserverHostPort)
|
|
|
|
if err != nil {
|
2017-01-16 05:30:14 -05:00
|
|
|
c.Fatalf("unable to parse file server host:port: %v", err)
|
2015-07-27 14:08:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
dockerHostURL, err := url.Parse(daemonHost())
|
|
|
|
if err != nil {
|
2017-01-16 05:30:14 -05:00
|
|
|
c.Fatalf("unable to parse daemon host URL: %v", err)
|
2015-07-27 14:08:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
host, _, err := net.SplitHostPort(dockerHostURL.Host)
|
|
|
|
if err != nil {
|
2017-01-16 05:30:14 -05:00
|
|
|
c.Fatalf("unable to parse docker daemon host:port: %v", err)
|
2015-07-27 14:08:40 -04:00
|
|
|
}
|
|
|
|
|
2015-02-19 05:01:27 -05:00
|
|
|
return &remoteFileServer{
|
|
|
|
container: container,
|
|
|
|
image: image,
|
2015-07-27 14:08:40 -04:00
|
|
|
host: fmt.Sprintf("%s:%s", host, port),
|
2017-01-16 05:30:14 -05:00
|
|
|
ctx: ctx}
|
2014-07-08 16:25:22 -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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-03-30 17:26:02 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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-01-10 13:16:25 -05:00
|
|
|
func buildImageSuccessfully(c *check.C, name string, cmdOperators ...func(*icmd.Cmd) func()) {
|
2017-01-16 05:30:14 -05:00
|
|
|
buildImage(name, cmdOperators...).Assert(c, icmd.Success)
|
2017-01-10 13:16:25 -05:00
|
|
|
}
|
|
|
|
|
2017-01-16 05:30:14 -05:00
|
|
|
func buildImage(name string, cmdOperators ...func(*icmd.Cmd) func()) *icmd.Result {
|
2017-01-10 13:16:25 -05:00
|
|
|
cmd := icmd.Command(dockerBinary, "build", "-t", name)
|
|
|
|
for _, op := range cmdOperators {
|
|
|
|
deferFn := op(&cmd)
|
|
|
|
if deferFn != nil {
|
|
|
|
defer deferFn()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return icmd.RunCmd(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
func withBuildContextPath(path string) func(*icmd.Cmd) func() {
|
|
|
|
return func(cmd *icmd.Cmd) func() {
|
|
|
|
cmd.Command = append(cmd.Command, path)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-16 05:30:14 -05:00
|
|
|
func withExternalBuildContext(ctx *FakeContext) func(*icmd.Cmd) func() {
|
|
|
|
return func(cmd *icmd.Cmd) func() {
|
|
|
|
cmd.Dir = ctx.Dir
|
|
|
|
cmd.Command = append(cmd.Command, ".")
|
|
|
|
return nil
|
2017-01-10 13:16:25 -05:00
|
|
|
}
|
2017-01-16 05:30:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func withBuildContext(c *check.C, contextOperators ...func(*FakeContext) error) func(*icmd.Cmd) func() {
|
|
|
|
ctx := fakeContextFromNewTempDir(c)
|
2017-01-10 13:16:25 -05:00
|
|
|
for _, op := range contextOperators {
|
|
|
|
if err := op(ctx); err != nil {
|
|
|
|
c.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return func(cmd *icmd.Cmd) func() {
|
|
|
|
cmd.Dir = ctx.Dir
|
|
|
|
cmd.Command = append(cmd.Command, ".")
|
|
|
|
return closeBuildContext(c, ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func withBuildFlags(flags ...string) func(*icmd.Cmd) func() {
|
|
|
|
return func(cmd *icmd.Cmd) func() {
|
|
|
|
cmd.Command = append(cmd.Command, flags...)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func withoutCache(cmd *icmd.Cmd) func() {
|
|
|
|
cmd.Command = append(cmd.Command, "--no-cache")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func withFile(name, content string) func(*FakeContext) error {
|
|
|
|
return func(ctx *FakeContext) error {
|
|
|
|
return ctx.Add(name, content)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func closeBuildContext(c *check.C, ctx *FakeContext) func() {
|
|
|
|
return func() {
|
|
|
|
if err := ctx.Close(); err != nil {
|
|
|
|
c.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func withDockerfile(dockerfile string) func(*icmd.Cmd) func() {
|
|
|
|
return func(cmd *icmd.Cmd) func() {
|
|
|
|
cmd.Command = append(cmd.Command, "-")
|
|
|
|
cmd.Stdin = strings.NewReader(dockerfile)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func trustedBuild(cmd *icmd.Cmd) func() {
|
|
|
|
trustedCmd(cmd)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func withEnvironmentVariales(envs ...string) func(cmd *icmd.Cmd) func() {
|
|
|
|
return func(cmd *icmd.Cmd) func() {
|
|
|
|
cmd.Env = envs
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-22 08:59:24 -04:00
|
|
|
type gitServer interface {
|
2015-03-09 23:53:28 -04:00
|
|
|
URL() string
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type localGitServer struct {
|
2014-07-24 03:19:16 -04:00
|
|
|
*httptest.Server
|
2015-03-09 23:53:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *localGitServer) Close() error {
|
|
|
|
r.Server.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *localGitServer) URL() string {
|
|
|
|
return r.Server.URL
|
|
|
|
}
|
|
|
|
|
2015-07-22 08:59:24 -04:00
|
|
|
type fakeGit struct {
|
2015-03-09 23:53:28 -04:00
|
|
|
root string
|
2015-07-22 08:59:24 -04:00
|
|
|
server gitServer
|
2014-07-24 03:19:16 -04:00
|
|
|
RepoURL string
|
|
|
|
}
|
|
|
|
|
2015-07-22 08:59:24 -04:00
|
|
|
func (g *fakeGit) Close() {
|
2015-03-09 23:53:28 -04:00
|
|
|
g.server.Close()
|
|
|
|
os.RemoveAll(g.root)
|
2014-07-24 03:19:16 -04:00
|
|
|
}
|
|
|
|
|
2017-01-16 05:30:14 -05:00
|
|
|
func newFakeGit(c *check.C, name string, files map[string]string, enforceLocalServer bool) *fakeGit {
|
|
|
|
ctx := fakeContextWithFiles(c, files)
|
2014-07-24 03:19:16 -04:00
|
|
|
defer ctx.Close()
|
|
|
|
curdir, err := os.Getwd()
|
|
|
|
if err != nil {
|
2017-01-16 05:30:14 -05:00
|
|
|
c.Fatal(err)
|
2014-07-24 03:19:16 -04:00
|
|
|
}
|
|
|
|
defer os.Chdir(curdir)
|
|
|
|
|
|
|
|
if output, err := exec.Command("git", "init", ctx.Dir).CombinedOutput(); err != nil {
|
2017-01-16 05:30:14 -05:00
|
|
|
c.Fatalf("error trying to init repo: %s (%s)", err, output)
|
2014-07-24 03:19:16 -04:00
|
|
|
}
|
|
|
|
err = os.Chdir(ctx.Dir)
|
|
|
|
if err != nil {
|
2017-01-16 05:30:14 -05:00
|
|
|
c.Fatal(err)
|
2014-07-24 03:19:16 -04:00
|
|
|
}
|
2015-02-14 05:27:31 -05:00
|
|
|
if output, err := exec.Command("git", "config", "user.name", "Fake User").CombinedOutput(); err != nil {
|
2017-01-16 05:30:14 -05:00
|
|
|
c.Fatalf("error trying to set 'user.name': %s (%s)", err, output)
|
2015-02-14 05:27:31 -05:00
|
|
|
}
|
|
|
|
if output, err := exec.Command("git", "config", "user.email", "fake.user@example.com").CombinedOutput(); err != nil {
|
2017-01-16 05:30:14 -05:00
|
|
|
c.Fatalf("error trying to set 'user.email': %s (%s)", err, output)
|
2015-02-14 05:27:31 -05:00
|
|
|
}
|
2014-07-24 03:19:16 -04:00
|
|
|
if output, err := exec.Command("git", "add", "*").CombinedOutput(); err != nil {
|
2017-01-16 05:30:14 -05:00
|
|
|
c.Fatalf("error trying to add files to repo: %s (%s)", err, output)
|
2014-07-24 03:19:16 -04:00
|
|
|
}
|
|
|
|
if output, err := exec.Command("git", "commit", "-a", "-m", "Initial commit").CombinedOutput(); err != nil {
|
2017-01-16 05:30:14 -05:00
|
|
|
c.Fatalf("error trying to commit to repo: %s (%s)", err, output)
|
2014-07-24 03:19:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
root, err := ioutil.TempDir("", "docker-test-git-repo")
|
|
|
|
if err != nil {
|
2017-01-16 05:30:14 -05:00
|
|
|
c.Fatal(err)
|
2014-07-24 03:19:16 -04:00
|
|
|
}
|
|
|
|
repoPath := filepath.Join(root, name+".git")
|
|
|
|
if output, err := exec.Command("git", "clone", "--bare", ctx.Dir, repoPath).CombinedOutput(); err != nil {
|
|
|
|
os.RemoveAll(root)
|
2017-01-16 05:30:14 -05:00
|
|
|
c.Fatalf("error trying to clone --bare: %s (%s)", err, output)
|
2014-07-24 03:19:16 -04:00
|
|
|
}
|
|
|
|
err = os.Chdir(repoPath)
|
|
|
|
if err != nil {
|
|
|
|
os.RemoveAll(root)
|
2017-01-16 05:30:14 -05:00
|
|
|
c.Fatal(err)
|
2014-07-24 03:19:16 -04:00
|
|
|
}
|
|
|
|
if output, err := exec.Command("git", "update-server-info").CombinedOutput(); err != nil {
|
|
|
|
os.RemoveAll(root)
|
2017-01-16 05:30:14 -05:00
|
|
|
c.Fatalf("error trying to git update-server-info: %s (%s)", err, output)
|
2014-07-24 03:19:16 -04:00
|
|
|
}
|
|
|
|
err = os.Chdir(curdir)
|
|
|
|
if err != nil {
|
|
|
|
os.RemoveAll(root)
|
2017-01-16 05:30:14 -05:00
|
|
|
c.Fatal(err)
|
2014-07-24 03:19:16 -04:00
|
|
|
}
|
2015-03-09 23:53:28 -04:00
|
|
|
|
2015-07-22 08:59:24 -04:00
|
|
|
var server gitServer
|
2015-03-09 23:53:28 -04:00
|
|
|
if !enforceLocalServer {
|
|
|
|
// use fakeStorage server, which might be local or remote (at test daemon)
|
2017-01-16 05:30:14 -05:00
|
|
|
server = fakeStorageWithContext(c, fakeContextFromDir(root))
|
2015-03-09 23:53:28 -04:00
|
|
|
} else {
|
2015-12-13 11:00:39 -05:00
|
|
|
// always start a local http server on CLI test machine
|
2015-03-09 23:53:28 -04:00
|
|
|
httpServer := httptest.NewServer(http.FileServer(http.Dir(root)))
|
|
|
|
server = &localGitServer{httpServer}
|
|
|
|
}
|
2015-07-22 08:59:24 -04:00
|
|
|
return &fakeGit{
|
2015-03-09 23:53:28 -04:00
|
|
|
root: root,
|
|
|
|
server: server,
|
|
|
|
RepoURL: fmt.Sprintf("%s/%s.git", server.URL(), name),
|
2017-01-16 05:30:14 -05:00
|
|
|
}
|
2014-07-24 03:19:16 -04: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-01-13 11:23:28 -05:00
|
|
|
return filepath.Join(testEnv.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.
|
2014-10-24 14:12:54 -04:00
|
|
|
func runCommandAndReadContainerFile(filename string, cmd *exec.Cmd) ([]byte, error) {
|
|
|
|
out, _, err := runCommandWithOutput(cmd)
|
|
|
|
if err != nil {
|
2014-10-24 17:39:12 -04:00
|
|
|
return nil, fmt.Errorf("%v: %q", err, out)
|
2014-10-24 14:12:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
contID := strings.TrimSpace(out)
|
|
|
|
|
2015-08-11 03:41:11 -04:00
|
|
|
if err := waitRun(contID); err != nil {
|
|
|
|
return nil, fmt.Errorf("%v: %q", contID, err)
|
|
|
|
}
|
|
|
|
|
2014-10-24 14:12:54 -04:00
|
|
|
return readContainerFile(contID, filename)
|
|
|
|
}
|
|
|
|
|
2015-07-22 08:59:24 -04:00
|
|
|
func readContainerFile(containerID, filename string) ([]byte, error) {
|
|
|
|
f, err := os.Open(containerStorageFile(containerID, filename))
|
2014-10-24 14:12:54 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
content, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return content, nil
|
|
|
|
}
|
2015-01-13 13:46:32 -05:00
|
|
|
|
2015-07-22 08:59:24 -04:00
|
|
|
func readContainerFileWithExec(containerID, filename string) ([]byte, error) {
|
|
|
|
out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "exec", containerID, "cat", filename))
|
2015-02-16 14:32:25 -05:00
|
|
|
return []byte(out), err
|
|
|
|
}
|
|
|
|
|
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-01-13 11:23:28 -05:00
|
|
|
if testEnv.LocalDaemon() {
|
2015-03-10 17:19:32 -04:00
|
|
|
return time.Now()
|
|
|
|
}
|
|
|
|
|
2016-12-30 04:49:36 -05:00
|
|
|
status, body, err := request.SockRequest("GET", "/info", nil, daemonHost())
|
2015-04-20 17:03:56 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2015-10-11 03:33:24 -04:00
|
|
|
c.Assert(status, check.Equals, http.StatusOK)
|
2015-03-10 17:19:32 -04:00
|
|
|
|
|
|
|
type infoJSON struct {
|
|
|
|
SystemTime string
|
|
|
|
}
|
|
|
|
var info infoJSON
|
2015-10-11 03:33:24 -04:00
|
|
|
err = json.Unmarshal(body, &info)
|
|
|
|
c.Assert(err, check.IsNil, check.Commentf("unable to unmarshal GET /info response"))
|
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()))
|
|
|
|
}
|
|
|
|
|
2016-12-30 13:10:04 -05:00
|
|
|
func setupRegistry(c *check.C, schema1 bool, auth, tokenURL string) *registry.V2 {
|
|
|
|
reg, err := registry.NewV2(schema1, auth, tokenURL, privateRegistryURL)
|
2015-10-11 03:33:24 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2015-01-31 14:28:11 -05:00
|
|
|
|
|
|
|
// Wait for registry to be ready to serve requests.
|
2015-11-20 17:12:12 -05:00
|
|
|
for i := 0; i != 50; i++ {
|
2015-01-31 14:28:11 -05:00
|
|
|
if err = reg.Ping(); err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
2016-01-10 13:10:45 -05:00
|
|
|
c.Assert(err, check.IsNil, check.Commentf("Timeout waiting for test registry to become available: %v", err))
|
2015-04-24 17:16:56 -04:00
|
|
|
return reg
|
2015-01-13 13:46:32 -05:00
|
|
|
}
|
2015-02-12 14:20:48 -05:00
|
|
|
|
2015-07-20 01:56:10 -04:00
|
|
|
func setupNotary(c *check.C) *testNotary {
|
|
|
|
ts, err := newTestNotary(c)
|
2015-10-11 03:33:24 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2015-07-20 01:56:10 -04:00
|
|
|
|
|
|
|
return ts
|
|
|
|
}
|
|
|
|
|
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
|
|
|
func waitForContainer(contID string, args ...string) error {
|
2016-08-04 12:57:34 -04:00
|
|
|
args = append([]string{dockerBinary, "run", "--name", contID}, args...)
|
|
|
|
result := icmd.RunCmd(icmd.Cmd{Command: args})
|
|
|
|
if result.Error != nil {
|
|
|
|
return result.Error
|
2015-09-09 09:36:44 -04:00
|
|
|
}
|
2016-08-04 12:57:34 -04:00
|
|
|
return waitRun(contID)
|
2015-09-09 09:36:44 -04:00
|
|
|
}
|
|
|
|
|
2016-11-09 17:43:08 -05:00
|
|
|
// waitRestart will wait for the specified container to restart once
|
|
|
|
func waitRestart(contID string, duration time.Duration) error {
|
|
|
|
return waitInspect(contID, "{{.RestartCount}}", "1", duration)
|
|
|
|
}
|
|
|
|
|
2015-09-09 09:36:44 -04:00
|
|
|
// waitRun will wait for the specified container to be running, maximum 5 seconds.
|
|
|
|
func waitRun(contID string) error {
|
2015-09-01 17:37:04 -04:00
|
|
|
return waitInspect(contID, "{{.State.Running}}", "true", 5*time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
// waitExited will wait for the specified container to state exit, subject
|
|
|
|
// to a maximum time limit in seconds supplied by the caller
|
|
|
|
func waitExited(contID string, duration time.Duration) error {
|
|
|
|
return waitInspect(contID, "{{.State.Status}}", "exited", duration)
|
2015-09-09 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.
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
endpoint := fmt.Sprintf("/%s/containers/%s/json", version, id)
|
2016-12-30 04:49:36 -05:00
|
|
|
status, body, err := request.SockRequest("GET", endpoint, nil, daemonHost())
|
2015-10-30 14:57:15 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(status, check.Equals, http.StatusOK)
|
|
|
|
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.
|
|
|
|
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)
|
2016-08-17 18:46:28 -04:00
|
|
|
args = append(args, sleepCommandForDaemonPlatform()...)
|
2016-01-26 23:16:36 -05:00
|
|
|
return dockerCmd(c, args...)
|
|
|
|
}
|
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 {
|
2016-12-25 14:28:38 -05:00
|
|
|
return testEnv.MinimalBaseImage()
|
2016-02-04 16:06:12 -05:00
|
|
|
}
|
2016-03-14 18:52:50 -04:00
|
|
|
|
|
|
|
func getGoroutineNumber() (int, error) {
|
|
|
|
i := struct {
|
|
|
|
NGoroutines int
|
|
|
|
}{}
|
2016-12-30 04:49:36 -05:00
|
|
|
status, b, err := request.SockRequest("GET", "/info", nil, daemonHost())
|
2016-03-14 18:52:50 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
}
|