2015-10-20 16:36:09 -04:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2016-08-27 21:39:34 +08:00
|
|
|
"fmt"
|
2015-10-20 16:36:09 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2016-09-06 11:18:12 -07:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
2015-11-12 11:55:17 -08:00
|
|
|
"github.com/docker/docker/container"
|
2017-04-03 12:10:39 +02:00
|
|
|
"github.com/docker/docker/pkg/testutil/assert"
|
2015-10-20 16:36:09 -04:00
|
|
|
)
|
|
|
|
|
2017-04-03 12:10:39 +02:00
|
|
|
func newDaemonWithTmpRoot(t *testing.T) (*Daemon, func()) {
|
2015-10-20 16:36:09 -04:00
|
|
|
tmp, err := ioutil.TempDir("", "docker-daemon-unix-test-")
|
2017-04-03 12:10:39 +02:00
|
|
|
assert.NilError(t, err)
|
|
|
|
d := &Daemon{
|
2015-10-20 16:36:09 -04:00
|
|
|
repository: tmp,
|
|
|
|
root: tmp,
|
|
|
|
}
|
2017-04-03 12:10:39 +02:00
|
|
|
d.containers = container.NewMemoryStore()
|
|
|
|
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{
|
|
|
|
CommonContainer: container.CommonContainer{
|
|
|
|
ID: "test",
|
|
|
|
State: &container.State{Paused: true, 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})
|
|
|
|
|
|
|
|
assert.Error(t, err, "cannot remove a paused container")
|
|
|
|
assert.Error(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{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
c.SetRunning(0, true)
|
|
|
|
c.SetRestarting(&container.ExitStatus{})
|
|
|
|
|
|
|
|
d, cleanup := newDaemonWithTmpRoot(t)
|
|
|
|
defer cleanup()
|
|
|
|
d.containers.Add(c.ID, c)
|
|
|
|
|
|
|
|
err := d.ContainerRm(c.ID, &types.ContainerRmConfig{ForceRemove: false})
|
|
|
|
assert.Error(t, err, "cannot remove a restarting container")
|
|
|
|
assert.Error(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{},
|
|
|
|
},
|
|
|
|
}
|
2015-10-20 16:36:09 -04:00
|
|
|
|
2017-04-03 12:10:39 +02:00
|
|
|
d, cleanup := newDaemonWithTmpRoot(t)
|
|
|
|
defer cleanup()
|
|
|
|
d.containers.Add(c.ID, c)
|
|
|
|
|
|
|
|
err := d.ContainerRm(c.ID, &types.ContainerRmConfig{ForceRemove: false})
|
|
|
|
assert.Error(t, err, "cannot remove a running container")
|
|
|
|
assert.Error(t, err, "Stop the container before attempting removal or force remove")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainerDoubleDelete(t *testing.T) {
|
|
|
|
c := &container.Container{
|
2015-11-12 11:55:17 -08:00
|
|
|
CommonContainer: container.CommonContainer{
|
2015-12-01 09:54:26 -08:00
|
|
|
ID: "test",
|
2015-11-12 11:55:17 -08:00
|
|
|
State: container.NewState(),
|
2015-12-18 13:36:17 -05:00
|
|
|
Config: &containertypes.Config{},
|
2015-10-20 16:36:09 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark the container as having a delete in progress
|
2017-04-03 12:10:39 +02:00
|
|
|
c.SetRemovalInProgress()
|
|
|
|
|
|
|
|
d, cleanup := newDaemonWithTmpRoot(t)
|
|
|
|
defer cleanup()
|
|
|
|
d.containers.Add(c.ID, c)
|
2015-10-20 16:36:09 -04:00
|
|
|
|
2016-08-27 21:39:34 +08:00
|
|
|
// Try to remove the container when its state is removalInProgress.
|
|
|
|
// It should return an error indicating it is under removal progress.
|
2017-04-03 12:10:39 +02:00
|
|
|
err := d.ContainerRm(c.ID, &types.ContainerRmConfig{ForceRemove: true})
|
|
|
|
assert.Error(t, err, fmt.Sprintf("removal of container %s is already in progress", c.ID))
|
2015-10-20 16:36:09 -04:00
|
|
|
}
|