clean.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package environment
  2. import (
  3. "regexp"
  4. "strings"
  5. "github.com/docker/docker/api/types"
  6. "github.com/docker/docker/api/types/filters"
  7. "github.com/docker/docker/client"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. "golang.org/x/net/context"
  11. )
  12. type testingT interface {
  13. require.TestingT
  14. logT
  15. Fatalf(string, ...interface{})
  16. }
  17. type logT interface {
  18. Logf(string, ...interface{})
  19. }
  20. // Clean the environment, preserving protected objects (images, containers, ...)
  21. // and removing everything else. It's meant to run after any tests so that they don't
  22. // depend on each others.
  23. func (e *Execution) Clean(t testingT) {
  24. client := e.APIClient()
  25. platform := e.DaemonInfo.OSType
  26. if (platform != "windows") || (platform == "windows" && e.DaemonInfo.Isolation == "hyperv") {
  27. unpauseAllContainers(t, client)
  28. }
  29. deleteAllContainers(t, client)
  30. deleteAllImages(t, client, e.protectedElements.images)
  31. deleteAllVolumes(t, client)
  32. deleteAllNetworks(t, client, platform)
  33. if platform == "linux" {
  34. deleteAllPlugins(t, client)
  35. }
  36. }
  37. func unpauseAllContainers(t assert.TestingT, client client.ContainerAPIClient) {
  38. ctx := context.Background()
  39. containers := getPausedContainers(ctx, t, client)
  40. if len(containers) > 0 {
  41. for _, container := range containers {
  42. err := client.ContainerUnpause(ctx, container.ID)
  43. assert.NoError(t, err, "failed to unpause container %s", container.ID)
  44. }
  45. }
  46. }
  47. func getPausedContainers(ctx context.Context, t assert.TestingT, client client.ContainerAPIClient) []types.Container {
  48. filter := filters.NewArgs()
  49. filter.Add("status", "paused")
  50. containers, err := client.ContainerList(ctx, types.ContainerListOptions{
  51. Filters: filter,
  52. Quiet: true,
  53. All: true,
  54. })
  55. assert.NoError(t, err, "failed to list containers")
  56. return containers
  57. }
  58. var alreadyExists = regexp.MustCompile(`Error response from daemon: removal of container (\w+) is already in progress`)
  59. func deleteAllContainers(t assert.TestingT, apiclient client.ContainerAPIClient) {
  60. ctx := context.Background()
  61. containers := getAllContainers(ctx, t, apiclient)
  62. if len(containers) == 0 {
  63. return
  64. }
  65. for _, container := range containers {
  66. err := apiclient.ContainerRemove(ctx, container.ID, types.ContainerRemoveOptions{
  67. Force: true,
  68. RemoveVolumes: true,
  69. })
  70. if err == nil || client.IsErrNotFound(err) || alreadyExists.MatchString(err.Error()) {
  71. continue
  72. }
  73. assert.NoError(t, err, "failed to remove %s", container.ID)
  74. }
  75. }
  76. func getAllContainers(ctx context.Context, t assert.TestingT, client client.ContainerAPIClient) []types.Container {
  77. containers, err := client.ContainerList(ctx, types.ContainerListOptions{
  78. Quiet: true,
  79. All: true,
  80. })
  81. assert.NoError(t, err, "failed to list containers")
  82. return containers
  83. }
  84. func deleteAllImages(t testingT, apiclient client.ImageAPIClient, protectedImages map[string]struct{}) {
  85. images, err := apiclient.ImageList(context.Background(), types.ImageListOptions{})
  86. assert.NoError(t, err, "failed to list images")
  87. ctx := context.Background()
  88. for _, image := range images {
  89. tags := tagsFromImageSummary(image)
  90. if len(tags) == 0 {
  91. t.Logf("Removing image %s", image.ID)
  92. removeImage(ctx, t, apiclient, image.ID)
  93. continue
  94. }
  95. for _, tag := range tags {
  96. if _, ok := protectedImages[tag]; !ok {
  97. t.Logf("Removing image %s", tag)
  98. removeImage(ctx, t, apiclient, tag)
  99. continue
  100. }
  101. }
  102. }
  103. }
  104. func removeImage(ctx context.Context, t assert.TestingT, apiclient client.ImageAPIClient, ref string) {
  105. _, err := apiclient.ImageRemove(ctx, ref, types.ImageRemoveOptions{
  106. Force: true,
  107. })
  108. if client.IsErrNotFound(err) {
  109. return
  110. }
  111. assert.NoError(t, err, "failed to remove image %s", ref)
  112. }
  113. func deleteAllVolumes(t assert.TestingT, c client.VolumeAPIClient) {
  114. volumes, err := c.VolumeList(context.Background(), filters.Args{})
  115. assert.NoError(t, err, "failed to list volumes")
  116. for _, v := range volumes.Volumes {
  117. err := c.VolumeRemove(context.Background(), v.Name, true)
  118. assert.NoError(t, err, "failed to remove volume %s", v.Name)
  119. }
  120. }
  121. func deleteAllNetworks(t assert.TestingT, c client.NetworkAPIClient, daemonPlatform string) {
  122. networks, err := c.NetworkList(context.Background(), types.NetworkListOptions{})
  123. assert.NoError(t, err, "failed to list networks")
  124. for _, n := range networks {
  125. if n.Name == "bridge" || n.Name == "none" || n.Name == "host" {
  126. continue
  127. }
  128. if daemonPlatform == "windows" && strings.ToLower(n.Name) == "nat" {
  129. // nat is a pre-defined network on Windows and cannot be removed
  130. continue
  131. }
  132. err := c.NetworkRemove(context.Background(), n.ID)
  133. assert.NoError(t, err, "failed to remove network %s", n.ID)
  134. }
  135. }
  136. func deleteAllPlugins(t assert.TestingT, c client.PluginAPIClient) {
  137. plugins, err := c.PluginList(context.Background(), filters.Args{})
  138. assert.NoError(t, err, "failed to list plugins")
  139. for _, p := range plugins {
  140. err := c.PluginRemove(context.Background(), p.Name, types.PluginRemoveOptions{Force: true})
  141. assert.NoError(t, err, "failed to remove plugin %s", p.ID)
  142. }
  143. }