File Coverage

blib/lib/Testcontainers/Wait/HealthCheck.pm
Criterion Covered Total %
statement 15 23 65.2
branch 0 4 0.0
condition 0 4 0.0
subroutine 5 6 83.3
pod 0 1 0.0
total 20 38 52.6


line stmt bran cond sub pod time code
1             package Testcontainers::Wait::HealthCheck;
2             # ABSTRACT: Wait strategy for Docker health checks
3              
4 4     4   26 use strict;
  4         7  
  4         154  
5 4     4   17 use warnings;
  4         17  
  4         185  
6 4     4   20 use Moo;
  4         6  
  4         20  
7 4     4   1441 use Carp qw( croak );
  4         9  
  4         300  
8 4     4   27 use Log::Any qw( $log );
  4         8  
  4         42  
9              
10             our $VERSION = '0.001';
11              
12             with 'Testcontainers::Wait::Base';
13              
14             =head1 SYNOPSIS
15              
16             use Testcontainers::Wait;
17              
18             my $wait = Testcontainers::Wait::for_health_check();
19              
20             =head1 DESCRIPTION
21              
22             Waits for the Docker health check (if configured in the image) to report
23             "healthy". Equivalent to Go's C.
24              
25             =cut
26              
27             sub check {
28 0     0 0   my ($self, $container) = @_;
29              
30 0           my $state = eval { $container->state };
  0            
31 0 0         return 0 unless $state;
32              
33 0   0       my $health = $state->{Health} // {};
34 0   0       my $status = $health->{Status} // '';
35              
36 0           $log->tracef("Health check status: %s", $status);
37              
38 0 0         return lc($status) eq 'healthy' ? 1 : 0;
39             }
40              
41             =method check($container)
42              
43             Check if Docker reports the container as "healthy".
44              
45             =cut
46              
47             1;