-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathvirt_feature_test_base.pm
216 lines (189 loc) · 8.46 KB
/
virt_feature_test_base.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# VIRSH TEST MODULE BASE PACKAGE
#
# Copyright 2022 SUSE LLC
# SPDX-License-Identifier: FSFAP
#
# Summary: This is the base package for virsh test modules, for example,
# tests/virtualization/universal/hotplugging.pm
# tests/virt_autotest/virsh_internal_snapshot.pm
# tests/virt_autotest/virsh_external_snapshot.pm
# and etc.
#
# The elements that author of newly developed feature test can customize
# are:
# 1. $self->{test_results}->{$guest}->{CUSTOMIZED_TEST1}->{status} which can
# be given 'SKIPPED', 'FAILED', 'PASSED', 'SOFTFAILED', 'TIMEOUT' or 'UNKNOWN'.
# 2. $self->{test_results}->{$guest}->{CUSTOMIZED_TEST1}->{test_time} which
# should be given time cost duration in format like 'XXmYYs'.
# 3. $self->{test_results}->{$guest}->{CUSTOMIZED_TEST1}->{error} which can
# be given any customized error message that is suitable to be placed in
# system-err section.
# 4. $self->{test_results}->{$guest}->{CUSTOMIZED_TEST1}->{output} which can
# be given any customized output message that is suitable to be placed in
# system-out section.
# Maintainer: Wayne Chen <[email protected]>, [email protected]
package virt_feature_test_base;
use base "consoletest";
use strict;
use warnings;
use POSIX 'strftime';
use File::Basename;
use Data::Dumper;
use XML::Writer;
use IO::File;
use List::Util 'first';
use testapi;
use utils;
use ipmi_backend_utils;
use Utils::Backends 'use_ssh_serial_console';
use virt_utils;
use virt_autotest::common;
use virt_autotest::utils;
use version_utils qw(is_sle is_alp);
use alp_workloads::kvm_workload_utils;
sub run_test {
die('Please override this subroutine in children modules to run desired tests.');
}
sub reconnect_if_problematic {
enter_cmd "rm -f /root/{commands_history,commands_failure}; echo DONE > /dev/$serialdev";
unless (defined(wait_serial('DONE', timeout => 30))) {
reconnect_when_ssh_console_broken;
alp_workloads::kvm_workload_utils::enter_kvm_container_sh if is_alp;
}
}
sub prepare_run_test {
my $self = shift;
select_console 'sol', await_console => 0;
use_ssh_serial_console;
reconnect_if_problematic;
assert_script_run("history -c");
check_host_health;
virt_utils::cleanup_host_and_guest_logs;
virt_utils::start_monitor_guest_console;
}
sub run {
my ($self) = @_;
$self->prepare_run_test if (!(get_var("TEST", '') =~ /qam/) && (is_xen_host() || is_kvm_host()));
$self->{"start_run"} = time();
$self->run_test;
$self->{"stop_run"} = time();
check_guest_health($_) foreach (keys %virt_autotest::common::guests);
virt_utils::stop_monitor_guest_console if (!(get_var("TEST", '') =~ /qam/) && (is_xen_host() || is_kvm_host()));
#(caller(0))[3] can help pass calling subroutine name into called subroutine
$self->junit_log_provision((caller(0))[3]) if get_var("VIRT_AUTOTEST");
}
sub junit_log_provision {
my ($self, $runsub) = @_;
my $overall_status = eval { $runsub =~ /post_fail_hook/img ? 'FAILED' : 'PASSED' };
$self->analyzeResult($overall_status);
$self->junit_log_params_provision;
###Load instance attributes into %stats
my %stats;
foreach (keys %{$self}) {
if (defined($self->{$_})) {
if (ref($self->{$_}) eq 'HASH') {
%{$stats{$_}} = %{$self->{$_}};
}
elsif (ref($self->{$_}) eq 'ARRAY') {
@{$stats{$_}} = @{$self->{$_}};
}
else {
$stats{$_} = $self->{$_};
}
}
else {
next;
}
}
print "The data to be used for xml generation:", Dumper(\%stats);
my %tc_result = %{$stats{test_results}};
my $xml_result = generateXML_from_data(\%tc_result, \%stats);
script_run "echo \'$xml_result\' > /tmp/output.xml";
save_screenshot;
parse_junit_log("/tmp/output.xml");
}
sub junit_log_params_provision {
my $self = shift;
my $start_time = $self->{"start_run"};
my $stop_time = $self->{"stop_run"};
$self->{"test_time"} = strftime("\%H:\%M:\%S", gmtime($stop_time - $start_time));
if (!version_utils::is_alp) {
$self->{"product_tested_on"} = script_output("cat /etc/issue | grep -io -e \"SUSE.*\$(arch))\" -e \"openSUSE.*[0-9]\"");
} else {
alp_workloads::kvm_workload_utils::exit_kvm_container;
$self->{"product_tested_on"} = script_output(q@cat /etc/os-release |grep PRETTY_NAME | sed 's/PRETTY_NAME=//'@);
alp_workloads::kvm_workload_utils::enter_kvm_container_sh;
}
$self->{"product_name"} = ref($self);
$self->{"package_name"} = ref($self);
}
sub analyzeResult {
my ($self, $status) = @_;
#Initialize all test status counters to zero
#Then count up all counters by the number of tests in corresponding status
my @test_item_status_array = ('pass', 'fail', 'skip', 'softfail', 'timeout', 'unknown');
$self->{$_ . '_nums'} = 0 foreach (@test_item_status_array);
foreach my $guest (keys %virt_autotest::common::guests) {
foreach my $item (keys %{$self->{test_results}->{$guest}}) {
my $item_status = $self->{test_results}->{$guest}->{$item}->{status};
my $test_item_status = first { $item_status =~ /^$_/i } @test_item_status_array;
$self->{$test_item_status . '_nums'} += 1;
}
}
#If test failed at undefined checkpoint, it still needs to be counted in to maintain
#the correctness and effectivenees of entire JUnit log
if ($status eq 'FAILED' && $self->{"fail_nums"} eq '0') {
$self->{"fail_nums"} = '1';
my $uncheckpoint_failure = "";
$uncheckpoint_failure = script_output("cat /root/commands_history | tail -3 | head -1", 60, proceed_on_failure => 1);
$uncheckpoint_failure = "echo 'NOT found the failure from commands history'" if $uncheckpoint_failure eq '';
my @involved_failure_guest = grep { $uncheckpoint_failure =~ /$_/img } (keys %virt_autotest::common::guests);
my $uncheckpoint_failure_guest = "";
if (!scalar @involved_failure_guest) {
$uncheckpoint_failure_guest = "NO SPECIFIC TEST GUEST INVOLVED";
}
else {
$uncheckpoint_failure_guest = join(' ', @involved_failure_guest);
}
diag "The accidental failure happended at: $uncheckpoint_failure involves: $uncheckpoint_failure_guest";
script_run("($uncheckpoint_failure) 2>&1 | tee -a /root/commands_failure", quiet => 1);
my $uncheckpoint_failure_error = script_output("cat /root/commands_failure", type_command => 0, proceed_on_failure => 1, quiet => 1);
$self->{test_results}->{$uncheckpoint_failure_guest}->{$uncheckpoint_failure}->{status} = 'FAILED';
$self->{test_results}->{$uncheckpoint_failure_guest}->{$uncheckpoint_failure}->{error} = $uncheckpoint_failure_error;
}
if ($status eq 'PASSED' and !defined $self->{test_results}) {
$self->{test_results}->{'ALL GUESTS'}->{'ALL TESTS'}->{status} = 'PASSED';
$self->{test_results}->{'ALL GUESTS'}->{'ALL TESTS'}->{error} = 'NONE';
}
}
sub post_fail_hook {
my ($self) = shift;
$self->{"stop_run"} = time();
reconnect_if_problematic;
check_host_health;
virt_utils::stop_monitor_guest_console() if (!(get_var("TEST", '') =~ /qam/) && (is_xen_host() || is_kvm_host()));
#(caller(0))[3] can help pass calling subroutine name into called subroutine
$self->junit_log_provision((caller(0))[3]) if get_var("VIRT_AUTOTEST");
# virt logs are included in next step "virt_utils::collect_host_and_guest_logs"
collect_virt_system_logs() unless get_var("VIRT_AUTOTEST");
virt_utils::collect_host_and_guest_logs('', '', '', "_" . caller 0);
upload_logs("/var/log/clean_up_virt_logs.log");
save_screenshot;
upload_logs("/var/log/guest_console_monitor.log");
save_screenshot;
script_run("rm -f -r /var/log/clean_up_virt_logs.log /var/log/guest_console_monitor.log");
save_screenshot;
$self->upload_coredumps;
save_screenshot;
alp_workloads::kvm_workload_utils::collect_kvm_container_setup_logs if (version_utils::is_alp);
alp_workloads::kvm_workload_utils::enter_kvm_container_sh if is_alp;
}
sub get_virt_disk_and_available_space {
# ensure the available disk space size for virt disk - /var/lib/libvirt
my $virt_disk_name = script_output 'lsblk -rnoPKNAME $(findmnt -nrvoSOURCE /var/lib/libvirt)';
my $virt_available_size = script_output("df -k | grep libvirt | awk '{print \$4}'");
# default available virt disk unit as GiB
$virt_available_size = int($virt_available_size / 1048576);
return ($virt_disk_name, $virt_available_size);
}
1;