diff --git a/lib/fog/model.rb b/lib/fog/model.rb index f60940f7a..9bfe2204d 100644 --- a/lib/fog/model.rb +++ b/lib/fog/model.rb @@ -97,6 +97,17 @@ module Fog end end + def wait_for(timeout = 600, &block) + start = Time.now + until instance_eval(&block) + if Time.now - start > timeout + break + end + reload + sleep(1) + end + end + private def collection=(new_collection) diff --git a/spec/aws/models/ec2/address_spec.rb b/spec/aws/models/ec2/address_spec.rb index 6b0ae9807..bb17eec2c 100644 --- a/spec/aws/models/ec2/address_spec.rb +++ b/spec/aws/models/ec2/address_spec.rb @@ -52,9 +52,7 @@ describe 'Fog::AWS::EC2::Address' do it "should associate with instance to an already saved address" do @address.save.should be_true - while @instance.state == 'pending' - @instance.reload - end + @instance.wait_for { state == 'running' } @address.instance = @instance @address.instance_id.should == @instance.id end diff --git a/spec/aws/models/ec2/volume_spec.rb b/spec/aws/models/ec2/volume_spec.rb index 74bb280e5..c332983b1 100644 --- a/spec/aws/models/ec2/volume_spec.rb +++ b/spec/aws/models/ec2/volume_spec.rb @@ -49,21 +49,15 @@ describe 'Fog::AWS::EC2::Volume' do before(:each) do @instance = ec2.instances.create(:image_id => GENTOO_AMI) @volume = ec2.volumes.new(:availability_zone => @instance.availability_zone, :size => 1, :device => '/dev/sdz1') - while @instance.state == 'pending' - @instance.reload - end + @instance.wait_for { state == 'running' } end after(:each) do @instance.destroy if @volume.id - while ['attaching', 'creating'].include?(@volume.status) - @volume.reload - end + @volume.wait_for { status == 'attached' } @volume.instance = nil - while ['attached', 'detaching'].include?(@volume.status) - @volume.reload - end + @volume.wait_for { status == 'available' } @volume.destroy end end