2019-08-29 16:52:40 -04:00
|
|
|
package environment // import "github.com/docker/docker/testutil/environment"
|
2017-08-25 18:48:36 -04:00
|
|
|
|
|
|
|
import (
|
2018-04-19 18:30:59 -04:00
|
|
|
"context"
|
2017-08-25 18:48:36 -04:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2019-09-23 08:23:01 -04:00
|
|
|
"testing"
|
2017-08-25 18:48:36 -04:00
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
|
|
|
"github.com/docker/docker/client"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
2017-08-25 18:48:36 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Clean the environment, preserving protected objects (images, containers, ...)
|
|
|
|
// and removing everything else. It's meant to run after any tests so that they don't
|
|
|
|
// depend on each others.
|
2019-09-23 08:23:01 -04:00
|
|
|
func (e *Execution) Clean(t testing.TB) {
|
|
|
|
t.Helper()
|
2017-08-25 18:48:36 -04:00
|
|
|
client := e.APIClient()
|
|
|
|
|
2017-09-20 08:47:49 -04:00
|
|
|
platform := e.OSType
|
2017-08-25 18:48:36 -04:00
|
|
|
if (platform != "windows") || (platform == "windows" && e.DaemonInfo.Isolation == "hyperv") {
|
|
|
|
unpauseAllContainers(t, client)
|
|
|
|
}
|
2017-09-08 10:41:02 -04:00
|
|
|
deleteAllContainers(t, client, e.protectedElements.containers)
|
2017-08-25 18:48:36 -04:00
|
|
|
deleteAllImages(t, client, e.protectedElements.images)
|
2017-09-08 10:41:02 -04:00
|
|
|
deleteAllVolumes(t, client, e.protectedElements.volumes)
|
|
|
|
deleteAllNetworks(t, client, platform, e.protectedElements.networks)
|
2017-08-25 18:48:36 -04:00
|
|
|
if platform == "linux" {
|
2017-09-08 10:41:02 -04:00
|
|
|
deleteAllPlugins(t, client, e.protectedElements.plugins)
|
2017-08-25 18:48:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-23 08:23:01 -04:00
|
|
|
func unpauseAllContainers(t testing.TB, client client.ContainerAPIClient) {
|
|
|
|
t.Helper()
|
2017-08-25 18:48:36 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
containers := getPausedContainers(ctx, t, client)
|
|
|
|
if len(containers) > 0 {
|
|
|
|
for _, container := range containers {
|
|
|
|
err := client.ContainerUnpause(ctx, container.ID)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, err, "failed to unpause container %s", container.ID)
|
2017-08-25 18:48:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-23 08:23:01 -04:00
|
|
|
func getPausedContainers(ctx context.Context, t testing.TB, client client.ContainerAPIClient) []types.Container {
|
|
|
|
t.Helper()
|
2017-08-25 18:48:36 -04:00
|
|
|
filter := filters.NewArgs()
|
|
|
|
filter.Add("status", "paused")
|
|
|
|
containers, err := client.ContainerList(ctx, types.ContainerListOptions{
|
|
|
|
Filters: filter,
|
|
|
|
Quiet: true,
|
|
|
|
All: true,
|
|
|
|
})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, err, "failed to list containers")
|
2017-08-25 18:48:36 -04:00
|
|
|
return containers
|
|
|
|
}
|
|
|
|
|
|
|
|
var alreadyExists = regexp.MustCompile(`Error response from daemon: removal of container (\w+) is already in progress`)
|
|
|
|
|
2019-09-23 08:23:01 -04:00
|
|
|
func deleteAllContainers(t testing.TB, apiclient client.ContainerAPIClient, protectedContainers map[string]struct{}) {
|
|
|
|
t.Helper()
|
2017-08-25 18:48:36 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
containers := getAllContainers(ctx, t, apiclient)
|
|
|
|
if len(containers) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, container := range containers {
|
2017-09-08 10:41:02 -04:00
|
|
|
if _, ok := protectedContainers[container.ID]; ok {
|
|
|
|
continue
|
|
|
|
}
|
2017-08-25 18:48:36 -04:00
|
|
|
err := apiclient.ContainerRemove(ctx, container.ID, types.ContainerRemoveOptions{
|
|
|
|
Force: true,
|
|
|
|
RemoveVolumes: true,
|
|
|
|
})
|
2017-09-25 08:09:17 -04:00
|
|
|
if err == nil || client.IsErrNotFound(err) || alreadyExists.MatchString(err.Error()) || isErrNotFoundSwarmClassic(err) {
|
2017-08-25 18:48:36 -04:00
|
|
|
continue
|
|
|
|
}
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, err, "failed to remove %s", container.ID)
|
2017-08-25 18:48:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-23 08:23:01 -04:00
|
|
|
func getAllContainers(ctx context.Context, t testing.TB, client client.ContainerAPIClient) []types.Container {
|
|
|
|
t.Helper()
|
2017-08-25 18:48:36 -04:00
|
|
|
containers, err := client.ContainerList(ctx, types.ContainerListOptions{
|
|
|
|
Quiet: true,
|
|
|
|
All: true,
|
|
|
|
})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, err, "failed to list containers")
|
2017-08-25 18:48:36 -04:00
|
|
|
return containers
|
|
|
|
}
|
|
|
|
|
2019-09-23 08:23:01 -04:00
|
|
|
func deleteAllImages(t testing.TB, apiclient client.ImageAPIClient, protectedImages map[string]struct{}) {
|
|
|
|
t.Helper()
|
2017-08-25 18:48:36 -04:00
|
|
|
images, err := apiclient.ImageList(context.Background(), types.ImageListOptions{})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, err, "failed to list images")
|
2017-08-25 18:48:36 -04:00
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
for _, image := range images {
|
|
|
|
tags := tagsFromImageSummary(image)
|
|
|
|
if len(tags) == 0 {
|
|
|
|
removeImage(ctx, t, apiclient, image.ID)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, tag := range tags {
|
|
|
|
if _, ok := protectedImages[tag]; !ok {
|
|
|
|
removeImage(ctx, t, apiclient, tag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-23 08:23:01 -04:00
|
|
|
func removeImage(ctx context.Context, t testing.TB, apiclient client.ImageAPIClient, ref string) {
|
|
|
|
t.Helper()
|
2017-08-25 18:48:36 -04:00
|
|
|
_, err := apiclient.ImageRemove(ctx, ref, types.ImageRemoveOptions{
|
|
|
|
Force: true,
|
|
|
|
})
|
|
|
|
if client.IsErrNotFound(err) {
|
|
|
|
return
|
|
|
|
}
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, err, "failed to remove image %s", ref)
|
2017-08-25 18:48:36 -04:00
|
|
|
}
|
|
|
|
|
2019-09-23 08:23:01 -04:00
|
|
|
func deleteAllVolumes(t testing.TB, c client.VolumeAPIClient, protectedVolumes map[string]struct{}) {
|
|
|
|
t.Helper()
|
2017-08-25 18:48:36 -04:00
|
|
|
volumes, err := c.VolumeList(context.Background(), filters.Args{})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, err, "failed to list volumes")
|
2017-08-25 18:48:36 -04:00
|
|
|
|
|
|
|
for _, v := range volumes.Volumes {
|
2017-09-08 10:41:02 -04:00
|
|
|
if _, ok := protectedVolumes[v.Name]; ok {
|
|
|
|
continue
|
|
|
|
}
|
2017-08-25 18:48:36 -04:00
|
|
|
err := c.VolumeRemove(context.Background(), v.Name, true)
|
2017-09-25 08:09:17 -04:00
|
|
|
// Docker EE may list volumes that no longer exist.
|
|
|
|
if isErrNotFoundSwarmClassic(err) {
|
|
|
|
continue
|
|
|
|
}
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, err, "failed to remove volume %s", v.Name)
|
2017-08-25 18:48:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-23 08:23:01 -04:00
|
|
|
func deleteAllNetworks(t testing.TB, c client.NetworkAPIClient, daemonPlatform string, protectedNetworks map[string]struct{}) {
|
|
|
|
t.Helper()
|
2017-08-25 18:48:36 -04:00
|
|
|
networks, err := c.NetworkList(context.Background(), types.NetworkListOptions{})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, err, "failed to list networks")
|
2017-08-25 18:48:36 -04:00
|
|
|
|
|
|
|
for _, n := range networks {
|
|
|
|
if n.Name == "bridge" || n.Name == "none" || n.Name == "host" {
|
|
|
|
continue
|
|
|
|
}
|
2017-09-08 10:41:02 -04:00
|
|
|
if _, ok := protectedNetworks[n.ID]; ok {
|
|
|
|
continue
|
|
|
|
}
|
2017-08-25 18:48:36 -04:00
|
|
|
if daemonPlatform == "windows" && strings.ToLower(n.Name) == "nat" {
|
|
|
|
// nat is a pre-defined network on Windows and cannot be removed
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err := c.NetworkRemove(context.Background(), n.ID)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, err, "failed to remove network %s", n.ID)
|
2017-08-25 18:48:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-23 08:23:01 -04:00
|
|
|
func deleteAllPlugins(t testing.TB, c client.PluginAPIClient, protectedPlugins map[string]struct{}) {
|
|
|
|
t.Helper()
|
2017-08-25 18:48:36 -04:00
|
|
|
plugins, err := c.PluginList(context.Background(), filters.Args{})
|
2017-09-25 08:09:17 -04:00
|
|
|
// Docker EE does not allow cluster-wide plugin management.
|
|
|
|
if client.IsErrNotImplemented(err) {
|
|
|
|
return
|
|
|
|
}
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, err, "failed to list plugins")
|
2017-08-25 18:48:36 -04:00
|
|
|
|
|
|
|
for _, p := range plugins {
|
2017-09-08 10:41:02 -04:00
|
|
|
if _, ok := protectedPlugins[p.Name]; ok {
|
|
|
|
continue
|
|
|
|
}
|
2017-08-25 18:48:36 -04:00
|
|
|
err := c.PluginRemove(context.Background(), p.Name, types.PluginRemoveOptions{Force: true})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, err, "failed to remove plugin %s", p.ID)
|
2017-08-25 18:48:36 -04:00
|
|
|
}
|
|
|
|
}
|
2017-09-25 08:09:17 -04:00
|
|
|
|
|
|
|
// Swarm classic aggregates node errors and returns a 500 so we need to check
|
|
|
|
// the error string instead of just IsErrNotFound().
|
|
|
|
func isErrNotFoundSwarmClassic(err error) bool {
|
|
|
|
return err != nil && strings.Contains(strings.ToLower(err.Error()), "no such")
|
|
|
|
}
|