@@ -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,49 @@ 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 $io_loop_mock = mock_io_loop(start => 1);
943+ $plugin_mock -> redefine(
944+ publish_amqp => sub ($self , $topic , $data ) {
945+ $published {$topic } = $data ;
946+ Mojo::IOLoop-> next_tick(
947+ sub {
948+ OpenQA::Events-> singleton-> emit(' amqp_handled' );
949+ });
943950 });
944-
951+ # new app otherwise it runs slow. Maybe tries no-mocked stuff
952+ my $t = Test::Mojo-> new(' OpenQA::WebAPI' );
945953 my $minion = $t -> app-> minion;
954+ my $jobs2 = $t -> app-> schema-> resultset(' Jobs' );
955+ is $t -> app-> config-> {amqp }-> {enabled }, 1, ' AMQP enabled from config file' ;
956+
946957 my %_settings = %settings ;
947- $_settings{TEST } = ' test_restart ' ;
958+ $_settings{TEST } = ' test_amqp_restart ' ;
948959 $_settings{RETRY } = ' 1' ;
949- my $job = _job_create(\%_settings);
960+ my $job = $jobs2 -> create_from_settings(\%_settings);
961+ $job -> discard_changes;
950962 my $job_id = $job -> id;
951963
952- @emitted_events = ();
964+ %published = ();
953965 $job -> done(result => FAILED);
954966 stdout_like { perform_minion_jobs($minion ) } qr / Job \d + duplicated as \d +/ ;
955- my @restart_events = grep { $_ -> {type } eq ' openqa_job_restart' } @emitted_events ;
967+ my $expected_topic = ' suse.openqa.job.restart' ;
968+ my @restart_events = grep { $_ eq $expected_topic } keys %published ;
969+ ok exists $published {$expected_topic }, ' restart event published via AMQP' ;
970+ is $published {$expected_topic }{id }, $job_id , ' event contains original job ID' ;
971+ is $published {$expected_topic }{auto }, 1, ' event marked as auto restart' ;
956972 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' ;
973+ ok exists $published {$expected_topic }{result }{cluster_cloned }, ' job object has cluster_cloned hash' ;
974+ my $cluster_cloned = $published {$expected_topic }{result }{cluster_cloned };
975+ is_deeply $cluster_cloned , {$job_id => $cluster_cloned -> {$job_id }},
976+ ' results contain expected info in the event body' ;
977+ # checking additional args. see emit_events() in lib/OpenQA/Events.pm
978+ is $published {$expected_topic }{result }{user_id }, undef , ' unused user_id in minion event' ;
979+ is $published {$expected_topic }{result }{connection }, undef , ' unused connection in minion event' ;
960980};
961981
962982subtest ' "race" between status updates and stale job detection' => sub {
0 commit comments