Receiver/Util.pm

filter: Statementstmt 91.6% 164 / 179 Branchbran 73.5% 25 / 34 Conditioncond 53.8% 7 / 13 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 94.1% 32 / 34 Podpod 100% 13 / 13 totaltotal 86.4% 243 / 281 CC 29 SCAR 12.2
CC
29
Cov
87%
update_parsed_report_in_db
7.1
create_report_groups
6.6
forward_to_level2_receivers
6.2
CRAP 3.4
« Receiver/Daemon.pm ↑ Summary Receiver.pm »
1 package Tapper::Reports::Receiver::Util;
2 9 our $AUTHORITY = 'cpan:TAPPER';
3 # ABSTRACT: Receive test reports
4 9 $Tapper::Reports::Receiver::Util::VERSION = '5.0.1';
5 9 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 9 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use DateTime::Format::Natural;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use File::MimeInfo::Magic;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use IO::Scalar;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use YAML::Syck;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use Devel::Backtrace;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 9 ▶use Sys::Syslog; # core module since 1994
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 9 ▶use Tapper::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 9 ▶use Tapper::Model 'model';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 9 ▶use Tapper::TAP::Harness;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 9 extends 'Tapper::Base';
24  
25 9 has report => (is => 'rw');
26 9 has tap => (is => 'rw');
27  
28  
29  
30 sub cfg
31 {
32 6 ▶ my ($self) = @_;
Subroutine cfg: called CC=1 SCAR=0.0
Pod documented
33 6 return Tapper::Config->subconfig();
34 }
35  
36  
37 sub start_new_report {
38 11 ▶ my ($self, $host, $port) = @_;
Subroutine start_new_report: called CC=1 SCAR=0.0
Pod documented
39  
40 11 $self->report( model('ReportsDB')->resultset('Report')->new({
41 peerport => $port,
42 peerhost => $host,
43 }));
44 11 $self->report->insert;
45 11 my $tap = model('ReportsDB')->resultset('Tap')->new({
46 tap => '',
47 report_id => $self->report->id,
48 });
49 11 $tap->insert;
50 11 return $self->report->id;
51 }
52  
53  
54 sub tap_mimetype {
55 10 ▶ my ($self) = shift;
Subroutine tap_mimetype: called CC=1 SCAR=0.0
Pod documented
56  
57 10 my $TAPH = IO::Scalar->new(\($self->tap));
58 10 return mimetype($TAPH);
59 }
60  
61  
62 sub tap_is_archive
63 {
64 7 ▶ my ($self) = shift;
Subroutine tap_is_archive: called CC=2 SCAR=6.9
Pod documented
65  
66 7 ▶ return $self->tap_mimetype =~ m,application/(x-)?(octet-stream|compressed|gzip), ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->tap_mimetype =~ m[application/(x-)?(octet-stream|compressed|gzip)] ? : 2 5 7
67 }
68  
69  
70  
71 sub write_tap_to_db
72 {
73 5 ▶ my ($self) = shift;
Subroutine write_tap_to_db: called CC=2 SCAR=6.9
Pod documented
74  
75 5 ▶ $self->report->tap->tap_is_archive(1) if $self->tap_is_archive;
Branch: 1 branch
BranchTrueFalseTotal
if $self->tap_is_archive 1 4 5
76 5 $self->report->tap->tap( $self->tap );
77 5 $self->report->tap->update;
78 5 return;
79 }
80  
81  
82 sub get_suite {
83 5 ▶ my ($self, $suite_name, $suite_type) = @_;
Subroutine get_suite: called CC=4 SCAR=14.0
Pod documented
84  
85 5 ▶ $suite_name ||= 'unknown';
Condition50%1/2
$suite_name ||= "unknown"
l!l
50
MC/DC0%0/1
$suite_name ||= "unknown"
$suite_name
Truth table50%1/2
A
A= $suite_name
Aresult
11
00
86 5 ▶ $suite_type ||= 'software';
Condition50%1/2
$suite_type ||= "software"
l!l
50
MC/DC0%0/1
$suite_type ||= "software"
$suite_type
Truth table50%1/2
A
A= $suite_type
Aresult
11
00
87  
88 5 my $suite = model("ReportsDB")->resultset('Suite')->search({name => $suite_name }, {rows => 1})->first;
89 5 ▶ if (not $suite) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($suite) 4 1 5
90 4 $suite = model("ReportsDB")->resultset('Suite')->new({
91 name => $suite_name,
92 type => $suite_type,
93 description => "$suite_name test suite",
94 });
95 4 $suite->insert;
96 }
97 5 return $suite;
98 }
99  
100  
101 sub create_report_sections
102 {
103 5 ▶ my ($self, $parsed_report) = @_;
Subroutine create_report_sections: called CC=4 SCAR=13.9
Pod documented
104  
105 # meta keys
106 5 my $section_nr = 0;
107 5 foreach my $section ( @{$parsed_report->{tap_sections}} ) {
108 8 $section_nr++;
109 my $report_section = model('ReportsDB')->resultset('ReportSection')->new
110 ({
111 report_id => $self->report->id,
112 succession => $section_nr,
113 name => $section->{section_name},
114 8 });
115  
116 8 foreach (keys %{$section->{db_section_meta}})
117 {
118 5 my $value = $section->{db_section_meta}{$_};
119 5 ▶ $report_section->$_ ($value) if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 5 0 5
120 }
121  
122 8 $report_section->insert;
123 }
124 }
125  
126  
127 sub update_reportgroup_testrun_stats
128 {
129 2 ▶ my ($self, $testrun_id) = @_;
Subroutine update_reportgroup_testrun_stats: called CC=3 SCAR=11.0
Pod documented
130  
131 2 my $reportgroupstats = model('ReportsDB')->resultset('ReportgroupTestrunStats')->find($testrun_id);
132 2 ▶ unless ($reportgroupstats and $reportgroupstats->testrun_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($reportgroupstats and $reportgroupstats->testrun_id) 1 1 2
Condition66%2/3
$reportgroupstats and $reportgroupstats->testrun_id
!ll&&!rl&&r
101
MC/DC50%1/2
$reportgroupstats and $reportgroupstats->testrun_id
$reportgroupstats $reportgroupstats->testrun_id
Truth table66%2/3
A and B
A= $reportgroupstatsB= $reportgroupstats->testrun_id
ABresult
0X0
100
111
133 1 $reportgroupstats = model('ReportsDB')->resultset('ReportgroupTestrunStats')->new({ testrun_id => $testrun_id });
134 1 $reportgroupstats->insert;
135 }
136  
137 2 $reportgroupstats->update_failed_passed;
138 2 $reportgroupstats->update;
139 }
140  
141  
142 sub create_report_groups
143 {
144 5 ▶ my ($self, $parsed_report) = @_;
Subroutine create_report_groups: called CC=6 SCAR=18.8
Pod documented
145  
146 my ($reportgroup_arbitrary,
147 $reportgroup_testrun,
148 $reportgroup_primary,
149 $owner
150 ) = (
151 $parsed_report->{db_report_reportgroup_meta}{reportgroup_arbitrary},
152 $parsed_report->{db_report_reportgroup_meta}{reportgroup_testrun},
153 $parsed_report->{db_report_reportgroup_meta}{reportgroup_primary},
154 $parsed_report->{db_report_reportgroup_meta}{owner},
155 5 );
156  
157 5 ▶ if ($reportgroup_arbitrary and $reportgroup_arbitrary ne 'None') {
Branch: 1 branch
BranchTrueFalseTotal
if ($reportgroup_arbitrary and $reportgroup_arbitrary ne "None") 0 5 5
Condition33%1/3
$reportgroup_arbitrary and $reportgroup_arbitrary ne "None"
!ll&&!rl&&r
500
MC/DC0%0/2
$reportgroup_arbitrary and $reportgroup_arbitrary ne "None"
$reportgroup_arbitrary $reportgroup_arbitrary ne "None"
Truth table33%1/3
A and B
A= $reportgroup_arbitraryB= $reportgroup_arbitrary ne "None"
ABresult
0X0
100
111
158 0 my $reportgroup = model('ReportsDB')->resultset('ReportgroupArbitrary')->new
159 ({
160 report_id => $self->report->id,
161 arbitrary_id => $reportgroup_arbitrary,
162 primaryreport => $reportgroup_primary,
163 owner => $owner,
164 });
165 0 $reportgroup->insert;
166 }
167  
168 5 ▶ if ($reportgroup_testrun and $reportgroup_testrun ne 'None') {
Branch: 1 branch
BranchTrueFalseTotal
if ($reportgroup_testrun and $reportgroup_testrun ne "None") 2 3 5
Condition66%2/3
$reportgroup_testrun and $reportgroup_testrun ne "None"
!ll&&!rl&&r
302
MC/DC50%1/2
$reportgroup_testrun and $reportgroup_testrun ne "None"
$reportgroup_testrun $reportgroup_testrun ne "None"
Truth table66%2/3
A and B
A= $reportgroup_testrunB= $reportgroup_testrun ne "None"
ABresult
0X0
100
111
169 2 ▶ if (not $owner) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($owner) 1 1 2
170 # don't check existance of each element in the search chain
171 1 eval {
172 1 $owner = model('TestrunDB')->resultset('Testrun')->find($reportgroup_testrun)->owner->login;
173 };
174 }
175  
176 2 my $reportgroup = model('ReportsDB')->resultset('ReportgroupTestrun')->new
177 ({
178 report_id => $self->report->id,
179 testrun_id => $reportgroup_testrun,
180 primaryreport => $reportgroup_primary,
181 owner => $owner,
182 });
183 2 $reportgroup->insert;
184  
185 2 $self->update_reportgroup_testrun_stats($reportgroup_testrun);
186 }
187 }
188  
189  
190 sub create_report_comment
191 {
192 5 ▶ my ($self, $parsed_report) = @_;
Subroutine create_report_comment: called CC=2 SCAR=8.4
Pod documented
193  
194 5 my ($comment) = ( $parsed_report->{db_report_reportcomment_meta}{reportcomment} );
195 5 ▶ if ($comment) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comment) 0 5 5
196 0 my $reportcomment = model('ReportsDB')->resultset('ReportComment')->new
197 ({
198 report_id => $self->report->id,
199 comment => $comment,
200 succession => 1,
201 });
202 0 $reportcomment->insert;
203 }
204 }
205  
206  
207 sub update_parsed_report_in_db
208 {
209 5 ▶ my ($self, $parsed_report) = @_;
Subroutine update_parsed_report_in_db: called CC=7 SCAR=19.6
Pod documented
210  
211 9 ▶ no strict 'refs'; ## no critic (ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
212  
213 # lookup missing values in db
214 $parsed_report->{db_report_meta}{suite_id} = $self->get_suite($parsed_report->{report_meta}{'suite-name'},
215 5 $parsed_report->{report_meta}{'suite-type'},
216 )->id;
217  
218 # report information
219 5 foreach (keys %{$parsed_report->{db_report_meta}})
220 {
221 14 my $value = $parsed_report->{db_report_meta}{$_};
222 14 ▶ $self->report->$_( $value ) if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 14 0 14
223 }
224  
225 # report information - date fields
226 5 foreach (keys %{$parsed_report->{db_report_date_meta}})
227 {
228 1 my $value = $parsed_report->{db_report_date_meta}{$_};
229 1 ▶ $self->report->$_( DateTime::Format::Natural->new->parse_datetime($value ) ) if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 1 0 1
230 }
231  
232 # success statistics
233 5 foreach (keys %{$parsed_report->{stats}})
234 {
235 45 my $value = $parsed_report->{stats}{$_};
236 45 ▶ $self->report->$_( $value ) if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 45 0 45
237 }
238  
239 5 $self->report->update;
240  
241 5 $self->create_report_sections($parsed_report);
242 5 $self->create_report_groups($parsed_report);
243 5 $self->create_report_comment($parsed_report);
244  
245 }
246  
247  
248 sub forward_to_level2_receivers
249 {
250 5 ▶ my ($self) = @_;
Subroutine forward_to_level2_receivers: called CC=6 SCAR=18.3
Pod documented
251  
252 5 ▶ my @level2_receivers = (keys %{$self->cfg->{receiver}{level2} || {}});
Branch: 1 branch
BranchTrueFalseTotal
$self->cfg->{"receiver"}{"level2"} or {} 4 1 5
253  
254 5 foreach my $l2receiver (@level2_receivers) {
255 1 $self->log->debug( "L2 receiver: $l2receiver" );
256  
257 1 my $options = $self->cfg->{receiver}{level2}{$l2receiver};
258 1 ▶ next if $options->{disabled};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"disabled"} 0 1 1
259  
260 1 my $l2r_class = "Tapper::Reports::Receiver::Level2::$l2receiver";
261 1 ▶ eval "use $l2r_class"; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
262 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 1 1
263 0 return "Could not load $l2r_class";
264 } else {
265 9 ▶ no strict 'refs'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
266 1 $self->log->debug( "Call ${l2r_class}::submit()" );
267 1 my ($error, $retval) = &{"${l2r_class}::submit"}($self, $self->report, $options);
268 1 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 1 1
269 0 $self->log->error( "Error calling ${l2r_class}::submit: " . $retval );
270 0 return $retval;
271 }
272 1 return 0;
273 }
274 }
275 }
276  
277  
278  
279 sub process_request
280 {
281 11 ▶ my ($self, $tap) = @_;
Subroutine process_request: called CC=2 SCAR=7.6
Pod documented
282  
283 11 $SIG{CHLD} = 'IGNORE';
284 11 my $pid = fork();
285 11 ▶ if ($pid == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) { } 5 6 11
286 try {
287 5 ▶ $0 = "tapper-reports-receiver-".$self->report->id;
Subroutine __ANON__: called CC=1 SCAR=0.4
288 5 $self->log->debug("Processing ".$self->report->id);
289 $SIG{USR1} = sub {
290 0 ▶ local $SIG{USR1} = 'ignore'; # make handler reentrant, don't handle signal twice
Subroutine __ANON__: not called CC=1 SCAR=6.9
291 0 my $backtrace = Devel::Backtrace->new(-start=>2, -format => '%I. %s');
292 0 open my $fh, ">>", '/tmp/tapper-receiver-util-'.$self->report->id;
293 0 print $fh $backtrace;
294 0 close $fh;
295 5 };
296  
297 5 $self->tap($tap);
298  
299 5 $self->write_tap_to_db();
300  
301 5 my $harness = Tapper::TAP::Harness->new( tap => $self->tap,
302 tap_is_archive => $self->report->tap->tap_is_archive );
303 5 $harness->evaluate_report();
304  
305 5 $self->update_parsed_report_in_db( $harness->parsed_report );
306 5 $self->forward_to_level2_receivers();
307 } catch {
308 # We can not use log4perl, because that might throw another
309 # exception e.g. when logfile is not writable
310 0 ▶ openlog('Tapper-Reports-Receiver', 'nofatal, ndelay', 'local0');
Subroutine __ANON__: not called CC=1 SCAR=6.9
311 0 syslog('ALERT', "Error in processing report and can not safely log with Log4perl: $_");
312 0 closelog();
313 5 };
314 5 exit 0;
315 } else {
316 # noop in parent, return immediately
317 }
318  
319 }
320  
321 9 1;
322  
323 __END__
324  
325 =pod
326  
327 =encoding UTF-8
328  
329 =head1 NAME
330  
331 Tapper::Reports::Receiver::Util - Receive test reports
332  
333 =head2 cfg
334  
335 Provide Tapper config.
336  
337 =head2 start_new_report
338  
339 Create database entries to store the new report.
340  
341 @param string - remote host name
342 @param int - remote port
343  
344 @return success - report id
345  
346 =head2 tap_mimetype
347  
348 Return mimetype of received TAP (Text vs. TAP::Archive).
349  
350 =head2 tap_is_archive
351  
352 Return true when TAP is TAP::Archive.
353  
354 =head2 write_tap_to_db
355  
356 Put tap string into database.
357  
358 @return success - undef
359 @return error - die
360  
361 =head2 get_suite
362  
363 Get suite name from TAP.
364  
365 =head2 create_report_sections
366  
367 Divide TAP into sections (a Tapper specific extension).
368  
369 =head2 update_reportgroup_testrun_stats
370  
371 Update testrun stats where this report belongs to.
372  
373 =head2 create_report_groups
374  
375 Create reportgroup from testrun details or arbitrary IDs.
376  
377 =head2 create_report_comment
378  
379 Reports can be attached with a comment. Create this.
380  
381 =head2 update_parsed_report_in_db
382  
383 Carve out details from report and update those values in DB.
384  
385 =head2 forward_to_level2_receivers
386  
387 Load configured I<Level 2 receiver> plugins and call them with this
388 report.
389  
390 =head2 process_request
391  
392 Process the tap and put it into the database.
393  
394 @param string - tap
395  
396 =head1 AUTHOR
397  
398 AMD OSRC Tapper Team <tapper@amd64.org>
399  
400 =head1 COPYRIGHT AND LICENSE
401  
402 This software is Copyright (c) 2016 by Advanced Micro Devices, Inc..
403  
404 This is free software, licensed under:
405  
406 The (two-clause) FreeBSD License
407  
408 =cut
« Receiver/Daemon.pm ↑ Summary Receiver.pm »