Best Python code snippet using localstack_python
test_vpc_peering_connection.py
Source:test_vpc_peering_connection.py  
...195    196    connection_class = VPCConnection197    def default_body(self):198        return self.REJECT_VPC_PEERING_CONNECTION199    def test_reject_vpc_peering_connection(self):200        self.set_http_response(status_code=200)201        self.assertEquals(self.service_connection.reject_vpc_peering_connection('pcx-12345678'), True)202class TestAcceptVpcPeeringConnection(AWSMockServiceTestCase):203    ACCEPT_VPC_PEERING_CONNECTION= b"""<AcceptVpcPeeringConnectionResponse xmlns="http://ec2.amazonaws.com/doc/2014-05-01/">204  <requestId>7a62c49f-347e-4fc4-9331-6e8eEXAMPLE</requestId>205  <vpcPeeringConnection>206        <vpcPeeringConnectionId>pcx-1a2b3c4d</vpcPeeringConnectionId>207       <requesterVpcInfo>208            <ownerId>123456789012</ownerId>209            <vpcId>vpc-1a2b3c4d</vpcId>210            <cidrBlock>10.0.0.0/28</cidrBlock>211        </requesterVpcInfo>212        <accepterVpcInfo>213            <ownerId>777788889999</ownerId>214            <vpcId>vpc-111aaa22</vpcId>215            <cidrBlock>10.0.1.0/28</cidrBlock>...test_vpc_peering.py
Source:test_vpc_peering.py  
...35    vpc_pcx = test_vpc_peering_connections()36    vpc_pcx = conn.accept_vpc_peering_connection(vpc_pcx.id)37    vpc_pcx._status.code.should.equal('active')38    with assert_raises(EC2ResponseError) as cm:39        conn.reject_vpc_peering_connection(vpc_pcx.id)40    cm.exception.code.should.equal('InvalidStateTransition')41    cm.exception.status.should.equal(400)42    cm.exception.request_id.should_not.be.none43    all_vpc_pcxs = conn.get_all_vpc_peering_connections()44    all_vpc_pcxs.should.have.length_of(1)45    all_vpc_pcxs[0]._status.code.should.equal('active')46@requires_boto_gte("2.32.0")47@mock_ec2_deprecated48def test_vpc_peering_connections_reject():49    conn = boto.connect_vpc('the_key', 'the_secret')50    vpc_pcx = test_vpc_peering_connections()51    verdict = conn.reject_vpc_peering_connection(vpc_pcx.id)52    verdict.should.equal(True)53    with assert_raises(EC2ResponseError) as cm:54        conn.accept_vpc_peering_connection(vpc_pcx.id)55    cm.exception.code.should.equal('InvalidStateTransition')56    cm.exception.status.should.equal(400)57    cm.exception.request_id.should_not.be.none58    all_vpc_pcxs = conn.get_all_vpc_peering_connections()59    all_vpc_pcxs.should.have.length_of(1)60    all_vpc_pcxs[0]._status.code.should.equal('rejected')61@requires_boto_gte("2.32.1")62@mock_ec2_deprecated63def test_vpc_peering_connections_delete():64    conn = boto.connect_vpc('the_key', 'the_secret')65    vpc_pcx = test_vpc_peering_connections()66    verdict = vpc_pcx.delete()67    verdict.should.equal(True)68    all_vpc_pcxs = conn.get_all_vpc_peering_connections()69    all_vpc_pcxs.should.have.length_of(1)70    all_vpc_pcxs[0]._status.code.should.equal('deleted')71    with assert_raises(EC2ResponseError) as cm:72        conn.delete_vpc_peering_connection("pcx-1234abcd")73    cm.exception.code.should.equal('InvalidVpcPeeringConnectionId.NotFound')74    cm.exception.status.should.equal(400)75    cm.exception.request_id.should_not.be.none76@mock_ec277def test_vpc_peering_connections_cross_region():78    # create vpc in us-west-1 and ap-northeast-179    ec2_usw1 = boto3.resource('ec2', region_name='us-west-1')80    vpc_usw1 = ec2_usw1.create_vpc(CidrBlock='10.90.0.0/16')81    ec2_apn1 = boto3.resource('ec2', region_name='ap-northeast-1')82    vpc_apn1 = ec2_apn1.create_vpc(CidrBlock='10.20.0.0/16')83    # create peering84    vpc_pcx_usw1 = ec2_usw1.create_vpc_peering_connection(85        VpcId=vpc_usw1.id,86        PeerVpcId=vpc_apn1.id,87        PeerRegion='ap-northeast-1',88    )89    vpc_pcx_usw1.status['Code'].should.equal('initiating-request')90    vpc_pcx_usw1.requester_vpc.id.should.equal(vpc_usw1.id)91    vpc_pcx_usw1.accepter_vpc.id.should.equal(vpc_apn1.id)92    # test cross region vpc peering connection exist93    vpc_pcx_apn1 = ec2_apn1.VpcPeeringConnection(vpc_pcx_usw1.id)94    vpc_pcx_apn1.id.should.equal(vpc_pcx_usw1.id)95    vpc_pcx_apn1.requester_vpc.id.should.equal(vpc_usw1.id)96    vpc_pcx_apn1.accepter_vpc.id.should.equal(vpc_apn1.id)97@mock_ec298def test_vpc_peering_connections_cross_region_fail():99    # create vpc in us-west-1 and ap-northeast-1100    ec2_usw1 = boto3.resource('ec2', region_name='us-west-1')101    vpc_usw1 = ec2_usw1.create_vpc(CidrBlock='10.90.0.0/16')102    ec2_apn1 = boto3.resource('ec2', region_name='ap-northeast-1')103    vpc_apn1 = ec2_apn1.create_vpc(CidrBlock='10.20.0.0/16')104    # create peering wrong region with no vpc105    with assert_raises(ClientError) as cm:106        ec2_usw1.create_vpc_peering_connection(107            VpcId=vpc_usw1.id,108            PeerVpcId=vpc_apn1.id,109            PeerRegion='ap-northeast-2')110    cm.exception.response['Error']['Code'].should.equal('InvalidVpcID.NotFound')111@mock_ec2112def test_vpc_peering_connections_cross_region_accept():113    # create vpc in us-west-1 and ap-northeast-1114    ec2_usw1 = boto3.resource('ec2', region_name='us-west-1')115    vpc_usw1 = ec2_usw1.create_vpc(CidrBlock='10.90.0.0/16')116    ec2_apn1 = boto3.resource('ec2', region_name='ap-northeast-1')117    vpc_apn1 = ec2_apn1.create_vpc(CidrBlock='10.20.0.0/16')118    # create peering119    vpc_pcx_usw1 = ec2_usw1.create_vpc_peering_connection(120        VpcId=vpc_usw1.id,121        PeerVpcId=vpc_apn1.id,122        PeerRegion='ap-northeast-1',123    )124    # accept peering from ap-northeast-1125    ec2_apn1 = boto3.client('ec2', region_name='ap-northeast-1')126    ec2_usw1 = boto3.client('ec2', region_name='us-west-1')127    acp_pcx_apn1 = ec2_apn1.accept_vpc_peering_connection(128        VpcPeeringConnectionId=vpc_pcx_usw1.id129    )130    des_pcx_apn1 = ec2_usw1.describe_vpc_peering_connections(131        VpcPeeringConnectionIds=[vpc_pcx_usw1.id]132    )133    des_pcx_usw1 = ec2_usw1.describe_vpc_peering_connections(134        VpcPeeringConnectionIds=[vpc_pcx_usw1.id]135    )136    acp_pcx_apn1['VpcPeeringConnection']['Status']['Code'].should.equal('active')137    des_pcx_apn1['VpcPeeringConnections'][0]['Status']['Code'].should.equal('active')138    des_pcx_usw1['VpcPeeringConnections'][0]['Status']['Code'].should.equal('active')139@mock_ec2140def test_vpc_peering_connections_cross_region_reject():141    # create vpc in us-west-1 and ap-northeast-1142    ec2_usw1 = boto3.resource('ec2', region_name='us-west-1')143    vpc_usw1 = ec2_usw1.create_vpc(CidrBlock='10.90.0.0/16')144    ec2_apn1 = boto3.resource('ec2', region_name='ap-northeast-1')145    vpc_apn1 = ec2_apn1.create_vpc(CidrBlock='10.20.0.0/16')146    # create peering147    vpc_pcx_usw1 = ec2_usw1.create_vpc_peering_connection(148        VpcId=vpc_usw1.id,149        PeerVpcId=vpc_apn1.id,150        PeerRegion='ap-northeast-1',151    )152    # reject peering from ap-northeast-1153    ec2_apn1 = boto3.client('ec2', region_name='ap-northeast-1')154    ec2_usw1 = boto3.client('ec2', region_name='us-west-1')155    rej_pcx_apn1 = ec2_apn1.reject_vpc_peering_connection(156        VpcPeeringConnectionId=vpc_pcx_usw1.id157    )158    des_pcx_apn1 = ec2_usw1.describe_vpc_peering_connections(159        VpcPeeringConnectionIds=[vpc_pcx_usw1.id]160    )161    des_pcx_usw1 = ec2_usw1.describe_vpc_peering_connections(162        VpcPeeringConnectionIds=[vpc_pcx_usw1.id]163    )164    rej_pcx_apn1['Return'].should.equal(True)165    des_pcx_apn1['VpcPeeringConnections'][0]['Status']['Code'].should.equal('rejected')166    des_pcx_usw1['VpcPeeringConnections'][0]['Status']['Code'].should.equal('rejected')167@mock_ec2168def test_vpc_peering_connections_cross_region_delete():169    # create vpc in us-west-1 and ap-northeast-1170    ec2_usw1 = boto3.resource('ec2', region_name='us-west-1')171    vpc_usw1 = ec2_usw1.create_vpc(CidrBlock='10.90.0.0/16')172    ec2_apn1 = boto3.resource('ec2', region_name='ap-northeast-1')173    vpc_apn1 = ec2_apn1.create_vpc(CidrBlock='10.20.0.0/16')174    # create peering175    vpc_pcx_usw1 = ec2_usw1.create_vpc_peering_connection(176        VpcId=vpc_usw1.id,177        PeerVpcId=vpc_apn1.id,178        PeerRegion='ap-northeast-1',179    )180    # reject peering from ap-northeast-1181    ec2_apn1 = boto3.client('ec2', region_name='ap-northeast-1')182    ec2_usw1 = boto3.client('ec2', region_name='us-west-1')183    del_pcx_apn1 = ec2_apn1.delete_vpc_peering_connection(184        VpcPeeringConnectionId=vpc_pcx_usw1.id185    )186    des_pcx_apn1 = ec2_usw1.describe_vpc_peering_connections(187        VpcPeeringConnectionIds=[vpc_pcx_usw1.id]188    )189    des_pcx_usw1 = ec2_usw1.describe_vpc_peering_connections(190        VpcPeeringConnectionIds=[vpc_pcx_usw1.id]191    )192    del_pcx_apn1['Return'].should.equal(True)193    des_pcx_apn1['VpcPeeringConnections'][0]['Status']['Code'].should.equal('deleted')194    des_pcx_usw1['VpcPeeringConnections'][0]['Status']['Code'].should.equal('deleted')195@mock_ec2196def test_vpc_peering_connections_cross_region_accept_wrong_region():197    # create vpc in us-west-1 and ap-northeast-1198    ec2_usw1 = boto3.resource('ec2', region_name='us-west-1')199    vpc_usw1 = ec2_usw1.create_vpc(CidrBlock='10.90.0.0/16')200    ec2_apn1 = boto3.resource('ec2', region_name='ap-northeast-1')201    vpc_apn1 = ec2_apn1.create_vpc(CidrBlock='10.20.0.0/16')202    # create peering203    vpc_pcx_usw1 = ec2_usw1.create_vpc_peering_connection(204        VpcId=vpc_usw1.id,205        PeerVpcId=vpc_apn1.id,206        PeerRegion='ap-northeast-1',207    )208    # accept wrong peering from us-west-1 which will raise error209    ec2_apn1 = boto3.client('ec2', region_name='ap-northeast-1')210    ec2_usw1 = boto3.client('ec2', region_name='us-west-1')211    with assert_raises(ClientError) as cm:212        ec2_usw1.accept_vpc_peering_connection(213            VpcPeeringConnectionId=vpc_pcx_usw1.id214        )215    cm.exception.response['Error']['Code'].should.equal('OperationNotPermitted')216    exp_msg = 'Incorrect region ({0}) specified for this request.VPC ' \217              'peering connection {1} must be ' \218              'accepted in region {2}'.format('us-west-1', vpc_pcx_usw1.id, 'ap-northeast-1')219    cm.exception.response['Error']['Message'].should.equal(exp_msg)220@mock_ec2221def test_vpc_peering_connections_cross_region_reject_wrong_region():222    # create vpc in us-west-1 and ap-northeast-1223    ec2_usw1 = boto3.resource('ec2', region_name='us-west-1')224    vpc_usw1 = ec2_usw1.create_vpc(CidrBlock='10.90.0.0/16')225    ec2_apn1 = boto3.resource('ec2', region_name='ap-northeast-1')226    vpc_apn1 = ec2_apn1.create_vpc(CidrBlock='10.20.0.0/16')227    # create peering228    vpc_pcx_usw1 = ec2_usw1.create_vpc_peering_connection(229        VpcId=vpc_usw1.id,230        PeerVpcId=vpc_apn1.id,231        PeerRegion='ap-northeast-1',232    )233    # reject wrong peering from us-west-1 which will raise error234    ec2_apn1 = boto3.client('ec2', region_name='ap-northeast-1')235    ec2_usw1 = boto3.client('ec2', region_name='us-west-1')236    with assert_raises(ClientError) as cm:237        ec2_usw1.reject_vpc_peering_connection(238            VpcPeeringConnectionId=vpc_pcx_usw1.id239        )240    cm.exception.response['Error']['Code'].should.equal('OperationNotPermitted')241    exp_msg = 'Incorrect region ({0}) specified for this request.VPC ' \242              'peering connection {1} must be accepted or ' \243              'rejected in region {2}'.format('us-west-1', vpc_pcx_usw1.id, 'ap-northeast-1')...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
