Esempio n. 1
0
def test_ec2_zombie_vpc_delete():
    """
    This method tests the zombie vpc delete
    :return:
    """
    zombie_cluster_resources = ZombieClusterResources(
        cluster_prefix='kubernetes.io/cluster/',
        delete=True,
        cluster_tag='kubernetes.io/cluster/integration-test-cluster',
        region='us-east-2',
        resource_name='zombie_cluster_vpc')
    zombie_cluster_resources.zombie_cluster_vpc()
    assert not EC2Operations().find_vpc(
        'kubernetes.io/cluster/integration-test-cluster')
Esempio n. 2
0
def test_ec2_zombie_vpc_exists():
    """
    This method checks any zombie VPCs or not
    :return:
    """
    create_vpc()
    zombie_cluster_resources = ZombieClusterResources(
        cluster_prefix='kubernetes.io/cluster/',
        delete=False,
        cluster_tag='kubernetes.io/cluster/integration-test-cluster',
        region='us-east-2',
        resource_name='zombie_cluster_vpc')
    assert len(zombie_cluster_resources.zombie_cluster_vpc()) >= 1
def test_delete_vpc():
    """
    This method tests the deletion VPC and its dependencies are deleted.
    :return:
    """
    ec2_client = boto3.client('ec2', region_name=region_name)
    vpc_id = ec2_client.create_vpc(CidrBlock='10.0.0.0/16',
                                   TagSpecifications=[{
                                       'ResourceType': 'vpc',
                                       'Tags': tags
                                   }])['Vpc']['VpcId']
    subnet1 = ec2_client.create_subnet(CidrBlock='10.0.1.0/24',
                                       VpcId=vpc_id)['Subnet']['SubnetId']
    ec2_client.create_subnet(CidrBlock='10.0.2.0/24', VpcId=vpc_id)

    volume = ec2_client.create_volume(AvailabilityZone='us-east-2', Size=123)
    ec2_client.create_tags(Resources=[volume['VolumeId']], Tags=tags)

    elb = boto3.client('elb', region_name=region_name)
    elb.create_load_balancer(Listeners=[{
        'InstancePort': 80,
        'InstanceProtocol': 'HTTP',
        'LoadBalancerPort': 80,
        'Protocol': 'HTTP'
    }],
                             LoadBalancerName='test-load-balancer',
                             Tags=tags)
    elbv2 = boto3.client('elbv2', region_name=region_name)
    elbv2.create_load_balancer(Name='test-load-balancer-v2',
                               Tags=tags,
                               Subnets=[subnet1])

    dhcp = ec2_client.create_dhcp_options(TagSpecifications=[{
        'ResourceType': 'dhcp-options',
        'Tags': tags
    }],
                                          DhcpConfigurations=[{
                                              'Key':
                                              'domain-name-servers',
                                              'Values':
                                              ['10.2.5.1', '10.2.5.2']
                                          }])
    ec2_client.associate_dhcp_options(
        VpcId=vpc_id, DhcpOptionsId=dhcp['DhcpOptions']['DhcpOptionsId'])

    # route_table_id = ec2_client.create_route_table(VpcId=vpc_id,
    #                                                TagSpecifications=[{'ResourceType': 'route-table',
    #                                                                    'Tags': tags}])['RouteTable']['RouteTableId']

    sg1 = ec2_client.create_security_group(
        VpcId=vpc_id,
        Description='Testing the security groups',
        TagSpecifications=[{
            'ResourceType': 'security-group',
            'Tags': tags
        }],
        GroupName='sg-testing')['GroupId']

    ec2_client.create_vpc_endpoint(VpcEndpointType='Interface',
                                   VpcId=vpc_id,
                                   TagSpecifications=[{
                                       'ResourceType': 'vpc',
                                       'Tags': tags
                                   }],
                                   ServiceName='com.amazonaws.us-east-2.s3')

    ec2_client.create_nat_gateway(TagSpecifications=[{
        'ResourceType': 'nat-gateway',
        'Tags': tags
    }],
                                  SubnetId=subnet1)

    ec2_client.create_network_acl(VpcId=vpc_id,
                                  TagSpecifications=[{
                                      'ResourceType': 'network-acl',
                                      'Tags': tags
                                  }])

    ing_id = ec2_client.create_internet_gateway(
    )['InternetGateway']['InternetGatewayId']
    ec2_client.create_tags(Resources=[ing_id], Tags=tags)
    ec2_client.attach_internet_gateway(InternetGatewayId=ing_id, VpcId=vpc_id)

    allocation_id = ec2_client.allocate_address(Domain='vpc',
                                                TagSpecifications=[{
                                                    'ResourceType':
                                                    'elastic-ip',
                                                    'Tags':
                                                    tags
                                                }])['AllocationId']

    network_interface_id = ec2_client.create_network_interface(
        SubnetId=subnet1,
        Groups=[sg1],
        Description='testing the internet gateway'
    )['NetworkInterface']['NetworkInterfaceId']
    ec2_client.associate_address(NetworkInterfaceId=network_interface_id,
                                 AllocationId=allocation_id)

    zombie_cluster_resources = ZombieClusterResources(
        cluster_prefix='kubernetes.io/cluster/',
        delete=True,
        cluster_tag='kubernetes.io/cluster/unittest-test-cluster',
        region=region_name,
        resource_name='zombie_cluster_vpc')
    zombie_cluster_resources.zombie_cluster_vpc()
    assert not EC2Operations(region_name).find_vpc(
        'kubernetes.io/cluster/unittest-test-cluster')