mirror of https://github.com/docker/docs.git
Merge pull request #2282 from shykes/fix-tests
Tests: Cleanup the test suite
This commit is contained in:
commit
2e4ee72201
|
@ -112,6 +112,7 @@ func TestGetInfo(t *testing.T) {
|
||||||
|
|
||||||
func TestGetEvents(t *testing.T) {
|
func TestGetEvents(t *testing.T) {
|
||||||
runtime := mkRuntime(t)
|
runtime := mkRuntime(t)
|
||||||
|
defer nuke(runtime)
|
||||||
srv := &Server{
|
srv := &Server{
|
||||||
runtime: runtime,
|
runtime: runtime,
|
||||||
events: make([]utils.JSONMessage, 0, 64),
|
events: make([]utils.JSONMessage, 0, 64),
|
||||||
|
@ -471,10 +472,7 @@ func TestGetContainersChanges(t *testing.T) {
|
||||||
|
|
||||||
func TestGetContainersTop(t *testing.T) {
|
func TestGetContainersTop(t *testing.T) {
|
||||||
t.Skip("Fixme. Skipping test for now. Reported error when testing using dind: 'api_test.go:527: Expected 2 processes, found 0.'")
|
t.Skip("Fixme. Skipping test for now. Reported error when testing using dind: 'api_test.go:527: Expected 2 processes, found 0.'")
|
||||||
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}
|
||||||
|
|
|
@ -76,7 +76,7 @@ func TestCreateAccount(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupTempConfigFile() (*ConfigFile, error) {
|
func setupTempConfigFile() (*ConfigFile, error) {
|
||||||
root, err := ioutil.TempDir("", "docker-test")
|
root, err := ioutil.TempDir("", "docker-test-auth")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -101,6 +101,7 @@ func TestSameAuthDataPostSave(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
defer os.RemoveAll(configFile.rootPath)
|
||||||
|
|
||||||
err = SaveConfig(configFile)
|
err = SaveConfig(configFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -127,6 +128,7 @@ func TestResolveAuthConfigIndexServer(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
defer os.RemoveAll(configFile.rootPath)
|
||||||
|
|
||||||
for _, registry := range []string{"", IndexServerAddress()} {
|
for _, registry := range []string{"", IndexServerAddress()} {
|
||||||
resolved := configFile.ResolveAuthConfig(registry)
|
resolved := configFile.ResolveAuthConfig(registry)
|
||||||
|
@ -141,6 +143,7 @@ func TestResolveAuthConfigFullURL(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
defer os.RemoveAll(configFile.rootPath)
|
||||||
|
|
||||||
registryAuth := AuthConfig{
|
registryAuth := AuthConfig{
|
||||||
Username: "foo-user",
|
Username: "foo-user",
|
||||||
|
|
|
@ -229,10 +229,7 @@ func TestBuild(t *testing.T) {
|
||||||
|
|
||||||
func buildImage(context testContextTemplate, t *testing.T, srv *Server, useCache bool) *Image {
|
func buildImage(context testContextTemplate, t *testing.T, srv *Server, useCache bool) *Image {
|
||||||
if srv == nil {
|
if srv == nil {
|
||||||
runtime, err := newTestRuntime()
|
runtime := mkRuntime(t)
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer nuke(runtime)
|
defer nuke(runtime)
|
||||||
|
|
||||||
srv = &Server{
|
srv = &Server{
|
||||||
|
@ -370,10 +367,7 @@ func TestBuildEntrypoint(t *testing.T) {
|
||||||
// testing #1405 - config.Cmd does not get cleaned up if
|
// testing #1405 - config.Cmd does not get cleaned up if
|
||||||
// utilizing cache
|
// utilizing cache
|
||||||
func TestBuildEntrypointRunCleanup(t *testing.T) {
|
func TestBuildEntrypointRunCleanup(t *testing.T) {
|
||||||
runtime, err := newTestRuntime()
|
runtime := mkRuntime(t)
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer nuke(runtime)
|
defer nuke(runtime)
|
||||||
|
|
||||||
srv := &Server{
|
srv := &Server{
|
||||||
|
@ -402,10 +396,7 @@ func TestBuildEntrypointRunCleanup(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBuildImageWithCache(t *testing.T) {
|
func TestBuildImageWithCache(t *testing.T) {
|
||||||
runtime, err := newTestRuntime()
|
runtime := mkRuntime(t)
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer nuke(runtime)
|
defer nuke(runtime)
|
||||||
|
|
||||||
srv := &Server{
|
srv := &Server{
|
||||||
|
@ -433,10 +424,7 @@ func TestBuildImageWithCache(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBuildImageWithoutCache(t *testing.T) {
|
func TestBuildImageWithoutCache(t *testing.T) {
|
||||||
runtime, err := newTestRuntime()
|
runtime := mkRuntime(t)
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer nuke(runtime)
|
defer nuke(runtime)
|
||||||
|
|
||||||
srv := &Server{
|
srv := &Server{
|
||||||
|
@ -464,10 +452,7 @@ func TestBuildImageWithoutCache(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestForbiddenContextPath(t *testing.T) {
|
func TestForbiddenContextPath(t *testing.T) {
|
||||||
runtime, err := newTestRuntime()
|
runtime := mkRuntime(t)
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer nuke(runtime)
|
defer nuke(runtime)
|
||||||
|
|
||||||
srv := &Server{
|
srv := &Server{
|
||||||
|
@ -513,10 +498,7 @@ func TestForbiddenContextPath(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBuildADDFileNotFound(t *testing.T) {
|
func TestBuildADDFileNotFound(t *testing.T) {
|
||||||
runtime, err := newTestRuntime()
|
runtime := mkRuntime(t)
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer nuke(runtime)
|
defer nuke(runtime)
|
||||||
|
|
||||||
srv := &Server{
|
srv := &Server{
|
||||||
|
|
|
@ -1189,7 +1189,7 @@ func BenchmarkRunParallel(b *testing.B) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func tempDir(t *testing.T) string {
|
func tempDir(t *testing.T) string {
|
||||||
tmpDir, err := ioutil.TempDir("", "docker-test")
|
tmpDir, err := ioutil.TempDir("", "docker-test-container")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -87,45 +87,63 @@ func init() {
|
||||||
|
|
||||||
NetworkBridgeIface = unitTestNetworkBridge
|
NetworkBridgeIface = unitTestNetworkBridge
|
||||||
|
|
||||||
// Make it our Store root
|
// Setup the base runtime, which will be duplicated for each test.
|
||||||
if runtime, err := NewRuntimeFromDirectory(unitTestStoreBase, false); err != nil {
|
// (no tests are run directly in the base)
|
||||||
log.Fatalf("Unable to create a runtime for tests:", err)
|
setupBaseImage()
|
||||||
} else {
|
|
||||||
globalRuntime = runtime
|
|
||||||
}
|
|
||||||
|
|
||||||
// Cleanup any leftover container
|
// Create the "global runtime" with a long-running daemon for integration tests
|
||||||
for _, container := range globalRuntime.List() {
|
spawnGlobalDaemon()
|
||||||
if err := globalRuntime.Destroy(container); err != nil {
|
startFds, startGoroutines = utils.GetTotalUsedFds(), runtime.NumGoroutine()
|
||||||
log.Fatalf("Error destroying leftover container: %s", err)
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
func setupBaseImage() {
|
||||||
|
runtime, err := NewRuntimeFromDirectory(unitTestStoreBase, false)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Unable to create a runtime for tests:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the "Server"
|
// Create the "Server"
|
||||||
|
srv := &Server{
|
||||||
|
runtime: runtime,
|
||||||
|
enableCors: false,
|
||||||
|
pullingPool: make(map[string]struct{}),
|
||||||
|
pushingPool: make(map[string]struct{}),
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the unit test is not found, try to download it.
|
||||||
|
if img, err := runtime.repositories.LookupImage(unitTestImageName); err != nil || img.ID != unitTestImageID {
|
||||||
|
// Retrieve the Image
|
||||||
|
if err := srv.ImagePull(unitTestImageName, "", os.Stdout, utils.NewStreamFormatter(false), nil, nil, true); err != nil {
|
||||||
|
log.Fatalf("Unable to pull the test image:", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func spawnGlobalDaemon() {
|
||||||
|
if globalRuntime != nil {
|
||||||
|
utils.Debugf("Global runtime already exists. Skipping.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
globalRuntime = mkRuntime(log.New(os.Stderr, "", 0))
|
||||||
srv := &Server{
|
srv := &Server{
|
||||||
runtime: globalRuntime,
|
runtime: globalRuntime,
|
||||||
enableCors: false,
|
enableCors: false,
|
||||||
pullingPool: make(map[string]struct{}),
|
pullingPool: make(map[string]struct{}),
|
||||||
pushingPool: make(map[string]struct{}),
|
pushingPool: make(map[string]struct{}),
|
||||||
}
|
}
|
||||||
// If the unit test is not found, try to download it.
|
|
||||||
if img, err := globalRuntime.repositories.LookupImage(unitTestImageName); err != nil || img.ID != unitTestImageID {
|
|
||||||
// Retrieve the Image
|
|
||||||
if err := srv.ImagePull(unitTestImageName, "", os.Stdout, utils.NewStreamFormatter(false), nil, nil, true); err != nil {
|
|
||||||
log.Fatalf("Unable to pull the test image:", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Spawn a Daemon
|
// Spawn a Daemon
|
||||||
go func() {
|
go func() {
|
||||||
|
utils.Debugf("Spawning global daemon for integration tests")
|
||||||
if err := ListenAndServe(testDaemonProto, testDaemonAddr, srv, os.Getenv("DEBUG") != ""); err != nil {
|
if err := ListenAndServe(testDaemonProto, testDaemonAddr, srv, os.Getenv("DEBUG") != ""); err != nil {
|
||||||
log.Fatalf("Unable to spawn the test daemon:", err)
|
log.Fatalf("Unable to spawn the test daemon:", err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Give some time to ListenAndServer to actually start
|
// Give some time to ListenAndServer to actually start
|
||||||
|
// FIXME: use inmem transports instead of tcp
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
|
|
||||||
startFds, startGoroutines = utils.GetTotalUsedFds(), runtime.NumGoroutine()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: test that ImagePull(json=true) send correct json output
|
// FIXME: test that ImagePull(json=true) send correct json output
|
||||||
|
|
|
@ -351,6 +351,7 @@ func TestPools(t *testing.T) {
|
||||||
|
|
||||||
func TestLogEvent(t *testing.T) {
|
func TestLogEvent(t *testing.T) {
|
||||||
runtime := mkRuntime(t)
|
runtime := mkRuntime(t)
|
||||||
|
defer nuke(runtime)
|
||||||
srv := &Server{
|
srv := &Server{
|
||||||
runtime: runtime,
|
runtime: runtime,
|
||||||
events: make([]utils.JSONMessage, 0, 64),
|
events: make([]utils.JSONMessage, 0, 64),
|
||||||
|
|
|
@ -2,22 +2,38 @@ package docker
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/dotcloud/docker/utils"
|
"github.com/dotcloud/docker/utils"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
// This file contains utility functions for docker's unit test suite.
|
// This file contains utility functions for docker's unit test suite.
|
||||||
// It has to be named XXX_test.go, apparently, in other to access private functions
|
// It has to be named XXX_test.go, apparently, in other to access private functions
|
||||||
// from other XXX_test.go functions.
|
// from other XXX_test.go functions.
|
||||||
|
|
||||||
|
var globalTestID string
|
||||||
|
|
||||||
// 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(f Fataler) *Runtime {
|
func mkRuntime(f Fataler) *Runtime {
|
||||||
runtime, err := newTestRuntime()
|
// Use the caller function name as a prefix.
|
||||||
|
// This helps trace temp directories back to their test.
|
||||||
|
pc, _, _, _ := runtime.Caller(1)
|
||||||
|
callerLongName := runtime.FuncForPC(pc).Name()
|
||||||
|
parts := strings.Split(callerLongName, ".")
|
||||||
|
callerShortName := parts[len(parts) - 1]
|
||||||
|
if globalTestID == "" {
|
||||||
|
globalTestID = GenerateID()[:4]
|
||||||
|
}
|
||||||
|
prefix := fmt.Sprintf("docker-test%s-%s-", globalTestID, callerShortName)
|
||||||
|
utils.Debugf("prefix = '%s'", prefix)
|
||||||
|
|
||||||
|
runtime, err := newTestRuntime(prefix)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
f.Fatal(err)
|
f.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -30,8 +46,16 @@ type Fataler interface {
|
||||||
Fatal(args ...interface{})
|
Fatal(args ...interface{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTestRuntime() (*Runtime, error) {
|
func newTestRuntime(prefix string) (runtime *Runtime, err error) {
|
||||||
root, err := ioutil.TempDir("", "docker-test")
|
if prefix == "" {
|
||||||
|
prefix = "docker-test-"
|
||||||
|
}
|
||||||
|
utils.Debugf("prefix = %s", prefix)
|
||||||
|
utils.Debugf("newTestRuntime start")
|
||||||
|
root, err := ioutil.TempDir("", prefix)
|
||||||
|
defer func() {
|
||||||
|
utils.Debugf("newTestRuntime: %s", root)
|
||||||
|
}()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -42,7 +66,7 @@ func newTestRuntime() (*Runtime, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
runtime, err := NewRuntimeFromDirectory(root, false)
|
runtime, err = NewRuntimeFromDirectory(root, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ func displayFdGoroutines(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFinal(t *testing.T) {
|
func TestFinal(t *testing.T) {
|
||||||
cleanup(globalRuntime)
|
nuke(globalRuntime)
|
||||||
t.Logf("Start Fds: %d, Start Goroutines: %d", startFds, startGoroutines)
|
t.Logf("Start Fds: %d, Start Goroutines: %d", startFds, startGoroutines)
|
||||||
displayFdGoroutines(t)
|
displayFdGoroutines(t)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue