Net-OpenSSH-More.t 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. my $expected = [ "whee", '', 0 ];
  22. is( \@cmd_ret, $expected, "Got expected return (non-persistent shell)" );
  23. $obj->use_persistent_shell(1);
  24. @cmd_ret = $obj->cmd(qw{echo whee});
  25. is( \@cmd_ret, $expected, "Got expected return (persistent shell)" );
  26. $obj->write( "net-openssh-more-test", "whee" );
  27. @cmd_ret = $obj->cmd(qw{cat net-openssh-more-test});
  28. is( \@cmd_ret, $expected, "Got expected result from write" );
  29. my $ec = $obj->cmd_exit_code(qw{rm -f net-openssh-more-test});
  30. is( $ec, 0, "cmd_exit_code returns 0 on successful command" );
  31. my $ret = $obj->eval_full( 'code' => sub { return $_[0] ? "whee" : "widdly"; }, 'args' => [1] );
  32. is( $ret, "whee", "Got expected result from eval_full" );
  33. };
  34. # Mock based testing
  35. subtest_streamed "Common tests using mocks" => sub {
  36. local %Net::OpenSSH::More::cache;
  37. my $parent_mock = Test::MockModule->new('Net::OpenSSH');
  38. $parent_mock->redefine(
  39. 'new' => sub { bless {}, $_[0] },
  40. 'check_master' => 1,
  41. );
  42. {
  43. # MockModule can't actually redefine destructors properly due to the mock also going out of scope.
  44. no warnings qw{redefine};
  45. *Net::OpenSSH::DESTROY = sub { undef };
  46. }
  47. my $obj = Net::OpenSSH::More->new( 'host' => '127.0.0.1', retry_max => 1, 'output_prefix' => '# ' );
  48. is( ref $obj, 'Net::OpenSSH::More', "Got right ref type for object upon instantiation" );
  49. is( $obj->diag("Whee"), undef, "You should see whee before this subtest" );
  50. };
  51. done_testing();