forked from os-autoinst/os-autoinst-distri-opensuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpower_action_utils.pm
469 lines (387 loc) · 17.6 KB
/
power_action_utils.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
=head1 power_action_utils
The module provides base and helper functions for powering off or rebooting a machine under test.
=cut
# SUSE's openQA tests
#
# Copyright 2018-2019 SUSE LLC
# SPDX-License-Identifier: FSFAP
# Summary: The module provides base and helper functions for powering off or rebooting a machine under test.
# Maintainer: Oleksandr Orlov <oorlov@suse.de>
package power_action_utils;
use base Exporter;
use Exporter;
use strict;
use warnings;
use utils;
use testapi;
use Utils::Architectures;
use Utils::Backends;
use version_utils qw(is_sle is_leap is_opensuse is_tumbleweed is_vmware is_jeos);
use Carp 'croak';
our @EXPORT = qw(
prepare_system_shutdown
reboot_x11
poweroff_x11
power_action
assert_shutdown_and_restore_system
assert_shutdown_with_soft_timeout
check_bsc1215132
);
=head2 prepare_system_shutdown
prepare_system_shutdown();
Need to kill ssh connection with backends like ipmi, spvm, pvm_hmc, s390x.
For s390_zkvm or xen, assign console($vnc_console) with C<disable_vnc_stalls>
and assign console('svirt') with C<stop_serial_grab>.
$vnc_console get required variable 'SVIRT_VNC_CONSOLE' before assignment.
=cut
sub prepare_system_shutdown {
# kill the ssh connection before triggering reboot
console('root-ssh')->kill_ssh if get_var('BACKEND', '') =~ /ipmi|spvm|pvm_hmc/;
if (is_s390x) {
if (is_backend_s390x) {
# kill serial ssh connection (if it exists)
eval { console('iucvconn')->kill_ssh unless get_var('BOOT_EXISTING_S390', ''); };
diag('ignoring already shut down console') if ($@);
}
console('installation')->disable_vnc_stalls;
}
if (check_var('VIRSH_VMM_FAMILY', 'xen') || get_var('S390_ZKVM')) {
my $vnc_console = get_required_var('SVIRT_VNC_CONSOLE');
console($vnc_console)->disable_vnc_stalls;
console('svirt')->stop_serial_grab;
}
return undef;
}
=head2 reboot_x11
reboot_x11();
Reboot from a desktop session and handle authentication scenarios during shutdown.
Run C<prepare_system_shutdown> if shutdown needs authentification.
=cut
sub reboot_x11 {
my ($self) = @_;
wait_still_screen;
if (check_var('DESKTOP', 'gnome')) {
# For systems with GNOME 40+, use mouse click instead of 'ctrl-alt-delete'.
if (is_tumbleweed || is_sle('>=15-SP4') || is_leap('>=15.4')) {
assert_and_click('reboot-power-icon');
assert_and_click('reboot-power-menu');
assert_and_click('reboot-click-restart');
} else {
send_key_until_needlematch 'logoutdialog', 'ctrl-alt-delete', 8, 10; # reboot
}
my $repetitions = assert_and_click_until_screen_change 'logoutdialog-reboot-highlighted';
record_soft_failure 'poo#19082' if ($repetitions > 0);
if (get_var("SHUTDOWN_NEEDS_AUTH")) {
assert_screen 'shutdown-auth';
wait_still_screen(3); # 981299#c41
type_string $testapi::password, max_interval => 5;
wait_still_screen(3); # 981299#c41
if (get_var('REBOOT_DEBUG')) {
wait_screen_change {
# Extra assert_and_click (with right click) to check the correct number of characters is typed and open up the 'show text' option
assert_and_click('reboot-auth-typed', button => 'right');
};
wait_screen_change {
# Click the 'Show Text' Option to enable the display of the typed text
assert_and_click 'reboot-auth-showtext';
};
# Check the password is correct
assert_screen 'reboot-auth-correct-password';
}
# we need to kill ssh for iucvconn here,
# because after pressing return, the system is down
prepare_system_shutdown;
send_key 'ret'; # Confirm
}
}
elsif (check_var('DESKTOP', 'kde')) {
# Open the reboot/logout greeter and select the reboot option
send_key 'ctrl-alt-delete';
assert_and_click 'sddm_reboot_option_btn';
}
else {
die 'Unhandled desktop';
}
}
=head2 poweroff_x11
Power off desktop.
Handle each desktop differently for kde, gnome, xfce, lxde, lxqt, enlightenment, awesome, mate, minimalx.
Work around issue with CD-ROM pop-up: bsc#1137230 and make sure that s390 SUT shutdown correctly.
=cut
sub poweroff_x11 {
my ($self) = @_;
wait_still_screen;
if (check_var("DESKTOP", "kde")) {
send_key "ctrl-alt-delete"; # shutdown
assert_screen_with_soft_timeout('logoutdialog', timeout => 90, soft_timeout => 15, bugref => 'bsc#1091933');
assert_and_click 'sddm_shutdown_option_btn';
}
if (check_var("DESKTOP", "gnome")) {
send_key "ctrl-alt-delete";
if (is_sle('=12-SP5')) {
unless (check_screen 'logoutdialog', 15) {
record_soft_failure 'poo#136901 - "ctrl-alt-del" key can not work sometimes';
# Use mouse click 'system power button' to shutdown the system
assert_and_click 'gnome-system_power_btn';
assert_and_click 'gnome-power_action_btn';
}
}
else {
assert_screen 'logoutdialog', 15;
}
assert_and_click 'gnome-shell_shutdown_btn';
if (get_var("SHUTDOWN_NEEDS_AUTH")) {
assert_screen 'shutdown-auth';
type_password;
# we need to kill all open ssh connections before the system shuts down
prepare_system_shutdown;
send_key "ret";
# Switch to sol console to observe system shutdown on IPMI backend
select_console 'sol', await_console => 0 if is_ipmi;
}
}
if (check_var("DESKTOP", "xfce")) {
for (1 .. 5) {
send_key "alt-f4"; # opens log out popup after all windows closed
}
assert_screen 'logoutdialog';
wait_screen_change { type_string "\t\t" }; # select shutdown
# assert_screen 'test-shutdown-1', 3;
send_key 'ret';
}
if (check_var("DESKTOP", "lxde")) {
# opens logout dialog
x11_start_program('lxsession-logout', target_match => 'logoutdialog');
send_key "ret";
}
if (check_var("DESKTOP", "lxqt")) {
# Handle bsc#1137230
if (check_screen 'authorization_failed') {
record_soft_failure 'bsc#1137230 - "Authorization failed" pop-up shown';
assert_and_click 'authorization_failed_ok_btn';
}
elsif (check_screen 'authentication-required') {
record_soft_failure 'bsc#1137230 - "Authentication required" pop-up shown';
assert_and_click 'authentication-required_cancel_btn';
}
# opens logout dialog
x11_start_program('Shutdown', target_match => [qw(authentication-required authorization_failed lxqt_shutdowndialog)], match_timeout => 60);
# we have typing issue because of poor performance, to record this if happens.
# Double check for bsc#1137230
if (match_has_tag 'authorization_failed' || 'authentication-required') {
croak "bsc#1137230, CD-ROM pop-up displayed at shutdown, authorization failed";
}
elsif (match_has_tag 'lxqt_shutdowndialog') {
assert_and_click 'shutdowndialog-yes';
}
}
if (check_var("DESKTOP", "enlightenment")) {
send_key "ctrl-alt-delete"; # shutdown
assert_screen 'logoutdialog', 15;
assert_and_click 'enlightenment_shutdown_btn';
}
if (check_var('DESKTOP', 'awesome')) {
assert_and_click 'awesome-menu-main';
assert_and_click 'awesome-menu-system';
assert_and_click 'awesome-menu-shutdown';
}
if (check_var("DESKTOP", "mate")) {
send_key "ctrl-alt-delete"; # shutdown
assert_and_click 'mate_shutdown_btn';
}
if (check_var("DESKTOP", "minimalx")) {
send_key "ctrl-alt-delete"; # logout dialog
assert_screen 'logoutdialog', 10;
send_key "alt-d"; # shut_d_own
assert_screen 'logout-confirm-dialog', 10;
send_key "alt-o"; # _o_k
}
}
=head2 handle_livecd_reboot_failure
handle_livecd_reboot_failure();
Handle a potential failure on a live CD related to boo#993885 that the reboot
action from a desktop session does not work and we are stuck on the desktop.
=cut
sub handle_livecd_reboot_failure {
mouse_hide;
wait_still_screen;
assert_screen([qw(generic-desktop-after_installation grub2)]);
if (match_has_tag('generic-desktop-after_installation')) {
record_soft_failure 'boo#993885 Kde-Live net installer does not reboot after installation';
select_console 'install-shell';
enter_cmd "reboot";
save_screenshot;
}
}
=head2 power_action
power_action($action [,observe => $observe] [,keepconsole => $keepconsole] [,textmode => $textmode] [,force => boolean ]);
Executes the selected power action (e.g. poweroff, reboot).
If C<$observe> is set, the function expects that the specified C<$action> was already executed by
another actor and the function just makes sure the system shuts down, restarts etc. properly.
C<$keepconsole> prevents a console change, which we do by default to make sure that a system with a GUI
desktop which was in text console at the time of C<power_action> call, is switched to the expected
console, that is 'root-console' for textmode, 'x11' otherwise. The actual execution happens in a shell
for textmode or with GUI commands otherwise unless explicitly overridden by setting C<$textmode> to either 0 or 1.
C<$force> sets force option to reboot command in textmode.
=cut
sub power_action {
my ($action, %args) = @_;
$args{observe} //= 0;
$args{keepconsole} //= 0;
$args{textmode} //= check_var('DESKTOP', 'textmode');
$args{first_reboot} //= 0;
$args{force} //= 0;
die "'action' was not provided" unless $action;
prepare_system_shutdown;
unless ($args{keepconsole}) {
select_console $args{textmode} ? 'root-console' : 'x11';
}
unless ($args{observe}) {
if ($args{textmode}) {
$args{force} ? enter_cmd "$action -f" : enter_cmd "$action";
}
elsif ($action eq 'reboot') {
reboot_x11;
}
elsif ($action eq 'poweroff') {
if ((is_s390x || is_backend_s390x) && check_var("DESKTOP", "gnome")) {
send_key "ctrl-alt-delete";
assert_screen 'logoutdialog', 15;
assert_and_click 'gnome-shell_shutdown_btn';
if (get_var("SHUTDOWN_NEEDS_AUTH")) {
assert_screen 'shutdown-auth';
type_password;
# we need to take care of test module scheduled after shutdown like 'svirt_upload_assets'
send_key "ret" && return 1 if is_backend_s390x;
send_key "ret" if is_s390x;
}
}
else {
poweroff_x11;
}
}
}
my $soft_fail_data;
my $shutdown_timeout = 60;
if (is_sle('15-sp1+') && check_var('DESKTOP', 'textmode') && ($action eq 'poweroff')) {
$soft_fail_data = {bugref => 'bsc#1158145', soft_timeout => 60, timeout => $shutdown_timeout *= 3};
}
# Shutdown takes longer than 60 seconds on SLE12 SP4 and SLE 15
if (is_sle('12+') && check_var('DESKTOP', 'gnome') && ($action eq 'poweroff')) {
$soft_fail_data = {bugref => 'bsc#1055462', soft_timeout => 60, timeout => $shutdown_timeout *= 3};
}
# The timeout is increased as shutdown takes longer on Live CD
if (get_var('LIVECD')) {
$soft_fail_data = {soft_timeout => 60, timeout => $shutdown_timeout *= 4, bugref => "bsc#1096241"};
}
if (get_var("OFW") && check_var('DISTRI', 'opensuse') && check_var('DESKTOP', 'gnome') && get_var('PUBLISH_HDD_1')) {
$soft_fail_data = {bugref => 'bsc#1057637', soft_timeout => 60, timeout => $shutdown_timeout *= 3};
}
# Sometimes QEMU CD-ROM pop-up is displayed on shutdown, see bsc#1137230
if (is_opensuse && check_screen 'qemu-cd-rom-authentication-required') {
$soft_fail_data = {bugref => 'bsc#1137230', soft_timeout => 60, timeout => $shutdown_timeout *= 5};
}
# no need to redefine the system when we boot from an existing qcow image
# Do not redefine if autoyast or s390 zKVM reboot, as did initial reboot already
if (check_var('VIRSH_VMM_FAMILY', 'kvm')
|| check_var('VIRSH_VMM_FAMILY', 'xen')
|| (get_var('S390_ZKVM') && !get_var('BOOT_HDD_IMAGE') && !get_var('AUTOYAST') && $args{first_reboot}))
{
assert_shutdown_and_restore_system($action, $shutdown_timeout *= 3);
}
else {
if (check_var('DESKTOP', 'minimalx') && check_screen('shutdown-wall', timeout => 30)) {
record_soft_failure 'bsc#1076817 manually shutting down';
select_console 'root-console';
systemctl 'poweroff';
}
assert_shutdown_with_soft_timeout($soft_fail_data) if ($action eq 'poweroff');
# We should only reset consoles if the system really rebooted.
# Otherwise the next select_console will check for a login prompt
# instead of handling the still logged in system.
handle_livecd_reboot_failure if get_var('LIVECD') && !get_var('PATCH_BEFORE_MIGRATION') && $action eq 'reboot';
# Look aside before we are sure 'sut' console on VMware is ready, see poo#47150
select_console('svirt') if is_vmware && $action eq 'reboot' && !get_var('UEFI');
reset_consoles;
if ((check_var('VIRSH_VMM_FAMILY', 'xen') || get_var('S390_ZKVM')) && $action ne 'poweroff') {
console('svirt')->start_serial_grab;
}
# When 'sut' is ready, select it
# GRUB's serial terminal configuration relies on installation/add_serial_console.pm
if (is_vmware && $action eq 'reboot' && !get_var('UEFI')) {
die 'GRUB not found on serial console' unless (is_jeos || wait_serial('GNU GRUB', 180));
select_console('sut');
}
}
}
=head2 assert_shutdown_and_restore_system
assert_shutdown_and_restore_system($action, $shutdown_timeout);
VNC connection to SUT (the 'sut' console) is terminated on Xen via svirt backend
and we have to re-connect *after* the restart, otherwise we end up with stalled
VNC connection. The tricky part is to know *when* the system is already booting.
Default $action is reboot, $shutdown_timeout is timeout for shutdown, default value is 60 seconds.
=cut
# VNC connection to SUT (the 'sut' console) is terminated on Xen via svirt
# backend and we have to re-connect *after* the restart, otherwise we end up
# with stalled VNC connection. The tricky part is to know *when* the system
# is already booting.
sub assert_shutdown_and_restore_system {
my ($action, $shutdown_timeout) = @_;
$action //= 'reboot';
$shutdown_timeout //= 60;
my $vnc_console = get_required_var('SVIRT_VNC_CONSOLE');
console($vnc_console)->disable_vnc_stalls;
assert_shutdown($shutdown_timeout);
if ($action eq 'reboot') {
reset_consoles;
my $svirt = console('svirt');
# Set disk as a primary boot device
if (is_s390x or get_var('NETBOOT')) {
$svirt->change_domain_element(os => initrd => undef);
$svirt->change_domain_element(os => kernel => undef);
$svirt->change_domain_element(os => cmdline => undef);
$svirt->change_domain_element(on_reboot => undef);
$svirt->define_and_start;
}
else {
$svirt->define_and_start;
select_console($vnc_console);
}
}
}
=head2 assert_shutdown_with_soft_timeout
assert_shutdown_with_soft_timeout([$args]);
$args = {[timeout => $timeout] [,soft_timeout => $soft_timeout] [,bugref => $bugref] [,soft_failure_reason => $soft_failure_reason]}
Extending assert_shutdown with a soft timeout. When C<$args->{soft_timeout}> is reached,
a soft failure is recorded with the message C<$args->{soft_failure_reason}>.
After that, assert_shutdown continues until the (hard) timeout C<$args->{timeout}> is hit.
This makes sense when a shutdown sporadically takes longer then it normally should take
and the proper statistics of such cases should be gathered instead of just increasing a timeout.
If C<$args->{soft_timeout}> is not specified, then the default assert_shutdown is executed.
Example:
assert_shutdown_with_soft_timeout({timeout => 300, soft_timeout => 60, bugref => 'bsc#123456'});
=cut
sub assert_shutdown_with_soft_timeout {
my ($args) = @_;
$args->{timeout} //= (is_s390x || is_ipmi) ? 600 : get_var('DEBUG_SHUTDOWN') ? 180 : 60;
$args->{soft_timeout} //= 0;
$args->{bugref} //= "No bugref specified";
if ($args->{soft_timeout}) {
diag("assert_shutdown_with_soft_timeout(): soft_timeout=" . $args->{soft_timeout});
die "soft timeout has to be smaller than timeout" unless ($args->{soft_timeout} < $args->{timeout});
my $ret = check_shutdown $args->{soft_timeout};
return if $ret;
$args->{soft_failure_reason} //= "$args->{bugref}: Machine didn't shut down within $args->{soft_timeout} sec";
record_info('Softfail', "$args->{soft_failure_reason}", result => 'softfail');
}
assert_shutdown($args->{timeout} - $args->{soft_timeout});
}
=head2 check_bsc1215132
Validate dependencies which provides shutdown/poweroff/reboot commands.
Use this in the post_fail_hook to raise a softfail of the reported bug.
=cut
sub check_bsc1215132 {
record_soft_failure("bsc1215132: Possible missing dependency on systemd")
if (script_run("rpm -q --provides systemd | grep systemd-sysvinit") ||
script_run("rpm -q --provides systemd | grep shutdown"));
}