File Coverage

blib/lib/Testcontainers/Module/Nginx.pm
Criterion Covered Total %
statement 30 43 69.7
branch 0 6 0.0
condition 0 6 0.0
subroutine 10 13 76.9
pod 0 3 0.0
total 40 71 56.3


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