2020-07-27 13:35:17 +00:00
|
|
|
#!/usr/bin/env bats
|
|
|
|
|
|
|
|
load 'libs/bats-support/load'
|
|
|
|
load 'libs/bats-assert/load'
|
|
|
|
load 'libs/helpers'
|
|
|
|
|
|
|
|
setup() {
|
2021-11-11 00:46:21 +00:00
|
|
|
_setup_environment
|
2020-07-27 13:35:17 +00:00
|
|
|
cleanup_containers
|
|
|
|
}
|
|
|
|
|
|
|
|
teardown() {
|
|
|
|
cleanup_containers
|
|
|
|
}
|
|
|
|
|
2021-11-07 11:40:42 +00:00
|
|
|
@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)"
|
2020-07-27 13:35:17 +00:00
|
|
|
|
2021-11-07 11:40:42 +00:00
|
|
|
run $TOOLBOX run true
|
2020-07-27 13:35:17 +00:00
|
|
|
|
|
|
|
assert_failure
|
2021-11-07 11:40:42 +00:00
|
|
|
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."
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "run: Try to run a command in the default container when 1 non-default container is present" {
|
|
|
|
local default_container_name="$(get_system_id)-toolbox-$(get_system_version)"
|
|
|
|
|
|
|
|
create_container other-container
|
|
|
|
|
|
|
|
run $TOOLBOX run true
|
|
|
|
|
|
|
|
assert_failure
|
|
|
|
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."
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "run: Try to run a command in a specific non-existent container" {
|
|
|
|
create_container other-container
|
|
|
|
|
|
|
|
run $TOOLBOX run -c wrong-container true
|
|
|
|
|
|
|
|
assert_failure
|
|
|
|
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."
|
2020-07-27 13:35:17 +00:00
|
|
|
}
|
|
|
|
|
2022-07-30 17:38:32 +00:00
|
|
|
@test "run: Try to run a command in a container based on unsupported distribution" {
|
|
|
|
local distro="foo"
|
|
|
|
|
|
|
|
run $TOOLBOX --assumeyes run --distro "$distro" ls
|
|
|
|
|
|
|
|
assert_failure
|
|
|
|
assert_line --index 0 "Error: distribution $distro is unsupported"
|
|
|
|
assert [ ${#lines[@]} -eq 1 ]
|
|
|
|
}
|
|
|
|
|
2021-12-17 14:27:21 +00:00
|
|
|
@test "run: Try to run a command in a container based on Fedora but with wrong version" {
|
2022-08-01 18:38:56 +00:00
|
|
|
run $TOOLBOX run -d fedora -r foobar ls
|
2021-12-17 14:27:21 +00:00
|
|
|
|
|
|
|
assert_failure
|
|
|
|
assert_line --index 0 "Error: invalid argument for '--release'"
|
2022-07-31 08:46:33 +00:00
|
|
|
assert_line --index 1 "Run 'toolbox --help' for usage."
|
|
|
|
assert [ ${#lines[@]} -eq 2 ]
|
2021-12-17 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@test "run: Try to run a command in a container based on non-default distro without providing a version" {
|
|
|
|
local distro="fedora"
|
|
|
|
local system_id="$(get_system_id)"
|
|
|
|
|
|
|
|
if [ "$system_id" = "fedora" ]; then
|
|
|
|
distro="rhel"
|
|
|
|
fi
|
|
|
|
|
|
|
|
run $TOOLBOX run -d "$distro" ls
|
|
|
|
|
|
|
|
assert_failure
|
|
|
|
assert_line --index 0 "Error: release not found for non-default distribution $distro"
|
|
|
|
assert [ ${#lines[@]} -eq 1 ]
|
|
|
|
}
|
|
|
|
|
2020-07-27 13:35:17 +00:00
|
|
|
@test "run: Run echo 'Hello World' inside of the default container" {
|
|
|
|
create_default_container
|
|
|
|
|
2021-07-16 18:32:06 +00:00
|
|
|
run $TOOLBOX --verbose run echo -n "Hello World"
|
2020-07-27 13:35:17 +00:00
|
|
|
|
|
|
|
assert_success
|
2021-07-16 18:32:06 +00:00
|
|
|
assert_line --index $((${#lines[@]}-1)) "Hello World"
|
2020-07-27 13:35:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@test "run: Run echo 'Hello World' inside a container after being stopped" {
|
|
|
|
create_container running
|
|
|
|
|
|
|
|
start_container running
|
|
|
|
stop_container running
|
|
|
|
|
2021-05-26 20:12:18 +00:00
|
|
|
run $TOOLBOX --verbose run --container running echo -n "Hello World"
|
2020-07-27 13:35:17 +00:00
|
|
|
|
|
|
|
assert_success
|
2021-07-16 18:32:06 +00:00
|
|
|
assert_line --index $((${#lines[@]}-1)) "Hello World"
|
2020-07-27 13:35:17 +00:00
|
|
|
}
|
2020-08-10 17:07:17 +00:00
|
|
|
|
|
|
|
@test "run: Run sudo id inside of the default container" {
|
|
|
|
create_default_container
|
|
|
|
|
2021-07-16 18:30:19 +00:00
|
|
|
output="$($TOOLBOX --verbose run sudo id 2>$BATS_TMPDIR/stderr)"
|
|
|
|
status="$?"
|
|
|
|
|
|
|
|
echo "# stderr"
|
|
|
|
cat $BATS_TMPDIR/stderr
|
|
|
|
echo "# stdout"
|
|
|
|
echo $output
|
2020-08-10 17:07:17 +00:00
|
|
|
|
|
|
|
assert_success
|
2021-07-16 18:30:19 +00:00
|
|
|
assert_output --partial "uid=0(root)"
|
2020-08-10 17:07:17 +00:00
|
|
|
}
|
2021-08-30 13:47:45 +00:00
|
|
|
|
|
|
|
@test "run: Run command exiting with zero code in the default container" {
|
|
|
|
create_default_container
|
|
|
|
|
|
|
|
run $TOOLBOX run /bin/sh -c 'exit 0'
|
|
|
|
|
|
|
|
assert_success
|
|
|
|
assert_output ""
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "run: Run command exiting with non-zero code in the default container" {
|
|
|
|
create_default_container
|
|
|
|
|
|
|
|
run $TOOLBOX run /bin/sh -c 'exit 2'
|
|
|
|
assert_failure
|
|
|
|
assert [ $status -eq 2 ]
|
|
|
|
assert_output ""
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "run: Try to run non-existent command in the default container" {
|
|
|
|
local cmd="non-existent-command"
|
|
|
|
|
|
|
|
create_default_container
|
|
|
|
|
test/system: Silence warning with Bats >= 1.7.0
Bats 1.7.0 emits a warning if a command passed to 'run' returns with an
exit code of 127 [1]:
BW01: `run`'s command `/opt/bin/toolbox run non-existent-command`
exited with code 127, indicating 'Command not found'. Use run's
return code checks, e.g. `run -127`, to fix this message.
(from function `run' in file
/usr/lib/bats-core/test_functions.bash, line 299,
in test file test/system/104-run.bats, line 148)
This requires Bats >= 1.5.0, which is present in Fedora >=35, and
supports specifying the exit code as an argument to Bats' 'run'
command [2].
However, bats_require_minimum_version can't be used, because it's
only available from Bats 1.7.0, which is new enough that it's absent
from Fedora 35.
[1] Bats commit c6dc2f88361a4f5b
https://github.com/bats-core/bats-core/issues/547
https://bats-core.readthedocs.io/en/stable/warnings/BW01.html
[2] https://github.com/bats-core/bats-core/pull/367
https://github.com/bats-core/bats-core/pull/507
https://bats-core.readthedocs.io/en/stable/writing-tests.html
[3] Bats commit 71d6b71cebc3d32b
https://github.com/bats-core/bats-core/issues/556
https://bats-core.readthedocs.io/en/stable/warnings/BW02.html
https://github.com/containers/toolbox/pull/1081
2022-07-31 17:16:00 +00:00
|
|
|
run -127 $TOOLBOX run $cmd
|
2021-08-30 13:47:45 +00:00
|
|
|
|
|
|
|
assert_failure
|
|
|
|
assert [ $status -eq 127 ]
|
|
|
|
assert_line --index 0 "/bin/sh: 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 ]
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "run: Try to run /etc as a command in the deault container" {
|
|
|
|
create_default_container
|
|
|
|
|
|
|
|
run $TOOLBOX run /etc
|
|
|
|
|
|
|
|
assert_failure
|
|
|
|
assert [ $status -eq 126 ]
|
|
|
|
assert_line --index 0 "/bin/sh: line 1: /etc: Is a directory"
|
|
|
|
assert_line --index 1 "/bin/sh: 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 ]
|
|
|
|
}
|