|
| 1 | +# Testcontainers Perl 5 — Architecture |
| 2 | + |
| 3 | +This document describes the internal design and architectural decisions of the library. |
| 4 | +For a feature inventory and implementation stats see [PROJECT_SUMMARY.md](PROJECT_SUMMARY.md). |
| 5 | +For usage examples and getting-started instructions see [QUICKSTART.md](QUICKSTART.md). |
| 6 | + |
| 7 | +## Design Goals |
| 8 | + |
| 9 | +| Goal | How | |
| 10 | +|------|-----| |
| 11 | +| Perl-first | Moo OO, roles, functional API, CPAN conventions | |
| 12 | +| Simplicity | Single `run()` entry point returns a ready-to-use container | |
| 13 | +| Extensibility | Moo roles for wait strategies, factory functions for modules | |
| 14 | +| Test-friendly | Works with `Test::More`, automatic cleanup via `DEMOLISH` | |
| 15 | +| Go-inspired | API mirrors [Testcontainers for Go](https://golang.testcontainers.org/) where practical | |
| 16 | + |
| 17 | +## Module Dependency Graph |
| 18 | + |
| 19 | +The library has two independent layers with a strict dependency direction: |
| 20 | + |
| 21 | +``` |
| 22 | +Testcontainers ──depends on──► WWW::Docker ──speaks to──► Docker Engine |
| 23 | +(test API) (HTTP client) (REST API) |
| 24 | +``` |
| 25 | + |
| 26 | +**WWW::Docker** is a self-contained Docker API client vendored from [Getty/p5-www-docker](https://github.com/Getty/p5-www-docker). It translates Perl method calls into Docker Engine REST requests over a Unix socket. It knows nothing about test containers, wait strategies, or modules. |
| 27 | + |
| 28 | +**Testcontainers** owns all concepts meaningful to test authors: container lifecycle, wait strategies, pre-built modules, labels, and network management. It delegates all Docker I/O to `WWW::Docker` via the `Testcontainers::DockerClient` wrapper. |
| 29 | + |
| 30 | +## Component Layout |
| 31 | + |
| 32 | +``` |
| 33 | +┌──────────────────────────────────────────────────────────────────┐ |
| 34 | +│ Testcontainers module │ |
| 35 | +│ │ |
| 36 | +│ ┌──────────────┐ ┌───────────────────┐ ┌──────────────────┐ │ |
| 37 | +│ │ Testcontainers│ │ ContainerRequest │ │ Container │ │ |
| 38 | +│ │ ::run() │──│ (config builder) │──│ (running wrapper)│ │ |
| 39 | +│ └──────────────┘ └───────────────────┘ └──────────────────┘ │ |
| 40 | +│ │ |
| 41 | +│ ┌────────────────────────────────────────────────────────────┐ │ |
| 42 | +│ │ Wait Strategies (Moo::Role based) │ │ |
| 43 | +│ │ HostPort │ HTTP │ Log │ HealthCheck │ Multi │ │ |
| 44 | +│ └────────────────────────────────────────────────────────────┘ │ |
| 45 | +│ │ |
| 46 | +│ ┌────────────────────────────────────────────────────────────┐ │ |
| 47 | +│ │ Modules (factory functions) │ │ |
| 48 | +│ │ PostgreSQL │ MySQL │ Redis │ Nginx │ │ |
| 49 | +│ └────────────────────────────────────────────────────────────┘ │ |
| 50 | +│ │ |
| 51 | +│ ┌──────────────┐ ┌───────────────────┐ │ |
| 52 | +│ │ DockerClient │──│ Labels │ │ |
| 53 | +│ │ (wrapper) │ │ (org.testcontainers│ │ |
| 54 | +│ └──────┬───────┘ │ label mgmt) │ │ |
| 55 | +│ │ └───────────────────┘ │ |
| 56 | +├─────────┼────────────────────────────────────────────────────────┤ |
| 57 | +│ ▼ │ |
| 58 | +│ ┌──────────────────────────────────────────────────────────────┐│ |
| 59 | +│ │ WWW::Docker (vendored) ││ |
| 60 | +│ │ Docker.pm │ Request.pm │ Container.pm │ Image.pm │ etc. ││ |
| 61 | +│ └──────────────────────────────────────────────────────────────┘│ |
| 62 | +└──────────────────────────────────────────────────────────────────┘ |
| 63 | +``` |
| 64 | + |
| 65 | +## Key Patterns |
| 66 | + |
| 67 | +### Factory Function API |
| 68 | + |
| 69 | +The primary API is a single exported function rather than an object constructor: |
| 70 | + |
| 71 | +```perl |
| 72 | +use Testcontainers qw( run ); |
| 73 | +use Testcontainers::Wait; |
| 74 | + |
| 75 | +my $container = run('postgres:16-alpine', |
| 76 | + exposed_ports => ['5432/tcp'], |
| 77 | + env => { POSTGRES_PASSWORD => 'test' }, |
| 78 | + wait_for => Testcontainers::Wait::for_log('ready to accept connections'), |
| 79 | +); |
| 80 | +``` |
| 81 | + |
| 82 | +This mirrors Go's `testcontainers.Run(ctx, req)` pattern. Internally, `run()` builds a `ContainerRequest`, creates the container via `DockerClient`, starts it, and executes the wait strategy. |
| 83 | + |
| 84 | +### Moo-Based Object System |
| 85 | + |
| 86 | +All classes use [Moo](https://metacpan.org/pod/Moo) for lightweight object construction: |
| 87 | + |
| 88 | +- `has` declarations for attributes with defaults and validation |
| 89 | +- `Moo::Role` for shared behaviour (e.g., `Testcontainers::Wait::Base`) |
| 90 | +- `DEMOLISH` for automatic cleanup (container termination on object destruction) |
| 91 | + |
| 92 | +### Wait Strategy Composition |
| 93 | + |
| 94 | +Wait strategies follow the role pattern: |
| 95 | + |
| 96 | +```perl |
| 97 | +package Testcontainers::Wait::HostPort; |
| 98 | +use Moo; |
| 99 | +with 'Testcontainers::Wait::Base'; |
| 100 | + |
| 101 | +sub check { |
| 102 | + my ($self, $container) = @_; |
| 103 | + # Attempt TCP connection, return 1 on success, 0 on failure |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +`Testcontainers::Wait::Base` provides the `wait_until_ready($container, $timeout)` polling loop that repeatedly calls `check()` until it returns true or the timeout expires. |
| 108 | + |
| 109 | +`Testcontainers::Wait::Multi` composes multiple strategies — all must pass. |
| 110 | + |
| 111 | +### Module Pattern |
| 112 | + |
| 113 | +Pre-built modules follow a consistent factory pattern: |
| 114 | + |
| 115 | +```perl |
| 116 | +package Testcontainers::Module::PostgreSQL; |
| 117 | +use Exporter 'import'; |
| 118 | +our @EXPORT_OK = qw( postgres_container ); |
| 119 | + |
| 120 | +sub postgres_container { |
| 121 | + my (%opts) = @_; |
| 122 | + my $container = Testcontainers::run($image, |
| 123 | + exposed_ports => ['5432/tcp'], |
| 124 | + env => { POSTGRES_PASSWORD => $password, ... }, |
| 125 | + _internal_labels => { 'org.testcontainers.module' => 'postgresql' }, |
| 126 | + wait_for => Testcontainers::Wait::for_log('ready to accept connections', |
| 127 | + occurrences => 2), |
| 128 | + ); |
| 129 | + return Testcontainers::Module::PostgreSQL::Container->new( |
| 130 | + _container => $container, ... |
| 131 | + ); |
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | +The inner `::Container` class wraps the base container and adds service-specific methods like `dsn()` and `connection_string()`. |
| 136 | + |
| 137 | +### Labels Specification |
| 138 | + |
| 139 | +`Testcontainers::Labels` centralizes label management: |
| 140 | + |
| 141 | +- `default_labels($session_id)` — returns the standard `org.testcontainers.*` labels |
| 142 | +- `merge_custom_labels(\%defaults, \%user_labels)` — merges user labels, rejecting any with the reserved `org.testcontainers` prefix |
| 143 | +- `_internal_labels` attribute on `ContainerRequest` — allows framework code (modules) to set reserved-prefix labels without triggering validation |
| 144 | + |
| 145 | +## Container Lifecycle |
| 146 | + |
| 147 | +``` |
| 148 | +run($image, %opts) |
| 149 | + │ |
| 150 | + ├── Build ContainerRequest |
| 151 | + ├── Pull image (unless no_pull) |
| 152 | + ├── Create container (Docker API) |
| 153 | + ├── Start container |
| 154 | + ├── Refresh (get port mappings) |
| 155 | + ├── Execute wait strategy |
| 156 | + │ └── Poll check() until ready or timeout |
| 157 | + └── Return Container object |
| 158 | + │ |
| 159 | + ├── host() / mapped_port() / endpoint() |
| 160 | + ├── exec() / logs() |
| 161 | + └── terminate() |
| 162 | + └── Stop + Remove + Volumes |
| 163 | +``` |
| 164 | + |
| 165 | +## Error Handling |
| 166 | + |
| 167 | +- User-facing errors use `Carp::croak` with descriptive messages. |
| 168 | +- Docker communication errors are caught with `eval { }` and logged via `Log::Any`. |
| 169 | +- Wait strategy timeouts produce a `croak` with the strategy name and elapsed time. |
| 170 | +- Container auto-cleanup in `DEMOLISH` uses `eval` to suppress errors during global destruction. |
| 171 | + |
| 172 | +## Testing Architecture |
| 173 | + |
| 174 | +Tests are organized by scope: |
| 175 | + |
| 176 | +| File | Scope | Docker? | |
| 177 | +|------|-------|---------| |
| 178 | +| `t/01-load.t` | Module loading | No | |
| 179 | +| `t/02-container-request.t` | ContainerRequest unit tests | No | |
| 180 | +| `t/03-wait-strategies.t` | Wait strategy unit tests | No | |
| 181 | +| `t/04-integration.t` | Full container lifecycle | Yes | |
| 182 | +| `t/05-modules.t` | Module integration | Yes | |
| 183 | +| `t/06-basic.t` – `t/12-volumes.t` | WWW::Docker client tests | No | |
| 184 | + |
| 185 | +Integration tests guard with: |
| 186 | + |
| 187 | +```perl |
| 188 | +plan skip_all => 'Set TESTCONTAINERS_LIVE=1' unless $ENV{TESTCONTAINERS_LIVE}; |
| 189 | +``` |
| 190 | + |
| 191 | +## References |
| 192 | + |
| 193 | +- [Testcontainers for Go](https://golang.testcontainers.org/) — primary inspiration |
| 194 | +- [WWW::Docker](https://github.com/Getty/p5-www-docker) — vendored Docker client |
| 195 | +- [Moo](https://metacpan.org/pod/Moo) — object system |
| 196 | +- [Docker Engine API v1.44](https://docs.docker.com/engine/api/v1.44/) — Docker REST API reference |
0 commit comments