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

Add more option to notsupported lists #153

Merged
merged 3 commits into from
Jan 30, 2023
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 setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def read(fname):
# This call to setup() does all the work
setup(
name="ansible-deployer",
version="0.0.39",
version="0.0.40",
description="Wrapper around ansible-playbook allowing configurable tasks and permissions",
long_description=README,
long_description_content_type="text/markdown",
Expand Down
23 changes: 19 additions & 4 deletions source/modules/validators/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,23 @@ def validate_options(self, options: dict):
notsupported = ["switches", "commit"]
elif options["subcommand"] in ("lock", "unlock"):
required = ["infra", "stage"]
notsupported = ["switches", "task", "commit", "limit", "raw_output"]
cinek810 marked this conversation as resolved.
Show resolved Hide resolved
notsupported = ["switches", "task", "commit", "keep_locked", "limit", "raw_output",
"self_setup"]
elif options["subcommand"] == "show":
notsupported = ["commit", "keep_locked", "limit", "raw_output"]
notsupported = ["task", "infra", "stage", "commit", "dry", "keep_locked", "syslog",
"limit", "raw_output", "self_setup"]

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 @@ -242,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):
cinek810 marked this conversation as resolved.
Show resolved Hide resolved
"""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
65 changes: 39 additions & 26 deletions tests/01-argument_parsing.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +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 --task task_exec_bin_true --limit test_hosts_1" "\[ERROR\]: limit 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 --task task_exec_bin_true --limit test_hosts_1" "\[ERROR\]: limit 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 --keep-locked" "\[ERROR\]: keep_locked 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