// RunAtDifferentDate runs the specified function with the given time. // It changes the date of the system, which can led to weird behaviors. func RunAtDifferentDate(date time.Time, block func()) { // Layout for date. MMDDhhmmYYYY const timeLayout = "010203042006" // Ensure we bring time back to now now := time.Now().Format(timeLayout) defer icmd.RunCommand("date", now) icmd.RunCommand("date", date.Format(timeLayout)) block() return }
func waitInspectWithArgs(name, expr, expected string, timeout time.Duration, arg ...string) error { after := time.After(timeout) args := append(arg, "inspect", "-f", expr, name) for { result := icmd.RunCommand(dockerBinary, args...) if result.Error != nil { if !strings.Contains(result.Stderr(), "No such") { return fmt.Errorf("error executing docker inspect: %v\n%s", result.Stderr(), result.Stdout()) } select { case <-after: return result.Error default: time.Sleep(10 * time.Millisecond) continue } } out := strings.TrimSpace(result.Stdout()) if out == expected { break } select { case <-after: return fmt.Errorf("condition \"%q == %q\" not true in time", out, expected) default: } time.Sleep(100 * time.Millisecond) } return nil }
func dockerCmd(c *check.C, args ...string) (string, int) { if err := validateArgs(args...); err != nil { c.Fatalf(err.Error()) } result := icmd.RunCommand(dockerBinary, args...) c.Assert(result, icmd.Matches, icmd.Success) return result.Combined(), result.ExitCode }
func dockerCmdWithError(args ...string) (string, int, error) { if err := validateArgs(args...); err != nil { return "", 0, err } result := icmd.RunCommand(dockerBinary, args...) if result.Error != nil { return result.Combined(), result.ExitCode, result.Compare(icmd.Success) } return result.Combined(), result.ExitCode, result.Error }
func dockerCmdWithStdoutStderr(c *check.C, args ...string) (string, string, int) { if err := validateArgs(args...); err != nil { c.Fatalf(err.Error()) } result := icmd.RunCommand(dockerBinary, args...) // TODO: why is c ever nil? if c != nil { c.Assert(result, icmd.Matches, icmd.Success) } return result.Stdout(), result.Stderr(), result.ExitCode }
func (s *DockerSuite) TestEventsImageLoad(c *check.C) { testRequires(c, DaemonIsLinux) myImageName := "footest:v1" dockerCmd(c, "tag", "busybox", myImageName) since := daemonUnixTime(c) out, _ := dockerCmd(c, "images", "-q", "--no-trunc", myImageName) longImageID := strings.TrimSpace(out) c.Assert(longImageID, checker.Not(check.Equals), "", check.Commentf("Id should not be empty")) dockerCmd(c, "save", "-o", "saveimg.tar", myImageName) dockerCmd(c, "rmi", myImageName) out, _ = dockerCmd(c, "images", "-q", myImageName) noImageID := strings.TrimSpace(out) c.Assert(noImageID, checker.Equals, "", check.Commentf("Should not have any image")) dockerCmd(c, "load", "-i", "saveimg.tar") result := icmd.RunCommand("rm", "-rf", "saveimg.tar") c.Assert(result, icmd.Matches, icmd.Success) out, _ = dockerCmd(c, "images", "-q", "--no-trunc", myImageName) imageID := strings.TrimSpace(out) c.Assert(imageID, checker.Equals, longImageID, check.Commentf("Should have same image id as before")) out, _ = dockerCmd(c, "events", "--since", since, "--until", daemonUnixTime(c), "--filter", "event=load") events := strings.Split(strings.TrimSpace(out), "\n") c.Assert(events, checker.HasLen, 1) matches := eventstestutils.ScanMap(events[0]) c.Assert(matches["id"], checker.Equals, imageID, check.Commentf("matches: %v\nout:\n%s\n", matches, out)) c.Assert(matches["action"], checker.Equals, "load", check.Commentf("matches: %v\nout:\n%s\n", matches, out)) out, _ = dockerCmd(c, "events", "--since", since, "--until", daemonUnixTime(c), "--filter", "event=save") events = strings.Split(strings.TrimSpace(out), "\n") c.Assert(events, checker.HasLen, 1) matches = eventstestutils.ScanMap(events[0]) c.Assert(matches["id"], checker.Equals, imageID, check.Commentf("matches: %v\nout:\n%s\n", matches, out)) c.Assert(matches["action"], checker.Equals, "save", check.Commentf("matches: %v\nout:\n%s\n", matches, out)) }
// Check that cp with unprivileged user doesn't return any error func (s *DockerSuite) TestCpUnprivilegedUser(c *check.C) { testRequires(c, DaemonIsLinux) testRequires(c, UnixCli) // uses chmod/su: not available on windows out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "touch "+cpTestName) containerID := strings.TrimSpace(out) out, _ = dockerCmd(c, "wait", containerID) // failed to set up container c.Assert(strings.TrimSpace(out), checker.Equals, "0") tmpdir, err := ioutil.TempDir("", "docker-integration") c.Assert(err, checker.IsNil) defer os.RemoveAll(tmpdir) c.Assert(os.Chmod(tmpdir, 0777), checker.IsNil) result := icmd.RunCommand("su", "unprivilegeduser", "-c", fmt.Sprintf("%s cp %s:%s %s", dockerBinary, containerID, cpTestName, tmpdir)) result.Assert(c, icmd.Expected{}) }
func (s *DockerSuite) TestExecExitStatus(c *check.C) { runSleepingContainer(c, "-d", "--name", "top") result := icmd.RunCommand(dockerBinary, "exec", "top", "sh", "-c", "exit 23") c.Assert(result, icmd.Matches, icmd.Expected{ExitCode: 23, Error: "exit status 23"}) }
func dockerCmdWithResult(args ...string) *icmd.Result { return icmd.RunCommand(dockerBinary, args...) }
func imageExists(image string) error { return icmd.RunCommand(dockerBinary, "inspect", image).Error }
func unpauseContainer(container string) error { return icmd.RunCommand(dockerBinary, "unpause", container).Error }
func deleteContainer(container ...string) error { result := icmd.RunCommand(dockerBinary, append([]string{"rm", "-fv"}, container...)...) return result.Compare(icmd.Success) }