Skip to content

Commit

Permalink
updated docker driver test to fix some tests by removing docker
Browse files Browse the repository at this point in the history
images/contaienrs before test
  • Loading branch information
Chris Baker committed May 30, 2019
1 parent d00ed5c commit 45a3408
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions drivers/docker/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,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 @@ -1428,8 +1453,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 +1518,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

0 comments on commit 45a3408

Please sign in to comment.