lorax-composer: Add basic case check to check_recipe_dict

This will detect if an expected field is not all lower case as is
required.

(cherry picked from commit 13a7dcf2b1)

Related: rhbz#1716596
This commit is contained in:
Brian C. Lane 2019-06-21 11:21:25 -07:00
parent 229253b0cb
commit 7270511379

View File

@ -282,11 +282,30 @@ def check_required_list(lst, fields):
errors = [] errors = []
for i, m in enumerate(lst): for i, m in enumerate(lst):
m_errs = [] m_errs = []
errors.extend(check_list_case(fields, m.keys(), prefix="%d " % (i+1)))
for f in fields: for f in fields:
if f not in m: if f not in m:
m_errs.append("'%s'" % f) m_errs.append("'%s'" % f)
if m_errs: if m_errs:
errors.append("%d is missing %s" % (i, ",".join(m_errs))) errors.append("%d is missing %s" % (i+1, ", ".join(m_errs)))
return errors
def check_list_case(expected_keys, recipe_keys, prefix=""):
"""Check the case of the recipe keys
:param expected_keys: A list of expected key strings
:type expected_keys: list of str
:param recipe_keys: A list of the recipe's key strings
:type recipe_keys: list of str
:returns: list of errors
:rtype: list of str
"""
errors = []
for k in recipe_keys:
if k in expected_keys:
continue
if k.lower() in expected_keys:
errors.append(prefix + "%s should be %s" % (k, k.lower()))
return errors return errors
def check_recipe_dict(recipe_dict): def check_recipe_dict(recipe_dict):
@ -307,6 +326,10 @@ def check_recipe_dict(recipe_dict):
""" """
errors = [] errors = []
# Check for wrong case of top level keys
top_keys = ["name", "description", "version", "modules", "packages", "groups", "repos", "customizations"]
errors.extend(check_list_case(recipe_dict.keys(), top_keys))
if "name" not in recipe_dict: if "name" not in recipe_dict:
errors.append("Missing 'name'") errors.append("Missing 'name'")
if "description" not in recipe_dict: if "description" not in recipe_dict:
@ -346,8 +369,10 @@ def check_recipe_dict(recipe_dict):
return errors return errors
# Make sure to catch empty sections by testing for keywords, not just looking at .get() result. # Make sure to catch empty sections by testing for keywords, not just looking at .get() result.
if "kernel" in c and "append" not in c.get("kernel", []): if "kernel" in c:
errors.append("'customizations.kernel': missing append field.") errors.extend(check_list_case(["append"], c["kernel"].keys(), prefix="kernel "))
if "append" not in c.get("kernel", []):
errors.append("'customizations.kernel': missing append field.")
if "sshkey" in c: if "sshkey" in c:
sshkey_errors = check_required_list(c.get("sshkey"), ["user", "key"]) sshkey_errors = check_required_list(c.get("sshkey"), ["user", "key"])
@ -364,20 +389,30 @@ def check_recipe_dict(recipe_dict):
if group_errors: if group_errors:
errors.append("'customizations.group' errors:\n%s" % "\n".join(group_errors)) errors.append("'customizations.group' errors:\n%s" % "\n".join(group_errors))
if "timezone" in c and not c.get("timezone"): if "timezone" in c:
errors.append("'customizations.timezone': missing timezone or ntpservers fields.") errors.extend(check_list_case(["timezone", "ntpservers"], c["timezone"].keys(), prefix="timezone "))
if not c.get("timezone"):
errors.append("'customizations.timezone': missing timezone or ntpservers fields.")
if "locale" in c and not c.get("locale"): if "locale" in c:
errors.append("'customizations.locale': missing languages or keyboard fields.") errors.extend(check_list_case(["languages", "keyboard"], c["locale"].keys(), prefix="locale "))
if not c.get("locale"):
errors.append("'customizations.locale': missing languages or keyboard fields.")
if "firewall" in c and not c.get("firewall"): if "firewall" in c:
errors.append("'customizations.firewall': missing ports field or services section.") errors.extend(check_list_case(["ports"], c["firewall"].keys(), prefix="firewall "))
if not c.get("firewall"):
errors.append("'customizations.firewall': missing ports field or services section.")
if "firewall" in c and "services" in c.get("firewall", []) and not c.get("firewall").get("services"): if "services" in c.get("firewall", []):
errors.append("'customizations.firewall.services': missing enabled or disabled fields.") errors.extend(check_list_case(["enabled", "disabled"], c["firewall"]["services"].keys(), prefix="firewall.services "))
if not c.get("firewall").get("services"):
errors.append("'customizations.firewall.services': missing enabled or disabled fields.")
if "services" in c and not c.get("services"): if "services" in c:
errors.append("'customizations.services': missing enabled or disabled fields.") errors.extend(check_list_case(["enabled", "disabled"], c["services"].keys(), prefix="services "))
if not c.get("services"):
errors.append("'customizations.services': missing enabled or disabled fields.")
return errors return errors