| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2018-03-22 02:10:38 +10:00
										 |  |  | from moto.ec2.exceptions import EC2ClientError | 
					
						
							|  |  |  | from botocore.exceptions import ClientError | 
					
						
							| 
									
										
										
										
											2014-08-25 15:09:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-12 22:36:09 +02:00
										 |  |  | import boto3 | 
					
						
							| 
									
										
										
										
											2013-02-21 23:13:01 -05:00
										 |  |  | import boto | 
					
						
							| 
									
										
										
										
											2013-03-05 22:33:41 -05:00
										 |  |  | from boto.exception import EC2ResponseError | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  | SAMPLE_DOMAIN_NAME = "example.com" | 
					
						
							|  |  |  | SAMPLE_NAME_SERVERS = ["10.0.0.6", "10.0.0.7"] | 
					
						
							| 
									
										
										
										
											2014-09-30 17:29:50 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-21 23:13:01 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2013-02-21 23:13:01 -05:00
										 |  |  | def test_vpcs(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_vpc("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2013-03-05 22:33:41 -05:00
										 |  |  |     vpc = conn.create_vpc("10.0.0.0/16") | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc.cidr_block.should.equal("10.0.0.0/16") | 
					
						
							| 
									
										
										
										
											2013-03-05 22:33:41 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     all_vpcs = conn.get_all_vpcs() | 
					
						
							| 
									
										
										
										
											2017-01-11 17:37:57 -08:00
										 |  |  |     all_vpcs.should.have.length_of(2) | 
					
						
							| 
									
										
										
										
											2013-03-05 22:33:41 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     vpc.delete() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     all_vpcs = conn.get_all_vpcs() | 
					
						
							| 
									
										
										
										
											2017-01-11 17:37:57 -08:00
										 |  |  |     all_vpcs.should.have.length_of(1) | 
					
						
							| 
									
										
										
										
											2013-03-05 22:33:41 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2014-08-25 10:54:47 -07:00
										 |  |  |         conn.delete_vpc("vpc-1234abcd") | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.code.should.equal("InvalidVpcID.NotFound") | 
					
						
							|  |  |  |     cm.value.status.should.equal(400) | 
					
						
							|  |  |  |     cm.value.request_id.should_not.be.none | 
					
						
							| 
									
										
										
										
											2014-05-11 19:00:28 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2014-09-10 11:15:43 -07:00
										 |  |  | def test_vpc_defaults(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_vpc("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2014-09-10 11:15:43 -07:00
										 |  |  |     vpc = conn.create_vpc("10.0.0.0/16") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-11 17:37:57 -08:00
										 |  |  |     conn.get_all_vpcs().should.have.length_of(2) | 
					
						
							|  |  |  |     conn.get_all_route_tables().should.have.length_of(2) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn.get_all_security_groups(filters={"vpc-id": [vpc.id]}).should.have.length_of(1) | 
					
						
							| 
									
										
										
										
											2014-09-10 11:15:43 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     vpc.delete() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-11 17:37:57 -08:00
										 |  |  |     conn.get_all_vpcs().should.have.length_of(1) | 
					
						
							|  |  |  |     conn.get_all_route_tables().should.have.length_of(1) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn.get_all_security_groups(filters={"vpc-id": [vpc.id]}).should.have.length_of(0) | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-10 11:15:43 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2016-08-14 16:58:35 +10:00
										 |  |  | def test_vpc_isdefault_filter(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_vpc("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2016-08-14 16:58:35 +10:00
										 |  |  |     vpc = conn.create_vpc("10.0.0.0/16") | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn.get_all_vpcs(filters={"isDefault": "true"}).should.have.length_of(1) | 
					
						
							| 
									
										
										
										
											2016-08-14 16:58:35 +10:00
										 |  |  |     vpc.delete() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn.get_all_vpcs(filters={"isDefault": "true"}).should.have.length_of(1) | 
					
						
							| 
									
										
										
										
											2016-08-14 16:58:35 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2016-08-14 16:58:35 +10:00
										 |  |  | def test_multiple_vpcs_default_filter(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_vpc("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2016-08-14 16:58:35 +10:00
										 |  |  |     conn.create_vpc("10.8.0.0/16") | 
					
						
							|  |  |  |     conn.create_vpc("10.0.0.0/16") | 
					
						
							|  |  |  |     conn.create_vpc("192.168.0.0/16") | 
					
						
							| 
									
										
										
										
											2017-01-11 17:37:57 -08:00
										 |  |  |     conn.get_all_vpcs().should.have.length_of(4) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc = conn.get_all_vpcs(filters={"isDefault": "true"}) | 
					
						
							| 
									
										
										
										
											2016-08-14 16:58:35 +10:00
										 |  |  |     vpc.should.have.length_of(1) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc[0].cidr_block.should.equal("172.31.0.0/16") | 
					
						
							| 
									
										
										
										
											2016-08-14 16:58:35 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2016-08-14 16:58:35 +10:00
										 |  |  | def test_vpc_state_available_filter(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_vpc("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2016-08-14 16:58:35 +10:00
										 |  |  |     vpc = conn.create_vpc("10.0.0.0/16") | 
					
						
							|  |  |  |     conn.create_vpc("10.1.0.0/16") | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn.get_all_vpcs(filters={"state": "available"}).should.have.length_of(3) | 
					
						
							| 
									
										
										
										
											2016-08-14 16:58:35 +10:00
										 |  |  |     vpc.delete() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn.get_all_vpcs(filters={"state": "available"}).should.have.length_of(2) | 
					
						
							| 
									
										
										
										
											2014-09-10 11:15:43 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2014-05-11 19:00:28 -04:00
										 |  |  | def test_vpc_tagging(): | 
					
						
							|  |  |  |     conn = boto.connect_vpc() | 
					
						
							|  |  |  |     vpc = conn.create_vpc("10.0.0.0/16") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     vpc.add_tag("a key", "some value") | 
					
						
							|  |  |  |     tag = conn.get_all_tags()[0] | 
					
						
							|  |  |  |     tag.name.should.equal("a key") | 
					
						
							|  |  |  |     tag.value.should.equal("some value") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Refresh the vpc | 
					
						
							| 
									
										
										
										
											2017-01-11 17:37:57 -08:00
										 |  |  |     vpc = conn.get_all_vpcs(vpc_ids=[vpc.id])[0] | 
					
						
							| 
									
										
										
										
											2014-05-11 19:00:28 -04:00
										 |  |  |     vpc.tags.should.have.length_of(1) | 
					
						
							|  |  |  |     vpc.tags["a key"].should.equal("some value") | 
					
						
							| 
									
										
										
										
											2014-09-30 17:07:09 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-30 17:29:50 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2014-09-30 17:07:09 +03:00
										 |  |  | def test_vpc_get_by_id(): | 
					
						
							|  |  |  |     conn = boto.connect_vpc() | 
					
						
							|  |  |  |     vpc1 = conn.create_vpc("10.0.0.0/16") | 
					
						
							|  |  |  |     vpc2 = conn.create_vpc("10.0.0.0/16") | 
					
						
							| 
									
										
										
										
											2015-03-14 18:55:44 -04:00
										 |  |  |     conn.create_vpc("10.0.0.0/16") | 
					
						
							| 
									
										
										
										
											2014-09-30 17:07:09 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     vpcs = conn.get_all_vpcs(vpc_ids=[vpc1.id, vpc2.id]) | 
					
						
							|  |  |  |     vpcs.should.have.length_of(2) | 
					
						
							| 
									
										
										
										
											2014-10-01 12:56:32 +03:00
										 |  |  |     vpc_ids = tuple(map(lambda v: v.id, vpcs)) | 
					
						
							| 
									
										
										
										
											2014-09-30 17:29:50 +03:00
										 |  |  |     vpc1.id.should.be.within(vpc_ids) | 
					
						
							|  |  |  |     vpc2.id.should.be.within(vpc_ids) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         conn.get_all_vpcs(vpc_ids=["vpc-does_not_exist"]) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.code.should.equal("InvalidVpcID.NotFound") | 
					
						
							|  |  |  |     cm.value.status.should.equal(400) | 
					
						
							|  |  |  |     cm.value.request_id.should_not.be.none | 
					
						
							| 
									
										
										
										
											2017-09-18 23:38:39 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-30 17:29:50 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2014-09-30 17:29:50 +03:00
										 |  |  | def test_vpc_get_by_cidr_block(): | 
					
						
							|  |  |  |     conn = boto.connect_vpc() | 
					
						
							|  |  |  |     vpc1 = conn.create_vpc("10.0.0.0/16") | 
					
						
							|  |  |  |     vpc2 = conn.create_vpc("10.0.0.0/16") | 
					
						
							| 
									
										
										
										
											2015-03-14 18:55:44 -04:00
										 |  |  |     conn.create_vpc("10.0.0.0/24") | 
					
						
							| 
									
										
										
										
											2014-09-30 17:29:50 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpcs = conn.get_all_vpcs(filters={"cidr": "10.0.0.0/16"}) | 
					
						
							| 
									
										
										
										
											2014-09-30 17:29:50 +03:00
										 |  |  |     vpcs.should.have.length_of(2) | 
					
						
							| 
									
										
										
										
											2014-10-01 12:56:32 +03:00
										 |  |  |     vpc_ids = tuple(map(lambda v: v.id, vpcs)) | 
					
						
							| 
									
										
										
										
											2014-09-30 17:29:50 +03:00
										 |  |  |     vpc1.id.should.be.within(vpc_ids) | 
					
						
							|  |  |  |     vpc2.id.should.be.within(vpc_ids) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2014-09-30 17:29:50 +03:00
										 |  |  | def test_vpc_get_by_dhcp_options_id(): | 
					
						
							|  |  |  |     conn = boto.connect_vpc() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     dhcp_options = conn.create_dhcp_options(SAMPLE_DOMAIN_NAME, SAMPLE_NAME_SERVERS) | 
					
						
							| 
									
										
										
										
											2014-09-30 17:29:50 +03:00
										 |  |  |     vpc1 = conn.create_vpc("10.0.0.0/16") | 
					
						
							|  |  |  |     vpc2 = conn.create_vpc("10.0.0.0/16") | 
					
						
							| 
									
										
										
										
											2015-03-14 18:55:44 -04:00
										 |  |  |     conn.create_vpc("10.0.0.0/24") | 
					
						
							| 
									
										
										
										
											2014-09-30 17:29:50 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     conn.associate_dhcp_options(dhcp_options.id, vpc1.id) | 
					
						
							|  |  |  |     conn.associate_dhcp_options(dhcp_options.id, vpc2.id) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpcs = conn.get_all_vpcs(filters={"dhcp-options-id": dhcp_options.id}) | 
					
						
							| 
									
										
										
										
											2014-09-30 17:29:50 +03:00
										 |  |  |     vpcs.should.have.length_of(2) | 
					
						
							| 
									
										
										
										
											2014-10-01 12:56:32 +03:00
										 |  |  |     vpc_ids = tuple(map(lambda v: v.id, vpcs)) | 
					
						
							| 
									
										
										
										
											2014-09-30 17:29:50 +03:00
										 |  |  |     vpc1.id.should.be.within(vpc_ids) | 
					
						
							| 
									
										
										
										
											2014-09-30 17:58:02 +03:00
										 |  |  |     vpc2.id.should.be.within(vpc_ids) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2014-09-30 17:58:02 +03:00
										 |  |  | def test_vpc_get_by_tag(): | 
					
						
							|  |  |  |     conn = boto.connect_vpc() | 
					
						
							|  |  |  |     vpc1 = conn.create_vpc("10.0.0.0/16") | 
					
						
							|  |  |  |     vpc2 = conn.create_vpc("10.0.0.0/16") | 
					
						
							|  |  |  |     vpc3 = conn.create_vpc("10.0.0.0/24") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc1.add_tag("Name", "TestVPC") | 
					
						
							|  |  |  |     vpc2.add_tag("Name", "TestVPC") | 
					
						
							|  |  |  |     vpc3.add_tag("Name", "TestVPC2") | 
					
						
							| 
									
										
										
										
											2014-09-30 17:58:02 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpcs = conn.get_all_vpcs(filters={"tag:Name": "TestVPC"}) | 
					
						
							| 
									
										
										
										
											2014-09-30 17:58:02 +03:00
										 |  |  |     vpcs.should.have.length_of(2) | 
					
						
							| 
									
										
										
										
											2014-10-01 12:56:32 +03:00
										 |  |  |     vpc_ids = tuple(map(lambda v: v.id, vpcs)) | 
					
						
							| 
									
										
										
										
											2014-09-30 17:58:02 +03:00
										 |  |  |     vpc1.id.should.be.within(vpc_ids) | 
					
						
							| 
									
										
										
										
											2014-10-01 15:33:12 +03:00
										 |  |  |     vpc2.id.should.be.within(vpc_ids) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2014-10-01 15:33:12 +03:00
										 |  |  | def test_vpc_get_by_tag_key_superset(): | 
					
						
							|  |  |  |     conn = boto.connect_vpc() | 
					
						
							|  |  |  |     vpc1 = conn.create_vpc("10.0.0.0/16") | 
					
						
							|  |  |  |     vpc2 = conn.create_vpc("10.0.0.0/16") | 
					
						
							|  |  |  |     vpc3 = conn.create_vpc("10.0.0.0/24") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc1.add_tag("Name", "TestVPC") | 
					
						
							|  |  |  |     vpc1.add_tag("Key", "TestVPC2") | 
					
						
							|  |  |  |     vpc2.add_tag("Name", "TestVPC") | 
					
						
							|  |  |  |     vpc2.add_tag("Key", "TestVPC2") | 
					
						
							|  |  |  |     vpc3.add_tag("Key", "TestVPC2") | 
					
						
							| 
									
										
										
										
											2014-10-01 15:33:12 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpcs = conn.get_all_vpcs(filters={"tag-key": "Name"}) | 
					
						
							| 
									
										
										
										
											2014-10-01 15:33:12 +03:00
										 |  |  |     vpcs.should.have.length_of(2) | 
					
						
							|  |  |  |     vpc_ids = tuple(map(lambda v: v.id, vpcs)) | 
					
						
							|  |  |  |     vpc1.id.should.be.within(vpc_ids) | 
					
						
							|  |  |  |     vpc2.id.should.be.within(vpc_ids) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-01 15:44:54 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2014-10-01 15:33:12 +03:00
										 |  |  | def test_vpc_get_by_tag_key_subset(): | 
					
						
							|  |  |  |     conn = boto.connect_vpc() | 
					
						
							|  |  |  |     vpc1 = conn.create_vpc("10.0.0.0/16") | 
					
						
							|  |  |  |     vpc2 = conn.create_vpc("10.0.0.0/16") | 
					
						
							|  |  |  |     vpc3 = conn.create_vpc("10.0.0.0/24") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc1.add_tag("Name", "TestVPC") | 
					
						
							|  |  |  |     vpc1.add_tag("Key", "TestVPC2") | 
					
						
							|  |  |  |     vpc2.add_tag("Name", "TestVPC") | 
					
						
							|  |  |  |     vpc2.add_tag("Key", "TestVPC2") | 
					
						
							|  |  |  |     vpc3.add_tag("Test", "TestVPC2") | 
					
						
							| 
									
										
										
										
											2014-10-01 15:33:12 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpcs = conn.get_all_vpcs(filters={"tag-key": ["Name", "Key"]}) | 
					
						
							| 
									
										
										
										
											2014-10-01 15:33:12 +03:00
										 |  |  |     vpcs.should.have.length_of(2) | 
					
						
							|  |  |  |     vpc_ids = tuple(map(lambda v: v.id, vpcs)) | 
					
						
							|  |  |  |     vpc1.id.should.be.within(vpc_ids) | 
					
						
							| 
									
										
										
										
											2014-10-01 15:44:54 +03:00
										 |  |  |     vpc2.id.should.be.within(vpc_ids) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2014-10-01 15:44:54 +03:00
										 |  |  | def test_vpc_get_by_tag_value_superset(): | 
					
						
							|  |  |  |     conn = boto.connect_vpc() | 
					
						
							|  |  |  |     vpc1 = conn.create_vpc("10.0.0.0/16") | 
					
						
							|  |  |  |     vpc2 = conn.create_vpc("10.0.0.0/16") | 
					
						
							|  |  |  |     vpc3 = conn.create_vpc("10.0.0.0/24") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc1.add_tag("Name", "TestVPC") | 
					
						
							|  |  |  |     vpc1.add_tag("Key", "TestVPC2") | 
					
						
							|  |  |  |     vpc2.add_tag("Name", "TestVPC") | 
					
						
							|  |  |  |     vpc2.add_tag("Key", "TestVPC2") | 
					
						
							|  |  |  |     vpc3.add_tag("Key", "TestVPC2") | 
					
						
							| 
									
										
										
										
											2014-10-01 15:44:54 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpcs = conn.get_all_vpcs(filters={"tag-value": "TestVPC"}) | 
					
						
							| 
									
										
										
										
											2014-10-01 15:44:54 +03:00
										 |  |  |     vpcs.should.have.length_of(2) | 
					
						
							|  |  |  |     vpc_ids = tuple(map(lambda v: v.id, vpcs)) | 
					
						
							|  |  |  |     vpc1.id.should.be.within(vpc_ids) | 
					
						
							|  |  |  |     vpc2.id.should.be.within(vpc_ids) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2014-10-01 15:44:54 +03:00
										 |  |  | def test_vpc_get_by_tag_value_subset(): | 
					
						
							|  |  |  |     conn = boto.connect_vpc() | 
					
						
							|  |  |  |     vpc1 = conn.create_vpc("10.0.0.0/16") | 
					
						
							|  |  |  |     vpc2 = conn.create_vpc("10.0.0.0/16") | 
					
						
							| 
									
										
										
										
											2015-03-14 18:55:44 -04:00
										 |  |  |     conn.create_vpc("10.0.0.0/24") | 
					
						
							| 
									
										
										
										
											2014-10-01 15:44:54 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc1.add_tag("Name", "TestVPC") | 
					
						
							|  |  |  |     vpc1.add_tag("Key", "TestVPC2") | 
					
						
							|  |  |  |     vpc2.add_tag("Name", "TestVPC") | 
					
						
							|  |  |  |     vpc2.add_tag("Key", "TestVPC2") | 
					
						
							| 
									
										
										
										
											2014-10-01 15:44:54 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpcs = conn.get_all_vpcs(filters={"tag-value": ["TestVPC", "TestVPC2"]}) | 
					
						
							| 
									
										
										
										
											2014-10-01 15:44:54 +03:00
										 |  |  |     vpcs.should.have.length_of(2) | 
					
						
							|  |  |  |     vpc_ids = tuple(map(lambda v: v.id, vpcs)) | 
					
						
							|  |  |  |     vpc1.id.should.be.within(vpc_ids) | 
					
						
							| 
									
										
										
										
											2014-10-12 21:51:12 -04:00
										 |  |  |     vpc2.id.should.be.within(vpc_ids) | 
					
						
							| 
									
										
										
										
											2016-05-12 22:36:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_default_vpc(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							| 
									
										
										
										
											2016-05-12 22:36:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Create the default VPC | 
					
						
							| 
									
										
										
										
											2017-01-11 17:37:57 -08:00
										 |  |  |     default_vpc = list(ec2.vpcs.all())[0] | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     default_vpc.cidr_block.should.equal("172.31.0.0/16") | 
					
						
							|  |  |  |     default_vpc.instance_tenancy.should.equal("default") | 
					
						
							| 
									
										
										
										
											2016-05-12 22:36:09 +02:00
										 |  |  |     default_vpc.reload() | 
					
						
							|  |  |  |     default_vpc.is_default.should.be.ok | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Test default values for VPC attributes | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     response = default_vpc.describe_attribute(Attribute="enableDnsSupport") | 
					
						
							|  |  |  |     attr = response.get("EnableDnsSupport") | 
					
						
							|  |  |  |     attr.get("Value").should.be.ok | 
					
						
							| 
									
										
										
										
											2016-05-12 22:36:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     response = default_vpc.describe_attribute(Attribute="enableDnsHostnames") | 
					
						
							|  |  |  |     attr = response.get("EnableDnsHostnames") | 
					
						
							|  |  |  |     attr.get("Value").should.be.ok | 
					
						
							| 
									
										
										
										
											2016-05-12 22:36:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_non_default_vpc(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							| 
									
										
										
										
											2016-05-12 22:36:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-22 02:10:38 +10:00
										 |  |  |     # Create the default VPC - this already exists when backend instantiated! | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     # ec2.create_vpc(CidrBlock='172.31.0.0/16') | 
					
						
							| 
									
										
										
										
											2016-05-12 22:36:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Create the non default VPC | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16") | 
					
						
							| 
									
										
										
										
											2016-05-12 22:36:09 +02:00
										 |  |  |     vpc.reload() | 
					
						
							|  |  |  |     vpc.is_default.shouldnt.be.ok | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-08 21:23:49 -05:00
										 |  |  |     # Test default instance_tenancy | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc.instance_tenancy.should.equal("default") | 
					
						
							| 
									
										
										
										
											2017-02-08 21:23:49 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-12 22:36:09 +02:00
										 |  |  |     # Test default values for VPC attributes | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     response = vpc.describe_attribute(Attribute="enableDnsSupport") | 
					
						
							|  |  |  |     attr = response.get("EnableDnsSupport") | 
					
						
							|  |  |  |     attr.get("Value").should.be.ok | 
					
						
							| 
									
										
										
										
											2016-05-12 22:36:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     response = vpc.describe_attribute(Attribute="enableDnsHostnames") | 
					
						
							|  |  |  |     attr = response.get("EnableDnsHostnames") | 
					
						
							|  |  |  |     attr.get("Value").shouldnt.be.ok | 
					
						
							| 
									
										
										
										
											2016-05-12 22:36:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-22 02:10:38 +10:00
										 |  |  |     # Check Primary CIDR Block Associations | 
					
						
							|  |  |  |     cidr_block_association_set = next(iter(vpc.cidr_block_association_set), None) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     cidr_block_association_set["CidrBlockState"]["State"].should.equal("associated") | 
					
						
							|  |  |  |     cidr_block_association_set["CidrBlock"].should.equal(vpc.cidr_block) | 
					
						
							|  |  |  |     cidr_block_association_set["AssociationId"].should.contain("vpc-cidr-assoc") | 
					
						
							| 
									
										
										
										
											2018-03-22 02:10:38 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-08 21:23:49 -05:00
										 |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_vpc_dedicated_tenancy(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							| 
									
										
										
										
											2017-02-08 21:23:49 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Create the default VPC | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2.create_vpc(CidrBlock="172.31.0.0/16") | 
					
						
							| 
									
										
										
										
											2017-02-08 21:23:49 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Create the non default VPC | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16", InstanceTenancy="dedicated") | 
					
						
							| 
									
										
										
										
											2017-02-08 21:23:49 -05:00
										 |  |  |     vpc.reload() | 
					
						
							|  |  |  |     vpc.is_default.shouldnt.be.ok | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc.instance_tenancy.should.equal("dedicated") | 
					
						
							| 
									
										
										
										
											2016-05-12 22:36:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-12 22:36:09 +02:00
										 |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_vpc_modify_enable_dns_support(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							| 
									
										
										
										
											2016-05-12 22:36:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Create the default VPC | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2.create_vpc(CidrBlock="172.31.0.0/16") | 
					
						
							| 
									
										
										
										
											2016-05-12 22:36:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16") | 
					
						
							| 
									
										
										
										
											2016-05-12 22:36:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Test default values for VPC attributes | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     response = vpc.describe_attribute(Attribute="enableDnsSupport") | 
					
						
							|  |  |  |     attr = response.get("EnableDnsSupport") | 
					
						
							|  |  |  |     attr.get("Value").should.be.ok | 
					
						
							| 
									
										
										
										
											2016-05-12 22:36:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc.modify_attribute(EnableDnsSupport={"Value": False}) | 
					
						
							| 
									
										
										
										
											2016-05-12 22:36:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     response = vpc.describe_attribute(Attribute="enableDnsSupport") | 
					
						
							|  |  |  |     attr = response.get("EnableDnsSupport") | 
					
						
							|  |  |  |     attr.get("Value").shouldnt.be.ok | 
					
						
							| 
									
										
										
										
											2016-05-12 22:36:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_vpc_modify_enable_dns_hostnames(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							| 
									
										
										
										
											2016-05-12 22:36:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Create the default VPC | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2.create_vpc(CidrBlock="172.31.0.0/16") | 
					
						
							| 
									
										
										
										
											2016-05-12 22:36:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16") | 
					
						
							| 
									
										
										
										
											2016-05-12 22:36:09 +02:00
										 |  |  |     # Test default values for VPC attributes | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     response = vpc.describe_attribute(Attribute="enableDnsHostnames") | 
					
						
							|  |  |  |     attr = response.get("EnableDnsHostnames") | 
					
						
							|  |  |  |     attr.get("Value").shouldnt.be.ok | 
					
						
							| 
									
										
										
										
											2016-05-12 22:36:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc.modify_attribute(EnableDnsHostnames={"Value": True}) | 
					
						
							| 
									
										
										
										
											2016-05-12 22:36:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     response = vpc.describe_attribute(Attribute="enableDnsHostnames") | 
					
						
							|  |  |  |     attr = response.get("EnableDnsHostnames") | 
					
						
							|  |  |  |     attr.get("Value").should.be.ok | 
					
						
							| 
									
										
										
										
											2016-10-08 10:34:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2016-10-08 10:34:55 +01:00
										 |  |  | def test_vpc_associate_dhcp_options(): | 
					
						
							|  |  |  |     conn = boto.connect_vpc() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     dhcp_options = conn.create_dhcp_options(SAMPLE_DOMAIN_NAME, SAMPLE_NAME_SERVERS) | 
					
						
							| 
									
										
										
										
											2016-10-08 10:34:55 +01:00
										 |  |  |     vpc = conn.create_vpc("10.0.0.0/16") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     conn.associate_dhcp_options(dhcp_options.id, vpc.id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     vpc.update() | 
					
						
							|  |  |  |     dhcp_options.id.should.equal(vpc.dhcp_options_id) | 
					
						
							| 
									
										
										
										
											2018-03-22 02:10:38 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_associate_vpc_ipv4_cidr_block(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							| 
									
										
										
										
											2018-03-22 02:10:38 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc = ec2.create_vpc(CidrBlock="10.10.42.0/24") | 
					
						
							| 
									
										
										
										
											2018-03-22 02:10:38 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Associate/Extend vpc CIDR range up to 5 ciders | 
					
						
							|  |  |  |     for i in range(43, 47): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         response = ec2.meta.client.associate_vpc_cidr_block( | 
					
						
							|  |  |  |             VpcId=vpc.id, CidrBlock="10.10.{}.0/24".format(i) | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         response["CidrBlockAssociation"]["CidrBlockState"]["State"].should.equal( | 
					
						
							|  |  |  |             "associating" | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         response["CidrBlockAssociation"]["CidrBlock"].should.equal( | 
					
						
							|  |  |  |             "10.10.{}.0/24".format(i) | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         response["CidrBlockAssociation"]["AssociationId"].should.contain( | 
					
						
							|  |  |  |             "vpc-cidr-assoc" | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2018-03-22 02:10:38 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Check all associations exist | 
					
						
							|  |  |  |     vpc = ec2.Vpc(vpc.id) | 
					
						
							|  |  |  |     vpc.cidr_block_association_set.should.have.length_of(5) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc.cidr_block_association_set[2]["CidrBlockState"]["State"].should.equal( | 
					
						
							|  |  |  |         "associated" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     vpc.cidr_block_association_set[4]["CidrBlockState"]["State"].should.equal( | 
					
						
							|  |  |  |         "associated" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-03-22 02:10:38 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Check error on adding 6th association. | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(ClientError) as ex: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         response = ec2.meta.client.associate_vpc_cidr_block( | 
					
						
							|  |  |  |             VpcId=vpc.id, CidrBlock="10.10.50.0/22" | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     str(ex.value).should.equal( | 
					
						
							| 
									
										
										
										
											2018-03-22 02:10:38 +10:00
										 |  |  |         "An error occurred (CidrLimitExceeded) when calling the AssociateVpcCidrBlock " | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "operation: This network '{}' has met its maximum number of allowed CIDRs: 5".format( | 
					
						
							|  |  |  |             vpc.id | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-22 02:10:38 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_disassociate_vpc_ipv4_cidr_block(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							| 
									
										
										
										
											2018-03-22 02:10:38 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc = ec2.create_vpc(CidrBlock="10.10.42.0/24") | 
					
						
							|  |  |  |     ec2.meta.client.associate_vpc_cidr_block(VpcId=vpc.id, CidrBlock="10.10.43.0/24") | 
					
						
							| 
									
										
										
										
											2018-03-22 02:10:38 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Remove an extended cidr block | 
					
						
							|  |  |  |     vpc = ec2.Vpc(vpc.id) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     non_default_assoc_cidr_block = next( | 
					
						
							|  |  |  |         iter( | 
					
						
							|  |  |  |             [ | 
					
						
							|  |  |  |                 x | 
					
						
							|  |  |  |                 for x in vpc.cidr_block_association_set | 
					
						
							|  |  |  |                 if vpc.cidr_block != x["CidrBlock"] | 
					
						
							|  |  |  |             ] | 
					
						
							|  |  |  |         ), | 
					
						
							|  |  |  |         None, | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     response = ec2.meta.client.disassociate_vpc_cidr_block( | 
					
						
							|  |  |  |         AssociationId=non_default_assoc_cidr_block["AssociationId"] | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     response["CidrBlockAssociation"]["CidrBlockState"]["State"].should.equal( | 
					
						
							|  |  |  |         "disassociating" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     response["CidrBlockAssociation"]["CidrBlock"].should.equal( | 
					
						
							|  |  |  |         non_default_assoc_cidr_block["CidrBlock"] | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     response["CidrBlockAssociation"]["AssociationId"].should.equal( | 
					
						
							|  |  |  |         non_default_assoc_cidr_block["AssociationId"] | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-03-22 02:10:38 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Error attempting to delete a non-existent CIDR_BLOCK association | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(ClientError) as ex: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         response = ec2.meta.client.disassociate_vpc_cidr_block( | 
					
						
							|  |  |  |             AssociationId="vpc-cidr-assoc-BORING123" | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     str(ex.value).should.equal( | 
					
						
							| 
									
										
										
										
											2018-03-22 02:10:38 +10:00
										 |  |  |         "An error occurred (InvalidVpcCidrBlockAssociationIdError.NotFound) when calling the " | 
					
						
							|  |  |  |         "DisassociateVpcCidrBlock operation: The vpc CIDR block association ID " | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "'vpc-cidr-assoc-BORING123' does not exist" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-03-22 02:10:38 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Error attempting to delete Primary CIDR BLOCK association | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc_base_cidr_assoc_id = next( | 
					
						
							|  |  |  |         iter( | 
					
						
							|  |  |  |             [ | 
					
						
							|  |  |  |                 x | 
					
						
							|  |  |  |                 for x in vpc.cidr_block_association_set | 
					
						
							|  |  |  |                 if vpc.cidr_block == x["CidrBlock"] | 
					
						
							|  |  |  |             ] | 
					
						
							|  |  |  |         ), | 
					
						
							|  |  |  |         {}, | 
					
						
							|  |  |  |     )["AssociationId"] | 
					
						
							| 
									
										
										
										
											2018-03-22 02:10:38 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(ClientError) as ex: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         response = ec2.meta.client.disassociate_vpc_cidr_block( | 
					
						
							|  |  |  |             AssociationId=vpc_base_cidr_assoc_id | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     str(ex.value).should.equal( | 
					
						
							| 
									
										
										
										
											2018-03-22 02:10:38 +10:00
										 |  |  |         "An error occurred (OperationNotPermitted) when calling the DisassociateVpcCidrBlock operation: " | 
					
						
							|  |  |  |         "The vpc CIDR block with association ID {} may not be disassociated. It is the primary " | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "IPv4 CIDR block of the VPC".format(vpc_base_cidr_assoc_id) | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-22 02:10:38 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_cidr_block_association_filters(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							|  |  |  |     vpc1 = ec2.create_vpc(CidrBlock="10.90.0.0/16") | 
					
						
							|  |  |  |     vpc2 = ec2.create_vpc(CidrBlock="10.91.0.0/16") | 
					
						
							|  |  |  |     ec2.meta.client.associate_vpc_cidr_block(VpcId=vpc2.id, CidrBlock="10.10.0.0/19") | 
					
						
							|  |  |  |     vpc3 = ec2.create_vpc(CidrBlock="10.92.0.0/24") | 
					
						
							|  |  |  |     ec2.meta.client.associate_vpc_cidr_block(VpcId=vpc3.id, CidrBlock="10.92.1.0/24") | 
					
						
							|  |  |  |     ec2.meta.client.associate_vpc_cidr_block(VpcId=vpc3.id, CidrBlock="10.92.2.0/24") | 
					
						
							|  |  |  |     vpc3_assoc_response = ec2.meta.client.associate_vpc_cidr_block( | 
					
						
							|  |  |  |         VpcId=vpc3.id, CidrBlock="10.92.3.0/24" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-03-22 02:10:38 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Test filters for a cidr-block in all VPCs cidr-block-associations | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     filtered_vpcs = list( | 
					
						
							|  |  |  |         ec2.vpcs.filter( | 
					
						
							|  |  |  |             Filters=[ | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     "Name": "cidr-block-association.cidr-block", | 
					
						
							|  |  |  |                     "Values": ["10.10.0.0/19"], | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             ] | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-03-22 02:10:38 +10:00
										 |  |  |     filtered_vpcs.should.be.length_of(1) | 
					
						
							|  |  |  |     filtered_vpcs[0].id.should.equal(vpc2.id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Test filter for association id in VPCs | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     association_id = vpc3_assoc_response["CidrBlockAssociation"]["AssociationId"] | 
					
						
							|  |  |  |     filtered_vpcs = list( | 
					
						
							|  |  |  |         ec2.vpcs.filter( | 
					
						
							|  |  |  |             Filters=[ | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     "Name": "cidr-block-association.association-id", | 
					
						
							|  |  |  |                     "Values": [association_id], | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             ] | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-03-22 02:10:38 +10:00
										 |  |  |     filtered_vpcs.should.be.length_of(1) | 
					
						
							|  |  |  |     filtered_vpcs[0].id.should.equal(vpc3.id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Test filter for association state in VPC - this will never show anything in this test | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     filtered_vpcs = list( | 
					
						
							|  |  |  |         ec2.vpcs.filter( | 
					
						
							|  |  |  |             Filters=[ | 
					
						
							|  |  |  |                 {"Name": "cidr-block-association.association-id", "Values": ["failing"]} | 
					
						
							|  |  |  |             ] | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-03-22 02:10:38 +10:00
										 |  |  |     filtered_vpcs.should.be.length_of(0) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-22 02:10:38 +10:00
										 |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_vpc_associate_ipv6_cidr_block(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							| 
									
										
										
										
											2018-03-22 02:10:38 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Test create VPC with IPV6 cidr range | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc = ec2.create_vpc(CidrBlock="10.10.42.0/24", AmazonProvidedIpv6CidrBlock=True) | 
					
						
							|  |  |  |     ipv6_cidr_block_association_set = next( | 
					
						
							|  |  |  |         iter(vpc.ipv6_cidr_block_association_set), None | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     ipv6_cidr_block_association_set["Ipv6CidrBlockState"]["State"].should.equal( | 
					
						
							|  |  |  |         "associated" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     ipv6_cidr_block_association_set["Ipv6CidrBlock"].should.contain("::/56") | 
					
						
							|  |  |  |     ipv6_cidr_block_association_set["AssociationId"].should.contain("vpc-cidr-assoc") | 
					
						
							| 
									
										
										
										
											2018-03-22 02:10:38 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Test Fail on adding 2nd IPV6 association - AWS only allows 1 at this time! | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(ClientError) as ex: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         response = ec2.meta.client.associate_vpc_cidr_block( | 
					
						
							|  |  |  |             VpcId=vpc.id, AmazonProvidedIpv6CidrBlock=True | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     str(ex.value).should.equal( | 
					
						
							| 
									
										
										
										
											2018-03-22 02:10:38 +10:00
										 |  |  |         "An error occurred (CidrLimitExceeded) when calling the AssociateVpcCidrBlock " | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "operation: This network '{}' has met its maximum number of allowed CIDRs: 1".format( | 
					
						
							|  |  |  |             vpc.id | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-03-22 02:10:38 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Test associate ipv6 cidr block after vpc created | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc = ec2.create_vpc(CidrBlock="10.10.50.0/24") | 
					
						
							|  |  |  |     response = ec2.meta.client.associate_vpc_cidr_block( | 
					
						
							|  |  |  |         VpcId=vpc.id, AmazonProvidedIpv6CidrBlock=True | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     response["Ipv6CidrBlockAssociation"]["Ipv6CidrBlockState"]["State"].should.equal( | 
					
						
							|  |  |  |         "associating" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     response["Ipv6CidrBlockAssociation"]["Ipv6CidrBlock"].should.contain("::/56") | 
					
						
							|  |  |  |     response["Ipv6CidrBlockAssociation"]["AssociationId"].should.contain( | 
					
						
							|  |  |  |         "vpc-cidr-assoc-" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-03-22 02:10:38 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Check on describe vpc that has ipv6 cidr block association | 
					
						
							|  |  |  |     vpc = ec2.Vpc(vpc.id) | 
					
						
							|  |  |  |     vpc.ipv6_cidr_block_association_set.should.be.length_of(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_vpc_disassociate_ipv6_cidr_block(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							| 
									
										
										
										
											2018-03-22 02:10:38 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Test create VPC with IPV6 cidr range | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc = ec2.create_vpc(CidrBlock="10.10.42.0/24", AmazonProvidedIpv6CidrBlock=True) | 
					
						
							| 
									
										
										
										
											2018-03-22 02:10:38 +10:00
										 |  |  |     # Test disassociating the only IPV6 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     assoc_id = vpc.ipv6_cidr_block_association_set[0]["AssociationId"] | 
					
						
							| 
									
										
										
										
											2018-03-22 02:10:38 +10:00
										 |  |  |     response = ec2.meta.client.disassociate_vpc_cidr_block(AssociationId=assoc_id) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     response["Ipv6CidrBlockAssociation"]["Ipv6CidrBlockState"]["State"].should.equal( | 
					
						
							|  |  |  |         "disassociating" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     response["Ipv6CidrBlockAssociation"]["Ipv6CidrBlock"].should.contain("::/56") | 
					
						
							|  |  |  |     response["Ipv6CidrBlockAssociation"]["AssociationId"].should.equal(assoc_id) | 
					
						
							| 
									
										
										
										
											2018-03-22 02:10:38 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_ipv6_cidr_block_association_filters(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							|  |  |  |     vpc1 = ec2.create_vpc(CidrBlock="10.90.0.0/16") | 
					
						
							| 
									
										
										
										
											2018-03-22 02:10:38 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc2 = ec2.create_vpc(CidrBlock="10.91.0.0/16", AmazonProvidedIpv6CidrBlock=True) | 
					
						
							|  |  |  |     vpc2_assoc_ipv6_assoc_id = vpc2.ipv6_cidr_block_association_set[0]["AssociationId"] | 
					
						
							|  |  |  |     ec2.meta.client.associate_vpc_cidr_block(VpcId=vpc2.id, CidrBlock="10.10.0.0/19") | 
					
						
							| 
									
										
										
										
											2018-03-22 02:10:38 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc3 = ec2.create_vpc(CidrBlock="10.92.0.0/24") | 
					
						
							|  |  |  |     ec2.meta.client.associate_vpc_cidr_block(VpcId=vpc3.id, CidrBlock="10.92.1.0/24") | 
					
						
							|  |  |  |     ec2.meta.client.associate_vpc_cidr_block(VpcId=vpc3.id, CidrBlock="10.92.2.0/24") | 
					
						
							|  |  |  |     response = ec2.meta.client.associate_vpc_cidr_block( | 
					
						
							|  |  |  |         VpcId=vpc3.id, AmazonProvidedIpv6CidrBlock=True | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     vpc3_ipv6_cidr_block = response["Ipv6CidrBlockAssociation"]["Ipv6CidrBlock"] | 
					
						
							| 
									
										
										
										
											2018-03-22 02:10:38 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc4 = ec2.create_vpc(CidrBlock="10.95.0.0/16")  # Here for its looks | 
					
						
							| 
									
										
										
										
											2018-03-22 02:10:38 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Test filters for an ipv6 cidr-block in all VPCs cidr-block-associations | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     filtered_vpcs = list( | 
					
						
							|  |  |  |         ec2.vpcs.filter( | 
					
						
							|  |  |  |             Filters=[ | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     "Name": "ipv6-cidr-block-association.ipv6-cidr-block", | 
					
						
							|  |  |  |                     "Values": [vpc3_ipv6_cidr_block], | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             ] | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-03-22 02:10:38 +10:00
										 |  |  |     filtered_vpcs.should.be.length_of(1) | 
					
						
							|  |  |  |     filtered_vpcs[0].id.should.equal(vpc3.id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Test filter for association id in VPCs | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     filtered_vpcs = list( | 
					
						
							|  |  |  |         ec2.vpcs.filter( | 
					
						
							|  |  |  |             Filters=[ | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     "Name": "ipv6-cidr-block-association.association-id", | 
					
						
							|  |  |  |                     "Values": [vpc2_assoc_ipv6_assoc_id], | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             ] | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-03-22 02:10:38 +10:00
										 |  |  |     filtered_vpcs.should.be.length_of(1) | 
					
						
							|  |  |  |     filtered_vpcs[0].id.should.equal(vpc2.id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Test filter for association state in VPC - this will never show anything in this test | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     filtered_vpcs = list( | 
					
						
							|  |  |  |         ec2.vpcs.filter( | 
					
						
							|  |  |  |             Filters=[ | 
					
						
							|  |  |  |                 {"Name": "ipv6-cidr-block-association.state", "Values": ["associated"]} | 
					
						
							|  |  |  |             ] | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     filtered_vpcs.should.be.length_of(2)  # 2 of 4 VPCs | 
					
						
							| 
									
										
										
										
											2019-05-25 19:35:07 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_create_vpc_with_invalid_cidr_block_parameter(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							| 
									
										
										
										
											2019-05-25 19:35:07 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc_cidr_block = "1000.1.0.0/20" | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(ClientError) as ex: | 
					
						
							| 
									
										
										
										
											2019-05-25 19:35:07 +02:00
										 |  |  |         vpc = ec2.create_vpc(CidrBlock=vpc_cidr_block) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     str(ex.value).should.equal( | 
					
						
							| 
									
										
										
										
											2019-05-25 19:35:07 +02:00
										 |  |  |         "An error occurred (InvalidParameterValue) when calling the CreateVpc " | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "operation: Value ({}) for parameter cidrBlock is invalid. This is not a valid CIDR block.".format( | 
					
						
							|  |  |  |             vpc_cidr_block | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-05-25 19:35:07 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_create_vpc_with_invalid_cidr_range(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							| 
									
										
										
										
											2019-05-25 19:35:07 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc_cidr_block = "10.1.0.0/29" | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(ClientError) as ex: | 
					
						
							| 
									
										
										
										
											2019-05-25 19:35:07 +02:00
										 |  |  |         vpc = ec2.create_vpc(CidrBlock=vpc_cidr_block) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     str(ex.value).should.equal( | 
					
						
							| 
									
										
										
										
											2019-05-25 19:35:07 +02:00
										 |  |  |         "An error occurred (InvalidVpc.Range) when calling the CreateVpc " | 
					
						
							| 
									
										
										
										
											2019-11-11 12:09:52 -08:00
										 |  |  |         "operation: The CIDR '{}' is invalid.".format(vpc_cidr_block) | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-02 12:35:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-27 13:54:17 +05:30
										 |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_create_vpc_with_tags(): | 
					
						
							|  |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							|  |  |  |     # Create VPC | 
					
						
							|  |  |  |     vpc = ec2.create_vpc( | 
					
						
							|  |  |  |         CidrBlock="10.0.0.0/16", | 
					
						
							|  |  |  |         TagSpecifications=[ | 
					
						
							|  |  |  |             {"ResourceType": "vpc", "Tags": [{"Key": "name", "Value": "some-vpc"}]} | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     assert vpc.tags == [{"Key": "name", "Value": "some-vpc"}] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-02 12:35:16 +02:00
										 |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_enable_vpc_classic_link(): | 
					
						
							| 
									
										
										
										
											2019-11-11 12:09:52 -08:00
										 |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							| 
									
										
										
										
											2019-09-02 12:35:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Create VPC | 
					
						
							| 
									
										
										
										
											2019-11-11 12:09:52 -08:00
										 |  |  |     vpc = ec2.create_vpc(CidrBlock="10.1.0.0/16") | 
					
						
							| 
									
										
										
										
											2019-09-02 12:35:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     response = ec2.meta.client.enable_vpc_classic_link(VpcId=vpc.id) | 
					
						
							| 
									
										
										
										
											2019-11-11 12:09:52 -08:00
										 |  |  |     assert response.get("Return").should.be.true | 
					
						
							| 
									
										
										
										
											2019-09-02 12:35:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-12 10:32:27 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-02 12:35:16 +02:00
										 |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_enable_vpc_classic_link_failure(): | 
					
						
							| 
									
										
										
										
											2019-11-11 12:09:52 -08:00
										 |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							| 
									
										
										
										
											2019-09-02 12:35:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Create VPC | 
					
						
							| 
									
										
										
										
											2019-11-11 12:09:52 -08:00
										 |  |  |     vpc = ec2.create_vpc(CidrBlock="10.90.0.0/16") | 
					
						
							| 
									
										
										
										
											2019-09-02 12:35:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     response = ec2.meta.client.enable_vpc_classic_link(VpcId=vpc.id) | 
					
						
							| 
									
										
										
										
											2019-11-11 12:09:52 -08:00
										 |  |  |     assert response.get("Return").should.be.false | 
					
						
							| 
									
										
										
										
											2019-09-02 12:35:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_disable_vpc_classic_link(): | 
					
						
							| 
									
										
										
										
											2019-11-11 12:09:52 -08:00
										 |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							| 
									
										
										
										
											2019-09-02 12:35:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Create VPC | 
					
						
							| 
									
										
										
										
											2019-11-11 12:09:52 -08:00
										 |  |  |     vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16") | 
					
						
							| 
									
										
										
										
											2019-09-02 12:35:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ec2.meta.client.enable_vpc_classic_link(VpcId=vpc.id) | 
					
						
							|  |  |  |     response = ec2.meta.client.disable_vpc_classic_link(VpcId=vpc.id) | 
					
						
							| 
									
										
										
										
											2019-11-11 12:09:52 -08:00
										 |  |  |     assert response.get("Return").should.be.false | 
					
						
							| 
									
										
										
										
											2019-09-02 12:35:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_describe_classic_link_enabled(): | 
					
						
							| 
									
										
										
										
											2019-11-11 12:09:52 -08:00
										 |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							| 
									
										
										
										
											2019-09-02 12:35:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Create VPC | 
					
						
							| 
									
										
										
										
											2019-11-11 12:09:52 -08:00
										 |  |  |     vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16") | 
					
						
							| 
									
										
										
										
											2019-09-02 12:35:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ec2.meta.client.enable_vpc_classic_link(VpcId=vpc.id) | 
					
						
							|  |  |  |     response = ec2.meta.client.describe_vpc_classic_link(VpcIds=[vpc.id]) | 
					
						
							| 
									
										
										
										
											2019-11-11 12:09:52 -08:00
										 |  |  |     assert response.get("Vpcs")[0].get("ClassicLinkEnabled").should.be.true | 
					
						
							| 
									
										
										
										
											2019-09-02 12:35:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_describe_classic_link_disabled(): | 
					
						
							| 
									
										
										
										
											2019-11-11 12:09:52 -08:00
										 |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							| 
									
										
										
										
											2019-09-02 12:35:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Create VPC | 
					
						
							| 
									
										
										
										
											2019-11-11 12:09:52 -08:00
										 |  |  |     vpc = ec2.create_vpc(CidrBlock="10.90.0.0/16") | 
					
						
							| 
									
										
										
										
											2019-09-02 12:35:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     response = ec2.meta.client.describe_vpc_classic_link(VpcIds=[vpc.id]) | 
					
						
							| 
									
										
										
										
											2019-11-11 12:09:52 -08:00
										 |  |  |     assert response.get("Vpcs")[0].get("ClassicLinkEnabled").should.be.false | 
					
						
							| 
									
										
										
										
											2019-09-02 12:35:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_describe_classic_link_multiple(): | 
					
						
							| 
									
										
										
										
											2019-11-11 12:09:52 -08:00
										 |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							| 
									
										
										
										
											2019-09-02 12:35:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Create VPC | 
					
						
							| 
									
										
										
										
											2019-11-11 12:09:52 -08:00
										 |  |  |     vpc1 = ec2.create_vpc(CidrBlock="10.90.0.0/16") | 
					
						
							|  |  |  |     vpc2 = ec2.create_vpc(CidrBlock="10.0.0.0/16") | 
					
						
							| 
									
										
										
										
											2019-09-02 12:35:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ec2.meta.client.enable_vpc_classic_link(VpcId=vpc2.id) | 
					
						
							|  |  |  |     response = ec2.meta.client.describe_vpc_classic_link(VpcIds=[vpc1.id, vpc2.id]) | 
					
						
							| 
									
										
										
										
											2019-11-12 14:51:31 -08:00
										 |  |  |     expected = [ | 
					
						
							|  |  |  |         {"VpcId": vpc1.id, "ClassicLinkDnsSupported": False}, | 
					
						
							|  |  |  |         {"VpcId": vpc2.id, "ClassicLinkDnsSupported": True}, | 
					
						
							|  |  |  |     ] | 
					
						
							| 
									
										
										
										
											2019-11-12 10:32:27 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Ensure response is sorted, because they can come in random order | 
					
						
							| 
									
										
										
										
											2019-11-12 14:51:31 -08:00
										 |  |  |     assert response.get("Vpcs").sort(key=lambda x: x["VpcId"]) == expected.sort( | 
					
						
							|  |  |  |         key=lambda x: x["VpcId"] | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-09-02 13:16:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_enable_vpc_classic_link_dns_support(): | 
					
						
							| 
									
										
										
										
											2019-11-11 12:09:52 -08:00
										 |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							| 
									
										
										
										
											2019-09-02 13:16:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Create VPC | 
					
						
							| 
									
										
										
										
											2019-11-11 12:09:52 -08:00
										 |  |  |     vpc = ec2.create_vpc(CidrBlock="10.1.0.0/16") | 
					
						
							| 
									
										
										
										
											2019-09-02 13:16:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     response = ec2.meta.client.enable_vpc_classic_link_dns_support(VpcId=vpc.id) | 
					
						
							| 
									
										
										
										
											2019-11-11 12:09:52 -08:00
										 |  |  |     assert response.get("Return").should.be.true | 
					
						
							| 
									
										
										
										
											2019-09-02 13:16:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_disable_vpc_classic_link_dns_support(): | 
					
						
							| 
									
										
										
										
											2019-11-11 12:09:52 -08:00
										 |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							| 
									
										
										
										
											2019-09-02 13:16:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Create VPC | 
					
						
							| 
									
										
										
										
											2019-11-11 12:09:52 -08:00
										 |  |  |     vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16") | 
					
						
							| 
									
										
										
										
											2019-09-02 13:16:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ec2.meta.client.enable_vpc_classic_link_dns_support(VpcId=vpc.id) | 
					
						
							|  |  |  |     response = ec2.meta.client.disable_vpc_classic_link_dns_support(VpcId=vpc.id) | 
					
						
							| 
									
										
										
										
											2019-11-11 12:09:52 -08:00
										 |  |  |     assert response.get("Return").should.be.false | 
					
						
							| 
									
										
										
										
											2019-09-02 13:16:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_describe_classic_link_dns_support_enabled(): | 
					
						
							| 
									
										
										
										
											2019-11-11 12:09:52 -08:00
										 |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							| 
									
										
										
										
											2019-09-02 13:16:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Create VPC | 
					
						
							| 
									
										
										
										
											2019-11-11 12:09:52 -08:00
										 |  |  |     vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16") | 
					
						
							| 
									
										
										
										
											2019-09-02 13:16:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ec2.meta.client.enable_vpc_classic_link_dns_support(VpcId=vpc.id) | 
					
						
							|  |  |  |     response = ec2.meta.client.describe_vpc_classic_link_dns_support(VpcIds=[vpc.id]) | 
					
						
							| 
									
										
										
										
											2019-11-11 12:09:52 -08:00
										 |  |  |     assert response.get("Vpcs")[0].get("ClassicLinkDnsSupported").should.be.true | 
					
						
							| 
									
										
										
										
											2019-09-02 13:16:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_describe_classic_link_dns_support_disabled(): | 
					
						
							| 
									
										
										
										
											2019-11-11 12:09:52 -08:00
										 |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							| 
									
										
										
										
											2019-09-02 13:16:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Create VPC | 
					
						
							| 
									
										
										
										
											2019-11-11 12:09:52 -08:00
										 |  |  |     vpc = ec2.create_vpc(CidrBlock="10.90.0.0/16") | 
					
						
							| 
									
										
										
										
											2019-09-02 13:16:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     response = ec2.meta.client.describe_vpc_classic_link_dns_support(VpcIds=[vpc.id]) | 
					
						
							| 
									
										
										
										
											2019-11-11 12:09:52 -08:00
										 |  |  |     assert response.get("Vpcs")[0].get("ClassicLinkDnsSupported").should.be.false | 
					
						
							| 
									
										
										
										
											2019-09-02 13:16:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_describe_classic_link_dns_support_multiple(): | 
					
						
							| 
									
										
										
										
											2019-11-11 12:09:52 -08:00
										 |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							| 
									
										
										
										
											2019-09-02 13:16:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Create VPC | 
					
						
							| 
									
										
										
										
											2019-11-11 12:09:52 -08:00
										 |  |  |     vpc1 = ec2.create_vpc(CidrBlock="10.90.0.0/16") | 
					
						
							|  |  |  |     vpc2 = ec2.create_vpc(CidrBlock="10.0.0.0/16") | 
					
						
							| 
									
										
										
										
											2019-09-02 13:16:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ec2.meta.client.enable_vpc_classic_link_dns_support(VpcId=vpc2.id) | 
					
						
							| 
									
										
										
										
											2019-11-12 14:51:31 -08:00
										 |  |  |     response = ec2.meta.client.describe_vpc_classic_link_dns_support( | 
					
						
							|  |  |  |         VpcIds=[vpc1.id, vpc2.id] | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     expected = [ | 
					
						
							|  |  |  |         {"VpcId": vpc1.id, "ClassicLinkDnsSupported": False}, | 
					
						
							|  |  |  |         {"VpcId": vpc2.id, "ClassicLinkDnsSupported": True}, | 
					
						
							|  |  |  |     ] | 
					
						
							| 
									
										
										
										
											2019-11-12 10:32:27 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Ensure response is sorted, because they can come in random order | 
					
						
							| 
									
										
										
										
											2019-11-12 14:51:31 -08:00
										 |  |  |     assert response.get("Vpcs").sort(key=lambda x: x["VpcId"]) == expected.sort( | 
					
						
							|  |  |  |         key=lambda x: x["VpcId"] | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2020-08-06 10:56:44 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_describe_vpc_end_point_services(): | 
					
						
							|  |  |  |     ec2 = boto3.client("ec2", region_name="us-west-1") | 
					
						
							|  |  |  |     vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     route_table = ec2.create_route_table(VpcId=vpc["Vpc"]["VpcId"]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ec2.create_vpc_endpoint( | 
					
						
							|  |  |  |         VpcId=vpc["Vpc"]["VpcId"], | 
					
						
							|  |  |  |         ServiceName="com.amazonaws.us-east-1.s3", | 
					
						
							|  |  |  |         RouteTableIds=[route_table["RouteTable"]["RouteTableId"]], | 
					
						
							|  |  |  |         VpcEndpointType="gateway", | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     vpc_end_point_services = ec2.describe_vpc_endpoint_services() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert vpc_end_point_services.get("ServiceDetails").should.be.true | 
					
						
							|  |  |  |     assert vpc_end_point_services.get("ServiceNames").should.be.true | 
					
						
							|  |  |  |     assert vpc_end_point_services.get("ServiceNames") == ["com.amazonaws.us-east-1.s3"] | 
					
						
							|  |  |  |     assert ( | 
					
						
							|  |  |  |         vpc_end_point_services.get("ServiceDetails")[0] | 
					
						
							|  |  |  |         .get("ServiceType", [])[0] | 
					
						
							|  |  |  |         .get("ServiceType") | 
					
						
							|  |  |  |         == "gateway" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     assert vpc_end_point_services.get("ServiceDetails")[0].get("AvailabilityZones") == [ | 
					
						
							|  |  |  |         "us-west-1a", | 
					
						
							|  |  |  |         "us-west-1b", | 
					
						
							|  |  |  |     ] |