kpod_rm: Add option for --all

Remove all containers with -a, --all.

Enable kpod rm tests which were all set to skip. Add two
tests for -a

Signed-off-by: baude <bbaude@redhat.com>

Closes: #74
Approved by: rhatdan
This commit is contained in:
baude 2017-11-24 09:21:09 -06:00 committed by Atomic Bot
parent 61e0ab4f47
commit dd88ce005f
4 changed files with 77 additions and 58 deletions

View File

@ -2,8 +2,10 @@ package main
import ( import (
"fmt" "fmt"
"os"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/projectatomic/libpod/libpod"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@ -13,6 +15,10 @@ var (
Name: "force, f", Name: "force, f",
Usage: "Force removal of a running container. The default is false", Usage: "Force removal of a running container. The default is false",
}, },
cli.BoolFlag{
Name: "all, a",
Usage: "Remove all containers",
},
} }
rmDescription = "Remove one or more containers" rmDescription = "Remove one or more containers"
rmCommand = cli.Command{ rmCommand = cli.Command{
@ -39,20 +45,45 @@ func rmCmd(c *cli.Context) error {
defer runtime.Shutdown(false) defer runtime.Shutdown(false)
args := c.Args() args := c.Args()
if len(args) == 0 { if len(args) == 0 && !c.Bool("all") {
return errors.Errorf("specify one or more containers to remove") return errors.Errorf("specify one or more containers to remove")
} }
for _, container := range args { var delContainers []*libpod.Container
ctr, err := runtime.LookupContainer(container) var lastError error
if c.Bool("all") {
delContainers, err = runtime.GetContainers()
if err != nil { if err != nil {
return errors.Wrapf(err, "error looking up container", container) return errors.Wrapf(err, "unable to get container list")
} }
err = runtime.RemoveContainer(ctr, c.Bool("force")) } else {
if err != nil { for _, i := range args {
return errors.Wrapf(err, "error removing container %q", ctr.ID()) container, err := runtime.LookupContainer(i)
if err != nil {
fmt.Fprintln(os.Stderr, err)
lastError = errors.Wrapf(err, "unable to find container %s", i)
continue
}
delContainers = append(delContainers, container)
} }
fmt.Println(ctr.ID())
} }
return nil for _, container := range delContainers {
if err != nil {
if lastError != nil {
fmt.Fprintln(os.Stderr, lastError)
}
lastError = errors.Wrapf(err, "failed to find container %s", container.ID())
continue
}
err = runtime.RemoveContainer(container, c.Bool("force"))
if err != nil {
if lastError != nil {
fmt.Fprintln(os.Stderr, lastError)
}
lastError = errors.Wrapf(err, "failed to delete container %v", container.ID())
} else {
fmt.Println(container.ID())
}
}
return lastError
} }

View File

@ -1213,6 +1213,8 @@ _kpod_run() {
_kpod_rm() { _kpod_rm() {
local boolean_options=" local boolean_options="
--all
-a
--force --force
-f -f
" "

View File

@ -9,7 +9,7 @@ kpod rm - Remove one or more containers
**kpod** **rm** [*options* [...]] container **kpod** **rm** [*options* [...]] container
## DESCRIPTION ## DESCRIPTION
Kpod rm will remove one or more containers from the host. The container name or ID can be used. This does not remove images. Running containers will not be removed without the -f option kpod rm will remove one or more containers from the host. The container name or ID can be used. This does not remove images. Running containers will not be removed without the -f option
## OPTIONS ## OPTIONS
@ -17,13 +17,20 @@ Kpod rm will remove one or more containers from the host. The container name or
Force the removal of a running container Force the removal of a running container
**--all, a**
Remove all containers. Can be used in conjunction with -f as well.
## EXAMPLE ## EXAMPLE
kpod rm mywebserver kpod rm mywebserver
kpod rm mywebserver myflaskserver 860a4b23
kpod rm -f 860a4b23 kpod rm -f 860a4b23
kpod rm -f -a
## SEE ALSO ## SEE ALSO
kpod(1), kpod-rmi(1) kpod(1), kpod-rmi(1)

View File

@ -2,60 +2,28 @@
load helpers load helpers
IMAGE="alpine:latest"
function teardown() {
cleanup_test
}
function setup() { function setup() {
copy_images copy_images
} }
@test "remove a stopped container" { @test "remove a stopped container" {
skip "Test needs to be converted to kpod run" run ${KPOD_BINARY} $KPOD_OPTIONS run -d ${ALPINE} ls
start_crio
run crioctl pod run --config "$TESTDATA"/sandbox_config.json
echo "$output"
[ "$status" -eq 0 ]
pod_id="$output"
run crioctl ctr create --config "$TESTDATA"/container_config.json --pod "$pod_id"
echo "$output" echo "$output"
[ "$status" -eq 0 ] [ "$status" -eq 0 ]
ctr_id="$output" ctr_id="$output"
run crioctl ctr start --id "$ctr_id"
echo "$output"
[ "$status" -eq 0 ]
run crioctl ctr stop --id "$ctr_id"
echo "$output"
[ "$status" -eq 0 ]
run bash -c ${KPOD_BINARY} $KPOD_OPTIONS rm "$ctr_id" run bash -c ${KPOD_BINARY} $KPOD_OPTIONS rm "$ctr_id"
echo "$output" echo "$output"
[ "$status" -eq 0 ] [ "$status" -eq 0 ]
cleanup_pods
stop_crio
} }
@test "refuse to remove a running container" { @test "refuse to remove a running container" {
skip "Test needs to be converted to kpod run" run ${KPOD_BINARY} $KPOD_OPTIONS run -d ${ALPINE} sleep 15
start_crio
run crioctl pod run --config "$TESTDATA"/sandbox_config.json
echo "$output"
[ "$status" -eq 0 ]
pod_id="$output"
run crioctl ctr create --config "$TESTDATA"/container_redis.json --pod "$pod_id"
echo "$output" echo "$output"
[ "$status" -eq 0 ] [ "$status" -eq 0 ]
ctr_id="$output" ctr_id="$output"
run crioctl ctr start --id "$ctr_id" run bash ${KPOD_BINARY} $KPOD_OPTIONS rm "$ctr_id"
echo "$output"
[ "$status" -eq 0 ]
run bash -c ${KPOD_BINARY} $KPOD_OPTIONS rm "$ctr_id"
echo "$output" echo "$output"
[ "$status" -ne 0 ] [ "$status" -ne 0 ]
cleanup_ctrs
cleanup_pods
stop_crio
} }
@test "remove a created container" { @test "remove a created container" {
@ -69,22 +37,33 @@ function setup() {
} }
@test "remove a running container" { @test "remove a running container" {
skip "Test needs to be converted to kpod run" skip "Skipping until kpod stop is implemented"
start_crio run ${KPOD_BINARY} $KPOD_OPTIONS run -d ${ALPINE} sleep 15
run crioctl pod run --config "$TESTDATA"/sandbox_config.json
echo "$output"
[ "$status" -eq 0 ]
pod_id="$output"
run crioctl ctr create --config "$TESTDATA"/container_redis.json --pod "$pod_id"
echo "$output" echo "$output"
[ "$status" -eq 0 ] [ "$status" -eq 0 ]
ctr_id="$output" ctr_id="$output"
run crioctl ctr start --id "$ctr_id"
echo "$output"
[ "$status" -eq 0 ]
run bash -c ${KPOD_BINARY} $KPOD_OPTIONS rm -f "$ctr_id" run bash -c ${KPOD_BINARY} $KPOD_OPTIONS rm -f "$ctr_id"
echo "$output" echo "$output"
[ "$status" -eq 0 ] [ "$status" -eq 1 ]
cleanup_pods }
stop_crio
@test "remove all containers" {
${KPOD_BINARY} ${KPOD_OPTIONS} create $BB ls
${KPOD_BINARY} ${KPOD_OPTIONS} create $BB ls -l
${KPOD_BINARY} ${KPOD_OPTIONS} create $BB true
${KPOD_BINARY} ${KPOD_OPTIONS} create $BB whoami
run ${KPOD_BINARY} $KPOD_OPTIONS rm -a
echo "$output"
[ "$status" -eq 1 ]
}
@test "remove all containers with one running" {
skip "Skipping until kpod stop is implemented"
${KPOD_BINARY} ${KPOD_OPTIONS} create $BB ls
${KPOD_BINARY} ${KPOD_OPTIONS} create $BB ls -l
${KPOD_BINARY} ${KPOD_OPTIONS} create $BB whoami
${KPOD_BINARY} ${KPOD_OPTIONS} run -d ${ALPINE} sleep 30
run ${KPOD_BINARY} $KPOD_OPTIONS rm -a -f
echo "$output"
[ "$status" -eq 1 ]
} }