diff --git a/lib/fog.rb b/lib/fog.rb index 3390e2149..091584c0f 100644 --- a/lib/fog.rb +++ b/lib/fog.rb @@ -57,4 +57,18 @@ module Fog !!@mocking end + def self.wait_for(timeout = 600, &block) + duration = 0 + start = Time.now + until yield || duration > timeout + sleep(1) + duration = Time.now - start + end + if duration > timeout + false + else + { :duration => duration } + end + end + end diff --git a/lib/fog/model.rb b/lib/fog/model.rb index fab2e6246..d36b326c4 100644 --- a/lib/fog/model.rb +++ b/lib/fog/model.rb @@ -110,11 +110,8 @@ module Fog end def wait_for(timeout = 600, &block) - start = Time.now - begin - reload - sleep(1) - end until instance_eval(&block) || Time.now - start > timeout + reload + Fog.wait_for(timeout) { reload && instance_eval(&block) } end private diff --git a/spec/aws/requests/s3/get_service_spec.rb b/spec/aws/requests/s3/get_service_spec.rb index 6dd49b264..f67540903 100644 --- a/spec/aws/requests/s3/get_service_spec.rb +++ b/spec/aws/requests/s3/get_service_spec.rb @@ -5,7 +5,7 @@ describe 'S3.get_service' do before(:all) do AWS[:s3].put_bucket('foggetservice') - wait_for { AWS[:s3].directories.get('foggetservice') } + Fog.wait_for { AWS[:s3].directories.get('foggetservice') } end after(:all) do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index f5bf30813..897346885 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -9,16 +9,6 @@ if ENV["FOG_MOCK"] == "true" Fog.mock! end -# unless defined?(LOADED_SPEC_OPTS) -# # inlined spec.opts -# require "#{current_directory}/compact_progress_bar_formatter" -# Spec::Runner.options.parse_format("Spec::Runner::Formatter::CompactProgressBarFormatter") -# Spec::Runner.options.loadby = 'mtime' -# Spec::Runner.options.reverse = true -# LOADED_SPEC_OPTS = true -# end - - module AWS class << self def [](service) @@ -104,16 +94,6 @@ def eventually(max_delay = 16, &block) end end -def wait_for(timeout = 600, &block) - start = Time.now - until instance_eval(&block) - if Time.now - start > timeout - break - end - sleep(1) - end -end - unless defined?(GENTOO_AMI) GENTOO_AMI = 'ami-5ee70037' end diff --git a/tests/helper.rb b/tests/helper.rb index 8a549e08a..a53edce5f 100644 --- a/tests/helper.rb +++ b/tests/helper.rb @@ -32,13 +32,3 @@ def validate_format(original_data, format) end valid end - -def wait_for(timeout = 600, &block) - start = Time.now - until instance_eval(&block) - if Time.now - start > timeout - break - end - sleep(1) - end -end \ No newline at end of file diff --git a/tests/rackspace/requests/servers/create_image_tests.rb b/tests/rackspace/requests/servers/create_image_tests.rb index df0f28fb3..6ddaa766a 100644 --- a/tests/rackspace/requests/servers/create_image_tests.rb +++ b/tests/rackspace/requests/servers/create_image_tests.rb @@ -3,14 +3,14 @@ Shindo.tests('Rackspace::Servers#create_image', 'rackspace') do before do @server_id = Rackspace[:servers].create_server(1, 3, 'fogcreateimage').body['server']['id'] - wait_for { Rackspace[:servers].get_server_details(@server_id).body['server']['status'] == 'ACTIVE' } + Fog.wait_for { Rackspace[:servers].get_server_details(@server_id).body['server']['status'] == 'ACTIVE' } @data = Rackspace[:servers].create_image(@server_id).body['image'] @image_id = @data['id'] end after do Rackspace[:servers].delete_server(@server_id) - wait_for { Rackspace[:servers].get_image_details(@image_id).body['image']['status'] == 'ACTIVE' } + Fog.wait_for { Rackspace[:servers].get_image_details(@image_id).body['image']['status'] == 'ACTIVE' } Rackspace[:servers].delete_image(@image_id) end diff --git a/tests/rackspace/requests/servers/create_server_tests.rb b/tests/rackspace/requests/servers/create_server_tests.rb index 34918a2b4..771857efa 100644 --- a/tests/rackspace/requests/servers/create_server_tests.rb +++ b/tests/rackspace/requests/servers/create_server_tests.rb @@ -7,7 +7,7 @@ Shindo.tests('Rackspace::Servers#create_server', 'rackspace') do end after do - wait_for { Rackspace[:servers].get_server_details(@server_id).body['server']['status'] == 'ACTIVE' } + Fog.wait_for { Rackspace[:servers].get_server_details(@server_id).body['server']['status'] == 'ACTIVE' } Rackspace[:servers].delete_server(@server_id) end diff --git a/tests/rackspace/requests/servers/delete_image_tests.rb b/tests/rackspace/requests/servers/delete_image_tests.rb index 540e77256..737ca0ae5 100644 --- a/tests/rackspace/requests/servers/delete_image_tests.rb +++ b/tests/rackspace/requests/servers/delete_image_tests.rb @@ -7,12 +7,12 @@ Shindo.tests('Rackspace::Servers#delete_image', 'rackspace') do end after do - wait_for { Rackspace[:servers].get_server_details(@server_id).body['server']['status'] == 'ACTIVE' } + Fog.wait_for { Rackspace[:servers].get_server_details(@server_id).body['server']['status'] == 'ACTIVE' } Rackspace[:servers].delete_server(@server_id) end test('has proper output format') do - wait_for { Rackspace[:servers].get_image_details(@image_id).body['image']['status'] == 'ACTIVE' } + Fog.wait_for { Rackspace[:servers].get_image_details(@image_id).body['image']['status'] == 'ACTIVE' } Rackspace[:servers].delete_image(@image_id) end diff --git a/tests/rackspace/requests/servers/delete_server_tests.rb b/tests/rackspace/requests/servers/delete_server_tests.rb index a2f46bd83..08995b93e 100644 --- a/tests/rackspace/requests/servers/delete_server_tests.rb +++ b/tests/rackspace/requests/servers/delete_server_tests.rb @@ -6,7 +6,7 @@ Shindo.tests('Rackspace::Servers#delete_server', 'rackspace') do end test('has proper output format') do - wait_for { Rackspace[:servers].get_server_details(@server_id).body['server']['status'] == 'ACTIVE' } + Fog.wait_for { Rackspace[:servers].get_server_details(@server_id).body['server']['status'] == 'ACTIVE' } Rackspace[:servers].delete_server(@server_id) end diff --git a/tests/rackspace/requests/servers/get_server_details_tests.rb b/tests/rackspace/requests/servers/get_server_details_tests.rb index c83c6626a..e58dace69 100644 --- a/tests/rackspace/requests/servers/get_server_details_tests.rb +++ b/tests/rackspace/requests/servers/get_server_details_tests.rb @@ -7,7 +7,7 @@ Shindo.tests('Rackspace::Servers#get_server_details', 'rackspace') do end after do - wait_for { Rackspace[:servers].get_server_details(@server_id).body['server']['status'] == 'ACTIVE' } + Fog.wait_for { Rackspace[:servers].get_server_details(@server_id).body['server']['status'] == 'ACTIVE' } Rackspace[:servers].delete_server(@server_id) end diff --git a/tests/rackspace/requests/servers/list_addresses_tests.rb b/tests/rackspace/requests/servers/list_addresses_tests.rb index 4d72236af..d0075a4a5 100644 --- a/tests/rackspace/requests/servers/list_addresses_tests.rb +++ b/tests/rackspace/requests/servers/list_addresses_tests.rb @@ -7,7 +7,7 @@ Shindo.tests('Rackspace::Servers#list_addresses', 'rackspace') do end after do - wait_for { Rackspace[:servers].get_server_details(@server_id).body['server']['status'] == 'ACTIVE' } + Fog.wait_for { Rackspace[:servers].get_server_details(@server_id).body['server']['status'] == 'ACTIVE' } Rackspace[:servers].delete_server(@server_id) end diff --git a/tests/rackspace/requests/servers/list_private_addresses_tests.rb b/tests/rackspace/requests/servers/list_private_addresses_tests.rb index e5ba49709..ecc94f315 100644 --- a/tests/rackspace/requests/servers/list_private_addresses_tests.rb +++ b/tests/rackspace/requests/servers/list_private_addresses_tests.rb @@ -7,7 +7,7 @@ Shindo.tests('Rackspace::Servers#list_private_addresses', 'rackspace') do end after do - wait_for { Rackspace[:servers].get_server_details(@server_id).body['server']['status'] == 'ACTIVE' } + Fog.wait_for { Rackspace[:servers].get_server_details(@server_id).body['server']['status'] == 'ACTIVE' } Rackspace[:servers].delete_server(@server_id) end diff --git a/tests/rackspace/requests/servers/list_public_addresses_tests.rb b/tests/rackspace/requests/servers/list_public_addresses_tests.rb index 8d9c8173b..49ffea1cb 100644 --- a/tests/rackspace/requests/servers/list_public_addresses_tests.rb +++ b/tests/rackspace/requests/servers/list_public_addresses_tests.rb @@ -7,7 +7,7 @@ Shindo.tests('Rackspace::Servers#list_public_addresses', 'rackspace') do end after do - wait_for { Rackspace[:servers].get_server_details(@server_id).body['server']['status'] == 'ACTIVE' } + Fog.wait_for { Rackspace[:servers].get_server_details(@server_id).body['server']['status'] == 'ACTIVE' } Rackspace[:servers].delete_server(@server_id) end diff --git a/tests/rackspace/requests/servers/list_servers_detail_tests.rb b/tests/rackspace/requests/servers/list_servers_detail_tests.rb index 6732fe2ea..79b7daf4d 100644 --- a/tests/rackspace/requests/servers/list_servers_detail_tests.rb +++ b/tests/rackspace/requests/servers/list_servers_detail_tests.rb @@ -7,7 +7,7 @@ Shindo.tests('Rackspace::Servers#list_servers_detail', 'rackspace') do end after do - wait_for { Rackspace[:servers].get_server_details(@server_id).body['server']['status'] == 'ACTIVE' } + Fog.wait_for { Rackspace[:servers].get_server_details(@server_id).body['server']['status'] == 'ACTIVE' } Rackspace[:servers].delete_server(@server_id) end diff --git a/tests/rackspace/requests/servers/list_servers_tests.rb b/tests/rackspace/requests/servers/list_servers_tests.rb index 0ec41cab9..3d71a2377 100644 --- a/tests/rackspace/requests/servers/list_servers_tests.rb +++ b/tests/rackspace/requests/servers/list_servers_tests.rb @@ -7,7 +7,7 @@ Shindo.tests('Rackspace::Servers#list_servers', 'rackspace') do end after do - wait_for { Rackspace[:servers].get_server_details(@server_id).body['server']['status'] == 'ACTIVE' } + Fog.wait_for { Rackspace[:servers].get_server_details(@server_id).body['server']['status'] == 'ACTIVE' } Rackspace[:servers].delete_server(@server_id) end diff --git a/tests/rackspace/requests/servers/reboot_server_tests.rb b/tests/rackspace/requests/servers/reboot_server_tests.rb index 9c3fc3342..65c2d616a 100644 --- a/tests/rackspace/requests/servers/reboot_server_tests.rb +++ b/tests/rackspace/requests/servers/reboot_server_tests.rb @@ -6,20 +6,20 @@ Shindo.tests('Rackspace::Servers#reboot_server', 'rackspace') do end after do - wait_for { Rackspace[:servers].get_server_details(@server_id).body['server']['status'] == 'ACTIVE' } + Fog.wait_for { Rackspace[:servers].get_server_details(@server_id).body['server']['status'] == 'ACTIVE' } Rackspace[:servers].delete_server(@server_id) end tests('HARD') do test('has proper output format') do - wait_for { Rackspace[:servers].get_server_details(@server_id).body['server']['status'] == 'ACTIVE' } + Fog.wait_for { Rackspace[:servers].get_server_details(@server_id).body['server']['status'] == 'ACTIVE' } Rackspace[:servers].reboot_server(@server_id, 'HARD') end end tests('SOFT') do test('has proper output format') do - wait_for { Rackspace[:servers].get_server_details(@server_id).body['server']['status'] == 'ACTIVE' } + Fog.wait_for { Rackspace[:servers].get_server_details(@server_id).body['server']['status'] == 'ACTIVE' } Rackspace[:servers].reboot_server(@server_id, 'SOFT') end end diff --git a/tests/rackspace/requests/servers/update_server_tests.rb b/tests/rackspace/requests/servers/update_server_tests.rb index acbe1175d..e950369c9 100644 --- a/tests/rackspace/requests/servers/update_server_tests.rb +++ b/tests/rackspace/requests/servers/update_server_tests.rb @@ -3,7 +3,7 @@ Shindo.tests('Rackspace::Servers#update_server', 'rackspace') do before do @server_id = Rackspace[:servers].create_server(1, 3, 'fogupdateserver').body['server']['id'] - wait_for { Rackspace[:servers].get_server_details(@server_id).body['server']['status'] == 'ACTIVE' } + Fog.wait_for { Rackspace[:servers].get_server_details(@server_id).body['server']['status'] == 'ACTIVE' } @data = Rackspace[:servers].update_server(@server_id, :name => 'fogupdatedserver', :adminPass => 'fogupdatedserver') end diff --git a/tests/slicehost/requests/create_slice_tests.rb b/tests/slicehost/requests/create_slice_tests.rb index 8528606e9..2954721e2 100644 --- a/tests/slicehost/requests/create_slice_tests.rb +++ b/tests/slicehost/requests/create_slice_tests.rb @@ -7,7 +7,7 @@ Shindo.tests('Slicehost#create_slice', 'slicehost') do end after do - wait_for { Slicehost[:slices].get_slice(@id).body['status'] == 'active' } + Fog.wait_for { Slicehost[:slices].get_slice(@id).body['status'] == 'active' } Slicehost[:slices].delete_slice(@id) end diff --git a/tests/slicehost/requests/delete_slice_tests.rb b/tests/slicehost/requests/delete_slice_tests.rb index 1b1c15bfa..043b640cb 100644 --- a/tests/slicehost/requests/delete_slice_tests.rb +++ b/tests/slicehost/requests/delete_slice_tests.rb @@ -7,7 +7,7 @@ Shindo.tests('Slicehost#delete_slice', 'slicehost') do end test('has proper output format') do - wait_for { Slicehost[:slices].get_slice(@id).body['status'] == 'active' } + Fog.wait_for { Slicehost[:slices].get_slice(@id).body['status'] == 'active' } Slicehost[:slices].delete_slice(@id) end diff --git a/tests/slicehost/requests/get_slice_tests.rb b/tests/slicehost/requests/get_slice_tests.rb index 1cddb8e53..78346ad05 100644 --- a/tests/slicehost/requests/get_slice_tests.rb +++ b/tests/slicehost/requests/get_slice_tests.rb @@ -8,7 +8,7 @@ Shindo.tests('Slicehost#get_slice', 'slicehost') do end after do - wait_for { Slicehost[:slices].get_slice(@id).body['status'] == 'active' } + Fog.wait_for { Slicehost[:slices].get_slice(@id).body['status'] == 'active' } Slicehost[:slices].delete_slice(@id) end diff --git a/tests/slicehost/requests/reboot_slice_tests.rb b/tests/slicehost/requests/reboot_slice_tests.rb index 56a86f332..12ac20a47 100644 --- a/tests/slicehost/requests/reboot_slice_tests.rb +++ b/tests/slicehost/requests/reboot_slice_tests.rb @@ -4,12 +4,12 @@ Shindo.tests('Slicehost#reboot_slice', 'slicehost') do before do @data = Slicehost[:slices].create_slice(1, 3, 'fogrebootslice').body @id = @data['id'] - wait_for { Slicehost[:slices].get_slice(@id).body['status'] == 'active' } + Fog.wait_for { Slicehost[:slices].get_slice(@id).body['status'] == 'active' } @data = Slicehost[:slices].reboot_slice(@id).body end after do - wait_for { Slicehost[:slices].get_slice(@id).body['status'] == 'active' } + Fog.wait_for { Slicehost[:slices].get_slice(@id).body['status'] == 'active' } Slicehost[:slices].delete_slice(@id) end