diff --git a/daemon/delete_test.go b/daemon/delete_test.go index 3113d77aee..4664cd9869 100644 --- a/daemon/delete_test.go +++ b/daemon/delete_test.go @@ -24,77 +24,65 @@ func newDaemonWithTmpRoot(t *testing.T) (*Daemon, func()) { return d, func() { os.RemoveAll(tmp) } } -// TestContainerDeletePaused tests that a useful error message and instructions is given when attempting -// to remove a paused container (#30842) -func TestContainerDeletePaused(t *testing.T) { - c := &container.Container{ +func newContainerWithState(state *container.State) *container.Container { + return &container.Container{ CommonContainer: container.CommonContainer{ ID: "test", - State: &container.State{Paused: true, Running: true}, + State: state, Config: &containertypes.Config{}, }, } - d, cleanup := newDaemonWithTmpRoot(t) - defer cleanup() - d.containers.Add(c.ID, c) - - err := d.ContainerRm(c.ID, &types.ContainerRmConfig{ForceRemove: false}) - - testutil.ErrorContains(t, err, "cannot remove a paused container") - testutil.ErrorContains(t, err, "Unpause and then stop the container before attempting removal or force remove") } -// TestContainerDeleteRestarting tests that a useful error message and instructions is given when attempting -// to remove a container that is restarting (#30842) -func TestContainerDeleteRestarting(t *testing.T) { - c := &container.Container{ - CommonContainer: container.CommonContainer{ - ID: "test", - State: container.NewState(), - Config: &containertypes.Config{}, - }, +// TestContainerDelete tests that a useful error message and instructions is +// given when attempting to remove a container (#30842) +func TestContainerDelete(t *testing.T) { + tt := []struct { + errMsg string + fixMsg string + initContainer func() *container.Container + }{ + // a paused container + { + errMsg: "cannot remove a paused container", + fixMsg: "Unpause and then stop the container before attempting removal or force remove", + initContainer: func() *container.Container { + return newContainerWithState(&container.State{Paused: true, Running: true}) + }}, + // a restarting container + { + errMsg: "cannot remove a restarting container", + fixMsg: "Stop the container before attempting removal or force remove", + initContainer: func() *container.Container { + c := newContainerWithState(container.NewState()) + c.SetRunning(0, true) + c.SetRestarting(&container.ExitStatus{}) + return c + }}, + // a running container + { + errMsg: "cannot remove a running container", + fixMsg: "Stop the container before attempting removal or force remove", + initContainer: func() *container.Container { + return newContainerWithState(&container.State{Running: true}) + }}, } - c.SetRunning(0, true) - c.SetRestarting(&container.ExitStatus{}) + for _, te := range tt { + c := te.initContainer() + d, cleanup := newDaemonWithTmpRoot(t) + defer cleanup() + d.containers.Add(c.ID, c) - d, cleanup := newDaemonWithTmpRoot(t) - defer cleanup() - d.containers.Add(c.ID, c) - - err := d.ContainerRm(c.ID, &types.ContainerRmConfig{ForceRemove: false}) - testutil.ErrorContains(t, err, "cannot remove a restarting container") - testutil.ErrorContains(t, err, "Stop the container before attempting removal or force remove") -} - -// TestContainerDeleteRunning tests that a useful error message and instructions is given when attempting -// to remove a running container (#30842) -func TestContainerDeleteRunning(t *testing.T) { - c := &container.Container{ - CommonContainer: container.CommonContainer{ - ID: "test", - State: &container.State{Running: true}, - Config: &containertypes.Config{}, - }, + err := d.ContainerRm(c.ID, &types.ContainerRmConfig{ForceRemove: false}) + testutil.ErrorContains(t, err, te.errMsg) + testutil.ErrorContains(t, err, te.fixMsg) } - - d, cleanup := newDaemonWithTmpRoot(t) - defer cleanup() - d.containers.Add(c.ID, c) - - err := d.ContainerRm(c.ID, &types.ContainerRmConfig{ForceRemove: false}) - testutil.ErrorContains(t, err, "cannot remove a running container") } func TestContainerDoubleDelete(t *testing.T) { - c := &container.Container{ - CommonContainer: container.CommonContainer{ - ID: "test", - State: container.NewState(), - Config: &containertypes.Config{}, - }, - } + c := newContainerWithState(container.NewState()) // Mark the container as having a delete in progress c.SetRemovalInProgress()