1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Using checkers assert for integration-cli/docker_cli_events_unix_test.go

Signed-off-by: Mohammed Aaqib Ansari <maaquib@gmail.com>
This commit is contained in:
Mohammed Aaqib Ansari 2015-10-19 19:15:24 -04:00
parent 82a8a14c4d
commit bc478b453f

View file

@ -12,6 +12,7 @@ import (
"time" "time"
"unicode" "unicode"
"github.com/docker/docker/pkg/integration/checker"
"github.com/go-check/check" "github.com/go-check/check"
"github.com/kr/pty" "github.com/kr/pty"
) )
@ -22,35 +23,25 @@ func (s *DockerSuite) TestEventsRedirectStdout(c *check.C) {
dockerCmd(c, "run", "busybox", "true") dockerCmd(c, "run", "busybox", "true")
file, err := ioutil.TempFile("", "") file, err := ioutil.TempFile("", "")
if err != nil { c.Assert(err, checker.IsNil, check.Commentf("could not create temp file"))
c.Fatalf("could not create temp file: %v", err)
}
defer os.Remove(file.Name()) defer os.Remove(file.Name())
command := fmt.Sprintf("%s events --since=%d --until=%d > %s", dockerBinary, since, daemonTime(c).Unix(), file.Name()) command := fmt.Sprintf("%s events --since=%d --until=%d > %s", dockerBinary, since, daemonTime(c).Unix(), file.Name())
_, tty, err := pty.Open() _, tty, err := pty.Open()
if err != nil { c.Assert(err, checker.IsNil, check.Commentf("Could not open pty"))
c.Fatalf("Could not open pty: %v", err)
}
cmd := exec.Command("sh", "-c", command) cmd := exec.Command("sh", "-c", command)
cmd.Stdin = tty cmd.Stdin = tty
cmd.Stdout = tty cmd.Stdout = tty
cmd.Stderr = tty cmd.Stderr = tty
if err := cmd.Run(); err != nil { c.Assert(cmd.Run(), checker.IsNil, check.Commentf("run err for command %q", command))
c.Fatalf("run err for command %q: %v", command, err)
}
scanner := bufio.NewScanner(file) scanner := bufio.NewScanner(file)
for scanner.Scan() { for scanner.Scan() {
for _, ch := range scanner.Text() { for _, ch := range scanner.Text() {
if unicode.IsControl(ch) { c.Assert(unicode.IsControl(ch), checker.False, check.Commentf("found control character %v", []byte(string(ch))))
c.Fatalf("found control character %v", []byte(string(ch)))
} }
} }
} c.Assert(scanner.Err(), checker.IsNil, check.Commentf("Scan err for command %q", command))
if err := scanner.Err(); err != nil {
c.Fatalf("Scan err for command %q: %v", command, err)
}
} }
@ -69,37 +60,25 @@ func (s *DockerSuite) TestEventsOOMDisableFalse(c *check.C) {
}() }()
select { select {
case err := <-errChan: case err := <-errChan:
c.Assert(err, check.IsNil) c.Assert(err, checker.IsNil)
case <-time.After(30 * time.Second): case <-time.After(30 * time.Second):
c.Fatal("Timeout waiting for container to die on OOM") c.Fatal("Timeout waiting for container to die on OOM")
} }
out, _ := dockerCmd(c, "events", "--since=0", "-f", "container=oomFalse", fmt.Sprintf("--until=%d", daemonTime(c).Unix())) out, _ := dockerCmd(c, "events", "--since=0", "-f", "container=oomFalse", fmt.Sprintf("--until=%d", daemonTime(c).Unix()))
events := strings.Split(strings.TrimSuffix(out, "\n"), "\n") events := strings.Split(strings.TrimSuffix(out, "\n"), "\n")
if len(events) < 5 { c.Assert(len(events), checker.GreaterOrEqualThan, 5) //Missing expected event
c.Fatalf("Missing expected event")
}
createEvent := strings.Fields(events[len(events)-5]) createEvent := strings.Fields(events[len(events)-5])
attachEvent := strings.Fields(events[len(events)-4]) attachEvent := strings.Fields(events[len(events)-4])
startEvent := strings.Fields(events[len(events)-3]) startEvent := strings.Fields(events[len(events)-3])
oomEvent := strings.Fields(events[len(events)-2]) oomEvent := strings.Fields(events[len(events)-2])
dieEvent := strings.Fields(events[len(events)-1]) dieEvent := strings.Fields(events[len(events)-1])
if createEvent[len(createEvent)-1] != "create" { c.Assert(createEvent[len(createEvent)-1], checker.Equals, "create", check.Commentf("event should be create, not %#v", createEvent))
c.Fatalf("event should be create, not %#v", createEvent) c.Assert(attachEvent[len(attachEvent)-1], checker.Equals, "attach", check.Commentf("event should be attach, not %#v", attachEvent))
} c.Assert(startEvent[len(startEvent)-1], checker.Equals, "start", check.Commentf("event should be start, not %#v", startEvent))
if attachEvent[len(attachEvent)-1] != "attach" { c.Assert(oomEvent[len(oomEvent)-1], checker.Equals, "oom", check.Commentf("event should be oom, not %#v", oomEvent))
c.Fatalf("event should be attach, not %#v", attachEvent) c.Assert(dieEvent[len(dieEvent)-1], checker.Equals, "die", check.Commentf("event should be die, not %#v", dieEvent))
}
if startEvent[len(startEvent)-1] != "start" {
c.Fatalf("event should be start, not %#v", startEvent)
}
if oomEvent[len(oomEvent)-1] != "oom" {
c.Fatalf("event should be oom, not %#v", oomEvent)
}
if dieEvent[len(dieEvent)-1] != "die" {
c.Fatalf("event should be die, not %#v", dieEvent)
}
} }
func (s *DockerSuite) TestEventsOOMDisableTrue(c *check.C) { func (s *DockerSuite) TestEventsOOMDisableTrue(c *check.C) {
@ -117,37 +96,25 @@ func (s *DockerSuite) TestEventsOOMDisableTrue(c *check.C) {
}() }()
select { select {
case err := <-errChan: case err := <-errChan:
c.Assert(err, check.IsNil) c.Assert(err, checker.IsNil)
case <-time.After(20 * time.Second): case <-time.After(20 * time.Second):
defer dockerCmd(c, "kill", "oomTrue") defer dockerCmd(c, "kill", "oomTrue")
out, _ := dockerCmd(c, "events", "--since=0", "-f", "container=oomTrue", fmt.Sprintf("--until=%d", daemonTime(c).Unix())) out, _ := dockerCmd(c, "events", "--since=0", "-f", "container=oomTrue", fmt.Sprintf("--until=%d", daemonTime(c).Unix()))
events := strings.Split(strings.TrimSuffix(out, "\n"), "\n") events := strings.Split(strings.TrimSuffix(out, "\n"), "\n")
if len(events) < 4 { c.Assert(len(events), checker.GreaterOrEqualThan, 4) //Missing expected event
c.Fatalf("Missing expected event")
}
createEvent := strings.Fields(events[len(events)-4]) createEvent := strings.Fields(events[len(events)-4])
attachEvent := strings.Fields(events[len(events)-3]) attachEvent := strings.Fields(events[len(events)-3])
startEvent := strings.Fields(events[len(events)-2]) startEvent := strings.Fields(events[len(events)-2])
oomEvent := strings.Fields(events[len(events)-1]) oomEvent := strings.Fields(events[len(events)-1])
if createEvent[len(createEvent)-1] != "create" { c.Assert(createEvent[len(createEvent)-1], checker.Equals, "create", check.Commentf("event should be create, not %#v", createEvent))
c.Fatalf("event should be create, not %#v", createEvent) c.Assert(attachEvent[len(attachEvent)-1], checker.Equals, "attach", check.Commentf("event should be attach, not %#v", attachEvent))
} c.Assert(startEvent[len(startEvent)-1], checker.Equals, "start", check.Commentf("event should be start, not %#v", startEvent))
if attachEvent[len(attachEvent)-1] != "attach" { c.Assert(oomEvent[len(oomEvent)-1], checker.Equals, "oom", check.Commentf("event should be oom, not %#v", oomEvent))
c.Fatalf("event should be attach, not %#v", attachEvent)
}
if startEvent[len(startEvent)-1] != "start" {
c.Fatalf("event should be start, not %#v", startEvent)
}
if oomEvent[len(oomEvent)-1] != "oom" {
c.Fatalf("event should be oom, not %#v", oomEvent)
}
out, _ = dockerCmd(c, "inspect", "-f", "{{.State.Status}}", "oomTrue") out, _ = dockerCmd(c, "inspect", "-f", "{{.State.Status}}", "oomTrue")
if strings.TrimSpace(out) != "running" { c.Assert(strings.TrimSpace(out), checker.Equals, "running", check.Commentf("container should be still running"))
c.Fatalf("container should be still running, not %v", out)
}
} }
} }