2015-01-30 19:23:47 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2015-04-18 12:46:47 -04:00
|
|
|
|
|
|
|
"github.com/go-check/check"
|
2015-01-30 19:23:47 -05:00
|
|
|
)
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestPause(c *check.C) {
|
2015-01-30 19:23:47 -05:00
|
|
|
defer unpauseAllContainers()
|
|
|
|
|
|
|
|
name := "testeventpause"
|
2015-04-18 12:46:47 -04:00
|
|
|
out, _ := dockerCmd(c, "images", "-q")
|
2015-01-30 19:23:47 -05:00
|
|
|
image := strings.Split(out, "\n")[0]
|
2015-04-18 12:46:47 -04:00
|
|
|
dockerCmd(c, "run", "-d", "--name", name, image, "top")
|
2015-01-30 19:23:47 -05:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
dockerCmd(c, "pause", name)
|
2015-01-30 19:23:47 -05:00
|
|
|
pausedContainers, err := getSliceOfPausedContainers()
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("error thrown while checking if containers were paused: %v", err)
|
2015-01-30 19:23:47 -05:00
|
|
|
}
|
|
|
|
if len(pausedContainers) != 1 {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("there should be one paused container and not %d", len(pausedContainers))
|
2015-01-30 19:23:47 -05:00
|
|
|
}
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
dockerCmd(c, "unpause", name)
|
2015-01-30 19:23:47 -05:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
eventsCmd := exec.Command(dockerBinary, "events", "--since=0", fmt.Sprintf("--until=%d", daemonTime(c).Unix()))
|
2015-01-30 19:23:47 -05:00
|
|
|
out, _, _ = runCommandWithOutput(eventsCmd)
|
|
|
|
events := strings.Split(out, "\n")
|
|
|
|
if len(events) <= 1 {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Missing expected event")
|
2015-01-30 19:23:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pauseEvent := strings.Fields(events[len(events)-3])
|
|
|
|
unpauseEvent := strings.Fields(events[len(events)-2])
|
|
|
|
|
|
|
|
if pauseEvent[len(pauseEvent)-1] != "pause" {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("event should be pause, not %#v", pauseEvent)
|
2015-01-30 19:23:47 -05:00
|
|
|
}
|
|
|
|
if unpauseEvent[len(unpauseEvent)-1] != "unpause" {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("event should be unpause, not %#v", unpauseEvent)
|
2015-01-30 19:23:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestPauseMultipleContainers(c *check.C) {
|
2015-01-30 19:23:47 -05:00
|
|
|
defer unpauseAllContainers()
|
|
|
|
|
|
|
|
containers := []string{
|
|
|
|
"testpausewithmorecontainers1",
|
|
|
|
"testpausewithmorecontainers2",
|
|
|
|
}
|
2015-04-18 12:46:47 -04:00
|
|
|
out, _ := dockerCmd(c, "images", "-q")
|
2015-01-30 19:23:47 -05:00
|
|
|
image := strings.Split(out, "\n")[0]
|
|
|
|
for _, name := range containers {
|
2015-04-18 12:46:47 -04:00
|
|
|
dockerCmd(c, "run", "-d", "--name", name, image, "top")
|
2015-01-30 19:23:47 -05:00
|
|
|
}
|
2015-04-18 12:46:47 -04:00
|
|
|
dockerCmd(c, append([]string{"pause"}, containers...)...)
|
2015-01-30 19:23:47 -05:00
|
|
|
pausedContainers, err := getSliceOfPausedContainers()
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("error thrown while checking if containers were paused: %v", err)
|
2015-01-30 19:23:47 -05:00
|
|
|
}
|
|
|
|
if len(pausedContainers) != len(containers) {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("there should be %d paused container and not %d", len(containers), len(pausedContainers))
|
2015-01-30 19:23:47 -05:00
|
|
|
}
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
dockerCmd(c, append([]string{"unpause"}, containers...)...)
|
2015-01-30 19:23:47 -05:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
eventsCmd := exec.Command(dockerBinary, "events", "--since=0", fmt.Sprintf("--until=%d", daemonTime(c).Unix()))
|
2015-01-30 19:23:47 -05:00
|
|
|
out, _, _ = runCommandWithOutput(eventsCmd)
|
|
|
|
events := strings.Split(out, "\n")
|
|
|
|
if len(events) <= len(containers)*3-2 {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Missing expected event")
|
2015-01-30 19:23:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pauseEvents := make([][]string, len(containers))
|
|
|
|
unpauseEvents := make([][]string, len(containers))
|
|
|
|
for i := range containers {
|
|
|
|
pauseEvents[i] = strings.Fields(events[len(events)-len(containers)*2-1+i])
|
|
|
|
unpauseEvents[i] = strings.Fields(events[len(events)-len(containers)-1+i])
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pauseEvent := range pauseEvents {
|
|
|
|
if pauseEvent[len(pauseEvent)-1] != "pause" {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("event should be pause, not %#v", pauseEvent)
|
2015-01-30 19:23:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, unpauseEvent := range unpauseEvents {
|
|
|
|
if unpauseEvent[len(unpauseEvent)-1] != "unpause" {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("event should be unpause, not %#v", unpauseEvent)
|
2015-01-30 19:23:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|