Skip to content

Commit

Permalink
Refactor subroutine which creates xml result file
Browse files Browse the repository at this point in the history
Move the parsing and the creation of the xml out of the SUT. As such we do not
repeat the script_run script which might make the runtime a bit faster
overall.
In addition the patch adds date and build elements in the xml as additional info
of the test results which it should help in the review of the tests.

Signed-off-by: Ioannis Bonatakis <[email protected]>
  • Loading branch information
b10n1k committed Oct 23, 2023
1 parent 13bab22 commit 24f1953
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 46 deletions.
53 changes: 25 additions & 28 deletions lib/utils.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ use zypper qw(wait_quit_zypper);
use Storable qw(dclone);
use Getopt::Long qw(GetOptionsFromString);
use File::Basename;
use XML::LibXML;

our @EXPORT = qw(
generate_results
pars_results
create_xml_results
check_console_font
clear_console
type_string_slow
Expand Down Expand Up @@ -2627,54 +2628,50 @@ sub generate_results {
return %results;
}

=head2 pars_results
pars_results();
=head2 create_xml_results
create_xml_results();
Takes C<test> as an argument. C<test> is an array of hashes which contain the
test results. They usually are generated by C<generate_results>. Those are
parsed and create the junit xml representation.
=cut

sub pars_results {
sub create_xml_results {
my ($testsuite, $xmlfile, @test) = @_;

# check if there are some single test failing
# and if so, make sure the whole testsuite will fail
my $fail_check = 0;
for my $i (@test) {
if ($i->{result} eq 'FAIL') {
$fail_check++;
}
}
my $dom = XML::LibXML::Document->new('1.0', 'utf-8');
my $root = $dom->createElement('testsuite');
$root->setAttribute(name => "$testsuite");
my $date_elem = $dom->createElement('date');
$date_elem->appendTextNode(`date +"%m/%d/%Y"`);
my $build_elem = $dom->createElement('build');
$build_elem->appendTextNode(get_required_var('BUILD'));
$root->appendChild($build_elem);
$root->appendChild($date_elem);

if ($fail_check > 0) {
script_run(qq{echo "<testsuite name='$testsuite' errors='1'>" >> $xmlfile});
} else {
script_run(qq{echo "<testsuite name='$testsuite'>" >> $xmlfile});
}

# parse all results and provide expected xml file
for my $i (@test) {
my $tc_elem = $dom->createElement('testcase');
$tc_elem->setAttribute(name => "$i->{test}");
if ($i->{result} eq 'FAIL') {
script_run("echo \"<testcase name='$i->{test}' errors='1'>\" >> $xmlfile");
} else {
script_run("echo \"<testcase name='$i->{test}'>\" >> $xmlfile");
$tc_elem->setAttribute(error => '1');
}
script_run("echo \"<system-out>\" >> $xmlfile");
script_run("echo $i->{description} >> $xmlfile");
script_run("echo \"</system-out>\" >> $xmlfile");
script_run("echo \"</testcase>\" >> $xmlfile");
my $description_elem = $dom->createElement('system-out');
$description_elem->appendTextNode($i->{description});
$tc_elem->appendChild($description_elem);
$root->appendChild($tc_elem);
}
script_run("echo \"</testsuite>\" >> $xmlfile");
$dom->setDocumentElement($root);
$dom->toFile(hashed_string($xmlfile), 1);
assert_script_run('curl -v ' . autoinst_url("/files/" . $xmlfile) . " -o /tmp/$xmlfile");
}

our @all_tests_results;

=head2 test_case
test_case($name, $description, $result);
C<test_case> can produce a data_structure which C<pars_results> can utilize.
C<test_case> can produce a data_structure which C<create_xml_results> can utilize.
Using C<test_case> in an OpenQA module you are able to /name/ and describe
the whole test as subtasks, in a XUnit format.
Expand Down
4 changes: 2 additions & 2 deletions tests/hpc/conman.pm
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ sub run ($self) {

sub post_run_hook ($self) {
select_serial_terminal;
pars_results('HPC conman tests', $file, @all_tests_results);
parse_extra_log('XUnit', $file);
create_xml_results('HPC conman tests', $file, @all_tests_results);
parse_extra_log('XUnit', "/tmp/$file");
$self->SUPER::post_run_hook();
}
sub post_fail_hook ($self) {
Expand Down
4 changes: 2 additions & 2 deletions tests/hpc/genders.pm
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ sub run ($self) {
}

sub post_run_hook ($self) {
pars_results('HPC genders tests', $file, @all_tests_results);
parse_extra_log('XUnit', $file);
create_xml_results('HPC genders tests', $file, @all_tests_results);
parse_extra_log('XUnit', "/tmp/$file");
$self->SUPER::post_run_hook();
}

Expand Down
4 changes: 2 additions & 2 deletions tests/hpc/mrsh_master.pm
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ sub run {
}

sub post_run_hook ($self) {
pars_results('HPC mrsh tests', $file, @all_tests_results);
parse_extra_log('XUnit', $file);
create_xml_results('HPC mrsh tests', $file, @all_tests_results);
parse_extra_log('XUnit', "/tmp/$file");
$self->SUPER::post_run_hook();
}
sub test_flags {
Expand Down
4 changes: 2 additions & 2 deletions tests/hpc/munge_master.pm
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ sub run ($self) {
}

sub post_run_hook ($self) {
pars_results('HPC pdsh tests', $file, @all_tests_results);
parse_extra_log('XUnit', $file);
create_xml_results('HPC pdsh tests', $file, @all_tests_results);
parse_extra_log('XUnit', "/tmp/$file");
$self->SUPER::post_run_hook();
}

Expand Down
4 changes: 2 additions & 2 deletions tests/hpc/pdsh_slave.pm
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ sub test_flags ($self) {
}

sub post_run_hook ($self) {
pars_results('HPC pdsh tests', $file, @all_tests_results);
parse_extra_log('XUnit', $file);
create_xml_results('HPC pdsh tests', $file, @all_tests_results);
parse_extra_log('XUnit', "/tmp/$file");
$self->SUPER::post_run_hook();
}

Expand Down
4 changes: 2 additions & 2 deletions tests/hpc/powerman.pm
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ EOF
}

sub post_run_hook ($self) {
pars_results('HPC powerman tests', $file, @all_tests_results);
parse_extra_log('XUnit', $file);
create_xml_results('HPC powerman tests', $file, @all_tests_results);
parse_extra_log('XUnit', "/tmp/$file");
$self->SUPER::post_run_hook();
}

Expand Down
4 changes: 2 additions & 2 deletions tests/hpc/rasdaemon.pm
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ sub run {
}

sub post_run_hook ($self) {
pars_results('HPC rasdaemon tests', $file, @all_tests_results);
parse_extra_log('XUnit', $file);
create_xml_results('HPC rasdaemon tests', $file, @all_tests_results);
parse_extra_log('XUnit', "/tmp/$file");
$self->SUPER::post_run_hook();
}

Expand Down
4 changes: 2 additions & 2 deletions tests/hpc/slurm_master.pm
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ sub run_tests ($slurm_conf) {
push(@all_tests_results, run_ha_tests());
}

pars_results('HPC slurm tests', $xmlfile, @all_tests_results);
parse_extra_log('XUnit', $xmlfile);
create_xml_results('HPC slurm tests', $xmlfile, @all_tests_results);
parse_extra_log('XUnit', "/tmp/$xmlfile");
}

########################################
Expand Down
4 changes: 2 additions & 2 deletions tests/hpc/ww4.pm
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ sub test_flags ($self) {

sub post_run_hook ($self) {
record_info "post_run", "hook started";
pars_results('HPC warewulf4 controller tests', $file, @all_tests_results);
parse_extra_log('XUnit', $file);
create_xml_results('HPC warewulf4 controller tests', $file, @all_tests_results);
parse_extra_log('XUnit', "/tmp/$file");
$self->upload_service_log('warewulfd');
save_and_upload_log('cat /etc/hosts', "/tmp/hostfile");
save_and_upload_log('ip a', "/tmp/controller_network");
Expand Down

0 comments on commit 24f1953

Please sign in to comment.