From 6c9e32ae32a8a6c9efdc3e6111215c3cefa8ee5f Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Thu, 11 Jul 2013 17:59:25 -0700 Subject: [PATCH] Simplify unit tests code with mkRuntime() Upstream-commit: 6bdb6f226b13d5fdd47f3665c9fb753207bf473b Component: engine --- components/engine/api_test.go | 111 ++++++---------------------- components/engine/buildfile_test.go | 5 +- components/engine/container_test.go | 15 +--- components/engine/runtime_test.go | 42 ++--------- components/engine/server_test.go | 21 ++---- components/engine/tags_test.go | 5 +- components/engine/utils_test.go | 31 +++++++- 7 files changed, 67 insertions(+), 163 deletions(-) diff --git a/components/engine/api_test.go b/components/engine/api_test.go index 95b915b738..04fdbb7b4a 100644 --- a/components/engine/api_test.go +++ b/components/engine/api_test.go @@ -41,10 +41,8 @@ func TestGetBoolParam(t *testing.T) { } func TestGetVersion(t *testing.T) { - runtime, err := newTestRuntime() - if err != nil { - t.Fatal(err) - } + var err error + runtime := mkRuntime(t) defer nuke(runtime) srv := &Server{runtime: runtime} @@ -65,10 +63,7 @@ func TestGetVersion(t *testing.T) { } func TestGetInfo(t *testing.T) { - runtime, err := newTestRuntime() - if err != nil { - t.Fatal(err) - } + runtime := mkRuntime(t) defer nuke(runtime) srv := &Server{runtime: runtime} @@ -95,10 +90,7 @@ func TestGetInfo(t *testing.T) { } func TestGetImagesJSON(t *testing.T) { - runtime, err := newTestRuntime() - if err != nil { - t.Fatal(err) - } + runtime := mkRuntime(t) defer nuke(runtime) srv := &Server{runtime: runtime} @@ -220,10 +212,7 @@ func TestGetImagesJSON(t *testing.T) { } func TestGetImagesViz(t *testing.T) { - runtime, err := newTestRuntime() - if err != nil { - t.Fatal(err) - } + runtime := mkRuntime(t) defer nuke(runtime) srv := &Server{runtime: runtime} @@ -248,10 +237,7 @@ func TestGetImagesViz(t *testing.T) { } func TestGetImagesHistory(t *testing.T) { - runtime, err := newTestRuntime() - if err != nil { - t.Fatal(err) - } + runtime := mkRuntime(t) defer nuke(runtime) srv := &Server{runtime: runtime} @@ -272,10 +258,7 @@ func TestGetImagesHistory(t *testing.T) { } func TestGetImagesByName(t *testing.T) { - runtime, err := newTestRuntime() - if err != nil { - t.Fatal(err) - } + runtime := mkRuntime(t) defer nuke(runtime) srv := &Server{runtime: runtime} @@ -295,10 +278,7 @@ func TestGetImagesByName(t *testing.T) { } func TestGetContainersJSON(t *testing.T) { - runtime, err := newTestRuntime() - if err != nil { - t.Fatal(err) - } + runtime := mkRuntime(t) defer nuke(runtime) srv := &Server{runtime: runtime} @@ -334,10 +314,7 @@ func TestGetContainersJSON(t *testing.T) { } func TestGetContainersExport(t *testing.T) { - runtime, err := newTestRuntime() - if err != nil { - t.Fatal(err) - } + runtime := mkRuntime(t) defer nuke(runtime) srv := &Server{runtime: runtime} @@ -389,10 +366,7 @@ func TestGetContainersExport(t *testing.T) { } func TestGetContainersChanges(t *testing.T) { - runtime, err := newTestRuntime() - if err != nil { - t.Fatal(err) - } + runtime := mkRuntime(t) defer nuke(runtime) srv := &Server{runtime: runtime} @@ -437,10 +411,7 @@ func TestGetContainersChanges(t *testing.T) { } func TestGetContainersByName(t *testing.T) { - runtime, err := newTestRuntime() - if err != nil { - t.Fatal(err) - } + runtime := mkRuntime(t) defer nuke(runtime) srv := &Server{runtime: runtime} @@ -473,10 +444,7 @@ func TestGetContainersByName(t *testing.T) { } func TestPostCommit(t *testing.T) { - runtime, err := newTestRuntime() - if err != nil { - t.Fatal(err) - } + runtime := mkRuntime(t) defer nuke(runtime) srv := &Server{runtime: runtime} @@ -522,10 +490,7 @@ func TestPostCommit(t *testing.T) { } func TestPostContainersCreate(t *testing.T) { - runtime, err := newTestRuntime() - if err != nil { - t.Fatal(err) - } + runtime := mkRuntime(t) defer nuke(runtime) srv := &Server{runtime: runtime} @@ -576,10 +541,7 @@ func TestPostContainersCreate(t *testing.T) { } func TestPostContainersKill(t *testing.T) { - runtime, err := newTestRuntime() - if err != nil { - t.Fatal(err) - } + runtime := mkRuntime(t) defer nuke(runtime) srv := &Server{runtime: runtime} @@ -621,10 +583,7 @@ func TestPostContainersKill(t *testing.T) { } func TestPostContainersRestart(t *testing.T) { - runtime, err := newTestRuntime() - if err != nil { - t.Fatal(err) - } + runtime := mkRuntime(t) defer nuke(runtime) srv := &Server{runtime: runtime} @@ -678,10 +637,7 @@ func TestPostContainersRestart(t *testing.T) { } func TestPostContainersStart(t *testing.T) { - runtime, err := newTestRuntime() - if err != nil { - t.Fatal(err) - } + runtime := mkRuntime(t) defer nuke(runtime) srv := &Server{runtime: runtime} @@ -731,10 +687,7 @@ func TestPostContainersStart(t *testing.T) { } func TestPostContainersStop(t *testing.T) { - runtime, err := newTestRuntime() - if err != nil { - t.Fatal(err) - } + runtime := mkRuntime(t) defer nuke(runtime) srv := &Server{runtime: runtime} @@ -781,10 +734,7 @@ func TestPostContainersStop(t *testing.T) { } func TestPostContainersWait(t *testing.T) { - runtime, err := newTestRuntime() - if err != nil { - t.Fatal(err) - } + runtime := mkRuntime(t) defer nuke(runtime) srv := &Server{runtime: runtime} @@ -826,10 +776,7 @@ func TestPostContainersWait(t *testing.T) { } func TestPostContainersAttach(t *testing.T) { - runtime, err := newTestRuntime() - if err != nil { - t.Fatal(err) - } + runtime := mkRuntime(t) defer nuke(runtime) srv := &Server{runtime: runtime} @@ -915,10 +862,7 @@ func TestPostContainersAttach(t *testing.T) { // FIXME: Test deleting container with volume // FIXME: Test deleting volume in use by other container func TestDeleteContainers(t *testing.T) { - runtime, err := newTestRuntime() - if err != nil { - t.Fatal(err) - } + runtime := mkRuntime(t) defer nuke(runtime) srv := &Server{runtime: runtime} @@ -958,10 +902,7 @@ func TestDeleteContainers(t *testing.T) { } func TestOptionsRoute(t *testing.T) { - runtime, err := newTestRuntime() - if err != nil { - t.Fatal(err) - } + runtime := mkRuntime(t) defer nuke(runtime) srv := &Server{runtime: runtime, enableCors: true} @@ -984,10 +925,7 @@ func TestOptionsRoute(t *testing.T) { } func TestGetEnabledCors(t *testing.T) { - runtime, err := newTestRuntime() - if err != nil { - t.Fatal(err) - } + runtime := mkRuntime(t) defer nuke(runtime) srv := &Server{runtime: runtime, enableCors: true} @@ -1025,10 +963,7 @@ func TestGetEnabledCors(t *testing.T) { } func TestDeleteImages(t *testing.T) { - runtime, err := newTestRuntime() - if err != nil { - t.Fatal(err) - } + runtime := mkRuntime(t) defer nuke(runtime) srv := &Server{runtime: runtime} diff --git a/components/engine/buildfile_test.go b/components/engine/buildfile_test.go index 8913284e8f..9749aa6402 100644 --- a/components/engine/buildfile_test.go +++ b/components/engine/buildfile_test.go @@ -96,10 +96,7 @@ CMD Hello world func TestBuild(t *testing.T) { for _, ctx := range testContexts { - runtime, err := newTestRuntime() - if err != nil { - t.Fatal(err) - } + runtime := mkRuntime(t) defer nuke(runtime) srv := &Server{ diff --git a/components/engine/container_test.go b/components/engine/container_test.go index 7646bb3793..e7f6818eb6 100644 --- a/components/engine/container_test.go +++ b/components/engine/container_test.go @@ -1046,10 +1046,7 @@ func TestEnv(t *testing.T) { } func TestEntrypoint(t *testing.T) { - runtime, err := newTestRuntime() - if err != nil { - t.Fatal(err) - } + runtime := mkRuntime(t) defer nuke(runtime) container, err := NewBuilder(runtime).Create( &Config{ @@ -1125,10 +1122,7 @@ func TestLXCConfig(t *testing.T) { } func BenchmarkRunSequencial(b *testing.B) { - runtime, err := newTestRuntime() - if err != nil { - b.Fatal(err) - } + runtime := mkRuntime(b) defer nuke(runtime) for i := 0; i < b.N; i++ { container, err := NewBuilder(runtime).Create(&Config{ @@ -1154,10 +1148,7 @@ func BenchmarkRunSequencial(b *testing.B) { } func BenchmarkRunParallel(b *testing.B) { - runtime, err := newTestRuntime() - if err != nil { - b.Fatal(err) - } + runtime := mkRuntime(b) defer nuke(runtime) var tasks []chan error diff --git a/components/engine/runtime_test.go b/components/engine/runtime_test.go index d8033467c2..9d43bd46e5 100644 --- a/components/engine/runtime_test.go +++ b/components/engine/runtime_test.go @@ -114,26 +114,6 @@ func init() { // FIXME: test that ImagePull(json=true) send correct json output -func newTestRuntime() (*Runtime, error) { - root, err := ioutil.TempDir("", "docker-test") - if err != nil { - return nil, err - } - if err := os.Remove(root); err != nil { - return nil, err - } - if err := utils.CopyDirectory(unitTestStoreBase, root); err != nil { - return nil, err - } - - runtime, err := NewRuntimeFromDirectory(root, false) - if err != nil { - return nil, err - } - runtime.UpdateCapabilities(true) - return runtime, nil -} - func GetTestImage(runtime *Runtime) *Image { imgs, err := runtime.graph.All() if err != nil { @@ -148,10 +128,7 @@ func GetTestImage(runtime *Runtime) *Image { } func TestRuntimeCreate(t *testing.T) { - runtime, err := newTestRuntime() - if err != nil { - t.Fatal(err) - } + runtime := mkRuntime(t) defer nuke(runtime) // Make sure we start we 0 containers @@ -223,10 +200,7 @@ func TestRuntimeCreate(t *testing.T) { } func TestDestroy(t *testing.T) { - runtime, err := newTestRuntime() - if err != nil { - t.Fatal(err) - } + runtime := mkRuntime(t) defer nuke(runtime) container, err := NewBuilder(runtime).Create(&Config{ Image: GetTestImage(runtime).ID, @@ -270,10 +244,7 @@ func TestDestroy(t *testing.T) { } func TestGet(t *testing.T) { - runtime, err := newTestRuntime() - if err != nil { - t.Fatal(err) - } + runtime := mkRuntime(t) defer nuke(runtime) builder := NewBuilder(runtime) @@ -323,11 +294,8 @@ func TestGet(t *testing.T) { } func startEchoServerContainer(t *testing.T, proto string) (*Runtime, *Container, string) { - runtime, err := newTestRuntime() - if err != nil { - t.Fatal(err) - } - + var err error + runtime := mkRuntime(t) port := 5554 var container *Container var strPort string diff --git a/components/engine/server_test.go b/components/engine/server_test.go index cf3e3f0bc8..05a286aaa8 100644 --- a/components/engine/server_test.go +++ b/components/engine/server_test.go @@ -5,10 +5,7 @@ import ( ) func TestContainerTagImageDelete(t *testing.T) { - runtime, err := newTestRuntime() - if err != nil { - t.Fatal(err) - } + runtime := mkRuntime(t) defer nuke(runtime) srv := &Server{runtime: runtime} @@ -62,10 +59,7 @@ func TestContainerTagImageDelete(t *testing.T) { } func TestCreateRm(t *testing.T) { - runtime, err := newTestRuntime() - if err != nil { - t.Fatal(err) - } + runtime := mkRuntime(t) defer nuke(runtime) srv := &Server{runtime: runtime} @@ -95,10 +89,7 @@ func TestCreateRm(t *testing.T) { } func TestCreateStartRestartStopStartKillRm(t *testing.T) { - runtime, err := newTestRuntime() - if err != nil { - t.Fatal(err) - } + runtime := mkRuntime(t) defer nuke(runtime) srv := &Server{runtime: runtime} @@ -154,11 +145,9 @@ func TestCreateStartRestartStopStartKillRm(t *testing.T) { } func TestRunWithTooLowMemoryLimit(t *testing.T) { - runtime, err := newTestRuntime() + var err error + runtime := mkRuntime(t) srv := &Server{runtime: runtime} - if err != nil { - t.Fatal(err) - } defer nuke(runtime) // Try to create a container with a memory limit of 1 byte less than the minimum allowed limit. _, err = srv.ContainerCreate( diff --git a/components/engine/tags_test.go b/components/engine/tags_test.go index 1974e751be..d920943795 100644 --- a/components/engine/tags_test.go +++ b/components/engine/tags_test.go @@ -5,10 +5,7 @@ import ( ) func TestLookupImage(t *testing.T) { - runtime, err := newTestRuntime() - if err != nil { - t.Fatal(err) - } + runtime := mkRuntime(t) defer nuke(runtime) if img, err := runtime.repositories.LookupImage(unitTestImageName); err != nil { diff --git a/components/engine/utils_test.go b/components/engine/utils_test.go index 5958aa70fe..4951d3a02d 100644 --- a/components/engine/utils_test.go +++ b/components/engine/utils_test.go @@ -7,6 +7,7 @@ import ( "path" "strings" "testing" + "github.com/dotcloud/docker/utils" ) // This file contains utility functions for docker's unit test suite. @@ -15,14 +16,40 @@ import ( // Create a temporary runtime suitable for unit testing. // Call t.Fatal() at the first error. -func mkRuntime(t *testing.T) *Runtime { +func mkRuntime(f Fataler) *Runtime { runtime, err := newTestRuntime() if err != nil { - t.Fatal(err) + f.Fatal(err) } return runtime } +// A common interface to access the Fatal method of +// both testing.B and testing.T. +type Fataler interface { + Fatal(args ...interface{}) +} + +func newTestRuntime() (*Runtime, error) { + root, err := ioutil.TempDir("", "docker-test") + if err != nil { + return nil, err + } + if err := os.Remove(root); err != nil { + return nil, err + } + if err := utils.CopyDirectory(unitTestStoreBase, root); err != nil { + return nil, err + } + + runtime, err := NewRuntimeFromDirectory(root, false) + if err != nil { + return nil, err + } + runtime.UpdateCapabilities(true) + return runtime, nil +} + // Write `content` to the file at path `dst`, creating it if necessary, // as well as any missing directories. // The file is truncated if it already exists.