lib/Kubernetes/REST/CLI/Minikube.pm

filter: Statementstmt 76.0% 197 / 259 Branchbran 64.4% 67 / 104 Conditioncond 46.4% 26 / 56 MC/DCmcdc 23.3% 7 / 30 Subroutinesub 82.2% 37 / 45 Podpod 100% 2 / 2 totaltotal 67.7% 336 / 496 CC 75 SCAR 22.9
CC
75
Cov
69%
_build_minikube_bin
42.0
_find_in_path
42.0
_running
19.4
CRAP 9.9
« lib/Kubernetes/REST.pm ↑ Summary blib/lib/Kubernetes/REST/CLI/Cmd/Get.pm »
1 package Kubernetes::REST::CLI::Minikube;
2 1 our $VERSION = '1.110';
3 # ABSTRACT: Bring up a minikube test cluster and run a command against it
4 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use MooX::Options;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Path::Tiny qw( path );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 option profile => (
15 is => 'ro',
16 format => 's',
17 short => 'p',
18 22 ▶ default => sub { 'kube-rest-test' },
Subroutine __ANON__: called CC=1 SCAR=0.0
19 1 doc => 'minikube profile name',
20 );
21  
22  
23 option driver => (
24 is => 'ro',
25 format => 's',
26 short => 'd',
27 22 ▶ default => sub { 'docker' },
Subroutine __ANON__: called CC=1 SCAR=0.0
28 1 doc => 'minikube driver (passed as --driver)',
29 );
30  
31  
32 1 option kubernetes_version => (
33 is => 'ro',
34 format => 's',
35 short => 'k',
36 doc => 'Kubernetes version to run (passed as --kubernetes-version)',
37 );
38  
39  
40 1 option minikube_version => (
41 is => 'ro',
42 format => 's',
43 doc => 'minikube release to use, e.g. v1.38.1 (default: latest)',
44 );
45  
46  
47 1 option cpus => (
48 is => 'ro',
49 format => 's',
50 short => 'c',
51 doc => 'CPUs for the cluster (passed as --cpus)',
52 );
53  
54  
55 1 option memory => (
56 is => 'ro',
57 format => 's',
58 short => 'm',
59 doc => 'Memory for the cluster, e.g. 4g (passed as --memory)',
60 );
61  
62  
63 1 option container_runtime => (
64 is => 'ro',
65 format => 's',
66 doc => 'Container runtime for the cluster (passed as --container-runtime)',
67 );
68  
69  
70 1 option rootless => (
71 is => 'ro',
72 doc => 'Run the cluster rootless (passed as --rootless)',
73 );
74  
75  
76 1 option install_dir => (
77 is => 'lazy',
78 format => 's',
79 doc => 'Where a downloaded minikube binary and the kubeconfig go',
80 );
81  
82 1 ▶sub _build_install_dir {
Subroutine _build_install_dir: called CC=5 SCAR=18.1
83 1 my ($self) = @_;
84 1 my $cache = $ENV{XDG_CACHE_HOME};
85 1 ▶ unless (defined $cache && length $cache) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cache and length $cache) 1 0 1
Condition33%1/3
defined $cache and length $cache
!ll&&!rl&&r
100
MC/DC0%0/2
defined $cache and length $cache
defined $cache length $cache
Truth table33%1/3
A and B
A= defined $cacheB= length $cache
ABresult
0X0
100
111
86 1 my $home = $ENV{HOME};
87 1 ▶ die "Cannot determine a cache directory: neither XDG_CACHE_HOME nor HOME is set (use --install-dir)\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $home and length $home 0 1 1
Condition33%1/3
defined $home and length $home
!ll&&!rl&&r
001
MC/DC0%0/2
defined $home and length $home
defined $home length $home
Truth table33%1/3
A and B
A= defined $homeB= length $home
ABresult
0X0
100
111
88 unless defined $home && length $home;
89 1 $cache = path($home, '.cache')->stringify;
90 }
91 1 return path($cache, 'kube_test_minikube')->stringify;
92 }
93  
94  
95 1 option kubeconfig => (
96 is => 'lazy',
97 format => 's',
98 doc => 'Isolated kubeconfig file (default: <install_dir>/<profile>.kubeconfig)',
99 );
100  
101 1 ▶sub _build_kubeconfig {
Subroutine _build_kubeconfig: called CC=1 SCAR=0.0
102 1 my ($self) = @_;
103 1 return $self->_install_dir_path->child($self->profile . '.kubeconfig')->stringify;
104 }
105  
106  
107 option env => (
108 is => 'ro',
109 format => 's@',
110 short => 'e',
111 24 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
112 1 doc => 'Extra environment variable that also receives the kubeconfig path (repeatable)',
113 );
114  
115  
116 1 option context => (
117 is => 'ro',
118 format => 's',
119 doc => 'Also export TEST_KUBERNETES_REST_CONTEXT with this value',
120 );
121  
122  
123 1 option stop => (
124 is => 'ro',
125 short => 's',
126 doc => 'Stop the profile afterwards (or only stop it, without a command)',
127 );
128  
129  
130 1 option delete => (
131 is => 'ro',
132 short => 'D',
133 doc => 'Delete the profile and its kubeconfig afterwards (or only delete, without a command)',
134 );
135  
136  
137 1 option restart => (
138 is => 'ro',
139 short => 'r',
140 doc => 'Run minikube start even when the profile is already running',
141 );
142  
143  
144 1 option verbose => (
145 is => 'ro',
146 short => 'v',
147 doc => 'Print every minikube command before running it',
148 );
149  
150  
151 1 has minikube_bin => (
152 is => 'lazy',
153 );
154  
155 0 ▶sub _build_minikube_bin {
Subroutine _build_minikube_bin: not called CC=6 SCAR=37.4
156 0 my ($self) = @_;
157 0 my $installed = $self->_install_dir_path->child('minikube');
158 0 ▶ if (my $pinned = $self->minikube_version) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pinned = $self->minikube_version) 0 0 0
159 0 ▶ if (-x $installed && $self->_minikube_version_of($installed->stringify) eq $pinned) {
Branch: 1 branch
BranchTrueFalseTotal
if (-x $installed and $self->_minikube_version_of($installed->stringify) eq $pinned) 0 0 0
Condition0%0/3
-x $installed and $self->_minikube_version_of($installed->stringify) eq $pinned
!ll&&!rl&&r
000
MC/DC0%0/2
-x $installed and $self->_minikube_version_of($installed->stringify) eq $pinned
-x $installed $self->_minikube_version_of($installed->stringify) eq $pinned
Truth table0%0/3
A and B
A= -x $installedB= $self->_minikube_version_of($installed->stringify) eq $pinned
ABresult
0X0
100
111
160 0 return $installed->stringify;
161 }
162 0 return $self->_install_minikube;
163 }
164 0 my $on_path = $self->_find_in_path('minikube');
165 0 ▶ return $on_path if defined $on_path;
Branch: 1 branch
BranchTrueFalseTotal
if defined $on_path 0 0 0
166 0 ▶ return $installed->stringify if -x $installed;
Branch: 1 branch
BranchTrueFalseTotal
if -x $installed 0 0 0
167 0 return $self->_install_minikube;
168 }
169  
170  
171 1 has _install_dir_path => (
172 is => 'lazy',
173 );
174  
175 8 ▶sub _build__install_dir_path { path($_[0]->install_dir)->absolute }
Subroutine _build__install_dir_path: called CC=1 SCAR=0.0
176  
177 1 has _kubeconfig_file => (
178 is => 'lazy',
179 );
180  
181 12 ▶sub _build__kubeconfig_file { path($_[0]->kubeconfig)->absolute }
Subroutine _build__kubeconfig_file: called CC=1 SCAR=0.0
182  
183 1 has _ua => (
184 is => 'lazy',
185 );
186  
187 0 ▶sub _build__ua {
Subroutine _build__ua: not called CC=1 SCAR=6.9
188 0 return LWP::UserAgent->new(agent => 'kube_test_minikube/' . $VERSION);
189 }
190  
191 1 my $LOG_PREFIX = '[kube_test_minikube] ';
192 1 my $RELEASES = 'https://storage.googleapis.com/minikube/releases/';
193  
194 8 ▶sub split_argv {
Subroutine split_argv: called CC=11 SCAR=24.0
Pod documented
195 8 my ($class, @argv) = @_;
196 8 my %data = $class->_options_data;
197 8 my %by_short = map { $data{$_}{short} => $_ } grep { defined $data{$_}{short} } keys %data;
198 8 my @options;
199 8 ▶ while (@argv) {
Branch: 1 branch
BranchTrueFalseTotal
if (@argv) 14 1 15
200 14 my $arg = shift @argv;
201 14 ▶ last if $arg eq '--';
Branch: 1 branch
BranchTrueFalseTotal
if $arg eq "--" 1 13 14
202 13 ▶ unless ($arg =~ /^-./) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg =~ /^-./) 6 7 13
203 6 unshift @argv, $arg;
204 6 last;
205 }
206 7 push @options, $arg;
207 7 ▶ next if $arg =~ /=/;
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ /=/ 1 6 7
208 6 my $name;
209 6 ▶ if ($arg =~ /^--(?:no-)?(.+)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($arg =~ /^--(?:no-)?(.+)$/) { } 2 4 6
elsif ($arg =~ /^-(.)(.*)$/) { } 4 0 4
210 2 (my $long = $1) =~ s/-/_/g;
211 2 $name = $class->_expand_option_name($long, \%data);
212 } elsif ($arg =~ /^-(.)(.*)$/) {
213 4 ▶ next if length $2;
Branch: 1 branch
BranchTrueFalseTotal
if length $2 1 3 4
214 3 $name = $by_short{$1};
215 }
216 push @options, shift @argv
217 5 ▶ if defined $name && $data{$name}{format} && @argv;
Branch: 1 branch
BranchTrueFalseTotal
if defined $name and $data{$name}{"format"} and @argv 3 2 5
Condition83%5/6
defined $name and $data{$name}{"format"} and @argv
!ll&&!rl&&r
203
defined $name and $data{$name}{"format"}
!ll&&!rl&&r
113
MC/DC66%2/3
defined $name and $data{$name}{"format"} and @argv
defined $name $data{$name}{"format"} @argv
Truth table75%3/4
A and B and C
A= defined $nameB= $data{$name}{"format"}C= @argv
ABCresult
0XX0
10X0
1100
1111
218 }
219 8 return (\@options, \@argv);
220 }
221  
222  
223 5 ▶sub _expand_option_name {
Subroutine _expand_option_name: called CC=3 SCAR=11.0
224 5 my ($class, $name, $data) = @_;
225 5 ▶ return $name if exists $data->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{$name} 1 4 5
226 4 my @matches = grep { index($_, $name) == 0 } keys %$data;
227 4 ▶ return @matches == 1 ? $matches[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
@matches == 1 ? : 2 2 4
228 }
229  
230 8 ▶sub run {
Subroutine run: called CC=6 SCAR=18.4
Pod documented
231 8 my ($self, @command) = @_;
232  
233 8 ▶ if (!@command && ($self->stop || $self->delete)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @command and $self->stop || $self->delete) 3 5 8
Condition66%4/6
not @command and $self->stop || $self->delete
!ll&&!rl&&r
503
$self->stop or $self->delete
l!l&&r!l&&!r
210
MC/DC33%1/3
not @command and $self->stop || $self->delete
not @command $self->stop $self->delete
Truth table75%3/4
not(A) and B or C
A= not @commandB= $self->stopC= $self->delete
ABCresult
0XX0
1000
11X1
1011
234 3 return $self->_exit_code($self->_teardown);
235 }
236  
237 5 $self->_ensure_running;
238 4 my $env = $self->_env_for_child;
239  
240 4 ▶ unless (@command) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@command) 0 4 4
241 0 print $self->_export_lines($env);
242 0 return 0;
243 }
244  
245 4 my ($status, $error);
246 {
247 4 local @ENV{keys %$env} = values %$env;
248 4 $status = $self->_run(@command);
249 4 $error = $!;
250 }
251 4 my $code = $self->_exit_code($status);
252 4 ▶ $self->_log('cannot run ' . $command[0] . ': ' . $error) if $status == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $status == -1 0 4 4
253  
254 4 $self->_teardown;
255 4 return $code;
256 }
257  
258  
259 5 ▶sub _ensure_running {
Subroutine _ensure_running: called CC=5 SCAR=16.1
260 5 my ($self) = @_;
261 5 my $profile = $self->profile;
262  
263 5 ▶ if (!$self->restart && $self->_running && -e $self->_kubeconfig_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->restart and $self->_running and -e $self->_kubeconfig_file) 1 4 5
Condition83%5/6
not $self->restart and $self->_running and -e $self->_kubeconfig_file
!ll&&!rl&&r
401
not $self->restart and $self->_running
!ll&&!rl&&r
131
MC/DC66%2/3
not $self->restart and $self->_running and -e $self->_kubeconfig_file
not $self->restart $self->_running -e $self->_kubeconfig_file
Truth table75%3/4
not(A) and B and C
A= not $self->restartB= $self->_runningC= -e $self->_kubeconfig_file
ABCresult
0XX0
10X0
1100
1111
264 1 $self->_log('profile ' . $profile . ' is already running');
265 1 return;
266 }
267  
268 4 $self->_log('starting profile ' . $profile . ' (driver ' . $self->driver . ') ...');
269 4 my $status = $self->_minikube_run($self->_start_args);
270 4 ▶ return if $status == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $status == 0 3 1 4
271  
272 1 my $code = $self->_exit_code($status);
273 # die exits with $! when it is non-zero: pass minikube's exit code on.
274 1 $! = $code;
275 1 die 'minikube start failed for profile ' . $profile
276 . ' (driver ' . $self->driver . ') with exit code ' . $code . "\n";
277 }
278  
279  
280 4 ▶sub _running {
Subroutine _running: called CC=10 SCAR=29.6
281 4 my ($self) = @_;
282 4 my $json = eval { $self->_minikube_capture($self->_status_args) };
283 4 ▶ return 0 unless defined $json;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $json 0 4 4
284 4 my $status = eval { decode_json($json) };
285 4 ▶ if (ref $status eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $status eq "ARRAY") 0 4 4
286 0 ▶ my ($control_plane) = grep { ref $_ eq 'HASH' && !$_->{Worker} } @$status;
Condition0%0/3
ref $_ eq 'HASH' && !$_->{'Worker'}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_ eq 'HASH' && !$_->{'Worker'}
ref $_ eq 'HASH' !$_->{'Worker'}
Truth table0%0/3
A && not(B)
A= ref $_ eq 'HASH'B= !$_->{'Worker'}
ABresult
0X0
100
111
287 0 ▶ $status = $control_plane // $status->[0];
Condition0%0/3
$control_plane // $status->[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
$control_plane // $status->[0]
$control_plane $status->[0]
Truth table0%0/3
A // B
A= $control_planeB= $status->[0]
ABresult
1X1
011
000
288 }
289 4 ▶ return 0 unless ref $status eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $status eq "HASH" 0 4 4
290 return ($status->{Host} // '') eq 'Running'
291 4 ▶ && ($status->{APIServer} // '') eq 'Running' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
($status->{'Host'} // '') eq 'Running' && ($status->{'APIServer'} // '') eq 'Running' ? : 1 3 4
Condition57%4/7
($status->{"Host"} // "") eq "Running" and ($status->{"APIServer"} // "") eq "Running"
!ll&&!rl&&r
301
$status->{"Host"} // ""
l!l
40
$status->{"APIServer"} // ""
l!l
10
MC/DC0%0/2
($status->{"Host"} // "") eq "Running" and ($status->{"APIServer"} // "") eq "Running"
$status->{"Host"} $status->{"APIServer"}
Truth table33%1/3
A and B
A= $status->{"Host"}B= $status->{"APIServer"}
ABresult
0X0
100
111
292 }
293  
294  
295 7 ▶sub _teardown {
Subroutine _teardown: called CC=6 SCAR=17.9
296 7 my ($self) = @_;
297 7 my $profile = $self->profile;
298 7 my $status = 0;
299 7 ▶ if ($self->delete) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->delete) { } 2 5 7
elsif ($self->stop) { } 1 4 5
300 2 $self->_log('deleting profile ' . $profile . ' ...');
301 2 $status = $self->_minikube_run($self->_delete_args);
302 2 ▶ $self->_log('minikube delete exited with ' . $self->_exit_code($status)) if $status;
Branch: 1 branch
BranchTrueFalseTotal
if $status 0 2 2
303 2 my $kubeconfig = $self->_kubeconfig_file;
304 2 ▶ if (-e $kubeconfig) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $kubeconfig) 1 1 2
305 1 $kubeconfig->remove;
306 1 $self->_log('removed ' . $kubeconfig);
307 }
308 } elsif ($self->stop) {
309 1 $self->_log('stopping profile ' . $profile . ' ...');
310 1 $status = $self->_minikube_run($self->_stop_args);
311 1 ▶ $self->_log('minikube stop exited with ' . $self->_exit_code($status)) if $status;
Branch: 1 branch
BranchTrueFalseTotal
if $status 0 1 1
312 }
313 7 return $status;
314 }
315  
316  
317 8 ▶sub _env_for_child {
Subroutine _env_for_child: called CC=5 SCAR=16.2
318 8 my ($self) = @_;
319 8 my $kubeconfig = $self->_kubeconfig_file->stringify;
320 26 my %env = map { $_ => $kubeconfig }
321 qw( KUBECONFIG TEST_KUBERNETES_REST_KUBECONFIG TEST_IO_K8S_KUBECONFIG ),
322 8 @{ $self->env };
323 8 ▶ $env{TEST_KUBERNETES_REST_CONTEXT} = $self->context
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->context and length $self->context 1 7 8
Condition66%2/3
defined $self->context and length $self->context
!ll&&!rl&&r
701
MC/DC50%1/2
defined $self->context and length $self->context
defined $self->context length $self->context
Truth table66%2/3
A and B
A= defined $self->contextB= length $self->context
ABresult
0X0
100
111
324 if defined $self->context && length $self->context;
325 8 my $bin_dir = path($self->minikube_bin)->absolute->parent;
326 8 ▶ $env{PATH} = join $Config{path_sep}, $bin_dir->stringify, ($ENV{PATH} // '')
Branch: 1 branch
BranchTrueFalseTotal
if $bin_dir eq $self->_install_dir_path 1 7 8
Condition50%1/2
$ENV{"PATH"} // ""
l!l
10
MC/DC0%0/1
$ENV{"PATH"} // ""
$ENV{"PATH"}
Truth table50%1/2
A
A= $ENV{"PATH"}
Aresult
11
00
327 if $bin_dir eq $self->_install_dir_path;
328 8 return \%env;
329 }
330  
331  
332 1 ▶sub _export_lines {
Subroutine _export_lines: called CC=1 SCAR=0.0
333 1 my ($self, $env) = @_;
334 return join '', map {
335 1 (my $value = $env->{$_}) =~ s/'/'\\''/g;
336 3 'export ' . $_ . "='" . $value . "'\n";
337 } sort keys %$env;
338 }
339  
340 10 ▶sub _start_args {
Subroutine _start_args: called CC=6 SCAR=17.9
341 10 my ($self) = @_;
342 return (
343 10 ▶ 'start', '-p', $self->profile,
Branch: 5 branches
BranchTrueFalseTotal
defined $self->kubernetes_version ? : 1 9 10
defined $self->cpus ? : 1 9 10
defined $self->memory ? : 1 9 10
defined $self->container_runtime ? : 2 8 10
$self->rootless ? : 1 9 10
344 '--driver=' . $self->driver,
345 '--interactive=false',
346 (defined $self->kubernetes_version ? ('--kubernetes-version=' . $self->kubernetes_version) : ()),
347 (defined $self->cpus ? ('--cpus=' . $self->cpus) : ()),
348 (defined $self->memory ? ('--memory=' . $self->memory) : ()),
349 (defined $self->container_runtime ? ('--container-runtime=' . $self->container_runtime) : ()),
350 ($self->rootless ? ('--rootless') : ()),
351 );
352 }
353  
354 6 ▶sub _status_args { ('status', '-p', $_[0]->profile, '-o', 'json') }
Subroutine _status_args: called CC=1 SCAR=0.0
355 3 ▶sub _stop_args { ('stop', '-p', $_[0]->profile) }
Subroutine _stop_args: called CC=1 SCAR=0.0
356 4 ▶sub _delete_args { ('delete', '-p', $_[0]->profile) }
Subroutine _delete_args: called CC=1 SCAR=0.0
357 3 ▶sub _version_args { ('version', '--output=json') }
Subroutine _version_args: called CC=1 SCAR=0.0
358  
359 7 ▶sub _minikube_run {
Subroutine _minikube_run: called CC=2 SCAR=7.0
360 7 my ($self, @args) = @_;
361 7 local $ENV{KUBECONFIG} = $self->_kubeconfig_file->stringify;
362 7 ▶ $self->_log('running: ' . join(' ', $self->minikube_bin, @args)) if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 7 7
363 7 return $self->_run($self->minikube_bin, @args);
364 }
365  
366 4 ▶sub _minikube_capture {
Subroutine _minikube_capture: called CC=2 SCAR=7.0
367 4 my ($self, @args) = @_;
368 4 local $ENV{KUBECONFIG} = $self->_kubeconfig_file->stringify;
369 4 ▶ $self->_log('running: ' . join(' ', $self->minikube_bin, @args)) if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 4 4
370 4 return $self->_capture($self->minikube_bin, @args);
371 }
372  
373 0 ▶sub _run {
Subroutine _run: not called CC=1 SCAR=1.7
374 0 my ($self, @cmd) = @_;
375 1 ▶ no warnings qw( exec );
Subroutine BEGIN: called CC=1 SCAR=0.0
376 0 system { $cmd[0] } @cmd;
377 0 return $?;
378 }
379  
380  
381 0 ▶sub _capture {
Subroutine _capture: not called CC=4 SCAR=25.6
382 0 my ($self, @cmd) = @_;
383 1 ▶ no warnings qw( exec );
Subroutine BEGIN: called CC=1 SCAR=0.0
384 0 ▶ open my $fh, '-|', @cmd
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "-|", @cmd or die 'Cannot run ' . $cmd[0] . ': ' . $! . "\n" 0 0 0
385 or die 'Cannot run ' . $cmd[0] . ': ' . $! . "\n";
386 0 my $output = do { local $/; <$fh> };
387 0 close $fh;
388 0 ▶ die 'Command "' . join(' ', @cmd) . '" failed with exit code ' . $self->_exit_code($?) . "\n"
Branch: 1 branch
BranchTrueFalseTotal
if $? 0 0 0
389 if $?;
390 0 ▶ return $output // '';
Condition0%0/2
$output // ""
l!l
00
MC/DC0%0/1
$output // ""
$output
Truth table0%0/2
A
A= $output
Aresult
11
00
391 }
392  
393  
394 0 ▶sub _download {
Subroutine _download: not called CC=2 SCAR=17.9
395 0 my ($self, $url, $file) = @_;
396 0 my $res = $self->_ua->get($url, ':content_file' => $file);
397 0 ▶ die 'Download of ' . $url . ' failed: ' . $res->status_line . "\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
398 unless $res->is_success;
399 0 return $file;
400 }
401  
402  
403 0 ▶sub _install_minikube {
Subroutine _install_minikube: not called CC=2 SCAR=17.9
404 0 my ($self) = @_;
405 0 my $dir = $self->_install_dir_path;
406 0 $dir->mkpath;
407 0 my $target = $dir->child('minikube');
408 0 my $tmp = $dir->child('minikube.tmp');
409 0 my $url = $self->_download_url;
410  
411 0 $self->_log('downloading ' . $url . ' to ' . $tmp . ' ...');
412 0 $self->_download($url, $tmp->stringify);
413  
414 0 $self->_log('verifying SHA-256 checksum ...');
415 0 my $expected = $self->_expected_sha256($url, $dir);
416 0 my $actual = Digest::SHA->new(256)->addfile($tmp->stringify, 'b')->hexdigest;
417 0 ▶ unless ($actual eq $expected) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($actual eq $expected) 0 0 0
418 0 $tmp->remove;
419 0 die 'SHA-256 mismatch for ' . $url . ': expected ' . $expected . ', got ' . $actual . "\n";
420 }
421  
422 0 chmod 0755, $tmp->stringify;
423 0 $tmp->move($target->stringify);
424 0 $self->_log('installed ' . $target);
425 0 return $target->stringify;
426 }
427  
428 0 ▶sub _expected_sha256 {
Subroutine _expected_sha256: not called CC=2 SCAR=17.9
429 0 my ($self, $url, $dir) = @_;
430 0 my $file = $dir->child('minikube.tmp.sha256');
431 0 $self->_download($url . '.sha256', $file->stringify);
432 0 ▶ my ($digest) = $file->slurp =~ /\b([0-9a-fA-F]{64})\b/
Branch: 1 branch
BranchTrueFalseTotal
my($digest) = $file->slurp =~ /\b([0-9a-fA-F]{64})\b/ or die 'No SHA-256 digest found in ' . $url . ".sha256\n" 0 0 0
433 or die 'No SHA-256 digest found in ' . $url . ".sha256\n";
434 0 $file->remove;
435 0 return lc $digest;
436 }
437  
438 2 ▶sub _download_url {
Subroutine _download_url: called CC=2 SCAR=6.9
439 2 my ($self) = @_;
440 2 my ($os, $arch) = $self->_platform;
441 2 ▶ return $RELEASES . ($self->minikube_version // 'latest') . '/minikube-' . $os . '-' . $arch;
Condition100%2/2
$self->minikube_version // "latest"
l!l
11
MC/DC100%1/1
$self->minikube_version // "latest"
$self->minikube_version
Truth table100%2/2
A
A= $self->minikube_version
Aresult
11
00
442 }
443  
444 1 my %OS = (linux => 'linux', darwin => 'darwin');
445 1 my %ARCH = (x86_64 => 'amd64', amd64 => 'amd64', aarch64 => 'arm64', arm64 => 'arm64');
446  
447 7 ▶sub _platform {
Subroutine _platform: called CC=3 SCAR=11.0
448 7 my ($self) = @_;
449 7 ▶ my $os = $OS{$^O}
Branch: 1 branch
BranchTrueFalseTotal
my $os = $OS{$^O} or die 'Unsupported operating system "' . $^O . '": minikube releases exist for linux and darwin' . "\n" 6 1 7
450 or die 'Unsupported operating system "' . $^O . '": minikube releases exist for linux and darwin' . "\n";
451 6 my $machine = (POSIX::uname())[4];
452 6 ▶ my $arch = $ARCH{$machine}
Branch: 1 branch
BranchTrueFalseTotal
my $arch = $ARCH{$machine} or die 'Unsupported architecture "' . $machine . '": minikube releases exist for x86_64 and arm64' . "\n" 5 1 6
453 or die 'Unsupported architecture "' . $machine . '": minikube releases exist for x86_64 and arm64' . "\n";
454 5 return ($os, $arch);
455 }
456  
457 2 ▶sub _minikube_version_of {
Subroutine _minikube_version_of: called CC=4 SCAR=14.0
458 2 my ($self, $bin) = @_;
459 2 my $output = eval { $self->_capture($bin, $self->_version_args) };
460 2 ▶ return '' unless defined $output;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $output 0 2 2
461 2 my $data = eval { decode_json($output) };
462 2 ▶ return ref $data eq 'HASH' ? ($data->{minikubeVersion} // '') : '';
Branch: 1 branch
BranchTrueFalseTotal
ref $data eq 'HASH' ? : 1 1 2
Condition50%1/2
$data->{"minikubeVersion"} // ""
l!l
10
MC/DC0%0/1
$data->{"minikubeVersion"} // ""
$data->{"minikubeVersion"}
Truth table50%1/2
A
A= $data->{"minikubeVersion"}
Aresult
11
00
463 }
464  
465 0 ▶sub _find_in_path {
Subroutine _find_in_path: not called CC=6 SCAR=37.4
466 0 my ($self, $name) = @_;
467 0 ▶ for my $dir (split /\Q$Config{path_sep}\E/, $ENV{PATH} // '') {
Condition0%0/2
$ENV{"PATH"} // ""
l!l
00
MC/DC0%0/1
$ENV{"PATH"} // ""
$ENV{"PATH"}
Truth table0%0/2
A
A= $ENV{"PATH"}
Aresult
11
00
468 0 ▶ next unless length $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless length $dir 0 0 0
469 0 my $candidate = path($dir, $name);
470 0 ▶ return $candidate->stringify if -f $candidate && -x _;
Branch: 1 branch
BranchTrueFalseTotal
if -f $candidate and -x _ 0 0 0
Condition0%0/3
-f $candidate and -x _
!ll&&!rl&&r
000
MC/DC0%0/2
-f $candidate and -x _
-f $candidate -x _
Truth table0%0/3
A and B
A= -f $candidateB= -x _
ABresult
0X0
100
111
471 }
472 0 return;
473 }
474  
475 8 ▶sub _exit_code {
Subroutine _exit_code: called CC=3 SCAR=11.3
476 8 my ($self, $status) = @_;
477 8 ▶ return 127 if $status == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $status == -1 0 8 8
478 8 my $signal = $status & 127;
479 8 ▶ return 128 + $signal if $signal;
Branch: 1 branch
BranchTrueFalseTotal
if $signal 0 8 8
480 8 return $status >> 8;
481 }
482  
483 9 ▶sub _log {
Subroutine _log: called CC=1 SCAR=0.0
484 9 my ($self, @message) = @_;
485 9 print STDERR $LOG_PREFIX . join('', @message) . "\n";
486 9 return;
487 }
488  
489 1 1;
490  
491 __END__
« lib/Kubernetes/REST.pm ↑ Summary blib/lib/Kubernetes/REST/CLI/Cmd/Get.pm »