Skip to content

Commit

Permalink
docker/tests:
Browse files Browse the repository at this point in the history
- modified tests to cleanup now that RemoveContainer isn't in StartTask
- fix some broken tests by removing docker images/containers before test
  • Loading branch information
Chris Baker committed Jun 3, 2019
1 parent 3a96683 commit 3b82770
Showing 1 changed file with 52 additions and 15 deletions.
67 changes: 52 additions & 15 deletions drivers/docker/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,12 @@ func dockerTask(t *testing.T) (*drivers.TaskConfig, *TaskConfig, []int) {

cfg := newTaskConfig("", busyboxLongRunningCmd)
task := &drivers.TaskConfig{
ID: uuid.Generate(),
Name: "redis-demo",
AllocID: uuid.Generate(),
ID: uuid.Generate(),
Name: "redis-demo",
AllocID: uuid.Generate(),
Env: map[string]string{
"test": t.Name(),
},
DeviceEnv: make(map[string]string),
Resources: &drivers.Resources{
NomadResources: &structs.AllocatedTaskResources{
Expand Down Expand Up @@ -140,6 +143,31 @@ func dockerSetup(t *testing.T, task *drivers.TaskConfig) (*docker.Client, *dtest
}
}

// cleanSlate removes the specified docker image, including potentially stopping/removing any
// containers based on that image. This is used to decouple tests that would be coupled
// by using the same container image.
func cleanSlate(client *docker.Client, imageID string) {
if img, _ := client.InspectImage(imageID); img == nil {
return
}
containers, _ := client.ListContainers(docker.ListContainersOptions{
All: true,
Filters: map[string][]string{
"ancestor": {imageID},
},
})
for _, c := range containers {
client.RemoveContainer(docker.RemoveContainerOptions{
Force: true,
ID: c.ID,
})
}
client.RemoveImageExtended(imageID, docker.RemoveImageOptions{
Force: true,
})
return
}

// dockerDriverHarness wires up everything needed to launch a task with a docker driver.
// A driver plugin interface and cleanup function is returned
func dockerDriverHarness(t *testing.T, cfg map[string]interface{}) *dtestutil.DriverHarness {
Expand Down Expand Up @@ -441,6 +469,7 @@ func TestDockerDriver_Start_StoppedContainer(t *testing.T) {
Config: &docker.Config{
Image: taskCfg.Image,
Cmd: []string{"sleep", "9000"},
Env: []string{fmt.Sprintf("test=%s", t.Name())},
},
}

Expand All @@ -449,11 +478,11 @@ func TestDockerDriver_Start_StoppedContainer(t *testing.T) {
}

_, _, err = d.StartTask(task)
require.NoError(t, err)

defer d.DestroyTask(task.ID, true)
require.NoError(t, err)

require.NoError(t, d.WaitUntilStarted(task.ID, 5*time.Second))
require.NoError(t, d.DestroyTask(task.ID, true))
}

func TestDockerDriver_Start_LoadImage(t *testing.T) {
Expand Down Expand Up @@ -762,9 +791,12 @@ func TestDockerDriver_StartN(t *testing.T) {
_, _, err := d.StartTask(task)
require.NoError(err)

defer d.DestroyTask(task.ID, true)
}

defer d.DestroyTask(task3.ID, true)
defer d.DestroyTask(task2.ID, true)
defer d.DestroyTask(task1.ID, true)

t.Log("All tasks are started. Terminating...")
for _, task := range taskList {
require.NoError(d.StopTask(task.ID, time.Second, "SIGINT"))
Expand Down Expand Up @@ -826,11 +858,13 @@ func TestDockerDriver_StartNVersions(t *testing.T) {
_, _, err := d.StartTask(task)
require.NoError(err)

defer d.DestroyTask(task.ID, true)

require.NoError(d.WaitUntilStarted(task.ID, 5*time.Second))
}

defer d.DestroyTask(task3.ID, true)
defer d.DestroyTask(task2.ID, true)
defer d.DestroyTask(task1.ID, true)

t.Log("All tasks are started. Terminating...")
for _, task := range taskList {
require.NoError(d.StopTask(task.ID, time.Second, "SIGINT"))
Expand All @@ -845,6 +879,7 @@ func TestDockerDriver_StartNVersions(t *testing.T) {
require.Fail("timeout waiting on task")
}
}

t.Log("Test complete!")
}

Expand Down Expand Up @@ -1178,6 +1213,7 @@ func TestDockerDriver_Capabilities(t *testing.T) {
copyImage(t, task.TaskDir(), "busybox.tar")

_, _, err := d.StartTask(task)
defer d.DestroyTask(task.ID, true)
if err == nil && tc.StartError != "" {
t.Fatalf("Expected error in start: %v", tc.StartError)
} else if err != nil {
Expand All @@ -1189,7 +1225,6 @@ func TestDockerDriver_Capabilities(t *testing.T) {
return
}

defer d.DestroyTask(task.ID, true)
handle, ok := dockerDriver.tasks.Get(task.ID)
require.True(t, ok)

Expand Down Expand Up @@ -1428,8 +1463,7 @@ func TestDockerDriver_EnableImageGC(t *testing.T) {
cleanup := driver.MkAllocDir(task, true)
defer cleanup()

// remove the image before the test
client.RemoveImage(cfg.Image)
cleanSlate(client, cfg.Image)

copyImage(t, task.TaskDir(), "busybox.tar")
_, _, err := driver.StartTask(task)
Expand Down Expand Up @@ -1494,8 +1528,7 @@ func TestDockerDriver_DisableImageGC(t *testing.T) {
cleanup := driver.MkAllocDir(task, true)
defer cleanup()

// remove the image before the test
client.RemoveImage(cfg.Image)
cleanSlate(client, cfg.Image)

copyImage(t, task.TaskDir(), "busybox.tar")
_, _, err := driver.StartTask(task)
Expand Down Expand Up @@ -1635,7 +1668,9 @@ func TestDockerDriver_VolumesDisabled(t *testing.T) {
task, driver, _, _, cleanup := setupDockerVolumes(t, cfg, tmpvol)
defer cleanup()

if _, _, err := driver.StartTask(task); err == nil {
_, _, err = driver.StartTask(task)
defer driver.DestroyTask(task.ID, true)
if err == nil {
require.Fail(t, "Started driver successfully when volumes should have been disabled.")
}
}
Expand Down Expand Up @@ -1673,7 +1708,9 @@ func TestDockerDriver_VolumesDisabled(t *testing.T) {
taskCfg.VolumeDriver = "flocker"
require.NoError(t, task.EncodeConcreteDriverConfig(taskCfg))

if _, _, err := driver.StartTask(task); err == nil {
_, _, err := driver.StartTask(task)
defer driver.DestroyTask(task.ID, true)
if err == nil {
require.Fail(t, "Started driver successfully when volume drivers should have been disabled.")
}
}
Expand Down

0 comments on commit 3b82770

Please sign in to comment.