Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
ncharles committed Nov 27, 2020
1 parent b2bfb98 commit 3e2b290
Showing 1 changed file with 150 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,117 +22,174 @@ bundle agent init
{
vars:
"tmp" string => getenv("TEMP", 1024);
"file_1" string => "${tmp}/test1.ini";
"reference_file_1" string => "${tmp}/ref1.ini";
"file_1_canon" string => canonify("${file_1}");
"file_2" string => "${tmp}/test2.ini";
"reference_file_2" string => "${tmp}/ref2.ini";
"file_2_canon" string => canonify("${file_2}");
"file_3" string => "${tmp}/test3.ini";
"reference_file_3" string => "${tmp}/ref3.ini";
"file_3_canon" string => canonify("${file_3}");

# First test: ensure that a line is really added into the right section
# with lines already present.
"section_1" string => "section_test1";
"name_1" string => "name";
"value_1" string => "value";
"base_text_up" string => "[section_test1]";
"base_text_down" string => "[section_test2]";
"reference_1" string => "${base_text_up}
${name_1}=${value_1}
${base_text_down}";

# Second test: ensure that if the section does not exist, it will be created
# and the lines added
"section_2" string => "section_test2";
"name_2" string => "name";
"value_2" string => "value";
"reference_2" string => "${base_text_down}
${name_2}=${value_2}
${base_text_up}";

# Third test: ensure that if the line already exists, it will be modified
"section_3" string => "section_test1";
"name_3" string => "name";
"value_3" string => "value";
"new_value_3" string => "newvalue";
"reference_3" string => "[${section_3}]
${name_3}=${new_value_3}";

commands:
# Initialize first test files
"/bin/echo"
args => "\"${reference_1}\" > \"${reference_file_1}\"",
contain => in_shell;
"/bin/echo"
args => "\"${base_text_up}\" > \"${file_1}\"",
contain => in_shell;
"/bin/echo"
args => "\"${base_text_down}\" >> \"${file_1}\"",
contain => in_shell;
# Initialize second test files
"/bin/echo"
args => "\"${reference_2}\" > \"${reference_file_2}\"",
contain => in_shell;
"/bin/echo"
args => "\"${base_text_up}\" > \"${file_2}\"",
contain => in_shell;
# Initialize third test files
"/bin/echo"
args => "\"${reference_3}\" > \"${reference_file_3}\"",
contain => in_shell;
"/bin/echo"
args => "\"${base_text_up}\" > \"${file_3}\"",
contain => in_shell;
"/bin/echo"
args => "\"${name_3}=${value_3}\" >> \"${file_3}\"",
contain => in_shell;



## REPAIRED
# ensure that a line is really added into the right section
# with lines already present.
"file[0]" string => "${tmp}/test0.ini";
"section[0]" string => "section_test1";
"name[0]" string => "name1";
"value[0]" string => "value1";
"section[0]" string => "section_test1";
"status[0]" string => "repaired";
"initial[0]" string => "[section_test1]
[section_test2]";
"expected[0]" string => "[section_test1]
${name[0]}=${value[0]}
[section_test2]";

# ensure that if the section does not exist, it will be created
"file[1]" string => "${tmp}/test1.ini";
"section[1]" string => "section_test1";
"name[1]" string => "name1";
"value[1]" string => "value1";
"section[1]" string => "section_test1";
"status[1]" string => "repaired";
"initial[1]" string => "[section_test2]";
"expected[1]" string => "[section_test1]
${name[1]}=${value[1]}
[section_test2]";


# ensure that if the value is modified if necessary
"file[2]" string => "${tmp}/test2.ini";
"section[2]" string => "section_test2";
"name[2]" string => "name2";
"value[2]" string => "value2";
"section[2]" string => "section_test2";
"status[2]" string => "repaired";
"initial[2]" string => "[section_test2]
name2=value";
"expected[2]" string => "[section_test2]
${name[2]}=${value[2]}";

# ensure that if the value is modified if necessary
# but only in the selected region
"file[3]" string => "${tmp}/test3.ini";
"section[3]" string => "section_test3";
"name[3]" string => "name3";
"value[3]" string => "key?value3";
"section[3]" string => "section_test3";
"status[3]" string => "repaired";
"initial[3]" string => "[section_test3]
${name[3]}=key?value
[section_dummy]
${name[3]}=value";
"expected[3]" string => "[section_test3]
${name[3]}=${value[3]}
[section_dummy]
${name[3]}=value";

# ensure that if the value is modified if necessary
# but only in the selected region
# with = sign in value
"file[4]" string => "${tmp}/test4.ini";
"section[4]" string => "section_test4";
"name[4]" string => "name4";
"value[4]" string => "key=value4";
"section[4]" string => "section_test4";
"status[4]" string => "repaired";
"initial[4]" string => "[section_test4]
${name[4]}=key=value
[section_dummy]
${name[4]}=key";
"expected[4]" string => "[section_test4]
${name[4]}=${value[4]}
[section_dummy]
${name[4]}=key";

# KEPT
# ensure that if the value is NOT modified
# but only in the selected region
# with = sign in value
"file[5]" string => "${tmp}/test5.ini";
"section[5]" string => "section_test5";
"name[5]" string => "name5";
"value[5]" string => "http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=BaseOS&infra=$infra";
"section[5]" string => "section_test5";
"status[5]" string => "success";
"initial[5]" string => "[section_test5]
${name[5]}=${value[5]}
[section_dummy]
${name[5]}=key";
"expected[5]" string => "${initial[5]}";


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

files:
"${file[${indices}]}"
create => "true",
edit_line => insert_lines("${initial[${indices}]}"),
edit_defaults => empty,
unless => strcmp("${indices}", "10");
}

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

