Skip to content

Commit

Permalink
fixup: options validation (better approach - for long time maintenance)
Browse files Browse the repository at this point in the history
  • Loading branch information
cinek810 committed Jan 29, 2022
1 parent 4b090e2 commit 2b29b7a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 39 deletions.
55 changes: 31 additions & 24 deletions ansible_deploy/command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import argparse
import logging
import datetime
import yaml
from logging import handlers as log_han


Expand Down Expand Up @@ -103,39 +104,44 @@ 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))
failed = False
required = []
notsupportd = []
if subcommand == "run":
if not options["task"]:
logger.error("run requires -t/--task")
failed = True
if not options["infra"]:
logger.error("run requires -i/--infrastructure")
failed = True
if not options["stage"]:
logger.error("run requires -s/--stage")
failed = True
required = ["task", "infra", "stage"]
notsupported = []
elif subcommand in ("lock", "unlock"):
if options["task"]:
logger.error("-t/--task is not compatible with %s", subcommand)
failed = True
if options["commit"]:
logger.error("-c/--commit is not compatible with %s", subcommand)
failed = True
if not options["infra"]:
logger.error("%s requires -i/--infrastructure", subcommand)
required = ["infra", "stage"]
notsupported = ["task", "commit"]
elif subcommand == "list":
required = []
notsupported = ["commit"]

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

for notsup in notsupported:
if options[notsup] is not None:
logger.error("%s is not supported by %s", notsup, subcommand);
failed = True
elif subcommand == "list":
if options["commit"]:
logger.error("-c/--commit is not compatible with list")

if failed:
sys.exit(55)

def load_configuration():
"""Function responsible for reading configuration files and running a schema validator against
it"""
it
Location of configuration files is hard coded due to security reasons.
"""
#CONF_DIR = /etc/ansible-deploy/

#TODO: Add verification of owner/group/persmissions
#with open(CONF_DIR+"/infra.yaml")




def validate_user_infra_stage():
"""Function checking if user has rights to execute command on selected infrastructure
Expand All @@ -159,5 +165,6 @@ def main():
subcommand = get_sub_command(sys.argv[1])
options = parse_options(sys.argv[2:])
validate_options(options, subcommand)
load_configuration()
#create_workdir(start_ts, WORKDIR)
sys.exit(0)
30 changes: 15 additions & 15 deletions tests/01-test_argument_parsing.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,24 @@ check_output() {

#Check wrong combinations
check_output 'ansible-deploy' '\[ERROR\]: To fee arguments'
check_output 'ansible-deploy run' '\[ERROR\]: run requires -t/--task'
check_output 'ansible-deploy run' '\[ERROR\]: run requires -i/--infrastructure'
check_output 'ansible-deploy run' '\[ERROR\]: run requires -s/--stage'
check_output 'ansible-deploy run -t testTask' '\[ERROR\]: run requires -s/--stage'
check_output 'ansible-deploy run -t testTask' '\[ERROR\]: run requires -i/--infrastructure'
check_output 'ansible-deploy run -t testTask -i testInfra' '\[ERROR\]: run requires -s/--stage'
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\]: -t/--task is not compatible with lock'
check_output 'ansible-deploy lock -t testTask -i testInfra' '\[ERROR\]: lock requires -s/--stage'
check_output 'ansible-deploy lock -t testTask -s prod' '\[ERROR\]: lock requires -i/--infrastructure'
check_output 'ansible-deploy lock -t testTask -s prod -c X' '\[ERROR\]: -c/--commit is not compatible with lock'
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\]: -t/--task is not compatible with unlock'
check_output 'ansible-deploy unlock -t testTask -i testInfra' '\[ERROR\]: unlock requires -s/--stage'
check_output 'ansible-deploy unlock -t testTask -s test' '\[ERROR\]: unlock requires -i/--infrastructure'
check_output 'ansible-deploy unlock -t testTask -s prod -c X' '\[ERROR\]: -c/--commit is not compatible with unlock'
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\]: -c/--commit is not compatible with list'
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
Expand Down

0 comments on commit 2b29b7a

Please sign in to comment.