Net-OpenSSH-More.t 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. use strict;
  2. use warnings;
  3. use Test2::V0;
  4. use Test2::Tools::Explain;
  5. use Test2::Plugin::NoWarnings;
  6. use Test::MockModule qw{strict};
  7. use Carp::Always;
  8. use FindBin;
  9. use lib "$FindBin::Bin/../lib";
  10. use Net::OpenSSH::More;
  11. subtest "Live tests versus localhost" => sub {
  12. plan 'skip_all' => 'AUTHOR_TESTS not set in shell environment, skipping...' if !$ENV{'AUTHOR_TESTS'};
  13. local %Net::OpenSSH::More::cache;
  14. my $obj = Net::OpenSSH::More->new( 'host' => '127.0.0.1' );
  15. is( ref $obj, 'Net::OpenSSH::More', "Got right ref type for object upon instantiation (using IP)" );
  16. $obj = Net::OpenSSH::More->new( 'host' => 'localhost', 'debug' => 1, 'output_prefix' => '# ' );
  17. is( ref $obj, 'Net::OpenSSH::More', "Got right ref type for object upon instantiation (using localhost)" );
  18. my @cmd_ret = $obj->cmd( { 'no_persist' => 1 }, 'echo "whee"' );
  19. is( \@cmd_ret, [ "whee", '', 0 ], "Got expected return (non-persistent shell)" );
  20. };
  21. # Mock based testing
  22. subtest "Common tests using mocks" => sub {
  23. local %Net::OpenSSH::More::cache;
  24. my $parent_mock = Test::MockModule->new('Net::OpenSSH');
  25. $parent_mock->redefine(
  26. 'new' => sub { bless {}, $_[0] },
  27. 'check_master' => 1,
  28. );
  29. {
  30. # MockModule can't actually redefine destructors properly due to the mock also going out of scope.
  31. no warnings qw{redefine};
  32. *Net::OpenSSH::DESTROY = sub { undef };
  33. }
  34. my $obj = Net::OpenSSH::More->new( 'host' => '127.0.0.1', retry_max => 1, 'output_prefix' => '# ' );
  35. is( ref $obj, 'Net::OpenSSH::More', "Got right ref type for object upon instantiation" );
  36. is( $obj->diag("Whee"), undef, "You should see whee before this subtest" );
  37. };
  38. done_testing();