测试 ssh 连接 [英] Testing ssh connection
问题描述
我的项目的一个重要部分是使用 ssh 登录远程服务器并对其上的文件做一些事情:
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
如何测试?我想我可以打开本地 ssh 服务器并检查其上的文件名(也许它可能在我的 test/spec 目录中).或者也许有人可以为我指出更好的解决方案?
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?
推荐答案
我认为这足以测试您是否向 ssh 服务器发送了正确的命令.您的应用程序可能没有实现服务器 - 因此您必须相信服务器正常工作并经过测试.
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.
如果您确实实现了服务器,那么您需要对其进行测试,但就 SSH 而言,我会做一些这样的模拟(RSpec 2 语法):
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
这篇关于测试 ssh 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!