From dc10b8f356b3c4cbf8f744631c08440c03018371 Mon Sep 17 00:00:00 2001 From: Jesse S Date: Fri, 6 Oct 2023 12:00:22 -0700 Subject: [PATCH] fix: TOOLS-2541 rm configs not allowed in SC mode (#211) --- lib/utils/conf_gen.py | 33 ++++++++++++++++++++++ test/unit/utils/test_conf_gen.py | 48 +++++++++++++++++++++++++++++++- 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/lib/utils/conf_gen.py b/lib/utils/conf_gen.py index 8b5fe9e6..b7cc46b4 100644 --- a/lib/utils/conf_gen.py +++ b/lib/utils/conf_gen.py @@ -881,6 +881,38 @@ async def __call__(self, context_dict: dict[str, Any]): self._remove_default_values(build, host_config) +class RemoveConfigsConditionally(ConfigPipelineStep): + """Some values return "null" but that is not a valid config value. You would think + that the step that removes non-defaults would handle this case but it does not. + e.g.: xdr.dc.namespace.remote-namespace + """ + + async def __call__(self, context_dict: dict[str, Any]): + for host in context_dict[INTERMEDIATE]: + for top_level_config in context_dict[INTERMEDIATE][host]: + if ( + isinstance(top_level_config, InterNamedSectionKey) + and top_level_config.type == "namespace" + ): + # Determine if we are in SC mode. + ns_config = context_dict["namespaces"][host][top_level_config.name] + + if ( + "strong-consistency" in ns_config + and ns_config["strong-consistency"] == "true" + ): + for config in list( + context_dict[INTERMEDIATE][host][top_level_config].keys() + ): + if config in { + "read-consistency-level-override", + "write-commit-level-override", + }: + del context_dict[INTERMEDIATE][host][top_level_config][ + config + ] + + class ConfigGenerator: async def generate(self) -> str: raise NotImplementedError("ConfigGenerator.generate not implemented") @@ -919,6 +951,7 @@ async def _generate_intermediate( RemoveInvalidKeysFoundInSchemas(), ConvertCommaSeparatedToList(), RemoveEmptyContexts(), # Should be after RemoveDefaultValues + RemoveConfigsConditionally(), ], ) diff --git a/test/unit/utils/test_conf_gen.py b/test/unit/utils/test_conf_gen.py index 4ca52932..0f41bff1 100644 --- a/test/unit/utils/test_conf_gen.py +++ b/test/unit/utils/test_conf_gen.py @@ -14,6 +14,7 @@ InterLoggingContextKey, InterNamedSectionKey, InterUnnamedSectionKey, + RemoveConfigsConditionally, RemoveDefaultAndNonExistentKeys, RemoveEmptyContexts, RemoveNullOrEmptyOrUndefinedValues, @@ -550,7 +551,11 @@ class RemoveNullValuesTest(asynctest.TestCase): maxDiff = None async def test_remove_null_values(self): - context_dict = {"intermediate": {"1.1.1.1": {"a": "null", "b": "1", "c": ""}}} + context_dict = { + "intermediate": { + "1.1.1.1": {"a": "null", "b": "1", "c": "", "d": "undefined"} + } + } await RemoveNullOrEmptyOrUndefinedValues()(context_dict) @@ -692,3 +697,44 @@ def __init__(self, schema_dir: str, version: str, strict: bool): } }, ) + + +class RemoveConfigsConditionallyTest(asynctest.TestCase): + maxDiff = None + + async def test_remove_null_values(self): + context_dict = { + "intermediate": { + "1.1.1.1": { + InterNamedSectionKey("namespace", "test"): { + "read-consistency-level-override": "all", + "write-commit-level-override": "all", + }, + InterNamedSectionKey("namespace", "bar"): { + "read-consistency-level-override": "all", + "write-commit-level-override": "all", + }, + } + }, + "namespaces": { + "1.1.1.1": { + "test": {"strong-consistency": "true"}, + "bar": {"strong-consistency": "false"}, + } + }, + } + + await RemoveConfigsConditionally()(context_dict) + + self.assertDictEqual( + context_dict["intermediate"], + { + "1.1.1.1": { + InterNamedSectionKey("namespace", "test"): {}, + InterNamedSectionKey("namespace", "bar"): { + "read-consistency-level-override": "all", + "write-commit-level-override": "all", + }, + } + }, + )