From 303cc90a1249cff2c0925250c036880f76345527 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 31 Jan 2010 22:38:00 -0800 Subject: [PATCH] Waiting for a VM to boot maxes out (configurable # of times) --- config/default.yml | 1 + lib/hobo/vm.rb | 9 ++++++++- test/hobo/vm_test.rb | 5 +++++ test/test_helper.rb | 5 +++-- 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/config/default.yml b/config/default.yml index a5bd96326..10d3db9ac 100644 --- a/config/default.yml +++ b/config/default.yml @@ -3,6 +3,7 @@ :pass: hobo :host: localhost :port: 2222 + :max_tries: 10 :dotfile_name: .hobo diff --git a/lib/hobo/vm.rb b/lib/hobo/vm.rb index e771d103d..0cd8ec1a9 100644 --- a/lib/hobo/vm.rb +++ b/lib/hobo/vm.rb @@ -96,14 +96,21 @@ module Hobo HOBO_LOGGER.info "Waiting for VM to boot..." counter = 1 begin + sleep 5 unless ENV['HOBO_ENV'] == 'test' + HOBO_LOGGER.info "Trying to connect (attempt ##{counter} of #{Hobo.config[:ssh][:max_tries]})..." SSH.execute { |ssh| } rescue Errno::ECONNREFUSED - sleep 5 unless ENV['HOBO_ENV'] == 'test' + if counter >= Hobo.config[:ssh][:max_tries].to_i + HOBO_LOGGER.info "Failed to connect to VM! Failed to boot?" + return false + end + counter += 1 retry end HOBO_LOGGER.info "VM booted and ready for use!" + true end end end \ No newline at end of file diff --git a/test/hobo/vm_test.rb b/test/hobo/vm_test.rb index 81f371f2d..5d4ced623 100644 --- a/test/hobo/vm_test.rb +++ b/test/hobo/vm_test.rb @@ -118,6 +118,11 @@ class VMTest < Test::Unit::TestCase Net::SSH.expects(:start).once.in_sequence(ssh_seq) @vm.start end + + should "try the max number of times then just return" do + Net::SSH.expects(:start).times(Hobo.config[:ssh][:max_tries].to_i).raises(Errno::ECONNREFUSED) + assert !@vm.start + end end context "importing" do diff --git a/test/test_helper.rb b/test/test_helper.rb index b8cd33d8a..e894355b9 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -28,11 +28,12 @@ require 'mocha' class Test::Unit::TestCase def hobo_mock_config - { :ssh => { + { :ssh => { :uname => 'foo', :pass => 'bar', :host => 'baz', - :port => 'bak' + :port => 'bak', + :max_tries => 10 }, :vm => { :base => "foo",