HealthCheck-Diagnostic-SFTP
view release on metacpan or search on metacpan
t/HealthCheck-Diagnostic-SFTP.t view on Meta::CPAN
use strict;
use warnings;
use Test::More;
use Test::MockModule;
use Test::Differences;
use HealthCheck::Diagnostic::SFTP;
# Mock the SFTP module so that we can pretend to have good and bad hosts.
my $last_sftp;
my $mock = Test::MockModule->new( 'Net::SFTP' );
$mock->mock( new => sub {
my ( $class, $host, %params ) = @_;
# In-accessible hosts should not be reached.
die "Net::SSH: Bad host name: $host"
if $host eq 'inaccessible-host';
# Slow hosts should be successfully timed out.
sleep 3 if $host eq 'slow-host';
$last_sftp = bless( \%params, 'Net::SFTP' );
return $last_sftp;
} );
# Check that we can use HealthCheck as a class.
{
my $result = HealthCheck::Diagnostic::SFTP->check(
host => 'good-host',
);
is $result->{status}, 'OK',
'Can use HealthCheck as a class.';
is $result->{info}, 'Successful connection for good-host SFTP',
'Info message is correct for instance check.';
}
# Check that we can use HealthCheck with initialized values too.
{
my $diagnostic = HealthCheck::Diagnostic::SFTP->new(
host => 'good-host',
);
my $result = $diagnostic->check;
is $result->{status}, 'OK',
'Can use HealthCheck with instance values too.';
is $result->{info}, 'Successful connection for good-host SFTP',
'Info message is correct for initialized diagnostic.';
}
# Check that `check` parameters override the initialized parameters.
{
my $diagnostic = HealthCheck::Diagnostic::SFTP->new(
host => 'good-host1',
);
my $result = $diagnostic->check( host => 'good-host2' );
is $result->{status}, 'OK',
'HealthCheck result status is still OK.';
is $result->{info}, 'Successful connection for good-host2 SFTP',
'Info message shows that the `check` host overrides all.';
}
# Create a method that returns the info and status after running the
# check. If it failed, then this just returns the error.
my $run_check_or_error = sub {
my $result;
local $@;
# We passed in a diagnostic, just run the check.
( run in 0.916 second using v1.01-cache-2.11-cpan-6aa56a78535 )