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/AuthTokenFile.pm »
1 package Kubernetes::REST::CLI::Minikube;
2 1 our $VERSION = '1.109';
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__
492  
493 =pod
494  
495 =encoding UTF-8
496  
497 =head1 NAME
498  
499 Kubernetes::REST::CLI::Minikube - Bring up a minikube test cluster and run a command against it
500  
501 =head1 VERSION
502  
503 version 1.109
504  
505 =head1 SYNOPSIS
506  
507 use Kubernetes::REST::CLI::Minikube;
508  
509 my ($options, $command) = Kubernetes::REST::CLI::Minikube->split_argv(@ARGV);
510 @ARGV = @$options;
511 my $runner = Kubernetes::REST::CLI::Minikube->new_with_options;
512 exit $runner->run(@$command);
513  
514 =head1 DESCRIPTION
515  
516 L<MooX::Options>-based class that powers the L<kube_test_minikube> CLI tool.
517 It makes sure a C<minikube> binary is available (downloading a verified one
518 into a cache directory when there is none), brings up a minikube profile whose
519 kubeconfig lives in an isolated file, exports the live-test gate variables of
520 the Kubernetes::REST family of distributions and runs the command it was
521 given with that environment.
522  
523 This class does not talk to the Kubernetes API and does not consume
524 L<Kubernetes::REST::CLI::Role::Connection>: it only orchestrates the
525 C<minikube> binary. Every process interaction goes through L</_run>,
526 L</_capture> and L</_download>, so tests can replace them without a real
527 minikube.
528  
529 The C<KUBECONFIG> environment variable is set to the isolated file for every
530 C<minikube> invocation, so the user's F<~/.kube/config> is never read or
531 written.
532  
533 =head2 profile
534  
535 The minikube profile to bring up, passed as C<-p> to every C<minikube>
536 invocation. Default: C<kube-rest-test>. The profile also names the kubeconfig
537 context minikube writes.
538  
539 Short option: C<-p>
540  
541 =head2 driver
542  
543 The minikube driver, passed as C<--driver=> to C<minikube start>. Default:
544 C<docker>.
545  
546 Short option: C<-d>
547  
548 =head2 kubernetes_version
549  
550 Kubernetes version for the cluster, passed as C<--kubernetes-version=> to
551 C<minikube start> when set. Omit to let minikube pick its default.
552  
553 Short option: C<-k>
554  
555 =head2 minikube_version
556  
557 Pin the minikube release, as tagged upstream (C<v1.38.1>). When set, the
558 binary in L</install_dir> is used if it reports exactly this version and
559 downloaded from the release directory of that version otherwise; a
560 C<minikube> found on C<PATH> is ignored. Without a pin a C<minikube> on
561 C<PATH> is used as-is, then one already in L</install_dir>, and only when
562 neither exists the latest release is downloaded.
563  
564 =head2 cpus
565  
566 Number of CPUs for the cluster, passed as C<--cpus=> to C<minikube start>
567 when set.
568  
569 Short option: C<-c>
570  
571 =head2 memory
572  
573 Memory for the cluster in minikube's notation (C<4g>, C<4096mb>, or a plain
574 number of megabytes), passed as C<--memory=> to C<minikube start> when set.
575  
576 Short option: C<-m>
577  
578 =head2 container_runtime
579  
580 Container runtime for the cluster, passed as C<--container-runtime=> to
581 C<minikube start> when set. On a Podman backend the rootless driver needs
582 C<--driver podman --container-runtime containerd --rootless>.
583  
584 =head2 rootless
585  
586 Pass C<--rootless> to C<minikube start>. Required by the rootless C<podman>
587 driver, which additionally needs C<--container-runtime containerd> (see
588 L</container_runtime>).
589  
590 =head2 install_dir
591  
592 Directory a downloaded C<minikube> binary is installed into, and where the
593 default L</kubeconfig> lives. Default: C<$XDG_CACHE_HOME/kube_test_minikube>,
594 or F<~/.cache/kube_test_minikube> when C<XDG_CACHE_HOME> is not set.
595  
596 =head2 kubeconfig
597  
598 Path of the isolated kubeconfig file. Default:
599 F<E<lt>install_dirE<gt>/E<lt>profileE<gt>.kubeconfig>. Every C<minikube>
600 invocation runs with C<KUBECONFIG> set to this file, and it is the value all
601 exported variables receive. Removed again by L</delete>.
602  
603 =head2 env
604  
605 Name of an additional environment variable to set to the kubeconfig path,
606 for distributions with a live-test gate of their own. Repeatable.
607  
608 Short option: C<-e>
609  
610 =head2 context
611  
612 When set, C<TEST_KUBERNETES_REST_CONTEXT> is exported with this value as well.
613 Without it that variable is left alone.
614  
615 =head2 stop
616  
617 Run C<minikube stop> after the command has finished. Without a command, stop
618 the profile and exit without starting anything.
619  
620 Short option: C<-s>
621  
622 =head2 delete
623  
624 Run C<minikube delete> after the command has finished and remove the
625 kubeconfig file. Without a command, delete the profile and exit without
626 starting anything. Takes precedence over L</stop>.
627  
628 Short option: C<-D>
629  
630 =head2 restart
631  
632 Run C<minikube start> even when the profile already reports a running host
633 and API server. C<start> is idempotent on minikube's side; this makes it
634 re-check the node.
635  
636 Short option: C<-r>
637  
638 =head2 verbose
639  
640 Print each C<minikube> command line to STDERR before it runs.
641  
642 Short option: C<-v>
643  
644 =head2 minikube_bin
645  
646 Path of the C<minikube> binary in use. Resolved lazily as described under
647 L</minikube_version>, downloading and verifying a release when nothing usable
648 exists; pass it to the constructor to use a specific binary and skip the
649 lookup.
650  
651 =head2 split_argv
652  
653 my ($options, $command) = Kubernetes::REST::CLI::Minikube->split_argv(@ARGV);
654  
655 Class method. Splits a command line into the tool's own options and the
656 command to run: the command starts at the first argument that is not an
657 option or the value of an option that takes one, or after a C<-->. Returns
658 two array references. L<MooX::Options> rewrites C<@ARGV> before parsing it
659 (a bundled C<-lr> becomes C<-l r>), so the command has to be taken out
660 before C<new_with_options> ever sees it - this is what C<bin/kube_test_minikube>
661 does. Unique prefixes of long option names are recognised the way
662 L<Getopt::Long> recognises them.
663  
664 =head2 run
665  
666 my $exit_code = $runner->run(@command);
667  
668 Entry point called by C<bin/kube_test_minikube>. Makes sure the profile is
669 running (L</_ensure_running>), then runs C<@command> with the environment
670 from L</_env_for_child> and returns the command's exit status: its exit code,
671 or 128 plus the signal number when it died from a signal, or 127 when it
672 could not be started at all. L</stop> and L</delete> tear the profile down
673 after the command and never mask that status.
674  
675 Without a command it prints one C<export NAME='value'> line per exported
676 variable to STDOUT, for C<eval "$(kube_test_minikube)">, and returns 0 -
677 unless L</stop> or L</delete> is set, in which case it only tears down and
678 returns minikube's exit status.
679  
680 Dies when C<minikube start> fails, naming the profile and driver; the
681 process exit code is then minikube's.
682  
683 =head2 _ensure_running
684  
685 Runs C<minikube start> unless L</_running> reports the profile up and the
686 kubeconfig file exists, or always with L</restart>. Dies naming the profile
687 and driver when C<start> fails.
688  
689 =head2 _running
690  
691 Returns true when C<minikube status -o json> reports both the host and the
692 API server of the profile as C<Running>. A failing C<status> (unknown or
693 stopped profile) counts as not running. On a multi-node profile the
694 control-plane entry is the one consulted.
695  
696 =head2 _teardown
697  
698 Runs C<minikube delete> (and removes the kubeconfig file) when L</delete> is
699 set, otherwise C<minikube stop> when L</stop> is set, otherwise nothing.
700 Returns the raw C<$?> of the minikube command, 0 when nothing ran.
701  
702 =head2 _env_for_child
703  
704 Returns a hash reference of the environment variables exported to the
705 command: C<KUBECONFIG>, C<TEST_KUBERNETES_REST_KUBECONFIG>,
706 C<TEST_IO_K8S_KUBECONFIG> and every L</env> name, all set to the absolute
707 kubeconfig path; C<TEST_KUBERNETES_REST_CONTEXT> only when L</context> is
708 given; and C<PATH> with L</install_dir> prepended when the binary in use
709 lives there, so C<minikube> is reachable from inside the command.
710  
711 =head2 _run
712  
713 my $status = $runner->_run(@cmd);
714  
715 Runs C<@cmd> without a shell, with STDIN, STDOUT and STDERR passed through,
716 and returns the raw C<$?> (-1 when it could not be started). The seam every
717 pass-through process interaction goes through; override it in tests.
718  
719 =head2 _capture
720  
721 my $stdout = $runner->_capture(@cmd);
722  
723 Runs C<@cmd> without a shell and returns its STDOUT as a string (STDERR is
724 passed through). Dies when the command cannot be started or exits non-zero.
725 The seam every parsed process interaction goes through; override it in tests.
726  
727 =head2 _download
728  
729 $runner->_download($url, $file);
730  
731 Fetches C<$url> straight into C<$file> (streamed to disk, never held in
732 memory) and dies on any HTTP failure. The seam for every HTTP interaction;
733 override it in tests.
734  
735 =head1 SEE ALSO
736  
737 =over
738  
739 =item * L<kube_test_minikube> - The CLI tool built on this class
740  
741 =item * L<Kubernetes::REST> - The client whose live tests this runs
742  
743 =item * L<Kubernetes::REST::CLI::Watch> - The other L<MooX::Options> CLI class of this distribution
744  
745 =back
746  
747 =head1 SUPPORT
748  
749 =head2 Issues
750  
751 Please report bugs and feature requests on GitHub at
752 L<https://github.com/pplu/kubernetes-rest/issues>.
753  
754 =head2 IRC
755  
756 Join C<#kubernetes> on C<irc.perl.org> or message Getty directly.
757  
758 =head1 CONTRIBUTING
759  
760 Contributions are welcome! Please fork the repository and submit a pull request.
761  
762 =head1 AUTHORS
763  
764 =over 4
765  
766 =item *
767  
768 Torsten Raudssus <getty@cpan.org>
769  
770 =item *
771  
772 Jose Luis Martinez Torres <jlmartin@cpan.org>
773  
774 =back
775  
776 =head1 COPYRIGHT AND LICENSE
777  
778 This software is Copyright (c) 2019-2026 by Jose Luis Martinez Torres <jlmartin@cpan.org>.
779  
780 This is free software, licensed under:
781  
782 The Apache License, Version 2.0, January 2004
783  
784 =cut
« lib/Kubernetes/REST.pm ↑ Summary blib/lib/Kubernetes/REST/AuthTokenFile.pm »