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.
This commit is contained in:
Brian C. Lane 2019-06-21 11:21:25 -07:00 committed by Lars Karlitski
parent 61059a2699
commit 13a7dcf2b1

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,7 +369,9 @@ 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.extend(check_list_case(["append"], c["kernel"].keys(), prefix="kernel "))
if "append" not in c.get("kernel", []):
errors.append("'customizations.kernel': missing append field.") errors.append("'customizations.kernel': missing append field.")
if "sshkey" in c: if "sshkey" in c:
@ -364,19 +389,29 @@ 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.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.") errors.append("'customizations.timezone': missing timezone or ntpservers fields.")
if "locale" in c and not c.get("locale"): if "locale" in c:
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.") errors.append("'customizations.locale': missing languages or keyboard fields.")
if "firewall" in c and not c.get("firewall"): if "firewall" in c:
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.") 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.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.") errors.append("'customizations.firewall.services': missing enabled or disabled fields.")
if "services" in c and not c.get("services"): if "services" in c:
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.") errors.append("'customizations.services': missing enabled or disabled fields.")
return errors return errors