| 1 |
package Test2::Harness::Auditor; |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
|
| 5 |
our $VERSION = '1.000179'; |
| 6 |
|
| 7 |
use File::Spec; |
| 8 |
use Time::HiRes qw/time/; |
| 9 |
|
| 10 |
use Test2::Harness::Util::UUID qw/gen_uuid/; |
| 11 |
use Test2::Harness::Util::JSON qw/decode_json/; |
| 12 |
|
| 13 |
use Test2::Harness::Event; |
| 14 |
use Test2::Harness::Auditor::Watcher; |
| 15 |
|
| 16 |
use Test2::Harness::Util::HashBase qw{ |
| 17 |
<action |
| 18 |
<run_id |
| 19 |
|
| 20 |
+broken |
| 21 |
|
| 22 |
<watchers |
| 23 |
<queued |
| 24 |
}; |
| 25 |
|
| 26 |
sub init { |
| 27 |
my $self = shift; |
| 28 |
|
| 29 |
$self->{+WATCHERS} //= {}; |
| 30 |
} |
| 31 |
|
| 32 |
sub process { |
| 33 |
my $self = shift; |
| 34 |
|
| 35 |
while (my $line = <STDIN>) { |
| 36 |
my $data = decode_json($line); |
| 37 |
last unless defined $data; |
| 38 |
my $e = Test2::Harness::Event->new($data); |
| 39 |
|
| 40 |
# If process_event does not return anything we need to record just this |
| 41 |
# event. If it does return then we want to record what it returns. |
| 42 |
if (my @events = $self->process_event($e)) { |
| 43 |
$self->{+ACTION}->($_) for @events; |
| 44 |
} |
| 45 |
else { |
| 46 |
$self->{+ACTION}->($e); |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
sub process_event { |
| 52 |
my $self = shift; |
| 53 |
my ($e) = @_; |
| 54 |
|
| 55 |
my $job_id = $e->job_id; |
| 56 |
my $job_try = $e->job_try // 0; |
| 57 |
|
| 58 |
# Do nothing for non-job events |
| 59 |
return $e unless $job_id; |
| 60 |
|
| 61 |
my $f = $e->facet_data; |
| 62 |
|
| 63 |
if (my $task = $f->{harness_job_queued}) { |
| 64 |
$self->{+WATCHERS}->{$job_id} //= []; |
| 65 |
$self->{+QUEUED}->{$job_id} //= $task; |
| 66 |
return $e; |
| 67 |
} |
| 68 |
|
| 69 |
my $tries = $self->{+WATCHERS}->{$job_id} or return $self->broken($e, "Never saw queue entry"); |
| 70 |
|
| 71 |
if (my $job = $f->{harness_job}) { |
| 72 |
$tries->[$job_try] = Test2::Harness::Auditor::Watcher->new(job => $job, try => $job_try); |
| 73 |
} |
| 74 |
|
| 75 |
my $watcher = $tries->[$job_try] or return $self->broken($e, "never saw harness_job facet"); |
| 76 |
|
| 77 |
return $watcher->process($e); |
| 78 |
} |
| 79 |
|
| 80 |
sub broken { |
| 81 |
my $self = shift; |
| 82 |
my ($e, $message) = @_; |
| 83 |
|
| 84 |
$self->{+BROKEN}->{$e->job_id}++; |
| 85 |
|
| 86 |
push @{$e->facet_data->{errors} //= []} => {details => $message, fail => 1}; |
| 87 |
|
| 88 |
return $e; |
| 89 |
} |
| 90 |
|
| 91 |
sub finish { |
| 92 |
my $self = shift; |
| 93 |
|
| 94 |
my $final_data = {pass => 1}; |
| 95 |
|
| 96 |
while (my ($job_id, $watchers) = each %{$self->{+WATCHERS}}) { |
| 97 |
my $file = File::Spec->abs2rel($self->{+QUEUED}->{$job_id}->{file}); |
| 98 |
|
| 99 |
if (@$watchers) { |
| 100 |
push @{$final_data->{failed}} => [$job_id, $file, $watchers->[-1]->failed_subtest_tree] if $watchers->[-1]->fail; |
| 101 |
push @{$final_data->{retried}} => [$job_id, scalar(@$watchers), $file, $watchers->[-1]->pass ? 'YES' : 'NO'] if @$watchers > 1; |
| 102 |
|
| 103 |
if (my $halt = $watchers->[-1]->halt) { |
| 104 |
push @{$final_data->{halted}} => [$job_id, $file, $halt]; |
| 105 |
} |
| 106 |
} |
| 107 |
else { |
| 108 |
push @{$final_data->{unseen}} => [$job_id, $self->{+QUEUED}->{$job_id}->{file}]; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
$final_data->{pass} = 0 if $final_data->{failed} or $final_data->{unseen}; |
| 113 |
|
| 114 |
my $e = Test2::Harness::Event->new( |
| 115 |
job_id => 0, |
| 116 |
stamp => time, |
| 117 |
event_id => gen_uuid(), |
| 118 |
run_id => $self->{+RUN_ID}, |
| 119 |
facet_data => {harness_final => $final_data}, |
| 120 |
); |
| 121 |
|
| 122 |
$self->{+ACTION}->($e); |
| 123 |
$self->{+ACTION}->(undef); |
| 124 |
} |
| 125 |
|
| 126 |
1; |
| 127 |
|
| 128 |
__END__ |
| 129 |
|
| 130 |
=pod |
| 131 |
|
| 132 |
=encoding UTF-8 |
| 133 |
|
| 134 |
=head1 NAME |
| 135 |
|
| 136 |
Test2::Harness::Auditor - Auditor that validates test results by processing an |
| 137 |
event stream. |
| 138 |
|
| 139 |
=head1 DESCRIPTION |
| 140 |
|
| 141 |
The auditor is responsible for taking a stream of events and determining what |
| 142 |
is passing or failing. An L<Test2::Harness::Auditor::Watcher> instance is |
| 143 |
created for every job_id seen, and events for each job are passed to the proper |
| 144 |
watcher for state management. |
| 145 |
|
| 146 |
=head1 SOURCE |
| 147 |
|
| 148 |
The source code repository for Test2-Harness can be found at |
| 149 |
F<http://github.com/Test-More/Test2-Harness/>. |
| 150 |
|
| 151 |
=head1 MAINTAINERS |
| 152 |
|
| 153 |
=over 4 |
| 154 |
|
| 155 |
=item Chad Granum E<lt>[email protected]<gt> |
| 156 |
|
| 157 |
=back |
| 158 |
|
| 159 |
=head1 AUTHORS |
| 160 |
|
| 161 |
=over 4 |
| 162 |
|
| 163 |
=item Chad Granum E<lt>[email protected]<gt> |
| 164 |
|
| 165 |
=back |
| 166 |
|
| 167 |
=head1 COPYRIGHT |
| 168 |
|
| 169 |
Copyright 2020 Chad Granum E<lt>[email protected]<gt>. |
| 170 |
|
| 171 |
This program is free software; you can redistribute it and/or |
| 172 |
modify it under the same terms as Perl itself. |
| 173 |
|
| 174 |
See F<http://dev.perl.org/licenses/> |
| 175 |
|
| 176 |
=cut |