From 0953ddde51a89ffc1342ab30548f79705f4ba854 Mon Sep 17 00:00:00 2001 From: Nuwan Goonasekera Date: Sat, 16 Sep 2017 12:26:43 +0530 Subject: [PATCH] Fix for instance public ip not being cleared on eip disassociation --- moto/ec2/models.py | 9 +-------- tests/test_ec2/test_elastic_ip_addresses.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/moto/ec2/models.py b/moto/ec2/models.py index 21d9a1e36..545a9a3a3 100755 --- a/moto/ec2/models.py +++ b/moto/ec2/models.py @@ -590,10 +590,6 @@ class Instance(TaggedEC2Resource, BotoInstance): self.attach_eni(use_nic, device_index) - def set_ip(self, ip_address): - # Should we be creating a new ENI? - self.nics[0].public_ip = ip_address - def attach_eni(self, eni, device_index): device_index = int(device_index) self.nics[device_index] = eni @@ -3047,8 +3043,6 @@ class ElasticAddressBackend(object): eip.eni.public_ip = eip.public_ip if eip.domain == "vpc": eip.association_id = random_eip_association_id() - if instance: - instance.set_ip(eip.public_ip) return eip @@ -3082,10 +3076,9 @@ class ElasticAddressBackend(object): eip = eips[0] if eip.eni: + eip.eni.public_ip = None if eip.eni.instance and eip.eni.instance._state.name == "running": eip.eni.check_auto_public_ip() - else: - eip.eni.public_ip = None eip.eni = None eip.instance = None diff --git a/tests/test_ec2/test_elastic_ip_addresses.py b/tests/test_ec2/test_elastic_ip_addresses.py index 824c9402c..709bdc33b 100644 --- a/tests/test_ec2/test_elastic_ip_addresses.py +++ b/tests/test_ec2/test_elastic_ip_addresses.py @@ -180,13 +180,31 @@ def test_eip_boto3_vpc_association(): 'SubnetId': subnet_res['Subnet']['SubnetId'] })[0] allocation_id = client.allocate_address(Domain='vpc')['AllocationId'] + address = service.VpcAddress(allocation_id) + address.load() + address.association_id.should.be.none + address.instance_id.should.be.empty + address.network_interface_id.should.be.empty association_id = client.associate_address( InstanceId=instance.id, AllocationId=allocation_id, AllowReassociation=False) instance.load() + address.reload() + address.association_id.should_not.be.none instance.public_ip_address.should_not.be.none instance.public_dns_name.should_not.be.none + address.network_interface_id.should.equal(instance.network_interfaces_attribute[0].get('NetworkInterfaceId')) + address.public_ip.should.equal(instance.public_ip_address) + address.instance_id.should.equal(instance.id) + + client.disassociate_address(AssociationId=address.association_id) + instance.reload() + address.reload() + instance.public_ip_address.should.be.none + address.network_interface_id.should.be.empty + address.association_id.should.be.none + address.instance_id.should.be.empty @mock_ec2_deprecated