line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::Docker::Image; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
120660
|
use strict; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
96
|
|
4
|
3
|
|
|
3
|
|
15
|
use warnings; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
104
|
|
5
|
|
|
|
|
|
|
|
6
|
3
|
|
|
3
|
|
15
|
use constant DEBUG => $ENV{DEBUG_TEST_DOCKER_IMAGE}; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
206
|
|
7
|
|
|
|
|
|
|
|
8
|
3
|
|
|
3
|
|
8423
|
use IPC::Run3; |
|
3
|
|
|
|
|
126235
|
|
|
3
|
|
|
|
|
222
|
|
9
|
3
|
|
|
3
|
|
3138
|
use Time::HiRes 'sleep'; |
|
3
|
|
|
|
|
5159
|
|
|
3
|
|
|
|
|
13
|
|
10
|
3
|
|
|
3
|
|
3020
|
use Data::Util ':check'; |
|
3
|
|
|
|
|
2437
|
|
|
3
|
|
|
|
|
586
|
|
11
|
3
|
|
|
3
|
|
1465
|
use Class::Load qw/try_load_class/; |
|
3
|
|
|
|
|
72640
|
|
|
3
|
|
|
|
|
205
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
use Class::Accessor::Lite ( |
14
|
3
|
|
|
|
|
29
|
ro => [qw/tag container_ports container_id/], |
15
|
3
|
|
|
3
|
|
3061
|
); |
|
3
|
|
|
|
|
3595
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
our $VERSION = "0.03"; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub WARN { |
20
|
0
|
|
|
0
|
0
|
0
|
my $msg = join " ", @_; |
21
|
0
|
|
|
|
|
0
|
chomp $msg; |
22
|
0
|
|
|
|
|
0
|
warn sprintf "[%s %.5f] %s\n", __PACKAGE__, Time::HiRes::time, $msg; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub new { |
26
|
6
|
|
|
6
|
1
|
29184
|
my $class = shift; |
27
|
6
|
50
|
|
|
|
46
|
my %args = @_ == 1 ? %{$_[0]} : @_; |
|
0
|
|
|
|
|
0
|
|
28
|
|
|
|
|
|
|
|
29
|
6
|
|
100
|
|
|
36
|
my $boot = delete $args{boot} || 'Test::Docker::Image::Boot'; |
30
|
|
|
|
|
|
|
|
31
|
6
|
100
|
|
|
|
27
|
try_load_class( $boot ) or die "failed to load $boot"; |
32
|
|
|
|
|
|
|
|
33
|
5
|
|
|
|
|
288
|
my $image_tag = delete $args{tag}; |
34
|
5
|
100
|
|
|
|
37
|
die "tag argument is required" unless $image_tag; |
35
|
|
|
|
|
|
|
|
36
|
4
|
100
|
|
|
|
60
|
die "container_ports argument must be ArrayRef" |
37
|
|
|
|
|
|
|
unless is_array_ref $args{container_ports}; |
38
|
|
|
|
|
|
|
|
39
|
2
|
|
|
|
|
5
|
my @ports = map { ('-p', $_) } @{ $args{container_ports} }; |
|
4
|
|
|
|
|
17
|
|
|
2
|
|
|
|
|
6
|
|
40
|
2
|
|
|
|
|
11
|
my $container_id = _run(qw/docker run -d -t/, @ports, $image_tag); |
41
|
|
|
|
|
|
|
# wait to launch the container |
42
|
2
|
|
50
|
|
|
1001451
|
sleep $args{sleep_sec} || 0.5; |
43
|
|
|
|
|
|
|
|
44
|
2
|
|
|
|
|
47
|
my $self = bless { |
45
|
|
|
|
|
|
|
tag => $image_tag, |
46
|
|
|
|
|
|
|
container_ports => $args{container_ports}, |
47
|
|
|
|
|
|
|
container_id => $container_id, |
48
|
|
|
|
|
|
|
boot => $boot->new, |
49
|
|
|
|
|
|
|
}, $class; |
50
|
|
|
|
|
|
|
|
51
|
2
|
|
|
|
|
19
|
return $self; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub port { |
55
|
1
|
|
|
1
|
1
|
9
|
my ($self, $container_port) = @_; |
56
|
1
|
|
|
|
|
9
|
my $port_info = _run(qw/docker port/, $self->container_id, $container_port); |
57
|
1
|
|
|
|
|
1082
|
my (undef, $port) = split ':', $port_info; |
58
|
1
|
|
|
|
|
10
|
return $port; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub host { |
62
|
0
|
|
|
0
|
1
|
0
|
$_[0]->{boot}->host; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub _run { |
66
|
2
|
|
|
2
|
|
3165
|
my (@cmd) = @_; |
67
|
|
|
|
|
|
|
|
68
|
2
|
|
|
|
|
5
|
DEBUG && WARN sprintf "Run [ %s ]", join ' ', @cmd; |
69
|
2
|
|
|
|
|
38
|
my $is_success = run3 [ @cmd ], \my $in, \my $out, \my $err; |
70
|
1
|
50
|
|
|
|
10197
|
if ($is_success) { |
71
|
1
|
|
|
|
|
11
|
chomp $out; |
72
|
1
|
|
|
|
|
76
|
return $out; |
73
|
|
|
|
|
|
|
} else { |
74
|
0
|
|
|
|
|
0
|
die $err; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub DESTROY { |
79
|
2
|
|
|
2
|
|
2990
|
my $self = shift; |
80
|
2
|
|
|
|
|
7
|
for my $subcommand ( qw/kill rm/ ) { |
81
|
4
|
|
|
|
|
17168
|
_run('docker', $subcommand, $self->container_id); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
1; |
86
|
|
|
|
|
|
|
__END__ |