delete_test.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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/pkg/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. CommonContainer: container.CommonContainer{
  26. ID: "test",
  27. State: state,
  28. Config: &containertypes.Config{},
  29. },
  30. }
  31. }
  32. // TestContainerDelete tests that a useful error message and instructions is
  33. // given when attempting to remove a container (#30842)
  34. func TestContainerDelete(t *testing.T) {
  35. tt := []struct {
  36. errMsg string
  37. fixMsg string
  38. initContainer func() *container.Container
  39. }{
  40. // a paused container
  41. {
  42. errMsg: "cannot remove a paused container",
  43. fixMsg: "Unpause and then stop the container before attempting removal or force remove",
  44. initContainer: func() *container.Container {
  45. return newContainerWithState(&container.State{Paused: true, Running: true})
  46. }},
  47. // a restarting container
  48. {
  49. errMsg: "cannot remove a restarting container",
  50. fixMsg: "Stop the container before attempting removal or force remove",
  51. initContainer: func() *container.Container {
  52. c := newContainerWithState(container.NewState())
  53. c.SetRunning(0, true)
  54. c.SetRestarting(&container.ExitStatus{})
  55. return c
  56. }},
  57. // a running container
  58. {
  59. errMsg: "cannot remove a running container",
  60. fixMsg: "Stop the container before attempting removal or force remove",
  61. initContainer: func() *container.Container {
  62. return newContainerWithState(&container.State{Running: true})
  63. }},
  64. }
  65. for _, te := range tt {
  66. c := te.initContainer()
  67. d, cleanup := newDaemonWithTmpRoot(t)
  68. defer cleanup()
  69. d.containers.Add(c.ID, c)
  70. err := d.ContainerRm(c.ID, &types.ContainerRmConfig{ForceRemove: false})
  71. testutil.ErrorContains(t, err, te.errMsg)
  72. testutil.ErrorContains(t, err, te.fixMsg)
  73. }
  74. }
  75. func TestContainerDoubleDelete(t *testing.T) {
  76. c := newContainerWithState(container.NewState())
  77. // Mark the container as having a delete in progress
  78. c.SetRemovalInProgress()
  79. d, cleanup := newDaemonWithTmpRoot(t)
  80. defer cleanup()
  81. d.containers.Add(c.ID, c)
  82. // Try to remove the container when its state is removalInProgress.
  83. // It should return an error indicating it is under removal progress.
  84. err := d.ContainerRm(c.ID, &types.ContainerRmConfig{ForceRemove: true})
  85. testutil.ErrorContains(t, err, fmt.Sprintf("removal of container %s is already in progress", c.ID))
  86. }