Streamline events (#287)
* Sync process.State() with the matching events Signed-off-by: Kenfe-Mickael Laventure <mickael.laventure@gmail.com> * Allow requesting events for a specific container Signed-off-by: Kenfe-Mickael Laventure <mickael.laventure@gmail.com> * Sync container state retrieval with other events Signed-off-by: Kenfe-Mickael Laventure <mickael.laventure@gmail.com> * Let containerd take care of calling runtime delete on exit Signed-off-by: Kenfe-Mickael Laventure <mickael.laventure@gmail.com> * Take care of possible race in TestBusyboxTopExecTopKillInit Signed-off-by: Kenfe-Mickael Laventure <mickael.laventure@gmail.com>
This commit is contained in:
parent
6dd2f1c422
commit
90f827ca10
11 changed files with 342 additions and 212 deletions
|
@ -9,8 +9,11 @@ import (
|
|||
"path/filepath"
|
||||
"sort"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/docker/containerd/api/grpc/types"
|
||||
"github.com/golang/protobuf/ptypes"
|
||||
"github.com/golang/protobuf/ptypes/timestamp"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
|
@ -19,6 +22,21 @@ func (cs *ContainerdSuite) GetLogs() string {
|
|||
return string(b)
|
||||
}
|
||||
|
||||
func (cs *ContainerdSuite) Events(from time.Time, storedOnly bool, id string) (types.API_EventsClient, error) {
|
||||
var (
|
||||
ftsp *timestamp.Timestamp
|
||||
err error
|
||||
)
|
||||
if !from.IsZero() {
|
||||
ftsp, err = ptypes.TimestampProto(from)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return cs.grpcClient.Events(context.Background(), &types.EventsRequest{Timestamp: ftsp, StoredOnly: storedOnly, Id: id})
|
||||
}
|
||||
|
||||
func (cs *ContainerdSuite) ListRunningContainers() ([]*types.Container, error) {
|
||||
resp, err := cs.grpcClient.State(context.Background(), &types.StateRequest{})
|
||||
if err != nil {
|
||||
|
|
63
integration-test/events_test.go
Normal file
63
integration-test/events_test.go
Normal file
|
@ -0,0 +1,63 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/docker/containerd/api/grpc/types"
|
||||
"github.com/docker/docker/pkg/integration/checker"
|
||||
"github.com/go-check/check"
|
||||
)
|
||||
|
||||
func (cs *ContainerdSuite) TestEventsId(t *check.C) {
|
||||
if err := CreateBusyboxBundle("busybox-ls", []string{"ls"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
from := time.Now()
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
_, err := cs.RunContainer(fmt.Sprintf("ls-%d", i), "busybox-ls")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
containerID := "ls-4"
|
||||
|
||||
events, err := cs.Events(from, true, containerID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
evs := []*types.Event{}
|
||||
for {
|
||||
e, err := events.Recv()
|
||||
if err != nil {
|
||||
if err.Error() == "EOF" {
|
||||
break
|
||||
}
|
||||
t.Fatal(err)
|
||||
}
|
||||
evs = append(evs, e)
|
||||
}
|
||||
|
||||
t.Assert(len(evs), checker.Equals, 2)
|
||||
for idx, evt := range []types.Event{
|
||||
{
|
||||
Type: "start-container",
|
||||
Id: containerID,
|
||||
Status: 0,
|
||||
Pid: "",
|
||||
},
|
||||
{
|
||||
Type: "exit",
|
||||
Id: containerID,
|
||||
Status: 0,
|
||||
Pid: "init",
|
||||
},
|
||||
} {
|
||||
evt.Timestamp = evs[idx].Timestamp
|
||||
t.Assert(*evs[idx], checker.Equals, evt)
|
||||
}
|
||||
}
|
|
@ -137,7 +137,8 @@ func (cs *ContainerdSuite) TestBusyboxTopExecTopKillInit(t *check.C) {
|
|||
_, err = cs.AddProcessToContainer(initp, execID, "/", []string{"PATH=/usr/bin"}, []string{"top"}, 0, 0)
|
||||
t.Assert(err, checker.Equals, nil)
|
||||
|
||||
for idx, evt := range []types.Event{
|
||||
ch := initp.GetEventsChannel()
|
||||
for _, evt := range []types.Event{
|
||||
{
|
||||
Type: "start-container",
|
||||
Id: containerID,
|
||||
|
@ -150,26 +151,37 @@ func (cs *ContainerdSuite) TestBusyboxTopExecTopKillInit(t *check.C) {
|
|||
Status: 0,
|
||||
Pid: execID,
|
||||
},
|
||||
{
|
||||
Type: "exit",
|
||||
Id: containerID,
|
||||
Status: 137,
|
||||
Pid: execID,
|
||||
},
|
||||
{
|
||||
Type: "exit",
|
||||
Id: containerID,
|
||||
Status: 143,
|
||||
Pid: "init",
|
||||
},
|
||||
} {
|
||||
ch := initp.GetEventsChannel()
|
||||
e := <-ch
|
||||
evt.Timestamp = e.Timestamp
|
||||
t.Assert(*e, checker.Equals, evt)
|
||||
if idx == 1 {
|
||||
// Process Started, kill it
|
||||
cs.SignalContainerProcess(containerID, "init", uint32(syscall.SIGTERM))
|
||||
}
|
||||
|
||||
cs.SignalContainerProcess(containerID, "init", uint32(syscall.SIGTERM))
|
||||
for i := 0; i < 2; i++ {
|
||||
e := <-ch
|
||||
switch e.Pid {
|
||||
case "init":
|
||||
evt := types.Event{
|
||||
Type: "exit",
|
||||
Id: containerID,
|
||||
Status: 143,
|
||||
Pid: "init",
|
||||
Timestamp: e.Timestamp,
|
||||
}
|
||||
t.Assert(*e, checker.Equals, evt)
|
||||
case execID:
|
||||
evt := types.Event{
|
||||
Type: "exit",
|
||||
Id: containerID,
|
||||
Status: 137,
|
||||
Pid: execID,
|
||||
Timestamp: e.Timestamp,
|
||||
}
|
||||
t.Assert(*e, checker.Equals, evt)
|
||||
default:
|
||||
t.Fatalf("Unexpected event %v", e)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue