@@ -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)' );
@@ -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]\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(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
962988subtest ' "race" between status updates and stale job detection' => sub {
0 commit comments