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
|
# Copyright © 2009-2013 Bernhard M. Wiedemann
# Copyright © 2012-2015 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/>.
package bmwqemu;
use strict;
use warnings;
use Time::HiRes qw(sleep gettimeofday);
use IO::Socket;
use Thread::Queue;
use POSIX;
use Term::ANSIColor;
use Carp;
use JSON;
use File::Path qw(remove_tree);
use Data::Dumper;
use base 'Exporter';
use Exporter;
our $VERSION;
our @EXPORT = qw(fileContent save_vars);
our @EXPORT_OK = qw(diag);
use backend::driver;
require IPC::System::Simple;
use autodie qw(:all);
sub mydie;
$| = 1;
our $default_timeout = 30; # assert timeout, 0 is a valid timeout
our $idle_timeout = 19; # wait_idle 0 makes no sense
my @ocrrect;
our $screenshotpath = "qemuscreenshot";
# global vars
our $logfd;
our $istty;
our $direct_output;
our $standstillthreshold = scale_timeout(600);
our %vars;
sub load_vars() {
my $fn = "vars.json";
my $ret = {};
local $/;
open(my $fh, '<', $fn) or return 0;
eval { $ret = JSON->new->relaxed->decode(<$fh>); };
die #
"parse error in vars.json:\n" . #
"$@" if $@;
close($fh);
%vars = %{$ret};
return;
}
sub save_vars() {
my $fn = "vars.json";
unlink "vars.json" if -e "vars.json";
open(my $fd, ">", $fn);
fcntl($fd, F_SETLKW, pack('ssqql', F_WRLCK, 0, 0, 0, $$)) or die "cannot lock vars.json: $!\n";
truncate($fd, 0) or die "cannot truncate vars.json: $!\n";
# make sure the JSON is sorted
my $json = JSON->new->pretty->canonical;
print $fd $json->encode(\%vars);
close($fd);
return;
}
sub result_dir() {
return "testresults";
}
our $gocrbin = "/usr/bin/gocr";
# set from isotovideo during initialization
our $scriptdir;
sub init {
load_vars();
$bmwqemu::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');
if ($direct_output) {
open($logfd, '>&STDERR');
}
else {
open($logfd, ">", result_dir . "/autoinst-log.txt");
}
# set unbuffered so that send_key lines from main thread will be written
my $oldfh = select($logfd);
$| = 1;
select($oldfh);
die "DISTRI undefined\n" . pp(\%vars) . "\n" unless $vars{DISTRI};
unless ($vars{CASEDIR}) {
my @dirs = ("$scriptdir/distri/$vars{DISTRI}");
unshift @dirs, $dirs[-1] . "-" . $vars{VERSION} if ($vars{VERSION});
for my $d (@dirs) {
if (-d $d) {
$vars{CASEDIR} = $d;
last;
}
}
die "can't determine test directory for $vars{DISTRI}\n" unless $vars{CASEDIR};
}
# defaults
$vars{QEMUPORT} ||= 15222;
$vars{VNC} ||= 90;
# openQA already sets a random string we can reuse
$vars{JOBTOKEN} ||= random_string(10);
# FIXME: does not belong here
if (defined($vars{DISTRI}) && $vars{DISTRI} eq 'archlinux') {
$vars{HDDMODEL} = "ide";
}
save_vars();
## env vars end
## some var checks
if (!-x $gocrbin) {
$gocrbin = undef;
}
if ($vars{SUSEMIRROR} && $vars{SUSEMIRROR} =~ s{^(\w+)://}{}) { # strip & check proto
if ($1 ne "http") {
die "only http mirror URLs are currently supported but found '$1'.";
}
}
}
## some var checks end
# global vars end
# local vars
our $backend; #FIXME: make local after adding frontend-api to bmwqemu
# local vars end
# global/shared var set functions
sub set_ocr_rect {
@ocrrect = @_;
return;
}
# global/shared var set functions end
# util and helper functions
sub get_timestamp {
my $t = gettimeofday;
return sprintf "%s.%04d ", (POSIX::strftime "%H:%M:%S", gmtime($t)), 10000 * ($t - int($t));
}
sub print_possibly_colored {
my ($text, $color) = @_;
if (($direct_output && !$istty) || !$direct_output) {
$logfd && print $logfd get_timestamp() . "$$ $text\n";
}
if ($istty || !$logfd) {
if ($color) {
print STDERR colored(get_timestamp() . "$$ " . $text, $color) . "\n";
}
else {
print STDERR get_timestamp() . "$$ $text\n";
}
}
return;
}
sub diag {
print_possibly_colored "@_";
return;
}
sub fctres {
my ($text, $fname) = @_;
$fname //= (caller(1))[3];
print_possibly_colored ">>> $fname: $text", 'green';
return;
}
sub fctinfo {
my ($text, $fname) = @_;
$fname //= (caller(1))[3];
print_possibly_colored "::: $fname: $text", 'yellow';
return;
}
sub fctwarn {
my ($text, $fname) = @_;
$fname //= (caller(1))[3];
print_possibly_colored "!!! $fname: $text", 'red';
return;
}
sub modstart {
my $text = sprintf "||| %s at %s", join(' ', @_), POSIX::strftime("%F %T", gmtime);
print_possibly_colored $text, 'bold';
return;
}
use autotest qw($current_test);
sub current_test() {
return $autotest::current_test;
}
sub update_line_number {
return unless current_test;
my $out = "";
my $ending = quotemeta(current_test->{script});
for my $i (1 .. 10) {
my ($package, $filename, $line, $subroutine, $hasargs, $wantarray, $evaltext, $is_require, $hints, $bitmask, $hinthash) = caller($i);
last unless $filename;
next unless $filename =~ m/$ending$/;
print get_timestamp() . "Debug: $filename:$line called $subroutine\n";
last;
}
return;
}
# pretty print like Data::Dumper but without the "VAR1 = " prefix
sub pp {
# FTR, I actually hate Data::Dumper.
my $value_with_trailing_newline = Data::Dumper->new(\@_)->Terse(1)->Dump();
chomp($value_with_trailing_newline);
return $value_with_trailing_newline;
}
sub log_call {
my $fname = (caller(1))[3];
update_line_number();
my @result;
while (my ($key, $value) = splice(@_, 0, 2)) {
push @result, join("=", $key, pp($value));
}
my $params = join(", ", @result);
print_possibly_colored '<<< ' . $fname . "($params)", 'blue';
return;
}
sub fileContent {
my ($fn) = @_;
no autodie qw(open);
open(my $fd, "<", $fn) or return;
local $/;
my $result = <$fd>;
close($fd);
return $result;
}
# util and helper functions end
# backend management
sub stop_vm() {
return unless $backend;
my $ret = $backend->stop();
if (!$direct_output && $logfd) {
close $logfd;
$logfd = undef;
}
return $ret;
}
sub mydie {
my ($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 {
my ($result, $fn) = @_;
open(my $fd, ">", "$fn.new");
print $fd to_json($result, {pretty => 1});
close($fd);
return rename("$fn.new", $fn);
}
sub scale_timeout {
my ($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 {
my ($count) = @_;
$count //= 4;
my $string;
my @chars = ('a' .. 'z', 'A' .. 'Z');
$string .= $chars[rand @chars] for 1 .. $count;
return $string;
}
sub hashed_string {
fctwarn '@DEPRECATED: Use testapi::hashed_string instead';
return testapi::hashed_string(@_);
}
sub wait_for_one_more_screenshot {
# sleeping for one second should ensure that one more screenshot is taken
sleep 1;
}
1;
# vim: set sw=4 et:
|