HealthCheck/Diagnostic.pm

filter: Statementstmt 98.2% 112 / 114 Branchbran 91.4% 75 / 82 Conditioncond 80.8% 55 / 68 MC/DCmcdc 68.4% 26 / 38 Subroutinesub 100% 16 / 16 Podpod 100% 8 / 8 totaltotal 89.5% 292 / 326 CC 71 SCAR 32.0
CC
71
Cov
92%
_summarize
42.1
check
15.7
new
6.6
CRAP 24.4
↑ Summary HealthCheck.pm »
1 package HealthCheck::Diagnostic;
2  
3 # ABSTRACT: A base clase for writing health check diagnositics
4 4 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 our $VERSION = 'v1.9.3'; # VERSION
6  
7 4 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Time::HiRes qw< gettimeofday tv_interval >;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 4 my $rfc3339_timestamp = qr/^(?:
15 (?P<year>[0-9]{4})-
16 (?P<month>1[0-2]|0[1-9])-
17 (?P<day>3[01]|0[1-9]|[12][0-9])
18 [tT ]
19 (?P<hour>2[0-3]|[01][0-9]):
20 (?P<minute>[0-5][0-9]):
21 (?P<second>[0-5][0-9]|60)
22 (?: \. (?P<ms>[0-9]+) )?
23 (?<tz> [-+][0-9]{2}:[0-9]{2} | [zZ] )
24 )$/x;
25  
26 #pod =head1 SYNOPSIS
27 #pod
28 #pod package HealthCheck::Diagnostic::Sample;
29 #pod use parent 'HealthCheck::Diagnostic';
30 #pod
31 #pod # Required implementation of the check
32 #pod # or you can override the 'check' method and avoid the
33 #pod # automatic call to 'summarize'
34 #pod sub run {
35 #pod my ( $class_or_self, %params ) = @_;
36 #pod
37 #pod # will be passed to 'summarize' by 'check'
38 #pod return { %params, status => 'OK' };
39 #pod }
40 #pod
41 #pod You can then either instantiate an instance and run the check.
42 #pod
43 #pod my $diagnostic = HealthCheck::Diagnostic::Sample->new( id => 'my_id' );
44 #pod my $result = $diagnostic->check;
45 #pod
46 #pod Or as a class method.
47 #pod
48 #pod my $result = HealthCheck::Diagnostic::Sample->check();
49 #pod
50 #pod Set C<runtime> to a truthy value in the params for check and the
51 #pod time spent checking will be returned in the results.
52 #pod
53 #pod my $result = HealthCheck::Diagnostic::Sample->check( runtime => 1 );
54 #pod
55 #pod =head1 DESCRIPTION
56 #pod
57 #pod A base class for writing Health Checks.
58 #pod Provides some helpers for validation of results returned from the check.
59 #pod
60 #pod This module does not require that an instance is created to run checks against.
61 #pod If your code requires an instance, you will need to verify that yourself.
62 #pod
63 #pod Results returned by these checks should correspond to the GSG
64 #pod L<Health Check Standard|https://grantstreetgroup.github.io/HealthCheck.html>.
65 #pod
66 #pod Implementing a diagnostic should normally be done in L<run>
67 #pod to allow use of the helper features that L</check> provides.
68 #pod
69 #pod =head1 REQUIRED METHODS
70 #pod
71 #pod =head2 run
72 #pod
73 #pod sub run {
74 #pod my ( $class_or_self, %params ) = @_;
75 #pod return { %params, status => 'OK' };
76 #pod }
77 #pod
78 #pod A subclass must either implement a C<run> method,
79 #pod which will be called by L</check>
80 #pod have its return value passed through L</summarize>,
81 #pod or override C<check> and handle all validation itself.
82 #pod
83 #pod See the L</check> method documentation for suggestions on when it
84 #pod might be overridden.
85 #pod
86 #pod =head1 METHODS
87 #pod
88 #pod =head2 new
89 #pod
90 #pod my $diagnostic
91 #pod = HealthCheck::Diagnostic::Sample->new( id => 'my_diagnostic' );
92 #pod
93 #pod =head3 ATTRIBUTES
94 #pod
95 #pod Attributes set on the object created will be copied into the result
96 #pod by L</summarize>, without overriding anything already set in the result.
97 #pod
98 #pod =over
99 #pod
100 #pod =item collapse_single_result
101 #pod
102 #pod If truthy, will collapse a single sub-result into the current result,
103 #pod with the child result overwriting the values from the parent.
104 #pod
105 #pod For example:
106 #pod
107 #pod { id => "my_id",
108 #pod label => "My Label",
109 #pod runbook => "https://grantstreetgroup.github.io/HealthCheck.html",
110 #pod results => [ {
111 #pod label => "Sub Label",
112 #pod status => "OK",
113 #pod } ]
114 #pod }
115 #pod
116 #pod Collapses to:
117 #pod
118 #pod { id => "my_id",
119 #pod label => "Sub Label",
120 #pod runbook => "https://grantstreetgroup.github.io/HealthCheck.html",
121 #pod status => "OK",
122 #pod }
123 #pod
124 #pod
125 #pod =item tags
126 #pod
127 #pod An arrayref used as the default set of tags for any checks that don't
128 #pod override them.
129 #pod
130 #pod =back
131 #pod
132 #pod Any other parameters are included in the "Result" hashref returned.
133 #pod
134 #pod Some recommended things to include are:
135 #pod
136 #pod =over
137 #pod
138 #pod =item id
139 #pod
140 #pod The unique id for this check.
141 #pod
142 #pod =item label
143 #pod
144 #pod A human readable name for this check.
145 #pod
146 #pod =item runbook
147 #pod
148 #pod A runbook link to help troubleshooting if the status is not OK.
149 #pod
150 #pod =back
151 #pod
152 #pod =cut
153  
154 13 ▶sub new {
Subroutine new: called CC=4 SCAR=18.9
Pod documented
155 13 my ($class, @params) = @_;
156  
157 # Allow either a hashref or even-sized list of params
158 my %params = @params == 1 && ( ref $params[0] || '' ) eq 'HASH'
159 0 ▶ ? %{ $params[0] } : @params;
Branch: 1 branch
BranchTrueFalseTotal
@params == 1 && (ref $params[0] || '') eq 'HASH' ? : 0 13 13
Condition20%1/5
@params == 1 and (ref $params[0] || "") eq "HASH"
!ll&&!rl&&r
1300
ref $params[0] || ''
l!l
00
MC/DC0%0/2
@params == 1 and (ref $params[0] || "") eq "HASH"
@params == 1 ref $params[0]
Truth table33%1/3
A and B
A= @params == 1B= ref $params[0]
ABresult
0X0
100
111
160  
161 13 bless \%params, $class;
162 }
163  
164 #pod =head2 collapse_single_result
165 #pod
166 #pod Read only accessor for the C</collapse_single_result> attribute.
167 #pod
168 #pod =cut
169  
170 17 ▶sub collapse_single_result {
Subroutine collapse_single_result: called CC=2 SCAR=6.9
Pod documented
171 17 ▶ return unless ref $_[0]; return shift->{collapse_single_result};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] 3 14 17
172 }
173  
174 #pod =head2 tags
175 #pod
176 #pod Read only accessor that returns the list of tags registered with this object.
177 #pod
178 #pod =cut
179  
180 103 ▶sub tags { return unless ref $_[0]; @{ shift->{tags} || [] } }
Subroutine tags: called CC=3 SCAR=11.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] 1 102 103
Condition100%2/2
+(shift())->{'tags'} || []
l!l
5151
MC/DC100%1/1
+(shift())->{'tags'} || []
+(shift())->{'tags'}
Truth table100%2/2
A
A= +(shift())->{'tags'}
Aresult
11
00
181  
182 #pod =head2 id
183 #pod
184 #pod Read only accessor that returns the id registered with this object.
185 #pod
186 #pod =cut
187  
188 59 ▶sub id { return unless ref $_[0]; return shift->{id} }
Subroutine id: called CC=2 SCAR=7.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] 0 59 59
189  
190 #pod =head2 label
191 #pod
192 #pod Read only accessor that returns the label registered with this object.
193 #pod
194 #pod =cut
195  
196 43 ▶sub label { return unless ref $_[0]; return shift->{label} }
Subroutine label: called CC=2 SCAR=7.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] 0 43 43
197  
198 #pod =head2 runbook
199 #pod
200 #pod Read only accessor that returns the runbook registered with this object.
201 #pod
202 #pod =cut
203  
204 45 ▶sub runbook { return unless ref $_[0]; return shift->{runbook} }
Subroutine runbook: called CC=2 SCAR=7.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] 0 45 45
205  
206 #pod =head2 check
207 #pod
208 #pod my %results = %{ $diagnostic->check(%params) }
209 #pod
210 #pod This method is what is normally called by the L<HealthCheck> runner,
211 #pod but this version expects you to implement a L</run> method for the
212 #pod body of your diagnostic.
213 #pod This thin wrapper
214 #pod makes sure C<%params> is an even-sided list (possibly unpacking a hashref)
215 #pod before passing it to L</run>,
216 #pod trapping any exceptions,
217 #pod and passing the return value through L</summarize> unless a falsy
218 #pod C<summarize_result> parameter is passed.
219 #pod
220 #pod This could be used to validate parameters or to modify the the return value
221 #pod in some way.
222 #pod
223 #pod sub check {
224 #pod my ( $self, @params ) = @_;
225 #pod
226 #pod # Require check as an instance method
227 #pod croak("check cannot be called as a class method") unless ref $self;
228 #pod
229 #pod # Allow either a hashref or even-sized list of params
230 #pod my %params = @params == 1 && ( ref $params[0] || '' ) eq 'HASH'
231 #pod ? %{ $params[0] } : @params;
232 #pod
233 #pod # Validate any required parameters and that they look right.
234 #pod my $required_param = $params{required} || $self->{required};
235 #pod return {
236 #pod status => 'UNKNOWN',
237 #pod info => 'The "required" parameter is required',
238 #pod } unless $required_param and ref $required_param == 'HASH';
239 #pod
240 #pod # Calls $self->run and then passes the result through $self->summarize
241 #pod my $res = $self->SUPER::check( %params, required => $required_param );
242 #pod
243 #pod # Modify the result after it has been summarized
244 #pod delete $res->{required};
245 #pod
246 #pod # and return it
247 #pod return $res;
248 #pod }
249 #pod
250 #pod =cut
251  
252 47 ▶sub check {
Subroutine check: called CC=15 SCAR=27.5
Pod documented
253 47 my ( $class_or_self, @params ) = @_;
254  
255 # Allow either a hashref or even-sized list of params
256 my %params = @params == 1 && ( ref $params[0] || '' ) eq 'HASH'
257 0 ▶ ? %{ $params[0] } : @params;
Branch: 1 branch
BranchTrueFalseTotal
@params == 1 && (ref $params[0] || '') eq 'HASH' ? : 0 47 47
Condition20%1/5
@params == 1 and (ref $params[0] || "") eq "HASH"
!ll&&!rl&&r
4700
ref $params[0] || ''
l!l
00
MC/DC0%0/2
@params == 1 and (ref $params[0] || "") eq "HASH"
@params == 1 ref $params[0]
Truth table33%1/3
A and B
A= @params == 1B= ref $params[0]
ABresult
0X0
100
111
258  
259 47 ▶ my $class = ref $class_or_self || $class_or_self;
Condition66%2/3
ref $class_or_self || $class_or_self
l!l&&r!l&&!r
4160
MC/DC0%0/2
ref $class_or_self || $class_or_self
ref $class_or_self $class_or_self
Truth table66%2/3
A || B
A= ref $class_or_selfB= $class_or_self
ABresult
1X1
011
000
260 47 ▶ croak("$class does not implement a 'run' method")
Branch: 1 branch
BranchTrueFalseTotal
unless $class_or_self->can("run") 2 45 47
261 unless $class_or_self->can('run');
262  
263 my $summarize
264 = exists $params{summarize_result}
265 ? $params{summarize_result}
266 45 ▶ : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $params{'summarize_result'} ? : 5 40 45
267  
268 45 local $@;
269 45 ▶ my $start = $params{runtime} ? [ gettimeofday ] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$params{'runtime'} ? : 4 41 45
270 45 my @res = eval { $class_or_self->run(%params) };
271 45 ▶ @res = { status => 'CRITICAL', info => "$@" } if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 44 45
272  
273 45 ▶ if ( @res == 1 && ( ref $res[0] || '' ) eq 'HASH' ) { } # noop, OK
Branch: 2 branches
BranchTrueFalseTotal
if (@res == 1 and (ref $res[0] || '') eq 'HASH') { } 28 17 45
elsif (@res % 2 == 0) { } 13 4 17
Condition100%5/5
@res == 1 and (ref $res[0] || "") eq "HASH"
!ll&&!rl&&r
13428
ref $res[0] || ''
l!l
302
MC/DC100%2/2
@res == 1 and (ref $res[0] || "") eq "HASH"
@res == 1 ref $res[0]
Truth table100%3/3
A and B
A= @res == 1B= ref $res[0]
ABresult
0X0
100
111
274 13 elsif ( @res % 2 == 0 ) { @res = {@res}; }
275 else {
276 4 carp("Invalid return from $class\->run (@res)");
277 4 @res = { status => 'UNKNOWN' };
278 }
279  
280 45 ▶ $res[0]->{runtime} = sprintf "%.03f", tv_interval($start) if $start;
Branch: 1 branch
BranchTrueFalseTotal
if $start 4 41 45
281  
282 45 ▶ return $res[0] unless $summarize;
Branch: 1 branch
BranchTrueFalseTotal
unless $summarize 4 41 45
283 41 return $class_or_self->summarize(@res);
284 }
285  
286 #pod =head2 summarize
287 #pod
288 #pod %result = %{ $diagnostic->summarize( \%result ) };
289 #pod
290 #pod Validates, pre-formats, and returns the C<result> so that it is easily
291 #pod usable by HealthCheck.
292 #pod
293 #pod The attributes C<id>, C<label>, C<runbook>, and C<tags>
294 #pod get copied from the C<$diagnostic> into the C<result>
295 #pod if they exist in the former and not in the latter.
296 #pod
297 #pod The C<status> and C<info> are summarized when we have multiple
298 #pod C<results> in the C<result>. All of the C<info> values get appended
299 #pod together. One C<status> value is selected from the list of C<status>
300 #pod values.
301 #pod
302 #pod Used by L</check>.
303 #pod
304 #pod Carps a warning if validation fails on several keys, and sets the
305 #pod C<status> from C<OK> to C<UNKNOWN>.
306 #pod
307 #pod =over
308 #pod
309 #pod =item status
310 #pod
311 #pod Expects it to be one of C<OK>, C<WARNING>, C<CRITICAL>, or C<UNKNOWN>.
312 #pod
313 #pod Also carps if it does not exist.
314 #pod
315 #pod =item results
316 #pod
317 #pod Complains if it is not an arrayref.
318 #pod
319 #pod =item id
320 #pod
321 #pod Complains if the id contains anything but
322 #pod lowercase ascii letters, numbers, and underscores.
323 #pod
324 #pod =item timestamp
325 #pod
326 #pod Expected to look like an
327 #pod L<RFC 3339 timestamp|https://tools.ietf.org/html/rfc3339>
328 #pod which is a more strict subset of an ISO8601 timestamp.
329 #pod
330 #pod =back
331 #pod
332 #pod Modifies the passed in hashref in-place.
333 #pod
334 #pod =cut
335  
336 178 ▶sub summarize {
Subroutine summarize: called CC=2 SCAR=6.9
Pod documented
337 178 my ( $self, $result ) = @_;
338  
339 178 $self->_set_default_fields($result, qw(id label runbook tags));
340  
341 178 ▶ return $self->_summarize( $result, $result->{id} // 0 );
Condition100%2/2
$result->{"id"} // 0
l!l
22156
MC/DC100%1/1
$result->{"id"} // 0
$result->{"id"}
Truth table100%2/2
A
A= $result->{"id"}
Aresult
11
00
342 }
343  
344 209 ▶sub _set_default_fields {
Subroutine _set_default_fields: called CC=6 SCAR=17.9
345 209 my ($self, $target, @fields) = @_;
346 209 ▶ if ( ref $self ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) 71 138 209
347 71 $target->{$_} = ($_ eq 'tags' ? [ $self->$_ ] : $self->$_) for (
348 grep {
349 191 ▶ !exists($target->{$_}) &&
Branch: 2 branches
BranchTrueFalseTotal
$_ eq 'tags' ? : 70 99 169
$_ eq 'tags' ? : 23 32 55
Condition100%3/3
!exists $target->{$_} && ($_ eq 'tags' ? scalar $self->$_ : defined $self->$_)
!ll&&!rl&&r
2211455
MC/DC100%2/2
!exists $target->{$_} && ($_ eq 'tags' ? scalar $self->$_ : defined $self->$_)
!exists $target->{$_} ($_ eq 'tags' ? scalar $self->$_ : defined $self->$_)
Truth table100%3/3
not(A) && B
A= !exists $target->{$_}B= ($_ eq 'tags' ? scalar $self->$_ : defined $self->$_)
ABresult
0X0
100
111
350 ($_ eq 'tags' ? scalar($self->$_) : defined($self->$_))
351 }
352 @fields
353 );
354 }
355 }
356  
357 284 ▶sub _summarize {
Subroutine _summarize: called CC=42 SCAR=37.4
358 284 my ($self, $result, $id) = @_;
359  
360 # Indexes correspond to Nagios Plugin Return Codes
361 # https://assets.nagios.com/downloads/nagioscore/docs/nagioscore/3/en/pluginapi.html
362 284 state $forward = [ qw( OK WARNING CRITICAL UNKNOWN ) ];
363  
364 # The order of preference to inherit from a child. The highest priority
365 # has the lowest number.
366 284 state $statuses = { map { state $i = 1; $_ => $i++ } qw(
367 CRITICAL
368 WARNING
369 UNKNOWN
370 OK
371 ) };
372  
373 284 ▶ my $status = uc( $result->{status} || '' );
Condition100%2/2
$result->{'status'} || ''
l!l
23945
MC/DC100%1/1
$result->{'status'} || ''
$result->{'status'}
Truth table100%2/2
A
A= $result->{'status'}
Aresult
11
00
374 284 ▶ $status = '' unless exists $statuses->{$status};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $statuses->{$status} 54 230 284
375  
376 284 my @results;
377 284 ▶ if ( exists $result->{results} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $result->{"results"}) 45 239 284
378 45 ▶ if ( ( ref $result->{results} || '' ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((ref $result->{'results'} || '') eq 'ARRAY') { } 41 4 45
Condition100%2/2
ref $result->{'results'} || ''
l!l
423
MC/DC100%1/1
ref $result->{'results'} || ''
ref $result->{'results'}
Truth table100%2/2
A
A= ref $result->{'results'}
Aresult
11
00
379 41 @results = @{ $result->{results} };
380  
381 # Merge if there is only a single result.
382 41 ▶ if ( @results == 1 and $self->collapse_single_result ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@results == 1 and $self->collapse_single_result) 5 36 41
Condition100%3/3
@results == 1 and $self->collapse_single_result
!ll&&!rl&&r
24125
MC/DC100%2/2
@results == 1 and $self->collapse_single_result
@results == 1 $self->collapse_single_result
Truth table100%3/3
A and B
A= @results == 1B= $self->collapse_single_result
ABresult
0X0
100
111
383 5 my ($r) = @{ delete $result->{results} };
384 5 %{$result} = ( %{$result}, %{$r} );
385  
386 # Now that we've merged, need to redo everything again
387 5 return $self->_summarize($result, $id);
388 }
389 }
390 else {
391 my $disp
392 = defined $result->{results}
393 4 ▶ ? "invalid results '$result->{results}'"
Branch: 1 branch
BranchTrueFalseTotal
defined $result->{'results'} ? : 3 1 4
394 : 'undefined results';
395 4 carp("Result $id has $disp");
396 }
397 }
398  
399 279 my %seen_ids;
400 279 foreach my $i ( 0 .. $#results ) {
401 101 my $r = $results[$i];
402 101 ▶ $self->_summarize( $r, "$id-" . ( $r->{id} // $i ) );
Condition66%2/3
$r->{"id"} // $i
l!l&&r!l&&!r
52490
MC/DC0%0/2
$r->{"id"} // $i
$r->{"id"} $i
Truth table66%2/3
A // B
A= $r->{"id"}B= $i
ABresult
1X1
011
000
403  
404 # If this result has an ID we have seen already, append a number
405 101 ▶ if ( exists $r->{id} and my $i = $seen_ids{ $r->{id} // '' }++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $r->{"id"} and my $i = $seen_ids{$r->{"id"} // ""}++) 10 91 101
Condition100%5/5
exists $r->{"id"} and my $i = $seen_ids{$r->{"id"} // ""}++
!ll&&!rl&&r
454610
$r->{"id"} // ""
l!l
533
MC/DC100%2/2
exists $r->{"id"} and my $i = $seen_ids{$r->{"id"} // ""}++
exists $r->{"id"} my $i = $seen_ids{$r->{"id"} // ""}++
MC/DC100%1/1
$r->{"id"} // ""
$r->{"id"}
Truth table100%3/3
A and B
A= exists $r->{"id"}B= my $i = $seen_ids{$r->{"id"} // ""}++
ABresult
0X0
100
111
Truth table100%2/2
A
A= $r->{"id"}
Aresult
11
00
406 10 ▶ $r->{id} .= defined $r->{id} && length $r->{id} ? "_$i" : $i;
Branch: 1 branch
BranchTrueFalseTotal
defined $r->{'id'} && length $r->{'id'} ? : 5 5 10
Condition100%3/3
defined $r->{"id"} and length $r->{"id"}
!ll&&!rl&&r
235
MC/DC100%2/2
defined $r->{"id"} and length $r->{"id"}
defined $r->{"id"} length $r->{"id"}
Truth table100%3/3
A and B
A= defined $r->{"id"}B= length $r->{"id"}
ABresult
0X0
100
111
407 }
408  
409 101 ▶ if ( defined( my $s = $r->{status} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $s = $r->{"status"})) 101 0 101
410 101 $s = uc $s;
411 101 ▶ $s = $forward->[$s] if $s =~ /^[0-3]$/;
Branch: 1 branch
BranchTrueFalseTotal
if $s =~ /^[0-3]$/ 4 97 101
412  
413 $status = $s
414 if exists $statuses->{$s}
415 101 ▶ and $statuses->{$s} < ( $statuses->{$status} // 5 );
Branch: 1 branch
BranchTrueFalseTotal
if exists $statuses->{$s} and $statuses->{$s} < ($statuses->{$status} // 5) 37 64 101
Condition100%5/5
exists $statuses->{$s} and $statuses->{$s} < ($statuses->{$status} // 5)
!ll&&!rl&&r
65837
$statuses->{$status} // 5
l!l
6332
MC/DC100%2/2
exists $statuses->{$s} and $statuses->{$s} < ($statuses->{$status} // 5)
exists $statuses->{$s} $statuses->{$s} < ($statuses->{$status} // 5)
MC/DC100%1/1
$statuses->{$status} // 5
$statuses->{$status}
Truth table100%3/3
A and B
A= exists $statuses->{$s}B= $statuses->{$s} < ($statuses->{$status} // 5)
ABresult
0X0
100
111
Truth table100%2/2
A
A= $statuses->{$status}
Aresult
11
00
416 }
417 }
418  
419 # If we've found a valid status in our children,
420 # use that if we don't have our own.
421 # Removing the // here will force "worse" status inheritance
422 279 ▶ $result->{status} //= $status if $status;
Branch: 1 branch
BranchTrueFalseTotal
if $status 262 17 279
Condition66%2/3
$result->{"status"} //= $status
l!l&&r!l&&!r
230320
MC/DC0%0/2
$result->{"status"} //= $status
$result->{"status"} $status
Truth table66%2/3
A //= B
A= $result->{"status"}B= $status
ABresult
1X1
011
000
423  
424 279 my @errors;
425  
426 279 ▶ if ( exists $result->{id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $result->{"id"}) 79 200 279
427 79 my $rid = $result->{id};
428 79 ▶ unless ( defined $rid and $rid =~ /^[a-z0-9_]+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rid and $rid =~ /^[a-z0-9_]+$/) 14 65 79
Condition100%3/3
defined $rid and $rid =~ /^[a-z0-9_]+$/
!ll&&!rl&&r
41065
MC/DC100%2/2
defined $rid and $rid =~ /^[a-z0-9_]+$/
defined $rid $rid =~ /^[a-z0-9_]+$/
Truth table100%3/3
A and B
A= defined $ridB= $rid =~ /^[a-z0-9_]+$/
ABresult
0X0
100
111
429 14 ▶ push @errors, defined $rid ? "invalid id '$rid'" : 'undefined id';
Branch: 1 branch
BranchTrueFalseTotal
defined $rid ? : 10 4 14
430 }
431 }
432  
433 279 ▶ if ( exists $result->{timestamp} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $result->{"timestamp"}) 125 154 279
434 125 my $ts = $result->{timestamp};
435 125 ▶ unless ( defined $ts and $ts =~ /$rfc3339_timestamp/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ts and $ts =~ /$rfc3339_timestamp/) 121 4 125
Condition66%2/3
defined $ts and $ts =~ /$rfc3339_timestamp/
!ll&&!rl&&r
01214
MC/DC50%1/2
defined $ts and $ts =~ /$rfc3339_timestamp/
defined $ts $ts =~ /$rfc3339_timestamp/
Truth table66%2/3
A and B
A= defined $tsB= $ts =~ /$rfc3339_timestamp/
ABresult
0X0
100
111
436 121 ▶ my $disp_timestamp
Branch: 1 branch
BranchTrueFalseTotal
defined $ts ? : 121 0 121
437 = defined $ts
438 ? "invalid timestamp '$ts'"
439 : 'undefined timestamp';
440 121 push @errors, "$disp_timestamp";
441 }
442 }
443  
444 279 ▶ if ( not exists $result->{status} ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not exists $result->{'status'}) { } 5 274 279
elsif (not defined $result->{'status'}) { } 1 273 274
elsif (not exists $$statuses{uc($result->{'status'} // '')}) { } 11 262 273
445 5 push @errors, "missing status";
446 }
447 elsif ( not defined $result->{status} ) {
448 1 push @errors, "undefined status";
449 }
450 ▶ elsif ( not exists $statuses->{ uc( $result->{status} // '' ) } ) {
Condition50%1/2
$result->{"status"} // ""
l!l
2730
MC/DC0%0/1
$result->{"status"} // ""
$result->{"status"}
Truth table50%1/2
A
A= $result->{"status"}
Aresult
11
00
451 11 push @errors, "invalid status '$result->{status}'";
452 }
453  
454 $result->{status} = 'UNKNOWN'
455 279 ▶ unless defined $result->{status} and length $result->{status};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result->{"status"} and length $result->{"status"} 7 272 279
Condition100%3/3
defined $result->{"status"} and length $result->{"status"}
!ll&&!rl&&r
61272
MC/DC100%2/2
defined $result->{"status"} and length $result->{"status"}
defined $result->{"status"} length $result->{"status"}
Truth table100%3/3
A and B
A= defined $result->{"status"}B= length $result->{"status"}
ABresult
0X0
100
111
456  
457 279 ▶ if (@errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (@errors) 152 127 279
458 152 carp("Result $id has $_") for @errors;
459 $result->{status} = 'UNKNOWN'
460 if $result->{status}
461 and $statuses->{ $result->{status} }
462 152 ▶ and $statuses->{UNKNOWN} < $statuses->{ $result->{status} };
Branch: 1 branch
BranchTrueFalseTotal
if $result->{"status"} and $$statuses{$result->{"status"}} and $statuses->{"UNKNOWN"} < $$statuses{$result->{"status"}} 135 17 152
Condition100%6/6
$result->{"status"} and $$statuses{$result->{"status"}} and $statuses->{"UNKNOWN"} < $$statuses{$result->{"status"}}
!ll&&!rl&&r
107135
$result->{"status"} and $$statuses{$result->{"status"}}
!ll&&!rl&&r
19142
MC/DC100%3/3
$result->{"status"} and $$statuses{$result->{"status"}} and $statuses->{"UNKNOWN"} < $$statuses{$result->{"status"}}
$result->{"status"} $$statuses{$result->{"status"}} $statuses->{"UNKNOWN"} < $$statuses{$result->{"status"}}
Truth table100%4/4
A and B and C
A= $result->{"status"}B= $$statuses{$result->{"status"}}C= $statuses->{"UNKNOWN"} < $$statuses{$result->{"status"}}
ABCresult
0XX0
10X0
1100
1111
463 152 $result->{info} = join "\n", grep {$_} $result->{info}, @errors;
464 }
465  
466 279 return $result;
467 }
468  
469 4 1;
470  
471 __END__
↑ Summary HealthCheck.pm »