@@ -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,62 @@ 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(
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
962995subtest ' "race" between status updates and stale job detection' => sub {
0 commit comments