Skip to content

Commit

Permalink
improve error output when validating options
Browse files Browse the repository at this point in the history
  • Loading branch information
LegenJCdary committed Sep 15, 2022
1 parent 791da8a commit a536dad
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 77 deletions.
17 changes: 15 additions & 2 deletions source/modules/validators/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,14 @@ def validate_options(self, options: dict):
failed = False
for req in required:
if not options[req]:
self.logger.error("%s is required for %s", req, options["subcommand"])
self.logger.error("Option %s is required for %s", self.expand_option_name(req),
options["subcommand"])
failed = True

for notsup in notsupported:
if options[notsup]:
self.logger.error("%s is not supported by %s", notsup, options["subcommand"])
self.logger.error("Option %s is not supported by %s",
self.expand_option_name(notsup), options["subcommand"])
failed = True

if failed:
Expand Down Expand Up @@ -244,3 +246,14 @@ def verify_task_permissions(self, selected_items: dict, user_groups: list, confi
return True
self.logger.debug("Task forbidden")
return False

@staticmethod
def expand_option_name(option: str):
"""Expand name of option variable name to option argument name"""
if option == "infra":
option = "infrastructure"
elif option == "raw_output":
option = "raw_runner_output"

formatted_option = "--" + option.replace("_", "-")
return formatted_option
78 changes: 39 additions & 39 deletions tests/01-argument_parsing.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,55 +6,55 @@ source ./tests/testing_lib.sh
echo -e " ___ _ _ _\n / _ \/ | __ _ _ __ __ _ _ _ _ __ ___ ___ _ __ | |_ _ __ __ _ _ __ ___(_)_ __ __ _\n | | | | | _____ / _\` | '__/ _\` | | | | '_ \` _ \ / _ \ '_ \| __| | '_ \ / _\` | '__/ __| | '_ \ / _\` |\n | |_| | | |_____| | (_| | | | (_| | |_| | | | | | | __/ | | | |_ | |_) | (_| | | \__ \ | | | | (_| |\n \___/|_| \__,_|_| \__, |\__,_|_| |_| |_|\___|_| |_|\__| | .__/ \__,_|_| |___/_|_| |_|\__, |\n |___/ |_| |___/\n _ _ _ _\n __ ___ __ ___ _ __ __ _ ___ ___ _ __ ___ | |__ (_)_ __ __ _| |_(_) ___ _ __ ___\n \ \ /\ / / '__/ _ \| '_ \ / _\` | / __/ _ \| '_ \` _ \| '_ \| | '_ \ / _\` | __| |/ _ \| '_ \/ __|\n \ V V /| | | (_) | | | | (_| | | (_| (_) | | | | | | |_) | | | | | (_| | |_| | (_) | | | \__ \ \n \_/\_/ |_| \___/|_| |_|\__, | \___\___/|_| |_| |_|_.__/|_|_| |_|\__,_|\__|_|\___/|_| |_|___/\n |___/\n"
# Check wrong combinations
check_message_in_output "ansible-deployer" "\[CRITICAL\]: Too few arguments"
check_message_in_output "ansible-deployer run" "\[ERROR\]: task is required for run"
check_message_in_output "ansible-deployer verify" "\[ERROR\]: task is required for verify"
check_message_in_output "ansible-deployer run" "\[ERROR\]: infra is required for run"
check_message_in_output "ansible-deployer verify" "\[ERROR\]: infra is required for verify"
check_message_in_output "ansible-deployer run" "\[ERROR\]: stage is required for run"
check_message_in_output "ansible-deployer verify" "\[ERROR\]: stage is required for verify"
check_message_in_output "ansible-deployer run --task task_exec_bin_true" "\[ERROR\]: stage is required for run"
check_message_in_output "ansible-deployer verify --task task_exec_bin_true" "\[ERROR\]: stage is required for verify"
check_message_in_output "ansible-deployer run --task task_exec_bin_true" "\[ERROR\]: infra is required for run"
check_message_in_output "ansible-deployer verify --task task_exec_bin_true" "\[ERROR\]: infra is required for verify"
check_message_in_output "ansible-deployer run --task task_exec_bin_true --infrastructure testInfra" "stage is required for run"
check_message_in_output "ansible-deployer verify --task task_exec_bin_true --infrastructure testInfra" "stage is required for verify"
check_message_in_output "ansible-deployer run" "\[ERROR\]: Option --task is required for run"
check_message_in_output "ansible-deployer verify" "\[ERROR\]: Option --task is required for verify"
check_message_in_output "ansible-deployer run" "\[ERROR\]: Option --infrastructure is required for run"
check_message_in_output "ansible-deployer verify" "\[ERROR\]: Option --infrastructure is required for verify"
check_message_in_output "ansible-deployer run" "\[ERROR\]: Option --stage is required for run"
check_message_in_output "ansible-deployer verify" "\[ERROR\]: Option --stage is required for verify"
check_message_in_output "ansible-deployer run --task task_exec_bin_true" "\[ERROR\]: Option --stage is required for run"
check_message_in_output "ansible-deployer verify --task task_exec_bin_true" "\[ERROR\]: Option --stage is required for verify"
check_message_in_output "ansible-deployer run --task task_exec_bin_true" "\[ERROR\]: Option --infrastructure is required for run"
check_message_in_output "ansible-deployer verify --task task_exec_bin_true" "\[ERROR\]: Option --infrastructure is required for verify"
check_message_in_output "ansible-deployer run --task task_exec_bin_true --infrastructure testInfra" "Option --stage is required for run"
check_message_in_output "ansible-deployer verify --task task_exec_bin_true --infrastructure testInfra" "Option --stage is required for verify"

check_message_in_output "ansible-deployer --task task_exec_bin_true --infrastructure testInfra" "\[CRITICAL\]: First positional argument (subcommand) is required! Available commands are: run, lock, unlock, verify, show."

check_message_in_output "ansible-deployer verify --task task_exec_bin_true --infrastructure testInfra --stage prod --commit testCommit" "commit is not supported by verify"
check_message_in_output "ansible-deployer lock --task task_exec_bin_true --infrastructure testInfra" "\[ERROR\]: task is not supported by lock"
check_message_in_output "ansible-deployer lock --task task_exec_bin_true --infrastructure testInfra --stage prod" "\[ERROR\]: task is not supported by lock"
check_message_in_output "ansible-deployer lock --task task_exec_bin_true --stage prod" "\[ERROR\]: infra is required for lock"
check_message_in_output "ansible-deployer lock --task task_exec_bin_true --stage prod --commit X" "\[ERROR\]: commit is not supported by lock"
check_message_in_output "ansible-deployer lock --infra testInfra --stage prod --keep-locked" "\[ERROR\]: keep_locked is not supported by lock"
check_message_in_output "ansible-deployer lock --task task_exec_bin_true --limit test_hosts_1" "\[ERROR\]: limit is not supported by lock"
check_message_in_output "ansible-deployer lock --infra testInfra --stage prod --raw-runner-output" "\[ERROR\]: raw_output is not supported by lock"
check_message_in_output "ansible-deployer lock --infra testInfra --stage prod --self-setup ." "\[ERROR\]: self_setup is not supported by lock"
check_message_in_output "ansible-deployer verify --task task_exec_bin_true --infrastructure testInfra --stage prod --commit testCommit" "Option --commit is not supported by verify"
check_message_in_output "ansible-deployer lock --task task_exec_bin_true --infrastructure testInfra" "\[ERROR\]: Option --task is not supported by lock"
check_message_in_output "ansible-deployer lock --task task_exec_bin_true --infrastructure testInfra --stage prod" "\[ERROR\]: Option --task is not supported by lock"
check_message_in_output "ansible-deployer lock --task task_exec_bin_true --stage prod" "\[ERROR\]: Option --infrastructure is required for lock"
check_message_in_output "ansible-deployer lock --task task_exec_bin_true --stage prod --commit X" "\[ERROR\]: Option --commit is not supported by lock"
check_message_in_output "ansible-deployer lock --infra testInfra --stage prod --keep-locked" "\[ERROR\]: Option --keep-locked is not supported by lock"
check_message_in_output "ansible-deployer lock --task task_exec_bin_true --limit test_hosts_1" "\[ERROR\]: Option --limit is not supported by lock"
check_message_in_output "ansible-deployer lock --infra testInfra --stage prod --raw-runner-output" "\[ERROR\]: Option --raw-runner-output is not supported by lock"
check_message_in_output "ansible-deployer lock --infra testInfra --stage prod --self-setup ." "\[ERROR\]: Option --self-setup is not supported by lock"

check_message_in_output "ansible-deployer unlock --task task_exec_bin_true --infrastructure testInfra" "\[ERROR\]: task is not supported by unlock"
check_message_in_output "ansible-deployer unlock --task task_exec_bin_true --infrastructure testInfra --stage prod" "\[ERROR\]: task is not supported by unlock"
check_message_in_output "ansible-deployer unlock --task task_exec_bin_true --stage test" "\[ERROR\]: infra is required for unlock"
check_message_in_output "ansible-deployer unlock --task task_exec_bin_true --stage prod --commit X" "\[ERROR\]: commit is not supported by unlock"
check_message_in_output "ansible-deployer unlock --infra testInfra --stage prod --keep-locked" "\[ERROR\]: keep_locked is not supported by unlock"
check_message_in_output "ansible-deployer unlock --task task_exec_bin_true --limit test_hosts_1" "\[ERROR\]: limit is not supported by unlock"
check_message_in_output "ansible-deployer unlock --infra testInfra --stage prod --raw-runner-output" "\[ERROR\]: raw_output is not supported by unlock"
check_message_in_output "ansible-deployer unlock --infra testInfra --stage prod --self-setup ." "\[ERROR\]: self_setup is not supported by unlock"
check_message_in_output "ansible-deployer unlock --task task_exec_bin_true --infrastructure testInfra" "\[ERROR\]: Option --task is not supported by unlock"
check_message_in_output "ansible-deployer unlock --task task_exec_bin_true --infrastructure testInfra --stage prod" "\[ERROR\]: Option --task is not supported by unlock"
check_message_in_output "ansible-deployer unlock --task task_exec_bin_true --stage test" "\[ERROR\]: Option --infrastructure is required for unlock"
check_message_in_output "ansible-deployer unlock --task task_exec_bin_true --stage prod --commit X" "\[ERROR\]: Option --commit is not supported by unlock"
check_message_in_output "ansible-deployer unlock --infra testInfra --stage prod --keep-locked" "\[ERROR\]: Option --keep-locked is not supported by unlock"
check_message_in_output "ansible-deployer unlock --task task_exec_bin_true --limit test_hosts_1" "\[ERROR\]: Option --limit is not supported by unlock"
check_message_in_output "ansible-deployer unlock --infra testInfra --stage prod --raw-runner-output" "\[ERROR\]: Option --raw-runner-output is not supported by unlock"
check_message_in_output "ansible-deployer unlock --infra testInfra --stage prod --self-setup ." "\[ERROR\]: Option --self-setup is not supported by unlock"

check_message_in_output "ansible-deployer run test" "\[CRITICAL\]: Too many positional arguments! Only subcommand \"show\" can accept following arguments: all, task, infra."
check_message_in_output "ansible-deployer verify test" "\[CRITICAL\]: Too many positional arguments! Only subcommand \"show\" can accept following arguments: all, task, infra."
check_message_in_output "ansible-deployer lock test" "\[CRITICAL\]: Too many positional arguments! Only subcommand \"show\" can accept following arguments: all, task, infra."
check_message_in_output "ansible-deployer unlock test" "\[CRITICAL\]: Too many positional arguments! Only subcommand \"show\" can accept following arguments: all, task, infra."
check_message_in_output "ansible-deployer show test" "\[CRITICAL\]: Invalid argument test! Subcommand \"show\" can accept only following arguments: all, task, infra."
check_message_in_output "ansible-deployer show --commit task_exec_bin_true" "\[ERROR\]: commit is not supported by show"
check_message_in_output "ansible-deployer show --limit test_hosts_1" "\[ERROR\]: limit is not supported by show"
check_message_in_output "ansible-deployer show --task task_exec_bin_true" "\[ERROR\]: task is not supported by show"
check_message_in_output "ansible-deployer show --infra testInfra" "\[ERROR\]: infra is not supported by show"
check_message_in_output "ansible-deployer show --stage prod" "\[ERROR\]: stage is not supported by show"
check_message_in_output "ansible-deployer show --dry" "\[ERROR\]: dry is not supported by show"
check_message_in_output "ansible-deployer show --keep-locked" "\[ERROR\]: keep_locked is not supported by show"
check_message_in_output "ansible-deployer show --syslog" "\[ERROR\]: syslog is not supported by show"
check_message_in_output "ansible-deployer show --raw-runner-output" "\[ERROR\]: raw_output is not supported by show"
check_message_in_output "ansible-deployer show --self-setup ." "\[ERROR\]: self_setup is not supported by show"
check_message_in_output "ansible-deployer show --commit task_exec_bin_true" "\[ERROR\]: Option --commit is not supported by show"
check_message_in_output "ansible-deployer show --limit test_hosts_1" "\[ERROR\]: Option --limit is not supported by show"
check_message_in_output "ansible-deployer show --task task_exec_bin_true" "\[ERROR\]: Option --task is not supported by show"
check_message_in_output "ansible-deployer show --infra testInfra" "\[ERROR\]: Option --infrastructure is not supported by show"
check_message_in_output "ansible-deployer show --stage prod" "\[ERROR\]: Option --stage is not supported by show"
check_message_in_output "ansible-deployer show --dry" "\[ERROR\]: Option --dry is not supported by show"
check_message_in_output "ansible-deployer show --keep-locked" "\[ERROR\]: Option --keep-locked is not supported by show"
check_message_in_output "ansible-deployer show --syslog" "\[ERROR\]: Option --syslog is not supported by show"
check_message_in_output "ansible-deployer show --raw-runner-output" "\[ERROR\]: Option --raw-runner-output is not supported by show"
check_message_in_output "ansible-deployer show --self-setup ." "\[ERROR\]: Option --self-setup is not supported by show"

echo -e " ___ _ _ _\n / _ \/ | __ _ _ __ __ _ _ _ _ __ ___ ___ _ __ | |_ _ __ __ _ _ __ ___(_)_ __ __ _\n | | | | | _____ / _\` | '__/ _\` | | | | '_ \` _ \ / _ \ '_ \| __| | '_ \ / _\` | '__/ __| | '_ \ / _\` |\n | |_| | | |_____| | (_| | | | (_| | |_| | | | | | | __/ | | | |_ | |_) | (_| | | \__ \ | | | | (_| |\n \___/|_| \__,_|_| \__, |\__,_|_| |_| |_|\___|_| |_|\__| | .__/ \__,_|_| |___/_|_| |_|\__, |\n |___/ |_| |___/\n _ _ _ _ _\n ___ ___ _ __ _ __ ___ ___| |_ ___ ___ _ __ ___ | |__ (_)_ __ __ _| |_(_) ___ _ __ ___\n / __/ _ \| '__| '__/ _ \/ __| __| / __/ _ \| '_ \` _ \| '_ \| | '_ \ / _\` | __| |/ _ \| '_ \/ __|\n | (_| (_) | | | | | __/ (__| |_ | (_| (_) | | | | | | |_) | | | | | (_| | |_| | (_) | | | \__ \ \n \___\___/|_| |_| \___|\___|\__| \___\___/|_| |_| |_|_.__/|_|_| |_|\__,_|\__|_|\___/|_| |_|___/\n"
# Check if correct combinations are accepted
Expand Down
Loading

0 comments on commit a536dad

Please sign in to comment.