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 Aug 29, 2022
1 parent 38f0097 commit 33abd51
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 41 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-test_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 \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 -t task_exec_bin_true' '\[ERROR\]: stage is required for run'
check_message_in_output 'ansible-deployer verify -t task_exec_bin_true' '\[ERROR\]: stage is required for verify'
check_message_in_output 'ansible-deployer run -t task_exec_bin_true' '\[ERROR\]: infra is required for run'
check_message_in_output 'ansible-deployer verify -t task_exec_bin_true' '\[ERROR\]: infra is required for verify'
check_message_in_output 'ansible-deployer run -t task_exec_bin_true -i testInfra' 'stage is required for run'
check_message_in_output 'ansible-deployer verify -t task_exec_bin_true -i 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 -t task_exec_bin_true' '\[ERROR\]: Option --stage is required for run'
check_message_in_output 'ansible-deployer verify -t task_exec_bin_true' '\[ERROR\]: Option --stage is required for verify'
check_message_in_output 'ansible-deployer run -t task_exec_bin_true' '\[ERROR\]: Option --infrastructure is required for run'
check_message_in_output 'ansible-deployer verify -t task_exec_bin_true' '\[ERROR\]: Option --infrastructure is required for verify'
check_message_in_output 'ansible-deployer run -t task_exec_bin_true -i testInfra' 'Option --stage is required for run'
check_message_in_output 'ansible-deployer verify -t task_exec_bin_true -i testInfra' 'Option --stage is required for verify'

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

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

check_message_in_output 'ansible-deployer unlock -t task_exec_bin_true -i testInfra' '\[ERROR\]: task is not supported by unlock'
check_message_in_output 'ansible-deployer unlock -t task_exec_bin_true -i testInfra -s prod' '\[ERROR\]: task is not supported by unlock'
check_message_in_output 'ansible-deployer unlock -t task_exec_bin_true -s test' '\[ERROR\]: infra is required for unlock'
check_message_in_output 'ansible-deployer unlock -t task_exec_bin_true -s prod -c X' '\[ERROR\]: commit is not supported by unlock'
check_message_in_output 'ansible-deployer unlock -i testInfra -s prod --keep-locked' '\[ERROR\]: keep_locked is not supported by unlock'
check_message_in_output 'ansible-deployer unlock -t task_exec_bin_true -l test_hosts_1' '\[ERROR\]: limit is not supported by unlock'
check_message_in_output 'ansible-deployer unlock -i testInfra -s prod --raw-runner-output' '\[ERROR\]: raw_output is not supported by unlock'
check_message_in_output 'ansible-deployer unlock -i testInfra -s prod --self-setup .' '\[ERROR\]: self_setup is not supported by unlock'
check_message_in_output 'ansible-deployer unlock -t task_exec_bin_true -i testInfra' '\[ERROR\]: Option --task is not supported by unlock'
check_message_in_output 'ansible-deployer unlock -t task_exec_bin_true -i testInfra -s prod' '\[ERROR\]: Option --task is not supported by unlock'
check_message_in_output 'ansible-deployer unlock -t task_exec_bin_true -s test' '\[ERROR\]: Option --infrastructure is required for unlock'
check_message_in_output 'ansible-deployer unlock -t task_exec_bin_true -s prod -c X' '\[ERROR\]: Option --commit is not supported by unlock'
check_message_in_output 'ansible-deployer unlock -i testInfra -s prod --keep-locked' '\[ERROR\]: Option --keep-locked is not supported by unlock'
check_message_in_output 'ansible-deployer unlock -t task_exec_bin_true -l test_hosts_1' '\[ERROR\]: Option --limit is not supported by unlock'
check_message_in_output 'ansible-deployer unlock -i testInfra -s prod --raw-runner-output' '\[ERROR\]: Option --raw-runner-output is not supported by unlock'
check_message_in_output 'ansible-deployer unlock -i testInfra -s 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 -l test_hosts_1' '\[ERROR\]: limit is not supported by show'
check_message_in_output 'ansible-deployer show -t task_exec_bin_true' '\[ERROR\]: task is not supported by show'
check_message_in_output 'ansible-deployer show -i testInfra' '\[ERROR\]: infra is not supported by show'
check_message_in_output 'ansible-deployer show -s 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 -l test_hosts_1' '\[ERROR\]: Option --limit is not supported by show'
check_message_in_output 'ansible-deployer show -t task_exec_bin_true' '\[ERROR\]: Option --task is not supported by show'
check_message_in_output 'ansible-deployer show -i testInfra' '\[ERROR\]: Option --infrastructure is not supported by show'
check_message_in_output 'ansible-deployer show -s 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 \n"
#Check if correct combinations are accepted
Expand Down

0 comments on commit 33abd51

Please sign in to comment.