Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #18512: variable from command does not always report an error when the command failed #1260

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
157 changes: 140 additions & 17 deletions tests/acceptance/30_generic_methods/variable_string_from_command.cf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#######################################################
#
# Read a file into a string
# Exit with a specific code
#
#######################################################

Expand All @@ -22,40 +22,163 @@ body common control
bundle agent init
{
vars:
"tmp" string => getenv("TEMP", 1024);
"command_1" string => "echo teststring1";
"command_2" string => "/bin/echo teststring2";
"command_3" string => "/bin/false";
"tmp" string => getenv("TEMP", 1024);

# expected_value to "" will check if the variable is well undefined
# Enforce
"var_prefix[0]" string => "my";
"var_name[0]" string => "var0";
"command[0]" string => "echo teststring0";
"status[0]" string => "success";
"expected_value[0]" string => execresult("${command[0]}", "useshell");
"mode[0]" string => "enforce";

"var_prefix[1]" string => "my";
"var_name[1]" string => "var1";
"command[1]" string => "/bin/echo teststring1";
"status[1]" string => "success";
"expected_value[1]" string => execresult("${command[1]}", "useshell");
"mode[1]" string => "enforce";

"var_prefix[2]" string => "my";
"var_name[2]" string => "var2";
"command[2]" string => "/bin/false";
"status[2]" string => "error";
"expected_value[2]" string => "";
"mode[2]" string => "enforce";

# with stderr output and failure command
"var_prefix[3]" string => "my";
"var_name[3]" string => "var3";
"command[3]" string => "cat afilethatdoesnotexists";
"status[3]" string => "error";
"expected_value[3]" string => "";
"mode[3]" string => "enforce";

"var_prefix[4]" string => "my";
"var_name[4]" string => "var4";
"command[4]" string => "echo 'hey' #with a comment";
"status[4]" string => "success";
"expected_value[4]" string => "hey";
"mode[4]" string => "enforce";

# with stderr, stdout and a successfull command
"var_prefix[5]" string => "my";
"var_name[5]" string => "var5";
"command[5]" string => "${tmp}/test5.sh";
"status[5]" string => "success";
"expected_value[5]" string => "stdout";
"mode[5]" string => "enforce";


#Audit
"var_prefix[10]" string => "my";
"var_name[10]" string => "var10";
"command[10]" string => "echo teststring10";
"status[10]" string => "success";
"expected_value[10]" string => execresult("${command[10]}", "useshell");
"mode[10]" string => "audit";

"var_prefix[11]" string => "my";
"var_name[11]" string => "var11";
"command[11]" string => "/bin/echo teststring11";
"status[11]" string => "success";
"expected_value[11]" string => execresult("${command[11]}", "useshell");
"mode[11]" string => "audit";

"var_prefix[12]" string => "my";
"var_name[12]" string => "var12";
"command[12]" string => "/bin/false";
"status[12]" string => "error";
"expected_value[12]" string => "";
"mode[12]" string => "audit";

"var_prefix[13]" string => "my";
"var_name[13]" string => "var13";
"command[13]" string => "cat afilethatdoesnotexists";
"status[13]" string => "error";
"expected_value[13]" string => "";
"mode[13]" string => "audit";

"var_prefix[14]" string => "my";
"var_name[14]" string => "var14";
"command[14]" string => "echo 'hey' #with a comment";
"status[14]" string => "success";
"expected_value[14]" string => "hey";
"mode[14]" string => "audit";

# with stderr, stdout and a successfull command
"var_prefix[15]" string => "my";
"var_name[15]" string => "var15";
"command[15]" string => "${tmp}/test5.sh";
"status[15]" string => "success";
"expected_value[15]" string => "stdout";
"mode[15]" string => "audit";

"indices" slist => getindices("var_prefix");

files:
"${tmp}/test5.sh"
create => "true",
edit_line => insert_lines("echo 'stdout' > /dev/stdout
echo 'stderr' > /dev/stderr"),
perms => mo("755", "root");
}

#######################################################

bundle agent test
{
vars:
"args${init.indices}" slist => { "${init.var_prefix[${init.indices}]}", "${init.var_name[${init.indices}]}", "${init.command[${init.indices}]}" };

methods:
"ph1" usebundle => variable_string_from_command("prefix", "var1", "${init.command_1}");
"ph2" usebundle => variable_string_from_command("prefix", "var2", "${init.command_2}");
"ph3" usebundle => variable_string_from_command("prefix", "var3", "${init.command_3}");
# Enforce
"ph0" usebundle => apply_gm("variable_string_from_command", @{args0}, "${init.status[0]}", "ph0", "${init.mode[0]}");
"ph1" usebundle => apply_gm("variable_string_from_command", @{args1}, "${init.status[1]}", "ph1", "${init.mode[1]}");
"ph2" usebundle => apply_gm("variable_string_from_command", @{args2}, "${init.status[2]}", "ph2", "${init.mode[2]}");
"ph3" usebundle => apply_gm("variable_string_from_command", @{args3}, "${init.status[3]}", "ph3", "${init.mode[3]}");
"ph4" usebundle => apply_gm("variable_string_from_command", @{args4}, "${init.status[4]}", "ph4", "${init.mode[4]}");
"ph5" usebundle => apply_gm("variable_string_from_command", @{args5}, "${init.status[5]}", "ph5", "${init.mode[5]}");

# Audit
"ph10" usebundle => apply_gm("variable_string_from_command", @{args10}, "${init.status[10]}", "ph10", "${init.mode[10]}");
"ph11" usebundle => apply_gm("variable_string_from_command", @{args11}, "${init.status[11]}", "ph11", "${init.mode[11]}");
"ph12" usebundle => apply_gm("variable_string_from_command", @{args12}, "${init.status[12]}", "ph12", "${init.mode[12]}");
"ph13" usebundle => apply_gm("variable_string_from_command", @{args13}, "${init.status[13]}", "ph13", "${init.mode[13]}");
"ph14" usebundle => apply_gm("variable_string_from_command", @{args14}, "${init.status[14]}", "ph14", "${init.mode[14]}");
"ph15" usebundle => apply_gm("variable_string_from_command", @{args15}, "${init.status[15]}", "ph15", "${init.mode[15]}");
}

#######################################################

bundle agent check
{
vars:
"var_name[${init.indices}]" string => "${init.var_prefix[${init.indices}]}.${init.var_name[${init.indices}]}";

classes:

"ok_1" expression => "variable_string_from_command_var1_kept.!variable_string_from_command_var1_repaired.!variable_string_from_command_var1_error";
"ok_2" expression => "variable_string_from_command_var2_kept.!variable_string_from_command_var2_repaired.!variable_string_from_command_var2_error";
"ok_3" expression => "!variable_string_from_command_var3_kept.!variable_string_from_command_var3_repaired.variable_string_from_command_var3_error";
"ok_var1" expression => strcmp("${prefix.var1}", "teststring1");
"ok_var2" expression => strcmp("${prefix.var2}", "teststring2");
"ok_var3" not => isvariable("prefix.var3");
# On error there var must not be created
"must_not_be_defined_${init.indices}" expression => strcmp("${init.expected_value[${init.indices}]}", "");
"error_${init.indices}_not_ok" expression => isvariable("${var_name[${init.indices}]}"),
ifvarclass => "must_not_be_defined_${init.indices}";

# On success, verify the value
"must_be_defined_${init.indices}" not => strcmp("${init.expected_value[${init.indices}]}", "");
"success_${init.indices}_not_ok" not => strcmp("${${var_name[${init.indices}]}}", "${init.expected_value[${init.indices}]}"),
ifvarclass => "must_be_defined_${init.indices}";


"error_not_ok" expression => "error_${init.indices}_not_ok";
"success_not_ok" expression => "success_${init.indices}_not_ok";
"classes_ok" expression => "ph0_ok.ph1_ok.ph2_ok.ph3_ok.ph4_ok.ph5_ok.ph10_ok.ph11_ok.ph12_ok.ph13_ok.ph14_ok.ph15_ok";

"ok" expression => "ok_1.ok_var1.ok_2.ok_var2.ok_3.ok_var3";
"ok" expression => "classes_ok.!error_not_ok.!success_not_ok";

reports:
ok::
"$(this.promise_filename) Pass";
!ok::
"$(this.promise_filename) FAIL";
"Unexpected value found for ${var_name[${init.indices}]}: ${${var_name[${init.indices}]}} instead of ${init.expected_value[${init.indices}]}"
ifvarclass => "error_${init.indices}_not_ok|success_${init.indices}_not_ok";
}
4 changes: 2 additions & 2 deletions tree/30_generic_methods/variable_string_from_command.cf
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ bundle agent variable_string_from_command(variable_prefix, variable_name, comman
# for example when checking promises on the server.
pass1::
# the cariage return is necessary if command ends by a comment
"temp" string => execresult("OUTPUT=${const.dollar}(/bin/true; ${command}
"temp" string => execresult("OUTPUT=${const.dollar}(/bin/true; 2>/dev/null ${command}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are you hiding errors ?

); printf \"%3d\" ${const.dollar}?; echo \"${const.dollar}OUTPUT\"", "useshell");
"temp_length" int => string_length("${temp}");
"raw_output_length" string => eval("${temp_length} - 3", "math", "infix");
"output_length" string => format("%d", "${raw_output_length}");
"raw_code" string => string_head("${temp}", 3);
Fdall marked this conversation as resolved.
Show resolved Hide resolved
"raw_code" string => string_head("${temp}", "3");
"code" string => format("%d", "${raw_code}");

# define the variable within the variable_prefix namespace
Expand Down