delete_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package daemon
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "testing"
  7. "github.com/docker/docker/api/types"
  8. containertypes "github.com/docker/docker/api/types/container"
  9. "github.com/docker/docker/container"
  10. "github.com/docker/docker/internal/testutil"
  11. "github.com/stretchr/testify/require"
  12. )
  13. func newDaemonWithTmpRoot(t *testing.T) (*Daemon, func()) {
  14. tmp, err := ioutil.TempDir("", "docker-daemon-unix-test-")
  15. require.NoError(t, err)
  16. d := &Daemon{
  17. repository: tmp,
  18. root: tmp,
  19. }
  20. d.containers = container.NewMemoryStore()
  21. return d, func() { os.RemoveAll(tmp) }
  22. }
  23. func newContainerWithState(state *container.State) *container.Container {
  24. return &container.Container{
  25. ID: "test",
  26. State: state,
  27. Config: &containertypes.Config{},
  28. }
  29. }
  30. // TestContainerDelete tests that a useful error message and instructions is
  31. // given when attempting to remove a container (#30842)
  32. func TestContainerDelete(t *testing.T) {
  33. tt := []struct {
  34. errMsg string
  35. fixMsg string
  36. initContainer func() *container.Container
  37. }{
  38. // a paused container
  39. {
  40. errMsg: "cannot remove a paused container",
  41. fixMsg: "Unpause and then stop the container before attempting removal or force remove",
  42. initContainer: func() *container.Container {
  43. return newContainerWithState(&container.State{Paused: true, Running: true})
  44. }},
  45. // a restarting container
  46. {
  47. errMsg: "cannot remove a restarting container",
  48. fixMsg: "Stop the container before attempting removal or force remove",
  49. initContainer: func() *container.Container {
  50. c := newContainerWithState(container.NewState())
  51. c.SetRunning(0, true)
  52. c.SetRestarting(&container.ExitStatus{})
  53. return c
  54. }},
  55. // a running container
  56. {
  57. errMsg: "cannot remove a running container",
  58. fixMsg: "Stop the container before attempting removal or force remove",
  59. initContainer: func() *container.Container {
  60. return newContainerWithState(&container.State{Running: true})
  61. }},
  62. }
  63. for _, te := range tt {
  64. c := te.initContainer()
  65. d, cleanup := newDaemonWithTmpRoot(t)
  66. defer cleanup()
  67. d.containers.Add(c.ID, c)
  68. err := d.ContainerRm(c.ID, &types.ContainerRmConfig{ForceRemove: false})
  69. testutil.ErrorContains(t, err, te.errMsg)
  70. testutil.ErrorContains(t, err, te.fixMsg)
  71. }
  72. }
  73. func TestContainerDoubleDelete(t *testing.T) {
  74. c := newContainerWithState(container.NewState())
  75. // Mark the container as having a delete in progress
  76. c.SetRemovalInProgress()
  77. d, cleanup := newDaemonWithTmpRoot(t)
  78. defer cleanup()
  79. d.containers.Add(c.ID, c)
  80. // Try to remove the container when its state is removalInProgress.
  81. // It should return an error indicating it is under removal progress.
  82. err := d.ContainerRm(c.ID, &types.ContainerRmConfig{ForceRemove: true})
  83. testutil.ErrorContains(t, err, fmt.Sprintf("removal of container %s is already in progress", c.ID))
  84. }