Files
moby/integration/container/remove_test.go
Sebastiaan van Stijn d5b47b8fdf integration/container: fix flaky TestRemoveContainerWithVolume
This test depended on the container to die after running the `true` command,
but this condition failed frequently on Windows 2025.

    === Failed
    === FAIL: github.com/docker/docker/integration/container TestRemoveContainerWithVolume (32.68s)
        remove_test.go:61: timeout hit after 10s: waiting for container State.Status to be 'exited', currently 'running'

While this may be revealing an actual issue (and we should have a test for
that), it's irrelevant for this test, which;

- creates and starts a container with an anonymous volume
- verifies the anonymous volume was created
- removes the container
- verifies the anonymous volume was removed

We can force-remove the container to kill, and removed it; we probably
could've sufficed with "container create" (without starting), but it's
good to add extra coverage, in case running the container impacts whether
we're able to remove the volume.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit d6c90dcb87)
Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
2025-08-11 21:57:18 +02:00

109 lines
3.1 KiB
Go

package container
import (
"os"
"testing"
cerrdefs "github.com/containerd/errdefs"
containertypes "github.com/docker/docker/api/types/container"
"github.com/docker/docker/integration/internal/container"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
"gotest.tools/v3/fs"
"gotest.tools/v3/poll"
"gotest.tools/v3/skip"
)
func getPrefixAndSlashFromDaemonPlatform() (prefix, slash string) {
if testEnv.DaemonInfo.OSType == "windows" {
return "c:", `\`
}
return "", "/"
}
// Test case for #5244: `docker rm` fails if bind dir doesn't exist anymore
func TestRemoveContainerWithRemovedVolume(t *testing.T) {
skip.If(t, testEnv.IsRemoteDaemon)
ctx := setupTest(t)
apiClient := testEnv.APIClient()
prefix, slash := getPrefixAndSlashFromDaemonPlatform()
tempDir := fs.NewDir(t, "test-rm-container-with-removed-volume", fs.WithMode(0o755))
cID := container.Run(ctx, t, apiClient, container.WithCmd("true"), container.WithBind(tempDir.Path(), prefix+slash+"test"))
poll.WaitOn(t, container.IsInState(ctx, apiClient, cID, containertypes.StateExited))
err := os.RemoveAll(tempDir.Path())
assert.NilError(t, err)
err = apiClient.ContainerRemove(ctx, cID, containertypes.RemoveOptions{
RemoveVolumes: true,
})
assert.NilError(t, err)
_, _, err = apiClient.ContainerInspectWithRaw(ctx, cID, true)
assert.Check(t, is.ErrorType(err, cerrdefs.IsNotFound))
assert.Check(t, is.ErrorContains(err, "No such container"))
}
// Test case for #2099/#2125
func TestRemoveContainerWithVolume(t *testing.T) {
ctx := setupTest(t)
apiClient := testEnv.APIClient()
prefix, slash := getPrefixAndSlashFromDaemonPlatform()
cID := container.Run(ctx, t, apiClient, container.WithVolume(prefix+slash+"srv"))
insp, _, err := apiClient.ContainerInspectWithRaw(ctx, cID, true)
assert.NilError(t, err)
assert.Check(t, is.Equal(1, len(insp.Mounts)))
volName := insp.Mounts[0].Name
_, err = apiClient.VolumeInspect(ctx, volName)
assert.NilError(t, err)
err = apiClient.ContainerRemove(ctx, cID, containertypes.RemoveOptions{
Force: true,
RemoveVolumes: true,
})
assert.NilError(t, err)
_, err = apiClient.VolumeInspect(ctx, volName)
assert.ErrorType(t, err, cerrdefs.IsNotFound, "Expected anonymous volume to be removed")
}
func TestRemoveContainerRunning(t *testing.T) {
ctx := setupTest(t)
apiClient := testEnv.APIClient()
cID := container.Run(ctx, t, apiClient)
err := apiClient.ContainerRemove(ctx, cID, containertypes.RemoveOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsConflict))
assert.Check(t, is.ErrorContains(err, "container is running"))
}
func TestRemoveContainerForceRemoveRunning(t *testing.T) {
ctx := setupTest(t)
apiClient := testEnv.APIClient()
cID := container.Run(ctx, t, apiClient)
err := apiClient.ContainerRemove(ctx, cID, containertypes.RemoveOptions{
Force: true,
})
assert.NilError(t, err)
}
func TestRemoveInvalidContainer(t *testing.T) {
ctx := setupTest(t)
apiClient := testEnv.APIClient()
err := apiClient.ContainerRemove(ctx, "unknown", containertypes.RemoveOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsNotFound))
assert.Check(t, is.ErrorContains(err, "No such container"))
}