From fb005a3da813ba855b6597c146b66842846df3d8 Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Tue, 16 Jul 2013 14:38:18 +0000 Subject: [PATCH] add server.ContainerTop, server.poolAdd and ser.poolRemove tests --- server.go | 2 +- server_test.go | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) diff --git a/server.go b/server.go index c43cae0c38..48a64aa8c8 100644 --- a/server.go +++ b/server.go @@ -475,7 +475,7 @@ func (srv *Server) poolAdd(kind, key string) error { defer srv.Unlock() if _, exists := srv.pullingPool[key]; exists { - return fmt.Errorf("%s %s is already in progress", key, kind) + return fmt.Errorf("pull %s is already in progress", key) } switch kind { diff --git a/server_test.go b/server_test.go index 05a286aaa8..5181a501c9 100644 --- a/server_test.go +++ b/server_test.go @@ -2,6 +2,7 @@ package docker import ( "testing" + "time" ) func TestContainerTagImageDelete(t *testing.T) { @@ -163,3 +164,90 @@ func TestRunWithTooLowMemoryLimit(t *testing.T) { } } + +func TestContainerTop(t *testing.T) { + runtime := mkRuntime(t) + srv := &Server{runtime: runtime} + defer nuke(runtime) + + c, hostConfig := mkContainer(runtime, []string{"_", "/bin/sh", "-c", "sleep 2"}, t) + defer runtime.Destroy(c) + if err := c.Start(hostConfig); err != nil { + t.Fatal(err) + } + + // Give some time to the process to start + c.WaitTimeout(500 * time.Millisecond) + + if !c.State.Running { + t.Errorf("Container should be running") + } + procs, err := srv.ContainerTop(c.ID) + if err != nil { + t.Fatal(err) + } + + if len(procs) != 2 { + t.Fatalf("Expected 2 processes, found %d.", len(procs)) + } + + if procs[0].Cmd != "sh" && procs[0].Cmd != "busybox" { + t.Fatalf("Expected `busybox` or `sh`, found %s.", procs[0].Cmd) + } + + if procs[1].Cmd != "sh" && procs[1].Cmd != "busybox" { + t.Fatalf("Expected `busybox` or `sh`, found %s.", procs[1].Cmd) + } +} + +func TestPools(t *testing.T) { + runtime := mkRuntime(t) + srv := &Server{ + runtime: runtime, + pullingPool: make(map[string]struct{}), + pushingPool: make(map[string]struct{}), + } + defer nuke(runtime) + + err := srv.poolAdd("pull", "test1") + if err != nil { + t.Fatal(err) + } + err = srv.poolAdd("pull", "test2") + if err != nil { + t.Fatal(err) + } + err = srv.poolAdd("push", "test1") + if err == nil || err.Error() != "pull test1 is already in progress" { + t.Fatalf("Expected `pull test1 is already in progress`") + } + err = srv.poolAdd("pull", "test1") + if err == nil || err.Error() != "pull test1 is already in progress" { + t.Fatalf("Expected `pull test1 is already in progress`") + } + err = srv.poolAdd("wait", "test3") + if err == nil || err.Error() != "Unkown pool type" { + t.Fatalf("Expected `Unkown pool type`") + } + + err = srv.poolRemove("pull", "test2") + if err != nil { + t.Fatal(err) + } + err = srv.poolRemove("pull", "test2") + if err != nil { + t.Fatal(err) + } + err = srv.poolRemove("pull", "test1") + if err != nil { + t.Fatal(err) + } + err = srv.poolRemove("push", "test1") + if err != nil { + t.Fatal(err) + } + err = srv.poolRemove("wait", "test3") + if err == nil || err.Error() != "Unkown pool type" { + t.Fatalf("Expected `Unkown pool type`") + } +}