From 203651e774c9c2fd58bb6c100c0e3753d02e2f63 Mon Sep 17 00:00:00 2001 From: Kenfe-Mickael Laventure Date: Mon, 28 Mar 2016 16:22:17 -0700 Subject: [PATCH 1/2] Allow tests to specify their own events filter routine Signed-off-by: Kenfe-Mickael Laventure --- integration-test/bundle_utils_test.go | 8 ++++++++ integration-test/container_utils_test.go | 22 +++++++++++++++------- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/integration-test/bundle_utils_test.go b/integration-test/bundle_utils_test.go index 3ed4232..b9d6be8 100644 --- a/integration-test/bundle_utils_test.go +++ b/integration-test/bundle_utils_test.go @@ -102,6 +102,14 @@ func CreateBundleWithFilter(source, name string, args []string, filter func(spec return nil } +func GetBundle(name string) *Bundle { + bundle, ok := bundleMap[name] + if !ok { + return nil + } + return &bundle +} + func CreateBusyboxBundle(name string, args []string) error { return CreateBundleWithFilter("busybox", name, args, nil) } diff --git a/integration-test/container_utils_test.go b/integration-test/container_utils_test.go index 4dff21b..95bc2fd 100644 --- a/integration-test/container_utils_test.go +++ b/integration-test/container_utils_test.go @@ -180,13 +180,13 @@ func NewContainerProcess(cs *ContainerdSuite, bundle *Bundle, cid, pid string) ( return c, nil } -func (cs *ContainerdSuite) StartContainer(id, bundleName string) (c *containerProcess, err error) { - bundle, ok := bundleMap[bundleName] - if !ok { +func (cs *ContainerdSuite) StartContainerWithEventFilter(id, bundleName string, filter func(*types.Event)) (c *containerProcess, err error) { + bundle := GetBundle(bundleName) + if bundle == nil { return nil, fmt.Errorf("No such bundle '%s'", bundleName) } - c, err = NewContainerProcess(cs, &bundle, id, "init") + c, err = NewContainerProcess(cs, bundle, id, "init") if err != nil { return nil, err } @@ -199,9 +199,13 @@ func (cs *ContainerdSuite) StartContainer(id, bundleName string) (c *containerPr Stderr: filepath.Join(cs.cwd, c.io.stderr), } - cs.SetContainerEventFilter(id, func(event *types.Event) { - c.eventsCh <- event - }) + if filter == nil { + filter = func(event *types.Event) { + c.eventsCh <- event + } + } + + cs.SetContainerEventFilter(id, filter) if _, err := cs.grpcClient.CreateContainer(context.Background(), r); err != nil { c.Cleanup() @@ -211,6 +215,10 @@ func (cs *ContainerdSuite) StartContainer(id, bundleName string) (c *containerPr return c, nil } +func (cs *ContainerdSuite) StartContainer(id, bundleName string) (c *containerProcess, err error) { + return cs.StartContainerWithEventFilter(id, bundleName, nil) +} + func (cs *ContainerdSuite) RunContainer(id, bundleName string) (c *containerProcess, err error) { c, err = cs.StartContainer(id, bundleName) if err != nil { From 9455c01bc45e8d52f9436a4fcda4f2d8962174bf Mon Sep 17 00:00:00 2001 From: Kenfe-Mickael Laventure Date: Mon, 28 Mar 2016 16:23:41 -0700 Subject: [PATCH 2/2] Add a test checking container life duration Signed-off-by: Kenfe-Mickael Laventure --- integration-test/start_test.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/integration-test/start_test.go b/integration-test/start_test.go index db4f81f..5972a7d 100644 --- a/integration-test/start_test.go +++ b/integration-test/start_test.go @@ -96,3 +96,30 @@ func (cs *ContainerdSuite) TestStartBusyboxLsEvents(t *check.C) { } } } + +func (cs *ContainerdSuite) TestStartBusyboxSleep(t *check.C) { + if err := CreateBusyboxBundle("busybox-sleep-5", []string{"sleep", "5"}); err != nil { + t.Fatal(err) + } + + ch := make(chan interface{}) + filter := func(e *types.Event) { + if e.Type == "exit" && e.Pid == "init" { + ch <- nil + } + } + + start := time.Now() + _, err := cs.StartContainerWithEventFilter("sleep5", "busybox-sleep-5", filter) + if err != nil { + t.Fatal(err) + } + + // We add a generous 20% marge of error + select { + case <-ch: + t.Assert(uint64(time.Now().Sub(start)), checker.LessOrEqualThan, uint64(6*time.Second)) + case <-time.After(6 * time.Second): + t.Fatal("Container took more than 6 seconds to exit") + } +}