Skip to content

Commit

Permalink
Tech asset tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pritchyspritch committed May 16, 2024
1 parent 93eb87e commit 741e9f6
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 7 deletions.
25 changes: 19 additions & 6 deletions build_tech_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ def build_container_app_tm(name: str, asset_type: str) -> tuple:
"size": "application",
"technology": "web-application",
"machine": "container",
"tags": [name, "azure", "azure-container-app"],
"tags": [name, "azure", "azure-container-app", asset_type],
}
template_file = open("yaml-templates/technical_asset_template.yaml")
template_str = template_file.read()
template_file.close()
tech_asset_template = Template(template_str)
container_app_asset_yaml = tech_asset_template.render(container_app_dict)

Expand All @@ -29,10 +30,19 @@ def build_key_vault_tm(name: str, asset_type: str) -> tuple:
"size": "service",
"technology": "vault",
"machine": "virtual",
"tags": [name, "azure", "azure-key-vault", "vault", "secrets", "keys"],
"tags": [
name,
"azure",
"azure-key-vault",
"vault",
"secrets",
"keys",
asset_type,
],
}
template_file = open("yaml-templates/technical_asset_template.yaml")
template_str = template_file.read()
template_file.close()
tech_asset_template = Template(template_str)
key_vault_asset_yaml = tech_asset_template.render(key_vault_dict)

Expand All @@ -49,10 +59,11 @@ def build_cache_tm(name: str, asset_type: str) -> tuple:
"size": "service",
"technology": "database",
"machine": "virtual",
"tags": [name, "azure", "azure-redis-cache", "cache"],
"tags": [name, "azure", "azure-redis-cache", "cache", asset_type],
}
template_file = open("yaml-templates/technical_asset_template.yaml")
template_str = template_file.read()
template_file.close()
tech_asset_template = Template(template_str)
redis_cache_asset_yaml = tech_asset_template.render(redis_cache_dict)

Expand Down Expand Up @@ -110,15 +121,16 @@ def build_app_service_tm(name: str, asset_type: str, kind: str) -> tuple:

app_service_dict = {
"name": name,
"type": "App Service",
"type": asset_type.split("/")[0],
"description": f"An app service plan, used to deploy a {kind_ref}",
"size": "service",
"technology": technology,
"machine": machine,
"tags": [name, "azure", "azure-app-service", machine, technology],
"tags": [name, "azure", "azure-app-service", machine, technology, asset_type],
}
template_file = open("yaml-templates/technical_asset_template.yaml")
template_str = template_file.read()
template_file.close()
tech_asset_template = Template(template_str)
app_service_asset_yaml = tech_asset_template.render(app_service_dict)

Expand All @@ -135,10 +147,11 @@ def build_storage_tm(name: str, asset_type: str) -> tuple:
"size": "service",
"technology": "block-storage",
"machine": "virtual",
"tags": [name, "azure", "azure-storage", "blob"],
"tags": [name, "azure", "azure-storage", "blob", asset_type],
}
template_file = open("yaml-templates/technical_asset_template.yaml")
template_str = template_file.read()
template_file.close()
tech_asset_template = Template(template_str)
storage_asset_yaml = tech_asset_template.render(storage_dict)

Expand Down
140 changes: 140 additions & 0 deletions tests/test_build_tech_assets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import unittest

from build_tech_assets import (
build_container_app_tm,
build_key_vault_tm,
build_cache_tm,
build_app_service_tm,
build_storage_tm,
)


def lists_are_equal(expected_tag_list: list, tag_list: list) -> bool:
return expected_tag_list == tag_list


def yaml_contains_correct_values(yaml: str, name: str, asset_type: str) -> bool:

if f"id: {name}" in yaml and f"description: {asset_type.split('/')[0]}" in yaml:
return True
else:
return False


def app_service_yaml_contains_correct_values(
yaml: str, name: str, asset_type: str, machine: str, technology: str
) -> bool:

if f"id: {name}" in yaml and f"description: {asset_type.split('/')[0]}" in yaml:
print("ok")
else:
return False

if f"machine: {machine}" in yaml and f"technology: {technology}" in yaml:
return True
else:
return False


class TestDataAssets(unittest.TestCase):

def test_build_container_app_tm(self):
name = "test_container_app_name"
asset_type = "test_container_app_asset_type/test"
container_app_asset_yaml, tag_list = build_container_app_tm(name, asset_type)

expected_tag_list = [
"test_container_app_name",
"azure",
"azure-container-app",
"test_container_app_asset_type/test",
]

self.assertTrue(lists_are_equal(expected_tag_list, tag_list))
self.assertTrue(
yaml_contains_correct_values(container_app_asset_yaml, name, asset_type)
)

def test_build_key_vault_tm(self):
name = "test_key_vault_name"
asset_type = "test_key_vault_asset_type/test"
key_vault_asset_yaml, tag_list = build_key_vault_tm(name, asset_type)

expected_tag_list = [
"test_key_vault_name",
"azure",
"azure-key-vault",
"vault",
"secrets",
"keys",
"test_key_vault_asset_type/test",
]

self.assertTrue(lists_are_equal(expected_tag_list, tag_list))
self.assertTrue(
yaml_contains_correct_values(key_vault_asset_yaml, name, asset_type)
)

def test_build_cache_tm(self):
name = "test_build_cache_name"
asset_type = "test_build_cache_asset_type/test"
build_cache_asset_yaml, tag_list = build_cache_tm(name, asset_type)

expected_tag_list = [
"test_build_cache_name",
"azure",
"azure-redis-cache",
"cache",
"test_build_cache_asset_type/test",
]

self.assertTrue(lists_are_equal(expected_tag_list, tag_list))
self.assertTrue(
yaml_contains_correct_values(build_cache_asset_yaml, name, asset_type)
)

def test_build_storage_tm(self):
name = "test_build_storage_name"
asset_type = "test_build_storage_asset_type/test"
build_storage_asset_yaml, tag_list = build_storage_tm(name, asset_type)

expected_tag_list = [
"test_build_storage_name",
"azure",
"azure-storage",
"blob",
"test_build_storage_asset_type/test",
]

self.assertTrue(lists_are_equal(expected_tag_list, tag_list))
self.assertTrue(
yaml_contains_correct_values(build_storage_asset_yaml, name, asset_type)
)

def test_build_app_service_tm(self):
name = "test_build_storage_name"
asset_type = "test_build_storage_asset_type/test"
kind = "functionapp,linux"
machine = "serverless"
technology = "function"
app_service_asset_yaml, tag_list = build_app_service_tm(name, asset_type, kind)

expected_tag_list = [
"test_build_storage_name",
"azure",
"azure-app-service",
"serverless",
"function",
"test_build_storage_asset_type/test",
]

self.assertTrue(lists_are_equal(expected_tag_list, tag_list))
self.assertTrue(
app_service_yaml_contains_correct_values(
app_service_asset_yaml, name, asset_type, machine, technology
)
)


if __name__ == "__main__":
unittest.main()
2 changes: 1 addition & 1 deletion yaml-templates/technical_asset_template.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{name}}:
id: {{name}}
description: {{description}}
description: {{type}}: {{description}}
type: external-entity
usage: business
used_as_client_by_human: false
Expand Down

0 comments on commit 741e9f6

Please sign in to comment.