github.com/Heebron/moby@v0.0.0-20221111184709-6eab4f55faf7/testutil/environment/clean.go (about) 1 package environment // import "github.com/docker/docker/testutil/environment" 2 3 import ( 4 "context" 5 "regexp" 6 "strings" 7 "testing" 8 9 "github.com/docker/docker/api/types" 10 "github.com/docker/docker/api/types/filters" 11 "github.com/docker/docker/api/types/volume" 12 "github.com/docker/docker/client" 13 "github.com/docker/docker/errdefs" 14 "gotest.tools/v3/assert" 15 ) 16 17 // Clean the environment, preserving protected objects (images, containers, ...) 18 // and removing everything else. It's meant to run after any tests so that they don't 19 // depend on each others. 20 func (e *Execution) Clean(t testing.TB) { 21 t.Helper() 22 client := e.APIClient() 23 24 platform := e.OSType 25 if (platform != "windows") || (platform == "windows" && e.DaemonInfo.Isolation == "hyperv") { 26 unpauseAllContainers(t, client) 27 } 28 deleteAllContainers(t, client, e.protectedElements.containers) 29 deleteAllImages(t, client, e.protectedElements.images) 30 deleteAllVolumes(t, client, e.protectedElements.volumes) 31 deleteAllNetworks(t, client, platform, e.protectedElements.networks) 32 if platform == "linux" { 33 deleteAllPlugins(t, client, e.protectedElements.plugins) 34 } 35 } 36 37 func unpauseAllContainers(t testing.TB, client client.ContainerAPIClient) { 38 t.Helper() 39 ctx := context.Background() 40 containers := getPausedContainers(ctx, t, client) 41 if len(containers) > 0 { 42 for _, container := range containers { 43 err := client.ContainerUnpause(ctx, container.ID) 44 assert.Check(t, err, "failed to unpause container %s", container.ID) 45 } 46 } 47 } 48 49 func getPausedContainers(ctx context.Context, t testing.TB, client client.ContainerAPIClient) []types.Container { 50 t.Helper() 51 filter := filters.NewArgs() 52 filter.Add("status", "paused") 53 containers, err := client.ContainerList(ctx, types.ContainerListOptions{ 54 Filters: filter, 55 All: true, 56 }) 57 assert.Check(t, err, "failed to list containers") 58 return containers 59 } 60 61 var alreadyExists = regexp.MustCompile(`Error response from daemon: removal of container (\w+) is already in progress`) 62 63 func deleteAllContainers(t testing.TB, apiclient client.ContainerAPIClient, protectedContainers map[string]struct{}) { 64 t.Helper() 65 ctx := context.Background() 66 containers := getAllContainers(ctx, t, apiclient) 67 if len(containers) == 0 { 68 return 69 } 70 71 for _, container := range containers { 72 if _, ok := protectedContainers[container.ID]; ok { 73 continue 74 } 75 err := apiclient.ContainerRemove(ctx, container.ID, types.ContainerRemoveOptions{ 76 Force: true, 77 RemoveVolumes: true, 78 }) 79 if err == nil || client.IsErrNotFound(err) || alreadyExists.MatchString(err.Error()) || isErrNotFoundSwarmClassic(err) { 80 continue 81 } 82 assert.Check(t, err, "failed to remove %s", container.ID) 83 } 84 } 85 86 func getAllContainers(ctx context.Context, t testing.TB, client client.ContainerAPIClient) []types.Container { 87 t.Helper() 88 containers, err := client.ContainerList(ctx, types.ContainerListOptions{ 89 All: true, 90 }) 91 assert.Check(t, err, "failed to list containers") 92 return containers 93 } 94 95 func deleteAllImages(t testing.TB, apiclient client.ImageAPIClient, protectedImages map[string]struct{}) { 96 t.Helper() 97 images, err := apiclient.ImageList(context.Background(), types.ImageListOptions{}) 98 assert.Check(t, err, "failed to list images") 99 100 ctx := context.Background() 101 for _, image := range images { 102 tags := tagsFromImageSummary(image) 103 if len(tags) == 0 { 104 removeImage(ctx, t, apiclient, image.ID) 105 continue 106 } 107 for _, tag := range tags { 108 if _, ok := protectedImages[tag]; !ok { 109 removeImage(ctx, t, apiclient, tag) 110 } 111 } 112 } 113 } 114 115 func removeImage(ctx context.Context, t testing.TB, apiclient client.ImageAPIClient, ref string) { 116 t.Helper() 117 _, err := apiclient.ImageRemove(ctx, ref, types.ImageRemoveOptions{ 118 Force: true, 119 }) 120 if client.IsErrNotFound(err) { 121 return 122 } 123 assert.Check(t, err, "failed to remove image %s", ref) 124 } 125 126 func deleteAllVolumes(t testing.TB, c client.VolumeAPIClient, protectedVolumes map[string]struct{}) { 127 t.Helper() 128 volumes, err := c.VolumeList(context.Background(), volume.ListOptions{}) 129 assert.Check(t, err, "failed to list volumes") 130 131 for _, v := range volumes.Volumes { 132 if _, ok := protectedVolumes[v.Name]; ok { 133 continue 134 } 135 err := c.VolumeRemove(context.Background(), v.Name, true) 136 // Docker EE may list volumes that no longer exist. 137 if isErrNotFoundSwarmClassic(err) { 138 continue 139 } 140 assert.Check(t, err, "failed to remove volume %s", v.Name) 141 } 142 } 143 144 func deleteAllNetworks(t testing.TB, c client.NetworkAPIClient, daemonPlatform string, protectedNetworks map[string]struct{}) { 145 t.Helper() 146 networks, err := c.NetworkList(context.Background(), types.NetworkListOptions{}) 147 assert.Check(t, err, "failed to list networks") 148 149 for _, n := range networks { 150 if n.Name == "bridge" || n.Name == "none" || n.Name == "host" { 151 continue 152 } 153 if _, ok := protectedNetworks[n.ID]; ok { 154 continue 155 } 156 if daemonPlatform == "windows" && strings.ToLower(n.Name) == "nat" { 157 // nat is a pre-defined network on Windows and cannot be removed 158 continue 159 } 160 err := c.NetworkRemove(context.Background(), n.ID) 161 assert.Check(t, err, "failed to remove network %s", n.ID) 162 } 163 } 164 165 func deleteAllPlugins(t testing.TB, c client.PluginAPIClient, protectedPlugins map[string]struct{}) { 166 t.Helper() 167 plugins, err := c.PluginList(context.Background(), filters.Args{}) 168 // Docker EE does not allow cluster-wide plugin management. 169 if errdefs.IsNotImplemented(err) { 170 return 171 } 172 assert.Check(t, err, "failed to list plugins") 173 174 for _, p := range plugins { 175 if _, ok := protectedPlugins[p.Name]; ok { 176 continue 177 } 178 err := c.PluginRemove(context.Background(), p.Name, types.PluginRemoveOptions{Force: true}) 179 assert.Check(t, err, "failed to remove plugin %s", p.ID) 180 } 181 } 182 183 // Swarm classic aggregates node errors and returns a 500 so we need to check 184 // the error string instead of just IsErrNotFound(). 185 func isErrNotFoundSwarmClassic(err error) bool { 186 return err != nil && strings.Contains(strings.ToLower(err.Error()), "no such") 187 }