Docker/API/Containers.pm

filter: Statementstmt 100% 254 / 254 Branchbran 76.7% 158 / 206 Conditioncond 73.9% 34 / 46 MC/DCmcdc 58.6% 17 / 29 Subroutinesub 100% 42 / 42 Podpod 100% 24 / 24 totaltotal 88.0% 529 / 601 CC 121 SCAR 21.1
CC
121
Cov
88%
logs
22.7
attach
15.1
remove
11.1
CRAP 8.2
« Docker/Role/HTTP.pm ↑ Summary Docker/Type.pm »
1 package API::Docker::API::Containers;
2 # ABSTRACT: Docker Engine Containers API
3 52 our $VERSION = '0.006';
4 52 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 52 with 'API::Docker::Role::Filters', 'API::Docker::Role::Using',
6 'API::Docker::Role::JSONBody';
7 52 ▶use API::Docker::Error::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 52 ▶use API::Docker::Role::Entity::Container;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 52 ▶use API::Docker::Type::ContainerInspectResponse;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 52 ▶use API::Docker::Type::ContainerSummary;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 52 ▶use Carp qw( croak shortmess );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 52 ▶use JSON::MaybeXS qw( decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 52 ▶use MIME::Base64 qw( decode_base64 );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 52 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 52 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 52 has client => (
19 is => 'ro',
20 required => 1,
21 weak_ref => 1,
22 );
23  
24  
25 # The class is the caller's argument rather than a constant of this module:
26 # `list` and `inspect` are two definitions in the swagger and therefore two
27 # generated classes. Both carry the same convenience methods, composed by
28 # API::Docker::Role::Entity::Container -- see "The two container shapes".
29 #
30 # from_data, not new: this is a daemon response, and the two entry points of
31 # API::Docker::Role::Type read it differently. from_data takes the swagger's
32 # wire names and nothing else, so a key it has not heard of keeps its own
33 # spelling instead of being read as the Perl name of one it has, and a value
34 # that disagrees with the swagger costs its own field rather than the whole
35 # response. `client` is ours rather than the engine's, so it goes beside the
36 # data instead of into it.
37 30 ▶sub _wrap {
Subroutine _wrap: called CC=1 SCAR=0.0
38 30 my ($self, $class, $data) = @_;
39 30 return $class->from_data($data, client => $self->client);
40 }
41  
42 13 ▶sub _wrap_list {
Subroutine _wrap_list: called CC=1 SCAR=0.0
43 13 my ($self, $class, $list) = @_;
44 13 return [ map { $self->_wrap($class, $_) } @$list ];
45 }
46  
47 # A state-change endpoint answers 204 when it changed something and 304 when
48 # the container was already in the state asked for. Neither carries a body, so
49 # the return value of the request is undef either way and the two are
50 # indistinguishable from it. The status comes out through the `response`
51 # out-parameter (see API::Docker::Role::HTTP/"Reading the status line and the
52 # response headers") and becomes the documented 1/0.
53 39 ▶sub _state_change {
Subroutine _state_change: called CC=3 SCAR=11.0
54 39 my ($self, $path, %opts) = @_;
55 39 my %response;
56 39 $self->client->post($path, undef, %opts, response => \%response);
57 39 ▶ return 0 if defined $response{status} && $response{status} == 304;
Branch: 1 branch
BranchTrueFalseTotal
if defined $response{"status"} and $response{"status"} == 304 7 32 39
Condition100%3/3
defined $response{"status"} and $response{"status"} == 304
!ll&&!rl&&r
15177
MC/DC100%2/2
defined $response{"status"} and $response{"status"} == 304
defined $response{"status"} $response{"status"} == 304
Truth table100%3/3
A and B
A= defined $response{"status"}B= $response{"status"} == 304
ABresult
0X0
100
111
58 32 return 1;
59 }
60  
61 18 ▶sub list {
Subroutine list: called CC=8 SCAR=22.1
Pod documented
62 18 my ($self, %opts) = @_;
63 18 my %params;
64 18 ▶ $params{all} = $opts{all} ? 1 : 0 if defined $opts{all};
Branch: 2 branches
BranchTrueFalseTotal
if defined $opts{"all"} 2 16 18
$opts{'all'} ? : 2 0 2
65 18 ▶ $params{limit} = $opts{limit} if defined $opts{limit};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"limit"} 0 18 18
66 18 ▶ $params{size} = $opts{size} ? 1 : 0 if defined $opts{size};
Branch: 2 branches
BranchTrueFalseTotal
if defined $opts{"size"} 0 18 18
$opts{'size'} ? : 0 0 0
67 $params{filters} = $self->_normalise_filters($opts{filters})
68 18 ▶ if defined $opts{filters};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"filters"} 2 16 18
69 my $result = $self->client->get('/containers/json',
70 params => \%params,
71 17 %{ $self->_request_options },
72 );
73 13 ▶ return $self->_wrap_list('API::Docker::Type::ContainerSummary', $result // []);
Condition50%1/2
$result // []
l!l
130
MC/DC0%0/1
$result // []
$result
Truth table50%1/2
A
A= $result
Aresult
11
00
74 }
75  
76  
77 # The booleans of the container create body, from spec/v1.51.yaml: the
78 # ContainerConfig flags at the top level, and the HostConfig flags in the
79 # nested `HostConfig` object (its own plus the ones it inherits from
80 # Resources). The engine rejects a number for any of them, so 1/0 is
81 # normalised to a JSON boolean on the way out; a caller may still pass 1/0 or a
82 # JSON boolean and it goes out correctly either way.
83 52 my @CONTAINER_CONFIG_BOOLS = qw(
84 ArgsEscaped AttachStderr AttachStdin AttachStdout NetworkDisabled
85 OpenStdin StdinOnce Tty
86 );
87 52 my @HOST_CONFIG_BOOLS = qw(
88 AutoRemove Init OomKillDisable Privileged PublishAllPorts ReadonlyRootfs
89 );
90  
91 2 ▶sub create {
Subroutine create: called CC=3 SCAR=11.0
Pod documented
92 2 my ($self, %config) = @_;
93 2 my %params;
94 2 ▶ $params{name} = delete $config{name} if defined $config{name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $config{"name"} 1 1 2
95 2 $self->_json_bools(\%config, @CONTAINER_CONFIG_BOOLS);
96 # Copy the nested HostConfig before touching it -- _json_bools mutates, and
97 # the sub-object is still the caller's until this copy replaces it.
98 2 ▶ if (ref $config{HostConfig} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $config{"HostConfig"} eq "HASH") 1 1 2
99 1 my %host_config = %{ $config{HostConfig} };
100 1 $self->_json_bools(\%host_config, @HOST_CONFIG_BOOLS);
101 1 $config{HostConfig} = \%host_config;
102 }
103 2 my $result = $self->client->post('/containers/create', \%config, params => \%params);
104 2 return $result;
105 }
106  
107  
108 23 ▶sub inspect {
Subroutine inspect: called CC=2 SCAR=6.9
Pod documented
109 23 my ($self, $id) = @_;
110 23 ▶ croak "Container ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 1 22 23
111 my $result = $self->client->get("/containers/$id/json",
112 22 %{ $self->_request_options },
113 );
114 21 return $self->_wrap('API::Docker::Type::ContainerInspectResponse', $result);
115 }
116  
117  
118 13 ▶sub start {
Subroutine start: called CC=2 SCAR=6.9
Pod documented
119 13 my ($self, $id) = @_;
120 13 ▶ croak "Container ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 1 12 13
121 return $self->_state_change("/containers/$id/start",
122 12 %{ $self->_request_options },
123 );
124 }
125  
126  
127 11 ▶sub stop {
Subroutine stop: called CC=4 SCAR=13.9
Pod documented
128 11 my ($self, $id, %opts) = @_;
129 11 ▶ croak "Container ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 1 10 11
130 10 my %params;
131 10 ▶ $params{t} = $opts{timeout} if defined $opts{timeout};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"timeout"} 2 8 10
132 10 ▶ $params{signal} = $opts{signal} if defined $opts{signal};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"signal"} 0 10 10
133 return $self->_state_change("/containers/$id/stop",
134 params => \%params,
135 10 %{ $self->_request_options },
136 );
137 }
138  
139  
140 5 ▶sub restart {
Subroutine restart: called CC=3 SCAR=11.2
Pod documented
141 5 my ($self, $id, %opts) = @_;
142 5 ▶ croak "Container ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 5 5
143 5 my %params;
144 5 ▶ $params{t} = $opts{timeout} if defined $opts{timeout};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"timeout"} 0 5 5
145 return $self->_state_change("/containers/$id/restart",
146 params => \%params,
147 5 %{ $self->_request_options },
148 );
149 }
150  
151  
152 5 ▶sub kill {
Subroutine kill: called CC=3 SCAR=11.0
Pod documented
153 5 my ($self, $id, %opts) = @_;
154 5 ▶ croak "Container ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 5 5
155 5 my %params;
156 5 ▶ $params{signal} = $opts{signal} if defined $opts{signal};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"signal"} 2 3 5
157 return $self->client->post("/containers/$id/kill", undef,
158 params => \%params,
159 5 %{ $self->_request_options },
160 );
161 }
162  
163  
164 5 ▶sub remove {
Subroutine remove: called CC=8 SCAR=24.0
Pod documented
165 5 my ($self, $id, %opts) = @_;
166 5 ▶ croak "Container ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 5 5
167 5 my %params;
168 5 ▶ $params{v} = $opts{volumes} ? 1 : 0 if defined $opts{volumes};
Branch: 2 branches
BranchTrueFalseTotal
if defined $opts{"volumes"} 0 5 5
$opts{'volumes'} ? : 0 0 0
169 5 ▶ $params{force} = $opts{force} ? 1 : 0 if defined $opts{force};
Branch: 2 branches
BranchTrueFalseTotal
if defined $opts{"force"} 1 4 5
$opts{'force'} ? : 1 0 1
170 5 ▶ $params{link} = $opts{link} ? 1 : 0 if defined $opts{link};
Branch: 2 branches
BranchTrueFalseTotal
if defined $opts{"link"} 0 5 5
$opts{'link'} ? : 0 0 0
171 return $self->client->delete_request("/containers/$id",
172 params => \%params,
173 5 %{ $self->_request_options },
174 );
175 }
176  
177  
178 14 ▶sub logs {
Subroutine logs: called CC=15 SCAR=31.2
Pod documented
179 14 my ($self, $id, %opts) = @_;
180 14 ▶ croak "Container ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 14 14
181 14 my %params;
182 14 ▶ $params{follow} = $opts{follow} ? 1 : 0 if defined $opts{follow};
Branch: 2 branches
BranchTrueFalseTotal
if defined $opts{"follow"} 3 11 14
$opts{'follow'} ? : 3 0 3
183 14 ▶ $params{stdout} = defined $opts{stdout} ? ($opts{stdout} ? 1 : 0) : 1;
Branch: 2 branches
BranchTrueFalseTotal
defined $opts{'stdout'} ? : 0 14 14
$opts{'stdout'} ? : 0 0 0
184 14 ▶ $params{stderr} = defined $opts{stderr} ? ($opts{stderr} ? 1 : 0) : 1;
Branch: 2 branches
BranchTrueFalseTotal
defined $opts{'stderr'} ? : 0 14 14
$opts{'stderr'} ? : 0 0 0
185 14 ▶ $params{since} = $opts{since} if defined $opts{since};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"since"} 0 14 14
186 14 ▶ $params{until} = $opts{until} if defined $opts{until};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"until"} 0 14 14
187 14 ▶ $params{timestamps} = $opts{timestamps} ? 1 : 0 if defined $opts{timestamps};
Branch: 2 branches
BranchTrueFalseTotal
if defined $opts{"timestamps"} 0 14 14
$opts{'timestamps'} ? : 0 0 0
188 14 ▶ $params{tail} = $opts{tail} if defined $opts{tail};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"tail"} 2 12 14
189 # exists, not truth: an unset callback is a caller bug, and quietly falling
190 # back to the buffered path for it would answer a follow with a hang.
191 return $self->client->stream_frames('GET', "/containers/$id/logs",
192 params => \%params,
193 defined $opts{tty} ? ( tty => $opts{tty} ) : (),
194 14 ▶ %{ $self->_request_options },
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'on_frame'} ? : 2 12 14
195 14 ▶ exists $opts{on_frame} ? ( on_frame => $opts{on_frame} ) : (),
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'tty'} ? : 1 13 14
196 );
197 }
198  
199  
200 # The guard behind attach's require_running, and a pre-flight check is all it
201 # is: it asks the engine what the container is doing now, and the container may
202 # still stop between that answer and the attach landing. That race is not
203 # closable from a client -- the engine offers no attach-if-running -- and the
204 # check earns its round trip anyway, because the condition it tests is exactly
205 # the condition that does the damage. Measured on Podman 5.4.2 (API 1.41) and
206 # Docker 29.7.2 (API 1.55), one container per row, each exiting with status 4:
207 #
208 # attach to an ALREADY-EXITED container Podman: status destroyed
209 # attach while RUNNING, exits under the call Podman: status intact (4)
210 # either of those Docker: status intact (4)
211 #
212 # So "running at the moment of the call" is the whole of the condition. A
213 # container that is still running when attach is sent stays safe even when it
214 # exits a millisecond later, which is why the pre-flight answer is worth having
215 # despite being one round trip stale.
216 #
217 # It fails open on anything it does not recognise: a State it cannot read is
218 # not evidence that the container is stopped, and a guard that is unsure must
219 # not be the thing that breaks a working call.
220 13 ▶sub _assert_container_running {
Subroutine _assert_container_running: called CC=6 SCAR=18.1
221 13 my ($self, $id) = @_;
222  
223 # A State the model could not use is one more shape the check does not
224 # recognise, and it arrives as one: the generated classes type their fields
225 # from the swagger, and a State that is not the object
226 # ContainerInspectResponse declares -- the bare status string of the list
227 # shape, say -- leaves ->state unset and keeps the raw value in
228 # unknown_fields rather than taking the response down with it. So there is
229 # nothing to catch here; an error that does reach this line, the daemon's
230 # own 404 included, is the caller's and goes up.
231 13 my $inspected = $self->inspect($id);
232  
233 # An API::Docker::Type::ContainerState, or undef where the daemon sent no
234 # State at all -- which is the "does not recognise" case above, not a stopped
235 # container.
236 13 my $state = $inspected->state;
237 13 ▶ return unless blessed($state) && defined $state->running;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($state) and defined $state->running 3 10 13
Condition100%3/3
builtin::blessed($state) and defined $state->running
!ll&&!rl&&r
2110
MC/DC100%2/2
builtin::blessed($state) and defined $state->running
builtin::blessed($state) defined $state->running
Truth table100%3/3
A and B
A= builtin::blessed($state)B= defined $state->running
ABresult
0X0
100
111
238 10 ▶ return if $state->running;
Branch: 1 branch
BranchTrueFalseTotal
if $state->running 9 1 10
239  
240 1 my $status = $state->status;
241 1 ▶ $status = 'not running' unless defined $status && length $status;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $status and length $status 0 1 1
Condition33%1/3
defined $status and length $status
!ll&&!rl&&r
001
MC/DC0%0/2
defined $status and length $status
defined $status length $status
Truth table33%1/3
A and B
A= defined $statusB= length $status
ABresult
0X0
100
111
242  
243 1 croak __PACKAGE__ . '->attach refused: container ' . $id . ' is ' . $status
244 . '. Attaching to a container that is not running destroys its exit status '
245 . 'on Podman, irrecoverably -- the engine keeps no copy -- and with '
246 . 'stream => 1 never returns on either engine. Read its output with logs() '
247 . 'instead, or pass require_running => 0 to attach anyway';
248 }
249  
250 25 ▶sub attach {
Subroutine attach: called CC=15 SCAR=27.1
Pod documented
251 25 my ($self, $id, %opts) = @_;
252 25 ▶ croak "Container ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 1 24 25
253  
254 # Pre-flight, and deliberately before the request is built: the call itself
255 # is what destroys the exit status, so a check made afterwards could only
256 # report the loss rather than prevent it. Turned off it costs nothing at all,
257 # not even the round trip.
258 my $require_running
259 24 ▶ = defined $opts{require_running} ? $opts{require_running} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'require_running'} ? : 11 13 24
260 # The pre-flight is a request the caller never wrote, and one that hangs is
261 # exactly what a bound was set to prevent -- so it carries the same one. It
262 # does that by itself here: the check runs on $self, which is the clone
263 # ->using returned when there was one (karr k74).
264 24 ▶ $self->_assert_container_running($id) if $require_running;
Branch: 1 branch
BranchTrueFalseTotal
if $require_running 13 11 24
265  
266 23 my %params;
267 23 ▶ $params{stream} = $opts{stream} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$opts{'stream'} ? : 1 22 23
268 23 ▶ $params{logs} = defined $opts{logs} ? ($opts{logs} ? 1 : 0) : 1;
Branch: 2 branches
BranchTrueFalseTotal
defined $opts{'logs'} ? : 2 21 23
$opts{'logs'} ? : 0 2 2
269 23 ▶ $params{stdout} = defined $opts{stdout} ? ($opts{stdout} ? 1 : 0) : 1;
Branch: 2 branches
BranchTrueFalseTotal
defined $opts{'stdout'} ? : 1 22 23
$opts{'stdout'} ? : 0 1 1
270 23 ▶ $params{stderr} = defined $opts{stderr} ? ($opts{stderr} ? 1 : 0) : 1;
Branch: 2 branches
BranchTrueFalseTotal
defined $opts{'stderr'} ? : 1 22 23
$opts{'stderr'} ? : 0 1 1
271 23 ▶ $params{stdin} = $opts{stdin} ? 1 : 0 if defined $opts{stdin};
Branch: 2 branches
BranchTrueFalseTotal
if defined $opts{"stdin"} 2 21 23
$opts{'stdin'} ? : 1 1 2
272 return $self->client->stream_frames('POST', "/containers/$id/attach",
273 params => \%params,
274 defined $opts{tty} ? ( tty => $opts{tty} ) : (),
275 23 ▶ %{ $self->_request_options },
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'on_frame'} ? : 1 22 23
276 23 ▶ exists $opts{on_frame} ? ( on_frame => $opts{on_frame} ) : (),
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'tty'} ? : 2 21 23
277 );
278 }
279  
280  
281 6 ▶sub top {
Subroutine top: called CC=3 SCAR=11.2
Pod documented
282 6 my ($self, $id, %opts) = @_;
283 6 ▶ croak "Container ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 6 6
284 6 my %params;
285 6 ▶ $params{ps_args} = $opts{ps_args} if defined $opts{ps_args};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"ps_args"} 0 6 6
286 return $self->client->get("/containers/$id/top",
287 params => \%params,
288 6 %{ $self->_request_options },
289 );
290 }
291  
292  
293 # The Podman compatibility path, and deliberately only that. Podman answers
294 # GET /containers/{id}/stats for a container that is not running with an error
295 # object inside a response it has already committed to 200 --
296 # {"cause":"container is stopped","message":"container is stopped",
297 # "response":500}, chunked, for the one-shot call and for stream => 1 alike
298 # (measured on Podman 5.4.2, API 1.41). Neither guard in the transport sees
299 # it: the >= 400 croak reads the status line, which says 200, and the stream
300 # check triggers on errorDetail, which this object does not carry. Docker
301 # 29.7.2 (API 1.55) answers the same call with a real, zero-filled reading and
302 # never produces this shape at all -- so this belongs here, next to the one
303 # endpoint and the one engine it was measured on, and not in Role::HTTP, where
304 # it would be a heuristic on daemon prose sitting under all twelve modules.
305 #
306 # All four clauses have to hold. The narrowness is the point, not an accident:
307 #
308 # 1. the status was 2xx. True by construction on both paths this guards:
309 # _request croaks before returning for >= 400, and
310 # _read_streaming_response reads such a body whole rather than handing it
311 # to a callback, so nothing that failed the status line reaches here
312 # 2. the decoded value is a HashRef
313 # 3. it carries all three of cause, message and response, exactly
314 # lower-cased. Never case-insensitively, and this is the counter-example
315 # that fixes it: POST /containers/{id}/wait answers its SUCCESS case with
316 # a top-level `Error` key -- Podman sends "Error":null on every wait --
317 # so a rule matching /error/i would turn every successful wait into a
318 # failure. Measured over fifteen read endpoints per engine and every
319 # fixture in t/fixtures: no 2xx body on either engine carries even one of
320 # these three lower-cased at the top level
321 # 4. `response` is a non-ref scalar reading as an integer >= 400. That is
322 # what makes the rule self-evidencing rather than a guess about prose:
323 # the object is an error because Podman says so inside it. Known miss:
324 # Podman's GET /plugins answers {"cause":"","message":"Path ... is not
325 # supported","response":0}, which clause 4 rejects -- but it arrives with
326 # 404 on the status line and the transport croaks it long before this
327 # runs, so the miss goes in the conservative direction and costs nothing
328 #
329 # A bare {message => ...} deliberately does not trigger: that is the ordinary
330 # Docker error body, and treating one inside a 2xx as a failure would be a
331 # guess about prose rather than a reading of what the engine said.
332 25 ▶sub _podman_error_object {
Subroutine _podman_error_object: called CC=9 SCAR=22.1
333 25 my ($self, $value) = @_;
334  
335 25 ▶ return unless ref $value eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "HASH" 0 25 25
336 return unless exists $value->{cause}
337 && exists $value->{message}
338 25 ▶ && exists $value->{response};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $value->{"cause"} and exists $value->{"message"} and exists $value->{"response"} 18 7 25
Condition83%5/6
exists $value->{"cause"} and exists $value->{"message"} and exists $value->{"response"}
!ll&&!rl&&r
1717
exists $value->{"cause"} and exists $value->{"message"}
!ll&&!rl&&r
1708
MC/DC66%2/3
exists $value->{"cause"} and exists $value->{"message"} and exists $value->{"response"}
exists $value->{"cause"} exists $value->{"message"} exists $value->{"response"}
Truth table75%3/4
A and B and C
A= exists $value->{"cause"}B= exists $value->{"message"}C= exists $value->{"response"}
ABCresult
0XX0
10X0
1100
1111
339  
340 7 my $response = $value->{response};
341 7 ▶ return if ref $response;
Branch: 1 branch
BranchTrueFalseTotal
if ref $response 1 6 7
342 6 ▶ return unless defined $response && $response =~ /\A[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $response and $response =~ /\A[0-9]+\z/ 1 5 6
Condition66%2/3
defined $response and $response =~ /\A[0-9]+\z/
!ll&&!rl&&r
015
MC/DC50%1/2
defined $response and $response =~ /\A[0-9]+\z/
defined $response $response =~ /\A[0-9]+\z/
Truth table66%2/3
A and B
A= defined $responseB= $response =~ /\A[0-9]+\z/
ABresult
0X0
100
111
343 5 ▶ return unless $response >= 400;
Branch: 1 branch
BranchTrueFalseTotal
unless $response >= 400 2 3 5
344  
345 3 return $value;
346 }
347  
348 # API::Docker::Error::HTTP rather than ::Stream: the one-shot call is not a
349 # stream at all, so "Docker API stream error" would be the wrong sentence for
350 # it and ->events would be a fabricated list. What the caller wants instead is
351 # exactly what this class carries -- ->status for the code Podman named, and
352 # ->data for the object, whose `cause` key that attribute's own POD already
353 # points at. Two of its attributes are left at their defaults on this path, on
354 # purpose: ->reason, because the status line's reason phrase was "OK" and
355 # putting that on a 500 would mislead, and ->body, because the bytes were
356 # decoded by the transport before this check ever saw them.
357 25 ▶sub _assert_no_podman_error {
Subroutine _assert_no_podman_error: called CC=6 SCAR=19.4
358 25 my ($self, $endpoint, $value) = @_;
359  
360 25 ▶ my $error = $self->_podman_error_object($value) or return $value;
Branch: 1 branch
BranchTrueFalseTotal
my $error = $self->_podman_error_object($value) or return $value 3 22 25
361  
362 3 my $reason = $error->{message};
363 3 ▶ $reason = $error->{cause} unless defined $reason && length $reason;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $reason and length $reason 0 3 3
Condition33%1/3
defined $reason and length $reason
!ll&&!rl&&r
003
MC/DC0%0/2
defined $reason and length $reason
defined $reason length $reason
Truth table33%1/3
A and B
A= defined $reasonB= length $reason
ABresult
0X0
100
111
364 3 ▶ $reason = 'no message given' unless defined $reason && length $reason;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $reason and length $reason 0 3 3
Condition33%1/3
defined $reason and length $reason
!ll&&!rl&&r
003
MC/DC0%0/2
defined $reason and length $reason
defined $reason length $reason
Truth table33%1/3
A and B
A= defined $reasonB= length $reason
ABresult
0X0
100
111
365 # Carp appends no location to a message that already ends in a newline.
366 3 $reason =~ s/\s+\z//;
367  
368 # The object goes into a variable first: `croak CLASS->new(...)` is indirect
369 # object syntax and parses as CLASS->croak(new(...)). Carp hands a reference
370 # straight back rather than decorating it, so the location is captured by
371 # hand, naming the frame a croak of a plain string would have named.
372 my $err = API::Docker::Error::HTTP->new(
373 message => 'Docker API error (' . $error->{response} . '): ' . $reason
374 . ' -- reported inside a 200 response to ' . $endpoint,
375 location => shortmess(''),
376 status => $error->{response},
377 3 data => $error,
378 );
379 3 croak $err;
380 }
381  
382 22 ▶sub stats {
Subroutine stats: called CC=10 SCAR=23.0
Pod documented
383 22 my ($self, $id, %opts) = @_;
384 22 ▶ croak "Container ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 22 22
385 22 ▶ my $stream = $opts{stream} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$opts{'stream'} ? : 5 17 22
386 22 my %params = ( stream => $stream );
387 # one-shot asks the engine not to wait for a second sampling cycle, which
388 # only means anything to a single reading. It is sent for the one-shot call
389 # alone, the way it always was, and never beside stream => 1.
390 22 ▶ $params{'one-shot'} = 1 unless $stream;
Branch: 1 branch
BranchTrueFalseTotal
unless $stream 17 5 22
391  
392 22 my $endpoint = 'GET /containers/' . $id . '/stats';
393  
394 # The guard has to sit on both sides of the callback split, because the same
395 # body arrives either way: buffered it is the return value, streamed it goes
396 # to the callback and is never returned at all. Wrapping puts the check in
397 # front of the caller's callback, so no caller is handed the error object as
398 # though it were a reading. Only a CodeRef is wrapped -- anything else is
399 # passed through untouched, so the transport still raises its own "on_event
400 # option must be a CodeRef" instead of this method dying on a closure it
401 # built around a non-callback.
402 22 my $on_event = $opts{on_event};
403 22 ▶ if (exists $opts{on_event} && ref $on_event eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"on_event"} and ref $on_event eq "CODE") 3 19 22
Condition66%2/3
exists $opts{"on_event"} and ref $on_event eq "CODE"
!ll&&!rl&&r
1903
MC/DC50%1/2
exists $opts{"on_event"} and ref $on_event eq "CODE"
exists $opts{"on_event"} ref $on_event eq "CODE"
Truth table66%2/3
A and B
A= exists $opts{"on_event"}B= ref $on_event eq "CODE"
ABresult
0X0
100
111
404 3 my $cb = $on_event;
405 6 ▶ $on_event = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
406 6 my ($reading, $stop) = @_;
407 6 $self->_assert_no_podman_error($endpoint, $reading);
408 5 return $cb->($reading, $stop);
409 3 };
410 }
411  
412 my $result = $self->client->get("/containers/$id/stats",
413 params => \%params,
414 22 ▶ %{ $self->_request_options },
Branch: 2 branches
BranchTrueFalseTotal
exists $opts{'on_event'} ? : 3 19 22
$stream ? : 2 17 19
415 22 exists $opts{on_event} ? ( on_event => $on_event )
416 : $stream ? ( ndjson => 1 )
417 : (),
418 );
419  
420 # A HashRef for the one-shot call and an ArrayRef of readings for
421 # stream => 1 without a callback -- both are the buffered body and both can
422 # be that error object. With a callback the return value is the summary
423 # HashRef, which carries none of the three keys and passes untouched.
424 $self->_assert_no_podman_error($endpoint, $_)
425 21 ▶ for ref $result eq 'ARRAY' ? @$result : ($result);
Branch: 1 branch
BranchTrueFalseTotal
ref $result eq 'ARRAY' ? : 5 16 21
426  
427 19 return $result;
428 }
429  
430  
431 8 ▶sub changes {
Subroutine changes: called CC=3 SCAR=11.0
Pod documented
432 8 my ($self, $id) = @_;
433 8 ▶ croak "Container ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 1 7 8
434 my $result = $self->client->get("/containers/$id/changes",
435 7 %{ $self->_request_options },
436 );
437 # A container with nothing changed answers with a JSON null, which the
438 # transport decodes to undef. Normalised here so the return is always
439 # something a caller can iterate.
440 7 ▶ return ref $result eq 'ARRAY' ? $result : [];
Branch: 1 branch
BranchTrueFalseTotal
ref $result eq 'ARRAY' ? : 2 5 7
441 }
442  
443  
444 7 ▶sub export {
Subroutine export: called CC=2 SCAR=6.9
Pod documented
445 7 my ($self, $id) = @_;
446 7 ▶ croak "Container ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 1 6 7
447 return $self->client->get("/containers/$id/export",
448 raw => 1,
449 6 %{ $self->_request_options },
450 );
451 }
452  
453  
454 7 ▶sub resize {
Subroutine resize: called CC=4 SCAR=13.9
Pod documented
455 7 my ($self, $id, %opts) = @_;
456 7 ▶ croak "Container ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 1 6 7
457 6 my %params;
458 6 ▶ $params{h} = $opts{h} if defined $opts{h};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"h"} 2 4 6
459 6 ▶ $params{w} = $opts{w} if defined $opts{w};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"w"} 3 3 6
460 return $self->client->post("/containers/$id/resize", undef,
461 params => \%params,
462 6 %{ $self->_request_options },
463 );
464 }
465  
466  
467 5 ▶sub wait {
Subroutine wait: called CC=3 SCAR=11.2
Pod documented
468 5 my ($self, $id, %opts) = @_;
469 5 ▶ croak "Container ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 5 5
470 5 my %params;
471 5 ▶ $params{condition} = $opts{condition} if defined $opts{condition};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"condition"} 0 5 5
472 return $self->client->post("/containers/$id/wait", undef,
473 params => \%params,
474 5 %{ $self->_request_options },
475 );
476 }
477  
478  
479 6 ▶sub pause {
Subroutine pause: called CC=2 SCAR=7.0
Pod documented
480 6 my ($self, $id) = @_;
481 6 ▶ croak "Container ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 6 6
482 return $self->_state_change("/containers/$id/pause",
483 6 %{ $self->_request_options },
484 );
485 }
486  
487  
488 6 ▶sub unpause {
Subroutine unpause: called CC=2 SCAR=7.0
Pod documented
489 6 my ($self, $id) = @_;
490 6 ▶ croak "Container ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 6 6
491 return $self->_state_change("/containers/$id/unpause",
492 6 %{ $self->_request_options },
493 );
494 }
495  
496  
497 4 ▶sub rename {
Subroutine rename: called CC=3 SCAR=11.3
Pod documented
498 4 my ($self, $id, $name) = @_;
499 4 ▶ croak "Container ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 4 4
500 4 ▶ croak "New name required" unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 4 4
501 return $self->client->post("/containers/$id/rename", undef,
502 params => { name => $name },
503 4 %{ $self->_request_options },
504 );
505 }
506  
507  
508 # The update body is Resources + RestartPolicy; the booleans are the two
509 # Resources flags. Normalised on the way out, as for create.
510 52 my @UPDATE_BOOLS = qw( Init OomKillDisable );
511  
512 1 ▶sub update {
Subroutine update: called CC=2 SCAR=7.0
Pod documented
513 1 my ($self, $id, %config) = @_;
514 1 ▶ croak "Container ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 1 1
515 1 $self->_json_bools(\%config, @UPDATE_BOOLS);
516 1 return $self->client->post("/containers/$id/update", \%config);
517 }
518  
519  
520 # The engine reports what a path is in a response header rather than a body,
521 # so both GET and HEAD carry it and only HEAD has nothing else to say. The
522 # header is base64-encoded JSON; handing the caller the base64 would make
523 # every one of them write this.
524 11 ▶sub _decode_path_stat {
Subroutine _decode_path_stat: called CC=4 SCAR=13.9
525 11 my ($self, $response) = @_;
526  
527 11 my $header = $response->{headers}{'x-docker-container-path-stat'};
528 11 ▶ return undef unless defined $header && length $header;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $header and length $header 6 5 11
Condition100%3/3
defined $header and length $header
!ll&&!rl&&r
515
MC/DC100%2/2
defined $header and length $header
defined $header length $header
Truth table100%3/3
A and B
A= defined $headerB= length $header
ABresult
0X0
100
111
529  
530 # Docker encodes this one with Go's base64.StdEncoding -- unlike
531 # X-Registry-Auth, which is URLEncoding. Decoded tolerantly rather than
532 # strictly: translating the two URL-safe characters first costs nothing and
533 # means an engine that reached for the other alphabet is still read.
534 5 $header =~ tr{-_}{+/};
535 5 my $stat = eval { decode_json(decode_base64($header)) };
536 5 ▶ croak "Cannot decode X-Docker-Container-Path-Stat header: $@"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $stat eq "HASH" 1 4 5
537 unless ref $stat eq 'HASH';
538  
539 4 return $stat;
540 }
541  
542 13 ▶sub get_archive {
Subroutine get_archive: called CC=8 SCAR=20.8
Pod documented
543 13 my ($self, $id, %opts) = @_;
544 13 ▶ croak "Container ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 1 12 13
545 12 ▶ croak "Path required" unless defined $opts{path} && length $opts{path};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"path"} and length $opts{"path"} 2 10 12
Condition100%3/3
defined $opts{"path"} and length $opts{"path"}
!ll&&!rl&&r
1110
MC/DC100%2/2
defined $opts{"path"} and length $opts{"path"}
defined $opts{"path"} length $opts{"path"}
Truth table100%3/3
A and B
A= defined $opts{"path"}B= length $opts{"path"}
ABresult
0X0
100
111
546 croak "The stat option must be a HashRef"
547 10 ▶ if exists $opts{stat} && ref $opts{stat} ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"stat"} and ref $opts{"stat"} ne "HASH" 1 9 10
Condition100%3/3
exists $opts{"stat"} and ref $opts{"stat"} ne "HASH"
!ll&&!rl&&r
721
MC/DC100%2/2
exists $opts{"stat"} and ref $opts{"stat"} ne "HASH"
exists $opts{"stat"} ref $opts{"stat"} ne "HASH"
Truth table100%3/3
A and B
A= exists $opts{"stat"}B= ref $opts{"stat"} ne "HASH"
ABresult
0X0
100
111
548  
549 9 my %response;
550 my $tar = $self->client->get("/containers/$id/archive",
551 params => { path => $opts{path} },
552 raw => 1,
553 response => \%response,
554 9 %{ $self->_request_options },
555 );
556  
557 9 ▶ if (my $out = $opts{stat}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $out = $opts{"stat"}) 2 7 9
558 2 ▶ %$out = %{ $self->_decode_path_stat(\%response) // {} };
Condition100%2/2
$self->_decode_path_stat(\%response) // {}
l!l
11
MC/DC100%1/1
$self->_decode_path_stat(\%response) // {}
$self->_decode_path_stat(\%response)
Truth table100%2/2
A
A= $self->_decode_path_stat(\%response)
Aresult
11
00
559 }
560  
561 9 return $tar;
562 }
563  
564  
565 12 ▶sub put_archive {
Subroutine put_archive: called CC=10 SCAR=23.0
Pod documented
566 12 my ($self, $id, $tar, %opts) = @_;
567 12 ▶ croak "Container ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 1 11 12
568 11 ▶ croak "Path required" unless defined $opts{path} && length $opts{path};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"path"} and length $opts{"path"} 1 10 11
Condition66%2/3
defined $opts{"path"} and length $opts{"path"}
!ll&&!rl&&r
1010
MC/DC50%1/2
defined $opts{"path"} and length $opts{"path"}
defined $opts{"path"} length $opts{"path"}
Truth table66%2/3
A and B
A= defined $opts{"path"}B= length $opts{"path"}
ABresult
0X0
100
111
569 10 ▶ croak "Tar archive required (raw bytes or a scalar ref)" unless defined $tar;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tar 1 9 10
570  
571 9 my %params = ( path => $opts{path} );
572 $params{noOverwriteDirNonDir} = $opts{noOverwriteDirNonDir} ? 1 : 0
573 9 ▶ if defined $opts{noOverwriteDirNonDir};
Branch: 2 branches
BranchTrueFalseTotal
if defined $opts{"noOverwriteDirNonDir"} 2 7 9
$opts{'noOverwriteDirNonDir'} ? : 1 1 2
574 $params{copyUIDGID} = $opts{copyUIDGID} ? 1 : 0
575 9 ▶ if defined $opts{copyUIDGID};
Branch: 2 branches
BranchTrueFalseTotal
if defined $opts{"copyUIDGID"} 2 7 9
$opts{'copyUIDGID'} ? : 1 1 2
576  
577 9 ▶ my $raw = ref $tar eq 'SCALAR' ? $$tar : $tar;
Branch: 1 branch
BranchTrueFalseTotal
ref $tar eq 'SCALAR' ? : 1 8 9
578  
579 return $self->client->put("/containers/$id/archive", undef,
580 params => \%params,
581 raw_body => $raw,
582 content_type => 'application/x-tar',
583 9 %{ $self->_request_options },
584 );
585 }
586  
587  
588 11 ▶sub stat_archive {
Subroutine stat_archive: called CC=4 SCAR=14.0
Pod documented
589 11 my ($self, $id, %opts) = @_;
590 11 ▶ croak "Container ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 11 11
591 11 ▶ croak "Path required" unless defined $opts{path} && length $opts{path};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"path"} and length $opts{"path"} 1 10 11
Condition66%2/3
defined $opts{"path"} and length $opts{"path"}
!ll&&!rl&&r
1010
MC/DC50%1/2
defined $opts{"path"} and length $opts{"path"}
defined $opts{"path"} length $opts{"path"}
Truth table66%2/3
A and B
A= defined $opts{"path"}B= length $opts{"path"}
ABresult
0X0
100
111
592  
593 10 my %response;
594 $self->client->head("/containers/$id/archive",
595 params => { path => $opts{path} },
596 response => \%response,
597 10 %{ $self->_request_options },
598 );
599  
600 9 return $self->_decode_path_stat(\%response);
601 }
602  
603  
604 5 ▶sub prune {
Subroutine prune: called CC=2 SCAR=6.9
Pod documented
605 5 my ($self, %opts) = @_;
606 5 my %params;
607 $params{filters} = $self->_normalise_filters($opts{filters})
608 5 ▶ if defined $opts{filters};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"filters"} 2 3 5
609 return $self->client->post('/containers/prune', undef,
610 params => \%params,
611 4 %{ $self->_request_options },
612 );
613 }
614  
615  
616  
617 52 1;
618  
619 __END__
« Docker/Role/HTTP.pm ↑ Summary Docker/Type.pm »