2018-02-05 21:05:59 +00:00
|
|
|
package daemon // import "github.com/docker/docker/daemon"
|
2015-10-20 20:36:09 +00:00
|
|
|
|
|
|
|
import (
|
2016-08-27 13:39:34 +00:00
|
|
|
"fmt"
|
2015-10-20 20:36:09 +00:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2023-12-05 14:58:22 +00:00
|
|
|
"github.com/docker/docker/api/types/backend"
|
2016-09-06 18:18:12 +00:00
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
2015-11-12 19:55:17 +00:00
|
|
|
"github.com/docker/docker/container"
|
2023-08-13 19:47:25 +00:00
|
|
|
"github.com/docker/docker/errdefs"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2015-10-20 20:36:09 +00:00
|
|
|
)
|
|
|
|
|
2017-04-03 10:10:39 +00:00
|
|
|
func newDaemonWithTmpRoot(t *testing.T) (*Daemon, func()) {
|
2021-08-24 10:10:50 +00:00
|
|
|
tmp, err := os.MkdirTemp("", "docker-daemon-unix-test-")
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
2017-04-03 10:10:39 +00:00
|
|
|
d := &Daemon{
|
2015-10-20 20:36:09 +00:00
|
|
|
repository: tmp,
|
|
|
|
root: tmp,
|
|
|
|
}
|
2017-04-03 10:10:39 +00:00
|
|
|
d.containers = container.NewMemoryStore()
|
|
|
|
return d, func() { os.RemoveAll(tmp) }
|
|
|
|
}
|
|
|
|
|
2017-04-04 07:46:12 +00:00
|
|
|
func newContainerWithState(state *container.State) *container.Container {
|
|
|
|
return &container.Container{
|
2017-04-25 19:03:45 +00:00
|
|
|
ID: "test",
|
|
|
|
State: state,
|
|
|
|
Config: &containertypes.Config{},
|
2017-04-03 10:10:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-04 07:46:12 +00:00
|
|
|
// TestContainerDelete tests that a useful error message and instructions is
|
|
|
|
// given when attempting to remove a container (#30842)
|
|
|
|
func TestContainerDelete(t *testing.T) {
|
2023-08-13 19:47:25 +00:00
|
|
|
tests := []struct {
|
|
|
|
doc string
|
2017-04-04 07:46:12 +00:00
|
|
|
errMsg string
|
|
|
|
initContainer func() *container.Container
|
|
|
|
}{
|
|
|
|
{
|
2023-08-13 19:47:25 +00:00
|
|
|
doc: "paused container",
|
|
|
|
errMsg: "container is paused and must be unpaused first",
|
2017-04-04 07:46:12 +00:00
|
|
|
initContainer: func() *container.Container {
|
|
|
|
return newContainerWithState(&container.State{Paused: true, Running: true})
|
2022-01-20 13:25:24 +00:00
|
|
|
},
|
|
|
|
},
|
2017-04-04 07:46:12 +00:00
|
|
|
{
|
2023-08-13 19:47:25 +00:00
|
|
|
doc: "restarting container",
|
|
|
|
errMsg: "container is restarting: stop the container before removing or force remove",
|
2017-04-04 07:46:12 +00:00
|
|
|
initContainer: func() *container.Container {
|
|
|
|
c := newContainerWithState(container.NewState())
|
2022-05-10 19:59:00 +00:00
|
|
|
c.SetRunning(nil, nil, true)
|
2017-04-04 07:46:12 +00:00
|
|
|
c.SetRestarting(&container.ExitStatus{})
|
|
|
|
return c
|
2022-01-20 13:25:24 +00:00
|
|
|
},
|
|
|
|
},
|
2017-04-04 07:46:12 +00:00
|
|
|
{
|
2023-08-13 19:47:25 +00:00
|
|
|
doc: "running container",
|
|
|
|
errMsg: "container is running: stop the container before removing or force remove",
|
2017-04-04 07:46:12 +00:00
|
|
|
initContainer: func() *container.Container {
|
|
|
|
return newContainerWithState(&container.State{Running: true})
|
2022-01-20 13:25:24 +00:00
|
|
|
},
|
|
|
|
},
|
2017-04-03 10:10:39 +00:00
|
|
|
}
|
|
|
|
|
2023-08-13 19:47:25 +00:00
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.doc, func(t *testing.T) {
|
|
|
|
c := tc.initContainer()
|
|
|
|
d, cleanup := newDaemonWithTmpRoot(t)
|
|
|
|
defer cleanup()
|
|
|
|
d.containers.Add(c.ID, c)
|
2017-04-03 10:10:39 +00:00
|
|
|
|
2023-12-05 14:58:22 +00:00
|
|
|
err := d.ContainerRm(c.ID, &backend.ContainerRmConfig{ForceRemove: false})
|
2023-08-13 19:47:25 +00:00
|
|
|
assert.Check(t, is.ErrorType(err, errdefs.IsConflict))
|
|
|
|
assert.Check(t, is.ErrorContains(err, tc.errMsg))
|
|
|
|
})
|
2017-04-03 10:10:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainerDoubleDelete(t *testing.T) {
|
2017-04-04 07:46:12 +00:00
|
|
|
c := newContainerWithState(container.NewState())
|
2015-10-20 20:36:09 +00:00
|
|
|
|
|
|
|
// Mark the container as having a delete in progress
|
2017-04-03 10:10:39 +00:00
|
|
|
c.SetRemovalInProgress()
|
|
|
|
|
|
|
|
d, cleanup := newDaemonWithTmpRoot(t)
|
|
|
|
defer cleanup()
|
|
|
|
d.containers.Add(c.ID, c)
|
2015-10-20 20:36:09 +00:00
|
|
|
|
2016-08-27 13:39:34 +00:00
|
|
|
// Try to remove the container when its state is removalInProgress.
|
|
|
|
// It should return an error indicating it is under removal progress.
|
2023-12-05 14:58:22 +00:00
|
|
|
err := d.ContainerRm(c.ID, &backend.ContainerRmConfig{ForceRemove: true})
|
2018-05-20 22:06:50 +00:00
|
|
|
assert.Check(t, is.ErrorContains(err, fmt.Sprintf("removal of container %s is already in progress", c.ID)))
|
2015-10-20 20:36:09 +00:00
|
|
|
}
|