Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion list.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func getContainers(context *cli.Context) ([]containerState, error) {
root := context.GlobalString("root")
list, err := os.ReadDir(root)
if err != nil {
if errors.Is(err, os.ErrNotExist) && context.IsSet("root") {
if errors.Is(err, os.ErrNotExist) && !context.GlobalIsSet("root") {
// Ignore non-existing default root directory
// (no containers created yet).
return nil, nil
Expand Down
54 changes: 54 additions & 0 deletions tests/integration/list.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@

load helpers

# Default root directory for runc (as defined in main.go).
RUNC_DEFAULT_ROOT="/run/runc"

function setup() {
setup_busybox
ALT_ROOT="$ROOT/alt"
mkdir -p "$ALT_ROOT/state"
}

function teardown() {
# Restore default root if it was backed up by the
# "non-existent default root" test.
if [ -d "${RUNC_DEFAULT_ROOT}.bak" ]; then
rm -rf "$RUNC_DEFAULT_ROOT"
mv "${RUNC_DEFAULT_ROOT}.bak" "$RUNC_DEFAULT_ROOT"
fi
ROOT="$ALT_ROOT" teardown_bundle
unset ALT_ROOT
teardown_bundle
Expand Down Expand Up @@ -51,3 +60,48 @@ function teardown() {
[[ "${lines[0]}" == *[,][\{]"\"ociVersion\""[:]"\""*[0-9][\.]*[0-9][\.]*[0-9]*"\""[,]"\"id\""[:]"\"test_box2\""[,]"\"pid\""[:]*[0-9][,]"\"status\""[:]*"\"running\""[,]"\"bundle\""[:]*$bundle*[,]"\"rootfs\""[:]"\""*"\""[,]"\"created\""[:]*[0-9]*[\}]* ]]
[[ "${lines[0]}" == *[,][\{]"\"ociVersion\""[:]"\""*[0-9][\.]*[0-9][\.]*[0-9]*"\""[,]"\"id\""[:]"\"test_box3\""[,]"\"pid\""[:]*[0-9][,]"\"status\""[:]*"\"running\""[,]"\"bundle\""[:]*$bundle*[,]"\"rootfs\""[:]"\""*"\""[,]"\"created\""[:]*[0-9]*[\}][\]] ]]
}

@test "list with non-existent default root succeeds" {
# The default root /run/runc is only used when running as real
# root; rootless uses $XDG_RUNTIME_DIR/runc instead.
requires root unsafe # Modifies /run/runc.

# Back up and remove the default root to guarantee it
# doesn't exist. Restored in teardown.
[ -d "$RUNC_DEFAULT_ROOT" ] && mv "$RUNC_DEFAULT_ROOT" "${RUNC_DEFAULT_ROOT}.bak"

# Use ROOT="" so the runc wrapper doesn't pass --root,
# letting runc use its default root.
ROOT="" runc list
[ "$status" -eq 0 ]

ROOT="" runc list --format json
[ "$status" -eq 0 ]
[ "$output" = "null" ]

ROOT="" runc list -q
[ "$status" -eq 0 ]
[ "$output" = "" ]
}

@test "list with empty default root succeeds" {
# The default root /run/runc is only used when running as real
# root; rootless uses $XDG_RUNTIME_DIR/runc instead.
requires root unsafe # Modifies /run/runc.

# Ensure the default root exists but is empty.
mkdir -p "$RUNC_DEFAULT_ROOT"

# Use ROOT="" so the runc wrapper doesn't pass --root,
# letting runc use its default root.
ROOT="" runc list
[ "$status" -eq 0 ]

ROOT="" runc list --format json
[ "$status" -eq 0 ]
[ "$output" = "null" ]

ROOT="" runc list -q
[ "$status" -eq 0 ]
[ "$output" = "" ]
}