bundle agent test
{
vars:
"args${init.indices}" slist => { "${init.file[${init.indices}]}", "${init.section[${init.indices}]}", "${init.name[${init.indices}]}", "${init.value[${init.indices}]}"};

methods:
"ph1" usebundle => file_key_value_present_in_ini_section("${init.file_1}", "${init.section_1}", "${init.name_1}", "${init.value_1}");
"ph2" usebundle => file_key_value_present_in_ini_section("${init.file_2}", "${init.section_2}", "${init.name_2}", "${init.value_2}");
"ph3" usebundle => file_key_value_present_in_ini_section("${init.file_3}", "${init.section_3}", "${init.name_3}", "${init.new_value_3}");
# Enforce
"ph0" usebundle => apply_gm("file_key_value_present_in_ini_section", @{args0}, "${init.status[0]}", "ph0", "enforce" );
"ph1" usebundle => apply_gm("file_key_value_present_in_ini_section", @{args1}, "${init.status[1]}", "ph1", "enforce" );
"ph2" usebundle => apply_gm("file_key_value_present_in_ini_section", @{args2}, "${init.status[2]}", "ph2", "enforce" );
"ph3" usebundle => apply_gm("file_key_value_present_in_ini_section", @{args3}, "${init.status[3]}", "ph3", "enforce" );
"ph4" usebundle => apply_gm("file_key_value_present_in_ini_section", @{args4}, "${init.status[4]}", "ph4", "enforce" );
"ph5" usebundle => apply_gm("file_key_value_present_in_ini_section", @{args5}, "${init.status[5]}", "ph5", "enforce" );
}

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

bundle agent check
{
vars:
# Commands to check that reference files (expected result) are the same
# than the modified files by the generic_method 'file_key_value_present_in_ini_section'
"line_1_exists_test" string => "/usr/bin/diff \"${init.reference_file_1}\" \"${init.file_1}\"";
"line_2_exists_test" string => "/usr/bin/diff \"${init.reference_file_2}\" \"${init.file_2}\"";
"line_3_exists_test" string => "/usr/bin/diff \"${init.reference_file_3}\" \"${init.file_3}\"";
pass1::
"indices" slist => { @{init.indices} };

# function readfile adds an extra trailing newline if there is no trailing newline, too inconsistent
"content[${indices}]" string => execresult("${paths.cat} ${init.file[${indices}]}", "noshell"),
unless => strcmp("${indices}", "10");

classes:
"line_1_exists"
expression => returnszero("${line_1_exists_test}", "noshell"),
ifvarclass => canonify("file_key_value_present_in_ini_section_${init.file_1}_reached");
"line_2_exists"
expression => returnszero("${line_2_exists_test}", "noshell"),
ifvarclass => canonify("file_key_value_present_in_ini_section_${init.file_2}_reached");
"line_3_exists"
expression => returnszero("${line_3_exists_test}", "noshell"),
ifvarclass => canonify("file_key_value_present_in_ini_section_${init.file_3}_reached");

"ok_test1" expression => "line_1_exists.file_key_value_present_in_ini_section_${init.file_1_canon}_ok.!file_1_line_present_in_ini_section_${init.file_1_canon}_error";
"ok_test2" expression => "line_2_exists.file_key_value_present_in_ini_section_${init.file_2_canon}_ok.!file_2_line_present_in_ini_section_${init.file_2_canon}_error";
"ok_test3" expression => "line_3_exists.file_key_value_present_in_ini_section_${init.file_3_canon}_ok.!file_3_line_present_in_ini_section_${init.file_3_canon}_error";
"ok" and => {"ok_test1","ok_test2","ok_test3"};
"pass3" expression => "pass2";
"pass2" expression => "pass1";
"pass1" expression => "any";

pass2::
# file 10 is non existant
"content_ok_${indices}" expression => and(
strcmp("${content[${indices}]}", "${init.expected[${indices}]}"),
fileexists("${init.file[${indices}]}")
),
unless => strcmp("${indices}", "10");
"content_ok_10" expression => "any";

"content_not_ok" expression => "!content_ok_${indices}";
"classes_ok" expression => "ph0_ok.ph1_ok.ph2_ok.ph3_ok.ph4_ok.ph5_ok";
"ok" expression => "!content_not_ok.classes_ok";


reports:
ok::
pass3::
"###########################
ERROR test ${indices} in
${init.file[${indices}]}
EXPECTED:
${init.expected[${indices}]}
---------------------------
FOUND:
${content[${indices}]}
###########################"
ifvarclass => "!content_ok_${indices}";

pass3.ok::
"$(this.promise_filename) Pass";
!ok::
pass3.!ok::
"$(this.promise_filename) FAIL";
!line_1_exists::
"diff command doesn't return 0 for command: ${line_1_exists_test}";
}

0 comments on commit 3e2b290

Please sign in to comment.