test/system: Test the order in 'list' for images with & without names

Note that 'run --keep-empty-lines' counts the trailing newline on the
last line as a separate line.

Until Bats 1.7.0, 'run --keep-empty-lines' had a bug where even when a
command produced no output, it would report a line count of one [1] due
to a stray line feed character.  This needs to be conditionalized, since
Fedora 35 has Bats 1.5.0.

[1] https://github.com/bats-core/bats-core/issues/573

https://github.com/containers/toolbox/pull/1192
This commit is contained in:
Debarshi Ray 2022-12-07 19:52:50 +01:00
parent cc60bc6893
commit 4d1cc5b39b
2 changed files with 50 additions and 0 deletions

View file

@ -107,3 +107,23 @@ teardown() {
assert_line --index 6 --partial "non-default-one"
assert_line --index 7 --partial "non-default-two"
}
@test "list: Images with and without names" {
local default_image
default_image="$(get_default_image)"
pull_default_image
pull_distro_image fedora 34
build_image_without_name
run --keep-empty-lines --separate-stderr "$TOOLBOX" list --images
assert_success
assert_line --index 1 --partial "<none>"
assert_line --index 2 --partial "$default_image"
assert_line --index 3 --partial "fedora-toolbox:34"
assert [ ${#lines[@]} -eq 5 ]
if check_bats_version 1.7.0; then
assert [ ${#stderr_lines[@]} -eq 0 ]
fi
}

View file

@ -239,6 +239,36 @@ function build_image_without_name() {
}
function check_bats_version() {
local required_version
required_version="$1"
if ! old_version=$(printf "%s\n%s\n" "$BATS_VERSION" "$required_version" | sort --version-sort | head --lines 1); then
return 1
fi
if [ "$required_version" = "$old_version" ]; then
return 0
fi
return 1
}
function get_default_image() {
local distro
local image
local release
distro="$(get_system_id)"
release="$(get_system_version)"
image="${IMAGES[$distro]}:$release"
echo "$image"
return 0
}
# Copies an image from local storage to Podman's image store
#
# Call before creating any container. Network failures are not nice.