xbin/dbio-mysql-k8s

filter: Statementstmt 18.9% 24 / 127 Branchbran 2.6% 1 / 38 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 44.4% 8 / 18 Podpod n/a totaltotal 15.1% 33 / 218 CC 29 SCAR 40.6
CC
29
Cov
13%
_wait_for_ready
156.0
main
72.0
_create_pod
20.0
CRAP 57.9
« blib/lib/DBIO/MySQL/Introspect/Tables.pm ↑ Summary blib/lib/DBIO/MySQL/DDL.pm »
1 #!/usr/bin/env perl
2 # xbin/dbio-mysql-k8s — run a command with a live MySQL or MariaDB pod on Kubernetes
3 #
4 # Reads DBIO_TEST_KUBECONFIG (path to a kubeconfig file).
5 # Starts a MySQL or MariaDB pod, sets DBIO_TEST_MYSQL_DSN/_USER/_PASS internally,
6 # runs the given command, then stops the pod on exit.
7 #
8 # Usage:
9 # DBIO_TEST_KUBECONFIG=~/.kube/myconfig xbin/dbio-mysql-k8s prove -l t/
10 # DBIO_TEST_KUBECONFIG=~/.kube/myconfig xbin/dbio-mysql-k8s --mariadb prove -l t/
11 # DBIO_TEST_KUBECONFIG=~/.kube/myconfig xbin/dbio-mysql-k8s dzil test
12 #
13 # Without a command: starts the pod and blocks until Ctrl-C, then stops.
14 #
15 # Options:
16 # --mariadb Use MariaDB instead of MySQL (image: mariadb:11)
17 # --mysql Use MySQL (image: mysql:8); default is MariaDB
18 #
19 # DBD::MariaDB is used for both flavors — it bundles the MariaDB Connector/C
20 # and works with MySQL 5.7+ and MariaDB 10+. DBD::mysql requires system
21 # MySQL/MariaDB client libraries and is not used by default.
22  
23 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use Kubernetes::REST::Kubeconfig;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use IO::K8s;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 # Credentials and resources follow maint/k8s/mariadb.yaml and
31 # maint/docker/docker-compose.yml: database dbio_test, user dbio/dbio, root
32 # password dbioroot; the pod resources likewise. t/89-k8s-pod-spec.t compares
33 # _pod_spec against the manifest.
34 1 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 DB_USER => 'dbio',
36 DB_PASS => 'dbio',
37 DB_ROOT => 'dbioroot',
38 DB_NAME => 'dbio_test',
39 DB_PORT => 3306,
40 1 };
41  
42 # Loaded by a test (do FILE) only the subs are wanted; run main otherwise.
43 1 ▶main() unless caller;
Branch: 1 branch
BranchTrueFalseTotal
unless caller 0 1 1
44  
45 0 ▶sub main {
Subroutine main: not called CC=8 SCAR=42.8
46 my $kubeconfig_path = $ENV{DBIO_TEST_KUBECONFIG}
47 0 ▶ or die "DBIO_TEST_KUBECONFIG is not set\n";
Branch: 1 branch
BranchTrueFalseTotal
my $kubeconfig_path = $ENV{"DBIO_TEST_KUBECONFIG"} or die "DBIO_TEST_KUBECONFIG is not set\n" 0 0 0
48  
49 # Parse --mariadb / --mysql flag before the command (default: mariadb)
50 0 my $mariadb = 1;
51 0 ▶ while (@ARGV && $ARGV[0] =~ /^--(mariadb|mysql)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV and $ARGV[0] =~ /^--(mariadb|mysql)$/) 0 0 0
Condition0%0/3
@ARGV and $ARGV[0] =~ /^--(mariadb|mysql)$/
!ll&&!rl&&r
000
MC/DC0%0/2
@ARGV and $ARGV[0] =~ /^--(mariadb|mysql)$/
@ARGV $ARGV[0] =~ /^--(mariadb|mysql)$/
Truth table0%0/3
A and B
A= @ARGVB= $ARGV[0] =~ /^--(mariadb|mysql)$/
ABresult
0X0
100
111
52 0 ▶ $mariadb = ($1 eq 'mariadb') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq 'mariadb' ? : 0 0 0
53 0 shift @ARGV;
54 }
55  
56 0 my $k8s = Kubernetes::REST::Kubeconfig->new(kubeconfig_path => $kubeconfig_path)->api;
57 0 my $io = IO::K8s->new;
58  
59 0 my $ns = 'dbio-test';
60 0 ▶ my $flavor = $mariadb ? 'mariadb' : 'mysql';
Branch: 1 branch
BranchTrueFalseTotal
$mariadb ? : 0 0 0
61 0 my $name = "dbio-$flavor";
62 # PINNED to 8.0.30 — mysql:8.0.31+ is built on Oracle Linux 9 / newer glibc and
63 # requires the x86-64-v2 CPU baseline, which the shared test-cluster node lacks:
64 # floating mysql:8.0 CrashLoops with "Fatal glibc error: CPU does not support
65 # x86-64-v2". 8.0.30 is the last v1-compatible 8.0.x. See core k206 (commit
66 # 69fcbf48). Do NOT float back to mysql:8.
67 0 ▶ my $image = $mariadb ? 'mariadb:11' : 'mysql:8.0.30';
Branch: 1 branch
BranchTrueFalseTotal
$mariadb ? : 0 0 0
68  
69 0 _ensure_namespace($k8s, $io, $ns);
70 0 _create_pod($k8s, $io, $ns, $name, $image);
71 0 _create_service($k8s, $io, $ns, $name, DB_PORT);
72 0 my ($host, $port) = _wait_for_ready($k8s, $ns, $name, DB_USER);
73  
74 # Always use DBD::MariaDB — it bundles its own connector and works with both
75 0 $ENV{DBIO_TEST_MYSQL_DSN} = "dbi:MariaDB:database=@{[ DB_NAME ]};host=$host;port=$port";
76 # The suite runs as root ('root'@'%' in both images): dbio, granted on
77 # dbio_test only, may not create the scratch/temp databases nor, on MySQL's
78 # binlog, triggers (karr k104; cf. maint/k8s/mariadb.yaml).
79 0 $ENV{DBIO_TEST_MYSQL_USER} = 'root';
80 0 $ENV{DBIO_TEST_MYSQL_PASS} = DB_ROOT;
81  
82 0 my $exit = 0;
83  
84 0 ▶ if (@ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV) { } 0 0 0
85 0 $exit = system(@ARGV);
86 0 $exit = $exit >> 8;
87 }
88 else {
89 0 print STDERR "\u${flavor} ready at $host:$port (namespace $ns)\n";
90 0 print STDERR "DSN: $ENV{DBIO_TEST_MYSQL_DSN}\n";
91 0 print STDERR "Press Ctrl-C to stop.\n";
92 0 ▶ local $SIG{INT} = sub { };
Subroutine __ANON__: not called CC=1 SCAR=6.9
93 0 POSIX::pause();
94 }
95  
96 0 _stop($k8s, $ns, $name);
97 0 exit $exit;
98 }
99  
100 0 ▶sub _stop {
Subroutine _stop: not called CC=1 SCAR=6.9
101 0 my ($k8s, $ns, $name) = @_;
102 0 print STDERR "Stopping $name...\n";
103 0 eval { $k8s->delete($k8s->get('Pod', name => $name, namespace => $ns)) };
104 0 eval { $k8s->delete($k8s->get('Service', name => $name, namespace => $ns)) };
105 }
106  
107 0 ▶sub _ensure_namespace {
Subroutine _ensure_namespace: not called CC=2 SCAR=17.9
108 0 my ($k8s, $io, $ns) = @_;
109 0 ▶ return if eval { $k8s->get('Namespace', name => $ns); 1 };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $k8s->get("Namespace", "name", $ns); 1 } } 0 0 0
110 0 $k8s->create($io->new_object('Namespace',
111 metadata => { name => $ns },
112 ));
113 }
114  
115 0 ▶sub _create_pod {
Subroutine _create_pod: not called CC=4 SCAR=30.0
116 0 my ($k8s, $io, $ns, $name, $image) = @_;
117 0 ▶ if (eval { $k8s->get('Pod', name => $name, namespace => $ns); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $k8s->get("Pod", "name", $name, "namespace", $ns); 1 } }) 0 0 0
118 0 $k8s->delete($k8s->get('Pod', name => $name, namespace => $ns));
119 0 print STDERR "Waiting for old pod to terminate";
120 0 my $deadline = time + 60;
121 0 ▶ while (time < $deadline) {
Branch: 1 branch
BranchTrueFalseTotal
if (time < $deadline) 0 0 0
122 0 ▶ last unless eval { $k8s->get('Pod', name => $name, namespace => $ns); 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $k8s->get("Pod", "name", $name, "namespace", $ns); 1 } } 0 0 0
123 0 print STDERR '.';
124 0 Time::HiRes::sleep(2);
125 }
126 0 print STDERR "\n";
127 }
128 0 $k8s->create($io->new_object('Pod', %{ _pod_spec($ns, $name, $image) }));
129 }
130  
131 # The Pod as IO::K8s->new_object takes it. Every DB container carries cpu AND
132 # memory requests and limits (house rule "Live testing", point 4).
133 1 ▶sub _pod_spec {
Subroutine _pod_spec: called CC=1 SCAR=0.0
134 1 my ($ns, $name, $image) = @_;
135 return {
136 1 metadata => { name => $name, namespace => $ns, labels => { app => $name } },
137 spec => {
138 containers => [{
139 name => $name,
140 image => $image,
141 resources => {
142 requests => { cpu => '250m', memory => '384Mi' },
143 limits => { cpu => '1', memory => '1Gi' },
144 },
145 env => [
146 { name => 'MYSQL_USER', value => DB_USER },
147 { name => 'MYSQL_PASSWORD', value => DB_PASS },
148 { name => 'MYSQL_ROOT_PASSWORD', value => DB_ROOT },
149 { name => 'MYSQL_DATABASE', value => DB_NAME },
150 ],
151 ports => [{ containerPort => 3306 }],
152 readinessProbe => {
153 tcpSocket => { port => 3306 },
154 initialDelaySeconds => 15,
155 periodSeconds => 3,
156 },
157 }],
158 },
159 };
160 }
161  
162 0 ▶sub _create_service {
Subroutine _create_service: not called CC=1 SCAR=6.9
163 0 my ($k8s, $io, $ns, $name, $port) = @_;
164 0 eval { $k8s->delete($k8s->get('Service', name => $name, namespace => $ns)) };
165 0 $k8s->create($io->new_object('Service',
166 metadata => { name => $name, namespace => $ns },
167 spec => {
168 type => 'NodePort',
169 selector => { app => $name },
170 ports => [{ port => $port, targetPort => $port }],
171 },
172 ));
173 }
174  
175 0 ▶sub _wait_for_ready {
Subroutine _wait_for_ready: not called CC=12 SCAR=50.5
176 0 my ($k8s, $ns, $name, $user) = @_;
177 0 print STDERR "Waiting for pod $name to be ready";
178 0 my $deadline = time + 300;
179 0 my $last_phase = '';
180 0 ▶ while (time < $deadline) {
Branch: 1 branch
BranchTrueFalseTotal
if (time < $deadline) 0 0 0
181 0 my $pod = eval { $k8s->get('Pod', name => $name, namespace => $ns) };
182 0 ▶ if ($pod) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod) 0 0 0
183 0 ▶ return _get_service_endpoint($k8s, $ns, $name) if _pod_ready($pod);
Branch: 1 branch
BranchTrueFalseTotal
if _pod_ready($pod) 0 0 0
184 0 ▶ my $phase = eval { $pod->status->phase } // '?';
Condition0%0/2
eval { do { $pod->status->phase } } // "?"
l!l
00
MC/DC0%0/1
eval { do { $pod->status->phase } } // "?"
eval { do { $pod->status->phase } }
Truth table0%0/2
A
A= eval { do { $pod->status->phase } }
Aresult
11
00
185 # Show phase changes and any waiting reason (e.g. ErrImagePull)
186 0 ▶ my $reason = eval {
Condition0%0/2
eval { do { my $cs = $pod->status->containerStatuses // []; @$cs ? +($cs->[0]->state->waiting // {})->{"reason"} // "" : "" } } // ""
l!l
00
MC/DC0%0/1
eval { do { my $cs = $pod->status->containerStatuses // []; @$cs ? +($cs->[0]->state->waiting // {})->{"reason"} // "" : "" } } // ""
eval { do { my $cs = $pod->status->containerStatuses // []; @$cs ? +($cs->[0]->state->waiting // {})->{"reason"} // "" : "" } }
Truth table0%0/2
A
A= eval { do { my $cs = $pod->status->containerStatuses // []; @$cs ? +($cs->[0]->state->waiting // {})->{"reason"} // "" : "" } }
Aresult
11
00
187 0 ▶ my $cs = $pod->status->containerStatuses // [];
Condition0%0/2
$pod->status->containerStatuses // []
l!l
00
MC/DC0%0/1
$pod->status->containerStatuses // []
$pod->status->containerStatuses
Truth table0%0/2
A
A= $pod->status->containerStatuses
Aresult
11
00
188 0 ▶ @$cs ? ($cs->[0]->state->waiting // {})->{reason} // '' : ''
Branch: 1 branch
BranchTrueFalseTotal
@$cs ? : 0 0 0
Condition0%0/4
+($cs->[0]->state->waiting // {})->{"reason"} // ""
l!l
00
$cs->[0]->state->waiting // {}
l!l
00
MC/DC0%0/1
+($cs->[0]->state->waiting // {})->{"reason"} // ""
$cs->[0]->state->waiting
Truth table0%0/2
A
A= $cs->[0]->state->waiting
Aresult
11
00
189 } // '';
190 0 ▶ my $status = $reason ? "$phase/$reason" : $phase;
Branch: 1 branch
BranchTrueFalseTotal
$reason ? : 0 0 0
191 0 ▶ if ($status ne $last_phase) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status ne $last_phase) 0 0 0
192 0 print STDERR " [$status]";
193 0 $last_phase = $status;
194 }
195 }
196 0 print STDERR '.';
197 0 Time::HiRes::sleep(3);
198 }
199 0 print STDERR "\n";
200 0 die "Timed out waiting for pod $name\n";
201 }
202  
203 0 ▶sub _pod_ready {
Subroutine _pod_ready: not called CC=3 SCAR=24.8
204 0 my ($pod) = @_;
205 0 ▶ my $conditions = $pod->status->conditions // [];
Condition0%0/2
$pod->status->conditions // []
l!l
00
MC/DC0%0/1
$pod->status->conditions // []
$pod->status->conditions
Truth table0%0/2
A
A= $pod->status->conditions
Aresult
11
00
206 0 ▶ return grep { $_->type eq 'Ready' && $_->status eq 'True' } @$conditions;
Condition0%0/3
$_->type eq 'Ready' && $_->status eq 'True'
!ll&&!rl&&r
000
MC/DC0%0/2
$_->type eq 'Ready' && $_->status eq 'True'
$_->type eq 'Ready' $_->status eq 'True'
Truth table0%0/3
A && B
A= $_->type eq 'Ready'B= $_->status eq 'True'
ABresult
0X0
100
111
207 }
208  
209 0 ▶sub _get_service_endpoint {
Subroutine _get_service_endpoint: not called CC=3 SCAR=24.8
210 0 my ($k8s, $ns, $name) = @_;
211 0 my $svc = $k8s->get('Service', name => $name, namespace => $ns);
212 0 ▶ my $node_port = $svc->spec->ports->[0]->nodePort
Branch: 1 branch
BranchTrueFalseTotal
my $node_port = $svc->spec->ports->[0]->nodePort or die "No nodePort assigned to service $name\n" 0 0 0
213 or die "No nodePort assigned to service $name\n";
214  
215 0 my $nodes = $k8s->list('Node');
216 0 my ($node) = grep { _node_ready($_) } @{ $nodes->items };
217 0 ▶ die "No ready nodes found\n" unless $node;
Branch: 1 branch
BranchTrueFalseTotal
unless $node 0 0 0
218  
219 0 my ($addr) = grep { $_->type eq 'InternalIP' } @{ $node->status->addresses };
220 0 return ($addr->address, $node_port);
221 }
222  
223 0 ▶sub _node_ready {
Subroutine _node_ready: not called CC=3 SCAR=24.8
224 0 my ($node) = @_;
225 0 ▶ my $conditions = $node->status->conditions // [];
Condition0%0/2
$node->status->conditions // []
l!l
00
MC/DC0%0/1
$node->status->conditions // []
$node->status->conditions
Truth table0%0/2
A
A= $node->status->conditions
Aresult
11
00
226 0 ▶ return grep { $_->type eq 'Ready' && $_->status eq 'True' } @$conditions;
Condition0%0/3
$_->type eq 'Ready' && $_->status eq 'True'
!ll&&!rl&&r
000
MC/DC0%0/2
$_->type eq 'Ready' && $_->status eq 'True'
$_->type eq 'Ready' $_->status eq 'True'
Truth table0%0/3
A && B
A= $_->type eq 'Ready'B= $_->status eq 'True'
ABresult
0X0
100
111
227 }
« blib/lib/DBIO/MySQL/Introspect/Tables.pm ↑ Summary blib/lib/DBIO/MySQL/DDL.pm »