def test__enter__exit__(self): temp_vpc = Temp_VPC(add_internet_gateway=True, add_route_table=True, add_security_group=True, add_subnet=True) with temp_vpc: vpc_id = temp_vpc.vpc.vpc_id internet_gateway_id = temp_vpc.vpc.internet_gateway_id security_group_id = temp_vpc.vpc.security_group_id subnet_id = temp_vpc.vpc.subnet_id route_table_id = temp_vpc.vpc.route_table_id assert self.ec2.internet_gateway_exists( internet_gateway_id) is True assert self.ec2.security_group_exists(security_group_id) is True assert self.ec2.subnet_exists(subnet_id) is True assert self.ec2.route_table_exists(route_table_id) is True assert self.ec2.vpc_exists(vpc_id) is True assert self.ec2.internet_gateway(internet_gateway_id).get( 'Attachments') == [{ 'State': 'available', 'VpcId': vpc_id }] assert self.ec2.internet_gateway_exists(internet_gateway_id) is False assert self.ec2.security_group_exists(security_group_id) is False assert self.ec2.subnet_exists(subnet_id) is False assert self.ec2.route_table_exists(route_table_id) is False assert self.ec2.vpc_exists(vpc_id) is False
def test_subnet_create(self): with Temp_VPC() as temp_vpc: vpc_id = temp_vpc.vpc_id tags = {'Name': 'osbot_aws - test route table'} subnet_id = self.ec2.subnet_create(vpc_id=vpc_id, tags=tags).get('SubnetId') assert self.ec2.subnet_exists(subnet_id) is True self.ec2.subnet_delete(subnet_id) assert self.ec2.subnet_exists(subnet_id) is False
def test_route_table_create(self): with Temp_VPC() as temp_vpc: vpc_id = temp_vpc.vpc_id tags = {'Name': 'osbot_aws - test route table'} route_table_id = self.ec2.route_table_create( vpc_id=vpc_id, tags=tags).get('RouteTableId') assert self.ec2.route_table_exists(route_table_id) is True self.ec2.route_table_delete(route_table_id) assert self.ec2.route_table_exists(route_table_id) is False
def test_using_Temp_VPC(self): # this class using VPC for its creation and deleting steps, and if no exceptions are # thrown it means that everything went ok temp_vpc = Temp_VPC(add_internet_gateway=True, add_route_table=True, add_security_group=True, add_subnet=True) with temp_vpc: pass # todo add tests to check correct setup of VPC environment
def test_route_create(self): temp_vpc = Temp_VPC(add_route_table=True, add_internet_gateway=True) with temp_vpc: route_table_id = temp_vpc.vpc.route_table_id internet_gateway_id = temp_vpc.vpc.internet_gateway_id self.ec2.route_create(route_table_id=route_table_id, internet_gateway_id=internet_gateway_id) route_table = self.ec2.route_table(route_table_id=route_table_id) assert route_table.get('Routes').pop() == { 'DestinationCidrBlock': '0.0.0.0/0', 'GatewayId': internet_gateway_id, 'Origin': 'CreateRoute', 'State': 'active' }
def test_security_group_authorize_ingress(self): security_group_name = 'SSH-ONLY' description = 'only allow SSH traffic' port = 22 account_id = STS().current_account_id() with Temp_VPC() as temp_vpc: vpc_id = temp_vpc.vpc_id security_group_id = self.ec2.security_group_create( security_group_name=security_group_name, description=description, vpc_id=vpc_id).get('data').get('security_group_id') self.ec2.security_group_authorize_ingress( security_group_id=security_group_id, port=port) security_group = self.ec2.security_group(security_group_id) assert security_group == { 'Description': description, 'GroupId': security_group_id, 'GroupName': security_group_name, 'IpPermissions': [{ 'FromPort': port, 'IpProtocol': 'tcp', 'IpRanges': [{ 'CidrIp': '0.0.0.0/0' }], 'Ipv6Ranges': [], 'PrefixListIds': [], 'ToPort': port, 'UserIdGroupPairs': [] }], 'IpPermissionsEgress': [{ 'IpProtocol': '-1', 'IpRanges': [{ 'CidrIp': '0.0.0.0/0' }], 'Ipv6Ranges': [], 'PrefixListIds': [], 'UserIdGroupPairs': [] }], 'OwnerId': account_id, 'VpcId': vpc_id } assert self.ec2.security_group_delete(security_group_id) is True
def test_route_table_associate(self): temp_vpc = Temp_VPC(add_route_table=True, add_subnet=True) with temp_vpc: route_table_id = temp_vpc.vpc.route_table_id subnet_id = temp_vpc.vpc.subnet_id association_id = self.ec2.route_table_associate( route_table_id=route_table_id, subnet_id=subnet_id).get('AssociationId') route_table = self.ec2.route_table(route_table_id) association = route_table.get('Associations')[0] assert association.get('RouteTableId') == route_table_id assert association.get('SubnetId') == subnet_id self.ec2.route_table_disassociate(association_id) route_table = self.ec2.route_table(route_table_id) assert route_table.get('Associations') == []
def test_internet_gateways(self): temp_vpc = Temp_VPC(add_internet_gateway=True) with temp_vpc: internet_gateway_id = temp_vpc.vpc.internet_gateway_id assert internet_gateway_id in self.ec2.internet_gateways( index_by='InternetGatewayId')
def test_ec2_with_ssh_support(self): import logging #logging.getLogger('botocore').setLevel(logging.DEBUG) #logging.getLogger('boto3').setLevel(logging.DEBUG) image_id = AMI().amazon_linux_2() name = random_string(prefix='test_ec2_with_ssh_support-') tags = {'Name': f'osbot_aws - {name}'} key_name = f'osbot-{name}' allow_port = 22 key_pair = self.ec2.key_pair_create_to_file(key_name=key_name) key_pair_id = key_pair.get('key_pair_id' ) path_key_pair = key_pair.get('path_key_pair') temp_vpc = Temp_VPC(add_security_group=True, add_subnet=True,add_internet_gateway=True,add_route_table=True) with temp_vpc: vpc_id = temp_vpc.vpc.vpc_id internet_gateway_id = temp_vpc.vpc.internet_gateway_id security_group_id = temp_vpc.vpc.security_group_id subnet_id = temp_vpc.vpc.subnet_id route_table_id = temp_vpc.vpc.route_table_id # self.ec2.vpc_attribute_set (vpc_id, 'EnableDnsSupport' , True) # self.ec2.vpc_attribute_set (vpc_id, 'EnableDnsHostnames', True) # self.ec2.route_create (route_table_id = route_table_id , internet_gateway_id = internet_gateway_id) # self.ec2.route_table_associate (route_table_id = route_table_id , subnet_id = subnet_id ) # self.ec2.security_group_authorize_ingress(security_group_id = security_group_id , port = allow_port ) network_interface = {'SubnetId' : subnet_id, 'DeviceIndex' : 0 , 'AssociatePublicIpAddress': True , 'Groups' : [security_group_id]} kwargs = { "image_id" : image_id , "key_name" : key_name , "name" : name , "network_interface": network_interface , "tags" : tags } instance_id = self.ec2.instance_create(**kwargs) print_now() pprint(self.ec2.instance_details(instance_id=instance_id)) #self.ec2.wait_for_instance_exists(instance_id=instance_id) # # print_now() # # pprint(self.ec2.instance_details(instance_id=instance_id)) # # self.ec2.wait_for_instance_running(instance_id=instance_id) # # # # print_now() # # pprint(self.ec2.instance_details(instance_id=instance_id)) # # self.ec2.wait_for_instance_status_ok(instance_id=instance_id) # instance_id = self.ec2.instance_delete(instance_id) # print_now() # pprint(self.ec2.instance_details(instance_id=instance_id)) # # this was throwing error "botocore.exceptions.WaiterError: Waiter InstanceTerminated failed: Waiter encountered a terminal failure state: For expression "Reservations[].Instances[].State.Name" we matched expected path: "pending" at least once" self.ec2.wait_for_instance_terminated(instance_id=instance_id) # print_now() pprint(self.ec2.instance_details(instance_id=instance_id)) assert self.ec2.key_pair_delete(key_pair_id=key_pair_id) is True