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
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
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]
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
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-9f9f6a2b' second_instance.name = "second-instance" second_instance.image_id = first_ami.id second_instance.launch_time = datetime.now() # constructing dicts amis_dict = {first_ami.id: first_ami, second_ami.id: second_ami} instances_dict = {} instances_dict[first_instance.image_id] = instances_dict instances_dict[second_instance.image_id] = second_instance # testing filter unused_ami_dict = App(parse_args([])).fetch_candidates( amis_dict, list(instances_dict)) assert len(unused_ami_dict) == 1 assert amis_dict.get('unused-ami') is not None
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
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]
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
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
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
def test_map_with_tags(): # 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() # prod and web-server third_ami = AMI() third_ami.id = 'ami-28c2b350' third_ami.name = "debian-20160104" third_ami.tags.append(stack_tag) third_ami.tags.append(env_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"]} grouped_amis = AMICleaner().map_candidates(candidates, grouping_strategy) assert grouped_amis is not None assert len(grouped_amis.get("prod")) == 1 assert len(grouped_amis.get("prod.web-server")) == 2
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
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
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
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 = cleaner.reduce_candidates(candidates, rotation_number) assert len(left) == 1 assert left[0].id == first_ami.id # keep 1 recent ami rotation_number = 1 left = cleaner.reduce_candidates(candidates, rotation_number) assert len(left) == 2 assert left[0].id == second_ami.id # keep 5 recent amis rotation_number = 5 left = cleaner.reduce_candidates(candidates, rotation_number) assert len(left) == 0
def test_fetch_candidates_exclude_amis(): # 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 = 'excluded-ami' second_ami.creation_date = datetime.now() second_instance = AWSEC2Instance() second_instance.id = 'i-9f9f6a2b' 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 = App(parse_args(["--exclude-ami", "excluded-ami"])).fetch_candidates( amis_dict, list(instances_dict) ) assert len(unused_ami_dict) == 0