1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Use a test table in the daemon delete unit tests

Signed-off-by: Boaz Shuster <ripcurld.github@gmail.com>
This commit is contained in:
Boaz Shuster 2017-04-04 10:46:12 +03:00
parent cd4c089b9e
commit 8f51746997

View file

@ -24,77 +24,65 @@ func newDaemonWithTmpRoot(t *testing.T) (*Daemon, func()) {
return d, func() { os.RemoveAll(tmp) } return d, func() { os.RemoveAll(tmp) }
} }
// TestContainerDeletePaused tests that a useful error message and instructions is given when attempting func newContainerWithState(state *container.State) *container.Container {
// to remove a paused container (#30842) return &container.Container{
func TestContainerDeletePaused(t *testing.T) {
c := &container.Container{
CommonContainer: container.CommonContainer{ CommonContainer: container.CommonContainer{
ID: "test", ID: "test",
State: &container.State{Paused: true, Running: true}, State: state,
Config: &containertypes.Config{}, 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 // TestContainerDelete tests that a useful error message and instructions is
// to remove a container that is restarting (#30842) // given when attempting to remove a container (#30842)
func TestContainerDeleteRestarting(t *testing.T) { func TestContainerDelete(t *testing.T) {
c := &container.Container{ tt := []struct {
CommonContainer: container.CommonContainer{ errMsg string
ID: "test", fixMsg string
State: container.NewState(), initContainer func() *container.Container
Config: &containertypes.Config{}, }{
}, // 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) for _, te := range tt {
c.SetRestarting(&container.ExitStatus{}) c := te.initContainer()
d, cleanup := newDaemonWithTmpRoot(t)
defer cleanup()
d.containers.Add(c.ID, c)
d, cleanup := newDaemonWithTmpRoot(t) err := d.ContainerRm(c.ID, &types.ContainerRmConfig{ForceRemove: false})
defer cleanup() testutil.ErrorContains(t, err, te.errMsg)
d.containers.Add(c.ID, c) testutil.ErrorContains(t, err, te.fixMsg)
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{},
},
} }
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) { func TestContainerDoubleDelete(t *testing.T) {
c := &container.Container{ c := newContainerWithState(container.NewState())
CommonContainer: container.CommonContainer{
ID: "test",
State: container.NewState(),
Config: &containertypes.Config{},
},
}
// Mark the container as having a delete in progress // Mark the container as having a delete in progress
c.SetRemovalInProgress() c.SetRemovalInProgress()