Exemplo n.º 1
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
Exemplo n.º 2
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