Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing ssh connection

Tags:

ssh

ruby

testing

an important part of my project is to log in into remote server with ssh and do something with files on it:

Net::SSH.start(@host, @username, :password => @password) do |ssh|  
  ssh.exec!(rename_files_on_remote_server) 
end

How to test it? I think I can have local ssh server on and check file names on it (maybe it could be in my test/spec directory). Or maybe someone could point me better solution?

like image 546
Sławosz Avatar asked Sep 25 '26 02:09

Sławosz


1 Answers

I think it's enough to test that you're sending the correct commands to the ssh server. You're application presumably doesn't implement the server - so you have to trust that the server is correctly working and tested.

If you do implement the server then you'd need to test that, but as far as the SSH stuff goes, i'd do some mocking like this (RSpec 2 syntax):

describe "SSH Access" do
  let (:ssh_connection) { mock("SSH Connection") }
  before (:each) do
    Net::SSH.stub(:start) { ssh_connection }
  end
  it "should send rename commands to the connection" do
    ssh_connection.should_receive(:exec!).ordered.with("expected command")
    ssh_connection.should_receive(:exec!).ordered.with("next expected command")
    SSHAccessClass.rename_files!
  end
end
like image 108
Glenjamin Avatar answered Sep 26 '26 22:09

Glenjamin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!