Skip to content

Commit

Permalink
fix: InsightsClient: fix handling of bool+string config keys
Browse files Browse the repository at this point in the history
Apparently there are configuration keys that may allow both boolean and
string values, and "cert_verify" is a notable one.

To handle this properly, add a new category of keys, and tweak the
reading of keys to fallback with a string-based get() in case the
(likely from getboolean()) reading fails with ValueError.

Add a couple of tests to verify that the right types are properly
handled.

Signed-off-by: Pino Toscano <[email protected]>
  • Loading branch information
ptoscano committed Aug 14, 2024
1 parent 8c97a0c commit 2750977
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
17 changes: 14 additions & 3 deletions pytest_client_tools/insights_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ class InsightsClientConfig:
"upload_url",
"username",
}
# bool & string config keys
_KEYS_BOOL_STRING = {
"cert_verify",
}

def __init__(self, path="/etc/insights-client/insights-client.conf"):
self._path = path
Expand Down Expand Up @@ -166,7 +170,7 @@ def save(self):
self._config.write(f, space_around_delimiters=False)

def __getattr__(self, name):
if name in self._KEYS_BOOL:
if name in self._KEYS_BOOL | self._KEYS_BOOL_STRING:
read_func = self._config.getboolean
elif name in self._KEYS_INT:
read_func = self._config.getint
Expand All @@ -177,13 +181,20 @@ def __getattr__(self, name):
else:
raise KeyError(name)
try:
return read_func("insights-client", name)
try:
return read_func("insights-client", name)
except ValueError:
return self._config.get("insights-client", name)
except configparser.NoOptionError:
raise KeyError(name)

def __setattr__(self, name, value):
if name in (
self._KEYS_BOOL | self._KEYS_INT | self._KEYS_FLOAT | self._KEYS_STRING
self._KEYS_BOOL
| self._KEYS_INT
| self._KEYS_FLOAT
| self._KEYS_STRING
| self._KEYS_BOOL_STRING
):
self._config.set("insights-client", name, str(value))
return
Expand Down
37 changes: 37 additions & 0 deletions tests/test_insights_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,40 @@ def test_config_reload(tmp_path):
)
conf.reload()
assert not conf.auto_config


@pytest.mark.parametrize(
"key_value",
[
True,
"/some/path",
],
)
def test_config_cert_verify_read(key_value, tmp_path):
conf_file = tmp_path / "file.conf"
conf_file.write_text(
f"""[insights-client]
cert_verify={key_value}
"""
)
conf = InsightsClientConfig(conf_file)
assert isinstance(conf.cert_verify, type(key_value))
assert conf.cert_verify == key_value


@pytest.mark.parametrize(
"key_value",
[
True,
"/some/path",
],
)
def test_config_cert_verify_write(key_value, tmp_path):
conf_file = tmp_path / "file.conf"
conf_file.touch()
conf = InsightsClientConfig(conf_file)
conf.cert_verify = key_value
assert isinstance(conf.cert_verify, type(key_value))
conf.save()
conf_file_text = conf_file.read_text()
assert f"cert_verify={key_value}" in conf_file_text

0 comments on commit 2750977

Please sign in to comment.