blib/lib/Test2/Tools/QuickDB.pm

filter: Statementstmt 78.2% 61 / 78 Branchbran 65.7% 25 / 38 Conditioncond 76.9% 20 / 26 MC/DCmcdc 58.3% 7 / 12 Subroutinesub 100% 11 / 11 Podpod 80.0% 4 / 5 totaltotal 75.2% 128 / 170 CC 31 SCAR 27.1
CC
31
Cov
75%
skipall_unless_can_db
27.7
resource_exhaustion_reason
10.9
get_db_or_skipall
4.9
CRAP 15.0
« blib/lib/DBIx/QuickDB/Driver/DuckDB.pm ↑ Summary blib/lib/DBIx/QuickDB/Driver/MariaDB.pm »
1 package Test2::Tools::QuickDB;
2 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 10 our $VERSION = '0.000066';
6  
7 10 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Test2::API qw/context/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use DBIx::QuickDB();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 10 ▶use Importer Importer => 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 10 our @EXPORT = qw/get_db_or_skipall get_db skipall_unless_can_db skipall_on_resource_error/;
14  
15 # Match the errors a host throws when it cannot give a database server the
16 # System V IPC resources it needs to start -- semaphore or shared-memory table
17 # exhaustion. These come through as the failed initdb/start command's captured
18 # output. Accept PostgreSQL's ENOSPC text (or its ENOSPC-only SEMMNI/SEMMNS
19 # hint), plus driver-neutral semget()/shmget() failures that explicitly report
20 # ENOSPC. The failed syscall alone is insufficient: it can also fail for
21 # actionable reasons such as EACCES. Returns a human reason if the error is
22 # specifically resource exhaustion, else undef.
23 12 ▶sub resource_exhaustion_reason {
Subroutine resource_exhaustion_reason: called CC=10 SCAR=23.9
Pod undocumented
24 12 my ($err) = @_;
25 12 ▶ return undef unless defined $err;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $err 0 12 12
26  
27 12 ▶ return "host is out of System V semaphores (cannot start a database server)"
Branch: 1 branch
BranchTrueFalseTotal
if $err =~ /could not create semaphores[^\n]*No space left on device/i or $err =~ /could not create semaphores/i and $err =~ /\bSEMM(?:NI|NS)\b.*\b(?:would be exceeded|need to raise)\b/is or $err =~ /\bsemget\(/ and $err =~ /No space left on device|\berrno[:= ]*28\b/i 7 5 12
Condition83%10/12
$err =~ /could not create semaphores[^\n]*No space left on device/i or $err =~ /could not create semaphores/i and $err =~ /\bSEMM(?:NI|NS)\b.*\b(?:would be exceeded|need to raise)\b/is or $err =~ /\bsemget\(/ and $err =~ /No space left on device|\berrno[:= ]*28\b/i
l!l&&r!l&&!r
615
$err =~ /could not create semaphores[^\n]*No space left on device/i or $err =~ /could not create semaphores/i and $err =~ /\bSEMM(?:NI|NS)\b.*\b(?:would be exceeded|need to raise)\b/is
l!l&&r!l&&!r
606
$err =~ /could not create semaphores/i and $err =~ /\bSEMM(?:NI|NS)\b.*\b(?:would be exceeded|need to raise)\b/is
!ll&&!rl&&r
510
$err =~ /\bsemget\(/ and $err =~ /No space left on device|\berrno[:= ]*28\b/i
!ll&&!rl&&r
321
MC/DC60%3/5
$err =~ /could not create semaphores[^\n]*No space left on device/i or $err =~ /could not create semaphores/i and $err =~ /\bSEMM(?:NI|NS)\b.*\b(?:would be exceeded|need to raise)\b/is or $err =~ /\bsemget\(/ and $err =~ /No space left on device|\berrno[:= ]*28\b/i
$err =~ /could not create semaphores[^\n]*No space left on device/i $err =~ /could not create semaphores/i $err =~ /\bSEMM(?:NI|NS)\b.*\b(?:would be exceeded|need to raise)\b/is $err =~ /\bsemget\(/ $err =~ /No space left on device|\berrno[:= ]*28\b/i
Truth table62%5/8
A or B and C or D and E
A= $err =~ /could not create semaphores[^\n]*No space left on device/iB= $err =~ /could not create semaphores/iC= $err =~ /\bSEMM(?:NI|NS)\b.*\b(?:would be exceeded|need to raise)\b/isD= $err =~ /\bsemget\(/E= $err =~ /No space left on device|\berrno[:= ]*28\b/i
ABCDEresult
1XXXX1
011XX1
00X111
010111
00X0X0
00X100
0100X0
010100
28 if $err =~ /could not create semaphores[^\n]*No space left on device/i
29 || ($err =~ /could not create semaphores/i
30 && $err =~ /\bSEMM(?:NI|NS)\b.*\b(?:would be exceeded|need to raise)\b/is)
31 || ($err =~ /\bsemget\(/
32 && $err =~ /No space left on device|\berrno[:= ]*28\b/i);
33  
34 5 ▶ return "host is out of System V shared memory (cannot start a database server)"
Branch: 1 branch
BranchTrueFalseTotal
if $err =~ /could not create shared memory[^\n]*No space left on device/i or $err =~ /\bshmget\(/ and $err =~ /No space left on device|\berrno[:= ]*28\b/i 1 4 5
Condition50%3/6
$err =~ /could not create shared memory[^\n]*No space left on device/i or $err =~ /\bshmget\(/ and $err =~ /No space left on device|\berrno[:= ]*28\b/i
l!l&&r!l&&!r
104
$err =~ /\bshmget\(/ and $err =~ /No space left on device|\berrno[:= ]*28\b/i
!ll&&!rl&&r
400
MC/DC33%1/3
$err =~ /could not create shared memory[^\n]*No space left on device/i or $err =~ /\bshmget\(/ and $err =~ /No space left on device|\berrno[:= ]*28\b/i
$err =~ /could not create shared memory[^\n]*No space left on device/i $err =~ /\bshmget\(/ $err =~ /No space left on device|\berrno[:= ]*28\b/i
Truth table50%2/4
A or B and C
A= $err =~ /could not create shared memory[^\n]*No space left on device/iB= $err =~ /\bshmget\(/C= $err =~ /No space left on device|\berrno[:= ]*28\b/i
ABCresult
1XX1
0111
00X0
0100
35 if $err =~ /could not create shared memory[^\n]*No space left on device/i
36 || ($err =~ /\bshmget\(/
37 && $err =~ /No space left on device|\berrno[:= ]*28\b/i);
38  
39 4 return undef;
40 }
41  
42 # If $err is a host IPC-exhaustion error, skip the whole test (it is an
43 # environment limit, not a fault in this distribution) -- this does not return,
44 # it terminates like skip_all. Otherwise returns false so the caller can rethrow.
45 9 ▶sub skipall_on_resource_error {
Subroutine skipall_on_resource_error: called CC=2 SCAR=7.2
Pod documented
46 9 my ($err) = @_;
47  
48 9 ▶ my $reason = resource_exhaustion_reason($err) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $reason = resource_exhaustion_reason($err) or return 0 5 4 9
49  
50 5 my $ctx = context();
51 5 $ctx->plan(0, SKIP => ucfirst($reason));
52 0 $ctx->release;
53  
54 0 return 1;
55 }
56  
57 12 ▶sub skipall_unless_can_db {
Subroutine skipall_unless_can_db: called CC=17 SCAR=33.2
Pod documented
58 12 my %spec;
59 12 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 1 11 12
60 1 ▶ my $type = ref($_[0]) || '';
Condition50%1/2
ref $_[0] || ''
l!l
10
MC/DC0%0/1
ref $_[0] || ''
ref $_[0]
Truth table50%1/2
A
A= ref $_[0]
Aresult
11
00
61 1 ▶ if (!$type) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $type) { } 0 1 1
elsif ($type eq 'ARRAY') { } 1 0 1
elsif ($type eq 'HASH') { } 0 0 0
62 0 $spec{driver} = $_[0];
63 }
64 elsif ($type eq 'ARRAY') {
65 1 $spec{drivers} = $_[0];
66 }
67 elsif ($type eq 'HASH') {
68 0 %spec = %{$_[0]};
69 }
70 else {
71 0 croak "Invalid Argument: $_[0]";
72 }
73 }
74 else {
75 11 %spec = @_;
76 }
77  
78 12 my $ctx = context();
79  
80 12 ▶ $spec{bootstrap} = 1 unless defined $spec{bootstrap};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spec{"bootstrap"} 11 1 12
81 12 ▶ $spec{autostart} = 1 unless defined $spec{autostart};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spec{"autostart"} 12 0 12
82 12 ▶ $spec{load_sql} = 1 unless defined $spec{load_sql};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spec{"load_sql"} 12 0 12
83  
84 12 ▶ my $drivers = $spec{driver} ? [$spec{driver}] : $spec{drivers} || [DBIx::QuickDB->plugins];
Branch: 1 branch
BranchTrueFalseTotal
$spec{'driver'} ? : 6 6 12
Condition100%2/2
$spec{"drivers"} or ["DBIx::QuickDB"->plugins]
l!l
33
MC/DC100%1/1
$spec{"drivers"} or ["DBIx::QuickDB"->plugins]
$spec{"drivers"}
Truth table100%2/2
A
A= $spec{"drivers"}
Aresult
11
00
85  
86 12 my $reason;
87 12 my $ok = 0;
88 12 for my $driver (@$drivers) {
89 37 ▶ next unless defined $driver;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $driver 0 37 37
90 37 my ($v, $fqn, $why) = DBIx::QuickDB->check_driver($driver, \%spec);
91 37 ▶ $reason = $why if @$drivers == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$drivers == 1 6 31 37
92 37 ▶ next unless $v;
Branch: 1 branch
BranchTrueFalseTotal
unless $v 37 0 37
93 0 $ok = $fqn;
94 0 last;
95 }
96  
97 12 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 0 12 12
98 0 $ctx->release;
99 0 return $ok;
100 }
101  
102 12 ▶ $ctx->plan(0, 'SKIP' => $reason || "no db driver is viable");
Condition100%2/2
$reason || 'no db driver is viable'
l!l
66
MC/DC100%1/1
$reason || 'no db driver is viable'
$reason
Truth table100%2/2
A
A= $reason
Aresult
11
00
103 0 $ctx->release;
104  
105 0 return;
106 }
107  
108 2 ▶sub get_db {
Subroutine get_db: called CC=2 SCAR=7.3
Pod documented
109 # Get a context in case anything below here has testing code.
110 2 my $ctx = context();
111  
112 2 my $db = eval { DBIx::QuickDB->build_db(@_) };
113 2 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 2 0 2
114 # A host out of System V semaphores/shared memory cannot start a server;
115 # that is an environment limit, not a fault here, so skip rather than
116 # fail. skipall_on_resource_error() terminates the test in that case;
117 # any other error is real and is rethrown.
118 2 skipall_on_resource_error($err);
119 1 $ctx->release;
120 1 die $err;
121 }
122  
123 0 $ctx->release;
124  
125 0 return $db;
126 }
127  
128 7 ▶sub get_db_or_skipall {
Subroutine get_db_or_skipall: called CC=4 SCAR=15.9
Pod documented
129 7 ▶ my $name = ref($_[0]) ? undef : shift(@_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 5 2 7
130 7 ▶ my $spec = shift(@_) || {};
Condition100%2/2
shift @_ || {}
l!l
61
MC/DC100%1/1
shift @_ || {}
shift @_
Truth table100%2/2
A
A= shift @_
Aresult
11
00
131  
132 7 my $ctx = context();
133  
134 7 skipall_unless_can_db(%$spec);
135 0 ▶ my $db = get_db($name ? $name : (), $spec);
Branch: 1 branch
BranchTrueFalseTotal
$name ? : 0 0 0
136  
137 0 $ctx->release;
138  
139 0 return $db;
140 }
141  
142 10 1;
143  
144 __END__
« blib/lib/DBIx/QuickDB/Driver/DuckDB.pm ↑ Summary blib/lib/DBIx/QuickDB/Driver/MariaDB.pm »