File Coverage

blib/lib/Testcontainers/Module/MySQL.pm
Criterion Covered Total %
statement 30 51 58.8
branch 0 6 0.0
condition 0 14 0.0
subroutine 10 14 71.4
pod 0 4 0.0
total 40 89 44.9


line stmt bran cond sub pod time code
1             package Testcontainers::Module::MySQL;
2             # ABSTRACT: MySQL container module for Testcontainers
3              
4 1     1   1351 use strict;
  1         2  
  1         33  
5 1     1   4 use warnings;
  1         1  
  1         42  
6 1     1   4 use Carp qw( croak );
  1         1  
  1         43  
7 1     1   3 use Testcontainers;
  1         2  
  1         57  
8 1     1   7 use Testcontainers::Wait;
  1         2  
  1         63  
9              
10             our $VERSION = '0.001';
11              
12 1     1   6 use Exporter 'import';
  1         2  
  1         82  
13             our @EXPORT_OK = qw( mysql_container );
14              
15             use constant {
16 1         260 DEFAULT_IMAGE => 'mysql:8.0',
17             DEFAULT_PORT => '3306/tcp',
18             DEFAULT_ROOT_PASSWORD => 'test',
19             DEFAULT_USER => 'test',
20             DEFAULT_PASSWORD => 'test',
21             DEFAULT_DB => 'testdb',
22 1     1   5 };
  1         1  
23              
24             =head1 SYNOPSIS
25              
26             use Testcontainers::Module::MySQL qw( mysql_container );
27              
28             my $mysql = mysql_container();
29              
30             my $host = $mysql->host;
31             my $port = $mysql->mapped_port('3306/tcp');
32             my $dsn = $mysql->dsn;
33              
34             $mysql->terminate;
35              
36             =head1 DESCRIPTION
37              
38             Pre-configured MySQL container module, equivalent to Go's
39             C.
40              
41             =cut
42              
43             sub mysql_container {
44 0     0 0   my (%opts) = @_;
45              
46 0   0       my $image = $opts{image} // DEFAULT_IMAGE;
47 0   0       my $root_password = $opts{root_password} // DEFAULT_ROOT_PASSWORD;
48 0   0       my $username = $opts{username} // DEFAULT_USER;
49 0   0       my $password = $opts{password} // DEFAULT_PASSWORD;
50 0   0       my $database = $opts{database} // DEFAULT_DB;
51 0   0       my $port = $opts{port} // DEFAULT_PORT;
52              
53             my $container = Testcontainers::run($image,
54             exposed_ports => [$port],
55             env => {
56             MYSQL_ROOT_PASSWORD => $root_password,
57             MYSQL_USER => $username,
58             MYSQL_PASSWORD => $password,
59             MYSQL_DATABASE => $database,
60             },
61             _internal_labels => {
62             'org.testcontainers.module' => 'mysql',
63             },
64             wait_for => Testcontainers::Wait::for_log(
65             'port: 3306 MySQL Community Server',
66             ),
67             startup_timeout => $opts{startup_timeout} // 120,
68 0 0 0       ($opts{name} ? (name => $opts{name}) : ()),
69             );
70              
71 0           return Testcontainers::Module::MySQL::Container->new(
72             _inner => $container,
73             username => $username,
74             password => $password,
75             root_password => $root_password,
76             database => $database,
77             port => $port,
78             );
79             }
80              
81             =func mysql_container(%opts)
82              
83             Create and start a MySQL container.
84              
85             Options: C, C, C, C, C,
86             C, C, C.
87              
88             =cut
89              
90              
91             package Testcontainers::Module::MySQL::Container;
92              
93 1     1   4 use strict;
  1         1  
  1         26  
94 1     1   3 use warnings;
  1         1  
  1         43  
95 1     1   3 use Moo;
  1         7  
  1         27  
96              
97             has _inner => (is => 'ro', required => 1, handles => [qw(
98             id image host mapped_port mapped_port_info endpoint container_id
99             name state is_running logs exec stop start terminate refresh
100             )]);
101             has username => (is => 'ro', required => 1);
102             has password => (is => 'ro', required => 1);
103             has root_password => (is => 'ro', required => 1);
104             has database => (is => 'ro', required => 1);
105             has port => (is => 'ro', required => 1);
106              
107             sub connection_string {
108 0     0 0   my ($self) = @_;
109 0           my $host = $self->host;
110 0           my $mapped = $self->mapped_port($self->port);
111 0           return sprintf("mysql://%s:%s\@%s:%s/%s",
112             $self->username, $self->password, $host, $mapped, $self->database);
113             }
114              
115             sub dsn {
116 0     0 0   my ($self) = @_;
117 0           my $host = $self->host;
118 0           my $mapped = $self->mapped_port($self->port);
119 0           return sprintf("dbi:mysql:database=%s;host=%s;port=%s",
120             $self->database, $host, $mapped);
121             }
122              
123             =method dsn
124              
125             Returns a DBI-compatible DSN: C
126              
127             =cut
128              
129             sub DEMOLISH {
130 0     0 0   my ($self, $in_global) = @_;
131 0 0         return if $in_global;
132 0 0         $self->_inner->terminate if $self->_inner;
133 0           return;
134             }
135              
136             1;