blib/lib/DBIO/Test/Kubernetes.pm

filter: Statementstmt 76.9% 197 / 256 Branchbran 68.1% 60 / 88 Conditioncond 52.6% 40 / 76 MC/DCmcdc 24.3% 10 / 41 Subroutinesub 89.4% 34 / 38 Podpod 100% 16 / 16 totaltotal 69.3% 357 / 515 CC 85 SCAR 26.6
CC
85
Cov
71%
setup_port_forwards
56.0
wait_for_job
50.6
new
17.4
CRAP 14.3
« blib/lib/DBIO/ResultSource/RowParser/Util.pm ↑ Summary blib/lib/DBIO/Storage/PoolBase.pm »
1 package DBIO::Test::Kubernetes;
2 # ABSTRACT: Provision temporary database pods in Kubernetes for DBIO testing
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 our $VERSION = '0.900007';
7  
8 6 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use DBIO::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use DBIO::Optional::Dependencies ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use DBIO::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use POSIX qw(WNOHANG);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16  
17 19 ▶sub new {
Subroutine new: called CC=16 SCAR=28.6
Pod documented
18 19 my ($class, %args) = @_;
19  
20 my $kubeconfig = $args{kubeconfig}
21 // $ENV{DBIO_TEST_KUBECONFIG}
22 // $ENV{KUBECONFIG}
23 19 ▶ // DBIO::Util::file_path($ENV{HOME}, '.kube', 'config');
Condition33%3/9
(($args{"kubeconfig"} // $ENV{"DBIO_TEST_KUBECONFIG"}) // $ENV{"KUBECONFIG"}) // DBIO::Util::file_path($ENV{"HOME"}, ".kube", "config")
l!l&&r!l&&!r
1900
($args{"kubeconfig"} // $ENV{"DBIO_TEST_KUBECONFIG"}) // $ENV{"KUBECONFIG"}
l!l&&r!l&&!r
1900
$args{"kubeconfig"} // $ENV{"DBIO_TEST_KUBECONFIG"}
l!l&&r!l&&!r
1900
MC/DC0%0/4
(($args{"kubeconfig"} // $ENV{"DBIO_TEST_KUBECONFIG"}) // $ENV{"KUBECONFIG"}) // DBIO::Util::file_path($ENV{"HOME"}, ".kube", "config")
$args{"kubeconfig"} $ENV{"DBIO_TEST_KUBECONFIG"} $ENV{"KUBECONFIG"} DBIO::Util::file_path($ENV{"HOME"}, ".kube", "config")
Truth table20%1/5
A // B // C // D
A= $args{"kubeconfig"}B= $ENV{"DBIO_TEST_KUBECONFIG"}C= $ENV{"KUBECONFIG"}D= DBIO::Util::file_path($ENV{"HOME"}, ".kube", "config")
ABCDresult
1XXX1
01XX1
001X1
00011
00000
24  
25 19 ▶ croak "Kubeconfig not found: $kubeconfig" unless -f $kubeconfig;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $kubeconfig 0 19 19
26  
27 # Two ways to name the namespace, and they are not interchangeable: one is
28 # created and deleted by this run, the other is somebody else's and is
29 # neither. Which one applies is said by the caller, never worked out from
30 # what the cluster permits (see NAMESPACES).
31 DBIO::Exception->throw("Pass either namespace or existing_namespace, not both")
32 19 ▶ if defined $args{namespace} && exists $args{existing_namespace};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"namespace"} and exists $args{"existing_namespace"} 1 18 19
Condition100%3/3
defined $args{"namespace"} and exists $args{"existing_namespace"}
!ll&&!rl&&r
1621
MC/DC100%2/2
defined $args{"namespace"} and exists $args{"existing_namespace"}
defined $args{"namespace"} exists $args{"existing_namespace"}
Truth table100%3/3
A and B
A= defined $args{"namespace"}B= exists $args{"existing_namespace"}
ABresult
0X0
100
111
33 DBIO::Exception->throw("existing_namespace needs the name of the namespace to work in")
34 if exists $args{existing_namespace}
35 18 ▶ && !(defined $args{existing_namespace} && length $args{existing_namespace});
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"existing_namespace"} and not defined $args{"existing_namespace"} && length $args{"existing_namespace"} 2 16 18
Condition100%6/6
exists $args{"existing_namespace"} and not defined $args{"existing_namespace"} && length $args{"existing_namespace"}
!ll&&!rl&&r
6102
defined $args{'existing_namespace'} && length $args{'existing_namespace'}
!ll&&!rl&&r
1110
MC/DC100%3/3
exists $args{"existing_namespace"} and not defined $args{"existing_namespace"} && length $args{"existing_namespace"}
exists $args{"existing_namespace"} defined $args{'existing_namespace'} length $args{'existing_namespace'}
Truth table100%4/4
A and not(B && C)
A= exists $args{"existing_namespace"}B= defined $args{'existing_namespace'}C= length $args{'existing_namespace'}
ABCresult
0XX0
1110
10X1
1101
36  
37 16 ▶ my $existing = exists $args{existing_namespace} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'existing_namespace'} ? : 10 6 16
38 my $namespace = $existing
39 ? $args{existing_namespace}
40 16 ▶ : $args{namespace} // _generate_namespace();
Branch: 1 branch
BranchTrueFalseTotal
$existing ? : 10 6 16
Condition66%2/3
$args{"namespace"} // _generate_namespace()
l!l&&r!l&&!r
240
MC/DC0%0/2
$args{"namespace"} // _generate_namespace()
$args{"namespace"} _generate_namespace()
Truth table66%2/3
A // B
A= $args{"namespace"}B= _generate_namespace()
ABresult
1X1
011
000
41 16 ▶ my $databases = $args{databases} // [qw(pg mysql)];
Condition100%2/2
$args{"databases"} // ["pg", "mysql"]
l!l
133
MC/DC100%1/1
$args{"databases"} // ["pg", "mysql"]
$args{"databases"}
Truth table100%2/2
A
A= $args{"databases"}
Aresult
11
00
42  
43 # Validate database names
44 16 my %valid = map { $_ => 1 } qw(pg mysql pg-ext duckdb);
45 16 for my $db (@$databases) {
46 26 ▶ croak "Unknown database: $db (valid: pg, mysql, pg-ext, duckdb)" unless $valid{$db};
Branch: 1 branch
BranchTrueFalseTotal
unless $valid{$db} 0 26 26
47 }
48  
49 16 require Kubernetes::REST::Kubeconfig;
50 16 my $kc = Kubernetes::REST::Kubeconfig->new(
51 kubeconfig_path => $kubeconfig,
52 );
53 16 my $api = $kc->api;
54  
55 16 return bless {
56 kubeconfig => $kubeconfig,
57 namespace => $namespace,
58 existing_namespace => $existing,
59 run_id => _generate_suffix(),
60 databases => $databases,
61 api => $api,
62 port_forward_pids => [],
63 local_ports => {},
64 _created => [],
65 _deployed => 0,
66 _ready => 0,
67 }, $class;
68 }
69  
70  
71 81 ▶sub namespace { $_[0]->{namespace} }
Subroutine namespace: called CC=1 SCAR=0.0
Pod documented
72  
73  
74 25 ▶sub databases { $_[0]->{databases} }
Subroutine databases: called CC=1 SCAR=0.0
Pod documented
75  
76  
77 11036 ▶sub api { $_[0]->{api} }
Subroutine api: called CC=1 SCAR=0.0
Pod documented
78  
79  
80 0 ▶sub kubeconfig { $_[0]->{kubeconfig} }
Subroutine kubeconfig: not called CC=1 SCAR=6.9
Pod documented
81  
82  
83 3 ▶sub uses_existing_namespace { $_[0]->{existing_namespace} ? 1 : 0 }
Subroutine uses_existing_namespace: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'existing_namespace'} ? : 1 2 3
84  
85  
86 3 ▶sub created_objects { @{ $_[0]->{_created} // [] } }
Subroutine created_objects: called CC=2 SCAR=7.2
Pod documented
Condition50%1/2
$_[0]{"_created"} // []
l!l
30
MC/DC0%0/1
$_[0]{"_created"} // []
$_[0]{"_created"}
Truth table50%1/2
A
A= $_[0]{"_created"}
Aresult
11
00
87  
88 20 ▶sub _generate_suffix {
Subroutine _generate_suffix: called CC=1 SCAR=0.0
89 20 my @chars = ('a'..'z', '0'..'9');
90 20 return join '', map { $chars[rand @chars] } 1..8;
91 }
92  
93 4 ▶sub _generate_namespace { 'dbio-test-' . _generate_suffix() }
Subroutine _generate_namespace: called CC=1 SCAR=0.0
94  
95 # ============================================================================
96 # OBJECT NAMES AND LABELS
97 # ============================================================================
98  
99 # A namespace of our own is the isolation, so the names in it are the plain
100 # ones. In somebody else's namespace a plain name is a collision waiting to
101 # happen (a Pod `mysql` and a Service `mysql-svc` are exactly what a hand-run
102 # test database is called), so every name carries this run's id.
103 11002 ▶sub _object_name {
Subroutine _object_name: called CC=2 SCAR=6.9
104 11002 my ($self, $base) = @_;
105  
106 11002 ▶ return $base unless $self->{existing_namespace};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"existing_namespace"} 32 10970 11002
107 10970 return "dbio-test-$self->{run_id}-$base";
108 }
109  
110 8 ▶sub _job_name {
Subroutine _job_name: called CC=2 SCAR=6.9
111 8 my ($self) = @_;
112  
113 8 ▶ return 'dbio-test-runner' unless $self->{existing_namespace};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"existing_namespace"} 4 4 8
114 4 return $self->_object_name('runner');
115 }
116  
117 # Labels are the sharper edge. A Service selects by label, not by name: a pod
118 # of ours labelled app=mysql in a namespace that already has a Service
119 # selecting app=mysql becomes one of that Service's endpoints, and a share of
120 # its traffic lands on our throwaway pod. So in an existing namespace a pod
121 # carries only label keys that belong to this module, one of them the run id
122 # -- nothing a selector written by anybody else can match.
123 47 ▶sub _pod_labels {
Subroutine _pod_labels: called CC=2 SCAR=6.9
124 47 my ($self, $db) = @_;
125  
126 return { app => $db, role => 'dbio-test-db' }
127 47 ▶ unless $self->{existing_namespace};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"existing_namespace"} 18 29 47
128 29 return { 'dbio-test-run' => $self->{run_id}, 'dbio-test-db' => $db };
129 }
130  
131 # Service and Job are not selected by anything of ours, but they are left
132 # behind just as well by a run that dies. In an existing namespace they carry
133 # the run id as a label, so that one label finds every object of a run
134 # (maint/live-preflight). Metadata only: no selector reads a Service's or a
135 # Job's own labels. Empty list in a namespace of our own.
136 31 ▶sub _run_labels {
Subroutine _run_labels: called CC=2 SCAR=6.9
137 31 my ($self) = @_;
138  
139 31 ▶ return () unless $self->{existing_namespace};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"existing_namespace"} 14 17 31
140 17 return ( labels => { 'dbio-test-run' => $self->{run_id} } );
141 }
142  
143 # What the Service of a database selects: its own pod and no other -- in an
144 # existing namespace that takes the run id, or two concurrent runs would
145 # serve each other's connections.
146 24 ▶sub _pod_selector {
Subroutine _pod_selector: called CC=2 SCAR=6.9
147 24 my ($self, $db) = @_;
148  
149 24 ▶ return { app => $db } unless $self->{existing_namespace};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"existing_namespace"} 10 14 24
150 14 return $self->_pod_labels($db);
151 }
152  
153 # Every object goes through here, and is written down the moment the API has
154 # accepted it. cleanup works from that list alone, so a deploy that dies
155 # halfway leaves nothing behind, and nothing is ever deleted that this run did
156 # not create: a create that fails (the name is taken, the quota is full) is
157 # not recorded.
158 62 ▶sub _create {
Subroutine _create: called CC=1 SCAR=0.0
159 62 my ($self, $kind, %args) = @_;
160  
161 62 my $api = $self->api;
162 62 $api->create($api->new_object($kind => %args));
163 59 push @{ $self->{_created} }, { kind => $kind, name => $args{metadata}{name} };
164  
165 59 return;
166 }
167  
168 # ============================================================================
169 # RESOURCE LIMITS
170 # ============================================================================
171  
172 # The ONE place the default container resource envelope lives. Both the
173 # database pods and the in-cluster test Job resolve through here, so the call
174 # sites cannot drift apart. The values match the checked-in manifests of the
175 # driver distributions (maint/k8s/*.yaml): at most one core, because these
176 # suites run on small shared machines next to other databases and services.
177 #
178 # Returns a fresh hashref on every call -- the result is handed to the k8s
179 # object builder, which must not end up aliasing a shared structure.
180 30 ▶sub _default_resources {
Subroutine _default_resources: called CC=1 SCAR=0.0
181 return {
182 30 requests => { cpu => '250m', memory => '256Mi' },
183 limits => { cpu => '1', memory => '1Gi' },
184 };
185 }
186  
187 # Resolve the resources block for one container.
188 #
189 # A spec may carry its own `resources` to raise the ceiling for a heavier
190 # engine. What it may NOT do is ask for no limits: an unbounded container is
191 # precisely the failure this mechanism exists to prevent, and a forgotten one
192 # can take a shared host down with it.
193 #
194 # That case throws rather than warning-and-defaulting. A warning scrolls past
195 # in the provisioning output and the container gets created anyway, which is
196 # the exact shape we are trying to make unreachable; the caller learns nothing
197 # until the box is already under pressure. Throwing means an unlimited
198 # container cannot be produced from here at all -- a caller that wants a
199 # different ceiling has to name it.
200 #
201 # Half-specified limits (cpu but no memory, or the reverse) are rejected for
202 # the same reason: memory is what OOMs the host, cpu is what starves it, and
203 # leaving either open is not meaningfully safer than leaving both open.
204 40 ▶sub _resources_for {
Subroutine _resources_for: called CC=8 SCAR=20.8
205 40 my ($spec, $what) = @_;
206  
207 40 ▶ return _default_resources() unless exists $spec->{resources};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $spec->{"resources"} 30 10 40
208  
209 10 my $res = $spec->{resources};
210 10 ▶ my $limits = ref $res eq 'HASH' ? $res->{limits} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $res eq 'HASH' ? : 9 1 10
211  
212 croak "Refusing to create $what without cpu and memory limits: every "
213 . "container created here must carry resources.limits (see the "
214 . "RESOURCE LIMITS section of DBIO::Test::Kubernetes)"
215 unless ref $limits eq 'HASH'
216 && defined $limits->{cpu} && length $limits->{cpu}
217 10 ▶ && defined $limits->{memory} && length $limits->{memory};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $limits eq "HASH" and defined $limits->{"cpu"} and length $limits->{"cpu"} and defined $limits->{"memory"} and length $limits->{"memory"} 8 2 10
Condition83%10/12
ref $limits eq "HASH" and defined $limits->{"cpu"} and length $limits->{"cpu"} and defined $limits->{"memory"} and length $limits->{"memory"}
!ll&&!rl&&r
802
ref $limits eq "HASH" and defined $limits->{"cpu"} and length $limits->{"cpu"} and defined $limits->{"memory"}
!ll&&!rl&&r
712
ref $limits eq "HASH" and defined $limits->{"cpu"} and length $limits->{"cpu"}
!ll&&!rl&&r
703
ref $limits eq "HASH" and defined $limits->{"cpu"}
!ll&&!rl&&r
523
MC/DC60%3/5
ref $limits eq "HASH" and defined $limits->{"cpu"} and length $limits->{"cpu"} and defined $limits->{"memory"} and length $limits->{"memory"}
ref $limits eq "HASH" defined $limits->{"cpu"} length $limits->{"cpu"} defined $limits->{"memory"} length $limits->{"memory"}
Truth table66%4/6
A and B and C and D and E
A= ref $limits eq "HASH"B= defined $limits->{"cpu"}C= length $limits->{"cpu"}D= defined $limits->{"memory"}E= length $limits->{"memory"}
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
218  
219 2 return $res;
220 }
221  
222 # ============================================================================
223 # DATABASE SPECS
224 # ============================================================================
225  
226 6 ▶my %DB_SPECS = (
Branch: 1 branch
BranchTrueFalseTotal
DBIO::Optional::Dependencies::_module_findable('DBD/MariaDB.pm') ? : 0 6 6
227 pg => {
228 name => 'pg',
229 image => 'postgres:16',
230 port => 5432,
231 svc_name => 'pg-svc',
232 env => [
233 { name => 'POSTGRES_PASSWORD', value => 'dbiotest' },
234 { name => 'POSTGRES_DB', value => 'dbio_test' },
235 ],
236 readiness_cmd => [qw(pg_isready -U postgres)],
237 dsn_template => 'dbi:Pg:database=dbio_test;host=%s;port=%s',
238 user => 'postgres',
239 pass => 'dbiotest',
240 env_prefix => 'DBIO_TEST_PG',
241 },
242 'pg-ext' => {
243 name => 'pg-ext',
244 image => 'src.ci/srv/postgres:18',
245 port => 5432,
246 svc_name => 'pg-ext-svc',
247 env => [
248 { name => 'POSTGRES_PASSWORD', value => 'dbiotest' },
249 { name => 'POSTGRES_DB', value => 'postgres' },
250 { name => 'POSTGRES_HOST_AUTH_METHOD', value => 'scram-sha-256' },
251 ],
252 readiness_cmd => [qw(pg_isready -U postgres)],
253 dsn_template => 'dbi:Pg:database=postgres;host=%s;port=%s',
254 user => 'postgres',
255 pass => 'dbiotest',
256 env_prefix => 'DBIO_TEST_PG_EXT',
257 },
258 mysql => {
259 name => 'mysql',
260 # PINNED to 8.0.30, the last 8.0.x that runs on a plain x86-64 (v1)
261 # CPU: mysql:8.0.31+ dies with "Fatal glibc error: CPU does not
262 # support x86-64-v2" on a node without that baseline (k206). It was
263 # first set for the old test cluster; the workers of the HQ cluster
264 # (kaiser) do have v2 -- but not v3/AVX2 -- so the pin is no longer
265 # forced, only still harmless. Not lifted here because
266 # xbin/k8s/mysql-pod.yaml, which dbio-db runs the standing database
267 # from, carries the same image and has to move with it.
268 image => 'mysql:8.0.30',
269 port => 3306,
270 svc_name => 'mysql-svc',
271 env => [
272 { name => 'MYSQL_ROOT_PASSWORD', value => 'dbiotest' },
273 { name => 'MYSQL_DATABASE', value => 'dbio_test' },
274 ],
275 # Ready means "a TCP client can run SQL against dbio_test", not
276 # "mysqld answers on its socket". The image's entrypoint first boots a
277 # TEMPORARY server with --skip-networking (socket only, "port: 0") to
278 # run the init scripts, stops it, then starts the real server on 3306.
279 # `mysqladmin ping -h localhost` goes over the socket, so it passed on
280 # the temporary server and marked the pod Ready ~5s before anything
281 # listened on 3306 -- every client connecting in that window got
282 # "connection refused" (k208, observed live). Forcing TCP to 127.0.0.1
283 # cannot succeed until the real server is up, and running a query
284 # against the DSN's database proves auth + schema, not just a socket.
285 readiness_cmd => [
286 'sh', '-c',
287 'MYSQL_PWD="$MYSQL_ROOT_PASSWORD" mysql --protocol=TCP'
288 . ' -h 127.0.0.1 -P 3306 -u root -e "SELECT 1" dbio_test',
289 ],
290 # Probed, never loaded: this table is built when the module is loaded,
291 # so a require here initialises an XS database driver plus DBI in every
292 # process that merely loads this file -- for a template string (k179,
293 # same coupling as k173).
294 dsn_template => (
295 DBIO::Optional::Dependencies::_module_findable('DBD/MariaDB.pm')
296 ? 'dbi:MariaDB:database=dbio_test;host=%s;port=%s'
297 : 'dbi:mysql:database=dbio_test;host=%s;port=%s'
298 ),
299 user => 'root',
300 pass => 'dbiotest',
301 env_prefix => 'DBIO_TEST_MYSQL',
302 },
303 duckdb => {
304 name => 'duckdb',
305 image => 'duckdb/duckdb:latest',
306 port => 5432,
307 svc_name => 'duckdb-svc',
308 env => [
309 { name => 'DUCKDB_PASSWORD', value => 'dbiotest' },
310 ],
311 readiness_probe => {
312 tcpSocket => { port => 5432 },
313 initialDelaySeconds => 5,
314 periodSeconds => 3,
315 timeoutSeconds => 2,
316 failureThreshold => 10,
317 },
318 dsn_template => 'dbi:DuckDB:database=dbio_test;host=%s;port=%s',
319 user => 'duckdb',
320 pass => 'dbiotest',
321 env_prefix => 'DBIO_TEST_DUCKDB',
322 },
323 );
324  
325 # ============================================================================
326 # DEPLOY
327 # ============================================================================
328  
329  
330 15 ▶sub deploy_databases {
Subroutine deploy_databases: called CC=4 SCAR=13.9
Pod documented
331 15 my ($self) = @_;
332  
333 15 my $ns = $self->namespace;
334  
335 15 ▶ if ($self->{existing_namespace}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'existing_namespace'}) { } 9 6 15
336 9 print "Using existing namespace $ns (run $self->{run_id})...\n";
337 } else {
338 6 print "Creating namespace $ns...\n";
339 6 $self->_create(Namespace =>
340 metadata => { name => $ns },
341 );
342 }
343  
344 # Deploy each database
345 14 for my $db (@{$self->databases}) {
346 24 ▶ my $spec = $DB_SPECS{$db} or croak "No spec for database: $db";
Branch: 1 branch
BranchTrueFalseTotal
my $spec = $DB_SPECS{$db} or croak("No spec for database: $db") 24 0 24
347 24 $self->_deploy_db($spec);
348 }
349  
350 12 $self->{_deployed} = 1;
351 }
352  
353 33 ▶sub _deploy_db {
Subroutine _deploy_db: called CC=2 SCAR=6.9
354 33 my ($self, $spec) = @_;
355  
356 33 my $ns = $self->namespace;
357  
358 33 print "Deploying $spec->{name} pod...\n";
359  
360 # Create Pod
361 $self->_create(Pod =>
362 metadata => {
363 name => $self->_object_name($spec->{name}),
364 namespace => $ns,
365 labels => $self->_pod_labels($spec->{name}),
366 },
367 spec => {
368 containers => [{
369 name => $spec->{name},
370 image => $spec->{image},
371 ports => [{ containerPort => $spec->{port} }],
372 env => $spec->{env},
373 resources => _resources_for($spec, "database pod $spec->{name}"),
374 ( $spec->{readiness_probe}
375 ? ( readinessProbe => $spec->{readiness_probe} )
376 : ( readinessProbe => {
377 exec => { command => $spec->{readiness_cmd} },
378 33 ▶ initialDelaySeconds => 5,
Branch: 1 branch
BranchTrueFalseTotal
$spec->{'readiness_probe'} ? : 1 25 26
379 periodSeconds => 3,
380 timeoutSeconds => 2,
381 failureThreshold => 30,
382 } )
383 ),
384 }],
385 },
386 );
387  
388 # Create Service
389 $self->_create(Service =>
390 metadata => {
391 name => $self->_object_name($spec->{svc_name}),
392 namespace => $ns,
393 $self->_run_labels,
394 },
395 spec => {
396 selector => $self->_pod_selector($spec->{name}),
397 ports => [{
398 port => $spec->{port},
399 targetPort => $spec->{port},
400 24 protocol => 'TCP',
401 }],
402 },
403 );
404 }
405  
406 # ============================================================================
407 # READINESS
408 # ============================================================================
409  
410 # One poll step: the object, or undef when the API says it is not there (yet).
411 # Any other failure -- a 403, an expired token, a refused connection -- is the
412 # caller's problem and propagates; swallowing it made it look like the object
413 # merely was not ready, and the wait ended as a plain timeout.
414 10932 ▶sub _get_or_none {
Subroutine _get_or_none: called CC=3 SCAR=11.0
415 10932 my ($self, $kind, $name, $ns) = @_;
416  
417 10932 my $obj = eval { $self->api->get($kind, $name, namespace => $ns) };
418 10932 ▶ return $obj unless my $err = $@;
Branch: 1 branch
BranchTrueFalseTotal
unless my $err = $@ 2 10930 10932
419  
420 10930 ▶ return undef if "$err" =~ /\b404\b|\bnot\s*found\b/i;
Branch: 1 branch
BranchTrueFalseTotal
if "$err" =~ /\b404\b|\bnot\s*found\b/i 10928 2 10930
421 2 DBIO::Exception->throw("Reading $kind $name in $ns failed: $err");
422 }
423  
424  
425 3 ▶sub wait_for_ready {
Subroutine wait_for_ready: called CC=12 SCAR=25.6
Pod documented
426 3 my ($self, %opts) = @_;
427  
428 3 ▶ my $timeout = $opts{timeout} // 120;
Condition50%1/2
$opts{"timeout"} // 120
l!l
30
MC/DC0%0/1
$opts{"timeout"} // 120
$opts{"timeout"}
Truth table50%1/2
A
A= $opts{"timeout"}
Aresult
11
00
429 3 ▶ my $interval = $opts{interval} // 3;
Condition50%1/2
$opts{"interval"} // 3
l!l
30
MC/DC0%0/1
$opts{"interval"} // 3
$opts{"interval"}
Truth table50%1/2
A
A= $opts{"interval"}
Aresult
11
00
430  
431 3 ▶ croak "Must call deploy_databases first" unless $self->{_deployed};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_deployed"} 0 3 3
432  
433 3 my $api = $self->api;
434 3 my $ns = $self->namespace;
435 3 my $deadline = time + $timeout;
436  
437 3 for my $db (@{$self->databases}) {
438 4 print "Waiting for $db to be ready...\n";
439  
440 4 ▶ while (time < $deadline) {
Branch: 1 branch
BranchTrueFalseTotal
if (time < $deadline) 10931 1 10932
441 my $pod = $self->_get_or_none('Pod',
442 10931 $self->_object_name($DB_SPECS{$db}{name}), $ns);
443 10930 ▶ if ($pod && $pod->status) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod and $pod->status) 2 10928 10930
Condition66%2/3
$pod and $pod->status
!ll&&!rl&&r
1092802
MC/DC50%1/2
$pod and $pod->status
$pod $pod->status
Truth table66%2/3
A and B
A= $podB= $pod->status
ABresult
0X0
100
111
444 2 my $conditions = $pod->status->conditions;
445 2 ▶ if ($conditions) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conditions) 2 0 2
446 2 for my $cond (@$conditions) {
447 2 ▶ if ($cond->type eq 'Ready' && $cond->status eq 'True') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cond->type eq "Ready" and $cond->status eq "True") 2 0 2
Condition33%1/3
$cond->type eq "Ready" and $cond->status eq "True"
!ll&&!rl&&r
002
MC/DC0%0/2
$cond->type eq "Ready" and $cond->status eq "True"
$cond->type eq "Ready" $cond->status eq "True"
Truth table33%1/3
A and B
A= $cond->type eq "Ready"B= $cond->status eq "True"
ABresult
0X0
100
111
448 2 print "$db is ready.\n";
449 2 goto NEXT_DB;
450 }
451 }
452 }
453 }
454 10928 sleep $interval;
455 }
456  
457 1 croak "Timed out waiting for $db to become ready (${timeout}s)";
458 NEXT_DB:
459 2 }
460  
461 1 $self->{_ready} = 1;
462 }
463  
464 # ============================================================================
465 # CONNECTION INFO
466 # ============================================================================
467  
468  
469 8 ▶sub connection_info {
Subroutine connection_info: called CC=5 SCAR=17.2
Pod documented
470 8 my ($self, %opts) = @_;
471  
472 8 ▶ my $mode = $opts{mode} // 'local';
Condition50%1/2
$opts{"mode"} // "local"
l!l
80
MC/DC0%0/1
$opts{"mode"} // "local"
$opts{"mode"}
Truth table50%1/2
A
A= $opts{"mode"}
Aresult
11
00
473 8 my $ns = $self->namespace;
474 8 my %info;
475  
476 8 for my $db (@{$self->databases}) {
477 10 my $spec = $DB_SPECS{$db};
478  
479 10 my ($host, $port);
480 10 ▶ if ($mode eq 'cluster') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq 'cluster') { } 10 0 10
481 10 $host = $self->_object_name($spec->{svc_name}) . ".$ns.svc.cluster.local";
482 10 $port = $spec->{port};
483 } else {
484 # local mode -- use port-forward ports
485 0 $host = '127.0.0.1';
486 0 ▶ $port = $self->{local_ports}{$db}
Branch: 1 branch
BranchTrueFalseTotal
$port = $self->{"local_ports"}{$db} or croak("No local port for $db -- call setup_port_forwards first") 0 0 0
487 or croak "No local port for $db -- call setup_port_forwards first";
488 }
489  
490 $info{$db} = {
491 dsn => sprintf($spec->{dsn_template}, $host, $port),
492 user => $spec->{user},
493 pass => $spec->{pass},
494 10 host => $host,
495 port => $port,
496 };
497 }
498  
499 8 return \%info;
500 }
501  
502  
503 7 ▶sub env_vars {
Subroutine env_vars: called CC=2 SCAR=6.9
Pod documented
504 7 my ($self, %opts) = @_;
505  
506 7 my $info = $self->connection_info(%opts);
507 7 my %env;
508  
509 7 for my $db (keys %$info) {
510 8 my $spec = $DB_SPECS{$db};
511 8 my $prefix = $spec->{env_prefix};
512  
513 8 $env{"${prefix}_DSN"} = $info->{$db}{dsn};
514 8 $env{"${prefix}_USER"} = $info->{$db}{user};
515 8 $env{"${prefix}_PASS"} = $info->{$db}{pass};
516  
517 }
518  
519 7 return %env;
520 }
521  
522 # ============================================================================
523 # PORT FORWARDING (local mode)
524 # ============================================================================
525  
526  
527 0 ▶sub setup_port_forwards {
Subroutine setup_port_forwards: not called CC=7 SCAR=40.3
Pod documented
528 0 my ($self) = @_;
529  
530 croak "Must call deploy_databases and wait_for_ready first"
531 0 ▶ unless $self->{_ready};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_ready"} 0 0 0
532  
533 0 my $ns = $self->namespace;
534  
535 0 for my $db (@{$self->databases}) {
536 0 my $spec = $DB_SPECS{$db};
537 0 my $svc_name = $self->_object_name($spec->{svc_name});
538 0 my $local_port = _find_free_port();
539  
540 0 $self->{local_ports}{$db} = $local_port;
541  
542 0 print "Port-forwarding $svc_name to 127.0.0.1:$local_port...\n";
543  
544 0 my $pid = fork();
545 0 ▶ if (!defined $pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pid) 0 0 0
546 0 croak "fork failed: $!";
547 }
548 0 ▶ if ($pid == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) 0 0 0
549 # child
550 0 exec('kubectl',
551 '--kubeconfig=' . $self->kubeconfig,
552 'port-forward',
553 '-n', $ns,
554 "svc/$svc_name",
555 "$local_port:$spec->{port}",
556 );
557 0 die "exec kubectl failed: $!";
558 }
559  
560 0 push @{$self->{port_forward_pids}}, $pid;
561 }
562  
563 # Give port-forwards a moment to establish
564 0 sleep 2;
565  
566 # Verify they're still running
567 0 for my $pid (@{$self->{port_forward_pids}}) {
568 0 my $res = waitpid($pid, WNOHANG);
569 0 ▶ if ($res != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res != 0) 0 0 0
570 0 croak "Port-forward process $pid died unexpectedly";
571 }
572 }
573 }
574  
575 0 ▶sub _find_free_port {
Subroutine _find_free_port: not called CC=2 SCAR=17.9
576 0 require IO::Socket::INET;
577 0 ▶ my $sock = IO::Socket::INET->new(
Branch: 1 branch
BranchTrueFalseTotal
my $sock = "IO::Socket::INET"->new("LocalAddr", "127.0.0.1", "LocalPort", 0, "Proto", "tcp", "Listen", 1, "ReuseAddr", 1) or croak("Cannot find free port: $!") 0 0 0
578 LocalAddr => '127.0.0.1',
579 LocalPort => 0,
580 Proto => 'tcp',
581 Listen => 1,
582 ReuseAddr => 1,
583 ) or croak "Cannot find free port: $!";
584 0 my $port = $sock->sockport;
585 0 close $sock;
586 0 return $port;
587 }
588  
589 # ============================================================================
590 # IN-CLUSTER JOB MODE
591 # ============================================================================
592  
593  
594 7 ▶sub deploy_test_job {
Subroutine deploy_test_job: called CC=3 SCAR=11.0
Pod documented
595 7 my ($self, %opts) = @_;
596  
597 7 my $ns = $self->namespace;
598 7 my $name = $self->_job_name;
599 7 ▶ my $image = $opts{image} // 'dbio-test:latest';
Condition50%1/2
$opts{"image"} // "dbio-test:latest"
l!l
07
MC/DC0%0/1
$opts{"image"} // "dbio-test:latest"
$opts{"image"}
Truth table50%1/2
A
A= $opts{"image"}
Aresult
11
00
600 7 ▶ my @args = @{$opts{prove_args} // ['-l', 't/']};
Condition50%1/2
$opts{"prove_args"} // ["-l", "t/"]
l!l
07
MC/DC0%0/1
$opts{"prove_args"} // ["-l", "t/"]
$opts{"prove_args"}
Truth table50%1/2
A
A= $opts{"prove_args"}
Aresult
11
00
601  
602 # Build env vars for in-cluster mode
603 7 my %env = $self->env_vars(mode => 'cluster');
604 my @env_list = map {
605 7 { name => $_, value => $env{$_} }
606 } sort keys %env;
607  
608 7 print "Deploying test Job in namespace $ns...\n";
609  
610 7 $self->_create(Job =>
611 metadata => {
612 name => $name,
613 namespace => $ns,
614 $self->_run_labels,
615 },
616 spec => {
617 backoffLimit => 0,
618 template => {
619 spec => {
620 restartPolicy => 'Never',
621 containers => [{
622 name => 'test-runner',
623 image => $image,
624 command => ['prove'],
625 args => \@args,
626 env => \@env_list,
627 # The runner is not a database, so the envelope was
628 # reconsidered rather than assumed -- but it lands on
629 # the same numbers. CPU: the one-core cap is not about
630 # this workload's appetite (prove would happily eat
631 # every core), it is the shared-box ceiling, and it
632 # applies to the greediest tenant first. Memory: steady
633 # state is a chain of small short-lived perl processes,
634 # but a --cover run keeps Devel::Cover's coverage db in
635 # memory for the whole suite, and that is the real
636 # high-water mark; 1Gi covers it. Keeping the runner on
637 # the same ceiling as the DB pods also makes the
638 # namespace's worst case easy to reason about.
639 resources => _resources_for(\%opts, 'test Job container'),
640 }],
641 },
642 },
643 },
644 );
645  
646 6 return $name;
647 }
648  
649  
650 1 ▶sub wait_for_job {
Subroutine wait_for_job: called CC=11 SCAR=39.2
Pod documented
651 1 my ($self, %opts) = @_;
652  
653 1 ▶ my $job_name = $opts{name} // $self->_job_name;
Condition33%1/3
$opts{"name"} // $self->_job_name
l!l&&r!l&&!r
010
MC/DC0%0/2
$opts{"name"} // $self->_job_name
$opts{"name"} $self->_job_name
Truth table33%1/3
A // B
A= $opts{"name"}B= $self->_job_name
ABresult
1X1
011
000
654 1 ▶ my $timeout = $opts{timeout} // 3600;
Condition50%1/2
$opts{"timeout"} // 3600
l!l
10
MC/DC0%0/1
$opts{"timeout"} // 3600
$opts{"timeout"}
Truth table50%1/2
A
A= $opts{"timeout"}
Aresult
11
00
655 1 ▶ my $interval = $opts{interval} // 5;
Condition50%1/2
$opts{"interval"} // 5
l!l
10
MC/DC0%0/1
$opts{"interval"} // 5
$opts{"interval"}
Truth table50%1/2
A
A= $opts{"interval"}
Aresult
11
00
656  
657 1 my $api = $self->api;
658 1 my $ns = $self->namespace;
659 1 my $deadline = time + $timeout;
660  
661 1 print "Waiting for Job $job_name to complete...\n";
662  
663 1 ▶ while (time < $deadline) {
Branch: 1 branch
BranchTrueFalseTotal
if (time < $deadline) 1 0 1
664 1 my $job = $self->_get_or_none('Job', $job_name, $ns);
665  
666 0 ▶ if ($job && $job->status) {
Branch: 1 branch
BranchTrueFalseTotal
if ($job and $job->status) 0 0 0
Condition0%0/3
$job and $job->status
!ll&&!rl&&r
000
MC/DC0%0/2
$job and $job->status
$job $job->status
Truth table0%0/3
A and B
A= $jobB= $job->status
ABresult
0X0
100
111
667 0 my $succeeded = $job->status->succeeded;
668 0 my $failed = $job->status->failed;
669  
670 0 ▶ if ($succeeded && $succeeded > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($succeeded and $succeeded > 0) 0 0 0
Condition0%0/3
$succeeded and $succeeded > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$succeeded and $succeeded > 0
$succeeded $succeeded > 0
Truth table0%0/3
A and B
A= $succeededB= $succeeded > 0
ABresult
0X0
100
111
671 0 print "Job succeeded.\n";
672 0 return 0; # exit code 0
673 }
674 0 ▶ if ($failed && $failed > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($failed and $failed > 0) 0 0 0
Condition0%0/3
$failed and $failed > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$failed and $failed > 0
$failed $failed > 0
Truth table0%0/3
A and B
A= $failedB= $failed > 0
ABresult
0X0
100
111
675 0 print "Job failed.\n";
676 0 return 1; # exit code 1
677 }
678 }
679 0 sleep $interval;
680 }
681  
682 0 croak "Timed out waiting for Job $job_name (${timeout}s)";
683 }
684  
685  
686 0 ▶sub fetch_job_logs {
Subroutine fetch_job_logs: not called CC=3 SCAR=24.8
Pod documented
687 0 my ($self, %opts) = @_;
688  
689 0 ▶ my $job_name = $opts{name} // $self->_job_name;
Condition0%0/3
$opts{"name"} // $self->_job_name
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"name"} // $self->_job_name
$opts{"name"} $self->_job_name
Truth table0%0/3
A // B
A= $opts{"name"}B= $self->_job_name
ABresult
1X1
011
000
690 0 my $api = $self->api;
691 0 my $ns = $self->namespace;
692  
693 # Find pods belonging to the job
694 0 my $pods = $api->list('Pod',
695 namespace => $ns,
696 labelSelector => "job-name=$job_name",
697 );
698  
699 0 my @logs;
700 0 for my $pod (@{ $pods->items }) {
701 0 my $pod_name = $pod->metadata->name;
702  
703 # Use kubectl for logs since Kubernetes::REST doesn't expose pod logs directly
704 0 my $log = `kubectl --kubeconfig=${\$self->kubeconfig} logs -n $ns $pod_name 2>&1`;
705 0 push @logs, { pod => $pod_name, log => $log };
706 }
707  
708 0 return \@logs;
709 }
710  
711 # ============================================================================
712 # CLEANUP
713 # ============================================================================
714  
715  
716 11 ▶sub cleanup {
Subroutine cleanup: called CC=11 SCAR=24.1
Pod documented
717 11 my ($self) = @_;
718  
719 # Kill port-forward processes
720 11 for my $pid (@{$self->{port_forward_pids}}) {
721 0 kill 'TERM', $pid;
722 0 waitpid($pid, 0);
723 }
724 11 $self->{port_forward_pids} = [];
725  
726 # Delete what this run created -- by kind and exact name, from the list
727 # _create keeps, never by a label selector: a selector deletes whatever
728 # matches it, including what matched before this run began.
729 11 my $ns = $self->namespace;
730 11 ▶ my @created = @{ $self->{_created} // [] };
Condition50%1/2
$self->{"_created"} // []
l!l
110
MC/DC0%0/1
$self->{"_created"} // []
$self->{"_created"}
Truth table50%1/2
A
A= $self->{"_created"}
Aresult
11
00
731  
732 # A namespace this run created takes everything in it along (cascading
733 # delete), so that one delete is the whole job. A namespace that was
734 # already there is not in the list, and is never deleted.
735 11 my ($own_ns) = grep { $_->{kind} eq 'Namespace' } @created;
736 11 ▶ my @targets = $own_ns ? ($own_ns) : reverse @created;
Branch: 1 branch
BranchTrueFalseTotal
$own_ns ? : 3 8 11
737  
738 11 my %gone;
739 11 for my $obj (@targets) {
740 19 my ($kind, $name) = @{$obj}{qw(kind name)};
741 19 ▶ my $what = $kind eq 'Namespace'
Branch: 1 branch
BranchTrueFalseTotal
$kind eq 'Namespace' ? : 3 16 19
742 ? "namespace $name"
743 : "$kind $name in namespace $ns";
744 19 print "Deleting $what...\n";
745 19 eval {
746 19 ▶ $self->api->delete($kind, $name,
Branch: 2 branches
BranchTrueFalseTotal
$kind eq 'Namespace' ? : 3 16 19
$kind eq 'Job' ? : 3 16 19
747 $kind eq 'Namespace' ? () : (namespace => $ns),
748 # Asked through the REST API, a Job is deleted without its
749 # pods unless told otherwise. In a namespace of our own the
750 # namespace delete collects them; here nothing would.
751 $kind eq 'Job' ? (propagationPolicy => 'Background') : (),
752 );
753 18 $gone{$obj} = 1;
754 };
755 19 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 18 19
756 1 warn "Warning: failed to delete $what: $@\n";
757 }
758 }
759  
760 # Keep what could not be deleted, so created_objects still names it and a
761 # second cleanup tries again; forget the rest, so it is deleted only once.
762 $self->{_created} = $own_ns
763 ? ( $gone{$own_ns} ? [] : \@created )
764 11 ▶ : [ grep { !$gone{$_} } @created ];
Branch: 2 branches
BranchTrueFalseTotal
$own_ns ? : 3 8 11
$gone{$own_ns} ? : 3 0 3
765 }
766  
767 30 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=12.0
768 30 my ($self) = @_;
769  
770 # Kill any leftover port-forward processes
771 30 ▶ for my $pid (@{$self->{port_forward_pids} // []}) {
Condition50%1/2
$self->{"port_forward_pids"} // []
l!l
300
MC/DC0%0/1
$self->{"port_forward_pids"} // []
$self->{"port_forward_pids"}
Truth table50%1/2
A
A= $self->{"port_forward_pids"}
Aresult
11
00
772 0 kill 'TERM', $pid;
773 }
774 }
775  
776 6 1;
777  
778 __END__
« blib/lib/DBIO/ResultSource/RowParser/Util.pm ↑ Summary blib/lib/DBIO/Storage/PoolBase.pm »