File Coverage

blib/lib/Testcontainers/Module/Redis.pm
Criterion Covered Total %
statement 30 48 62.5
branch 0 12 0.0
condition 0 6 0.0
subroutine 10 13 76.9
pod 0 3 0.0
total 40 82 48.7


line stmt bran cond sub pod time code
1             package Testcontainers::Module::Redis;
2             # ABSTRACT: Redis container module for Testcontainers
3              
4 2     2   2023 use strict;
  2         3  
  2         60  
5 2     2   7 use warnings;
  2         4  
  2         98  
6 2     2   9 use Carp qw( croak );
  2         145  
  2         121  
7 2     2   23 use Testcontainers;
  2         3  
  2         55  
8 2     2   8 use Testcontainers::Wait;
  2         2  
  2         68  
9              
10             our $VERSION = '0.001';
11              
12 2     2   7 use Exporter 'import';
  2         2  
  2         93  
13             our @EXPORT_OK = qw( redis_container );
14              
15             use constant {
16 2         465 DEFAULT_IMAGE => 'redis:7-alpine',
17             DEFAULT_PORT => '6379/tcp',
18 2     2   8 };
  2         3  
19              
20             =head1 SYNOPSIS
21              
22             use Testcontainers::Module::Redis qw( redis_container );
23              
24             my $redis = redis_container();
25              
26             my $host = $redis->host;
27             my $port = $redis->mapped_port('6379/tcp');
28             my $url = $redis->connection_string; # "redis://localhost:32789"
29              
30             $redis->terminate;
31              
32             =head1 DESCRIPTION
33              
34             Pre-configured Redis container module, equivalent to Go's
35             C.
36              
37             =cut
38              
39             sub redis_container {
40 0     0 0   my (%opts) = @_;
41              
42 0   0       my $image = $opts{image} // DEFAULT_IMAGE;
43 0   0       my $port = $opts{port} // DEFAULT_PORT;
44              
45 0           my @cmd;
46 0 0         if ($opts{password}) {
47 0           push @cmd, 'redis-server', '--requirepass', $opts{password};
48             }
49              
50             my $container = Testcontainers::run($image,
51             exposed_ports => [$port],
52             _internal_labels => {
53             'org.testcontainers.module' => 'redis',
54             },
55             ($opts{password} ? (cmd => \@cmd) : ()),
56             wait_for => Testcontainers::Wait::for_log('Ready to accept connections'),
57             startup_timeout => $opts{startup_timeout} // 30,
58 0 0 0       ($opts{name} ? (name => $opts{name}) : ()),
    0          
59             );
60              
61             return Testcontainers::Module::Redis::Container->new(
62             _inner => $container,
63             password => $opts{password},
64 0           port => $port,
65             );
66             }
67              
68             =func redis_container(%opts)
69              
70             Create and start a Redis container.
71              
72             Options: C, C, C, C, C.
73              
74             =cut
75              
76              
77             package Testcontainers::Module::Redis::Container;
78              
79 2     2   9 use strict;
  2         4  
  2         47  
80 2     2   7 use warnings;
  2         3  
  2         69  
81 2     2   7 use Moo;
  2         3  
  2         9  
82              
83             has _inner => (is => 'ro', required => 1, handles => [qw(
84             id image host mapped_port mapped_port_info endpoint container_id
85             name state is_running logs exec stop start terminate refresh
86             )]);
87             has password => (is => 'ro', default => undef);
88             has port => (is => 'ro', required => 1);
89              
90             sub connection_string {
91 0     0 0   my ($self) = @_;
92 0           my $host = $self->host;
93 0           my $mapped = $self->mapped_port($self->port);
94 0 0         if ($self->password) {
95 0           return sprintf("redis://:%s\@%s:%s", $self->password, $host, $mapped);
96             }
97 0           return sprintf("redis://%s:%s", $host, $mapped);
98             }
99              
100             =method connection_string
101              
102             Returns a Redis connection URL: C or
103             C.
104              
105             =cut
106              
107             sub DEMOLISH {
108 0     0 0   my ($self, $in_global) = @_;
109 0 0         return if $in_global;
110 0 0         $self->_inner->terminate if $self->_inner;
111 0           return;
112             }
113              
114             1;