forked from os-autoinst/os-autoinst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bmwqemu.pm
executable file
·345 lines (275 loc) · 8.59 KB
/
bmwqemu.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
# Copyright 2009-2013 Bernhard M. Wiedemann
# Copyright 2012-2021 SUSE LLC
# SPDX-License-Identifier: GPL-2.0-or-later
package bmwqemu;
use Mojo::Base -strict, -signatures;
use autodie ':all';
use Fcntl ':flock';
use Time::HiRes qw(sleep);
use IO::Socket;
use Carp;
use Mojo::JSON qw(encode_json);
use Cpanel::JSON::XS ();
use File::Path 'remove_tree';
use Data::Dumper;
use Mojo::Log;
use Mojo::File qw(path);
use Term::ANSIColor;
use Exporter 'import';
our $VERSION;
our @EXPORT_OK = qw(diag fctres fctinfo fctwarn modstate save_vars);
require IPC::System::Simple;
use log;
sub mydie;
$| = 1;
our $default_timeout = 30; # assert timeout, 0 is a valid timeout
our $openqa_default_share = '/var/lib/openqa/share';
my @ocrrect;
our $screenshotpath = "qemuscreenshot";
# global vars
# Known locations of OVMF (UEFI) firmware: first is openSUSE, second is
# the kraxel.org nightly packages, third is Fedora's edk2-ovmf package,
# fourth is Debian's ovmf package.
our @ovmf_locations = (
'/usr/share/qemu/ovmf-x86_64-ms-code.bin', '/usr/share/edk2.git/ovmf-x64/OVMF_CODE-pure-efi.fd',
'/usr/share/edk2/ovmf/OVMF_CODE.fd', '/usr/share/OVMF/OVMF_CODE.fd'
);
our %vars;
tie %vars, 'bmwqemu::tiedvars', %vars;
sub result_dir () { 'testresults' }
# deprecated functions, moved to log module
{
no warnings 'once';
*log_format_callback = \&log::log_format_callback;
*diag = \&log::diag;
*fctres = \&log::fctres;
*fctinfo = \&log::fctinfo;
*fctwarn = \&log::fctwarn;
*modstate = \&log::modstate;
*logger = \&log::logger;
*init_logger = \&log::init_logger;
}
use constant STATE_FILE => 'base_state.json';
# Write a JSON representation of the process termination to disk
sub serialize_state (%state) {
bmwqemu::fctwarn($state{msg}) if delete $state{error};
bmwqemu::diag($state{msg}) if delete $state{log};
return undef if -e STATE_FILE;
eval { path(STATE_FILE)->spew(encode_json(\%state)) };
bmwqemu::diag("Unable to serialize fatal error: $@") if $@;
}
sub load_vars () {
my $fn = "vars.json";
my $ret = {};
local $/;
my $fh;
eval { open($fh, '<', $fn) };
return 0 if $@;
eval { $ret = Cpanel::JSON::XS->new->relaxed->decode(<$fh>); };
die "parse error in vars.json:\n$@" if $@;
close($fh);
%vars = %{$ret};
return;
}
sub save_vars (%args) {
my $fn = "vars.json";
unlink "vars.json" if -e "vars.json";
open(my $fd, ">", $fn);
flock($fd, LOCK_EX) or die "cannot lock vars.json: $!\n";
truncate($fd, 0) or die "cannot truncate vars.json: $!\n";
my $write_vars = \%vars;
if ($args{no_secret}) {
$write_vars = {};
$write_vars->{$_} = $vars{$_} for (grep !/(^_SECRET_|_PASSWORD)/, keys(%vars));
}
# make sure the JSON is sorted
my $json = Cpanel::JSON::XS->new->pretty->canonical;
print $fd $json->encode($write_vars);
close($fd);
return;
}
our $gocrbin = "/usr/bin/gocr";
# set from isotovideo during initialization
our $topdir;
sub init () {
load_vars();
$vars{BACKEND} ||= "qemu";
# remove directories for asset upload
remove_tree("assets_public");
remove_tree("assets_private");
remove_tree(result_dir);
mkdir result_dir;
mkdir join('/', result_dir, 'ulogs');
log::init_logger;
}
sub _check_publish_vars () {
return 0 unless my $nd = $vars{NUMDISKS};
my @hdds = map { $vars{"HDD_$_"} } 1 .. $nd;
for my $i (1 .. $nd) {
for my $type (qw(STORE PUBLISH FORCE_PUBLISH)) {
my $name = $type . "_HDD_$i";
next unless my $out = $vars{$name};
die "HDD_$i also specified in $name. This is not supported" if grep { $_ && $_ eq $out } @hdds;
}
}
return 1;
}
sub ensure_valid_vars () {
# defaults
$vars{QEMUPORT} ||= 15222;
$vars{VNC} ||= 90;
# openQA already sets a random string we can reuse
$vars{JOBTOKEN} ||= random_string(10);
if ($gocrbin && !-x $gocrbin) {
$gocrbin = undef;
}
die "CASEDIR variable not set, unknown test case directory" if !defined $vars{CASEDIR};
die "No scripts in CASEDIR '$vars{CASEDIR}'\n" unless -e $vars{CASEDIR};
die "WHEELS_DIR '$vars{WHEELS_DIR}' does not exist" if defined $vars{WHEELS_DIR} && !-d $vars{WHEELS_DIR};
_check_publish_vars();
save_vars();
}
## some var checks end
# global vars end
# local vars
our $backend;
# local vars end
# util and helper functions
sub current_test () {
require autotest;
no warnings 'once';
return $autotest::current_test;
}
sub update_line_number () {
return unless current_test;
return unless current_test->{script};
my @out;
my $casedir = $vars{CASEDIR} // '';
for (my $i = 10; $i > 0; $i--) {
my ($package, $filename, $line, $subroutine) = caller($i);
next unless $filename && $filename =~ /\Q$casedir/;
$filename =~ s@$casedir/?@@;
push @out, "$filename:$line called $subroutine";
}
log::logger->debug(join(' -> ', @out));
return;
}
# pretty print like Data::Dumper but without the "VAR1 = " prefix
sub pp (@args) {
# FTR, I actually hate Data::Dumper.
my $value_with_trailing_newline = Data::Dumper->new(\@args)->Terse(1)->Useqq(1)->Dump();
chomp($value_with_trailing_newline);
return $value_with_trailing_newline;
}
# Use special argument `-masked` to hide the given value in log output.
# It can be specified multiple times and or the value can be a ARRAY_REF or
# scalar.
sub log_call (@args) {
my $fname = (caller(1))[3];
update_line_number();
# extract -masked parameter out of argument list
my @masked;
my @effective_args;
while (@args) {
my $v = shift @args;
if (defined($v) && $v eq '-masked' && @args) {
my $mval = shift @args;
push @masked, ref($mval) eq 'ARRAY' ? @$mval : $mval;
} else {
push @effective_args, $v;
}
}
my $params;
if (@effective_args == 1) {
$params = pp($effective_args[0]);
}
else {
# key/value pairs
my @result;
while (my ($key, $value) = splice(@effective_args, 0, 2)) {
if ($key =~ tr/0-9a-zA-Z_//c) {
# only quote if needed
$key = pp($key);
}
push @result, join("=", $key, pp($value));
}
$params = join(", ", @result);
}
foreach (@masked) {
my $mask = pp($_);
$mask =~ s/^"(.*)"$/$1/;
$params =~ s/\Q$mask\E/[masked]/g;
}
log::logger->debug('<<< ' . $fname . "($params)");
return;
}
# util and helper functions end
# backend management
sub stop_vm () {
return unless $backend;
my $ret = $backend->stop();
return $ret;
}
sub mydie ($cause_of_death) {
log_call(cause_of_death => $cause_of_death);
croak "mydie";
}
# runtime information gathering functions end
# store the obj as json into the given filename
sub save_json_file ($result, $fn) {
open(my $fd, ">", "$fn.new");
my $json = eval { Cpanel::JSON::XS->new->utf8->pretty->canonical->encode($result) };
if (my $err = $@) {
my $dump = Data::Dumper->Dump([$result], ['result']);
croak "Cannot encode input: $@\n$dump";
}
print $fd $json;
close($fd);
return rename("$fn.new", $fn);
}
sub scale_timeout ($timeout) {
return $timeout * ($vars{TIMEOUT_SCALE} // 1);
}
=head2 random_string
random_string([$count]);
Just a random string useful for pseudo security or temporary files.
=cut
sub random_string ($count) {
$count //= 4;
my $string;
my @chars = ('a' .. 'z', 'A' .. 'Z');
$string .= $chars[rand @chars] for 1 .. $count;
return $string;
}
# sleeping for one second should ensure that one more screenshot is taken
sub wait_for_one_more_screenshot () { sleep 1 }
package bmwqemu::tiedvars;
use Tie::Hash;
use base qw/ Tie::StdHash /; # no:style prevent style warning regarding use of Mojo::Base and base in this file
use Carp 'croak';
sub TIEHASH ($class, %args) {
my $self = bless {
data => {%args},
}, $class;
}
sub STORE ($self, $key, $val) {
croak("Settings key '$key' is invalid (check your test settings)") unless $key =~ m/^(?:[A-Z0-9_]+)\z/;
$self->{data}->{$key} = $val;
}
sub FIRSTKEY ($self) {
my $data = $self->{data};
my @k = keys %$data; # reset
my $next = each %$data;
}
sub NEXTKEY ($self, $last) {
my $data = $self->{data};
my $next = each %$data;
}
sub FETCH ($self, $key) {
my $val = $self->{data}->{$key};
}
sub DELETE ($self, $key) { delete $self->{data}->{$key} }
sub EXISTS ($self, $key) { exists $self->{data}->{$key} }
sub CLEAR ($self) { $self->{data} = {} }
sub SCALAR ($self) { scalar %{$self->{data}} }
1;