Skip to content

Commit bb1fc9d

Browse files
committed
Make restart_openqa_job emit event same as API
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 c3e0bef commit bb1fc9d

File tree

2 files changed

+51
-19
lines changed

2 files changed

+51
-19
lines changed

lib/OpenQA/Task/Job/Restart.pm

Lines changed: 2 additions & 3 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

t/10-jobs.t

Lines changed: 49 additions & 16 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)');
@@ -934,29 +934,62 @@ subtest 'job setting based retriggering' => sub {
934934
};
935935

936936
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);
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(
945+
start => sub ($loop) {
946+
$loop->one_tick while $loop->is_running;
947+
});
948+
$io_loop_mock->redefine(
949+
stop => sub ($loop) {
950+
$io_loop_mock->original('stop')->($loop);
951+
});
952+
$plugin_mock->redefine(
953+
publish_amqp => sub ($self, $topic, $data) {
954+
$published{$topic} = $data;
955+
Mojo::IOLoop->next_tick(sub { OpenQA::Events->singleton->emit('amqp_handled'); });
943956
});
944957

958+
my $events_mock = Test::MockModule->new('OpenQA::Events');
959+
$events_mock->redefine(
960+
emit => sub ($self, $type, $args) {
961+
if ($type eq 'openqa_job_restart') {
962+
@event_body = ($type, $args);
963+
}
964+
$events_mock->original('emit')->($self, $type, $args);
965+
});
966+
# new app otherwise it runs slow. Maybe tries no-mocked stuff
967+
my $t = Test::Mojo->new('OpenQA::WebAPI');
945968
my $minion = $t->app->minion;
969+
is $t->app->config->{amqp}->{enabled}, 1, 'AMQP enabled from config file';
970+
946971
my %_settings = %settings;
947-
$_settings{TEST} = 'test_restart';
972+
$_settings{TEST} = 'test_amqp_restart';
948973
$_settings{RETRY} = '1';
949-
my $job = _job_create(\%_settings);
974+
my $job = $jobs->create_from_settings(\%_settings);
975+
$job->discard_changes;
950976
my $job_id = $job->id;
951977

952-
@emitted_events = ();
978+
%published = ();
953979
$job->done(result => FAILED);
954980
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';
981+
is scalar keys %published, 1, 'exactly one job restart event emitted';
982+
my $event = $published{'suse.openqa.job.restart'};
983+
is $event->{id}, $job_id, 'event contains original job ID';
984+
is $event->{auto}, 1, 'event marked as auto restart';
985+
my ($user_id, $connection, $type, $data) = @{$event_body[1]};
986+
is $user_id, undef, 'user_id is undef for Minion restart';
987+
is $connection, undef, 'connection is undef for Minion restart';
988+
is $type, 'openqa_job_restart', 'type matches event type';
989+
is_deeply $data, $event, 'published event equals emitted event';
990+
ok exists $data->{result}->{$job_id}, 'old job id is in result';
991+
$job->discard_changes;
992+
is $job->clone_id, $data->{result}->{$job_id}, 'clone_id points to reported id';
960993
};
961994

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

0 commit comments

Comments
 (0)