Skip to content

Commit 406afd4

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 406afd4

File tree

2 files changed

+51
-16
lines changed

2 files changed

+51
-16
lines changed

lib/OpenQA/Task/Job/Restart.pm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ sub restart_openqa_job ($minion_job, $openqa_job) {
2020
my $is_ok = ref $cloned_job_or_error || $cloned_job_or_error =~ qr/(already.*clone|direct parent)/i;
2121
if ($is_ok) {
2222
my $openqa_job_id = $openqa_job->id;
23-
my %event_data = (id => $openqa_job_id, result => $cloned_job_or_error, auto => 1);
23+
my $result = ref $cloned_job_or_error ? $cloned_job_or_error->{cluster_cloned} : $cloned_job_or_error;
24+
my %event_data = (id => $openqa_job_id, result => $result, auto => 1);
2425
OpenQA::Events->singleton->emit_event('openqa_job_restart', data => \%event_data);
2526
}
2627

t/10-jobs.t

Lines changed: 49 additions & 15 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,63 @@ 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(start => sub { });
945+
$plugin_mock->redefine(
946+
publish_amqp => sub ($self, $topic, $data) {
947+
$published{$topic} = $data;
948+
Mojo::IOLoop->next_tick(
949+
sub {
950+
OpenQA::Events->singleton->emit('amqp_handled');
951+
});
943952
});
944953

954+
my $events_mock = Test::MockModule->new('OpenQA::Events');
955+
$events_mock->redefine(
956+
emit => sub ($self, $type, $args) {
957+
if ($type eq 'openqa_job_restart') {
958+
@event_body = ($type, $args);
959+
}
960+
$events_mock->original('emit')->($self, $type, $args);
961+
});
962+
# new app otherwise it runs slow. Maybe tries no-mocked stuff
963+
my $t = Test::Mojo->new('OpenQA::WebAPI');
945964
my $minion = $t->app->minion;
965+
my $jobs2 = $t->app->schema->resultset('Jobs');
966+
is $t->app->config->{amqp}->{enabled}, 1, 'AMQP enabled from config file';
967+
946968
my %_settings = %settings;
947-
$_settings{TEST} = 'test_restart';
969+
$_settings{TEST} = 'test_amqp_restart';
948970
$_settings{RETRY} = '1';
949-
my $job = _job_create(\%_settings);
971+
my $job = $jobs2->create_from_settings(\%_settings);
972+
$job->discard_changes;
950973
my $job_id = $job->id;
951974

952-
@emitted_events = ();
975+
%published = ();
953976
$job->done(result => FAILED);
954977
stdout_like { perform_minion_jobs($minion) } qr/Job \d+ duplicated as \d+/;
955-
my @restart_events = grep { $_->{type} eq 'openqa_job_restart' } @emitted_events;
978+
my $event = OpenQA::Test::Case::find_most_recent_event($schema, 'job_restart');
979+
my $expected_topic = 'suse.openqa.job.restart';
980+
my @restart_events = grep { $_ eq $expected_topic } keys %published;
981+
ok exists $published{$expected_topic}, 'restart event published via AMQP';
982+
is $published{$expected_topic}{id}, $job_id, 'event contains original job ID';
983+
is $published{$expected_topic}{auto}, 1, 'event marked as auto restart';
956984
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';
985+
is $event_body[0], 'openqa_job_restart', 'event type is openqa_job_restart';
986+
my ($user_id, $connection, $type, $data) = @{$event_body[1]};
987+
is $user_id, undef, 'user_id is undef for Minion restart';
988+
is $connection, undef, 'connection is undef for Minion restart';
989+
is $type, 'openqa_job_restart', 'type matches event type';
990+
is $data->{id}, $job_id, 'data contains original job ID';
991+
is $data->{auto}, 1, 'data marked as auto restart';
992+
is ref($data->{result}), 'HASH', 'result should be a plain hash like API';
993+
is_deeply $data->{result}, {$job_id => $data->{result}{$job_id}}, 'result shows cloned job info';
960994
};
961995

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

0 commit comments

Comments
 (0)