diff --git a/amicleaner/core.py b/amicleaner/core.py index f4b18ae..b70ff01 100644 --- a/amicleaner/core.py +++ b/amicleaner/core.py @@ -119,13 +119,14 @@ def remove_amis(self, amis): self.ec2.deregister_image(ImageId=ami.id) print("{0} deregistered".format(ami.id)) for block_device in ami.block_device_mappings: - try: - self.ec2.delete_snapshot( - SnapshotId=block_device.snapshot_id - ) - except ClientError: - failed_snapshots.append(block_device.snapshot_id) - print("{0} deleted\n".format(block_device.snapshot_id)) + if block_device.snapshot_id is not None: + try: + self.ec2.delete_snapshot( + SnapshotId=block_device.snapshot_id + ) + except ClientError: + failed_snapshots.append(block_device.snapshot_id) + print("{0} deleted\n".format(block_device.snapshot_id)) return failed_snapshots diff --git a/tests/test_core.py b/tests/test_core.py index f4a2736..bb0d0f5 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -4,7 +4,7 @@ from moto import mock_ec2 from amicleaner.core import AMICleaner, OrphanSnapshotCleaner -from amicleaner.resources.models import AMI, AWSTag +from amicleaner.resources.models import AMI, AWSTag, AWSBlockDevice def test_map_candidates_with_null_arguments(): @@ -205,6 +205,23 @@ def test_reduce_without_rotation_number(): assert AMICleaner().reduce_candidates(candidates) == candidates +def test_reduce_without_snapshot_id(): + # creating block device + first_block_device = AWSBlockDevice() + first_block_device.snapshot_id = None + + # creating tests objects + first_ami = AMI() + first_ami.id = 'ami-28c2b348' + first_ami.name = "ubuntu-20160102" + first_ami.block_device_mappings.append(first_block_device) + + # creating amis to drop dict + candidates = [first_ami] + + assert AMICleaner().reduce_candidates(candidates) == candidates + + def test_reduce(): # creating tests objects first_ami = AMI()