github.com/rumpl/bof@v23.0.0-rc.2+incompatible/integration/container/remove_test.go (about) 1 package container // import "github.com/docker/docker/integration/container" 2 3 import ( 4 "context" 5 "os" 6 "testing" 7 "time" 8 9 "github.com/docker/docker/api/types" 10 "github.com/docker/docker/api/types/filters" 11 "github.com/docker/docker/integration/internal/container" 12 "gotest.tools/v3/assert" 13 is "gotest.tools/v3/assert/cmp" 14 "gotest.tools/v3/fs" 15 "gotest.tools/v3/poll" 16 "gotest.tools/v3/skip" 17 ) 18 19 func getPrefixAndSlashFromDaemonPlatform() (prefix, slash string) { 20 if testEnv.OSType == "windows" { 21 return "c:", `\` 22 } 23 return "", "/" 24 } 25 26 // Test case for #5244: `docker rm` fails if bind dir doesn't exist anymore 27 func TestRemoveContainerWithRemovedVolume(t *testing.T) { 28 skip.If(t, testEnv.IsRemoteDaemon) 29 30 defer setupTest(t)() 31 ctx := context.Background() 32 client := testEnv.APIClient() 33 34 prefix, slash := getPrefixAndSlashFromDaemonPlatform() 35 36 tempDir := fs.NewDir(t, "test-rm-container-with-removed-volume", fs.WithMode(0755)) 37 defer tempDir.Remove() 38 39 cID := container.Run(ctx, t, client, container.WithCmd("true"), container.WithBind(tempDir.Path(), prefix+slash+"test")) 40 poll.WaitOn(t, container.IsInState(ctx, client, cID, "exited"), poll.WithDelay(100*time.Millisecond)) 41 42 err := os.RemoveAll(tempDir.Path()) 43 assert.NilError(t, err) 44 45 err = client.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{ 46 RemoveVolumes: true, 47 }) 48 assert.NilError(t, err) 49 50 _, _, err = client.ContainerInspectWithRaw(ctx, cID, true) 51 assert.Check(t, is.ErrorContains(err, "No such container")) 52 } 53 54 // Test case for #2099/#2125 55 func TestRemoveContainerWithVolume(t *testing.T) { 56 defer setupTest(t)() 57 ctx := context.Background() 58 client := testEnv.APIClient() 59 60 prefix, slash := getPrefixAndSlashFromDaemonPlatform() 61 62 cID := container.Run(ctx, t, client, container.WithCmd("true"), container.WithVolume(prefix+slash+"srv")) 63 poll.WaitOn(t, container.IsInState(ctx, client, cID, "exited"), poll.WithDelay(100*time.Millisecond)) 64 65 insp, _, err := client.ContainerInspectWithRaw(ctx, cID, true) 66 assert.NilError(t, err) 67 assert.Check(t, is.Equal(1, len(insp.Mounts))) 68 volName := insp.Mounts[0].Name 69 70 err = client.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{ 71 RemoveVolumes: true, 72 }) 73 assert.NilError(t, err) 74 75 volumes, err := client.VolumeList(ctx, filters.NewArgs(filters.Arg("name", volName))) 76 assert.NilError(t, err) 77 assert.Check(t, is.Equal(0, len(volumes.Volumes))) 78 } 79 80 func TestRemoveContainerRunning(t *testing.T) { 81 defer setupTest(t)() 82 ctx := context.Background() 83 client := testEnv.APIClient() 84 85 cID := container.Run(ctx, t, client) 86 87 err := client.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{}) 88 assert.Check(t, is.ErrorContains(err, "cannot remove a running container")) 89 } 90 91 func TestRemoveContainerForceRemoveRunning(t *testing.T) { 92 defer setupTest(t)() 93 ctx := context.Background() 94 client := testEnv.APIClient() 95 96 cID := container.Run(ctx, t, client) 97 98 err := client.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{ 99 Force: true, 100 }) 101 assert.NilError(t, err) 102 } 103 104 func TestRemoveInvalidContainer(t *testing.T) { 105 defer setupTest(t)() 106 ctx := context.Background() 107 client := testEnv.APIClient() 108 109 err := client.ContainerRemove(ctx, "unknown", types.ContainerRemoveOptions{}) 110 assert.Check(t, is.ErrorContains(err, "No such container")) 111 }