@@ -21,9 +21,9 @@ use Test::MockModule 'strict';
2121use Test::Mojo;
2222use Test::Output;
2323use Test::Warnings qw( :report_warnings warning) ;
24- use Mojo::File ' path' ;
24+ use Mojo::File qw( path tempdir ) ;
2525use 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 ) ;
2727use OpenQA::Test::TimeLimit ' 30' ;
2828
2929binmode (STDOUT , ' :encoding(UTF-8)' );
@@ -934,29 +934,63 @@ subtest 'job setting based retriggering' => sub {
934934};
935935
936936subtest ' 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]\n plugins=AMQP\n [amqp]\n publish_attempts = 2\n publish_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
962996subtest ' "race" between status updates and stale job detection' => sub {
0 commit comments