1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Simplify unit tests code with mkRuntime()

This commit is contained in:
Solomon Hykes 2013-07-11 17:59:25 -07:00
parent 2ac1141980
commit 6bdb6f226b
7 changed files with 67 additions and 163 deletions

View file

@ -41,10 +41,8 @@ func TestGetBoolParam(t *testing.T) {
} }
func TestGetVersion(t *testing.T) { func TestGetVersion(t *testing.T) {
runtime, err := newTestRuntime() var err error
if err != nil { runtime := mkRuntime(t)
t.Fatal(err)
}
defer nuke(runtime) defer nuke(runtime)
srv := &Server{runtime: runtime} srv := &Server{runtime: runtime}
@ -65,10 +63,7 @@ func TestGetVersion(t *testing.T) {
} }
func TestGetInfo(t *testing.T) { func TestGetInfo(t *testing.T) {
runtime, err := newTestRuntime() runtime := mkRuntime(t)
if err != nil {
t.Fatal(err)
}
defer nuke(runtime) defer nuke(runtime)
srv := &Server{runtime: runtime} srv := &Server{runtime: runtime}
@ -95,10 +90,7 @@ func TestGetInfo(t *testing.T) {
} }
func TestGetImagesJSON(t *testing.T) { func TestGetImagesJSON(t *testing.T) {
runtime, err := newTestRuntime() runtime := mkRuntime(t)
if err != nil {
t.Fatal(err)
}
defer nuke(runtime) defer nuke(runtime)
srv := &Server{runtime: runtime} srv := &Server{runtime: runtime}
@ -220,10 +212,7 @@ func TestGetImagesJSON(t *testing.T) {
} }
func TestGetImagesViz(t *testing.T) { func TestGetImagesViz(t *testing.T) {
runtime, err := newTestRuntime() runtime := mkRuntime(t)
if err != nil {
t.Fatal(err)
}
defer nuke(runtime) defer nuke(runtime)
srv := &Server{runtime: runtime} srv := &Server{runtime: runtime}
@ -248,10 +237,7 @@ func TestGetImagesViz(t *testing.T) {
} }
func TestGetImagesHistory(t *testing.T) { func TestGetImagesHistory(t *testing.T) {
runtime, err := newTestRuntime() runtime := mkRuntime(t)
if err != nil {
t.Fatal(err)
}
defer nuke(runtime) defer nuke(runtime)
srv := &Server{runtime: runtime} srv := &Server{runtime: runtime}
@ -272,10 +258,7 @@ func TestGetImagesHistory(t *testing.T) {
} }
func TestGetImagesByName(t *testing.T) { func TestGetImagesByName(t *testing.T) {
runtime, err := newTestRuntime() runtime := mkRuntime(t)
if err != nil {
t.Fatal(err)
}
defer nuke(runtime) defer nuke(runtime)
srv := &Server{runtime: runtime} srv := &Server{runtime: runtime}
@ -295,10 +278,7 @@ func TestGetImagesByName(t *testing.T) {
} }
func TestGetContainersJSON(t *testing.T) { func TestGetContainersJSON(t *testing.T) {
runtime, err := newTestRuntime() runtime := mkRuntime(t)
if err != nil {
t.Fatal(err)
}
defer nuke(runtime) defer nuke(runtime)
srv := &Server{runtime: runtime} srv := &Server{runtime: runtime}
@ -334,10 +314,7 @@ func TestGetContainersJSON(t *testing.T) {
} }
func TestGetContainersExport(t *testing.T) { func TestGetContainersExport(t *testing.T) {
runtime, err := newTestRuntime() runtime := mkRuntime(t)
if err != nil {
t.Fatal(err)
}
defer nuke(runtime) defer nuke(runtime)
srv := &Server{runtime: runtime} srv := &Server{runtime: runtime}
@ -389,10 +366,7 @@ func TestGetContainersExport(t *testing.T) {
} }
func TestGetContainersChanges(t *testing.T) { func TestGetContainersChanges(t *testing.T) {
runtime, err := newTestRuntime() runtime := mkRuntime(t)
if err != nil {
t.Fatal(err)
}
defer nuke(runtime) defer nuke(runtime)
srv := &Server{runtime: runtime} srv := &Server{runtime: runtime}
@ -437,10 +411,7 @@ func TestGetContainersChanges(t *testing.T) {
} }
func TestGetContainersByName(t *testing.T) { func TestGetContainersByName(t *testing.T) {
runtime, err := newTestRuntime() runtime := mkRuntime(t)
if err != nil {
t.Fatal(err)
}
defer nuke(runtime) defer nuke(runtime)
srv := &Server{runtime: runtime} srv := &Server{runtime: runtime}
@ -473,10 +444,7 @@ func TestGetContainersByName(t *testing.T) {
} }
func TestPostCommit(t *testing.T) { func TestPostCommit(t *testing.T) {
runtime, err := newTestRuntime() runtime := mkRuntime(t)
if err != nil {
t.Fatal(err)
}
defer nuke(runtime) defer nuke(runtime)
srv := &Server{runtime: runtime} srv := &Server{runtime: runtime}
@ -522,10 +490,7 @@ func TestPostCommit(t *testing.T) {
} }
func TestPostContainersCreate(t *testing.T) { func TestPostContainersCreate(t *testing.T) {
runtime, err := newTestRuntime() runtime := mkRuntime(t)
if err != nil {
t.Fatal(err)
}
defer nuke(runtime) defer nuke(runtime)
srv := &Server{runtime: runtime} srv := &Server{runtime: runtime}
@ -576,10 +541,7 @@ func TestPostContainersCreate(t *testing.T) {
} }
func TestPostContainersKill(t *testing.T) { func TestPostContainersKill(t *testing.T) {
runtime, err := newTestRuntime() runtime := mkRuntime(t)
if err != nil {
t.Fatal(err)
}
defer nuke(runtime) defer nuke(runtime)
srv := &Server{runtime: runtime} srv := &Server{runtime: runtime}
@ -621,10 +583,7 @@ func TestPostContainersKill(t *testing.T) {
} }
func TestPostContainersRestart(t *testing.T) { func TestPostContainersRestart(t *testing.T) {
runtime, err := newTestRuntime() runtime := mkRuntime(t)
if err != nil {
t.Fatal(err)
}
defer nuke(runtime) defer nuke(runtime)
srv := &Server{runtime: runtime} srv := &Server{runtime: runtime}
@ -678,10 +637,7 @@ func TestPostContainersRestart(t *testing.T) {
} }
func TestPostContainersStart(t *testing.T) { func TestPostContainersStart(t *testing.T) {
runtime, err := newTestRuntime() runtime := mkRuntime(t)
if err != nil {
t.Fatal(err)
}
defer nuke(runtime) defer nuke(runtime)
srv := &Server{runtime: runtime} srv := &Server{runtime: runtime}
@ -731,10 +687,7 @@ func TestPostContainersStart(t *testing.T) {
} }
func TestPostContainersStop(t *testing.T) { func TestPostContainersStop(t *testing.T) {
runtime, err := newTestRuntime() runtime := mkRuntime(t)
if err != nil {
t.Fatal(err)
}
defer nuke(runtime) defer nuke(runtime)
srv := &Server{runtime: runtime} srv := &Server{runtime: runtime}
@ -781,10 +734,7 @@ func TestPostContainersStop(t *testing.T) {
} }
func TestPostContainersWait(t *testing.T) { func TestPostContainersWait(t *testing.T) {
runtime, err := newTestRuntime() runtime := mkRuntime(t)
if err != nil {
t.Fatal(err)
}
defer nuke(runtime) defer nuke(runtime)
srv := &Server{runtime: runtime} srv := &Server{runtime: runtime}
@ -826,10 +776,7 @@ func TestPostContainersWait(t *testing.T) {
} }
func TestPostContainersAttach(t *testing.T) { func TestPostContainersAttach(t *testing.T) {
runtime, err := newTestRuntime() runtime := mkRuntime(t)
if err != nil {
t.Fatal(err)
}
defer nuke(runtime) defer nuke(runtime)
srv := &Server{runtime: runtime} srv := &Server{runtime: runtime}
@ -915,10 +862,7 @@ func TestPostContainersAttach(t *testing.T) {
// FIXME: Test deleting container with volume // FIXME: Test deleting container with volume
// FIXME: Test deleting volume in use by other container // FIXME: Test deleting volume in use by other container
func TestDeleteContainers(t *testing.T) { func TestDeleteContainers(t *testing.T) {
runtime, err := newTestRuntime() runtime := mkRuntime(t)
if err != nil {
t.Fatal(err)
}
defer nuke(runtime) defer nuke(runtime)
srv := &Server{runtime: runtime} srv := &Server{runtime: runtime}
@ -958,10 +902,7 @@ func TestDeleteContainers(t *testing.T) {
} }
func TestOptionsRoute(t *testing.T) { func TestOptionsRoute(t *testing.T) {
runtime, err := newTestRuntime() runtime := mkRuntime(t)
if err != nil {
t.Fatal(err)
}
defer nuke(runtime) defer nuke(runtime)
srv := &Server{runtime: runtime, enableCors: true} srv := &Server{runtime: runtime, enableCors: true}
@ -984,10 +925,7 @@ func TestOptionsRoute(t *testing.T) {
} }
func TestGetEnabledCors(t *testing.T) { func TestGetEnabledCors(t *testing.T) {
runtime, err := newTestRuntime() runtime := mkRuntime(t)
if err != nil {
t.Fatal(err)
}
defer nuke(runtime) defer nuke(runtime)
srv := &Server{runtime: runtime, enableCors: true} srv := &Server{runtime: runtime, enableCors: true}
@ -1025,10 +963,7 @@ func TestGetEnabledCors(t *testing.T) {
} }
func TestDeleteImages(t *testing.T) { func TestDeleteImages(t *testing.T) {
runtime, err := newTestRuntime() runtime := mkRuntime(t)
if err != nil {
t.Fatal(err)
}
defer nuke(runtime) defer nuke(runtime)
srv := &Server{runtime: runtime} srv := &Server{runtime: runtime}

View file

@ -96,10 +96,7 @@ CMD Hello world
func TestBuild(t *testing.T) { func TestBuild(t *testing.T) {
for _, ctx := range testContexts { for _, ctx := range testContexts {
runtime, err := newTestRuntime() runtime := mkRuntime(t)
if err != nil {
t.Fatal(err)
}
defer nuke(runtime) defer nuke(runtime)
srv := &Server{ srv := &Server{

View file

@ -1046,10 +1046,7 @@ func TestEnv(t *testing.T) {
} }
func TestEntrypoint(t *testing.T) { func TestEntrypoint(t *testing.T) {
runtime, err := newTestRuntime() runtime := mkRuntime(t)
if err != nil {
t.Fatal(err)
}
defer nuke(runtime) defer nuke(runtime)
container, err := NewBuilder(runtime).Create( container, err := NewBuilder(runtime).Create(
&Config{ &Config{
@ -1125,10 +1122,7 @@ func TestLXCConfig(t *testing.T) {
} }
func BenchmarkRunSequencial(b *testing.B) { func BenchmarkRunSequencial(b *testing.B) {
runtime, err := newTestRuntime() runtime := mkRuntime(b)
if err != nil {
b.Fatal(err)
}
defer nuke(runtime) defer nuke(runtime)
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
container, err := NewBuilder(runtime).Create(&Config{ container, err := NewBuilder(runtime).Create(&Config{
@ -1154,10 +1148,7 @@ func BenchmarkRunSequencial(b *testing.B) {
} }
func BenchmarkRunParallel(b *testing.B) { func BenchmarkRunParallel(b *testing.B) {
runtime, err := newTestRuntime() runtime := mkRuntime(b)
if err != nil {
b.Fatal(err)
}
defer nuke(runtime) defer nuke(runtime)
var tasks []chan error var tasks []chan error

View file

@ -114,26 +114,6 @@ func init() {
// FIXME: test that ImagePull(json=true) send correct json output // 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 { func GetTestImage(runtime *Runtime) *Image {
imgs, err := runtime.graph.All() imgs, err := runtime.graph.All()
if err != nil { if err != nil {
@ -148,10 +128,7 @@ func GetTestImage(runtime *Runtime) *Image {
} }
func TestRuntimeCreate(t *testing.T) { func TestRuntimeCreate(t *testing.T) {
runtime, err := newTestRuntime() runtime := mkRuntime(t)
if err != nil {
t.Fatal(err)
}
defer nuke(runtime) defer nuke(runtime)
// Make sure we start we 0 containers // Make sure we start we 0 containers
@ -223,10 +200,7 @@ func TestRuntimeCreate(t *testing.T) {
} }
func TestDestroy(t *testing.T) { func TestDestroy(t *testing.T) {
runtime, err := newTestRuntime() runtime := mkRuntime(t)
if err != nil {
t.Fatal(err)
}
defer nuke(runtime) defer nuke(runtime)
container, err := NewBuilder(runtime).Create(&Config{ container, err := NewBuilder(runtime).Create(&Config{
Image: GetTestImage(runtime).ID, Image: GetTestImage(runtime).ID,
@ -270,10 +244,7 @@ func TestDestroy(t *testing.T) {
} }
func TestGet(t *testing.T) { func TestGet(t *testing.T) {
runtime, err := newTestRuntime() runtime := mkRuntime(t)
if err != nil {
t.Fatal(err)
}
defer nuke(runtime) defer nuke(runtime)
builder := NewBuilder(runtime) builder := NewBuilder(runtime)
@ -323,11 +294,8 @@ func TestGet(t *testing.T) {
} }
func startEchoServerContainer(t *testing.T, proto string) (*Runtime, *Container, string) { func startEchoServerContainer(t *testing.T, proto string) (*Runtime, *Container, string) {
runtime, err := newTestRuntime() var err error
if err != nil { runtime := mkRuntime(t)
t.Fatal(err)
}
port := 5554 port := 5554
var container *Container var container *Container
var strPort string var strPort string

View file

@ -5,10 +5,7 @@ import (
) )
func TestContainerTagImageDelete(t *testing.T) { func TestContainerTagImageDelete(t *testing.T) {
runtime, err := newTestRuntime() runtime := mkRuntime(t)
if err != nil {
t.Fatal(err)
}
defer nuke(runtime) defer nuke(runtime)
srv := &Server{runtime: runtime} srv := &Server{runtime: runtime}
@ -62,10 +59,7 @@ func TestContainerTagImageDelete(t *testing.T) {
} }
func TestCreateRm(t *testing.T) { func TestCreateRm(t *testing.T) {
runtime, err := newTestRuntime() runtime := mkRuntime(t)
if err != nil {
t.Fatal(err)
}
defer nuke(runtime) defer nuke(runtime)
srv := &Server{runtime: runtime} srv := &Server{runtime: runtime}
@ -95,10 +89,7 @@ func TestCreateRm(t *testing.T) {
} }
func TestCreateStartRestartStopStartKillRm(t *testing.T) { func TestCreateStartRestartStopStartKillRm(t *testing.T) {
runtime, err := newTestRuntime() runtime := mkRuntime(t)
if err != nil {
t.Fatal(err)
}
defer nuke(runtime) defer nuke(runtime)
srv := &Server{runtime: runtime} srv := &Server{runtime: runtime}
@ -154,11 +145,9 @@ func TestCreateStartRestartStopStartKillRm(t *testing.T) {
} }
func TestRunWithTooLowMemoryLimit(t *testing.T) { func TestRunWithTooLowMemoryLimit(t *testing.T) {
runtime, err := newTestRuntime() var err error
runtime := mkRuntime(t)
srv := &Server{runtime: runtime} srv := &Server{runtime: runtime}
if err != nil {
t.Fatal(err)
}
defer nuke(runtime) defer nuke(runtime)
// Try to create a container with a memory limit of 1 byte less than the minimum allowed limit. // Try to create a container with a memory limit of 1 byte less than the minimum allowed limit.
_, err = srv.ContainerCreate( _, err = srv.ContainerCreate(

View file

@ -5,10 +5,7 @@ import (
) )
func TestLookupImage(t *testing.T) { func TestLookupImage(t *testing.T) {
runtime, err := newTestRuntime() runtime := mkRuntime(t)
if err != nil {
t.Fatal(err)
}
defer nuke(runtime) defer nuke(runtime)
if img, err := runtime.repositories.LookupImage(unitTestImageName); err != nil { if img, err := runtime.repositories.LookupImage(unitTestImageName); err != nil {

View file

@ -7,6 +7,7 @@ import (
"path" "path"
"strings" "strings"
"testing" "testing"
"github.com/dotcloud/docker/utils"
) )
// This file contains utility functions for docker's unit test suite. // This file contains utility functions for docker's unit test suite.
@ -15,14 +16,40 @@ import (
// Create a temporary runtime suitable for unit testing. // Create a temporary runtime suitable for unit testing.
// Call t.Fatal() at the first error. // Call t.Fatal() at the first error.
func mkRuntime(t *testing.T) *Runtime { func mkRuntime(f Fataler) *Runtime {
runtime, err := newTestRuntime() runtime, err := newTestRuntime()
if err != nil { if err != nil {
t.Fatal(err) f.Fatal(err)
} }
return runtime 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, // Write `content` to the file at path `dst`, creating it if necessary,
// as well as any missing directories. // as well as any missing directories.
// The file is truncated if it already exists. // The file is truncated if it already exists.