Best Python code snippet using localstack_python
test_internet_gateways.py
Source:test_internet_gateways.py  
...34    conn = boto.connect_vpc("the_key", "the_secret")35    igw = conn.create_internet_gateway()36    vpc = conn.create_vpc(VPC_CIDR)37    with assert_raises(EC2ResponseError) as ex:38        conn.attach_internet_gateway(igw.id, vpc.id, dry_run=True)39    ex.exception.error_code.should.equal("DryRunOperation")40    ex.exception.status.should.equal(400)41    ex.exception.message.should.equal(42        "An error occurred (DryRunOperation) when calling the AttachInternetGateway operation: Request would have succeeded, but DryRun flag is set"43    )44    conn.attach_internet_gateway(igw.id, vpc.id)45    igw = conn.get_all_internet_gateways()[0]46    igw.attachments[0].vpc_id.should.be.equal(vpc.id)47@mock_ec2_deprecated48def test_igw_attach_bad_vpc():49    """ internet gateway fail to attach w/ bad vpc """50    conn = boto.connect_vpc("the_key", "the_secret")51    igw = conn.create_internet_gateway()52    with assert_raises(EC2ResponseError) as cm:53        conn.attach_internet_gateway(igw.id, BAD_VPC)54    cm.exception.code.should.equal("InvalidVpcID.NotFound")55    cm.exception.status.should.equal(400)56    cm.exception.request_id.should_not.be.none57@mock_ec2_deprecated58def test_igw_attach_twice():59    """ internet gateway fail to attach twice """60    conn = boto.connect_vpc("the_key", "the_secret")61    igw = conn.create_internet_gateway()62    vpc1 = conn.create_vpc(VPC_CIDR)63    vpc2 = conn.create_vpc(VPC_CIDR)64    conn.attach_internet_gateway(igw.id, vpc1.id)65    with assert_raises(EC2ResponseError) as cm:66        conn.attach_internet_gateway(igw.id, vpc2.id)67    cm.exception.code.should.equal("Resource.AlreadyAssociated")68    cm.exception.status.should.equal(400)69    cm.exception.request_id.should_not.be.none70@mock_ec2_deprecated71def test_igw_detach():72    """ internet gateway detach"""73    conn = boto.connect_vpc("the_key", "the_secret")74    igw = conn.create_internet_gateway()75    vpc = conn.create_vpc(VPC_CIDR)76    conn.attach_internet_gateway(igw.id, vpc.id)77    with assert_raises(EC2ResponseError) as ex:78        conn.detach_internet_gateway(igw.id, vpc.id, dry_run=True)79    ex.exception.error_code.should.equal("DryRunOperation")80    ex.exception.status.should.equal(400)81    ex.exception.message.should.equal(82        "An error occurred (DryRunOperation) when calling the DetachInternetGateway operation: Request would have succeeded, but DryRun flag is set"83    )84    conn.detach_internet_gateway(igw.id, vpc.id)85    igw = conn.get_all_internet_gateways()[0]86    igw.attachments.should.have.length_of(0)87@mock_ec2_deprecated88def test_igw_detach_wrong_vpc():89    """ internet gateway fail to detach w/ wrong vpc """90    conn = boto.connect_vpc("the_key", "the_secret")91    igw = conn.create_internet_gateway()92    vpc1 = conn.create_vpc(VPC_CIDR)93    vpc2 = conn.create_vpc(VPC_CIDR)94    conn.attach_internet_gateway(igw.id, vpc1.id)95    with assert_raises(EC2ResponseError) as cm:96        conn.detach_internet_gateway(igw.id, vpc2.id)97    cm.exception.code.should.equal("Gateway.NotAttached")98    cm.exception.status.should.equal(400)99    cm.exception.request_id.should_not.be.none100@mock_ec2_deprecated101def test_igw_detach_invalid_vpc():102    """ internet gateway fail to detach w/ invalid vpc """103    conn = boto.connect_vpc("the_key", "the_secret")104    igw = conn.create_internet_gateway()105    vpc = conn.create_vpc(VPC_CIDR)106    conn.attach_internet_gateway(igw.id, vpc.id)107    with assert_raises(EC2ResponseError) as cm:108        conn.detach_internet_gateway(igw.id, BAD_VPC)109    cm.exception.code.should.equal("Gateway.NotAttached")110    cm.exception.status.should.equal(400)111    cm.exception.request_id.should_not.be.none112@mock_ec2_deprecated113def test_igw_detach_unattached():114    """ internet gateway fail to detach unattached """115    conn = boto.connect_vpc("the_key", "the_secret")116    igw = conn.create_internet_gateway()117    vpc = conn.create_vpc(VPC_CIDR)118    with assert_raises(EC2ResponseError) as cm:119        conn.detach_internet_gateway(igw.id, vpc.id)120    cm.exception.code.should.equal("Gateway.NotAttached")121    cm.exception.status.should.equal(400)122    cm.exception.request_id.should_not.be.none123@mock_ec2_deprecated124def test_igw_delete():125    """ internet gateway delete"""126    conn = boto.connect_vpc("the_key", "the_secret")127    vpc = conn.create_vpc(VPC_CIDR)128    conn.get_all_internet_gateways().should.have.length_of(0)129    igw = conn.create_internet_gateway()130    conn.get_all_internet_gateways().should.have.length_of(1)131    with assert_raises(EC2ResponseError) as ex:132        conn.delete_internet_gateway(igw.id, dry_run=True)133    ex.exception.error_code.should.equal("DryRunOperation")134    ex.exception.status.should.equal(400)135    ex.exception.message.should.equal(136        "An error occurred (DryRunOperation) when calling the DeleteInternetGateway operation: Request would have succeeded, but DryRun flag is set"137    )138    conn.delete_internet_gateway(igw.id)139    conn.get_all_internet_gateways().should.have.length_of(0)140@mock_ec2_deprecated141def test_igw_delete_attached():142    """ internet gateway fail to delete attached """143    conn = boto.connect_vpc("the_key", "the_secret")144    igw = conn.create_internet_gateway()145    vpc = conn.create_vpc(VPC_CIDR)146    conn.attach_internet_gateway(igw.id, vpc.id)147    with assert_raises(EC2ResponseError) as cm:148        conn.delete_internet_gateway(igw.id)149    cm.exception.code.should.equal("DependencyViolation")150    cm.exception.status.should.equal(400)151    cm.exception.request_id.should_not.be.none152@mock_ec2_deprecated153def test_igw_desribe():154    """ internet gateway fetch by id """155    conn = boto.connect_vpc("the_key", "the_secret")156    igw = conn.create_internet_gateway()157    igw_by_search = conn.get_all_internet_gateways([igw.id])[0]158    igw.id.should.equal(igw_by_search.id)159@mock_ec2_deprecated160def test_igw_describe_bad_id():161    """ internet gateway fail to fetch by bad id """162    conn = boto.connect_vpc("the_key", "the_secret")163    with assert_raises(EC2ResponseError) as cm:164        conn.get_all_internet_gateways([BAD_IGW])165    cm.exception.code.should.equal("InvalidInternetGatewayID.NotFound")166    cm.exception.status.should.equal(400)167    cm.exception.request_id.should_not.be.none168@mock_ec2_deprecated169def test_igw_filter_by_vpc_id():170    """ internet gateway filter by vpc id """171    conn = boto.connect_vpc("the_key", "the_secret")172    igw1 = conn.create_internet_gateway()173    igw2 = conn.create_internet_gateway()174    vpc = conn.create_vpc(VPC_CIDR)175    conn.attach_internet_gateway(igw1.id, vpc.id)176    result = conn.get_all_internet_gateways(filters={"attachment.vpc-id": vpc.id})177    result.should.have.length_of(1)178    result[0].id.should.equal(igw1.id)179@mock_ec2_deprecated180def test_igw_filter_by_tags():181    """ internet gateway filter by vpc id """182    conn = boto.connect_vpc("the_key", "the_secret")183    igw1 = conn.create_internet_gateway()184    igw2 = conn.create_internet_gateway()185    igw1.add_tag("tests", "yes")186    result = conn.get_all_internet_gateways(filters={"tag:tests": "yes"})187    result.should.have.length_of(1)188    result[0].id.should.equal(igw1.id)189@mock_ec2_deprecated190def test_igw_filter_by_internet_gateway_id():191    """ internet gateway filter by internet gateway id """192    conn = boto.connect_vpc("the_key", "the_secret")193    igw1 = conn.create_internet_gateway()194    igw2 = conn.create_internet_gateway()195    result = conn.get_all_internet_gateways(filters={"internet-gateway-id": igw1.id})196    result.should.have.length_of(1)197    result[0].id.should.equal(igw1.id)198@mock_ec2_deprecated199def test_igw_filter_by_attachment_state():200    """ internet gateway filter by attachment state """201    conn = boto.connect_vpc("the_key", "the_secret")202    igw1 = conn.create_internet_gateway()203    igw2 = conn.create_internet_gateway()204    vpc = conn.create_vpc(VPC_CIDR)205    conn.attach_internet_gateway(igw1.id, vpc.id)206    result = conn.get_all_internet_gateways(filters={"attachment.state": "available"})207    result.should.have.length_of(1)208    result[0].id.should.equal(igw1.id)209@mock_ec2210def test_create_internet_gateway_with_tags():211    ec2 = boto3.resource("ec2", region_name="eu-central-1")212    igw = ec2.create_internet_gateway(213        TagSpecifications=[214            {215                "ResourceType": "internet-gateway",216                "Tags": [{"Key": "test", "Value": "TestRouteTable"}],217            }218        ],219    )...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!!
