| 
									
										
										
										
											2014-08-27 11:17:06 -04:00
										 |  |  | from __future__ import unicode_literals | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  | # Ensure 'pytest.raises' context manager support for Python 2.6 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  | import pytest | 
					
						
							| 
									
										
										
										
											2014-08-25 15:09:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-21 23:13:01 -05:00
										 |  |  | import boto | 
					
						
							| 
									
										
										
										
											2016-10-12 22:02:06 -04:00
										 |  |  | import boto3 | 
					
						
							| 
									
										
										
										
											2017-02-23 19:43:48 -05:00
										 |  |  | from boto.exception import EC2ResponseError | 
					
						
							| 
									
										
										
										
											2014-08-26 13:25:50 -04:00
										 |  |  | import six | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-03 17:21:25 -04:00
										 |  |  | import sure  # noqa | 
					
						
							| 
									
										
										
										
											2013-02-21 23:13:01 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | from moto import mock_ec2, mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2013-02-21 23:13:01 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  | import logging | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  | def test_eip_allocate_classic(): | 
					
						
							|  |  |  |     """Allocate/release Classic EIP""" | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_ec2("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as ex: | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  |         standard = conn.allocate_address(dry_run=True) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     ex.value.error_code.should.equal("DryRunOperation") | 
					
						
							|  |  |  |     ex.value.status.should.equal(400) | 
					
						
							|  |  |  |     ex.value.message.should.equal( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "An error occurred (DryRunOperation) when calling the AllocateAddress operation: Request would have succeeded, but DryRun flag is set" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  |     standard = conn.allocate_address() | 
					
						
							|  |  |  |     standard.should.be.a(boto.ec2.address.Address) | 
					
						
							| 
									
										
										
										
											2014-08-26 13:25:50 -04:00
										 |  |  |     standard.public_ip.should.be.a(six.text_type) | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  |     standard.instance_id.should.be.none | 
					
						
							|  |  |  |     standard.domain.should.be.equal("standard") | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as ex: | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  |         standard.release(dry_run=True) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     ex.value.error_code.should.equal("DryRunOperation") | 
					
						
							|  |  |  |     ex.value.status.should.equal(400) | 
					
						
							|  |  |  |     ex.value.message.should.equal( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "An error occurred (DryRunOperation) when calling the ReleaseAddress operation: Request would have succeeded, but DryRun flag is set" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  |     standard.release() | 
					
						
							|  |  |  |     standard.should_not.be.within(conn.get_all_addresses()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  | def test_eip_allocate_vpc(): | 
					
						
							|  |  |  |     """Allocate/release VPC EIP""" | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_ec2("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as ex: | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  |         vpc = conn.allocate_address(domain="vpc", dry_run=True) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     ex.value.error_code.should.equal("DryRunOperation") | 
					
						
							|  |  |  |     ex.value.status.should.equal(400) | 
					
						
							|  |  |  |     ex.value.message.should.equal( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "An error occurred (DryRunOperation) when calling the AllocateAddress operation: Request would have succeeded, but DryRun flag is set" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  |     vpc = conn.allocate_address(domain="vpc") | 
					
						
							|  |  |  |     vpc.should.be.a(boto.ec2.address.Address) | 
					
						
							|  |  |  |     vpc.domain.should.be.equal("vpc") | 
					
						
							|  |  |  |     logging.debug("vpc alloc_id:".format(vpc.allocation_id)) | 
					
						
							|  |  |  |     vpc.release() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 23:39:19 -07:00
										 |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_specific_eip_allocate_vpc(): | 
					
						
							|  |  |  |     """Allocate VPC EIP with specific address""" | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     service = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							|  |  |  |     client = boto3.client("ec2", region_name="us-west-1") | 
					
						
							| 
									
										
										
										
											2018-05-10 23:39:19 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     vpc = client.allocate_address(Domain="vpc", Address="127.38.43.222") | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc["Domain"].should.be.equal("vpc") | 
					
						
							|  |  |  |     vpc["PublicIp"].should.be.equal("127.38.43.222") | 
					
						
							|  |  |  |     logging.debug("vpc alloc_id:".format(vpc["AllocationId"])) | 
					
						
							| 
									
										
										
										
											2018-05-10 23:39:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  | def test_eip_allocate_invalid_domain(): | 
					
						
							|  |  |  |     """Allocate EIP invalid domain""" | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_ec2("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2014-08-25 10:54:47 -07:00
										 |  |  |         conn.allocate_address(domain="bogus") | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.code.should.equal("InvalidParameterValue") | 
					
						
							|  |  |  |     cm.value.status.should.equal(400) | 
					
						
							|  |  |  |     cm.value.request_id.should_not.be.none | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-21 23:13:01 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  | def test_eip_associate_classic(): | 
					
						
							|  |  |  |     """Associate/Disassociate EIP to classic instance""" | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_ec2("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     reservation = conn.run_instances("ami-1234abcd") | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  |     instance = reservation.instances[0] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     eip = conn.allocate_address() | 
					
						
							|  |  |  |     eip.instance_id.should.be.none | 
					
						
							| 
									
										
										
										
											2014-08-25 10:54:47 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2014-08-25 10:54:47 -07:00
										 |  |  |         conn.associate_address(public_ip=eip.public_ip) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.code.should.equal("MissingParameter") | 
					
						
							|  |  |  |     cm.value.status.should.equal(400) | 
					
						
							|  |  |  |     cm.value.request_id.should_not.be.none | 
					
						
							| 
									
										
										
										
											2014-08-25 10:54:47 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as ex: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         conn.associate_address( | 
					
						
							|  |  |  |             instance_id=instance.id, public_ip=eip.public_ip, dry_run=True | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     ex.value.error_code.should.equal("DryRunOperation") | 
					
						
							|  |  |  |     ex.value.status.should.equal(400) | 
					
						
							|  |  |  |     ex.value.message.should.equal( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "An error occurred (DryRunOperation) when calling the AssociateAddress operation: Request would have succeeded, but DryRun flag is set" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  |     conn.associate_address(instance_id=instance.id, public_ip=eip.public_ip) | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     # no .update() on address ): | 
					
						
							|  |  |  |     eip = conn.get_all_addresses(addresses=[eip.public_ip])[0] | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  |     eip.instance_id.should.be.equal(instance.id) | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as ex: | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  |         conn.disassociate_address(public_ip=eip.public_ip, dry_run=True) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     ex.value.error_code.should.equal("DryRunOperation") | 
					
						
							|  |  |  |     ex.value.status.should.equal(400) | 
					
						
							|  |  |  |     ex.value.message.should.equal( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "An error occurred (DryRunOperation) when calling the DisAssociateAddress operation: Request would have succeeded, but DryRun flag is set" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  |     conn.disassociate_address(public_ip=eip.public_ip) | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     # no .update() on address ): | 
					
						
							|  |  |  |     eip = conn.get_all_addresses(addresses=[eip.public_ip])[0] | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     eip.instance_id.should.be.equal("") | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  |     eip.release() | 
					
						
							|  |  |  |     eip.should_not.be.within(conn.get_all_addresses()) | 
					
						
							|  |  |  |     eip = None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     instance.terminate() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  | def test_eip_associate_vpc(): | 
					
						
							|  |  |  |     """Associate/Disassociate EIP to VPC instance""" | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_ec2("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     reservation = conn.run_instances("ami-1234abcd") | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  |     instance = reservation.instances[0] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     eip = conn.allocate_address(domain="vpc") | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  |     eip.instance_id.should.be.none | 
					
						
							| 
									
										
										
										
											2014-08-25 10:54:47 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2014-08-25 10:54:47 -07:00
										 |  |  |         conn.associate_address(allocation_id=eip.allocation_id) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.code.should.equal("MissingParameter") | 
					
						
							|  |  |  |     cm.value.status.should.equal(400) | 
					
						
							|  |  |  |     cm.value.request_id.should_not.be.none | 
					
						
							| 
									
										
										
										
											2014-08-25 10:54:47 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn.associate_address(instance_id=instance.id, allocation_id=eip.allocation_id) | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     # no .update() on address ): | 
					
						
							|  |  |  |     eip = conn.get_all_addresses(addresses=[eip.public_ip])[0] | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  |     eip.instance_id.should.be.equal(instance.id) | 
					
						
							|  |  |  |     conn.disassociate_address(association_id=eip.association_id) | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     # no .update() on address ): | 
					
						
							|  |  |  |     eip = conn.get_all_addresses(addresses=[eip.public_ip])[0] | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     eip.instance_id.should.be.equal("") | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  |     eip.association_id.should.be.none | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as ex: | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  |         eip.release(dry_run=True) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     ex.value.error_code.should.equal("DryRunOperation") | 
					
						
							|  |  |  |     ex.value.status.should.equal(400) | 
					
						
							|  |  |  |     ex.value.message.should.equal( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "An error occurred (DryRunOperation) when calling the ReleaseAddress operation: Request would have succeeded, but DryRun flag is set" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  |     eip.release() | 
					
						
							|  |  |  |     eip = None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     instance.terminate() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 22:02:06 -04:00
										 |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_eip_boto3_vpc_association(): | 
					
						
							|  |  |  |     """Associate EIP to VPC instance in a new subnet with boto3""" | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     service = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							|  |  |  |     client = boto3.client("ec2", region_name="us-west-1") | 
					
						
							|  |  |  |     vpc_res = client.create_vpc(CidrBlock="10.0.0.0/24") | 
					
						
							| 
									
										
										
										
											2016-10-12 22:02:06 -04:00
										 |  |  |     subnet_res = client.create_subnet( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         VpcId=vpc_res["Vpc"]["VpcId"], CidrBlock="10.0.0.0/24" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     instance = service.create_instances( | 
					
						
							|  |  |  |         **{ | 
					
						
							|  |  |  |             "InstanceType": "t2.micro", | 
					
						
							|  |  |  |             "ImageId": "ami-test", | 
					
						
							|  |  |  |             "MinCount": 1, | 
					
						
							|  |  |  |             "MaxCount": 1, | 
					
						
							|  |  |  |             "SubnetId": subnet_res["Subnet"]["SubnetId"], | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     )[0] | 
					
						
							|  |  |  |     allocation_id = client.allocate_address(Domain="vpc")["AllocationId"] | 
					
						
							| 
									
										
										
										
											2017-09-16 12:26:43 +05:30
										 |  |  |     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 | 
					
						
							| 
									
										
										
										
											2016-10-12 22:02:06 -04:00
										 |  |  |     association_id = client.associate_address( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         InstanceId=instance.id, AllocationId=allocation_id, AllowReassociation=False | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-10-12 22:02:06 -04:00
										 |  |  |     instance.load() | 
					
						
							| 
									
										
										
										
											2017-09-16 12:26:43 +05:30
										 |  |  |     address.reload() | 
					
						
							|  |  |  |     address.association_id.should_not.be.none | 
					
						
							| 
									
										
										
										
											2016-10-12 22:02:06 -04:00
										 |  |  |     instance.public_ip_address.should_not.be.none | 
					
						
							|  |  |  |     instance.public_dns_name.should_not.be.none | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     address.network_interface_id.should.equal( | 
					
						
							|  |  |  |         instance.network_interfaces_attribute[0].get("NetworkInterfaceId") | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2017-09-16 12:26:43 +05:30
										 |  |  |     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 | 
					
						
							| 
									
										
										
										
											2016-10-12 22:02:06 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  | def test_eip_associate_network_interface(): | 
					
						
							|  |  |  |     """Associate/Disassociate EIP to NIC""" | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_vpc("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  |     vpc = conn.create_vpc("10.0.0.0/16") | 
					
						
							|  |  |  |     subnet = conn.create_subnet(vpc.id, "10.0.0.0/18") | 
					
						
							|  |  |  |     eni = conn.create_network_interface(subnet.id) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     eip = conn.allocate_address(domain="vpc") | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  |     eip.network_interface_id.should.be.none | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  |         conn.associate_address(network_interface_id=eni.id) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.code.should.equal("MissingParameter") | 
					
						
							|  |  |  |     cm.value.status.should.equal(400) | 
					
						
							|  |  |  |     cm.value.request_id.should_not.be.none | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn.associate_address(network_interface_id=eni.id, allocation_id=eip.allocation_id) | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     # no .update() on address ): | 
					
						
							|  |  |  |     eip = conn.get_all_addresses(addresses=[eip.public_ip])[0] | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  |     eip.network_interface_id.should.be.equal(eni.id) | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  |     conn.disassociate_address(association_id=eip.association_id) | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     # no .update() on address ): | 
					
						
							|  |  |  |     eip = conn.get_all_addresses(addresses=[eip.public_ip])[0] | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     eip.network_interface_id.should.be.equal("") | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  |     eip.association_id.should.be.none | 
					
						
							|  |  |  |     eip.release() | 
					
						
							|  |  |  |     eip = None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  | def test_eip_reassociate(): | 
					
						
							|  |  |  |     """reassociate EIP""" | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_ec2("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     reservation = conn.run_instances("ami-1234abcd", min_count=2) | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  |     instance1, instance2 = reservation.instances | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     eip = conn.allocate_address() | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  |     conn.associate_address(instance_id=instance1.id, public_ip=eip.public_ip) | 
					
						
							| 
									
										
										
										
											2014-08-25 10:54:47 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  |     # Same ID is idempotent | 
					
						
							|  |  |  |     conn.associate_address(instance_id=instance1.id, public_ip=eip.public_ip) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Different ID detects resource association | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |         conn.associate_address( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             instance_id=instance2.id, public_ip=eip.public_ip, allow_reassociation=False | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.code.should.equal("Resource.AlreadyAssociated") | 
					
						
							|  |  |  |     cm.value.status.should.equal(400) | 
					
						
							|  |  |  |     cm.value.request_id.should_not.be.none | 
					
						
							| 
									
										
										
										
											2014-08-25 10:54:47 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     conn.associate_address.when.called_with( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         instance_id=instance2.id, public_ip=eip.public_ip, allow_reassociation=True | 
					
						
							|  |  |  |     ).should_not.throw(EC2ResponseError) | 
					
						
							| 
									
										
										
										
											2014-08-25 10:54:47 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  |     eip.release() | 
					
						
							|  |  |  |     eip = None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  |     instance1.terminate() | 
					
						
							|  |  |  |     instance2.terminate() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  | def test_eip_reassociate_nic(): | 
					
						
							|  |  |  |     """reassociate EIP""" | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_vpc("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     vpc = conn.create_vpc("10.0.0.0/16") | 
					
						
							|  |  |  |     subnet = conn.create_subnet(vpc.id, "10.0.0.0/18") | 
					
						
							|  |  |  |     eni1 = conn.create_network_interface(subnet.id) | 
					
						
							|  |  |  |     eni2 = conn.create_network_interface(subnet.id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     eip = conn.allocate_address() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn.associate_address(network_interface_id=eni1.id, public_ip=eip.public_ip) | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Same ID is idempotent | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn.associate_address(network_interface_id=eni1.id, public_ip=eip.public_ip) | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Different ID detects resource association | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         conn.associate_address(network_interface_id=eni2.id, public_ip=eip.public_ip) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.code.should.equal("Resource.AlreadyAssociated") | 
					
						
							|  |  |  |     cm.value.status.should.equal(400) | 
					
						
							|  |  |  |     cm.value.request_id.should_not.be.none | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     conn.associate_address.when.called_with( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         network_interface_id=eni2.id, public_ip=eip.public_ip, allow_reassociation=True | 
					
						
							|  |  |  |     ).should_not.throw(EC2ResponseError) | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     eip.release() | 
					
						
							|  |  |  |     eip = None | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  | def test_eip_associate_invalid_args(): | 
					
						
							|  |  |  |     """Associate EIP, invalid args """ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_ec2("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     reservation = conn.run_instances("ami-1234abcd") | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  |     instance = reservation.instances[0] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     eip = conn.allocate_address() | 
					
						
							| 
									
										
										
										
											2014-08-25 10:54:47 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2014-08-25 10:54:47 -07:00
										 |  |  |         conn.associate_address(instance_id=instance.id) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.code.should.equal("MissingParameter") | 
					
						
							|  |  |  |     cm.value.status.should.equal(400) | 
					
						
							|  |  |  |     cm.value.request_id.should_not.be.none | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     instance.terminate() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  | def test_eip_disassociate_bogus_association(): | 
					
						
							|  |  |  |     """Disassociate bogus EIP""" | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_ec2("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2014-08-25 10:54:47 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2014-08-25 10:54:47 -07:00
										 |  |  |         conn.disassociate_address(association_id="bogus") | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.code.should.equal("InvalidAssociationID.NotFound") | 
					
						
							|  |  |  |     cm.value.status.should.equal(400) | 
					
						
							|  |  |  |     cm.value.request_id.should_not.be.none | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  | def test_eip_release_bogus_eip(): | 
					
						
							|  |  |  |     """Release bogus EIP""" | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_ec2("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2014-08-25 10:54:47 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2014-08-25 10:54:47 -07:00
										 |  |  |         conn.release_address(allocation_id="bogus") | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.code.should.equal("InvalidAllocationID.NotFound") | 
					
						
							|  |  |  |     cm.value.status.should.equal(400) | 
					
						
							|  |  |  |     cm.value.request_id.should_not.be.none | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  | def test_eip_disassociate_arg_error(): | 
					
						
							|  |  |  |     """Invalid arguments disassociate address""" | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_ec2("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2014-08-25 10:54:47 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2014-08-25 10:54:47 -07:00
										 |  |  |         conn.disassociate_address() | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.code.should.equal("MissingParameter") | 
					
						
							|  |  |  |     cm.value.status.should.equal(400) | 
					
						
							|  |  |  |     cm.value.request_id.should_not.be.none | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  | def test_eip_release_arg_error(): | 
					
						
							|  |  |  |     """Invalid arguments release address""" | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_ec2("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2014-08-25 10:54:47 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2014-08-25 10:54:47 -07:00
										 |  |  |         conn.release_address() | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.code.should.equal("MissingParameter") | 
					
						
							|  |  |  |     cm.value.status.should.equal(400) | 
					
						
							|  |  |  |     cm.value.request_id.should_not.be.none | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  | def test_eip_describe(): | 
					
						
							|  |  |  |     """Listing of allocated Elastic IP Addresses.""" | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_ec2("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  |     eips = [] | 
					
						
							|  |  |  |     number_of_classic_ips = 2 | 
					
						
							|  |  |  |     number_of_vpc_ips = 2 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     # allocate some IPs | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  |     for _ in range(number_of_classic_ips): | 
					
						
							|  |  |  |         eips.append(conn.allocate_address()) | 
					
						
							|  |  |  |     for _ in range(number_of_vpc_ips): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         eips.append(conn.allocate_address(domain="vpc")) | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  |     len(eips).should.be.equal(number_of_classic_ips + number_of_vpc_ips) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Can we find each one individually? | 
					
						
							|  |  |  |     for eip in eips: | 
					
						
							|  |  |  |         if eip.allocation_id: | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |             lookup_addresses = conn.get_all_addresses( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |                 allocation_ids=[eip.allocation_id] | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             lookup_addresses = conn.get_all_addresses(addresses=[eip.public_ip]) | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  |         len(lookup_addresses).should.be.equal(1) | 
					
						
							|  |  |  |         lookup_addresses[0].public_ip.should.be.equal(eip.public_ip) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Can we find first two when we search for them? | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     lookup_addresses = conn.get_all_addresses( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         addresses=[eips[0].public_ip, eips[1].public_ip] | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  |     len(lookup_addresses).should.be.equal(2) | 
					
						
							|  |  |  |     lookup_addresses[0].public_ip.should.be.equal(eips[0].public_ip) | 
					
						
							|  |  |  |     lookup_addresses[1].public_ip.should.be.equal(eips[1].public_ip) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     # Release all IPs | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  |     for eip in eips: | 
					
						
							|  |  |  |         eip.release() | 
					
						
							|  |  |  |     len(conn.get_all_addresses()).should.be.equal(0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  | def test_eip_describe_none(): | 
					
						
							| 
									
										
										
										
											2014-08-25 10:54:47 -07:00
										 |  |  |     """Error when search for bogus IP""" | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_ec2("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2013-09-03 21:47:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2014-08-25 10:54:47 -07:00
										 |  |  |         conn.get_all_addresses(addresses=["256.256.256.256"]) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.code.should.equal("InvalidAddress.NotFound") | 
					
						
							|  |  |  |     cm.value.status.should.equal(400) | 
					
						
							|  |  |  |     cm.value.request_id.should_not.be.none | 
					
						
							| 
									
										
										
										
											2017-09-16 01:49:57 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_eip_filters(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     service = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							|  |  |  |     client = boto3.client("ec2", region_name="us-west-1") | 
					
						
							|  |  |  |     vpc_res = client.create_vpc(CidrBlock="10.0.0.0/24") | 
					
						
							| 
									
										
										
										
											2017-09-16 01:49:57 +05:30
										 |  |  |     subnet_res = client.create_subnet( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         VpcId=vpc_res["Vpc"]["VpcId"], CidrBlock="10.0.0.0/24" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2017-09-16 01:49:57 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |     def create_inst_with_eip(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         instance = service.create_instances( | 
					
						
							|  |  |  |             **{ | 
					
						
							|  |  |  |                 "InstanceType": "t2.micro", | 
					
						
							|  |  |  |                 "ImageId": "ami-test", | 
					
						
							|  |  |  |                 "MinCount": 1, | 
					
						
							|  |  |  |                 "MaxCount": 1, | 
					
						
							|  |  |  |                 "SubnetId": subnet_res["Subnet"]["SubnetId"], | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         )[0] | 
					
						
							|  |  |  |         allocation_id = client.allocate_address(Domain="vpc")["AllocationId"] | 
					
						
							| 
									
										
										
										
											2017-09-16 01:49:57 +05:30
										 |  |  |         _ = client.associate_address( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             InstanceId=instance.id, AllocationId=allocation_id, AllowReassociation=False | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2017-09-16 01:49:57 +05:30
										 |  |  |         instance.load() | 
					
						
							|  |  |  |         address = service.VpcAddress(allocation_id) | 
					
						
							|  |  |  |         address.load() | 
					
						
							|  |  |  |         return instance, address | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     inst1, eip1 = create_inst_with_eip() | 
					
						
							|  |  |  |     inst2, eip2 = create_inst_with_eip() | 
					
						
							|  |  |  |     inst3, eip3 = create_inst_with_eip() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Param search by AllocationId | 
					
						
							|  |  |  |     addresses = list(service.vpc_addresses.filter(AllocationIds=[eip2.allocation_id])) | 
					
						
							|  |  |  |     len(addresses).should.be.equal(1) | 
					
						
							|  |  |  |     addresses[0].public_ip.should.equal(eip2.public_ip) | 
					
						
							|  |  |  |     inst2.public_ip_address.should.equal(addresses[0].public_ip) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Param search by PublicIp | 
					
						
							|  |  |  |     addresses = list(service.vpc_addresses.filter(PublicIps=[eip3.public_ip])) | 
					
						
							|  |  |  |     len(addresses).should.be.equal(1) | 
					
						
							|  |  |  |     addresses[0].public_ip.should.equal(eip3.public_ip) | 
					
						
							|  |  |  |     inst3.public_ip_address.should.equal(addresses[0].public_ip) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Param search by Filter | 
					
						
							|  |  |  |     def check_vpc_filter_valid(filter_name, filter_values): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         addresses = list( | 
					
						
							|  |  |  |             service.vpc_addresses.filter( | 
					
						
							|  |  |  |                 Filters=[{"Name": filter_name, "Values": filter_values}] | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2017-09-16 01:49:57 +05:30
										 |  |  |         len(addresses).should.equal(2) | 
					
						
							|  |  |  |         ips = [addr.public_ip for addr in addresses] | 
					
						
							|  |  |  |         set(ips).should.equal(set([eip1.public_ip, eip2.public_ip])) | 
					
						
							|  |  |  |         ips.should.contain(inst1.public_ip_address) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def check_vpc_filter_invalid(filter_name): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         addresses = list( | 
					
						
							|  |  |  |             service.vpc_addresses.filter( | 
					
						
							|  |  |  |                 Filters=[{"Name": filter_name, "Values": ["dummy1", "dummy2"]}] | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2017-09-16 01:49:57 +05:30
										 |  |  |         len(addresses).should.equal(0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def check_vpc_filter(filter_name, filter_values): | 
					
						
							|  |  |  |         check_vpc_filter_valid(filter_name, filter_values) | 
					
						
							|  |  |  |         check_vpc_filter_invalid(filter_name) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     check_vpc_filter("allocation-id", [eip1.allocation_id, eip2.allocation_id]) | 
					
						
							|  |  |  |     check_vpc_filter("association-id", [eip1.association_id, eip2.association_id]) | 
					
						
							|  |  |  |     check_vpc_filter("instance-id", [inst1.id, inst2.id]) | 
					
						
							| 
									
										
										
										
											2017-09-16 01:49:57 +05:30
										 |  |  |     check_vpc_filter( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "network-interface-id", | 
					
						
							|  |  |  |         [ | 
					
						
							|  |  |  |             inst1.network_interfaces_attribute[0].get("NetworkInterfaceId"), | 
					
						
							|  |  |  |             inst2.network_interfaces_attribute[0].get("NetworkInterfaceId"), | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2017-09-16 01:49:57 +05:30
										 |  |  |     check_vpc_filter( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "private-ip-address", | 
					
						
							|  |  |  |         [ | 
					
						
							|  |  |  |             inst1.network_interfaces_attribute[0].get("PrivateIpAddress"), | 
					
						
							|  |  |  |             inst2.network_interfaces_attribute[0].get("PrivateIpAddress"), | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     check_vpc_filter("public-ip", [inst1.public_ip_address, inst2.public_ip_address]) | 
					
						
							| 
									
										
										
										
											2017-09-16 01:49:57 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |     # all the ips are in a VPC | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     addresses = list( | 
					
						
							|  |  |  |         service.vpc_addresses.filter(Filters=[{"Name": "domain", "Values": ["vpc"]}]) | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2017-09-16 01:49:57 +05:30
										 |  |  |     len(addresses).should.equal(3) | 
					
						
							| 
									
										
										
										
											2020-09-25 16:25:30 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_eip_tags(): | 
					
						
							|  |  |  |     service = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							|  |  |  |     client = boto3.client("ec2", region_name="us-west-1") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Allocate one address without tags | 
					
						
							|  |  |  |     client.allocate_address(Domain="vpc") | 
					
						
							|  |  |  |     # Allocate one address and add tags | 
					
						
							|  |  |  |     alloc_tags = client.allocate_address(Domain="vpc") | 
					
						
							|  |  |  |     with_tags = client.create_tags( | 
					
						
							|  |  |  |         Resources=[alloc_tags["AllocationId"]], | 
					
						
							|  |  |  |         Tags=[{"Key": "ManagedBy", "Value": "MyCode"}], | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     addresses_with_tags = client.describe_addresses( | 
					
						
							|  |  |  |         Filters=[ | 
					
						
							|  |  |  |             {"Name": "domain", "Values": ["vpc"]}, | 
					
						
							|  |  |  |             {"Name": "tag:ManagedBy", "Values": ["MyCode"]}, | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     len(addresses_with_tags["Addresses"]).should.equal(1) | 
					
						
							|  |  |  |     addresses_with_tags = list( | 
					
						
							|  |  |  |         service.vpc_addresses.filter( | 
					
						
							|  |  |  |             Filters=[ | 
					
						
							|  |  |  |                 {"Name": "domain", "Values": ["vpc"]}, | 
					
						
							|  |  |  |                 {"Name": "tag:ManagedBy", "Values": ["MyCode"]}, | 
					
						
							|  |  |  |             ] | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     len(addresses_with_tags).should.equal(1) | 
					
						
							|  |  |  |     addresses_with_tags = list( | 
					
						
							|  |  |  |         service.vpc_addresses.filter( | 
					
						
							|  |  |  |             Filters=[ | 
					
						
							|  |  |  |                 {"Name": "domain", "Values": ["vpc"]}, | 
					
						
							|  |  |  |                 {"Name": "tag:ManagedBy", "Values": ["SomethingOther"]}, | 
					
						
							|  |  |  |             ] | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     len(addresses_with_tags).should.equal(0) | 
					
						
							|  |  |  |     addresses = list( | 
					
						
							|  |  |  |         service.vpc_addresses.filter(Filters=[{"Name": "domain", "Values": ["vpc"]}]) | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     # Expected total is 2, one with and one without tags | 
					
						
							|  |  |  |     len(addresses).should.equal(2) |