diff --git a/client/driver/docker_test.go b/client/driver/docker_test.go index c60590364..c4db2bf61 100644 --- a/client/driver/docker_test.go +++ b/client/driver/docker_test.go @@ -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) +} diff --git a/client/driver/test-resources/docker/busybox_glibc.tar b/client/driver/test-resources/docker/busybox_glibc.tar new file mode 100644 index 000000000..c68fb88ba --- /dev/null +++ b/client/driver/test-resources/docker/busybox_glibc.tar @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:07fa0ca9532f2c6d1ed12248c157a5bf17a07e4be99dd9a10442fc4e21e80a37 +size 4404736 diff --git a/client/driver/test-resources/docker/busybox_musl.tar b/client/driver/test-resources/docker/busybox_musl.tar new file mode 100644 index 000000000..8f5e53c76 --- /dev/null +++ b/client/driver/test-resources/docker/busybox_musl.tar @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:40a2768386bd2d1b0d7d735f4a3728c1aa130f0a09dca3c45e33e7ff6ef433ba +size 1418752 diff --git a/scripts/test.sh b/scripts/test.sh index b9cde8b9a..d19c5c2b4 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -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}