From 741e9f6cbf138428313f107573b16af4f9e212ed Mon Sep 17 00:00:00 2001 From: pritchyspritch Date: Thu, 16 May 2024 16:40:55 +0100 Subject: [PATCH] Tech asset tests --- build_tech_assets.py | 25 +++- tests/test_build_tech_assets.py | 140 +++++++++++++++++++ yaml-templates/technical_asset_template.yaml | 2 +- 3 files changed, 160 insertions(+), 7 deletions(-) create mode 100644 tests/test_build_tech_assets.py diff --git a/build_tech_assets.py b/build_tech_assets.py index 160fec5..5dd92cf 100644 --- a/build_tech_assets.py +++ b/build_tech_assets.py @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/tests/test_build_tech_assets.py b/tests/test_build_tech_assets.py new file mode 100644 index 0000000..40fc730 --- /dev/null +++ b/tests/test_build_tech_assets.py @@ -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() diff --git a/yaml-templates/technical_asset_template.yaml b/yaml-templates/technical_asset_template.yaml index 99a1916..ec41ab9 100644 --- a/yaml-templates/technical_asset_template.yaml +++ b/yaml-templates/technical_asset_template.yaml @@ -1,6 +1,6 @@ {{name}}: id: {{name}} - description: {{description}} + description: {{type}}: {{description}} type: external-entity usage: business used_as_client_by_human: false