Net-OpenSSH-More.t 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. use strict;
  2. use warnings;
  3. use Test2::V0;
  4. use Test2::Tools::Explain;
  5. use Test2::Tools::Subtest qw{subtest_streamed};
  6. use Test2::Plugin::NoWarnings;
  7. use Test::MockModule qw{strict};
  8. use FindBin;
  9. use lib "$FindBin::Bin/../lib";
  10. use Net::OpenSSH::More;
  11. subtest_streamed "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', 'no_cache' => 1 );
  15. is( ref $obj, 'Net::OpenSSH::More', "Got right ref type for object upon instantiation (using IP)" );
  16. $obj = Net::OpenSSH::More->new(
  17. 'host' => 'localhost', 'output_prefix' => '# ', 'use_persistent_shell' => 0, 'expect_timeout' => 1,
  18. );
  19. is( ref $obj, 'Net::OpenSSH::More', "Got right ref type for object upon instantiation (using localhost)" );
  20. my @cmd_ret = $obj->cmd( qw{echo whee} );
  21. is( \@cmd_ret, [ "whee", '', 0 ], "Got expected return (non-persistent shell)" );
  22. $obj->use_persistent_shell(1);
  23. @cmd_ret = $obj->cmd( qw{echo widdly} );
  24. is( \@cmd_ret, [ 'widdly', '', 0 ], "Got expected return (persistent shell)" );
  25. };
  26. # Mock based testing
  27. subtest_streamed "Common tests using mocks" => sub {
  28. local %Net::OpenSSH::More::cache;
  29. my $parent_mock = Test::MockModule->new('Net::OpenSSH');
  30. $parent_mock->redefine(
  31. 'new' => sub { bless {}, $_[0] },
  32. 'check_master' => 1,
  33. );
  34. {
  35. # MockModule can't actually redefine destructors properly due to the mock also going out of scope.
  36. no warnings qw{redefine};
  37. *Net::OpenSSH::DESTROY = sub { undef };
  38. }
  39. my $obj = Net::OpenSSH::More->new( 'host' => '127.0.0.1', retry_max => 1, 'output_prefix' => '# ' );
  40. is( ref $obj, 'Net::OpenSSH::More', "Got right ref type for object upon instantiation" );
  41. is( $obj->diag("Whee"), undef, "You should see whee before this subtest" );
  42. };
  43. done_testing();