diff --git a/lib/hobo/ssh.rb b/lib/hobo/ssh.rb index 5fb2a58e5..e01482775 100644 --- a/lib/hobo/ssh.rb +++ b/lib/hobo/ssh.rb @@ -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 diff --git a/test/hobo/ssh_test.rb b/test/hobo/ssh_test.rb index e0ac59eeb..cf4dbc149 100644 --- a/test/hobo/ssh_test.rb +++ b/test/hobo/ssh_test.rb @@ -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 diff --git a/test/hobo/vm_test.rb b/test/hobo/vm_test.rb index ab0984d37..bcdb963d7 100644 --- a/test/hobo/vm_test.rb +++ b/test/hobo/vm_test.rb @@ -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