SSH.up? uses Net::SSH timeouts again, for now. Looks like checking if a VM is up is finally working properly.

This commit is contained in:
Mitchell Hashimoto 2010-02-09 16:48:59 -08:00
parent 8c2068565b
commit d48b79e8ec
3 changed files with 23 additions and 13 deletions

View File

@ -24,7 +24,13 @@ module Hobo
def up?
port = Hobo.config.vm.forwarded_ports[Hobo.config.ssh.forwarded_port_key][:hostport]
Ping.pingecho Hobo.config.ssh.host, 1, port
Net::SSH.start(Hobo.config.ssh.host, Hobo.config.ssh.username, :port => port, :password => Hobo.config.ssh.password, :timeout => 5) do |ssh|
return true
end
false
rescue Errno::ECONNREFUSED
false
end
end
end

View File

@ -42,17 +42,21 @@ class SshTest < Test::Unit::TestCase
hobo_mock_config
end
should "pingecho the server" do
port = Hobo.config.vm.forwarded_ports[Hobo.config.ssh.forwarded_port_key][:hostport]
Ping.expects(:pingecho).with(Hobo.config.ssh.host, 1, port).once
Hobo::SSH.up?
should "return true if SSH connection works" do
Net::SSH.expects(:start).yields("success")
assert Hobo::SSH.up?
end
should "use custom host if set" do
port = Hobo.config.vm.forwarded_ports[Hobo.config.ssh.forwarded_port_key][:hostport]
Hobo.config.ssh.host = "foo"
Ping.expects(:pingecho).with(Hobo.config.ssh.host, 1, port).once
Hobo::SSH.up?
should "return false if SSH connection times out" do
Net::SSH.expects(:start)
assert !Hobo::SSH.up?
end
should "return false if the connection is refused" do
Net::SSH.expects(:start).raises(Errno::ECONNREFUSED)
assert_nothing_raised {
assert !Hobo::SSH.up?
}
end
end
end

View File

@ -119,13 +119,13 @@ class VMTest < Test::Unit::TestCase
should "repeatedly ping the SSH port and return false with no response" do
seq = sequence('pings')
Ping.expects(:pingecho).times(Hobo.config[:ssh][:max_tries].to_i - 1).returns(false).in_sequence(seq)
Ping.expects(:pingecho).once.returns(true).in_sequence(seq)
Hobo::SSH.expects(:up?).times(Hobo.config[:ssh][:max_tries].to_i - 1).returns(false).in_sequence(seq)
Hobo::SSH.expects(:up?).once.returns(true).in_sequence(seq)
assert @vm.start
end
should "ping the max number of times then just return" do
Ping.expects(:pingecho).times(Hobo.config[:ssh][:max_tries].to_i).returns(false)
Hobo::SSH.expects(:up?).times(Hobo.config[:ssh][:max_tries].to_i).returns(false)
assert !@vm.start
end
end