Best Python code snippet using localstack_python
test_vpcs.py
Source:test_vpcs.py
...286 ec2 = boto3.resource('ec2', region_name='us-west-1')287 vpc = ec2.create_vpc(CidrBlock='10.10.42.0/24')288 # Associate/Extend vpc CIDR range up to 5 ciders289 for i in range(43, 47):290 response = ec2.meta.client.associate_vpc_cidr_block(VpcId=vpc.id, CidrBlock='10.10.{}.0/24'.format(i))291 response['CidrBlockAssociation']['CidrBlockState']['State'].should.equal('associating')292 response['CidrBlockAssociation']['CidrBlock'].should.equal('10.10.{}.0/24'.format(i))293 response['CidrBlockAssociation']['AssociationId'].should.contain('vpc-cidr-assoc')294 # Check all associations exist295 vpc = ec2.Vpc(vpc.id)296 vpc.cidr_block_association_set.should.have.length_of(5)297 vpc.cidr_block_association_set[2]['CidrBlockState']['State'].should.equal('associated')298 vpc.cidr_block_association_set[4]['CidrBlockState']['State'].should.equal('associated')299 # Check error on adding 6th association.300 with assert_raises(ClientError) as ex:301 response = ec2.meta.client.associate_vpc_cidr_block(VpcId=vpc.id, CidrBlock='10.10.50.0/22')302 str(ex.exception).should.equal(303 "An error occurred (CidrLimitExceeded) when calling the AssociateVpcCidrBlock "304 "operation: This network '{}' has met its maximum number of allowed CIDRs: 5".format(vpc.id))305@mock_ec2306def test_disassociate_vpc_ipv4_cidr_block():307 ec2 = boto3.resource('ec2', region_name='us-west-1')308 vpc = ec2.create_vpc(CidrBlock='10.10.42.0/24')309 ec2.meta.client.associate_vpc_cidr_block(VpcId=vpc.id, CidrBlock='10.10.43.0/24')310 # Remove an extended cidr block311 vpc = ec2.Vpc(vpc.id)312 non_default_assoc_cidr_block = next(iter([x for x in vpc.cidr_block_association_set if vpc.cidr_block != x['CidrBlock']]), None)313 response = ec2.meta.client.disassociate_vpc_cidr_block(AssociationId=non_default_assoc_cidr_block['AssociationId'])314 response['CidrBlockAssociation']['CidrBlockState']['State'].should.equal('disassociating')315 response['CidrBlockAssociation']['CidrBlock'].should.equal(non_default_assoc_cidr_block['CidrBlock'])316 response['CidrBlockAssociation']['AssociationId'].should.equal(non_default_assoc_cidr_block['AssociationId'])317 # Error attempting to delete a non-existent CIDR_BLOCK association318 with assert_raises(ClientError) as ex:319 response = ec2.meta.client.disassociate_vpc_cidr_block(AssociationId='vpc-cidr-assoc-BORING123')320 str(ex.exception).should.equal(321 "An error occurred (InvalidVpcCidrBlockAssociationIdError.NotFound) when calling the "322 "DisassociateVpcCidrBlock operation: The vpc CIDR block association ID "323 "'vpc-cidr-assoc-BORING123' does not exist")324 # Error attempting to delete Primary CIDR BLOCK association325 vpc_base_cidr_assoc_id = next(iter([x for x in vpc.cidr_block_association_set326 if vpc.cidr_block == x['CidrBlock']]), {})['AssociationId']327 with assert_raises(ClientError) as ex:328 response = ec2.meta.client.disassociate_vpc_cidr_block(AssociationId=vpc_base_cidr_assoc_id)329 str(ex.exception).should.equal(330 "An error occurred (OperationNotPermitted) when calling the DisassociateVpcCidrBlock operation: "331 "The vpc CIDR block with association ID {} may not be disassociated. It is the primary "332 "IPv4 CIDR block of the VPC".format(vpc_base_cidr_assoc_id))333@mock_ec2334def test_cidr_block_association_filters():335 ec2 = boto3.resource('ec2', region_name='us-west-1')336 vpc1 = ec2.create_vpc(CidrBlock='10.90.0.0/16')337 vpc2 = ec2.create_vpc(CidrBlock='10.91.0.0/16')338 ec2.meta.client.associate_vpc_cidr_block(VpcId=vpc2.id, CidrBlock='10.10.0.0/19')339 vpc3 = ec2.create_vpc(CidrBlock='10.92.0.0/24')340 ec2.meta.client.associate_vpc_cidr_block(VpcId=vpc3.id, CidrBlock='10.92.1.0/24')341 ec2.meta.client.associate_vpc_cidr_block(VpcId=vpc3.id, CidrBlock='10.92.2.0/24')342 vpc3_assoc_response = ec2.meta.client.associate_vpc_cidr_block(VpcId=vpc3.id, CidrBlock='10.92.3.0/24')343 # Test filters for a cidr-block in all VPCs cidr-block-associations344 filtered_vpcs = list(ec2.vpcs.filter(Filters=[{'Name': 'cidr-block-association.cidr-block',345 'Values': ['10.10.0.0/19']}]))346 filtered_vpcs.should.be.length_of(1)347 filtered_vpcs[0].id.should.equal(vpc2.id)348 # Test filter for association id in VPCs349 association_id = vpc3_assoc_response['CidrBlockAssociation']['AssociationId']350 filtered_vpcs = list(ec2.vpcs.filter(Filters=[{'Name': 'cidr-block-association.association-id',351 'Values': [association_id]}]))352 filtered_vpcs.should.be.length_of(1)353 filtered_vpcs[0].id.should.equal(vpc3.id)354 # Test filter for association state in VPC - this will never show anything in this test355 filtered_vpcs = list(ec2.vpcs.filter(Filters=[{'Name': 'cidr-block-association.association-id',356 'Values': ['failing']}]))357 filtered_vpcs.should.be.length_of(0)358@mock_ec2359def test_vpc_associate_ipv6_cidr_block():360 ec2 = boto3.resource('ec2', region_name='us-west-1')361 # Test create VPC with IPV6 cidr range362 vpc = ec2.create_vpc(CidrBlock='10.10.42.0/24', AmazonProvidedIpv6CidrBlock=True)363 ipv6_cidr_block_association_set = next(iter(vpc.ipv6_cidr_block_association_set), None)364 ipv6_cidr_block_association_set['Ipv6CidrBlockState']['State'].should.equal('associated')365 ipv6_cidr_block_association_set['Ipv6CidrBlock'].should.contain('::/56')366 ipv6_cidr_block_association_set['AssociationId'].should.contain('vpc-cidr-assoc')367 # Test Fail on adding 2nd IPV6 association - AWS only allows 1 at this time!368 with assert_raises(ClientError) as ex:369 response = ec2.meta.client.associate_vpc_cidr_block(VpcId=vpc.id, AmazonProvidedIpv6CidrBlock=True)370 str(ex.exception).should.equal(371 "An error occurred (CidrLimitExceeded) when calling the AssociateVpcCidrBlock "372 "operation: This network '{}' has met its maximum number of allowed CIDRs: 1".format(vpc.id))373 # Test associate ipv6 cidr block after vpc created374 vpc = ec2.create_vpc(CidrBlock='10.10.50.0/24')375 response = ec2.meta.client.associate_vpc_cidr_block(VpcId=vpc.id, AmazonProvidedIpv6CidrBlock=True)376 response['Ipv6CidrBlockAssociation']['Ipv6CidrBlockState']['State'].should.equal('associating')377 response['Ipv6CidrBlockAssociation']['Ipv6CidrBlock'].should.contain('::/56')378 response['Ipv6CidrBlockAssociation']['AssociationId'].should.contain('vpc-cidr-assoc-')379 # Check on describe vpc that has ipv6 cidr block association380 vpc = ec2.Vpc(vpc.id)381 vpc.ipv6_cidr_block_association_set.should.be.length_of(1)382@mock_ec2383def test_vpc_disassociate_ipv6_cidr_block():384 ec2 = boto3.resource('ec2', region_name='us-west-1')385 # Test create VPC with IPV6 cidr range386 vpc = ec2.create_vpc(CidrBlock='10.10.42.0/24', AmazonProvidedIpv6CidrBlock=True)387 # Test disassociating the only IPV6388 assoc_id = vpc.ipv6_cidr_block_association_set[0]['AssociationId']389 response = ec2.meta.client.disassociate_vpc_cidr_block(AssociationId=assoc_id)390 response['Ipv6CidrBlockAssociation']['Ipv6CidrBlockState']['State'].should.equal('disassociating')391 response['Ipv6CidrBlockAssociation']['Ipv6CidrBlock'].should.contain('::/56')392 response['Ipv6CidrBlockAssociation']['AssociationId'].should.equal(assoc_id)393@mock_ec2394def test_ipv6_cidr_block_association_filters():395 ec2 = boto3.resource('ec2', region_name='us-west-1')396 vpc1 = ec2.create_vpc(CidrBlock='10.90.0.0/16')397 vpc2 = ec2.create_vpc(CidrBlock='10.91.0.0/16', AmazonProvidedIpv6CidrBlock=True)398 vpc2_assoc_ipv6_assoc_id = vpc2.ipv6_cidr_block_association_set[0]['AssociationId']399 ec2.meta.client.associate_vpc_cidr_block(VpcId=vpc2.id, CidrBlock='10.10.0.0/19')400 vpc3 = ec2.create_vpc(CidrBlock='10.92.0.0/24')401 ec2.meta.client.associate_vpc_cidr_block(VpcId=vpc3.id, CidrBlock='10.92.1.0/24')402 ec2.meta.client.associate_vpc_cidr_block(VpcId=vpc3.id, CidrBlock='10.92.2.0/24')403 response = ec2.meta.client.associate_vpc_cidr_block(VpcId=vpc3.id, AmazonProvidedIpv6CidrBlock=True)404 vpc3_ipv6_cidr_block = response['Ipv6CidrBlockAssociation']['Ipv6CidrBlock']405 vpc4 = ec2.create_vpc(CidrBlock='10.95.0.0/16') # Here for its looks406 # Test filters for an ipv6 cidr-block in all VPCs cidr-block-associations407 filtered_vpcs = list(ec2.vpcs.filter(Filters=[{'Name': 'ipv6-cidr-block-association.ipv6-cidr-block',408 'Values': [vpc3_ipv6_cidr_block]}]))409 filtered_vpcs.should.be.length_of(1)410 filtered_vpcs[0].id.should.equal(vpc3.id)411 # Test filter for association id in VPCs412 filtered_vpcs = list(ec2.vpcs.filter(Filters=[{'Name': 'ipv6-cidr-block-association.association-id',413 'Values': [vpc2_assoc_ipv6_assoc_id]}]))414 filtered_vpcs.should.be.length_of(1)415 filtered_vpcs[0].id.should.equal(vpc2.id)416 # Test filter for association state in VPC - this will never show anything in this test417 filtered_vpcs = list(ec2.vpcs.filter(Filters=[{'Name': 'ipv6-cidr-block-association.state',...
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!!