Skip to content

Commit

Permalink
Fix convert2rhel.ini with defaults values and parsing
Browse files Browse the repository at this point in the history
Parsing the values for inhibitor_overrides now convert it to a boolean
value by default.
  • Loading branch information
r0x0d committed Sep 18, 2024
1 parent b5cb24e commit b4e7e61
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 61 deletions.
15 changes: 8 additions & 7 deletions config/convert2rhel.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
# org = <insert_org>

[host_metering]
# configure_host_metering = "0"
# Possible values here are "auto" or "force"
# configure_host_metering = "auto"

[inhibitor_overrides]
# incomplete_rollback = "0"
# tainted_kernel_module_check_skip = "0"
# outdated_package_check_skip = "0"
# allow_older_version = "0"
# allow_unavailable_kmods = "0"
# skip_kernel_currency_check = "0"
# incomplete_rollback = false
# tainted_kernel_module_check_skip = false
# outdated_package_check_skip = false
# allow_older_version = false
# allow_unavailable_kmods = false
# skip_kernel_currency_check = false
47 changes: 19 additions & 28 deletions convert2rhel/toolopts/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
],
}

BOOLEAN_OPTIONS_HEADERS = ["inhibitor_overrides"]


@six.add_metaclass(abc.ABCMeta)
class BaseConfig:
Expand Down Expand Up @@ -137,23 +139,8 @@ def options_from_config_files(self):
raise FileNotFoundError("No such file or directory: %s" % ", ".join(paths))

Check warning on line 139 in convert2rhel/toolopts/config.py

View check run for this annotation

Codecov / codecov/patch

convert2rhel/toolopts/config.py#L139

Added line #L139 was not covered by tests

found_opts = self._parse_options_from_config(paths)
found_opts = self._normalize_settings_options(found_opts)
return found_opts

def _normalize_settings_options(self, opts):
"""Normalize the inhibitor and host metering options to be boolean instead of integers."""
unparsed_opts = copy.copy(opts)
# Loop through the options from inhibitor_overrides and host_metering to apply the conversion of integer (str)
# to bool.
for option in zip(
CONFIG_FILE_MAPPING_OPTIONS["inhibitor_overrides"], CONFIG_FILE_MAPPING_OPTIONS["host_metering"]
):
# Not all options might be set in the config file.
if option in unparsed_opts:
unparsed_opts[option] = False if unparsed_opts[option] == "0" else True

return unparsed_opts

