Net-OpenSSH-More-Linux.t 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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::Linux;
  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::Linux->new(
  15. 'host' => '127.0.0.1', 'output_prefix' => '# ', 'retry_max' => 1,
  16. );
  17. is( ref $obj, 'Net::OpenSSH::More::Linux', "Got right ref type for object upon instantiation (using localhost)" );
  18. my $adapter = $obj->get_primary_adapter(1);
  19. ok( $adapter, "Got something back as the primary adapter (use_local)" );
  20. is( $obj->get_primary_adapter(), $adapter, "Got expected adapter (remote)" );
  21. # Test backup/restore, first with existing
  22. $obj->cmd(qw{touch /tmp/howdy});
  23. $obj->backup_files('/tmp/howdy');
  24. $obj->cmd(qw{rm -f /tmp/howdy});
  25. $obj->restore_files();
  26. ok( $obj->sftp->test_e('/tmp/howdy'), "Created /tmp/howdy file restored via backup/restore methods" );
  27. $obj->cmd(qw{rm -f /tmp/howdy});
  28. # "Backup" non-existing file
  29. $obj->backup_files('/tmp/yeehaw');
  30. $obj->cmd(qw{touch /tmp/yeehaw});
  31. ok( $obj->sftp->test_e('/tmp/yeehaw'), "Created /tmp/yeehaw touch file for testing backup/restore" );
  32. $obj->DESTROY();
  33. $obj = Net::OpenSSH::More::Linux->new(
  34. 'host' => 'localhost', 'use_persistent_shell' => 0, 'retry_max' => 1,
  35. );
  36. ok( !$obj->sftp->test_e('/tmp/yeehaw'), "File no longer exists after restored to original state via destructor" );
  37. };
  38. # Mock based testing
  39. subtest_streamed "Common tests using mocks" => sub {
  40. local %Net::OpenSSH::More::cache;
  41. my $parent_mock = Test::MockModule->new('Net::OpenSSH::More');
  42. $parent_mock->redefine(
  43. 'new' => sub { bless {}, $_[0] },
  44. 'check_master' => 1,
  45. 'DESTROY' => undef,
  46. );
  47. my $obj = Net::OpenSSH::More::Linux->new( 'host' => 'localhost', retry_max => 1 );
  48. is( ref $obj, 'Net::OpenSSH::More::Linux', "Got right ref type for object upon instantiation" );
  49. };
  50. done_testing();