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

Change option parsing testing and add subcommands and subcommand vs option validation #9

Merged
merged 3 commits into from
Jan 31, 2022
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
8 changes: 2 additions & 6 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,8 @@ jobs:
- run:
command: "cat $(which ansible-deploy)"
- run:
name: "Simple exec"
command: ansible-deploy || if [ $? -eq 2 ]; then exit 0; fi
- run:
name: "Fail if run without tasks with exit 55"
command: ansible-deploy run || if [ $? -eq 55 ]; then exit 0; fi

name: "Run shell script for argument parsing"
command: ./tests/01-test_argument_parsing.sh

install_and_exec:
# Specify the execution environment. You can specify an image from Dockerhub or use one of our Convenience Images from CircleCI's Developer Hub.
Expand Down
34 changes: 29 additions & 5 deletions ansible_deploy/command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ def get_sub_command(command):
"""Function to check the first arguments (argv[1..]) looking for a subcommand"""
if command == "run":
subcommand = "run"
elif command == "lock":
subcommand = "lock"
elif command == "unlock":
subcommand = "unlock"
elif command == "list":
subcommand = "list"
else:
logger.error("Unknown subcommand :%s", (command))
sys.exit("55")
Expand Down Expand Up @@ -68,7 +74,7 @@ def parse_options(argv):
arguments = parser.parse_args(argv)

options = {}
options["infrastructure"] = arguments.infrastructure[0]
options["infra"] = arguments.infrastructure[0]
options["stage"] = arguments.stage[0]
options["commit"] = arguments.commit[0]
options["task"] = arguments.task[0]
Expand Down Expand Up @@ -97,11 +103,29 @@ def create_workdir(timestamp: str, base_dir: str):
def validate_options(options, subcommand):
"""Function checking if the options set are allowed in this subcommand"""
logger.debug("validate_options running for subcommand: %s", (subcommand))
required = []
notsupported = []
if subcommand == "run":
if not options["task"]:
logger.error("run requires --task")
sys.exit(55)
return True
required = ["task", "infra", "stage"]
elif subcommand in ("lock", "unlock"):
required = ["infra", "stage"]
notsupported = ["task", "commit"]
elif subcommand == "list":
notsupported = ["commit"]

failed = False
for req in required:
if options[req] is None:
logger.error("%s is required for %s", req, subcommand)
failed = True

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

if failed:
sys.exit(55)

def load_configuration():
"""Function responsible for reading configuration files and running a schema validator against
Expand Down
44 changes: 44 additions & 0 deletions tests/01-test_argument_parsing.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash -l

check_output() {
CMD=$1
EXPTEXT=$2

eval "$CMD |& grep '$EXPTEXT'"
if [ $? -eq 0 ]
then
echo "OK: '${CMD} returned ${EXPTEXT}'"
else
echo "FAILED: '${CMD}' didn't return '${EXPTEXT}'"
exit 1
fi
}

#Check wrong combinations
check_output 'ansible-deploy' '\[ERROR\]: To fee arguments'
check_output 'ansible-deploy run' '\[ERROR\]: task is required for run'
check_output 'ansible-deploy run' '\[ERROR\]: infra is required for run'
check_output 'ansible-deploy run' '\[ERROR\]: stage is required for run'
check_output 'ansible-deploy run -t testTask' '\[ERROR\]: stage is required for run'
check_output 'ansible-deploy run -t testTask' '\[ERROR\]: infra is required for run'
check_output 'ansible-deploy run -t testTask -i testInfra' 'stage is required for run'

check_output 'ansible-deploy lock -t testTask -i testInfra' '\[ERROR\]: task is not supported by lock'
check_output 'ansible-deploy lock -t testTask -i testInfra -s prod' '\[ERROR\]: task is not supported by lock'
check_output 'ansible-deploy lock -t testTask -s prod' '\[ERROR\]: infra is required for lock'
check_output 'ansible-deploy lock -t testTask -s prod -c X' '\[ERROR\]: commit is not supported by lock'

check_output 'ansible-deploy unlock -t testTask -i testInfra' '\[ERROR\]: task is not supported by unlock'
check_output 'ansible-deploy unlock -t testTask -i testInfra -s prod' '\[ERROR\]: task is not supported by unlock'
check_output 'ansible-deploy unlock -t testTask -s test' '\[ERROR\]: infra is required for unlock'
check_output 'ansible-deploy unlock -t testTask -s prod -c X' '\[ERROR\]: commit is not supported by unlock'

check_output 'ansible-deploy list --commit testTask' '\[ERROR\]: commit is not supported by list'

#Check if correct combinations are accepted
ansible-deploy run -t run_bin_true -s prod -i test1
ansible-deploy run -t run_bin_true -s prod -i test1 --commit test_version
ansible-deploy lock -s prod -i test1
ansible-deploy unlock -s prod -i test1
ansible-deploy list
ansible-deploy list --task="run_bin_true"