forked from os-autoinst/os-autoinst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
isotovideo
executable file
·435 lines (361 loc) · 12.2 KB
/
isotovideo
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
#!/usr/bin/perl -w
# Copyright © 2009-2013 Bernhard M. Wiedemann
# Copyright © 2012-2018 SUSE LLC
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, see <http://www.gnu.org/licenses/>.
#
=head1 SYNOPSIS
isotovideo [OPTIONS] [TEST PARAMETER]
Parses vars.json and tests the given assets/ISOs.
=head1 OPTIONS
=over 4
=item B<-d, --debug>
Enable direct output to STDERR instead of autoinst-log.txt
=item B<-v, --version>
Show the current program version and test API version
=item B<-h, -?, --help>
Show this help.
=head1 TEST PARAMETER
All additional command line arguments specified in the C<key=value> format are
parsed as test parameters which take precedence over the settings in the
vars.json file. Lower case key names are transformed into upper case
automatically for convenience.
=cut
use strict;
use warnings;
use autodie ':all';
no autodie 'kill';
my $installprefix; # $bmwqemu::scriptdir
BEGIN {
# the following line is modified during make install
$installprefix = undef;
my ($wd) = $0 =~ m-(.*)/-;
$wd ||= '.';
$installprefix ||= $wd;
unshift @INC, "$installprefix";
}
use bmwqemu;
use needle;
use autotest;
use commands;
use distribution;
use testapi 'diag';
use Getopt::Long;
require IPC::System::Simple;
use POSIX qw(:sys_wait_h _exit);
use Time::HiRes qw(gettimeofday tv_interval sleep time);
use File::Spec;
use File::Path;
use Mojo::IOLoop::ReadWriteProcess 'process';
use Mojo::IOLoop::ReadWriteProcess::Session 'session';
Getopt::Long::Configure("no_ignore_case");
use OpenQA::Isotovideo::CommandHandler;
use OpenQA::Isotovideo::Interface;
use OpenQA::Isotovideo::Utils;
session->enable;
session->enable_subreaper;
my %options;
# global exit status
my $return_code = 1;
sub usage {
my ($usage_return_code) = @_;
$return_code = $usage_return_code;
eval { use Pod::Usage; pod2usage($return_code); };
if ($@) {
die "cannot display help, install perl(Pod::Usage)\n";
}
}
sub _get_version_string {
my $thisversion = qx{git rev-parse HEAD};
chomp $thisversion;
return "Current version is $thisversion [interface v$OpenQA::Isotovideo::Interface::version]";
}
sub version {
print _get_version_string() . "\n";
exit 0;
}
GetOptions(\%options, 'debug|d', 'help|h|?', 'version|v') or usage(1);
usage(0) if $options{help};
version() if $options{version};
diag(_get_version_string());
# enable debug default when started from a tty
$bmwqemu::direct_output = $options{debug};
select(STDERR);
$| = 1;
select(STDOUT); # default
$| = 1;
$bmwqemu::scriptdir = $installprefix;
bmwqemu::init();
for my $arg (@ARGV) {
if ($arg =~ /^([[:alnum:]_\[\]\.]+)=(.+)/) {
my $key = uc $1;
$bmwqemu::vars{$key} = $2;
diag("Setting forced test parameter $key -> $2");
}
}
my $cmd_srv_process;
my $testprocess;
my $cmd_srv_fd;
my $backend_process;
my $loop = 1;
sub kill_commands {
return unless defined $cmd_srv_process;
$cmd_srv_process->stop() if $cmd_srv_process->is_running;
}
sub kill_autotest {
return unless defined $testprocess;
$testprocess->stop() if $testprocess->is_running;
}
sub kill_backend {
if (defined $bmwqemu::backend && $backend_process) {
diag "killing backend process " . $backend_process->pid;
$backend_process->stop if $backend_process->is_running;
diag("done with backend process");
}
}
sub signalhandler {
my ($sig) = @_;
diag("signalhandler got $sig");
if ($loop) {
$loop = 0;
return;
}
kill_backend;
kill_commands;
kill_autotest;
_exit(1);
}
sub init_backend {
my ($name) = @_;
$bmwqemu::vars{BACKEND} ||= "qemu";
# make sure the needles are initialized
needle::init($bmwqemu::vars{NEEDLES_DIR});
$bmwqemu::vars{NEEDLES_GIT_HASH} = OpenQA::Isotovideo::Utils::calculate_git_hash($bmwqemu::vars{NEEDLES_DIR});
$bmwqemu::backend = backend::driver->new($bmwqemu::vars{BACKEND});
return $bmwqemu::backend;
}
$SIG{TERM} = \&signalhandler;
$SIG{INT} = \&signalhandler;
$SIG{HUP} = \&signalhandler;
# make sure all commands coming from the backend will not be in the
# developers's locale - but a defined english one. This is SUSE's
# default locale
$ENV{LC_ALL} = 'en_US.UTF-8';
$ENV{LANG} = 'en_US.UTF-8';
OpenQA::Isotovideo::Utils::checkout_git_repo_and_branch('CASEDIR');
# Try to load the main.pm from one of the following in this order:
# - product dir
# - casedir
#
# This allows further structuring the test distribution collections with
# multiple distributions or flavors in one repository.
$bmwqemu::vars{PRODUCTDIR} ||= $bmwqemu::vars{CASEDIR};
$bmwqemu::vars{NEEDLES_DIR} //= $bmwqemu::vars{PRODUCTDIR} . '/needles';
OpenQA::Isotovideo::Utils::checkout_git_repo_and_branch('NEEDLES_DIR');
bmwqemu::ensure_valid_vars();
# as we are about to load the test modules store the git hash that has been
# used. If it is not a git repo fail silently, i.e. store an empty variable
$bmwqemu::vars{TEST_GIT_HASH} = OpenQA::Isotovideo::Utils::calculate_git_hash($bmwqemu::vars{CASEDIR});
# start the command fork before we get into the backend, the command child
# is not supposed to talk to the backend directly
($cmd_srv_process, $cmd_srv_fd) = commands::start_server($bmwqemu::vars{QEMUPORT} + 1);
# set a default distribution if the tests don't have one
$testapi::distri = distribution->new;
# add lib of the test distributions - but only for main.pm not to pollute
# further dependencies (the tests get it through autotest)
my @oldINC = @INC;
unshift @INC, $bmwqemu::vars{CASEDIR} . '/lib';
if ($bmwqemu::vars{SCHEDULE}) {
diag 'Enforced test schedule by \'SCHEDULE\' variable in action';
autotest::loadtest($_ . '.pm') foreach split(',', $bmwqemu::vars{SCHEDULE});
$bmwqemu::vars{INCLUDE_MODULES} = 'none';
}
if (-e $bmwqemu::vars{PRODUCTDIR} . '/main.pm') {
require $bmwqemu::vars{PRODUCTDIR} . '/main.pm';
}
elsif (!$bmwqemu::vars{SCHEDULE}) {
die '\'SCHEDULE\' not set and ' . $bmwqemu::vars{PRODUCTDIR} . '/main.pm not found, need one of both';
}
@INC = @oldINC;
if ($bmwqemu::vars{_EXIT_AFTER_SCHEDULE}) {
diag 'Early exit has been requested with _EXIT_AFTER_SCHEDULE. Only evaluating test schedule.';
exit 0;
}
testapi::init();
# init part
bmwqemu::save_vars();
my $testfd;
($testprocess, $testfd) = autotest::start_process();
init_backend();
open(my $fd, ">", "os-autoinst.pid");
print $fd "$$\n";
close $fd;
if (!$bmwqemu::backend->_send_json({cmd => 'alive'})) {
# might throw an exception
$bmwqemu::backend->start_vm();
}
if ($ENV{RUN_VNCVIEWER}) {
system("vncviewer -shared localhost:" . $bmwqemu::vars{VNC} . " -viewonly &");
}
if ($ENV{RUN_DEBUGVIEWER}) {
system("$bmwqemu::scriptdir/debugviewer/debugviewer qemuscreenshot/last.png &");
}
use IO::Select;
$backend_process = $bmwqemu::backend->{backend_process};
my $io_select = IO::Select->new();
$io_select->add($testfd);
$io_select->add($cmd_srv_fd);
$io_select->add($backend_process->channel_out);
my $stop_loop = sub { $loop = 0 if $loop; };
$testprocess->once(collected => $stop_loop);
$backend_process->once(collected => $stop_loop);
$cmd_srv_process->once(collected => $stop_loop);
# now we have everything, give the tests a go
$testfd->write("GO\n");
my $command_handler = OpenQA::Isotovideo::CommandHandler->new(
cmd_srv_fd => $cmd_srv_fd,
backend_fd => $backend_process->channel_in,
);
$command_handler->on(tests_done => sub {
CORE::close($testfd);
$testfd = undef;
kill_autotest;
$loop = 0;
});
my ($last_check_seconds, $last_check_microseconds) = gettimeofday;
sub _calc_check_delta {
# an estimate of eternity
my $delta = 100;
if ($last_check_seconds) {
$delta = tv_interval([$last_check_seconds, $last_check_microseconds], [gettimeofday]);
}
# sleep the remains of one second if $delta > 0
my $timeout = $delta > 0 ? 1 - $delta : 0;
$command_handler->timeout($timeout < 0 ? 0 : $timeout);
return $delta;
}
sub check_asserted_screen {
my $no_wait = shift;
if ($no_wait) {
# prevent CPU overload by waiting at least a little bit
$command_handler->timeout(0.1);
}
else {
_calc_check_delta;
# come back later, avoid too often called function
return if $command_handler->timeout > 0.05;
}
($last_check_seconds, $last_check_microseconds) = gettimeofday;
my $rsp = $bmwqemu::backend->_send_json({cmd => 'check_asserted_screen'}) || {};
# the test needs that information
$rsp->{tags} = $command_handler->tags;
if ($rsp->{found} || $rsp->{timeout}) {
myjsonrpc::send_json($testfd, {ret => $rsp});
$command_handler->clear_tags_and_timeout();
}
else {
_calc_check_delta unless $no_wait;
}
}
$return_code = 0;
while ($loop) {
my ($ready_for_read, $ready_for_write, $exceptions) = IO::Select::select($io_select, undef, $io_select, $command_handler->timeout);
for my $readable (@$ready_for_read) {
my $rsp = myjsonrpc::read_json($readable);
if (!defined $rsp) {
diag sprintf("THERE IS NOTHING TO READ %d %d %d", fileno($readable), fileno($testfd), fileno($cmd_srv_fd));
$readable = 1;
$loop = 0;
last;
}
if ($readable == $backend_process->channel_out) {
$command_handler->send_to_backend_requester({ret => $rsp->{rsp}});
next;
}
$command_handler->process_command($readable, $rsp);
}
if (defined($command_handler->tags)) {
check_asserted_screen($command_handler->no_wait);
}
}
# don't leave the commands server open - it will no longer react anyway
# as most of it ends up in the loop above
kill_commands;
if ($testfd) {
$return_code = 1; # unusual shutdown
CORE::close $testfd;
kill_autotest;
}
diag 'isotovideo ' . ($return_code ? 'failed' : 'done');
my $clean_shutdown;
if (!$return_code) {
eval {
$clean_shutdown = $bmwqemu::backend->_send_json({cmd => 'is_shutdown'});
diag "BACKEND SHUTDOWN $clean_shutdown";
};
# don't rely on the backend in a sane state if we failed - just kill it later
eval { bmwqemu::stop_vm(); };
if ($@) {
diag "Error during stop_vm: $@";
$return_code = 1;
}
}
# read calculated variables from backend and tests
bmwqemu::load_vars();
# mark hard disks for upload if test finished
if (!$return_code && $command_handler->test_completed && $bmwqemu::vars{BACKEND} eq 'qemu') {
my @toextract;
for my $i (1 .. $bmwqemu::vars{NUMDISKS}) {
my $dir = 'assets_private';
my $name = $bmwqemu::vars{"STORE_HDD_$i"} || undef;
unless ($name) {
$name = $bmwqemu::vars{"PUBLISH_HDD_$i"} || undef;
$dir = 'assets_public';
}
next unless $name;
$name =~ /\.([[:alnum:]]+)$/;
my $format = $1;
push @toextract, {hdd_num => $i, name => $name, dir => $dir, format => $format};
}
if ($bmwqemu::vars{UEFI} && $bmwqemu::vars{PUBLISH_PFLASH_VARS}) {
push(@toextract, {pflash_vars => 1,
name => $bmwqemu::vars{PUBLISH_PFLASH_VARS},
dir => 'assets_public',
format => 'qcow2'});
}
if (@toextract && !$clean_shutdown) {
diag "ERROR: Machine not shut down when uploading disks!\n";
$return_code = 1;
}
else {
for my $asset (@toextract) {
local $@;
eval { $bmwqemu::backend->extract_assets($asset); };
if ($@) {
diag "extract assets failed: $@";
$return_code = 1;
}
}
}
}
END {
kill_backend;
kill_commands;
kill_autotest;
# in case of early exit, e.g. help display
$return_code //= 0;
print "$$: EXIT $return_code\n";
$? = $return_code;
}
# vim: set sw=4 et: