API/Client.pm

filter: Statementstmt 81.7% 215 / 263 Branchbran 59.8% 91 / 152 Conditioncond 36.4% 39 / 107 MC/DCmcdc 17.4% 11 / 63 Subroutinesub 82.7% 24 / 29 Podpod 0.0% 0 / 8 totaltotal 61.0% 380 / 622 CC 136 SCAR 38.0
CC
136
Cov
66%
__ANON__
132.5
_build_auth_file_candidates
132.5
build_image
78.6
CRAP 44.7
↑ Summary API.pm »
1 package Dist::Zilla::Plugin::Docker::API::Client;
2 # ABSTRACT: Thin adapter around API::Docker
3 9 our $VERSION = '0.107';
4 9 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use Archive::Tar;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use Archive::Tar::Constant ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use JSON::MaybeXS qw( decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use MIME::Base64 qw( decode_base64 );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 9 ▶use API::Docker;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use Dist::Zilla::Plugin::Docker::API::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 has docker => (
16 is => 'ro',
17 lazy => 1,
18 0 ▶ builder => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine _build_docker: not called CC=1 SCAR=6.9
19 0 API::Docker->new;
20 },
21 9 );
22  
23 9 has logger => (
24 is => 'ro',
25 required => 1,
26 );
27  
28 9 has logger_fatal => (
29 is => 'ro',
30 required => 1,
31 );
32  
33 # A single explicit auth file. The historical seam: setting it overrides the
34 # whole discovery list below with just this one file. The @Author::GETTY::Docker
35 # bundle never sets it; the below-seam tests inject a fixture through it.
36 9 has docker_config_path => (
37 is => 'ro',
38 predicate => 'has_docker_config_path',
39 );
40  
41 # The ordered list of auth files to consult, most specific first. Podman's
42 # auth.json and Docker's config.json share the same {"auths":{...}} format, so
43 # one list covers both engines. A set docker_config_path replaces the list;
44 # an empty/unset env var simply drops its stage. ~/.dockercfg (legacy) is left
45 # out on purpose.
46 has auth_file_candidates => (
47 is => 'ro',
48 lazy => 1,
49 6 ▶ builder => sub {
Subroutine _build_auth_file_candidates: called CC=12 SCAR=48.9
50 6 my ($self) = @_;
51 6 ▶ return [ $self->docker_config_path ] if $self->has_docker_config_path;
Branch: 2 branches
BranchTrueFalseTotal
if $self->has_docker_config_path 6 0 6
if $self->has_docker_config_path 0 0 0
52  
53 0 my @candidates;
54  
55 # 1. REGISTRY_AUTH_FILE points straight at the file (podman's override).
56 push @candidates, Path::Tiny::path($ENV{REGISTRY_AUTH_FILE})
57 0 ▶ if defined $ENV{REGISTRY_AUTH_FILE} && length $ENV{REGISTRY_AUTH_FILE};
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"REGISTRY_AUTH_FILE"} and length $ENV{"REGISTRY_AUTH_FILE"} 0 0 0
Condition0%0/6
defined $ENV{"REGISTRY_AUTH_FILE"} and length $ENV{"REGISTRY_AUTH_FILE"}
!ll&&!rl&&r
000
defined $ENV{"REGISTRY_AUTH_FILE"} and length $ENV{"REGISTRY_AUTH_FILE"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ENV{"REGISTRY_AUTH_FILE"} and length $ENV{"REGISTRY_AUTH_FILE"}
defined $ENV{"REGISTRY_AUTH_FILE"} length $ENV{"REGISTRY_AUTH_FILE"}
MC/DC0%0/2
defined $ENV{"REGISTRY_AUTH_FILE"} and length $ENV{"REGISTRY_AUTH_FILE"}
defined $ENV{"REGISTRY_AUTH_FILE"} length $ENV{"REGISTRY_AUTH_FILE"}
Truth table0%0/3
A and B
A= defined $ENV{"REGISTRY_AUTH_FILE"}B= length $ENV{"REGISTRY_AUTH_FILE"}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $ENV{"REGISTRY_AUTH_FILE"}B= length $ENV{"REGISTRY_AUTH_FILE"}
ABresult
0X0
100
111
58  
59 # 2. $DOCKER_CONFIG/config.json
60 push @candidates, Path::Tiny::path($ENV{DOCKER_CONFIG}, 'config.json')
61 0 ▶ if defined $ENV{DOCKER_CONFIG} && length $ENV{DOCKER_CONFIG};
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"DOCKER_CONFIG"} and length $ENV{"DOCKER_CONFIG"} 0 0 0
Condition0%0/6
defined $ENV{"DOCKER_CONFIG"} and length $ENV{"DOCKER_CONFIG"}
!ll&&!rl&&r
000
defined $ENV{"DOCKER_CONFIG"} and length $ENV{"DOCKER_CONFIG"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ENV{"DOCKER_CONFIG"} and length $ENV{"DOCKER_CONFIG"}
defined $ENV{"DOCKER_CONFIG"} length $ENV{"DOCKER_CONFIG"}
MC/DC0%0/2
defined $ENV{"DOCKER_CONFIG"} and length $ENV{"DOCKER_CONFIG"}
defined $ENV{"DOCKER_CONFIG"} length $ENV{"DOCKER_CONFIG"}
Truth table0%0/3
A and B
A= defined $ENV{"DOCKER_CONFIG"}B= length $ENV{"DOCKER_CONFIG"}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $ENV{"DOCKER_CONFIG"}B= length $ENV{"DOCKER_CONFIG"}
ABresult
0X0
100
111
62  
63 # 3. $XDG_RUNTIME_DIR/containers/auth.json (podman's live login store).
64 push @candidates, Path::Tiny::path($ENV{XDG_RUNTIME_DIR}, 'containers', 'auth.json')
65 0 ▶ if defined $ENV{XDG_RUNTIME_DIR} && length $ENV{XDG_RUNTIME_DIR};
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"XDG_RUNTIME_DIR"} and length $ENV{"XDG_RUNTIME_DIR"} 0 0 0
Condition0%0/6
defined $ENV{"XDG_RUNTIME_DIR"} and length $ENV{"XDG_RUNTIME_DIR"}
!ll&&!rl&&r
000
defined $ENV{"XDG_RUNTIME_DIR"} and length $ENV{"XDG_RUNTIME_DIR"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ENV{"XDG_RUNTIME_DIR"} and length $ENV{"XDG_RUNTIME_DIR"}
defined $ENV{"XDG_RUNTIME_DIR"} length $ENV{"XDG_RUNTIME_DIR"}
MC/DC0%0/2
defined $ENV{"XDG_RUNTIME_DIR"} and length $ENV{"XDG_RUNTIME_DIR"}
defined $ENV{"XDG_RUNTIME_DIR"} length $ENV{"XDG_RUNTIME_DIR"}
Truth table0%0/3
A and B
A= defined $ENV{"XDG_RUNTIME_DIR"}B= length $ENV{"XDG_RUNTIME_DIR"}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $ENV{"XDG_RUNTIME_DIR"}B= length $ENV{"XDG_RUNTIME_DIR"}
ABresult
0X0
100
111
66  
67 # 4. $XDG_CONFIG_HOME/containers/auth.json, falling back to ~/.config.
68 my $config_home = (defined $ENV{XDG_CONFIG_HOME} && length $ENV{XDG_CONFIG_HOME})
69 ? $ENV{XDG_CONFIG_HOME}
70 0 ▶ : Path::Tiny::path($ENV{HOME} // '', '.config');
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'XDG_CONFIG_HOME'} && length $ENV{'XDG_CONFIG_HOME'} ? : 0 0 0
Condition0%0/10
defined $ENV{"XDG_CONFIG_HOME"} and length $ENV{"XDG_CONFIG_HOME"}
!ll&&!rl&&r
000
$ENV{"HOME"} // ""
l!l
00
defined $ENV{"XDG_CONFIG_HOME"} and length $ENV{"XDG_CONFIG_HOME"}
!ll&&!rl&&r
000
$ENV{"HOME"} // ""
l!l
00
MC/DC0%0/2
defined $ENV{"XDG_CONFIG_HOME"} and length $ENV{"XDG_CONFIG_HOME"}
defined $ENV{"XDG_CONFIG_HOME"} length $ENV{"XDG_CONFIG_HOME"}
MC/DC0%0/1
$ENV{"HOME"} // ""
$ENV{"HOME"}
MC/DC0%0/2
defined $ENV{"XDG_CONFIG_HOME"} and length $ENV{"XDG_CONFIG_HOME"}
defined $ENV{"XDG_CONFIG_HOME"} length $ENV{"XDG_CONFIG_HOME"}
MC/DC0%0/1
$ENV{"HOME"} // ""
$ENV{"HOME"}
Truth table0%0/3
A and B
A= defined $ENV{"XDG_CONFIG_HOME"}B= length $ENV{"XDG_CONFIG_HOME"}
ABresult
0X0
100
111
Truth table0%0/2
A
A= $ENV{"HOME"}
Aresult
11
00
Truth table0%0/3
A and B
A= defined $ENV{"XDG_CONFIG_HOME"}B= length $ENV{"XDG_CONFIG_HOME"}
ABresult
0X0
100
111
Truth table0%0/2
A
A= $ENV{"HOME"}
Aresult
11
00
71 0 push @candidates, Path::Tiny::path($config_home, 'containers', 'auth.json');
72  
73 # 5. ~/.docker/config.json (classic docker location).
74 0 ▶ push @candidates, Path::Tiny::path($ENV{HOME} // '', '.docker', 'config.json');
Condition0%0/2
$ENV{"HOME"} // ""
l!l
00
MC/DC0%0/1
$ENV{"HOME"} // ""
$ENV{"HOME"}
Truth table0%0/2
A
A= $ENV{"HOME"}
Aresult
11
00
75  
76 0 return \@candidates;
77 },
78 9 );
79  
80 # Parsed auth files, in candidate order. Missing, unreadable or unparseable
81 # files are skipped (a parse error warns, as before, then falls through), so
82 # the list holds only the configs that actually yielded JSON.
83 has _auth_configs => (
84 is => 'ro',
85 lazy => 1,
86 11 ▶ builder => sub {
Subroutine __ANON__: called CC=7 SCAR=26.1
87 0 my ($self) = @_;
88 0 my @configs;
89 0 for my $path (@{ $self->auth_file_candidates }) {
90 0 ▶ next unless $path;
Branch: 2 branches
BranchTrueFalseTotal
unless $path 0 15 15
unless $path 0 0 0
91 0 my $file = Path::Tiny::path($path);
92 0 ▶ next unless -r "$file";
Branch: 2 branches
BranchTrueFalseTotal
unless -r "$file" 2 13 15
unless -r "$file" 0 0 0
93 0 my $data = eval { decode_json($file->slurp_utf8) };
94 13 ▶ if ($@ || !$data) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@ or not $data) 1 12 13
if ($@ or not $data) 0 0 0
Condition33%2/6
$@ or not $data
l!l&&r!l&&!r
1012
$@ or not $data
l!l&&r!l&&!r
000
MC/DC50%1/2
$@ or not $data
$@ not $data
MC/DC0%0/2
$@ or not $data
$@ not $data
Truth table66%2/3
A or not(B)
A= $@B= not $data
ABresult
1X1
011
000
Truth table0%0/3
A or not(B)
A= $@B= not $data
ABresult
1X1
011
000
95 1 ▶ $self->logger->("Warning: cannot parse $file: $@") if $@;
Branch: 2 branches
BranchTrueFalseTotal
if $@ 1 0 1
if $@ 0 0 0
96 1 next;
97 }
98 12 push @configs, $data;
99 }
100 11 return \@configs;
101 },
102 9 );
103  
104 # Cheapest round trip that proves an engine is actually listening on the
105 # socket. Used by the plugin's before_build precheck, so a missing daemon
106 # is reported before Dist::Zilla does any work rather than after.
107 0 ▶sub engine_info {
Subroutine engine_info: not called CC=6 SCAR=37.4
Pod undocumented
108 0 my ($self) = @_;
109  
110 0 my $version = eval { $self->docker->system->version };
111 0 ▶ croak $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
112 0 ▶ croak 'the engine returned no version information'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $version eq "HASH" 0 0 0
113 unless ref $version eq 'HASH';
114  
115 0 ▶ my ($engine) = grep { ($_->{Name} // '') =~ /engine/i }
Condition0%0/2
$_->{"Name"} // ""
l!l
00
MC/DC0%0/1
$_->{"Name"} // ""
$_->{"Name"}
Truth table0%0/2
A
A= $_->{"Name"}
Aresult
11
00
116 0 ▶ @{ $version->{Components} // [] };
Condition0%0/2
$version->{"Components"} // []
l!l
00
MC/DC0%0/1
$version->{"Components"} // []
$version->{"Components"}
Truth table0%0/2
A
A= $version->{"Components"}
Aresult
11
00
117  
118 return {
119 version => $version->{Version},
120 api_version => $version->{ApiVersion},
121 engine => $engine ? $engine->{Name} : undef,
122 0 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
$engine ? : 0 0 0
123 }
124  
125 # The engine's tag endpoint takes the repository and the tag as two separate
126 # parameters (POST /images/{name}/tag?repo=&tag=), so a full image reference
127 # has to be taken apart before it goes over the wire. Passing the whole thing
128 # as `repo` leaves `tag` empty and the engine fills in its own default:
129 # podman then rejects `example/app:1.0:latest` with a 500 and every version
130 # tag is quietly lost. Splitting happens here rather than in API::Docker,
131 # which mirrors the endpoint and should keep doing so.
132 4 ▶sub _split_image_ref {
Subroutine _split_image_ref: called CC=3 SCAR=11.0
133 4 my ( $self, $ref ) = @_;
134  
135 4 my $colon = rindex $ref, ':';
136 4 ▶ return ( $ref, undef ) if $colon < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $colon < 0 0 4 4
137 # A colon before the last slash belongs to a registry host:port, not to a
138 # tag: registry.example.com:5000/team/app carries no tag at all.
139 4 ▶ return ( $ref, undef ) if rindex($ref, '/') > $colon;
Branch: 1 branch
BranchTrueFalseTotal
if rindex($ref, "/") > $colon 1 3 4
140  
141 3 return ( substr($ref, 0, $colon), substr($ref, $colon + 1) );
142 }
143  
144 7 ▶sub build_image {
Subroutine build_image: called CC=38 SCAR=43.6
Pod undocumented
145 7 my ($self, %arg) = @_;
146  
147 7 my $context = $arg{context_tar};
148 7 ▶ my $dockerfile = $arg{dockerfile} // 'Dockerfile';
Condition50%1/2
$arg{"dockerfile"} // "Dockerfile"
l!l
07
MC/DC0%0/1
$arg{"dockerfile"} // "Dockerfile"
$arg{"dockerfile"}
Truth table50%1/2
A
A= $arg{"dockerfile"}
Aresult
11
00
149 7 ▶ my @tags = @{ $arg{tags} // [] };
Condition50%1/2
$arg{"tags"} // []
l!l
70
MC/DC0%0/1
$arg{"tags"} // []
$arg{"tags"}
Truth table50%1/2
A
A= $arg{"tags"}
Aresult
11
00
150 7 ▶ my %labels = %{ $arg{labels} // {} };
Condition50%1/2
$arg{"labels"} // {}
l!l
07
MC/DC0%0/1
$arg{"labels"} // {}
$arg{"labels"}
Truth table50%1/2
A
A= $arg{"labels"}
Aresult
11
00
151 7 ▶ my %buildargs = %{ $arg{buildargs} // {} };
Condition50%1/2
$arg{"buildargs"} // {}
l!l
07
MC/DC0%0/1
$arg{"buildargs"} // {}
$arg{"buildargs"}
Truth table50%1/2
A
A= $arg{"buildargs"}
Aresult
11
00
152 7 ▶ my $pull = $arg{pull} // 0;
Condition50%1/2
$arg{"pull"} // 0
l!l
07
MC/DC0%0/1
$arg{"pull"} // 0
$arg{"pull"}
Truth table50%1/2
A
A= $arg{"pull"}
Aresult
11
00
153 7 ▶ my $nocache = $arg{nocache} // 0;
Condition50%1/2
$arg{"nocache"} // 0
l!l
07
MC/DC0%0/1
$arg{"nocache"} // 0
$arg{"nocache"}
Truth table50%1/2
A
A= $arg{"nocache"}
Aresult
11
00
154 7 ▶ my $rm = $arg{rm} // 1;
Condition50%1/2
$arg{"rm"} // 1
l!l
07
MC/DC0%0/1
$arg{"rm"} // 1
$arg{"rm"}
Truth table50%1/2
A
A= $arg{"rm"}
Aresult
11
00
155 7 ▶ my $forcerm = $arg{forcerm} // 1;
Condition50%1/2
$arg{"forcerm"} // 1
l!l
07
MC/DC0%0/1
$arg{"forcerm"} // 1
$arg{"forcerm"}
Truth table50%1/2
A
A= $arg{"forcerm"}
Aresult
11
00
156 7 my $target = $arg{target};
157 7 my $network_mode = $arg{network_mode};
158 7 my $platform = $arg{platform};
159 7 ▶ my $verbose = $arg{verbose} // 0;
Condition100%2/2
$arg{"verbose"} // 0
l!l
34
MC/DC100%1/1
$arg{"verbose"} // 0
$arg{"verbose"}
Truth table100%2/2
A
A= $arg{"verbose"}
Aresult
11
00
160  
161 7 my $docker = $self->docker;
162  
163 7 ▶ my %build_opts = (
Branch: 5 branches
BranchTrueFalseTotal
@tags ? : 7 0 7
$pull ? : 0 7 7
$nocache ? : 0 7 7
$rm ? : 7 0 7
$forcerm ? : 7 0 7
164 dockerfile => $dockerfile,
165 t => @tags ? $tags[0] : undef,
166 pull => $pull ? 1 : 0,
167 nocache => $nocache ? 1 : 0,
168 rm => $rm ? 1 : 0,
169 forcerm => $forcerm ? 1 : 0,
170 );
171  
172 7 ▶ $build_opts{labels} = \%labels if %labels;
Branch: 1 branch
BranchTrueFalseTotal
if %labels 0 7 7
173 7 ▶ $build_opts{buildargs} = \%buildargs if %buildargs;
Branch: 1 branch
BranchTrueFalseTotal
if %buildargs 0 7 7
174 7 ▶ $build_opts{target} = $target if defined $target && length $target;
Branch: 1 branch
BranchTrueFalseTotal
if defined $target and length $target 0 7 7
Condition33%1/3
defined $target and length $target
!ll&&!rl&&r
700
MC/DC0%0/2
defined $target and length $target
defined $target length $target
Truth table33%1/3
A and B
A= defined $targetB= length $target
ABresult
0X0
100
111
175 7 ▶ $build_opts{networkmode} = $network_mode if defined $network_mode && length $network_mode;
Branch: 1 branch
BranchTrueFalseTotal
if defined $network_mode and length $network_mode 0 7 7
Condition33%1/3
defined $network_mode and length $network_mode
!ll&&!rl&&r
700
MC/DC0%0/2
defined $network_mode and length $network_mode
defined $network_mode length $network_mode
Truth table33%1/3
A and B
A= defined $network_modeB= length $network_mode
ABresult
0X0
100
111
176 7 ▶ $build_opts{platform} = $platform if defined $platform && length $platform;
Branch: 1 branch
BranchTrueFalseTotal
if defined $platform and length $platform 0 7 7
Condition33%1/3
defined $platform and length $platform
!ll&&!rl&&r
700
MC/DC0%0/2
defined $platform and length $platform
defined $platform length $platform
Truth table33%1/3
A and B
A= defined $platformB= length $platform
ABresult
0X0
100
111
177  
178 7 my $image_id;
179 my @processed_tags;
180  
181 # Concise mode (default): only forward Dockerfile step headers — both the
182 # legacy builder ("Step N/M : ...") and BuildKit ("#N [N/M] ..."). Skip the
183 # noisy intermediate container chatter. Verbose mode forwards every line.
184 # The line buffer is used in both modes so the Dist::Zilla logger (which
185 # appends its own newline) doesn't get a stream chunk with a trailing \n.
186 7 my $line_buf = '';
187 10 ▶ my $progress_cb = sub {
Subroutine __ANON__: called CC=8 SCAR=23.4
188 10 my ($event) = @_;
189 10 ▶ if ($event->{errorDetail}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($event->{'errorDetail'}) { } 0 10 10
elsif (defined $event->{'stream'}) { } 6 4 10
elsif ($event->{'progress'}) { } 0 4 4
190 0 $self->logger_fatal->("Docker build error: " . $event->{errorDetail}{message});
191 }
192 elsif (defined $event->{stream}) {
193 6 for my $line ($self->_extract_build_lines(\$line_buf, $event->{stream}, $verbose)) {
194 5 $self->logger->($line);
195 }
196 }
197 elsif ($event->{progress}) {
198 0 ▶ $self->logger->($event->{status} . ' ' . $event->{progress}) if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
199 }
200 10 ▶ if ($event->{aux} && $event->{aux}{ID}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->{"aux"} and $event->{"aux"}{"ID"}) 4 6 10
Condition66%2/3
$event->{"aux"} and $event->{"aux"}{"ID"}
!ll&&!rl&&r
604
MC/DC50%1/2
$event->{"aux"} and $event->{"aux"}{"ID"}
$event->{"aux"} $event->{"aux"}{"ID"}
Truth table66%2/3
A and B
A= $event->{"aux"}B= $event->{"aux"}{"ID"}
ABresult
0X0
100
111
201 4 $image_id = $event->{aux}{ID};
202 }
203 7 };
204  
205 7 my $tarball;
206 7 ▶ if (ref($context) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $context eq 'HASH') { } 0 7 7
207 0 ▶ if ($context->{type} eq 'dir') {
Branch: 2 branches
BranchTrueFalseTotal
if ($context->{'type'} eq 'dir') { } 0 0 0
elsif ($context->{'type'} eq 'archive') { } 0 0 0
208 0 $tarball = $self->_create_tar($context->{path});
209 }
210 elsif ($context->{type} eq 'archive') {
211 0 $tarball = Path::Tiny::path($context->{path})->slurp_raw;
212 }
213 else {
214 0 ▶ $self->logger_fatal->("Unknown context type: " . ($context->{type} // 'undef'));
Condition0%0/2
$context->{"type"} // "undef"
l!l
00
MC/DC0%0/1
$context->{"type"} // "undef"
$context->{"type"}
Truth table0%0/2
A
A= $context->{"type"}
Aresult
11
00
215 }
216 }
217 else {
218 7 $tarball = $context;
219 }
220  
221 7 eval {
222 7 my $events = $docker->images->build(
223 context => $tarball,
224 %build_opts,
225 );
226  
227 4 ▶ for my $event (@{$events // []}) {
Condition50%1/2
$events // []
l!l
40
MC/DC0%0/1
$events // []
$events
Truth table50%1/2
A
A= $events
Aresult
11
00
228 4 $progress_cb->($event);
229 }
230 };
231  
232 7 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 3 4 7
233 # API::Docker 0.003 croaks on the errorDetail event before the loop
234 # above gets to run, so the output that led up to the failure rides
235 # on the exception instead. Drain it from there, or a failed build
236 # logs nothing but the last line (api-docker #12). The error event
237 # itself is left out: the reason arrives through the fatal below.
238 3 ▶ if (ref $err && $err->can('events')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $err and $err->can("events")) 2 1 3
Condition66%2/3
ref $err and $err->can("events")
!ll&&!rl&&r
102
MC/DC50%1/2
ref $err and $err->can("events")
ref $err $err->can("events")
Truth table66%2/3
A and B
A= ref $errB= $err->can("events")
ABresult
0X0
100
111
239 2 $progress_cb->($_) for grep { !$_->{errorDetail} } @{ $err->events };
240 }
241 3 $self->logger_fatal->("Docker build failed: $err");
242 }
243  
244 4 for my $tag (@tags) {
245 8 ▶ next if $tag eq ($tags[0] // '');
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq ($tags[0] // "") 4 4 8
Condition50%1/2
$tags[0] // ""
l!l
80
MC/DC0%0/1
$tags[0] // ""
$tags[0]
Truth table50%1/2
A
A= $tags[0]
Aresult
11
00
246 4 my ( $repo, $tag_name ) = $self->_split_image_ref($tag);
247 4 eval {
248 4 ▶ $docker->images->tag($image_id,
Branch: 1 branch
BranchTrueFalseTotal
defined $tag_name ? : 3 1 4
249 repo => $repo,
250 defined $tag_name ? ( tag => $tag_name ) : (),
251 );
252 };
253 4 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 3 4
254 1 $self->logger->("Warning: failed to tag image as $tag: $@");
255 1 next;
256 }
257 3 push @processed_tags, $tag;
258 }
259  
260 4 return Dist::Zilla::Plugin::Docker::API::Result->new(
261 image_id => $image_id,
262 tags => \@processed_tags,
263 pushed => [],
264 );
265 }
266  
267 11 ▶sub _extract_build_lines {
Subroutine _extract_build_lines: called CC=5 SCAR=16.1
268 11 my ($self, $buf_ref, $chunk, $verbose) = @_;
269 11 $$buf_ref .= $chunk;
270 11 my @out;
271 11 ▶ while ($$buf_ref =~ s/^([^\n]*)\n//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$buf_ref =~ s/^([^\n]*)\n//) 15 11 26
272 15 my $line = $1;
273 15 ▶ next unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 0 15 15
274 15 ▶ if ($verbose) {
Branch: 2 branches
BranchTrueFalseTotal
if ($verbose) { } 8 7 15
elsif ($self->_is_build_step_header($line)) { } 4 3 7
275 8 push @out, $line;
276 }
277 elsif ($self->_is_build_step_header($line)) {
278 4 push @out, $line;
279 }
280 }
281 11 return @out;
282 }
283  
284 29 ▶sub _is_build_step_header {
Subroutine _is_build_step_header: called CC=7 SCAR=19.5
285 29 my ($self, $line) = @_;
286 29 ▶ return 0 unless defined $line && length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line and length $line 1 28 29
Condition66%2/3
defined $line and length $line
!ll&&!rl&&r
0128
MC/DC50%1/2
defined $line and length $line
defined $line length $line
Truth table66%2/3
A and B
A= defined $lineB= length $line
ABresult
0X0
100
111
287 # Legacy builder: "Step 3/18 : RUN apt-get update"
288 28 ▶ return 1 if $line =~ m{^Step \s+ \d+/\d+ \s* :}x;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ m[^Step \s+ \d+/\d+ \s* :]x 6 22 28
289 # Podman classic builder: "STEP 3/18: RUN apt-get update"
290 22 ▶ return 1 if $line =~ m{^STEP \s+ \d+/\d+ \s* :}x;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ m[^STEP \s+ \d+/\d+ \s* :]x 2 20 22
291 # BuildKit stage header: "#5 [4/12] RUN apt-get update"
292 20 ▶ return 1 if $line =~ m{^\#\d+ \s+ \[\d+/\d+\]}x;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ m(^\#\d+ \s+ \[\d+/\d+\])x 1 19 20
293 # BuildKit named stage: "#7 [builder 3/8] COPY . ."
294 19 ▶ return 1 if $line =~ m{^\#\d+ \s+ \[[^\]]+ \s+ \d+/\d+\]}x;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ m(^\#\d+ \s+ \[[^\]]+ \s+ \d+/\d+\])x 2 17 19
295 17 return 0;
296 }
297  
298 # The whole directory as a tar. Which file in it is the Dockerfile is the
299 # engine's business: build_image passes that as its own dockerfile option.
300 1 ▶sub _create_tar {
Subroutine _create_tar: called CC=4 SCAR=13.9
301 1 my ($self, $dir) = @_;
302  
303 1 my $root = Path::Tiny::path($dir);
304 1 my $tar = Archive::Tar->new;
305  
306 1 for my $entry ($self->_collect_files($root, $root)) {
307 6 my $name = $entry->relative($root)->stringify;
308 6 my @stat = lstat $entry->stringify;
309 6 my %opt = ( mode => $stat[2] & 07777, mtime => $stat[9] );
310  
311 6 ▶ if (-l $entry->stringify) {
Branch: 2 branches
BranchTrueFalseTotal
if (-l $entry->stringify) { } 2 4 6
elsif (-d _) { } 1 3 4
312 2 $tar->add_data($name, '', {
313 %opt, type => Archive::Tar::Constant::SYMLINK(),
314 linkname => readlink($entry->stringify),
315 });
316 }
317 elsif (-d _) {
318 1 $tar->add_data($name, '', { %opt, type => Archive::Tar::Constant::DIR() });
319 }
320 else {
321 3 $tar->add_data($name, $entry->slurp_raw, \%opt);
322 }
323 }
324  
325 1 my $tarball;
326 1 open my $fh, '>', \$tarball;
327 1 $tar->write($fh, 1);
328 1 close $fh;
329  
330 1 return \$tarball;
331 }
332  
333 # Regular files, symlinks (never followed) and empty directories below $dir.
334 3 ▶sub _collect_files {
Subroutine _collect_files: called CC=5 SCAR=16.1
335 3 my ($self, $root, $dir) = @_;
336  
337 3 my @files;
338 3 for my $entry ($dir->children) {
339 7 ▶ if (-l $entry->stringify) {
Branch: 2 branches
BranchTrueFalseTotal
if (-l $entry->stringify) { } 2 5 7
elsif ($entry->is_dir) { } 2 3 5
340 2 push @files, $entry;
341 }
342 elsif ($entry->is_dir) {
343 2 my @below = $self->_collect_files($root, $entry);
344 2 ▶ push @files, @below ? @below : $entry;
Branch: 1 branch
BranchTrueFalseTotal
@below ? : 1 1 2
345 }
346 else {
347 3 push @files, $entry;
348 }
349 }
350 3 return @files;
351 }
352  
353 15 ▶sub auth_for_image_ref {
Subroutine auth_for_image_ref: called CC=3 SCAR=11.0
Pod undocumented
354 15 my ($self, $image_ref) = @_;
355 15 my $registry = $self->_registry_for_image_ref($image_ref);
356 # Walk the parsed auth files in order and run the per-file registry
357 # selection against each. The first file with a usable entry for this
358 # registry wins; a file that has none falls through to the next, so a
359 # docker config.json and a podman auth.json can each supply credentials
360 # for different registries.
361 15 for my $config (@{ $self->_auth_configs }) {
362 15 my $auth = $self->_auth_for_registry($registry, $config);
363 15 ▶ return $auth if defined $auth;
Branch: 1 branch
BranchTrueFalseTotal
if defined $auth 11 4 15
364 }
365 4 return undef;
366 }
367  
368 21 ▶sub _registry_for_image_ref {
Subroutine _registry_for_image_ref: called CC=4 SCAR=13.9
369 21 my ($self, $image_ref) = @_;
370  
371 # Strip ":tag" or "@sha256:..." suffix from the image part.
372 21 my $name = $image_ref;
373 21 $name =~ s/\@sha256:.*$//;
374 21 my @parts = split m{/}, $name;
375  
376 # If the first component does NOT look like a registry host
377 # (no dot, no colon, not "localhost"), it's an implicit Docker Hub repo.
378 21 ▶ if (@parts < 2 || ($parts[0] !~ /[.:]/ && $parts[0] ne 'localhost')) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts < 2 or not $parts[0] =~ /[.:]/ and $parts[0] ne "localhost") 4 17 21
Condition100%6/6
@parts < 2 or not $parts[0] =~ /[.:]/ and $parts[0] ne "localhost"
l!l&&r!l&&!r
1317
not $parts[0] =~ /[.:]/ and $parts[0] ne "localhost"
!ll&&!rl&&r
1613
MC/DC100%3/3
@parts < 2 or not $parts[0] =~ /[.:]/ and $parts[0] ne "localhost"
@parts < 2 not $parts[0] =~ /[.:]/ $parts[0] ne "localhost"
Truth table100%4/4
A or not(B) and C
A= @parts < 2B= not $parts[0] =~ /[.:]/C= $parts[0] ne "localhost"
ABCresult
1XX1
0111
00X0
0100
379 4 return 'https://index.docker.io/v1/';
380 }
381 17 return $parts[0];
382 }
383  
384 15 ▶sub _auth_for_registry {
Subroutine _auth_for_registry: called CC=16 SCAR=33.2
385 15 my ($self, $registry, $config) = @_;
386  
387 15 ▶ my $auths = $config->{auths} // {};
Condition50%1/2
$config->{"auths"} // {}
l!l
150
MC/DC0%0/1
$config->{"auths"} // {}
$config->{"auths"}
Truth table50%1/2
A
A= $config->{"auths"}
Aresult
11
00
388  
389 15 my @candidates = ($registry);
390 15 ▶ if ($registry eq 'https://index.docker.io/v1/'
Branch: 1 branch
BranchTrueFalseTotal
if ($registry eq "https://index.docker.io/v1/" or $registry eq "index.docker.io" or $registry eq "docker.io") 1 14 15
Condition66%4/6
$registry eq "https://index.docker.io/v1/" or $registry eq "index.docker.io" or $registry eq "docker.io"
l!l&&r!l&&!r
1014
$registry eq "https://index.docker.io/v1/" or $registry eq "index.docker.io"
l!l&&r!l&&!r
1014
MC/DC33%1/3
$registry eq "https://index.docker.io/v1/" or $registry eq "index.docker.io" or $registry eq "docker.io"
$registry eq "https://index.docker.io/v1/" $registry eq "index.docker.io" $registry eq "docker.io"
Truth table50%2/4
A or B or C
A= $registry eq "https://index.docker.io/v1/"B= $registry eq "index.docker.io"C= $registry eq "docker.io"
ABCresult
1XX1
01X1
0011
0000
391 || $registry eq 'index.docker.io'
392 || $registry eq 'docker.io') {
393 1 push @candidates,
394 'https://index.docker.io/v1/',
395 'https://index.docker.io/v2/',
396 'index.docker.io',
397 'docker.io';
398 }
399  
400 15 my $entry;
401 15 for my $key (@candidates) {
402 15 ▶ if (exists $auths->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $auths->{$key}) 11 4 15
403 11 $entry = $auths->{$key};
404 11 last;
405 }
406 }
407 15 ▶ return undef unless $entry;
Branch: 1 branch
BranchTrueFalseTotal
unless $entry 4 11 15
408  
409 11 my %auth = (serveraddress => $registry);
410  
411 11 ▶ if ($entry->{identitytoken}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->{"identitytoken"}) 1 10 11
412 1 $auth{identitytoken} = $entry->{identitytoken};
413 1 return \%auth;
414 }
415  
416 10 ▶ if ($entry->{auth}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->{"auth"}) 10 0 10
417 10 my $decoded = eval { decode_base64($entry->{auth}) };
418 10 ▶ if (defined $decoded && $decoded =~ /^([^:]+):(.*)$/s) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $decoded and $decoded =~ /^([^:]+):(.*)$/s) 10 0 10
Condition33%1/3
defined $decoded and $decoded =~ /^([^:]+):(.*)$/s
!ll&&!rl&&r
0010
MC/DC0%0/2
defined $decoded and $decoded =~ /^([^:]+):(.*)$/s
defined $decoded $decoded =~ /^([^:]+):(.*)$/s
Truth table33%1/3
A and B
A= defined $decodedB= $decoded =~ /^([^:]+):(.*)$/s
ABresult
0X0
100
111
419 10 $auth{username} = $1;
420 10 $auth{password} = $2;
421 10 return \%auth;
422 }
423 }
424  
425 0 ▶ if (defined $entry->{username} || defined $entry->{password}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $entry->{"username"} or defined $entry->{"password"}) 0 0 0
Condition0%0/3
defined $entry->{"username"} or defined $entry->{"password"}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $entry->{"username"} or defined $entry->{"password"}
defined $entry->{"username"} defined $entry->{"password"}
Truth table0%0/3
A or B
A= defined $entry->{"username"}B= defined $entry->{"password"}
ABresult
1X1
011
000
426 0 ▶ $auth{username} = $entry->{username} if defined $entry->{username};
Branch: 1 branch
BranchTrueFalseTotal
if defined $entry->{"username"} 0 0 0
427 0 ▶ $auth{password} = $entry->{password} if defined $entry->{password};
Branch: 1 branch
BranchTrueFalseTotal
if defined $entry->{"password"} 0 0 0
428 0 return \%auth;
429 }
430  
431 0 return undef;
432 }
433  
434 0 ▶sub tag_image {
Subroutine tag_image: not called CC=2 SCAR=17.9
Pod undocumented
435 0 my ($self, %arg) = @_;
436  
437 0 my $source = $arg{source};
438 0 my $target = $arg{target};
439  
440 0 my ( $repo, $tag ) = $self->_split_image_ref($target);
441  
442 0 ▶ $self->docker->images->tag(
Branch: 1 branch
BranchTrueFalseTotal
defined $tag ? : 0 0 0
443 $source,
444 repo => $repo,
445 defined $tag ? ( tag => $tag ) : (),
446 );
447 }
448  
449 3 ▶sub push_image {
Subroutine push_image: called CC=10 SCAR=23.6
Pod undocumented
450 3 my ($self, %arg) = @_;
451  
452 3 my $image_ref = $arg{image_ref};
453 3 ▶ my $auth = exists $arg{auth} ? $arg{auth} : $self->auth_for_image_ref($image_ref);
Branch: 1 branch
BranchTrueFalseTotal
exists $arg{'auth'} ? : 3 0 3
454  
455 3 my $events;
456 3 eval {
457 3 $events = $self->docker->images->push($image_ref, auth => $auth);
458 };
459  
460 3 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 2 1 3
461 # API::Docker 0.004 croaks on a push failure instead of handing back the
462 # errorDetail event in the returned array (against Docker an
463 # API::Docker::Error::Stream with ->events, on Podman it may be a plain
464 # HTTP error). Pull the message off the stream event when it is there,
465 # otherwise report the exception itself (api-docker #12).
466 2 my $message = "$err";
467 2 ▶ if (ref $err && $err->can('events')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $err and $err->can("events")) 1 1 2
Condition66%2/3
ref $err and $err->can("events")
!ll&&!rl&&r
101
MC/DC50%1/2
ref $err and $err->can("events")
ref $err $err->can("events")
Truth table66%2/3
A and B
A= ref $errB= $err->can("events")
ABresult
0X0
100
111
468 1 for my $event (@{ $err->events }) {
469 2 ▶ next unless $event->{errorDetail};
Branch: 1 branch
BranchTrueFalseTotal
unless $event->{"errorDetail"} 1 1 2
470 1 $message = $event->{errorDetail}{message};
471 1 last;
472 }
473 }
474 2 $self->logger_fatal->("Push error: " . $message);
475 }
476  
477 # A success still returns the raw array; keep scanning it, defensively, in
478 # case an engine reports a failure inside a 200 stream.
479 3 ▶ for my $event (@{$events // []}) {
Condition100%2/2
$events // []
l!l
12
MC/DC100%1/1
$events // []
$events
Truth table100%2/2
A
A= $events
Aresult
11
00
480 0 ▶ if ($event->{errorDetail}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->{"errorDetail"}) 0 0 0
481 0 $self->logger_fatal->("Push error: " . $event->{errorDetail}{message});
482 }
483 }
484 }
485  
486 0 ▶sub inspect_image {
Subroutine inspect_image: not called CC=1 SCAR=6.9
Pod undocumented
487 0 my ($self, $image_ref) = @_;
488  
489 0 return $self->docker->images->inspect($image_ref);
490 }
491  
492 # Ask the *registry* whether a reference is already published, through the
493 # engine's GET /distribution/{name}/json. Credentials are optional: without
494 # any, the lookup is anonymous, which is what a public image needs.
495 #
496 # API::Docker's exists() croaks instead of answering "no" when the engine has
497 # no /distribution route -- rootless Podman has none, measured -- and that
498 # croak is deliberately not caught here. "This engine cannot answer" is not
499 # the same as "the tag is free", and only the caller can decide what to do
500 # about the difference.
501 2 ▶sub remote_tag_exists {
Subroutine remote_tag_exists: called CC=3 SCAR=11.0
Pod undocumented
502 2 my ($self, $image_ref) = @_;
503  
504 2 my $auth = $self->auth_for_image_ref($image_ref);
505  
506 2 ▶ return $self->docker->distribution->exists(
Branch: 2 branches
BranchTrueFalseTotal
$self->docker->distribution->exists($image_ref, defined $auth ? ('auth', $auth) : ()) ? : 1 1 2
defined $auth ? : 1 1 2
507 $image_ref,
508 defined $auth ? ( auth => $auth ) : (),
509 ) ? 1 : 0;
510 }
511  
512 # Pre-flight for the release push: hand the engine, through POST /auth, the
513 # very credentials push_image would use for this reference and let the
514 # registry judge them. A rejected credential croaks; the caller is meant to
515 # make that fatal before anything is built.
516 #
517 # Returns undef when no credential could be resolved for the reference. That
518 # is not a failure -- an anonymous push to a public registry is a legal thing
519 # to do, and system->auth croaks on an empty AuthConfig, so there would be
520 # nothing to ask about.
521 2 ▶sub verify_auth_for_image_ref {
Subroutine verify_auth_for_image_ref: called CC=2 SCAR=6.9
Pod undocumented
522 2 my ($self, $image_ref) = @_;
523  
524 2 my $auth = $self->auth_for_image_ref($image_ref);
525 2 ▶ return undef unless $auth;
Branch: 1 branch
BranchTrueFalseTotal
unless $auth 1 1 2
526  
527 1 return $self->docker->system->auth(auth => $auth);
528 }
529  
530 9 1;
531  
532 __END__
↑ Summary API.pm »