Testing ssh connection

旧巷老猫 提交于 2019-12-30 10:30:41

问题


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?


回答1:


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



回答2:


Your suggested solution is similar to how I've done it before:

Log into the local machine. For convenience you could use 'localhost' or '127.0.0.1', but for a better simulation of network activity you might want to use the full hostname. On Mac OS and Linux you can grab the host easily by using:

`hostname`

or

require 'socket'
hostname = Socket.gethostname

which should be universal.

From there create or touch a file on the local machine after logging in, so you can test for the change with your test code.



来源:https://stackoverflow.com/questions/3889200/testing-ssh-connection

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!