test/system: Ensure that error messages go to the standard error stream

Currently, there's no way to get assert_line to use the stderr_lines
array [1].  This is worked around by assigning stderr_lines to the
'lines' array.

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

https://github.com/containers/toolbox/pull/1160
This commit is contained in:
Debarshi Ray 2022-11-16 11:58:10 +01:00
parent 8b7511ab6f
commit e8ad1eaad0

View file

@ -105,9 +105,10 @@ teardown() {
@test "run: Try to run a command in the default container with no containers created" {
local default_container_name="$(get_system_id)-toolbox-$(get_system_version)"
run $TOOLBOX run true
run --separate-stderr $TOOLBOX run true
assert_failure
lines=("${stderr_lines[@]}")
assert_line --index 0 "Error: container $default_container_name not found"
assert_line --index 1 "Use the 'create' command to create a toolbox."
assert_line --index 2 "Run 'toolbox --help' for usage."
@ -118,9 +119,10 @@ teardown() {
create_container other-container
run $TOOLBOX run true
run --separate-stderr $TOOLBOX run true
assert_failure
lines=("${stderr_lines[@]}")
assert_line --index 0 "Error: container $default_container_name not found"
assert_line --index 1 "Use the 'create' command to create a toolbox."
assert_line --index 2 "Run 'toolbox --help' for usage."
@ -129,9 +131,10 @@ teardown() {
@test "run: Try to run a command in a specific non-existent container" {
create_container other-container
run $TOOLBOX run -c wrong-container true
run --separate-stderr $TOOLBOX run -c wrong-container true
assert_failure
lines=("${stderr_lines[@]}")
assert_line --index 0 "Error: container wrong-container not found"
assert_line --index 1 "Use the 'create' command to create a toolbox."
assert_line --index 2 "Run 'toolbox --help' for usage."
@ -140,57 +143,63 @@ teardown() {
@test "run: Try to run a command in a container based on unsupported distribution" {
local distro="foo"
run $TOOLBOX --assumeyes run --distro "$distro" ls
run --separate-stderr $TOOLBOX --assumeyes run --distro "$distro" ls
assert_failure
lines=("${stderr_lines[@]}")
assert_line --index 0 "Error: invalid argument for '--distro'"
assert_line --index 1 "Distribution $distro is unsupported."
assert_line --index 2 "Run 'toolbox --help' for usage."
assert [ ${#lines[@]} -eq 3 ]
assert [ ${#stderr_lines[@]} -eq 3 ]
}
@test "run: Try to run a command in a container based on Fedora but with wrong version" {
run $TOOLBOX run -d fedora -r foobar ls
run --separate-stderr $TOOLBOX run -d fedora -r foobar ls
assert_failure
lines=("${stderr_lines[@]}")
assert_line --index 0 "Error: invalid argument for '--release'"
assert_line --index 1 "The release must be a positive integer."
assert_line --index 2 "Run 'toolbox --help' for usage."
assert [ ${#lines[@]} -eq 3 ]
assert [ ${#stderr_lines[@]} -eq 3 ]
run $TOOLBOX run --distro fedora --release -3 ls
run --separate-stderr $TOOLBOX run --distro fedora --release -3 ls
assert_failure
lines=("${stderr_lines[@]}")
assert_line --index 0 "Error: invalid argument for '--release'"
assert_line --index 1 "The release must be a positive integer."
assert_line --index 2 "Run 'toolbox --help' for usage."
assert [ ${#lines[@]} -eq 3 ]
assert [ ${#stderr_lines[@]} -eq 3 ]
}
@test "run: Try to run a command in a container based on RHEL but with wrong version" {
run $TOOLBOX run --distro rhel --release 8 ls
run --separate-stderr $TOOLBOX run --distro rhel --release 8 ls
assert_failure
lines=("${stderr_lines[@]}")
assert_line --index 0 "Error: invalid argument for '--release'"
assert_line --index 1 "The release must be in the '<major>.<minor>' format."
assert_line --index 2 "Run 'toolbox --help' for usage."
assert [ ${#lines[@]} -eq 3 ]
assert [ ${#stderr_lines[@]} -eq 3 ]
run $TOOLBOX run --distro rhel --release 8.2foo ls
run --separate-stderr $TOOLBOX run --distro rhel --release 8.2foo ls
assert_failure
lines=("${stderr_lines[@]}")
assert_line --index 0 "Error: invalid argument for '--release'"
assert_line --index 1 "The release must be in the '<major>.<minor>' format."
assert_line --index 2 "Run 'toolbox --help' for usage."
assert [ ${#lines[@]} -eq 3 ]
assert [ ${#stderr_lines[@]} -eq 3 ]
run $TOOLBOX run --distro rhel --release -2.1 ls
run --separate-stderr $TOOLBOX run --distro rhel --release -2.1 ls
assert_failure
lines=("${stderr_lines[@]}")
assert_line --index 0 "Error: invalid argument for '--release'"
assert_line --index 1 "The release must be a positive number."
assert_line --index 2 "Run 'toolbox --help' for usage."
assert [ ${#lines[@]} -eq 3 ]
assert [ ${#stderr_lines[@]} -eq 3 ]
}
@test "run: Try to run a command in a container based on non-default distro without providing a version" {
@ -201,13 +210,14 @@ teardown() {
distro="rhel"
fi
run $TOOLBOX run -d "$distro" ls
run --separate-stderr $TOOLBOX run -d "$distro" ls
assert_failure
lines=("${stderr_lines[@]}")
assert_line --index 0 "Error: option '--release' is needed"
assert_line --index 1 "Distribution $distro doesn't match the host."
assert_line --index 2 "Run 'toolbox --help' for usage."
assert [ ${#lines[@]} -eq 3 ]
assert [ ${#stderr_lines[@]} -eq 3 ]
}
@test "run: Run command exiting with non-zero code in the default container" {
@ -224,26 +234,28 @@ teardown() {
create_default_container
run -127 $TOOLBOX run $cmd
run -127 --separate-stderr $TOOLBOX run $cmd
assert_failure
assert [ $status -eq 127 ]
lines=("${stderr_lines[@]}")
assert_line --index 0 "bash: line 1: exec: $cmd: not found"
assert_line --index 1 "Error: command $cmd not found in container $(get_latest_container_name)"
assert [ ${#lines[@]} -eq 2 ]
assert [ ${#stderr_lines[@]} -eq 2 ]
}
@test "run: Try to run /etc as a command in the deault container" {
create_default_container
run $TOOLBOX run /etc
run --separate-stderr $TOOLBOX run /etc
assert_failure
assert [ $status -eq 126 ]
lines=("${stderr_lines[@]}")
assert_line --index 0 "bash: line 1: /etc: Is a directory"
assert_line --index 1 "bash: line 1: exec: /etc: cannot execute: Is a directory"
assert_line --index 2 "Error: failed to invoke command /etc in container $(get_latest_container_name)"
assert [ ${#lines[@]} -eq 3 ]
assert [ ${#stderr_lines[@]} -eq 3 ]
}
@test "run: Pass down 1 invalid file descriptor" {