Skip to content

Commit

Permalink
bugfix: distinct init process exit and exec process exit
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Wan <[email protected]>
  • Loading branch information
HusterWan authored and allencloud committed Oct 24, 2018
1 parent e0efa2e commit 4106b91
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 3 deletions.
2 changes: 1 addition & 1 deletion apis/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ paths:
description: |
Stream real-time events from the server.
Report various object events of pouchd when something happens to them.
Containers report these events: create`, `destroy`, `die`, `oom`, `pause`, `rename`, `resize`, `restart`, `start`, `stop`, `top`, `unpause`, and `update`
Containers report these events: create`, `destroy`, `die`, `oom`, `pause`, `rename`, `resize`, `restart`, `start`, `stop`, `top`, `unpause`, `update` and `exec_die`
Images report these events: `pull`, `untag`
Volumes report these events: `create`, `destroy`
Networks report these events: `create`, `connect`, `disconnect`, `destroy`
Expand Down
9 changes: 7 additions & 2 deletions ctrd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,14 @@ func (c *Client) collectContainerdEvents() {
logrus.Warnf("failed to parse %s event: %#v", TaskExitEventTopic, out)
continue
}
action = "die"
if exitEvent.ID == exitEvent.ContainerID {
action = "die"
} else {
action = "exec_die"
attributes["execID"] = exitEvent.ID
}
containerID = exitEvent.ContainerID
attributes["exitcode"] = strconv.Itoa(int(exitEvent.ExitStatus))
attributes["exitCode"] = strconv.Itoa(int(exitEvent.ExitStatus))
case TaskOOMEventTopic:
oomEvent, ok := out.(*eventstypes.TaskOOM)
if !ok {
Expand Down
93 changes: 93 additions & 0 deletions test/cli_events_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"strings"
"time"

Expand Down Expand Up @@ -51,3 +52,95 @@ func (suite *PouchEventsSuite) TestEventsWorks(c *check.C) {
c.Errorf("unexpected output %s: should contains create and start events\n", out)
}
}

func delEmptyStrInSlice(strSlice []string) []string {
if len(strSlice) == 0 {
return strSlice
}

newSlice := []string{}
for _, v := range strSlice {
if v != "" {
newSlice = append(newSlice, v)
}
}

return newSlice
}

// TestExecDieEventWorks tests exec_die event work.
func (suite *PouchEventsSuite) TestExecDieEventWorks(c *check.C) {
name := "test-exec-die-event-works"

res := command.PouchRun("run", "-d", "--name", name, busyboxImage, "top")
defer DelContainerForceMultyTime(c, name)
res.Assert(c, icmd.Success)

// only works when test case run on the same machine with pouchd
time.Sleep(1100 * time.Millisecond)
start := time.Now()
command.PouchRun("exec", name, "echo", "test").Assert(c, icmd.Success)
time.Sleep(1100 * time.Millisecond)
end := time.Now()

since, until := start.Format(time.RFC3339), end.Format(time.RFC3339)
res = command.PouchRun("events", "--since", since, "--until", until)
output := res.Combined()

// check output contains exec_die event
lines := delEmptyStrInSlice(strings.Split(output, "\n"))
if len(lines) != 1 {
c.Errorf("unexpected output %s: should just contains 1 line", output)
}

if err := checkContainerEvent(lines[0], "exec_die"); err != nil {
c.Errorf("exec_die event check error: %v", err)
}
}

// TestDieEventWorks tests container die event work.
func (suite *PouchEventsSuite) TestDieEventWorks(c *check.C) {
name := "test-die-event-works"

res := command.PouchRun("run", "-d", "--name", name, busyboxImage, "top")
defer DelContainerForceMultyTime(c, name)
res.Assert(c, icmd.Success)

// only works when test case run on the same machine with pouchd
time.Sleep(1100 * time.Millisecond)
start := time.Now()
command.PouchRun("stop", name).Assert(c, icmd.Success)
time.Sleep(1100 * time.Millisecond)
end := time.Now()

since, until := start.Format(time.RFC3339), end.Format(time.RFC3339)
res = command.PouchRun("events", "--since", since, "--until", until)
output := res.Combined()

// check events when stop a container
lines := delEmptyStrInSlice(strings.Split(output, "\n"))
if len(lines) != 2 {
c.Errorf("unexpected output %s: should contains 2 event line when stop a container", output)
}

if err := checkContainerEvent(lines[0], "die"); err != nil {
c.Errorf("die event check error: %v", err)
}

if err := checkContainerEvent(lines[1], "stop"); err != nil {
c.Errorf("exec_die event check error: %v", err)
}
}

func checkContainerEvent(eventStr, eventType string) error {
strSlice := strings.Split(eventStr, " ")
if len(strSlice) < 4 {
return fmt.Errorf("unexpected output %s: output line may not be container event", eventStr)
}

if strSlice[1] != "container" || strSlice[2] != eventType {
return fmt.Errorf("unexpected output %s: should be %s events", eventStr, eventType)
}

return nil
}

0 comments on commit 4106b91

Please sign in to comment.