delete_test.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package daemon // import "github.com/docker/docker/daemon"
  2. import (
  3. "fmt"
  4. "os"
  5. "testing"
  6. "github.com/docker/docker/api/types/backend"
  7. containertypes "github.com/docker/docker/api/types/container"
  8. "github.com/docker/docker/container"
  9. "github.com/docker/docker/errdefs"
  10. "gotest.tools/v3/assert"
  11. is "gotest.tools/v3/assert/cmp"
  12. )
  13. func newDaemonWithTmpRoot(t *testing.T) (*Daemon, func()) {
  14. tmp, err := os.MkdirTemp("", "docker-daemon-unix-test-")
  15. assert.NilError(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. tests := []struct {
  34. doc string
  35. errMsg string
  36. initContainer func() *container.Container
  37. }{
  38. {
  39. doc: "paused container",
  40. errMsg: "container is paused and must be unpaused first",
  41. initContainer: func() *container.Container {
  42. return newContainerWithState(&container.State{Paused: true, Running: true})
  43. },
  44. },
  45. {
  46. doc: "restarting container",
  47. errMsg: "container is restarting: stop the container before removing or force remove",
  48. initContainer: func() *container.Container {
  49. c := newContainerWithState(container.NewState())
  50. c.SetRunning(nil, nil, true)
  51. c.SetRestarting(&container.ExitStatus{})
  52. return c
  53. },
  54. },
  55. {
  56. doc: "running container",
  57. errMsg: "container is running: stop the container before removing or force remove",
  58. initContainer: func() *container.Container {
  59. return newContainerWithState(&container.State{Running: true})
  60. },
  61. },
  62. }
  63. for _, tc := range tests {
  64. tc := tc
  65. t.Run(tc.doc, func(t *testing.T) {
  66. c := tc.initContainer()
  67. d, cleanup := newDaemonWithTmpRoot(t)
  68. defer cleanup()
  69. d.containers.Add(c.ID, c)
  70. err := d.ContainerRm(c.ID, &backend.ContainerRmConfig{ForceRemove: false})
  71. assert.Check(t, is.ErrorType(err, errdefs.IsConflict))
  72. assert.Check(t, is.ErrorContains(err, tc.errMsg))
  73. })
  74. }
  75. }
  76. func TestContainerDoubleDelete(t *testing.T) {
  77. c := newContainerWithState(container.NewState())
  78. // Mark the container as having a delete in progress
  79. c.SetRemovalInProgress()
  80. d, cleanup := newDaemonWithTmpRoot(t)
  81. defer cleanup()
  82. d.containers.Add(c.ID, c)
  83. // Try to remove the container when its state is removalInProgress.
  84. // It should return an error indicating it is under removal progress.
  85. err := d.ContainerRm(c.ID, &backend.ContainerRmConfig{ForceRemove: true})
  86. assert.Check(t, is.ErrorContains(err, fmt.Sprintf("removal of container %s is already in progress", c.ID)))
  87. }