예제 #1
0
def test_deletion_filtered_tags():
    """ Test deletion methods """

    # creating tests objects
    first_ami = AMI()
    first_ami.name = "test-ami1"
    first_ami.id = 'ami-28c2b348'
    first_ami.creation_date = "2017-11-04T01:35:31.000Z"

    second_ami = AMI()
    second_ami.name = "test-ami2"
    second_ami.id = 'ami-28c2b349'
    second_ami.creation_date = "2017-11-04T01:35:31.000Z"
    second_ami.tags = [
        AWSTag.object_with_json(dict(Key="env", Value="prod")),
        AWSTag.object_with_json(dict(Key="role", Value="nginx")),
    ]

    third_ami = AMI()
    third_ami.name = "test-ami3"
    third_ami.id = 'ami-28c2b350'
    third_ami.creation_date = "2017-11-04T01:35:31.000Z"
    third_ami.tags = [
        AWSTag.object_with_json(dict(Key="env", Value="dev")),
        AWSTag.object_with_json(dict(Key="role", Value="nginx")),
    ]

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

    parser = parse_args(
        [
            '--keep-previous', '0',
            '--tag', 'role=nginx',
        ]
    )

    app = App(parser)
    # testing filter
    candidates = app.fetch_candidates(amis_dict)

    candidates_tobedeleted = app.prepare_candidates(candidates)
    assert len(candidates) == 3
    assert candidates_tobedeleted == [second_ami, third_ami]

    parser = parse_args(
        [
            '--keep-previous', '0',
            '--tag', 'role=nginx',
            '--tag', 'env=dev',
        ]
    )

    app = App(parser)
    candidates_tobedeleted2 = app.prepare_candidates(candidates)
    assert len(candidates) == 3
    assert candidates_tobedeleted2 == [third_ami]
예제 #2
0
def test_deletion_ami_min_days():
    """ Test deletion methods """

    # creating tests objects
    first_ami = AMI()
    first_ami.name = "test-ami"
    first_ami.id = "ami-28c2b348"
    first_ami.creation_date = "2017-11-04T01:35:31.000Z"

    second_ami = AMI()
    second_ami.name = "test-ami"
    second_ami.id = "ami-28c2b349"
    second_ami.creation_date = "2017-11-04T01:35:31.000Z"

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

    parser = parse_args(
        [
            "--keep-previous",
            "0",
            "--ami-min-days",
            "1",
            "--mapping-key",
            "name",
            "--mapping-values",
            "test-ami",
        ]
    )

    app = App(parser)
    # testing filter
    candidates = app.fetch_candidates(amis_dict)

    candidates_tobedeleted = app.prepare_candidates(candidates)
    assert len(candidates) == 2
    assert len(candidates_tobedeleted) == 2

    parser = parse_args(
        [
            "--keep-previous",
            "0",
            "--ami-min-days",
            "10000",
            "--mapping-key",
            "name",
            "--mapping-values",
            "test-ami",
        ]
    )

    app = App(parser)
    candidates_tobedeleted2 = app.prepare_candidates(candidates)
    assert len(candidates) == 2
    assert len(candidates_tobedeleted2) == 0
예제 #3
0
def test_deletion_filtered_name():
    """ Test deletion methods """

    # creating tests objects
    first_ami = AMI()
    first_ami.name = "test-ami-delete-1"
    first_ami.id = 'ami-28c2b348'
    first_ami.creation_date = "2017-11-04T01:35:31.000Z"

    second_ami = AMI()
    second_ami.name = "test-ami-keepme"
    second_ami.id = 'ami-28c2b349'
    second_ami.creation_date = "2017-11-04T01:35:31.000Z"

    third_ami = AMI()
    third_ami.name = "test-ami-delete-2"
    third_ami.id = 'ami-28c2b350'
    third_ami.creation_date = "2017-11-04T01:35:31.000Z"

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

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

    app = App(parser)
    # testing filter
    candidates = app.fetch_candidates(amis_dict)

    candidates_tobedeleted = app.prepare_candidates(candidates)
    assert len(candidates) == 3
    assert candidates_tobedeleted == [first_ami, third_ami]

    parser = parse_args(
        [
            '--keep-previous', '0',
            '--name', 'test-ami-delete-1',
        ]
    )

    app = App(parser)
    candidates_tobedeleted2 = app.prepare_candidates(candidates)
    assert len(candidates) == 3
    assert candidates_tobedeleted2 == [first_ami]
