Skip to content

Commit

Permalink
EC2: test --dry-run
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardobranco777 committed Jul 2, 2023
1 parent 4dc7e49 commit b8ab686
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions tests/test_ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,10 @@ def test_cleanup_images_one_old(ec2_patch):
{'Name': Faker().uuid4(), 'CreationDate': older_than_max_age_str, 'ImageId': 2},
]
}
ec2_patch.dry_run = True
ec2_patch.cleanup_images(ec2_max_age_days)
assert MockedEC2Client.deleted_images == []
ec2_patch.dry_run = False
ec2_patch.cleanup_images(ec2_max_age_days)
assert MockedEC2Client.deleted_images == [2]

Expand Down Expand Up @@ -320,6 +324,10 @@ def test_cleanup_volumes_cleanupcheck(ec2_patch):
{'VolumeId': MockedEC2Client.volumeid_to_delete, 'CreateTime': older_than_max_age_date,
'Tags': [{'Key': 'pcw_ignore', 'Value': '1'}]}, ]
}
ec2_patch.dry_run = True
ec2_patch.cleanup_volumes(ec2_max_age_days)
assert len(MockedEC2Client.deleted_volumes) == 0
ec2_patch.dry_run = False
ec2_patch.cleanup_volumes(ec2_max_age_days)
assert len(MockedEC2Client.deleted_volumes) == 1
assert MockedEC2Client.deleted_volumes[0] == MockedEC2Client.volumeid_to_delete
Expand Down Expand Up @@ -401,40 +409,71 @@ def mocked_get_boolean(config_path, field=None):


def test_delete_internet_gw(ec2_patch):
ec2_patch.dry_run = True
ec2_patch.delete_internet_gw(MockedVpc('vpcId'))
assert MockedVpc.detach_internet_gateway_called == 0
assert MockedCollectionItem.delete_called == 0
ec2_patch.dry_run = False
ec2_patch.delete_internet_gw(MockedVpc('vpcId'))
assert MockedVpc.detach_internet_gateway_called == 1
assert MockedCollectionItem.delete_called == 1


def test_delete_routing_tables(ec2_patch):
ec2_patch.dry_run = True
ec2_patch.delete_routing_tables(MockedVpc('vpcId'), 'vpcId')
assert MockedEC2Client.disassociate_route_table_called == 0
assert MockedEC2Client.delete_route_called == 0
ec2_patch.dry_run = False
ec2_patch.delete_routing_tables(MockedVpc('vpcId'), 'vpcId')
assert MockedEC2Client.disassociate_route_table_called
assert MockedEC2Client.delete_route_called


def test_delete_vpc_endpoints(ec2_patch):
MockedEC2Client.response = {'VpcEndpoints': [{'VpcEndpointId': 'id'}]}
ec2_patch.dry_run = True
ec2_patch.delete_vpc_endpoints('region', 'vpcId')
assert MockedEC2Client.delete_vpc_endpoints_called == 0
ec2_patch.dry_run = False
ec2_patch.delete_vpc_endpoints('region', 'vpcId')
assert MockedEC2Client.delete_vpc_endpoints_called


def test_delete_security_groups(ec2_patch):
ec2_patch.dry_run = True
ec2_patch.delete_security_groups(MockedVpc('vpcId'))
assert MockedCollectionItem.delete_called == 1
ec2_patch.dry_run = False
ec2_patch.delete_security_groups(MockedVpc('vpcId'))
assert MockedCollectionItem.delete_called == 2


def test_delete_vpc_peering_connections(ec2_patch):
MockedEC2Client.response = {'VpcPeeringConnections': [{'VpcPeeringConnectionId': 'id'}]}
ec2_patch.dry_run = True
ec2_patch.delete_vpc_peering_connections('region', 'vpcId')
assert MockedVpcPeeringConnection.delete_called == 0
ec2_patch.dry_run = False
ec2_patch.delete_vpc_peering_connections('region', 'vpcId')
assert MockedVpcPeeringConnection.delete_called


def test_delete_network_acls(ec2_patch):
ec2_patch.dry_run = True
ec2_patch.delete_network_acls(MockedVpc('vpcId'))
assert MockedCollectionItem.delete_called == 2
ec2_patch.dry_run = False
ec2_patch.delete_network_acls(MockedVpc('vpcId'))
assert MockedCollectionItem.delete_called == 3


def test_delete_vpc_subnets(ec2_patch):
ec2_patch.dry_run = True
ec2_patch.delete_vpc_subnets(MockedVpc('vpcId'))
assert MockedCollectionItem.delete_called == 3
assert MockedInterface.delete_called == 0
ec2_patch.dry_run = False
ec2_patch.delete_vpc_subnets(MockedVpc('vpcId'))
assert MockedCollectionItem.delete_called == 4
assert MockedInterface.delete_called
Expand Down

0 comments on commit b8ab686

Please sign in to comment.