Files
Kir Kolyshkin 07cd18c9c9 tests/int: use assert for substring and regexp comparisons
The point of the assert helpers is that a failed test says what it
wanted and what it got, so leaving most comparisons as bare [[ ]]
defeats it.

Convert the rest of the checks on $output and $lines. Those using a glob
to look for a substring become --partial, the rest become --regexp (the
patterns are translated to extended regular expressions, and anchored
where the glob was).

The few remaining [[ ]] are not assertions: one is an if condition, and
another one is the left hand side of ||, offering an alternative to the
assert that follows it.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2026-10-02 14:47:43 -07:00

135 lines
3.6 KiB
Bash

#!/usr/bin/env bats
# shellcheck disable=SC2016
# This disables the check for shell variables inside single quotes
# We do that all the time in this file, as we are testing env vars.
load helpers
function setup() {
setup_busybox
}
function teardown() {
teardown_bundle
}
# Several of these tests are inspired on regressions caught by Docker, besides other tests that
# check the behavior we already had in runc:
# https://github.com/moby/moby/blob/843e51459f14ebc964d349eba1013dc8a3e9d52e/integration-cli/docker_cli_links_test.go#L197-L204
# https://github.com/moby/moby/blob/843e51459f14ebc964d349eba1013dc8a3e9d52e/integration-cli/docker_cli_run_test.go#L822-L843
#
@test "non-empty HOME env is used" {
update_config ' .process.env += ["HOME=/override"]'
update_config ' .process.args += ["-c", "echo $HOME"]'
run -0 runc run test_busybox
assert_line --index 0 '/override'
}
@test "empty HOME env var is overridden" {
update_config ' .process.env += ["HOME="]'
update_config ' .process.args += ["-c", "echo $HOME"]'
run -0 runc run test_busybox
assert_line --index 0 '/root'
}
@test "empty HOME env var is overridden with multiple overrides" {
update_config ' .process.env += ["HOME=/override", "HOME="]'
update_config ' .process.args += ["-c", "echo $HOME"]'
run -0 runc run test_busybox
assert_line --index 0 '/root'
}
@test "env var HOME is set only once" {
# env will show if an env var is set multiple times.
update_config ' .process.args = ["env"]'
update_config ' .process.env = ["HOME=", "PATH=/usr/bin:/bin"]'
run -0 runc run test_busybox
# There should be 2 words/env-vars: HOME and PATH.
[ "$(wc -w <<<"$output")" -eq 2 ]
}
@test "env var override is set only once" {
# env will show if an env var is set multiple times.
update_config ' .process.args = ["env"]'
update_config ' .process.env = ["ONE=two", "ONE=", "PATH=/usr/bin:/bin"]'
run -0 runc run test_busybox
# There should be 3 words/env-vars: ONE, PATH and HOME.
[ "$(wc -w <<<"$output")" -eq 3 ]
}
@test "env var override" {
update_config ' .process.env += ["ONE=two", "ONE=three"]'
update_config ' .process.args += ["-c", "echo ONE=\"$ONE\""]'
run -0 runc run test_busybox
assert_line --index 0 "ONE=three"
}
@test "env var with new-line is honored" {
update_config ' .process.env = ["NEW_LINE_ENV=\n", "PATH=/usr/bin:/bin"]'
update_config ' .process.args = ["env"]'
run -0 runc run test_busybox
# There should be 4 lines
# NEW_LINE is a \n and when printed, it takes another line:
# 1. HOME=...
# 2. PATH=...
# 3. NEW_LINE_ENV=
# 4.
#
[ "$(wc -l <<<"$output")" -eq 4 ]
}
# Check that runc exec -p process.json takes env from
# process.json only, not from config.json, and that HOME
# is always set.
@test "env [runc exec -p]" {
run -0 runc run -d --console-socket "$CONSOLE_SOCKET" test_busybox
cat <<_EOF_ >process.json
{
"terminal": false,
"args": [
"/bin/env"
],
"cwd": "/",
"env": [
"FOO=bar"
]
}
_EOF_
run -0 runc exec -p process.json test_busybox
# Env should have entries from process.json.
assert_output --partial 'FOO=bar'
# ...and HOME set from container's /etc/passwd.
assert_output --partial 'HOME=/root'
# Env should NOT contain entries from config.json.
refute_output --partial 'TERM='
refute_output --partial 'PATH='
}
@test "env HOME is set for runc exec -p with no process.env" {
run -0 runc run -d --console-socket "$CONSOLE_SOCKET" test_busybox
# "env" is not set.
cat <<_EOF_ >process.json
{
"args": ["/bin/env"],
"cwd": "/"
}
_EOF_
run -0 runc exec -p process.json test_busybox
# Env should have HOME set from container's /etc/passwd.
assert_output --partial 'HOME=/root'
}