Beispiel #1
0
def test_deletion():
    """ Test deletion methods """

    base_ami = "ami-1234abcd"

    parser = parse_args([
        '--keep-previous', '0', '--mapping-key', 'name', '--mapping-values',
        'test-ami'
    ])

    conn = boto3.client('ec2')
    reservation = conn.run_instances(ImageId=base_ami, MinCount=1, MaxCount=1)
    instance = reservation["Instances"][0]

    # create amis
    images = []
    for i in xrange(5):
        image = conn.create_image(InstanceId=instance.get("InstanceId"),
                                  Name="test-ami")
        images.append(image.get("ImageId"))

    # delete one by id
    app = App(parser)
    assert len(AMICleaner(conn).fetch_available_amis()) == 5
    assert app.prepare_delete_amis(candidates=[images[4]],
                                   from_ids=True) is None
    assert len(AMICleaner(conn).fetch_available_amis()) == 4

    # delete with mapping strategy
    candidates = app.fetch_and_prepare()
    assert len(candidates) == 4
    assert app.prepare_delete_amis(candidates) is None
    assert len(AMICleaner(conn).fetch_available_amis()) == 0
Beispiel #2
0
def test_map_with_names():
    # creating tests objects
    first_ami = AMI()
    first_ami.id = 'ami-28c2b348'
    first_ami.name = "ubuntu-20160102"
    first_ami.creation_date = datetime.now()

    second_ami = AMI()
    second_ami.id = 'ami-28c2b349'
    second_ami.name = "ubuntu-20160103"
    second_ami.creation_date = datetime.now()

    third_ami = AMI()
    third_ami.id = 'ami-28c2b350'
    third_ami.name = "debian-20160104"
    third_ami.creation_date = datetime.now()

    # creating amis to drop dict
    candidates = [first_ami, second_ami, third_ami]

    # grouping strategy
    grouping_strategy = {"key": "name", "values": ["ubuntu", "debian"]}

    grouped_amis = AMICleaner().map_candidates(candidates, grouping_strategy)
    assert grouped_amis is not None
    assert len(grouped_amis.get('ubuntu')) == 2
    assert len(grouped_amis.get('debian')) == 1
Beispiel #3
0
def test_fetch_candidates():
    # creating tests objects
    first_ami = AMI()
    first_ami.id = 'ami-28c2b348'
    first_ami.creation_date = datetime.now()

    first_instance = AWSEC2Instance()
    first_instance.id = 'i-9f9f6a2a'
    first_instance.name = "first-instance"
    first_instance.image_id = first_ami.id
    first_instance.launch_time = datetime.now()

    second_ami = AMI()
    second_ami.id = 'unused-ami'
    second_ami.creation_date = datetime.now()

    second_instance = AWSEC2Instance()
    second_instance.id = 'i-9f9f6a2a'
    second_instance.name = "second-instance"
    second_instance.image_id = first_ami.id
    second_instance.launch_time = datetime.now()

    # constructing dicts
    amis_dict = dict()
    amis_dict[first_ami.id] = first_ami
    amis_dict[second_ami.id] = second_ami

    instances_dict = dict()
    instances_dict[first_instance.image_id] = instances_dict
    instances_dict[second_instance.image_id] = second_instance

    # testing filter
    unused_ami_dict = AMICleaner().fetch_candidates(amis_dict, instances_dict)
    assert len(unused_ami_dict) == 1
    assert amis_dict.get('unused-ami') is not None
Beispiel #4
0
def test_reduce_without_rotation_number():
    # creating tests objects
    first_ami = AMI()
    first_ami.id = 'ami-28c2b348'
    first_ami.name = "ubuntu-20160102"
    first_ami.creation_date = datetime(2016, 1, 10)

    # just prod
    second_ami = AMI()
    second_ami.id = 'ami-28c2b349'
    second_ami.name = "ubuntu-20160103"
    second_ami.creation_date = datetime(2016, 1, 11)

    # prod and web-server
    third_ami = AMI()
    third_ami.id = 'ami-28c2b350'
    third_ami.name = "debian-20160104"
    third_ami.creation_date = datetime(2016, 1, 12)

    # creating amis to drop dict
    candidates = [second_ami, third_ami, first_ami]

    reduced, keep_previous, keep_min_day = AMICleaner().reduce_candidates(
        candidates)

    assert reduced == candidates and not keep_previous and not keep_min_day
Beispiel #5
0
    def prepare_delete_amis(self, candidates, from_ids=False):

        """ Prepare deletion of candidates AMIs"""

        failed = []

        if from_ids:
            print(TERM.bold("\nCleaning from {} AMI id(s) ...".format(
                len(candidates))
            ))
            failed = AMICleaner().remove_amis_from_ids(candidates)
        else:
            print(TERM.bold("\nCleaning {} AMIs ...".format(len(candidates))))
            failed = AMICleaner().remove_amis(candidates)

        if failed:
            print(TERM.red("\n{0} failed snapshots".format(len(failed))))
            Printer.print_failed_snapshots(failed)
Beispiel #6
0
def test_fetch_instances():
    """ Tests fetch instances and AMIs """

    base_ami = "ami-1234abcd"

    conn = boto3.client('ec2')
    reservation = conn.run_instances(ImageId=base_ami, MinCount=1, MaxCount=1)
    instance = reservation["Instances"][0]
    assert instance.get("ImageId") == base_ami

    # Test fetch instances method
    assert len(AMICleaner(conn).fetch_instances()) == 1
