kyua: Pass unprivileged user config prop to ATF using all known names

Kyua and ATF speak different naming styles. In this case, the
unprivileged user property can be named with underscore on the Kyua
side, and with a hyphen on the ATF side. Sometimes it is not obvious
which style should be used in which situation. For instance, a test case
may require this configuration property being set using require.config.
Also, a test case may want to read the property using something like
atf_tc_get_config_var(). Which names should be used in these cases?
From the perspective of the original code, it is expected to be this:
    require.config unprivileged-user
    atf_tc_get_config_var(tc, "unprivileged-user")

But, as long as Kyua is the main interface, its users expect to work
with kyua.conf(5), which says that it must be named as unprivileged_user
(with underscore). As a result, test authors tend to do this instead:
    require.config unprivileged_user
    atf_tc_get_config_var(tc, "unprivileged_user")

Kyua already has hacks to understand both unprivileged_user and
unprivileged-user coming from require.config. And this patch covers the
missing second part -- make Kyua pass both names back to ATF as two
identical configuration properties named different ways.

Reviewed by:	ngie, asomers
MFC after:	2 weeks
Differential Revision:	https://reviews.freebsd.org/D49039

(cherry picked from commit 51a8eb6410461c94c8e0f2b59e3417cfb5d7da75)
This commit is contained in:
Igor Ostapenko 2025-02-23 10:41:58 +00:00
parent 1245f6e348
commit 40a8746a77
4 changed files with 54 additions and 3 deletions

View file

@ -1632,7 +1632,10 @@ scheduler::generate_config(const config::tree& user_config,
if (user_config.is_set("unprivileged_user")) {
const passwd::user& user =
user_config.lookup< engine::user_node >("unprivileged_user");
// The property is duplicated using both ATF and Kyua naming styles
// for better UX.
props["unprivileged-user"] = user.name;
props["unprivileged_user"] = user.name;
}
return props;

View file

@ -1192,6 +1192,7 @@ ATF_TEST_CASE_BODY(generate_config__some_matches)
config::properties_map exp_props;
exp_props["unprivileged-user"] = "nobody";
exp_props["unprivileged_user"] = "nobody";
exp_props["var1"] = "value 1";
ATF_REQUIRE_EQ(exp_props,

View file

@ -596,6 +596,48 @@ EOF
}
utils_test_case config_unprivileged_user
config_unprivileged_user_body() {
cat >"my-config" <<EOF
syntax(2)
unprivileged_user = "nobody"
EOF
cat >Kyuafile <<EOF
syntax(2)
atf_test_program{name="config1", test_suite="suite1"}
EOF
utils_cp_helper config config1
CONFIG_VAR_FILE="$(pwd)/cookie"; export CONFIG_VAR_FILE
if [ -f "${CONFIG_VAR_FILE}" ]; then
atf_fail "Cookie file already created; test case list may have gotten" \
"a bad configuration"
fi
CONFIG_VAR_NAME="unprivileged-user"; export CONFIG_VAR_NAME
atf_check -s exit:1 -o ignore -e ignore kyua -c my-config test config1
[ -f "${CONFIG_VAR_FILE}" ] || \
atf_fail "Cookie file not created; test case list did not get" \
"configuration variables"
value="$(cat "${CONFIG_VAR_FILE}")"
[ "${value}" = "nobody" ] || \
atf_fail "Invalid value (${value}) in cookie file; test case list did" \
"not get the correct configuration variables"
rm "${CONFIG_VAR_FILE}"
CONFIG_VAR_NAME="unprivileged_user"; export CONFIG_VAR_NAME
atf_check -s exit:1 -o ignore -e ignore kyua -c my-config test config1
[ -f "${CONFIG_VAR_FILE}" ] || \
atf_fail "Cookie file not created; test case list did not get" \
"configuration variables"
value="$(cat "${CONFIG_VAR_FILE}")"
[ "${value}" = "nobody" ] || \
atf_fail "Invalid value (${value}) in cookie file; test case list did" \
"not get the correct configuration variables"
}
utils_test_case store_contents
store_contents_body() {
utils_install_stable_test_wrapper
@ -1042,6 +1084,7 @@ atf_init_test_cases() {
atf_add_test_case only_load_used_test_programs
atf_add_test_case config_behavior
atf_add_test_case config_unprivileged_user
atf_add_test_case store_contents
atf_add_test_case results_file__ok

View file

@ -34,12 +34,16 @@
ATF_TEST_CASE(get_variable);
ATF_TEST_CASE_HEAD(get_variable)
{
const char* varname = ::getenv("CONFIG_VAR_NAME");
if (varname == NULL) {
varname = "the-variable";
}
const char* output = ::getenv("CONFIG_VAR_FILE");
if (output == NULL) {
set_md_var("require.config", "the-variable");
set_md_var("require.config", varname);
} else {
if (has_config_var("the-variable")) {
atf::utils::create_file(output, get_config_var("the-variable") +
if (has_config_var(varname)) {
atf::utils::create_file(output, get_config_var(varname) +
std::string("\n"));
} else {
atf::utils::create_file(output, "NOT DEFINED\n");