Skip to content

Commit 4540875

Browse files
authored
Merge pull request #6581 from Martchus/status-update-error-handling
Improve error handling when handling status updates from worker
2 parents a5ce762 + 840b510 commit 4540875

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

lib/OpenQA/Schema/Result/Jobs.pm

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use Text::Diff;
3535
use OpenQA::File;
3636
use OpenQA::Parser 'parser';
3737
use OpenQA::WebSockets::Client;
38-
use List::Util qw(any);
38+
use List::Util qw(any all);
3939
use Scalar::Util qw(looks_like_number);
4040
# The state and results constants are duplicated in the Python client:
4141
# if you change them or add any, please also update const.py.
@@ -1056,7 +1056,7 @@ sub calculate_result ($self) {
10561056
}
10571057

10581058
sub save_screenshot ($self, $screen) {
1059-
return unless length($screen->{name});
1059+
return unless ref $screen eq 'HASH' && length($screen->{name});
10601060

10611061
my $tmpdir = $self->worker->get_property('WORKER_TMPDIR');
10621062
return unless -d $tmpdir; # we can't help
@@ -1083,10 +1083,6 @@ sub append_log ($self, $log, $file_name) {
10831083
}
10841084
}
10851085

1086-
sub update_backend ($self, $backend_info) {
1087-
$self->update({backend => $backend_info->{backend}});
1088-
}
1089-
10901086
sub update_result ($self, $result, $state = undef) {
10911087
my %values = (result => $result);
10921088
$values{state} = $state if defined $state;
@@ -1096,6 +1092,9 @@ sub update_result ($self, $result, $state = undef) {
10961092
}
10971093

10981094
sub insert_module ($self, $tm, $skip_jobs_update = undef) {
1095+
my @required_fields = ($tm->{name}, $tm->{category}, $tm->{script});
1096+
return 0 unless all { defined $_ } @required_fields;
1097+
10991098
# prepare query to insert job module
11001099
my $insert_sth = $self->{_insert_job_module_sth};
11011100
$insert_sth = $self->{_insert_job_module_sth} = $self->result_source->schema->storage->dbh->prepare(
@@ -1112,7 +1111,7 @@ sub insert_module ($self, $tm, $skip_jobs_update = undef) {
11121111
# note: We have 'important' in the DB but 'ignore_failure' in the flags for historical reasons (see #1266).
11131112
my $flags = $tm->{flags};
11141113
$insert_sth->execute(
1115-
$self->id, $tm->{name}, $tm->{category}, $tm->{script},
1114+
$self->id, @required_fields,
11161115
$flags->{milestone} ? 1 : 0,
11171116
$flags->{ignore_failure} ? 0 : 1,
11181117
$flags->{fatal} ? 1 : 0,
@@ -1126,7 +1125,7 @@ sub insert_module ($self, $tm, $skip_jobs_update = undef) {
11261125
}
11271126

11281127
sub insert_test_modules ($self, $testmodules) {
1129-
return undef unless scalar @$testmodules;
1128+
return undef unless ref $testmodules eq 'ARRAY' && scalar @$testmodules;
11301129

11311130
# insert all test modules and update job module statistics uxing txn to avoid inconsistent job module
11321131
# statistics in the error case
@@ -1333,11 +1332,11 @@ sub parse_extra_tests ($self, $asset, $type, $script = undef) {
13331332

13341333
$parser->load($tmp_extra_test)->results->each(
13351334
sub {
1336-
return if !$_->test;
1337-
$_->test->script($script) if $script;
1338-
my $t_info = $_->test->to_openqa;
1339-
$self->insert_module($t_info);
1340-
$self->update_module($_->test->name, $_->to_openqa);
1335+
return unless my $test = $_->test;
1336+
return unless my $test_name = $test->name;
1337+
$test->script($script) if $script;
1338+
$self->insert_module($test->to_openqa);
1339+
$self->update_module($test_name, $_->to_openqa);
13411340
});
13421341

13431342
$self->account_result_size("$type results", $parser->write_output($self->result_dir));
@@ -1481,10 +1480,8 @@ sub update_status ($self, $status) {
14811480
$self->append_log($status->{serial_terminal}, 'serial-terminal-live.txt');
14821481
$self->append_log($status->{serial_terminal_user}, 'serial-terminal-live.txt');
14831482
# delete from the hash so it becomes dumpable for debugging
1484-
my $screen = delete $status->{screen};
1485-
$self->save_screenshot($screen) if $screen;
1486-
$self->update_backend($status->{backend}) if $status->{backend};
1487-
$self->insert_test_modules($status->{test_order}) if $status->{test_order};
1483+
$self->save_screenshot(delete $status->{screen});
1484+
$self->insert_test_modules($status->{test_order});
14881485
my %known_image;
14891486
my %known_files;
14901487
my @failed_modules;

lib/OpenQA/Utils.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ sub ensure_timestamp_appended ($str) {
263263
}
264264

265265
sub save_base64_png ($dir, $newfile, $png) {
266-
return unless $newfile;
266+
return unless $newfile && defined($png);
267267
# sanitize
268268
$newfile =~ s,\.png,,;
269269
$newfile =~ tr/a-zA-Z0-9-/_/cs;

t/10-jobs.t

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ my $users = $t->app->schema->resultset('Users');
3939
# for "investigation" tests
4040
my $fake_git_log = 'deadbeef Break test foo';
4141

42+
subtest 'adding job module without required fields' => sub {
43+
my $res = $jobs->first->insert_module({name => undef, category => 'foo', script => 'unk'});
44+
is 0, $res, 'no job module inserted if name missing';
45+
};
46+
4247
subtest 'handling of concurrent deletions in code updating jobs' => sub {
4348
ok my $job = $jobs->find(99927), 'job exists in first place';
4449

0 commit comments

Comments
 (0)