Problems creating an ssh tunnel

Hello,
I’m writing a series of test that test credit cart transactions and
our card service relies on communicating to a sabrix server through an
ssh tunnel.
I’ve created something that works but I have to admit, it doesn’t make
sense because I’m using a combination of a unix exec call and
Net::SSH::Gateway.
In order for the ssh tunnel work with the tests to to work they HAVE to
work in conjunction to each other. I’m ok with it but it seems like 1
out of 10 times, the tests will fail because the ssh tunnel isn’t open
for some reason. I’ve tried about every solution out there but can’t
seem to get anything else to work. Here’s the code:

  def setup
    super
    @controller = AccountController.new
    @pid = fork{ exec("ssh -N -L 8888:10.0.1.68:8080
[email protected]") } #this has to be open in a thread
    @gateway = Net::SSH::Gateway.new('test.test.com', 'deploy')
#this has to be created as well
    sleep(1) #sleep just a tad to let everything start up
  end

  def test_update_credit_card_info
    #test code here…
  end

And in my teardown method, I’m killing the ssh tunnel like this:

  def teardown
    system("kill -9 #{@pid}") #kill by pid
    @gateway.shutdown!    #kill the Net::SSH::Gateway instance
  end

Any ideas?

Thanks,
Eric

On Tue, May 7, 2013 at 12:28 PM, Clem R. [email protected] wrote:

for some reason. I’ve tried about every solution out there but can’t
sleep(1) #sleep just a tad to let everything start up
def teardown

Posted via http://www.ruby-forum.com/.

Just off the top of my head, instead of doing the setup and tear down,
perhaps pass the test code into a method as a block that does that
stuff, and mock the Net::SSH stuff?

Hey - there thanks for getting back to me on this and sorry for the long
reply. I’ve been out sick in the last few days.

I do not want to deal with mocking an ssh tunnel because I want the test
to be 100% a real test. I want to make sure the communication between
my code and the server to be 100% real. I want to be able to detect
any changes that come back from the sabrix server transactions.

Also, I’m not even sure how I’d mock out an ssh tunnel connection in the
first place. Really, it seems like I’d be mocking the response from
the sabrix server.

Thanks for your help so far.

tamouse mailing lists wrote in post #1108163:

On Tue, May 7, 2013 at 12:28 PM, Clem R. [email protected] wrote:

for some reason. I’ve tried about every solution out there but can’t
sleep(1) #sleep just a tad to let everything start up
def teardown

Posted via http://www.ruby-forum.com/.

Just off the top of my head, instead of doing the setup and tear down,
perhaps pass the test code into a method as a block that does that
stuff, and mock the Net::SSH stuff?

Cliff,
Thank you for the code suggestion. I started working with it just
now but had to put it down just to get some code out the door. I’ll
get back to you with the results in a bit…

Best

Can you just ssh directly in your code and deal with errors?

require ‘rubygems’
require ‘net/ssh’
require ‘net/ssh/telnet’

class SSH
attr_accessor :errors

def initialize(creds)
begin
@ssh_session = Net::SSH.start(creds[:host], creds[:user],
:password
=> creds[:password], :keys => [])
@ssh = Net::SSH::Telnet.new(“Session” => @ssh_session, “Prompt” =>
creds[:prompt])
@errors = false
rescue Exception => e
@errors = e
end
end

def cmd(command)
@ssh.cmd(command)
end

def close
@ssh.close
end

end

ssh = SSH.new(:host => “host.com”, :username => “billy”, :password =>
“123”, :prompt => /./)
unless ssh.errors
ssh.cmd(“pwd”)
end
ssh.close