mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
![Aaron Lehmann](/assets/img/avatar_default.png)
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>
110 lines
3.3 KiB
Go
110 lines
3.3 KiB
Go
package daemon
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
containertypes "github.com/docker/docker/api/types/container"
|
|
"github.com/docker/docker/container"
|
|
"github.com/docker/docker/pkg/testutil"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func newDaemonWithTmpRoot(t *testing.T) (*Daemon, func()) {
|
|
tmp, err := ioutil.TempDir("", "docker-daemon-unix-test-")
|
|
require.NoError(t, err)
|
|
d := &Daemon{
|
|
repository: tmp,
|
|
root: tmp,
|
|
}
|
|
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})
|
|
|
|
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{},
|
|
},
|
|
}
|
|
|
|
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})
|
|
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) {
|
|
c := &container.Container{
|
|
CommonContainer: container.CommonContainer{
|
|
ID: "test",
|
|
State: container.NewState(),
|
|
Config: &containertypes.Config{},
|
|
},
|
|
}
|
|
|
|
// Mark the container as having a delete in progress
|
|
c.SetRemovalInProgress()
|
|
|
|
d, cleanup := newDaemonWithTmpRoot(t)
|
|
defer cleanup()
|
|
d.containers.Add(c.ID, c)
|
|
|
|
// Try to remove the container when its state is removalInProgress.
|
|
// It should return an error indicating it is under removal progress.
|
|
err := d.ContainerRm(c.ID, &types.ContainerRmConfig{ForceRemove: true})
|
|
testutil.ErrorContains(t, err, fmt.Sprintf("removal of container %s is already in progress", c.ID))
|
|
}
|