def _parse_options_from_config(self, paths):
"""Parse the options from the given config files.
Expand Down Expand Up @@ -184,7 +171,6 @@ def _parse_options_from_config(self, paths):
"Couldn't find header '%s' in the configuration file %s." % (supported_header, path)
)
continue

options = self._get_options_value(config_file, supported_header, supported_opts)
found_opts.update(options)

Expand Down Expand Up @@ -214,7 +200,12 @@ def _get_options_value(self, config_file, header, supported_opts):
loggerinst.warning("Unsupported option '%s' in '%s'" % (option, header))
continue

options[option] = config_file.get(header, option)
# This is the only header that can contain boolean values for now.
if header in BOOLEAN_OPTIONS_HEADERS:
options[option] = config_file.getboolean(header, option)
else:
options[option] = config_file.get(header, option)

loggerinst.debug("Found %s in %s" % (option, header))

return options
Expand All @@ -227,24 +218,24 @@ def __init__(self, opts):
super(CliConfig, self).__init__()

self.debug = False # type: bool
self.username = None # type: str
self.password = None # type: str
self.org = None # type: str
self.activation_key = None # type: str
self.config_file = None # type: str
self.username = None # type: str | None
self.password = None # type: str | None
self.org = None # type: str | None
self.activation_key = None # type: str | None
self.config_file = None # type: str | None
self.no_rhsm = False # type: bool
self.enablerepo = [] # type: list[str]
self.disablerepo = [] # type: list[str]
self.pool = None # type: str
self.autoaccept = None # type: bool
self.auto_attach = None # type: str
self.pool = None # type: str | None
self.autoaccept = False # type: bool
self.auto_attach = None # type: str | None
self.restart = False # type: bool
self.arch = None # type: str
self.arch = None # type: str | None
self.no_rpm_va = False # type: bool
self.eus = False # type: bool
self.els = False # type: bool
self.activity = None # type: str
self.serverurl = None # type: str
self.activity = None # type: str | None
self.serverurl = None # type: str | None

self._opts = opts # type: arpgparse.Namepsace

Expand Down
4 changes: 2 additions & 2 deletions convert2rhel/unit_tests/cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def test_no_rhsm_option_work(argv, no_rhsm_value, monkeypatch, global_tool_opts)
org = conf_org
[host_metering]
configure_host_metering = 0
configure_host_metering = "auto"
[inhibitor_overrides]
incomplete_rollback = 0
Expand Down Expand Up @@ -568,7 +568,7 @@ def test_setting_no_rpm_va(argv, expected, message, monkeypatch, caplog, tmpdir)
with open(path, "w") as file:
content = """\
[inhibitor_overrides]
incomplete_rollback = "1"
incomplete_rollback = 1
"""
file.write(content)

Expand Down
73 changes: 49 additions & 24 deletions convert2rhel/unit_tests/toolopts/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,40 +114,67 @@ def test_options_from_config_files_invalid_head_and_options(self, content, expec
),
(
"""\
[inhibitor_override]
incomplete_rollback = 1
[inhibitor_overrides]
incomplete_rollback = false
""",
{"incomplete_rollback": "1"},
{"incomplete_rollback": False},
),
(
"""\
[subscription_manager]
org = correct_org
[inhibitor_override]
incomplete_rollback = 1
[inhibitor_overrides]
incomplete_rollback = false
""",
{"org": "correct_org", "incomplete_rollback": False},
),
(
"""\
[inhibitor_overrides]
incomplete_rollback = false
tainted_kernel_module_check_skip = false
outdated_package_check_skip = false
allow_older_version = false
allow_unavailable_kmods = false
configure_host_metering = false
skip_kernel_currency_check = false
""",
{
"incomplete_rollback": False,
"tainted_kernel_module_check_skip": False,
"outdated_package_check_skip": False,
"allow_older_version": False,
"allow_unavailable_kmods": False,
"configure_host_metering": False,
"skip_kernel_currency_check": False,
},
),
(
"""\
[inhibitor_overrides]
incomplete_rollback = on
""",
{"org": "correct_org", "incomplete_rollback": "1"},
{
"incomplete_rollback": True,
},
),
(
"""\
[inhibitor_override]
[inhibitor_overrides]
incomplete_rollback = 1
tainted_kernel_module_check_skip = 1
outdated_package_check_skip = 1
allow_older_version = 1
allow_unavailable_kmods = 1
configure_host_metering = 1
skip_kernel_currency_check = 1
""",
{
"incomplete_rollback": "1",
"tainted_kernel_module_check_skip": "1",
"outdated_package_check_skip": "1",
"allow_older_version": "1",
"allow_unavailable_kmods": "1",
"configure_host_metering": "1",
"skip_kernel_currency_check": "1",
"incomplete_rollback": True,
},
),
(
"""\
[inhibitor_overrides]
incomplete_rollback = yes
""",
{
"incomplete_rollback": True,
},
),
),
Expand All @@ -164,10 +191,8 @@ def test_options_from_config_files_default(self, content, output, monkeypatch, t
monkeypatch.setattr(FileConfig, "DEFAULT_CONFIG_FILES", value=paths)
file_config = FileConfig(None)
opts = file_config.options_from_config_files()

for key in ["username", "password", "activation_key", "org"]:
if key in opts:
assert opts[key] == output[key]
for key in output.keys():
assert opts[key] == output[key]

@pytest.mark.parametrize(
("content", "output", "content_lower_priority"),
Expand Down

0 comments on commit b4e7e61

Please sign in to comment.