Skip to content

Commit

Permalink
Merge pull request #131 from engineyard/fix-address-limit-error
Browse files Browse the repository at this point in the history
raise correct error when exceeding address limit
  • Loading branch information
geemus committed Jun 12, 2015
2 parents 916351a + df88766 commit 22a6deb
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 39 deletions.
40 changes: 21 additions & 19 deletions lib/fog/aws/requests/compute/allocate_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,29 @@ def allocate_address(domain='standard')

class Mock
def allocate_address(domain = 'standard')
domain = domain == 'vpc' ? 'vpc' : 'standard'
unless describe_addresses.body['addressesSet'].size < self.data[:limits][:addresses]
raise Fog::Compute::AWS::Error, "AddressLimitExceeded => Too many addresses allocated"
end

response = Excon::Response.new
if describe_addresses.body['addressesSet'].size < self.data[:limits][:addresses]
response.status = 200
public_ip = Fog::AWS::Mock.ip_address
data = {
'instanceId' => nil,
'publicIp' => public_ip,
'domain' => domain
}
if domain == 'vpc'
data['allocationId'] = "eipalloc-#{Fog::Mock.random_hex(8)}"
end
self.data[:addresses][public_ip] = data
response.body = data.reject {|k, v| k == 'instanceId' }.merge('requestId' => Fog::AWS::Mock.request_id)
response
else
response.status = 400
response.body = "<?xml version=\"1.0\"?><Response><Errors><Error><Code>AddressLimitExceeded</Code><Message>Too many addresses allocated</Message></Error></Errors><RequestID>#{Fog::AWS::Mock.request_id}</RequestID></Response>"
raise(Excon::Errors.status_error({:expects => 200}, response))
response.status = 200

domain = domain == 'vpc' ? 'vpc' : 'standard'
public_ip = Fog::AWS::Mock.ip_address

data = {
'instanceId' => nil,
'publicIp' => public_ip,
'domain' => domain
}

if domain == 'vpc'
data['allocationId'] = "eipalloc-#{Fog::Mock.random_hex(8)}"
end

self.data[:addresses][public_ip] = data
response.body = data.reject {|k, v| k == 'instanceId' }.merge('requestId' => Fog::AWS::Mock.request_id)
response
end
end
end
Expand Down
53 changes: 33 additions & 20 deletions tests/requests/compute/address_tests.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Shindo.tests('Fog::Compute[:aws] | address requests', ['aws']) do
compute = Fog::Compute[:aws]

@addresses_format = {
'addressesSet' => [{
Expand All @@ -10,7 +11,7 @@
}],
'requestId' => String
}
@server = Fog::Compute[:aws].servers.create
@server = compute.servers.create
@server.wait_for { ready? }
@ip_address = @server.public_ip_address

Expand All @@ -21,81 +22,93 @@
@vpc_allocation_id = nil

tests('#allocate_address').formats({'domain' => String, 'publicIp' => String, 'requestId' => String}) do
data = Fog::Compute[:aws].allocate_address.body
data = compute.allocate_address.body
@public_ip = data['publicIp']
data
end

tests("#allocate_address('vpc')").formats({'domain' => String, 'publicIp' => String, 'allocationId' => String, 'requestId' => String}) do
data = Fog::Compute[:aws].allocate_address('vpc').body
data = compute.allocate_address('vpc').body
@vpc_public_ip = data['publicIp']
@vpc_allocation_id = data['allocationId']
data
end

tests('#describe_addresses').formats(@addresses_format) do
Fog::Compute[:aws].describe_addresses.body
compute.describe_addresses.body
end

tests("#describe_addresses('public-ip' => #{@public_ip}')").formats(@addresses_format) do
Fog::Compute[:aws].describe_addresses('public-ip' => @public_ip).body
compute.describe_addresses('public-ip' => @public_ip).body
end

tests("#associate_addresses('#{@server.identity}', '#{@public_ip}')").formats(AWS::Compute::Formats::BASIC) do
Fog::Compute[:aws].associate_address(@server.identity, @public_ip).body
compute.associate_address(@server.identity, @public_ip).body
end