예제 #4
0
def test_deletion_ami_min_days():
    """ Test deletion methods """

    # creating tests objects
    first_ami = AMI()
    first_ami.name = "test-ami"
    first_ami.id = 'ami-28c2b348'
    first_ami.creation_date = "2017-11-04T01:35:31.000Z"

    second_ami = AMI()
    second_ami.name = "test-ami"
    second_ami.id = 'ami-28c2b349'
    second_ami.creation_date = "2017-11-04T01:35:31.000Z"

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

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

    app = App(parser)
    # testing filter
    candidates = app.fetch_candidates(amis_dict)

    candidates_tobedeleted = app.prepare_candidates(candidates)
    assert len(candidates) == 2
    assert len(candidates_tobedeleted) == 2

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

    app = App(parser)
    candidates_tobedeleted2 = app.prepare_candidates(candidates)
    assert len(candidates) == 2
    assert len(candidates_tobedeleted2) == 0
예제 #5
0
def test_deletion_ami_min_days():
    """ Test deletion methods """

    # creating tests objects
    first_ami = AMI()
    first_ami.name = "test-ami"
    first_ami.id = 'ami-28c2b348'
    first_ami.creation_date = "2017-11-04T01:35:31.000Z"

    second_ami = AMI()
    second_ami.name = "test-ami"
    second_ami.id = 'ami-28c2b349'
    second_ami.creation_date = "2017-11-04T01:35:31.000Z"

    # constructing dicts
    amis_dict = {first_ami.id: first_ami, second_ami.id: second_ami}
    parser = parse_args([
        '--keep-previous', '0', '--ami-min-days', '1', '--mapping-key', 'name',
        '--mapping-values', 'test-ami'
    ])

    app = App(parser)
    # testing filter
    candidates = app.fetch_candidates(amis_dict)

    candidates_tobedeleted = app.prepare_candidates(candidates)
    assert len(candidates) == 2
    assert len(candidates_tobedeleted) == 2

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

    app = App(parser)
    candidates_tobedeleted2 = app.prepare_candidates(candidates)
    assert len(candidates) == 2
    assert len(candidates_tobedeleted2) == 0
예제 #6
0
class ImageCleaner:

    def __init__(self, cleaner_params):

        self.cleaner = Cleaner(parse_args([
            '--ami-min-days', cleaner_params["min_days"],
            '--mapping-key', cleaner_params["mapping_key"],
            '--mapping-values', cleaner_params["mapping_values"]
        ]))

    def clean_images(self):

        images = self.cleaner.prepare_candidates()
        if images:
            self.cleaner.prepare_delete_amis(images)
예제 #7
0
def test_deletion():
    """ Test deletion methods """

    base_ami = "ami-1234abcd"

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

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

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

    # delete one AMI by id
    app = App(parser)
    asg = boto3.client('autoscaling')
    f = Fetcher(ec2=ec2, autoscaling=asg)

    assert len(f.fetch_available_amis()) == 5
    assert app.prepare_delete_amis(
        candidates=[images[4]], from_ids=True
    ) is None
    assert len(f.fetch_available_amis()) == 4

    # delete with mapping strategy
    candidates = app.prepare_candidates()
    assert len(candidates) == 4
    assert app.prepare_delete_amis(candidates) is None
    assert len(f.fetch_available_amis()) == 0
예제 #8
0
def test_deletion():
    """ Test deletion methods """

    base_ami = "ami-1234abcd"

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

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

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

    # delete one AMI by id
    app = App(parser)
    asg = boto3.client('autoscaling')
    f = Fetcher(ec2=ec2, autoscaling=asg)

    assert len(f.fetch_available_amis()) == 5
    assert app.prepare_delete_amis(
        candidates=[images[4]], from_ids=True
    ) is None
    assert len(f.fetch_available_amis()) == 4

    # delete with mapping strategy
    candidates = app.prepare_candidates()
    assert len(candidates) == 4
    assert app.prepare_delete_amis(candidates) is None
    assert len(f.fetch_available_amis()) == 0