diff --git a/lib/fog/aws/models/compute/volume.rb b/lib/fog/aws/models/compute/volume.rb index 2bebdd42d..36c7972a4 100644 --- a/lib/fog/aws/models/compute/volume.rb +++ b/lib/fog/aws/models/compute/volume.rb @@ -87,7 +87,7 @@ module Fog @server = nil self.server_id = nil unless new_record? - connection.detach_volume(@id) + connection.detach_volume(id) reload end end @@ -96,7 +96,7 @@ module Fog @server = nil self.server_id = nil unless new_record? - connection.detach_volume(@id, 'Force' => true) + connection.detach_volume(id, 'Force' => true) reload end end diff --git a/spec/aws/models/compute/volume_spec.rb b/spec/aws/models/compute/volume_spec.rb index b99975d3e..e041cd91f 100644 --- a/spec/aws/models/compute/volume_spec.rb +++ b/spec/aws/models/compute/volume_spec.rb @@ -67,42 +67,64 @@ describe 'Fog::AWS::Compute::Volume' do @volume = AWS[:compute].volumes.new(:availability_zone => @server.availability_zone, :size => 1, :device => '/dev/sdz1') end - after(:each) do - if @volume.id - @volume.wait_for { state == 'in-use' } - @volume.server = nil - @volume.wait_for { ready? } + describe "when set to a server" do + after(:each) do + if @volume.id + @volume.wait_for { state == 'in-use' } + @volume.server = nil + @volume.wait_for { ready? } + @volume.destroy + end + end + + it "should not attach to server if the volume has not been saved" do + @volume.server = @server + @volume.server_id.should_not == @server.id + end + + it "should change the availability_zone if the volume has not been saved" do + @volume.server = @server + @volume.availability_zone.should == @server.availability_zone + end + + it "should attach to server when the volume is saved" do + @volume.server = @server + @volume.save.should be_true + @volume.server_id.should == @server.id + end + + it "should attach to server to an already saved volume" do + @volume.save.should be_true + @volume.server = @server + @volume.server_id.should == @server.id + end + + it "should not change the availability_zone if the volume has been saved" do + @volume.save.should be_true + @volume.server = @server + @volume.availability_zone.should == @server.availability_zone + end + + end + + describe "when set to nil" do + after(:each) do @volume.destroy end + + it "should detach from server if the volume has been saved" do + @volume.server = @server + @volume.save.should be_true + @server.reload.volumes.map(&:id).should include(@volume.id) + + @volume.server = nil + @volume.wait_for { ready? } + @volume.server_id.should be_nil + @server.reload.volumes.map(&:id).should_not include(@volume.id) + end + end - it "should not attach to server if the volume has not been saved" do - @volume.server = @server - @volume.server_id.should_not == @server.id - end - - it "should change the availability_zone if the volume has not been saved" do - @volume.server = @server - @volume.availability_zone.should == @server.availability_zone - end - - it "should attach to server when the volume is saved" do - @volume.server = @server - @volume.save.should be_true - @volume.server_id.should == @server.id - end - - it "should attach to server to an already saved volume" do - @volume.save.should be_true - @volume.server = @server - @volume.server_id.should == @server.id - end - - it "should not change the availability_zone if the volume has been saved" do - @volume.save.should be_true - @volume.server = @server - @volume.availability_zone.should == @server.availability_zone - end end describe "#reload" do