blib/lib/Test/PostgreSQL/Docker.pm

filter: Statementstmt 85.7% 12 / 14 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 89.4% 17 / 19 CC 1 SCAR 0.6
CC
1
Cov
86%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Test::PostgreSQL::Docker;
2 6 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 0 ▶use DBD::Pg;
Subroutine BEGIN: called CC=1 SCAR=2.6
7 use Sub::Retry qw/retry/;
8 use Net::EmptyPort qw/empty_port/;
9 use IPC::Run ();
10  
11 our $VERSION = "0.03003";
12 our $DEBUG;
13  
14 sub new {
15 my ($class, %opts) = @_;
16 my $self = bless {
17 docker => undef,
18 pgname => "postgres",
19 tag => 'latest',
20 port => empty_port(),
21 host => "127.0.0.1",
22 dbowner => "postgres",
23 password=> "postgres",
24 dbname => "test",
25 print_docker_error => 1,
26 _orig_address => '',
27 _pid => $$,
28 %opts,
29 }, $class;
30 $self->{docker} = $self->_search_docker_path() unless $self->docker;
31 $self->oid;
32 return $self;
33 }
34  
35 sub _search_docker_path {
36 my $self = shift;
37 my @paths = qw( /usr/bin /usr/local/bin );
38 my $decided;
39 for my $path ( @paths ) {
40 if ( -x "$path/docker" ) {
41 $decided = "$path/docker";
42 last;
43 }
44 }
45 return $decided;
46 }
47  
48 sub _address {
49 my $self = shift;
50 ("$self" =~ /HASH\(0x([0-9a-f]+)\)/)[0];
51 }
52  
53 sub oid {
54 my ( $self ) = @_;
55 return $self->{oid} if defined $self->{oid};
56 $self->{oid} = $self->_address;
57 }
58  
59 sub pull {
60 my ($self) = @_;
61 my $image = $self->image_name();
62 $self->docker_cmd(pull => ['--quiet', $image]);
63 $self;
64 }
65  
66 sub run {
67 my ($self, %opt) = @_;
68  
69 return $self unless $self->docker_daemon_is_accessible;
70 return $self if $self->docker_is_running;
71  
72 $self->pull() unless (exists $opt{skip_pull} ? $opt{skip_pull} : 1);
73  
74 my $image = $self->image_name();
75 my $ctname = $self->container_name();
76 my $class = ref($self);
77  
78 my $host = $self->{host};
79 my $user = $self->{dbowner};
80 my $pass = $self->{password};
81 my $port = $self->{port};
82 my $dbname = $self->{dbname};
83 my @envs = map { ('-e', $_) } "POSTGRES_USER=$user", "POSTGRES_PASSWORD=$pass", "POSTGRES_DB=$dbname";
84 my ( $out, $err ) = $self->docker_cmd(run => ['--rm', '--name', $ctname, '-p', "$host:$port:5432", @envs, '-d', "$image"]);
85  
86 if ( !$err or $err =~ /Status: Downloaded newer image for/) { # for auto pulling
87 $self->{docker_is_running} = 1;
88 $self->{_orig_address} = $self->_address;
89 $self->dbh unless $opt{skip_connect};
90 }
91  
92 $self;
93 }
94  
95 sub DESTROY {
96 my ( $self ) = @_;
97 $self->{dbh}->disconnect() if defined $self->{dbh};
98 return if $self->_address ne $self->{_orig_address};
99 return if $$ != $self->{_pid};
100 $self->docker_cmd(kill => [$self->container_name]) if $self->docker_is_running;
101 }
102  
103 sub docker {
104 shift->{docker};
105 }
106  
107 sub docker_is_running {
108 shift->{docker_is_running};
109 }
110  
111 sub docker_daemon_is_accessible {
112 my ( $self ) = @_;
113 return unless -x $self->docker;
114 my ( $out, $err ) = $self->docker_cmd('ps');
115 $err ? 0 : 1;
116 }
117  
118 sub docker_cmd {
119 my ( $self, $action, $args, $io ) = @_;
120 $args //= [];
121 my @cmds = ($self->docker, $action, @$args);
122 $DEBUG && print STDERR join(' ', @cmds),"\n";
123  
124 my ( $in, $out, $err );
125 if ( $io ) {
126 IPC::Run::run( \@cmds, '<', ($io->{in} ? $io->{in} : \$in), '>', ($io->{out} ? $io->{out} : \$out), '2>', ($io->{err} ? $io->{err} : \$err) );
127 }
128 else {
129 IPC::Run::run \@cmds, \$in, \$out, \$err;
130 }
131  
132 $self->{print_docker_error} && $err && print STDERR $err, "\n";
133  
134 return ($out, $err);
135 }
136  
137 sub psql_args {
138 my $self = shift;
139 if ( @_ ) {
140 $self->{psql_args} = $_[0];
141 }
142 $self->{psql_args}
143 ||= ['-h', $self->{host}, '-p', 5432, '-U', $self->{dbowner}, '-d', $self->{dbname}];
144 }
145  
146 sub run_psql {
147 my ($self, @args) = @_;
148 my %io;
149 if ( ref($args[-1]) eq 'HASH' ) {
150 %io = %{ (pop @args) };
151 }
152 $self->dbh(); ## waiting for DB connection
153 $self->docker_cmd( exec => ['-i', $self->container_name, 'psql', @{$self->psql_args}, @args], \%io );
154 }
155  
156 sub run_psql_scripts {
157 my ($self, @scripts) = @_;
158 for my $script ( @scripts ) {
159 $self->run_psql({ in => $script });
160 }
161 $self;
162 }
163  
164 sub dsn {
165 my ($self, %args) = @_;
166 $args{port} ||= $self->{port};
167 $args{host} ||= $self->{host};
168 $args{user} ||= $self->{dbowner};
169 $args{dbname} ||= $self->{dbname};
170 $args{password} ||= $self->{password};
171 return 'DBI:Pg:' . join(';', map { "$_=$args{$_}" } sort keys %args);
172 }
173  
174 sub dbh {
175 my ($self, $option) = @_;
176 return $self->{dbh} if $self->{dbh};
177  
178 $option ||= {AutoCommit => 0, RaiseError => 1, PrintError => 0};
179 $self->{dbh} = retry 5, 2, sub {
180 DBI->connect($self->dsn, '', '', $option);
181 };
182 $self->{dbh};
183 }
184  
185 sub port {
186 shift->{port};
187 }
188  
189 sub container_name {
190 my ($self) = @_;
191 my $pgname = $self->{pgname};
192 $pgname =~ s/[^a-zA-Z0-9_.-]/__/g;
193 sprintf "%s-%s-%s", $pgname, $self->{tag}, $self->oid;
194 }
195  
196 sub image_name {
197 my ($self) = @_;
198 sprintf "%s:%s", $self->{pgname}, $self->{tag};
199 }
200  
201 1;
202 __END__
↑ Summary