From 807bf3cf6ca72b3f2c4d8cbcc32668684b6b6380 Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Tue, 12 Jul 2016 11:36:06 -0600 Subject: [PATCH] tests wait for the container to start --- client/driver/docker_test.go | 109 ++++++++++++++--------------------- 1 file changed, 42 insertions(+), 67 deletions(-) diff --git a/client/driver/docker_test.go b/client/driver/docker_test.go index 9be44e354..21db1b064 100644 --- a/client/driver/docker_test.go +++ b/client/driver/docker_test.go @@ -4,8 +4,6 @@ import ( "fmt" "io/ioutil" "math/rand" - "os" - "os/exec" "path/filepath" "reflect" "runtime/debug" @@ -15,13 +13,10 @@ import ( "time" docker "github.com/fsouza/go-dockerclient" - "github.com/hashicorp/go-plugin" "github.com/hashicorp/nomad/client/allocdir" "github.com/hashicorp/nomad/client/config" "github.com/hashicorp/nomad/client/driver/env" - cstructs "github.com/hashicorp/nomad/client/driver/structs" "github.com/hashicorp/nomad/client/testutil" - "github.com/hashicorp/nomad/helper/discover" "github.com/hashicorp/nomad/nomad/structs" tu "github.com/hashicorp/nomad/testutil" ) @@ -116,46 +111,6 @@ func dockerSetup(t *testing.T, task *structs.Task) (*docker.Client, DriverHandle return client, handle, cleanup } -func TestDockerDriver_Handle(t *testing.T) { - t.Parallel() - - bin, err := discover.NomadExecutable() - if err != nil { - t.Fatalf("got an err: %v", err) - } - - f, _ := ioutil.TempFile(os.TempDir(), "") - defer f.Close() - defer os.Remove(f.Name()) - pluginConfig := &plugin.ClientConfig{ - Cmd: exec.Command(bin, "syslog", f.Name()), - } - exec, pluginClient, err := createExecutor(pluginConfig, os.Stdout, &config.Config{}) - if err != nil { - t.Fatalf("got an err: %v", err) - } - defer pluginClient.Kill() - - h := &DockerHandle{ - version: "version", - imageID: "imageid", - executor: exec, - pluginClient: pluginClient, - containerID: "containerid", - killTimeout: 5 * time.Nanosecond, - maxKillTimeout: 15 * time.Nanosecond, - doneCh: make(chan bool), - waitCh: make(chan *cstructs.WaitResult, 1), - } - - actual := h.ID() - expected := fmt.Sprintf("DOCKER:{\"Version\":\"version\",\"ImageID\":\"imageid\",\"ContainerID\":\"containerid\",\"KillTimeout\":5,\"MaxKillTimeout\":15,\"PluginConfig\":{\"Pid\":%d,\"AddrNet\":\"unix\",\"AddrName\":\"%s\"}}", - pluginClient.ReattachConfig().Pid, pluginClient.ReattachConfig().Addr.String()) - if actual != expected { - t.Errorf("Expected `%s`, found `%s`", expected, actual) - } -} - // This test should always pass, even if docker daemon is not available func TestDockerDriver_Fingerprint(t *testing.T) { t.Parallel() @@ -425,7 +380,7 @@ func TestDockerDriver_Start_Kill_Wait(t *testing.T) { } } -func TestDocker_StartN(t *testing.T) { +func TestDockerDriver_StartN(t *testing.T) { t.Parallel() if !testutil.DockerIsConnected(t) { t.SkipNow() @@ -470,7 +425,7 @@ func TestDocker_StartN(t *testing.T) { t.Log("Test complete!") } -func TestDocker_StartNVersions(t *testing.T) { +func TestDockerDriver_StartNVersions(t *testing.T) { t.Parallel() if !testutil.DockerIsConnected(t) { t.SkipNow() @@ -521,7 +476,22 @@ func TestDocker_StartNVersions(t *testing.T) { t.Log("Test complete!") } -func TestDocker_NetworkMode_Host(t *testing.T) { +func waitForExist(t *testing.T, client *docker.Client, handle *DockerHandle) { + tu.WaitForResult(func() (bool, error) { + container, err := client.InspectContainer(handle.ContainerID()) + if err != nil { + if _, ok := err.(*docker.NoSuchContainer); !ok { + return false, err + } + } + + return container != nil, nil + }, func(err error) { + t.Fatalf("err: %v", err) + }) +} + +func TestDockerDriver_NetworkMode_Host(t *testing.T) { t.Parallel() expected := "host" @@ -544,25 +514,20 @@ func TestDocker_NetworkMode_Host(t *testing.T) { client, handle, cleanup := dockerSetup(t, task) defer cleanup() - actual := "" - tu.WaitForResult(func() (bool, error) { - container, err := client.InspectContainer(handle.(*DockerHandle).ContainerID()) - if err != nil { - return false, err - } + waitForExist(t, client, handle.(*DockerHandle)) - actual = container.HostConfig.NetworkMode - return actual == expected, nil - }, func(err error) { - if err != nil { - t.Fatalf("err: %v", err) - } + container, err := client.InspectContainer(handle.(*DockerHandle).ContainerID()) + if err != nil { + t.Fatalf("err: %v", err) + } + + actual := container.HostConfig.NetworkMode + if actual != expected { t.Fatalf("Got network mode %q; want %q", expected, actual) - }) - + } } -func TestDockerLabels(t *testing.T) { +func TestDockerDriver_Labels(t *testing.T) { t.Parallel() task, _, _ := dockerTask() task.Config["labels"] = []map[string]string{ @@ -575,6 +540,8 @@ func TestDockerLabels(t *testing.T) { client, handle, cleanup := dockerSetup(t, task) defer cleanup() + waitForExist(t, client, handle.(*DockerHandle)) + container, err := client.InspectContainer(handle.(*DockerHandle).ContainerID()) if err != nil { t.Fatalf("err: %v", err) @@ -589,7 +556,7 @@ func TestDockerLabels(t *testing.T) { } } -func TestDockerDNS(t *testing.T) { +func TestDockerDriver_DNS(t *testing.T) { t.Parallel() task, _, _ := dockerTask() task.Config["dns_servers"] = []string{"8.8.8.8", "8.8.4.4"} @@ -598,6 +565,8 @@ func TestDockerDNS(t *testing.T) { client, handle, cleanup := dockerSetup(t, task) defer cleanup() + waitForExist(t, client, handle.(*DockerHandle)) + container, err := client.InspectContainer(handle.(*DockerHandle).ContainerID()) if err != nil { t.Fatalf("err: %v", err) @@ -621,13 +590,15 @@ func inSlice(needle string, haystack []string) bool { return false } -func TestDockerPortsNoMap(t *testing.T) { +func TestDockerDriver_PortsNoMap(t *testing.T) { t.Parallel() task, res, dyn := dockerTask() client, handle, cleanup := dockerSetup(t, task) defer cleanup() + waitForExist(t, client, handle.(*DockerHandle)) + container, err := client.InspectContainer(handle.(*DockerHandle).ContainerID()) if err != nil { t.Fatalf("err: %v", err) @@ -672,7 +643,7 @@ func TestDockerPortsNoMap(t *testing.T) { } } -func TestDockerPortsMapping(t *testing.T) { +func TestDockerDriver_PortsMapping(t *testing.T) { t.Parallel() task, res, dyn := dockerTask() task.Config["port_map"] = []map[string]string{ @@ -685,6 +656,8 @@ func TestDockerPortsMapping(t *testing.T) { client, handle, cleanup := dockerSetup(t, task) defer cleanup() + waitForExist(t, client, handle.(*DockerHandle)) + container, err := client.InspectContainer(handle.(*DockerHandle).ContainerID()) if err != nil { t.Fatalf("err: %v", err) @@ -728,7 +701,7 @@ func TestDockerPortsMapping(t *testing.T) { } } -func TestDockerUser(t *testing.T) { +func TestDockerDriver_User(t *testing.T) { t.Parallel() task := &structs.Task{ @@ -848,6 +821,8 @@ func TestDockerDriver_Stats(t *testing.T) { _, handle, cleanup := dockerSetup(t, task) defer cleanup() + waitForExist(t, client, handle.(*DockerHandle)) + go func() { time.Sleep(3 * time.Second) ru, err := handle.Stats()