Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added back validation of modules in "build" if present #175

Merged
merged 3 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cfbs/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ def init_command(index=None, masterfiles=None, non_interactive=False) -> int:
@cfbs_command("status")
def status_command() -> int:
config = CFBSConfig.get_instance()
if validate_config(config, require_build_actions=False) != 0:
if validate_config(config, empty_build_list_ok=True) != 0:
return 1
config.warn_about_unknown_keys()
print("Name: %s" % config["name"])
Expand Down
21 changes: 11 additions & 10 deletions cfbs/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ def _validate_top_level_keys(config):
)


def _validate_config(config, require_build_actions=True):
def _validate_config(config, empty_build_list_ok=False):
# First validate the config i.e. the user's cfbs.json
config.warn_about_unknown_keys()
_validate_top_level_keys(config)
raw_data = config.raw_data

if config["type"] == "policy-set" or "build" in config:
_validate_config_for_build_field(config, require_build_actions)
_validate_config_for_build_field(config, empty_build_list_ok)

if "index" in raw_data and type(raw_data["index"]) in (dict, OrderedDict):
for name, module in raw_data["index"].items():
Expand All @@ -119,9 +119,9 @@ def _validate_config(config, require_build_actions=True):
_validate_module_object("provides", name, module, config)


def validate_config(config, require_build_actions=True):
def validate_config(config, empty_build_list_ok=False):
try:
_validate_config(config, require_build_actions)
_validate_config(config, empty_build_list_ok)
return 0
except CFBSValidationError as e:
print(e)
Expand Down Expand Up @@ -485,7 +485,7 @@ def validate_module_input(name, module):
validate_module_input(name, module)


def _validate_config_for_build_field(config, require_build_actions=True):
def _validate_config_for_build_field(config, empty_build_list_ok=False):
"""Validate that neccessary fields are in the config for the build/download commands to work"""
if not "build" in config:
user_error(
Expand All @@ -496,14 +496,15 @@ def _validate_config_for_build_field(config, require_build_actions=True):
user_error(
'The "build" field in ./cfbs.json must be a list (of modules involved in the build)'
)
if require_build_actions:
if config["build"] == []:
user_error(
"The \"build\" field in ./cfbs.json is empty - add modules with 'cfbs add'"
)
if len(config["build"]) > 0:
# If there are modules in "build" validate them:
for index, module in enumerate(config["build"]):
name = module["name"] if "name" in module else index
_validate_module_object("build", name, module, config)
elif not empty_build_list_ok:
user_error(
"The \"build\" field in ./cfbs.json is empty - add modules with 'cfbs add'"
)


def main():
Expand Down
6 changes: 3 additions & 3 deletions tests/shell/035_cfbs_build_compatibility_1.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ cfbs build
# Look for some proof that the build actually did something:
grep 'bundle common inventory' out/masterfiles/promises.cf

# These other commands should also work:
cfbs status

# NOTE: We expect cfbs build to work, but not cfbs validate since
# this older module entry has an empty string for "subdirectory".
!( cfbs validate )

# Same for cfbs status since it runs validate:
!( cfbs status )

# Once more, but let's do download and build as separate steps:
rm -rf out/
rm -rf ~/.cfengine/cfbs
Expand Down
6 changes: 3 additions & 3 deletions tests/shell/036_cfbs_build_compatibility_2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ grep 'inventory_systemd_service_units_running' out/masterfiles/def.json
grep 'bundle common inventory' out/masterfiles/promises.cf
grep '$(paths.systemctl) list-units --type=service --state=running' out/masterfiles/services/inventory-systemd/main.cf

# These other commands should also work:
cfbs status

# NOTE: We expect cfbs build to work, but not cfbs validate since
# this older module entry has an empty string for "subdirectory".
!( cfbs validate )

# Same for cfbs status since it runs validate:
!( cfbs status )

# Once more, but let's do download and build as separate steps:
rm -rf out/
rm -rf ~/.cfengine/cfbs
Expand Down
Loading