I've gotten much farther
[p5sagit/IPC-PerlSSH-MultiHop.git] / t / scratch.t
index e6b13c4..1d51086 100644 (file)
@@ -2,6 +2,8 @@
 use strictures 1;
 
 use Test::More;
+use Carp;
+use Devel::Dwarn;
 
 {
   package Net::SSH::Perl::ProxiedIPC;
@@ -23,16 +25,29 @@ use Test::More;
     Net::SSH::Perl::WithSocks->new();
   }
 
+  sub _ssh_env_vars {
+    $_[0]->{ssh_env_vars} ||= $_[0]->_build_ssh_env_vars;
+  }
+
+  sub _build_ssh_env_vars {
+    return '';
+    # this needs work I think. First off, it won't work.
+    # +{ $_[0]->_firsthop_perlssh->eval(; 'chomp(my @env = `ssh-agent`); my %new_env; foreach (@env) { /^(.*?)=(.*)/; $ENV{$1} =$new_env{$1}=$2; } return %new_env;' ); }
+  }
+
   sub _open_perlssh {
     my( $self, @hosts ) = @_;
     my $ssh = $self->_ssh;
-    my $command = join ' ', (map "ssh -A $_", @hosts), "perl";
+
+    my $env_str = $self->_ssh_env_vars;
+    my $command = join ' ', (map "ssh -o StrictHostKeyChecking=no -A $_", @hosts), "perl";
+    $command = "sh -c '$env_str$command'"; 
     my( $read, $write ) = $ssh->open2($command);
 
     my $readfunc = sub { sysread( $read, $_[0], $_[1] ) };
     my $writefunc = sub { syswrite( $write, $_[0] ) };
     
-    IPC::PerlSSH->new( Readfunc => $readfunc, Writefunc => $writefunc );
+    ($command, IPC::PerlSSH->new( Readfunc => $readfunc, Writefunc => $writefunc ));
   }
 
 
@@ -43,9 +58,9 @@ $ssh->login('test', 'test');
 
 my $pipc = Net::SSH::Perl::ProxiedIPC->new(ssh => $ssh);
 
-my $perlssh = $pipc->_open_perlssh;
+my ($cmd, $perlssh) = $pipc->_open_perlssh;
 
-is( ref $perlssh, "IPC::PerlSSH", '$perlssh isa IPC::PerlSSH' );
+is( ref $perlssh, "IPC::PerlSSH", "\$perlssh isa IPC::PerlSSH (via $cmd)" );
 
 $perlssh->eval( "use POSIX qw(uname)" );
 my @remote_uname = $perlssh->eval( "uname()" );
@@ -61,4 +76,16 @@ $perlssh->eval( "use File::HomeDir" );
 my $homedir2 = $perlssh->eval( 'File::HomeDir->my_home' );
 is( $homedir2, "/home/test", 'got $ENV{HOME} the smart way' );
 
+my $new_env = $perlssh->eval( 'chomp(my @env = `ssh-agent`); my %new_env; foreach (@env) { /^(.*?)=([^;]+)/ or next; $ENV{$1} =$new_env{$1}=$2; } my $output; $output .= "$_=$new_env{$_} " foreach ( keys %new_env ); $output;' );
+Dwarn $new_env;
+$pipc->{ssh_env_vars} = $new_env; 
+
+my @test_hosts = ( 'stagetwo@localhost', 'stagethree@localhost' );
+my ($cmd2, $pssh2) = $pipc->_open_perlssh(@test_hosts);
+is( ref $pssh2, "IPC::PerlSSH", "\$pssh2 isa IPC::PerlSSH (via $cmd2)" );
+
+$pssh2->eval( "use POSIX qw(uname)" );
+@remote_uname = $pssh2->eval( "uname()" );
+is( $remote_uname[1], "minerva", 'uname() returns minerva three jumps into localhost!' );
+
 done_testing;