Skip to content

Commit

Permalink
Protect from non-bool
Browse files Browse the repository at this point in the history
  • Loading branch information
tammy-baylis-swi committed Jan 30, 2025
1 parent 52231f7 commit 88f2b7b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
9 changes: 5 additions & 4 deletions solarwinds_apm/apm_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,15 +248,16 @@ def _get_extension_components(
@classmethod
def calculate_is_legacy(cls) -> bool:
"""Checks if agent is running in a legacy environment.
Invalid boolean values are ignored.
Order of precedence: Environment Variable > config file > default False
"""
is_legacy = False
cnf_dict = cls.get_cnf_dict()
if cnf_dict:
is_legacy = cls.convert_to_bool(cnf_dict.get("legacy", is_legacy))
is_legacy = cls.convert_to_bool(
os.environ.get("SW_APM_LEGACY", is_legacy)
)
cnf_legacy = cls.convert_to_bool(cnf_dict.get("legacy"))
is_legacy = cnf_legacy if cnf_legacy is not None else is_legacy
env_legacy = cls.convert_to_bool(os.environ.get("SW_APM_LEGACY"))
is_legacy = env_legacy if env_legacy is not None else is_legacy
return is_legacy

@classmethod
Expand Down
35 changes: 35 additions & 0 deletions tests/unit/test_apm_config/test_apm_config_is_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ def test_calculate_is_legacy_default(self, mocker):
)
assert SolarWindsApmConfig.calculate_is_legacy() is False

def test_calculate_is_legacy_with_env_var_not_boolean(self, mocker):
mocker.patch.dict(os.environ, {"SW_APM_LEGACY": "foo-bar"})
assert SolarWindsApmConfig.calculate_is_legacy() is False

def test_calculate_is_legacy_with_env_var_false(self, mocker):
mocker.patch.dict(os.environ, {"SW_APM_LEGACY": "false"})
assert SolarWindsApmConfig.calculate_is_legacy() is False
Expand All @@ -25,6 +29,13 @@ def test_calculate_is_legacy_with_env_var_true(self, mocker):
mocker.patch.dict(os.environ, {"SW_APM_LEGACY": "true"})
assert SolarWindsApmConfig.calculate_is_legacy() is True

def test_calculate_is_legacy_with_config_not_boolean(self, mocker):
mocker.patch(
"solarwinds_apm.apm_config.SolarWindsApmConfig.get_cnf_dict",
return_value={"legacy": "foo-bar"},
)
assert SolarWindsApmConfig.calculate_is_legacy() is False

def test_calculate_is_legacy_with_config_false(self, mocker):
mocker.patch(
"solarwinds_apm.apm_config.SolarWindsApmConfig.get_cnf_dict",
Expand All @@ -39,6 +50,14 @@ def test_calculate_is_legacy_with_config_true(self, mocker):
)
assert SolarWindsApmConfig.calculate_is_legacy() is True

def test_calculate_is_legacy_with_config_not_bool_env_var_not_bool(self, mocker):
mocker.patch.dict(os.environ, {"SW_APM_LEGACY": "foo-bar"})
mocker.patch(
"solarwinds_apm.apm_config.SolarWindsApmConfig.get_cnf_dict",
return_value={"legacy": "foo-bar"},
)
assert SolarWindsApmConfig.calculate_is_legacy() is False

def test_calculate_is_legacy_with_config_false_env_var_false(self, mocker):
mocker.patch.dict(os.environ, {"SW_APM_LEGACY": "false"})
mocker.patch(
Expand All @@ -55,6 +74,14 @@ def test_calculate_is_legacy_with_config_false_env_var_true(self, mocker):
)
assert SolarWindsApmConfig.calculate_is_legacy() is True

def test_calculate_is_legacy_with_config_not_bool_env_var_true(self, mocker):
mocker.patch.dict(os.environ, {"SW_APM_LEGACY": "true"})
mocker.patch(
"solarwinds_apm.apm_config.SolarWindsApmConfig.get_cnf_dict",
return_value={"legacy": "foo-bar"},
)
assert SolarWindsApmConfig.calculate_is_legacy() is True

def test_calculate_is_legacy_with_config_true_env_var_false(self, mocker):
mocker.patch.dict(os.environ, {"SW_APM_LEGACY": "false"})
mocker.patch(
Expand All @@ -63,6 +90,14 @@ def test_calculate_is_legacy_with_config_true_env_var_false(self, mocker):
)
assert SolarWindsApmConfig.calculate_is_legacy() is False

def test_calculate_is_legacy_with_config_true_env_var_not_bool(self, mocker):
mocker.patch.dict(os.environ, {"SW_APM_LEGACY": "foo-bar"})
mocker.patch(
"solarwinds_apm.apm_config.SolarWindsApmConfig.get_cnf_dict",
return_value={"legacy": "true"},
)
assert SolarWindsApmConfig.calculate_is_legacy() is True

def test_calculate_is_legacy_with_config_true_env_var_true(self, mocker):
mocker.patch.dict(os.environ, {"SW_APM_LEGACY": "true"})
mocker.patch(
Expand Down

0 comments on commit 88f2b7b

Please sign in to comment.