Beispiel #7
0
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
Beispiel #8
0
def test_map_with_tag_exclusions():
    # tags
    stack_tag = AWSTag()
    stack_tag.key = "stack"
    stack_tag.value = "web-server"

    env_tag = AWSTag()
    env_tag.key = "env"
    env_tag.value = "prod"

    # creating tests objects
    # prod and web-server
    first_ami = AMI()
    first_ami.id = 'ami-28c2b348'
    first_ami.name = "ubuntu-20160102"
    first_ami.tags.append(stack_tag)
    first_ami.tags.append(env_tag)
    first_ami.creation_date = datetime.now()

    # just prod
    second_ami = AMI()
    second_ami.id = 'ami-28c2b349'
    second_ami.name = "ubuntu-20160103"
    second_ami.tags.append(env_tag)
    second_ami.creation_date = datetime.now()

    # just web-server
    third_ami = AMI()
    third_ami.id = 'ami-28c2b350'
    third_ami.name = "debian-20160104"
    third_ami.tags.append(stack_tag)
    third_ami.creation_date = datetime.now()

    # creating amis to drop dict
    candidates = [first_ami, second_ami, third_ami]

    # grouping strategy
    grouping_strategy = {
        "key": "tags",
        "values": ["stack", "env"],
        "excluded": ["prod"]
    }
    grouped_amis = AMICleaner().map_candidates(candidates, grouping_strategy)
    assert grouped_amis is not None
    assert grouped_amis.get("prod") is None
    assert grouped_amis.get("prod.web-server") is None
    assert len(grouped_amis.get("web-server")) == 1
Beispiel #9
0
def test_reduce():
    # creating tests objects
    first_ami = AMI()
    first_ami.id = 'ami-28c2b348'
    first_ami.name = "ubuntu-20160102"
    first_ami.creation_date = datetime(2016, 1, 10)

    # just prod
    second_ami = AMI()
    second_ami.id = 'ami-28c2b349'
    second_ami.name = "ubuntu-20160103"
    second_ami.creation_date = datetime(2016, 1, 11)

    # prod and web-server
    third_ami = AMI()
    third_ami.id = 'ami-28c2b350'
    third_ami.name = "debian-20160104"
    third_ami.creation_date = datetime(2016, 1, 12)

    # keep 2 recent amis
    candidates = [second_ami, third_ami, first_ami]
    rotation_number = 2
    cleaner = AMICleaner()
    left, keep_previous, keep_min_day = cleaner.reduce_candidates(
        candidates, rotation_number)
    assert len(left) == 1
    assert len(keep_previous) == 2
    assert len(keep_min_day) == 0
    assert left[0].id == first_ami.id

    # keep 1 recent ami
    rotation_number = 1
    left, keep_previous, keep_min_day = cleaner.reduce_candidates(
        candidates, rotation_number)
    assert len(left) == 2
    assert len(keep_previous) == 2
    assert len(keep_min_day) == 0
    assert left[0].id == second_ami.id

    # keep 5 recent amis
    rotation_number = 5
    left, keep_previous, keep_min_day = cleaner.reduce_candidates(
        candidates, rotation_number)
    assert len(left) == 0
    assert len(keep_previous) == 2
    assert len(keep_min_day) == 0
Beispiel #10
0
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]

    reduced, keep_previous, keep_min_day = AMICleaner().reduce_candidates(
        candidates)

    assert reduced == candidates and not keep_previous and not keep_min_day
Beispiel #11
0
def test_reduce_without_rotation_number():
    # creating tests objects
    first_ami = AMI()
    first_ami.id = "ami-28c2b348"
    first_ami.name = "ubuntu-20160102"
    first_ami.creation_date = datetime(2016, 1, 10)

    # just prod
    second_ami = AMI()
    second_ami.id = "ami-28c2b349"
    second_ami.name = "ubuntu-20160103"
    second_ami.creation_date = datetime(2016, 1, 11)

    # prod and web-server
    third_ami = AMI()
    third_ami.id = "ami-28c2b350"
    third_ami.name = "debian-20160104"
    third_ami.creation_date = datetime(2016, 1, 12)

    # creating amis to drop dict
    candidates = [second_ami, third_ami, first_ami]

    assert AMICleaner().reduce_candidates(candidates) == candidates
Beispiel #12
0
    def prepare_candidates(self, candidates_amis=None):

        """ From an AMI list apply mapping strategy and filters """

        candidates_amis = candidates_amis or self.fetch_candidates()

        if not candidates_amis:
            return None

        c = AMICleaner()

        mapped_amis = c.map_candidates(
            candidates_amis=candidates_amis,
            mapping_strategy=self.mapping_strategy,
        )

        if not mapped_amis:
            return None

        candidates = []
        report = dict()

        for group_name, amis in mapped_amis.items():
            group_name = group_name or ""

            if not group_name:
                report["no-tags (excluded)"] = amis
            else:
                reduced = c.reduce_candidates(amis, self.keep_previous, self.ami_min_days)
                if reduced:
                    report[group_name] = reduced
                    candidates.extend(reduced)

        Printer.print_report(report, self.full_report)

        return candidates
Beispiel #13
0
def test_map_candidates_with_null_arguments():
    assert AMICleaner().map_candidates({}, {}) == {}
Beispiel #14
0
def test_remove_ami_from_none():
    assert AMICleaner().remove_amis(None) == []