blib/lib/HealthCheck/Diagnostic/DBHCheck.pm

filter: Statementstmt 96.3% 79 / 82 Branchbran 92.1% 35 / 38 Conditioncond 63.4% 26 / 41 MC/DCmcdc 38.1% 8 / 21 Subroutinesub 100% 12 / 12 Podpod 100% 3 / 3 totaltotal 82.7% 163 / 197 CC 35 SCAR 24.2
CC
35
Cov
87%
check
19.1
run
9.4
new
7.6
CRAP 11.3
↑ Summary
1 package HealthCheck::Diagnostic::DBHCheck;
2  
3 # ABSTRACT: Check a database handle to make sure you have read/write access
4 2 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 our $VERSION = 'v1.0.1'; # VERSION
6  
7 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use parent 'HealthCheck::Diagnostic';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 7 ▶sub new {
Subroutine new: called CC=6 SCAR=20.3
Pod documented
16 7 my ($class, @params) = @_;
17  
18 # Allow either a hashref or even-sized list of params
19 my %params = @params == 1 && ( ref $params[0] || '' ) eq 'HASH'
20 0 ▶ ? %{ $params[0] } : @params;
Branch: 1 branch
BranchTrueFalseTotal
@params == 1 && (ref $params[0] || '') eq 'HASH' ? : 0 7 7
Condition20%1/5
@params == 1 and (ref $params[0] || "") eq "HASH"
!ll&&!rl&&r
700
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
21  
22 croak("The 'dbh' parameter should be a coderef!")
23 7 ▶ if ($params{dbh} && (ref $params{dbh} ne "CODE"));
Branch: 1 branch
BranchTrueFalseTotal
if $params{"dbh"} and ref $params{"dbh"} ne "CODE" 1 6 7
Condition100%3/3
$params{"dbh"} and ref $params{"dbh"} ne "CODE"
!ll&&!rl&&r
151
MC/DC100%2/2
$params{"dbh"} and ref $params{"dbh"} ne "CODE"
$params{"dbh"} ref $params{"dbh"} ne "CODE"
Truth table100%3/3
A and B
A= $params{"dbh"}B= ref $params{"dbh"} ne "CODE"
ABresult
0X0
100
111
24  
25 6 return $class->SUPER::new(
26 tags => [ 'dbh_check' ],
27 label => 'dbh_check',
28 %params
29 );
30 }
31  
32 16 ▶sub check {
Subroutine check: called CC=19 SCAR=29.5
Pod documented
33 16 my ( $self, %params ) = @_;
34  
35 # 1st, try to get dbh from provided parameters
36 16 my $dbh = $params{dbh};
37 # 2nd, if invoked with an object (not the class), then get dbh from object
38 16 ▶ $dbh ||= $self->{dbh} if ref $self;
Branch: 1 branch
BranchTrueFalseTotal
if ref $self 6 10 16
Condition66%2/3
$dbh ||= $self->{"dbh"}
l!l&&r!l&&!r
051
MC/DC50%1/2
$dbh ||= $self->{"dbh"}
$dbh $self->{"dbh"}
Truth table66%2/3
A ||= B
A= $dbhB= $self->{"dbh"}
ABresult
1X1
011
000
39  
40 16 ▶ croak "Valid 'dbh' is required" unless $dbh;
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh 2 14 16
41  
42 14 ▶ croak "The 'dbh' parameter should be a coderef!"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $dbh eq "CODE" 1 13 14
43 unless ref $dbh eq "CODE";
44  
45 my $db_access = $params{db_access} # Provided call to check()
46 // ((ref $self) && $self->{db_access}) # Value from new()
47 13 ▶ || "rw"; # default value
Condition87%7/8
($params{'db_access'} // ref $self && $self->{'db_access'}) || 'rw'
l!l
211
$params{"db_access"} // ref $self && $self->{"db_access"}
l!l&&r!l&&!r
0103
ref $self and $self->{"db_access"}
!ll&&!rl&&r
832
MC/DC66%2/3
($params{'db_access'} // ref $self && $self->{'db_access'}) || 'rw'
$params{"db_access"} ref $self $self->{"db_access"}
Truth table75%3/4
A // B and C
A= $params{"db_access"}B= ref $selfC= $self->{"db_access"}
ABCresult
1XX1
0111
00X0
0100
48  
49 13 ▶ croak "The value '$db_access' is not valid for the 'db_access' parameter"
Branch: 1 branch
BranchTrueFalseTotal
unless $db_access =~ /^r[ow]$/ 1 12 13
50 unless $db_access =~ /^r[ow]$/;
51  
52 my $timeout =
53 defined $params{timeout} ? $params{timeout} :
54 (ref $self) && (defined $self->{timeout}) ? $self->{timeout} :
55 12 ▶ 10;
Branch: 2 branches
BranchTrueFalseTotal
defined $params{'timeout'} ? : 1 11 12
ref $self && defined $self->{'timeout'} ? : 1 10 11
Condition100%3/3
ref $self and defined $self->{"timeout"}
!ll&&!rl&&r
731
MC/DC100%2/2
ref $self and defined $self->{"timeout"}
ref $self defined $self->{"timeout"}
Truth table100%3/3
A and B
A= ref $selfB= defined $self->{"timeout"}
ABresult
0X0
100
111
56  
57 12 local $@;
58 12 eval {
59 12 local $SIG{__DIE__};
60 12 ▶ local $SIG{ALRM} = sub { die "timeout after $timeout seconds.\n" };
Subroutine __ANON__: called CC=1 SCAR=0.0
61 12 alarm $timeout;
62 12 $dbh = $dbh->(%params);
63 };
64 12 alarm 0;
65  
66 12 ▶ if ( $@ =~ /^timeout/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ /^timeout/) 2 10 12
67 2 chomp $@;
68 return {
69 2 status => 'CRITICAL',
70 info => "Database connection $@",
71 };
72 }
73  
74 # re-throw any other exceptions
75 10 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 2 8 10
76  
77 8 ▶ croak "The 'dbh' coderef should return an object!"
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($dbh) 2 6 8
78 unless blessed $dbh;
79  
80 my $db_class = $params{db_class} # Provided in call to check()
81 // ((ref $self) && $self->{db_class}) # Value from new
82 6 ▶ || "DBI::db"; # default value
Condition62%5/8
($params{'db_class'} // ref $self && $self->{'db_class'}) || 'DBI::db'
l!l
06
$params{"db_class"} // ref $self && $self->{"db_class"}
l!l&&r!l&&!r
033
ref $self and $self->{"db_class"}
!ll&&!rl&&r
330
MC/DC0%0/3
($params{'db_class'} // ref $self && $self->{'db_class'}) || 'DBI::db'
$params{"db_class"} ref $self $self->{"db_class"}
Truth table50%2/4
A // B and C
A= $params{"db_class"}B= ref $selfC= $self->{"db_class"}
ABCresult
1XX1
0111
00X0
0100
83  
84 6 my $isa = ref $dbh;
85  
86 6 ▶ croak "The 'dbh' coderef should return a '$db_class', not a '$isa'"
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh->isa($db_class) 1 5 6
87 unless $dbh->isa($db_class);
88  
89 5 my $res = $self->SUPER::check(
90 %params,
91 dbh => $dbh,
92 db_access => $db_access,
93 db_class => $db_class
94 );
95 5 delete $res->{dbh}; # don't include the object in the result
96  
97 5 return $res;
98 }
99  
100  
101 2 ▶sub _read_write_temp_table {
Subroutine _read_write_temp_table: called CC=4 SCAR=14.2
102 2 my (%params) = @_;
103 2 my $dbh = $params{dbh};
104 2 ▶ my $table = $params{table_name} // "__DBH_CHECK__";
Condition50%1/2
$params{"table_name"} // "__DBH_CHECK__"
l!l
02
MC/DC0%0/1
$params{"table_name"} // "__DBH_CHECK__"
$params{"table_name"}
Truth table50%1/2
A
A= $params{"table_name"}
Aresult
11
00
105 2 my $status = "CRITICAL";
106  
107 2 my $qtable = $dbh->quote_identifier($table);
108  
109 # Drop it like it's hot
110 2 $dbh->do("DROP TEMPORARY TABLE IF EXISTS $qtable");
111  
112 2 $dbh->do(
113 join(
114 "",
115 "CREATE TEMPORARY TABLE IF NOT EXISTS $qtable (",
116 "check_id INTEGER PRIMARY KEY,",
117 "check_string VARCHAR(64) NOT NULL",
118 ")"
119 )
120 );
121  
122 2 $dbh->do(
123 join(
124 "",
125 "INSERT INTO $qtable ",
126 " (check_id, check_string) ",
127 "VALUES (1, 'Hello world')",
128 )
129 );
130 2 my @row = $dbh->selectrow_array(
131 "SELECT check_string FROM $qtable WHERE check_id = 1"
132 );
133  
134 2 ▶ $status = "OK" if ($row[0] && ($row[0] eq "Hello world"));
Branch: 1 branch
BranchTrueFalseTotal
if $row[0] and $row[0] eq "Hello world" 2 0 2
Condition33%1/3
$row[0] and $row[0] eq "Hello world"
!ll&&!rl&&r
002
MC/DC0%0/2
$row[0] and $row[0] eq "Hello world"
$row[0] $row[0] eq "Hello world"
Truth table33%1/3
A and B
A= $row[0]B= $row[0] eq "Hello world"
ABresult
0X0
100
111
135  
136 2 $dbh->do("DROP TEMPORARY TABLE $qtable");
137  
138 2 return $status;
139 }
140  
141 5 ▶sub run {
Subroutine run: called CC=9 SCAR=22.5
Pod documented
142 5 my ( $self, %params ) = @_;
143 5 my $dbh = $params{dbh};
144  
145 # Get db_access from parameters
146 5 my $read_write = ($params{db_access} =~ /^rw$/i);
147  
148 5 my $status = "OK";
149  
150 RUN_TESTS: {
151  
152 # See if we can ping the DB connection
153 5 ▶ if ($dbh->can("ping") && !$dbh->ping) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh->can("ping") and not $dbh->ping) 2 3 5
Condition66%2/3
$dbh->can("ping") and not $dbh->ping
!ll&&!rl&&r
032
MC/DC50%1/2
$dbh->can("ping") and not $dbh->ping
$dbh->can("ping") not $dbh->ping
Truth table66%2/3
A and not(B)
A= $dbh->can("ping")B= not $dbh->ping
ABresult
0X0
100
111
154 2 $status = "CRITICAL";
155 2 last RUN_TESTS;
156 }
157  
158 # See if a simple SELECT works
159 3 my $value = eval { $dbh->selectrow_array("SELECT 1"); };
160 3 ▶ unless (defined $value && $value == 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value and $value == 1) 0 3 3
Condition33%1/3
defined $value and $value == 1
!ll&&!rl&&r
003
MC/DC0%0/2
defined $value and $value == 1
defined $value $value == 1
Truth table33%1/3
A and B
A= defined $valueB= $value == 1
ABresult
0X0
100
111
161 0 $status = "CRITICAL";
162 0 last RUN_TESTS;
163 }
164  
165 3 ▶ $status = _read_write_temp_table(%params) if $read_write;
Branch: 1 branch
BranchTrueFalseTotal
if $read_write 2 1 3
166 }
167  
168 # Generate the human readable info string
169 my $info = sprintf(
170 "%s %s %s check of %s%s",
171 $status eq "OK" ? "Successful" : "Unsuccessful",
172 $dbh->{Driver}->{Name},
173 $read_write ? "read write" : "read only",
174 $dbh->{Name},
175 5 ▶ $dbh->{Username} ? " as $dbh->{Username}" : "",
Branch: 3 branches
BranchTrueFalseTotal
$status eq 'OK' ? : 3 2 5
$read_write ? : 4 1 5
$dbh->{'Username'} ? : 2 3 5
176 );
177  
178 5 return { status => $status, info => $info };
179 }
180  
181 2 1;
182  
183 __END__
↑ Summary