tests wait for the container to start

This commit is contained in:
Alex Dadgar 2016-07-12 11:36:06 -06:00
parent caa0e48841
commit 807bf3cf6c

View file

@ -4,8 +4,6 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"math/rand" "math/rand"
"os"
"os/exec"
"path/filepath" "path/filepath"
"reflect" "reflect"
"runtime/debug" "runtime/debug"
@ -15,13 +13,10 @@ import (
"time" "time"
docker "github.com/fsouza/go-dockerclient" docker "github.com/fsouza/go-dockerclient"
"github.com/hashicorp/go-plugin"
"github.com/hashicorp/nomad/client/allocdir" "github.com/hashicorp/nomad/client/allocdir"
"github.com/hashicorp/nomad/client/config" "github.com/hashicorp/nomad/client/config"
"github.com/hashicorp/nomad/client/driver/env" "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/client/testutil"
"github.com/hashicorp/nomad/helper/discover"
"github.com/hashicorp/nomad/nomad/structs" "github.com/hashicorp/nomad/nomad/structs"
tu "github.com/hashicorp/nomad/testutil" 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 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 // This test should always pass, even if docker daemon is not available
func TestDockerDriver_Fingerprint(t *testing.T) { func TestDockerDriver_Fingerprint(t *testing.T) {
t.Parallel() 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() t.Parallel()
if !testutil.DockerIsConnected(t) { if !testutil.DockerIsConnected(t) {
t.SkipNow() t.SkipNow()
@ -470,7 +425,7 @@ func TestDocker_StartN(t *testing.T) {
t.Log("Test complete!") t.Log("Test complete!")
} }
func TestDocker_StartNVersions(t *testing.T) { func TestDockerDriver_StartNVersions(t *testing.T) {
t.Parallel() t.Parallel()
if !testutil.DockerIsConnected(t) { if !testutil.DockerIsConnected(t) {
t.SkipNow() t.SkipNow()
@ -521,7 +476,22 @@ func TestDocker_StartNVersions(t *testing.T) {
t.Log("Test complete!") 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() t.Parallel()
expected := "host" expected := "host"
@ -544,25 +514,20 @@ func TestDocker_NetworkMode_Host(t *testing.T) {
client, handle, cleanup := dockerSetup(t, task) client, handle, cleanup := dockerSetup(t, task)
defer cleanup() defer cleanup()
actual := "" waitForExist(t, client, handle.(*DockerHandle))
tu.WaitForResult(func() (bool, error) {
container, err := client.InspectContainer(handle.(*DockerHandle).ContainerID())
if err != nil {
return false, err
}
actual = container.HostConfig.NetworkMode container, err := client.InspectContainer(handle.(*DockerHandle).ContainerID())
return actual == expected, nil if err != nil {
}, func(err error) { t.Fatalf("err: %v", err)
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) t.Fatalf("Got network mode %q; want %q", expected, actual)
}) }
} }
func TestDockerLabels(t *testing.T) { func TestDockerDriver_Labels(t *testing.T) {
t.Parallel() t.Parallel()
task, _, _ := dockerTask() task, _, _ := dockerTask()
task.Config["labels"] = []map[string]string{ task.Config["labels"] = []map[string]string{
@ -575,6 +540,8 @@ func TestDockerLabels(t *testing.T) {
client, handle, cleanup := dockerSetup(t, task) client, handle, cleanup := dockerSetup(t, task)
defer cleanup() defer cleanup()
waitForExist(t, client, handle.(*DockerHandle))
container, err := client.InspectContainer(handle.(*DockerHandle).ContainerID()) container, err := client.InspectContainer(handle.(*DockerHandle).ContainerID())
if err != nil { if err != nil {
t.Fatalf("err: %v", err) 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() t.Parallel()
task, _, _ := dockerTask() task, _, _ := dockerTask()
task.Config["dns_servers"] = []string{"8.8.8.8", "8.8.4.4"} 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) client, handle, cleanup := dockerSetup(t, task)
defer cleanup() defer cleanup()
waitForExist(t, client, handle.(*DockerHandle))
container, err := client.InspectContainer(handle.(*DockerHandle).ContainerID()) container, err := client.InspectContainer(handle.(*DockerHandle).ContainerID())
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
@ -621,13 +590,15 @@ func inSlice(needle string, haystack []string) bool {
return false return false
} }
func TestDockerPortsNoMap(t *testing.T) { func TestDockerDriver_PortsNoMap(t *testing.T) {
t.Parallel() t.Parallel()
task, res, dyn := dockerTask() task, res, dyn := dockerTask()
client, handle, cleanup := dockerSetup(t, task) client, handle, cleanup := dockerSetup(t, task)
defer cleanup() defer cleanup()
waitForExist(t, client, handle.(*DockerHandle))
container, err := client.InspectContainer(handle.(*DockerHandle).ContainerID()) container, err := client.InspectContainer(handle.(*DockerHandle).ContainerID())
if err != nil { if err != nil {
t.Fatalf("err: %v", err) 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() t.Parallel()
task, res, dyn := dockerTask() task, res, dyn := dockerTask()
task.Config["port_map"] = []map[string]string{ task.Config["port_map"] = []map[string]string{
@ -685,6 +656,8 @@ func TestDockerPortsMapping(t *testing.T) {
client, handle, cleanup := dockerSetup(t, task) client, handle, cleanup := dockerSetup(t, task)
defer cleanup() defer cleanup()
waitForExist(t, client, handle.(*DockerHandle))
container, err := client.InspectContainer(handle.(*DockerHandle).ContainerID()) container, err := client.InspectContainer(handle.(*DockerHandle).ContainerID())
if err != nil { if err != nil {
t.Fatalf("err: %v", err) 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() t.Parallel()
task := &structs.Task{ task := &structs.Task{
@ -848,6 +821,8 @@ func TestDockerDriver_Stats(t *testing.T) {
_, handle, cleanup := dockerSetup(t, task) _, handle, cleanup := dockerSetup(t, task)
defer cleanup() defer cleanup()
waitForExist(t, client, handle.(*DockerHandle))
go func() { go func() {
time.Sleep(3 * time.Second) time.Sleep(3 * time.Second)
ru, err := handle.Stats() ru, err := handle.Stats()