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
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)
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
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