remove_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package container // import "github.com/docker/docker/integration/container"
  2. import (
  3. "context"
  4. "os"
  5. "testing"
  6. "time"
  7. "github.com/docker/docker/api/types"
  8. "github.com/docker/docker/api/types/filters"
  9. "github.com/docker/docker/integration/internal/container"
  10. "github.com/docker/docker/integration/internal/request"
  11. "github.com/docker/docker/internal/testutil"
  12. "github.com/gotestyourself/gotestyourself/fs"
  13. "github.com/gotestyourself/gotestyourself/poll"
  14. "github.com/gotestyourself/gotestyourself/skip"
  15. "github.com/stretchr/testify/assert"
  16. "github.com/stretchr/testify/require"
  17. )
  18. func getPrefixAndSlashFromDaemonPlatform() (prefix, slash string) {
  19. if testEnv.OSType == "windows" {
  20. return "c:", `\`
  21. }
  22. return "", "/"
  23. }
  24. // Test case for #5244: `docker rm` fails if bind dir doesn't exist anymore
  25. func TestRemoveContainerWithRemovedVolume(t *testing.T) {
  26. skip.If(t, testEnv.IsRemoteDaemon())
  27. defer setupTest(t)()
  28. ctx := context.Background()
  29. client := request.NewAPIClient(t)
  30. prefix, slash := getPrefixAndSlashFromDaemonPlatform()
  31. tempDir := fs.NewDir(t, "test-rm-container-with-removed-volume", fs.WithMode(0755))
  32. defer tempDir.Remove()
  33. cID := container.Run(t, ctx, client, container.WithCmd("true"), container.WithBind(tempDir.Path(), prefix+slash+"test"))
  34. poll.WaitOn(t, container.IsInState(ctx, client, cID, "exited"), poll.WithDelay(100*time.Millisecond))
  35. err := os.RemoveAll(tempDir.Path())
  36. require.NoError(t, err)
  37. err = client.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{
  38. RemoveVolumes: true,
  39. })
  40. require.NoError(t, err)
  41. _, _, err = client.ContainerInspectWithRaw(ctx, cID, true)
  42. testutil.ErrorContains(t, err, "No such container")
  43. }
  44. // Test case for #2099/#2125
  45. func TestRemoveContainerWithVolume(t *testing.T) {
  46. defer setupTest(t)()
  47. ctx := context.Background()
  48. client := request.NewAPIClient(t)
  49. prefix, slash := getPrefixAndSlashFromDaemonPlatform()
  50. cID := container.Run(t, ctx, client, container.WithCmd("true"), container.WithVolume(prefix+slash+"srv"))
  51. poll.WaitOn(t, container.IsInState(ctx, client, cID, "exited"), poll.WithDelay(100*time.Millisecond))
  52. insp, _, err := client.ContainerInspectWithRaw(ctx, cID, true)
  53. require.NoError(t, err)
  54. assert.Equal(t, len(insp.Mounts), 1)
  55. volName := insp.Mounts[0].Name
  56. err = client.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{
  57. RemoveVolumes: true,
  58. })
  59. require.NoError(t, err)
  60. volumes, err := client.VolumeList(ctx, filters.NewArgs(filters.Arg("name", volName)))
  61. require.NoError(t, err)
  62. assert.Equal(t, len(volumes.Volumes), 0)
  63. }
  64. func TestRemoveContainerRunning(t *testing.T) {
  65. defer setupTest(t)()
  66. ctx := context.Background()
  67. client := request.NewAPIClient(t)
  68. cID := container.Run(t, ctx, client)
  69. err := client.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{})
  70. testutil.ErrorContains(t, err, "cannot remove a running container")
  71. }
  72. func TestRemoveContainerForceRemoveRunning(t *testing.T) {
  73. defer setupTest(t)()
  74. ctx := context.Background()
  75. client := request.NewAPIClient(t)
  76. cID := container.Run(t, ctx, client)
  77. err := client.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{
  78. Force: true,
  79. })
  80. require.NoError(t, err)
  81. }
  82. func TestRemoveInvalidContainer(t *testing.T) {
  83. defer setupTest(t)()
  84. ctx := context.Background()
  85. client := request.NewAPIClient(t)
  86. err := client.ContainerRemove(ctx, "unknown", types.ContainerRemoveOptions{})
  87. testutil.ErrorContains(t, err, "No such container")
  88. }