Fixed docker tests

This commit is contained in:
Diptanu Choudhury 2016-08-11 19:23:48 -07:00
parent b71f687f62
commit 839ecd1df6
4 changed files with 62 additions and 47 deletions

View file

@ -49,7 +49,10 @@ func dockerTask() (*structs.Task, int, int) {
return &structs.Task{
Name: "redis-demo",
Config: map[string]interface{}{
"image": "redis",
"image": "busybox",
"load": []string{"busybox.tar"},
"command": "/bin/nc",
"args": []string{"-l", "127.0.0.1", "0"},
},
LogConfig: &structs.LogConfig{
MaxFiles: 10,
@ -92,6 +95,7 @@ func dockerSetup(t *testing.T, task *structs.Task) (*docker.Client, DriverHandle
driverCtx, execCtx := testDriverContexts(task)
driver := NewDockerDriver(driverCtx)
copyImage(execCtx, task, t)
handle, err := driver.Start(execCtx, task)
if err != nil {
@ -113,7 +117,6 @@ func dockerSetup(t *testing.T, task *structs.Task) (*docker.Client, DriverHandle
// This test should always pass, even if docker daemon is not available
func TestDockerDriver_Fingerprint(t *testing.T) {
t.Parallel()
driverCtx, _ := testDriverContexts(&structs.Task{Name: "foo"})
d := NewDockerDriver(driverCtx)
node := &structs.Node{
@ -133,15 +136,17 @@ func TestDockerDriver_Fingerprint(t *testing.T) {
}
func TestDockerDriver_StartOpen_Wait(t *testing.T) {
t.Parallel()
if !testutil.DockerIsConnected(t) {
t.SkipNow()
}
task := &structs.Task{
Name: "redis-demo",
Name: "nc-demo",
Config: map[string]interface{}{
"image": "redis",
"load": []string{"busybox.tar"},
"image": "busybox",
"command": "/bin/nc",
"args": []string{"-l", "127.0.0.1", "0"},
},
LogConfig: &structs.LogConfig{
MaxFiles: 10,
@ -153,6 +158,7 @@ func TestDockerDriver_StartOpen_Wait(t *testing.T) {
driverCtx, execCtx := testDriverContexts(task)
defer execCtx.AllocDir.Destroy()
d := NewDockerDriver(driverCtx)
copyImage(execCtx, task, t)
handle, err := d.Start(execCtx, task)
if err != nil {
@ -174,13 +180,13 @@ func TestDockerDriver_StartOpen_Wait(t *testing.T) {
}
func TestDockerDriver_Start_Wait(t *testing.T) {
t.Parallel()
task := &structs.Task{
Name: "redis-demo",
Name: "nc-demo",
Config: map[string]interface{}{
"image": "redis",
"command": "/usr/local/bin/redis-server",
"args": []string{"-v"},
"load": []string{"busybox.tar"},
"image": "busybox",
"command": "/bin/echo",
"args": []string{"hello"},
},
Resources: &structs.Resources{
MemoryMB: 256,
@ -212,7 +218,6 @@ func TestDockerDriver_Start_Wait(t *testing.T) {
}
func TestDockerDriver_Start_LoadImage(t *testing.T) {
t.Parallel()
if !testutil.DockerIsConnected(t) {
t.SkipNow()
}
@ -278,7 +283,6 @@ func TestDockerDriver_Start_LoadImage(t *testing.T) {
}
func TestDockerDriver_Start_Wait_AllocDir(t *testing.T) {
t.Parallel()
// This test requires that the alloc dir be mounted into docker as a volume.
// Because this cannot happen when docker is run remotely, e.g. when running
// docker in a VM, we skip this when we detect Docker is being run remotely.
@ -289,10 +293,11 @@ func TestDockerDriver_Start_Wait_AllocDir(t *testing.T) {
exp := []byte{'w', 'i', 'n'}
file := "output.txt"
task := &structs.Task{
Name: "redis-demo",
Name: "nc-demo",
Config: map[string]interface{}{
"image": "redis",
"command": "/bin/bash",
"image": "busybox",
"load": []string{"busybox.tar"},
"command": "/bin/sh",
"args": []string{
"-c",
fmt.Sprintf(`sleep 1; echo -n %s > $%s/%s`,
@ -312,6 +317,7 @@ func TestDockerDriver_Start_Wait_AllocDir(t *testing.T) {
driverCtx, execCtx := testDriverContexts(task)
defer execCtx.AllocDir.Destroy()
d := NewDockerDriver(driverCtx)
copyImage(execCtx, task, t)
handle, err := d.Start(execCtx, task)
if err != nil {
@ -344,11 +350,11 @@ func TestDockerDriver_Start_Wait_AllocDir(t *testing.T) {
}
func TestDockerDriver_Start_Kill_Wait(t *testing.T) {
t.Parallel()
task := &structs.Task{
Name: "redis-demo",
Name: "nc-demo",
Config: map[string]interface{}{
"image": "redis",
"image": "busybox",
"load": []string{"busybox.tar"},
"command": "/bin/sleep",
"args": []string{"10"},
},
@ -381,7 +387,6 @@ func TestDockerDriver_Start_Kill_Wait(t *testing.T) {
}
func TestDockerDriver_StartN(t *testing.T) {
t.Parallel()
if !testutil.DockerIsConnected(t) {
t.SkipNow()
}
@ -401,6 +406,7 @@ func TestDockerDriver_StartN(t *testing.T) {
driverCtx, execCtx := testDriverContexts(task)
defer execCtx.AllocDir.Destroy()
d := NewDockerDriver(driverCtx)
copyImage(execCtx, task, t)
handles[idx], err = d.Start(execCtx, task)
if err != nil {
@ -426,19 +432,21 @@ func TestDockerDriver_StartN(t *testing.T) {
}
func TestDockerDriver_StartNVersions(t *testing.T) {
t.Parallel()
if !testutil.DockerIsConnected(t) {
t.SkipNow()
}
task1, _, _ := dockerTask()
task1.Config["image"] = "redis"
task1.Config["image"] = "busybox"
task1.Config["load"] = []string{"busybox.tar"}
task2, _, _ := dockerTask()
task2.Config["image"] = "redis:latest"
task2.Config["image"] = "busybox:musl"
task2.Config["load"] = []string{"busybox_musl.tar"}
task3, _, _ := dockerTask()
task3.Config["image"] = "redis:3.0"
task3.Config["image"] = "busybox:glibc"
task3.Config["load"] = []string{"busybox_glibc.tar"}
taskList := []*structs.Task{task1, task2, task3}
@ -452,6 +460,7 @@ func TestDockerDriver_StartNVersions(t *testing.T) {
driverCtx, execCtx := testDriverContexts(task)
defer execCtx.AllocDir.Destroy()
d := NewDockerDriver(driverCtx)
copyImage(execCtx, task, t)
handles[idx], err = d.Start(execCtx, task)
if err != nil {
@ -492,13 +501,15 @@ func waitForExist(t *testing.T, client *docker.Client, handle *DockerHandle) {
}
func TestDockerDriver_NetworkMode_Host(t *testing.T) {
t.Parallel()
expected := "host"
task := &structs.Task{
Name: "redis-demo",
Name: "nc-demo",
Config: map[string]interface{}{
"image": "redis",
"image": "busybox",
"load": []string{"busybox.tar"},
"command": "/bin/nc",
"args": []string{"-l", "127.0.0.1", "0"},
"network_mode": expected,
},
Resources: &structs.Resources{
@ -528,7 +539,6 @@ func TestDockerDriver_NetworkMode_Host(t *testing.T) {
}
func TestDockerDriver_Labels(t *testing.T) {
t.Parallel()
task, _, _ := dockerTask()
task.Config["labels"] = []map[string]string{
map[string]string{
@ -557,7 +567,6 @@ func TestDockerDriver_Labels(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"}
task.Config["dns_search_domains"] = []string{"example.com", "example.org", "example.net"}
@ -582,7 +591,6 @@ func TestDockerDriver_DNS(t *testing.T) {
}
func TestDockerWorkDir(t *testing.T) {
t.Parallel()
task, _, _ := dockerTask()
task.Config["work_dir"] = "/some/path"
@ -609,7 +617,6 @@ func inSlice(needle string, haystack []string) bool {
}
func TestDockerDriver_PortsNoMap(t *testing.T) {
t.Parallel()
task, res, dyn := dockerTask()
client, handle, cleanup := dockerSetup(t, task)
@ -628,8 +635,6 @@ func TestDockerDriver_PortsNoMap(t *testing.T) {
docker.Port(fmt.Sprintf("%d/udp", res)): struct{}{},
docker.Port(fmt.Sprintf("%d/tcp", dyn)): struct{}{},
docker.Port(fmt.Sprintf("%d/udp", dyn)): struct{}{},
// This one comes from the redis container
docker.Port("6379/tcp"): struct{}{},
}
if !reflect.DeepEqual(container.Config.ExposedPorts, expectedExposedPorts) {
@ -662,7 +667,6 @@ func TestDockerDriver_PortsNoMap(t *testing.T) {
}
func TestDockerDriver_PortsMapping(t *testing.T) {
t.Parallel()
task, res, dyn := dockerTask()
task.Config["port_map"] = []map[string]string{
map[string]string{
@ -720,14 +724,13 @@ func TestDockerDriver_PortsMapping(t *testing.T) {
}
func TestDockerDriver_User(t *testing.T) {
t.Parallel()
task := &structs.Task{
Name: "redis-demo",
User: "alice",
Config: map[string]interface{}{
"image": "redis",
"command": "sleep",
"image": "busybox",
"load": []string{"busybox.tar"},
"command": "/bin/sleep",
"args": []string{"10000"},
},
Resources: &structs.Resources{
@ -747,6 +750,7 @@ func TestDockerDriver_User(t *testing.T) {
driverCtx, execCtx := testDriverContexts(task)
driver := NewDockerDriver(driverCtx)
defer execCtx.AllocDir.Destroy()
copyImage(execCtx, task, t)
// It should fail because the user "alice" does not exist on the given
// image.
@ -757,8 +761,7 @@ func TestDockerDriver_User(t *testing.T) {
}
msgs := []string{
"System error: Unable to find user alice",
"linux spec user: Unable to find user alice",
"linux spec user",
}
var found bool
for _, msg := range msgs {
@ -773,11 +776,11 @@ func TestDockerDriver_User(t *testing.T) {
}
func TestDockerDriver_CleanupContainer(t *testing.T) {
t.Parallel()
task := &structs.Task{
Name: "redis-demo",
Config: map[string]interface{}{
"image": "busybox",
"load": []string{"busybox.tar"},
"command": "/bin/echo",
"args": []string{"hello"},
},
@ -817,15 +820,14 @@ func TestDockerDriver_CleanupContainer(t *testing.T) {
case <-time.After(time.Duration(tu.TestMultiplier()*5) * time.Second):
t.Fatalf("timeout")
}
}
func TestDockerDriver_Stats(t *testing.T) {
t.Parallel()
task := &structs.Task{
Name: "sleep",
Config: map[string]interface{}{
"image": "busybox",
"load": []string{"busybox.tar"},
"command": "/bin/sleep",
"args": []string{"100"},
},
@ -867,3 +869,9 @@ func TestDockerDriver_Stats(t *testing.T) {
}
}
func copyImage(execCtx *ExecContext, task *structs.Task, t *testing.T) {
taskDir, _ := execCtx.AllocDir.TaskDirs[task.Name]
dst := filepath.Join(taskDir, allocdir.TaskLocal, "busybox.tar")
copyFile("./test-resources/docker/busybox.tar", dst, t)
}

BIN
client/driver/test-resources/docker/busybox_glibc.tar (Stored with Git LFS) Normal file

Binary file not shown.

BIN
client/driver/test-resources/docker/busybox_musl.tar (Stored with Git LFS) Normal file

Binary file not shown.

View file

@ -11,8 +11,9 @@ go build -o $TEMPDIR/nomad || exit 1
# Run the tests
echo "--> Running tests"
GOBIN="`which go`"
go list ./... | grep -v '^github.com/hashicorp/nomad/vendor/' | \
sudo \
-E PATH=$TEMPDIR:$PATH \
-E GOPATH=$GOPATH \
xargs $GOBIN test -v ${GOTEST_FLAGS:--cover -timeout=900s}
$GOBIN test -v -timeout=900s github.com/hashicorp/nomad/client/driver -run "TestDockerDriver*"
#go list ./... | grep -v '^github.com/hashicorp/nomad/vendor/' | \
# sudo \
# -E PATH=$TEMPDIR:$PATH \
# -E GOPATH=$GOPATH \
# xargs $GOBIN test -v ${GOTEST_FLAGS:--cover -timeout=900s}