Skip to content

Commit 4300915

Browse files
committed
Make restart_openqa_job emit proper event payload
And create test coverage - Covers the emission of events on Minion restarts - Loads the AMQP plugin - Checks event body for consistency with events from API issue: https://progress.opensuse.org/issues/190557 Signed-off-by: Ioannis Bonatakis <[email protected]>
1 parent 476bf89 commit 4300915

File tree

2 files changed

+49
-28
lines changed

2 files changed

+49
-28
lines changed

lib/OpenQA/Task/Job/Restart.pm

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ sub restart_delay { $ENV{OPENQA_JOB_RESTART_DELAY} // 5 }
1818
sub restart_openqa_job ($minion_job, $openqa_job) {
1919
my $cloned_job_or_error = $openqa_job->auto_duplicate;
2020
my $is_ok = ref $cloned_job_or_error || $cloned_job_or_error =~ qr/(already.*clone|direct parent)/i;
21-
if ($is_ok) {
22-
my $openqa_job_id = $openqa_job->id;
23-
my %event_data = (id => $openqa_job_id, result => $cloned_job_or_error, auto => 1);
21+
if (ref $cloned_job_or_error) {
22+
my %event_data = (id => $openqa_job->id, result => $cloned_job_or_error->{cluster_cloned}, auto => 1);
2423
OpenQA::Events->singleton->emit_event('openqa_job_restart', data => \%event_data);
2524
}
2625

@@ -64,12 +63,8 @@ sub _init_amqp_plugin ($app) {
6463

6564
sub _wait_for_event_publish ($app) {
6665
return undef unless $app->config->{amqp}->{enabled};
67-
OpenQA::Events->singleton->once(
68-
'amqp_handled',
69-
sub {
70-
Mojo::IOLoop->singleton->stop;
71-
});
72-
Mojo::IOLoop->singleton->start;
66+
OpenQA::Events->singleton->once(amqp_handled => sub { Mojo::IOLoop->stop });
67+
Mojo::IOLoop->start;
7368
}
7469

7570
1;

t/10-jobs.t

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ use Test::MockModule 'strict';
2121
use Test::Mojo;
2222
use Test::Output;
2323
use Test::Warnings qw(:report_warnings warning);
24-
use Mojo::File 'path';
24+
use Mojo::File qw(path tempdir);
2525
use Mojo::JSON qw(decode_json encode_json);
26-
use OpenQA::Test::Utils qw(perform_minion_jobs);
26+
use OpenQA::Test::Utils qw(perform_minion_jobs mock_io_loop);
2727
use OpenQA::Test::TimeLimit '30';
2828

2929
binmode(STDOUT, ':encoding(UTF-8)');
@@ -933,30 +933,56 @@ subtest 'job setting based retriggering' => sub {
933933
is $lastest_job->latest_job->id, $lastest_job->id, 'found the latest job from latest job itself';
934934
};
935935

936-
subtest 'AMQP event emission for minion restarts' => sub {
937-
my $events_module = Test::MockModule->new('OpenQA::Events');
938-
my @emitted_events;
939-
$events_module->redefine(
940-
emit_event => sub ($self, $event_type, %args) {
941-
push @emitted_events, {type => $event_type, data => $args{data}};
942-
$events_module->original('emit_event')->($self, $event_type, %args);
936+
subtest 'AMQP event emission for job restarts within Minion tasks' => sub {
937+
my $plugin_mock = Test::MockModule->new('OpenQA::WebAPI::Plugin::AMQP');
938+
my $conf = "[global]\nplugins=AMQP\n[amqp]\npublish_attempts = 2\npublish_retry_delay = 0\n";
939+
my $tempdir = tempdir;
940+
path($ENV{OPENQA_CONFIG} = $tempdir)->make_path->child('openqa.ini')->spew($conf);
941+
my %published;
942+
my @event_body;
943+
my $io_loop_mock = Test::MockModule->new('Mojo::IOLoop');
944+
$io_loop_mock->redefine(start => sub { });
945+
$plugin_mock->redefine(
946+
publish_amqp => sub ($self, $topic, $data) {
947+
$published{$topic} = $data;
948+
Mojo::IOLoop->next_tick(sub { OpenQA::Events->singleton->emit('amqp_handled'); });
943949
});
944950

951+
my $events_mock = Test::MockModule->new('OpenQA::Events');
952+
$events_mock->redefine(
953+
emit => sub ($self, $type, $args) {
954+
if ($type eq 'openqa_job_restart') {
955+
@event_body = ($type, $args);
956+
}
957+
$events_mock->original('emit')->($self, $type, $args);
958+
});
959+
# use a new app; otherwise it runs slowly, maybe trying to run none-mocked stuff
960+
my $t = Test::Mojo->new('OpenQA::WebAPI');
945961
my $minion = $t->app->minion;
946-
my %_settings = %settings;
947-
$_settings{TEST} = 'test_restart';
948-
$_settings{RETRY} = '1';
949-
my $job = _job_create(\%_settings);
962+
is $t->app->config->{amqp}->{enabled}, 1, 'AMQP enabled from config file';
963+
964+
my %retry_settings = %settings;
965+
$retry_settings{TEST} = 'test_amqp_restart';
966+
$retry_settings{RETRY} = '1';
967+
my $job = $jobs->create_from_settings(\%retry_settings);
968+
$job->discard_changes;
950969
my $job_id = $job->id;
951970

952-
@emitted_events = ();
971+
%published = ();
953972
$job->done(result => FAILED);
954973
stdout_like { perform_minion_jobs($minion) } qr/Job \d+ duplicated as \d+/;
955-
my @restart_events = grep { $_->{type} eq 'openqa_job_restart' } @emitted_events;
956-
is scalar(@restart_events), 1, 'exactly one job restart event emitted';
957-
my $event_data = $restart_events[0]->{data};
958-
is $event_data->{id}, $job_id, 'event contains original job ID';
959-
ok exists($event_data->{result}), 'event contains result';
974+
is scalar keys %published, 1, 'exactly one job restart event emitted';
975+
my $event = $published{'suse.openqa.job.restart'};
976+
is $event->{id}, $job_id, 'event contains original job ID';
977+
is $event->{auto}, 1, 'event marked as auto restart';
978+
my ($user_id, $connection, $type, $data) = @{$event_body[1]};
979+
is $user_id, undef, 'user_id is undef for Minion restart';
980+
is $connection, undef, 'connection is undef for Minion restart';
981+
is $type, 'openqa_job_restart', 'type matches event type';
982+
is_deeply $data, $event, 'published event equals emitted event';
983+
ok exists $data->{result}->{$job_id}, 'old job id is in result';
984+
$job->discard_changes;
985+
is $job->clone_id, $data->{result}->{$job_id}, 'clone_id points to reported id';
960986
};
961987

962988
subtest '"race" between status updates and stale job detection' => sub {

0 commit comments

Comments
 (0)