Exemplo n.º 1
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
Exemplo n.º 2
0
def test_tags_values_to_string_without_filters():
    first_tag = AWSTag()
    first_tag.key = "Key1"
    first_tag.value = "Value1"

    second_tag = AWSTag()
    second_tag.key = "Key2"
    second_tag.value = "Value2"

    third_tag = AWSTag()
    third_tag.key = "Key3"
    third_tag.value = "Value3"

    tags = [first_tag, third_tag, second_tag]
    filters = []

    tags_values_string = AMICleaner.tags_values_to_string(tags, filters)
    assert tags_values_string is not None
    assert tags_values_string == "Value1.Value2.Value3"
Exemplo n.º 3
0
def test_tags_values_to_string_without_filters():
    first_tag = AWSTag()
    first_tag.key = "Key1"
    first_tag.value = "Value1"

    second_tag = AWSTag()
    second_tag.key = "Key2"
    second_tag.value = "Value2"

    third_tag = AWSTag()
    third_tag.key = "Key3"
    third_tag.value = "Value3"

    tags = [first_tag, third_tag, second_tag]
    filters = []

    tags_values_string = AMICleaner.tags_values_to_string(tags, filters)
    assert tags_values_string is not None
    assert tags_values_string == "Value1.Value2.Value3"
Exemplo n.º 4
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
Exemplo n.º 5
0
def test_tags_values_to_string():
    first_tag = AWSTag()
    first_tag.key = "Key1"
    first_tag.value = "Value1"

    second_tag = AWSTag()
    second_tag.key = "Key2"
    second_tag.value = "Value2"

    third_tag = AWSTag()
    third_tag.key = "Key3"
    third_tag.value = "Value3"

    fourth_tag = AWSTag()
    fourth_tag.key = "Key4"
    fourth_tag.value = "Value4"

    tags = [first_tag, third_tag, second_tag, fourth_tag]
    filters = ["Key2", "Key3"]

    tags_values_string = AMICleaner.tags_values_to_string(tags, filters)
    assert tags_values_string is not None
    assert tags_values_string == "Value2.Value3"
Exemplo n.º 6
0
def test_tags_values_to_string():
    first_tag = AWSTag()
    first_tag.key = "Key1"
    first_tag.value = "Value1"

    second_tag = AWSTag()
    second_tag.key = "Key2"
    second_tag.value = "Value2"

    third_tag = AWSTag()
    third_tag.key = "Key3"
    third_tag.value = "Value3"

    fourth_tag = AWSTag()
    fourth_tag.key = "Key4"
    fourth_tag.value = "Value4"

    tags = [first_tag, third_tag, second_tag, fourth_tag]
    filters = ["Key2", "Key3"]

    tags_values_string = AMICleaner.tags_values_to_string(tags, filters)
    assert tags_values_string is not None
    assert tags_values_string == "Value2.Value3"