From 53f68bd72d488216ee0892c15fdbf5ef8aa05ddb Mon Sep 17 00:00:00 2001 From: sanderegg <35365065+sanderegg@users.noreply.github.com> Date: Thu, 7 Dec 2023 18:21:08 +0100 Subject: [PATCH] ensure dict are correctly setup --- .../utils/clusters.py | 2 +- .../tests/unit/test_utils_clusters.py | 31 +++++++++++++++++-- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/services/clusters-keeper/src/simcore_service_clusters_keeper/utils/clusters.py b/services/clusters-keeper/src/simcore_service_clusters_keeper/utils/clusters.py index 85e5361bf97..341d7f9d749 100644 --- a/services/clusters-keeper/src/simcore_service_clusters_keeper/utils/clusters.py +++ b/services/clusters-keeper/src/simcore_service_clusters_keeper/utils/clusters.py @@ -60,7 +60,7 @@ def _convert_to_env_dict(entries: dict[str, Any]) -> str: f"WORKERS_EC2_INSTANCES_SECURITY_GROUP_IDS={_convert_to_env_list(app_settings.CLUSTERS_KEEPER_WORKERS_EC2_INSTANCES.WORKERS_EC2_INSTANCES_SECURITY_GROUP_IDS)}", f"WORKERS_EC2_INSTANCES_SUBNET_ID={app_settings.CLUSTERS_KEEPER_WORKERS_EC2_INSTANCES.WORKERS_EC2_INSTANCES_SUBNET_ID}", f"WORKERS_EC2_INSTANCES_TIME_BEFORE_TERMINATION={app_settings.CLUSTERS_KEEPER_WORKERS_EC2_INSTANCES.WORKERS_EC2_INSTANCES_TIME_BEFORE_TERMINATION}", - f"WORKERS_EC2_INSTANCES_CUSTOM_TAGS={app_settings.CLUSTERS_KEEPER_WORKERS_EC2_INSTANCES.WORKERS_EC2_INSTANCES_CUSTOM_TAGS | additional_custom_tags}", + f"WORKERS_EC2_INSTANCES_CUSTOM_TAGS={_convert_to_env_dict(app_settings.CLUSTERS_KEEPER_WORKERS_EC2_INSTANCES.WORKERS_EC2_INSTANCES_CUSTOM_TAGS | additional_custom_tags)}", f"LOG_LEVEL={app_settings.LOG_LEVEL}", ] diff --git a/services/clusters-keeper/tests/unit/test_utils_clusters.py b/services/clusters-keeper/tests/unit/test_utils_clusters.py index 9c56d43e34a..cbb188a0c16 100644 --- a/services/clusters-keeper/tests/unit/test_utils_clusters.py +++ b/services/clusters-keeper/tests/unit/test_utils_clusters.py @@ -7,7 +7,12 @@ from typing import Any import pytest -from aws_library.ec2.models import EC2InstanceBootSpecific, EC2InstanceData +from aws_library.ec2.models import ( + AWSTagKey, + AWSTagValue, + EC2InstanceBootSpecific, + EC2InstanceData, +) from faker import Faker from models_library.api_schemas_clusters_keeper.clusters import ClusterState from pytest_simcore.helpers.utils_envs import EnvVarsDict @@ -45,8 +50,14 @@ def test_create_startup_script( clusters_keeper_docker_compose: dict[str, Any], ec2_boot_specs: EC2InstanceBootSpecific, ): + additional_custom_tags = { + AWSTagKey("pytest-tag-key"): AWSTagValue("pytest-tag-value") + } startup_script = create_startup_script( - app_settings, cluster_machines_name_prefix, ec2_boot_specs + app_settings, + cluster_machines_name_prefix=cluster_machines_name_prefix, + ec2_boot_specific=ec2_boot_specs, + additional_custom_tags=additional_custom_tags, ) assert isinstance(startup_script, str) assert len(ec2_boot_specs.custom_boot_scripts) > 0 @@ -104,6 +115,22 @@ def test_create_startup_script( re.search(rf"{i}=\[(\\\".+\\\")*\]", startup_script) for i in list_settings ) + # check dicts have \' in front + dict_settings = [ + "WORKERS_EC2_INSTANCES_ALLOWED_TYPES", + "WORKERS_EC2_INSTANCES_CUSTOM_TAGS", + ] + assert all( + re.search(rf"{i}=\'{{(\".+\":\s\".*\")+}}\'", startup_script) + for i in dict_settings + ) + + # check the additional tags are in + assert all( + f'"{key}": "{value}"' in startup_script + for key, value in additional_custom_tags.items() + ) + @pytest.mark.parametrize( "ec2_state, expected_cluster_state",