2018-02-05 16:05:59 -05:00
|
|
|
package daemon // import "github.com/docker/docker/daemon"
|
2015-10-20 16:36:09 -04:00
|
|
|
|
|
|
|
import (
|
2016-08-27 09:39:34 -04:00
|
|
|
"fmt"
|
2015-10-20 16:36:09 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
2015-11-12 14:55:17 -05:00
|
|
|
"github.com/docker/docker/container"
|
2017-08-23 17:35:09 -04:00
|
|
|
"github.com/docker/docker/internal/testutil"
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-10-20 16:36:09 -04:00
|
|
|
)
|
|
|
|
|
2017-04-03 06:10:39 -04:00
|
|
|
func newDaemonWithTmpRoot(t *testing.T) (*Daemon, func()) {
|
2015-10-20 16:36:09 -04:00
|
|
|
tmp, err := ioutil.TempDir("", "docker-daemon-unix-test-")
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
require.NoError(t, err)
|
2017-04-03 06:10:39 -04:00
|
|
|
d := &Daemon{
|
2015-10-20 16:36:09 -04:00
|
|
|
repository: tmp,
|
|
|
|
root: tmp,
|
|
|
|
}
|
2017-04-03 06:10:39 -04:00
|
|
|
d.containers = container.NewMemoryStore()
|
|
|
|
return d, func() { os.RemoveAll(tmp) }
|
|
|
|
}
|
|
|
|
|
2017-04-04 03:46:12 -04:00
|
|
|
func newContainerWithState(state *container.State) *container.Container {
|
|
|
|
return &container.Container{
|
2017-04-25 15:03:45 -04:00
|
|
|
ID: "test",
|
|
|
|
State: state,
|
|
|
|
Config: &containertypes.Config{},
|
2017-04-03 06:10:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-04-04 03:46:12 -04:00
|
|
|
// 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})
|
|
|
|
}},
|
2017-04-03 06:10:39 -04:00
|
|
|
}
|
|
|
|
|
2017-04-04 03:46:12 -04:00
|
|
|
for _, te := range tt {
|
|
|
|
c := te.initContainer()
|
|
|
|
d, cleanup := newDaemonWithTmpRoot(t)
|
|
|
|
defer cleanup()
|
|
|
|
d.containers.Add(c.ID, c)
|
2017-04-03 06:10:39 -04:00
|
|
|
|
2017-04-04 03:46:12 -04:00
|
|
|
err := d.ContainerRm(c.ID, &types.ContainerRmConfig{ForceRemove: false})
|
|
|
|
testutil.ErrorContains(t, err, te.errMsg)
|
|
|
|
testutil.ErrorContains(t, err, te.fixMsg)
|
2017-04-03 06:10:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainerDoubleDelete(t *testing.T) {
|
2017-04-04 03:46:12 -04:00
|
|
|
c := newContainerWithState(container.NewState())
|
2015-10-20 16:36:09 -04:00
|
|
|
|
|
|
|
// Mark the container as having a delete in progress
|
2017-04-03 06:10:39 -04: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 09:39:34 -04: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 06:10:39 -04:00
|
|
|
err := d.ContainerRm(c.ID, &types.ContainerRmConfig{ForceRemove: true})
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
testutil.ErrorContains(t, err, fmt.Sprintf("removal of container %s is already in progress", c.ID))
|
2015-10-20 16:36:09 -04:00
|
|
|
}
|