2018-02-11 20:18:35 -05:00
|
|
|
package container // import "github.com/docker/docker/integration/container"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
|
|
|
"github.com/docker/docker/integration/internal/container"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/fs"
|
|
|
|
"gotest.tools/v3/poll"
|
|
|
|
"gotest.tools/v3/skip"
|
2018-02-11 20:18:35 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func getPrefixAndSlashFromDaemonPlatform() (prefix, slash string) {
|
|
|
|
if testEnv.OSType == "windows" {
|
|
|
|
return "c:", `\`
|
|
|
|
}
|
|
|
|
return "", "/"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test case for #5244: `docker rm` fails if bind dir doesn't exist anymore
|
|
|
|
func TestRemoveContainerWithRemovedVolume(t *testing.T) {
|
2019-01-03 06:19:15 -05:00
|
|
|
skip.If(t, testEnv.IsRemoteDaemon)
|
2018-02-11 20:18:35 -05:00
|
|
|
|
|
|
|
defer setupTest(t)()
|
|
|
|
ctx := context.Background()
|
2019-01-02 08:16:25 -05:00
|
|
|
client := testEnv.APIClient()
|
2018-02-11 20:18:35 -05:00
|
|
|
|
|
|
|
prefix, slash := getPrefixAndSlashFromDaemonPlatform()
|
|
|
|
|
|
|
|
tempDir := fs.NewDir(t, "test-rm-container-with-removed-volume", fs.WithMode(0755))
|
|
|
|
defer tempDir.Remove()
|
|
|
|
|
2019-06-06 07:15:31 -04:00
|
|
|
cID := container.Run(ctx, t, client, container.WithCmd("true"), container.WithBind(tempDir.Path(), prefix+slash+"test"))
|
2018-02-11 20:18:35 -05:00
|
|
|
poll.WaitOn(t, container.IsInState(ctx, client, cID, "exited"), poll.WithDelay(100*time.Millisecond))
|
|
|
|
|
|
|
|
err := os.RemoveAll(tempDir.Path())
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-02-11 20:18:35 -05:00
|
|
|
|
|
|
|
err = client.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{
|
|
|
|
RemoveVolumes: true,
|
|
|
|
})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-02-11 20:18:35 -05:00
|
|
|
|
|
|
|
_, _, err = client.ContainerInspectWithRaw(ctx, cID, true)
|
2018-05-20 18:06:50 -04:00
|
|
|
assert.Check(t, is.ErrorContains(err, "No such container"))
|
2018-02-11 20:18:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test case for #2099/#2125
|
|
|
|
func TestRemoveContainerWithVolume(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
|
|
|
ctx := context.Background()
|
2019-01-02 08:16:25 -05:00
|
|
|
client := testEnv.APIClient()
|
2018-02-11 20:18:35 -05:00
|
|
|
|
|
|
|
prefix, slash := getPrefixAndSlashFromDaemonPlatform()
|
|
|
|
|
2019-06-06 07:15:31 -04:00
|
|
|
cID := container.Run(ctx, t, client, container.WithCmd("true"), container.WithVolume(prefix+slash+"srv"))
|
2018-02-11 20:18:35 -05:00
|
|
|
poll.WaitOn(t, container.IsInState(ctx, client, cID, "exited"), poll.WithDelay(100*time.Millisecond))
|
|
|
|
|
|
|
|
insp, _, err := client.ContainerInspectWithRaw(ctx, cID, true)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(1, len(insp.Mounts)))
|
2018-02-11 20:18:35 -05:00
|
|
|
volName := insp.Mounts[0].Name
|
|
|
|
|
|
|
|
err = client.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{
|
|
|
|
RemoveVolumes: true,
|
|
|
|
})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-02-11 20:18:35 -05:00
|
|
|
|
|
|
|
volumes, err := client.VolumeList(ctx, filters.NewArgs(filters.Arg("name", volName)))
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(0, len(volumes.Volumes)))
|
2018-02-11 20:18:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveContainerRunning(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
|
|
|
ctx := context.Background()
|
2019-01-02 08:16:25 -05:00
|
|
|
client := testEnv.APIClient()
|
2018-02-11 20:18:35 -05:00
|
|
|
|
2019-06-06 07:15:31 -04:00
|
|
|
cID := container.Run(ctx, t, client)
|
2018-02-11 20:18:35 -05:00
|
|
|
|
|
|
|
err := client.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{})
|
2018-05-20 18:06:50 -04:00
|
|
|
assert.Check(t, is.ErrorContains(err, "cannot remove a running container"))
|
2018-02-11 20:18:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveContainerForceRemoveRunning(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
|
|
|
ctx := context.Background()
|
2019-01-02 08:16:25 -05:00
|
|
|
client := testEnv.APIClient()
|
2018-02-11 20:18:35 -05:00
|
|
|
|
2019-06-06 07:15:31 -04:00
|
|
|
cID := container.Run(ctx, t, client)
|
2018-02-11 20:18:35 -05:00
|
|
|
|
|
|
|
err := client.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{
|
|
|
|
Force: true,
|
|
|
|
})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-02-11 20:18:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveInvalidContainer(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
|
|
|
ctx := context.Background()
|
2019-01-02 08:16:25 -05:00
|
|
|
client := testEnv.APIClient()
|
2018-02-11 20:18:35 -05:00
|
|
|
|
|
|
|
err := client.ContainerRemove(ctx, "unknown", types.ContainerRemoveOptions{})
|
2018-05-20 18:06:50 -04:00
|
|
|
assert.Check(t, is.ErrorContains(err, "No such container"))
|
2018-02-11 20:18:35 -05:00
|
|
|
}
|