tests("#associate_addresses({:instance_id=>'#{@server.identity}', :public_ip=>'#{@public_ip}'})").formats(AWS::Compute::Formats::BASIC) do
Fog::Compute[:aws].associate_address({:instance_id=>@server.identity,:public_ip=> @public_ip}).body
compute.associate_address({:instance_id=>@server.identity,:public_ip=> @public_ip}).body
end

tests("#dissassociate_address('#{@public_ip}')").formats(AWS::Compute::Formats::BASIC) do
Fog::Compute[:aws].disassociate_address(@public_ip).body
compute.disassociate_address(@public_ip).body
end

tests("#associate_addresses('#{@server.id}', nil, nil, '#{@vpc_allocation_id}')").formats(AWS::Compute::Formats::BASIC) do
Fog::Compute[:aws].associate_address(@server.id, nil, nil, @vpc_allocation_id).body
compute.associate_address(@server.id, nil, nil, @vpc_allocation_id).body
end

tests("#associate_addresses({:instance_id=>'#{@server.id}', :allocation_id=>'#{@vpc_allocation_id}'})").formats(AWS::Compute::Formats::BASIC) do
Fog::Compute[:aws].associate_address({:instance_id=>@server.id, :allocation_id=>@vpc_allocation_id}).body
compute.associate_address({:instance_id=>@server.id, :allocation_id=>@vpc_allocation_id}).body
end

tests("#release_address('#{@public_ip}')").formats(AWS::Compute::Formats::BASIC) do
Fog::Compute[:aws].release_address(@public_ip).body
compute.release_address(@public_ip).body
end

tests("#release_address('#{@vpc_allocation_id}')").formats(AWS::Compute::Formats::BASIC) do
Fog::Compute[:aws].release_address(@vpc_allocation_id).body
compute.release_address(@vpc_allocation_id).body
end
end

tests('failure') do

@address = Fog::Compute[:aws].addresses.create
@vpc_address = Fog::Compute[:aws].addresses.create(:domain => 'vpc')
@address = compute.addresses.create
@vpc_address = compute.addresses.create(:domain => 'vpc')

tests("#associate_addresses({:instance_id =>'i-00000000', :public_ip => '#{@address.identity}')}").raises(Fog::Compute::AWS::NotFound) do
Fog::Compute[:aws].associate_address({:instance_id => 'i-00000000', :public_ip => @address.identity})
compute.associate_address({:instance_id => 'i-00000000', :public_ip => @address.identity})
end

tests("#associate_addresses({:instance_id =>'#{@server.identity}', :public_ip => '127.0.0.1'})").raises(Fog::Compute::AWS::Error) do
Fog::Compute[:aws].associate_address({:instance_id => @server.identity, :public_ip => '127.0.0.1'})
compute.associate_address({:instance_id => @server.identity, :public_ip => '127.0.0.1'})
end

tests("#associate_addresses({:instance_id =>'i-00000000', :public_ip => '127.0.0.1'})").raises(Fog::Compute::AWS::NotFound) do
Fog::Compute[:aws].associate_address({:instance_id =>'i-00000000', :public_ip =>'127.0.0.1'})
compute.associate_address({:instance_id =>'i-00000000', :public_ip =>'127.0.0.1'})
end

tests("#disassociate_addresses('127.0.0.1') raises BadRequest error").raises(Fog::Compute::AWS::Error) do
Fog::Compute[:aws].disassociate_address('127.0.0.1')
compute.disassociate_address('127.0.0.1')
end

tests("#release_address('127.0.0.1')").raises(Fog::Compute::AWS::Error) do
Fog::Compute[:aws].release_address('127.0.0.1')
compute.release_address('127.0.0.1')
end

tests("#release_address('#{@vpc_address.identity}')").raises(Fog::Compute::AWS::Error) do
Fog::Compute[:aws].release_address(@vpc_address.identity)
compute.release_address(@vpc_address.identity)
end

if Fog.mocking?
old_limit = compute.data[:limits][:addresses]

tests("#allocate_address", "limit exceeded").raises(Fog::Compute::AWS::Error) do
compute.data[:limits][:addresses] = 0
compute.allocate_address
end

compute.data[:limits][:addresses] = old_limit
end

@address.destroy
Expand Down

0 comments on commit 22a6deb

Please sign in to comment.