Created
May 31, 2017 20:35
-
-
Save ashcrow/498608161bcb73fe3c6c2b54f8d2ab16 to your computer and use it in GitHub Desktop.
Proposed updates to gas.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from __future__ import print_function | |
import argparse | |
import logging | |
import os | |
import jinja2 | |
import yaml | |
from subprocess import call | |
RED = '\033[91m' | |
GREEN = '\033[92m' | |
BLUE = '\033[94m' | |
ENDC = '\033[0m' | |
def set_options(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument( | |
"-n", "--name", dest="name", default="", | |
help="Run specific task" | |
) | |
parser.add_argument( | |
"-p", "--path", default=os.getcwd(), | |
help="Location of role to test" | |
) | |
return parser | |
def render_template(searchpath, template_file, template_vars): | |
templateLoader = jinja2.FileSystemLoader(searchpath=searchpath) | |
templateEnv = jinja2.Environment(loader=templateLoader) | |
template = templateEnv.get_template(template_file) | |
content = template.render(template_vars) | |
return content | |
def run_molecule(path, scenario): | |
# Get current dir and move to requested directory | |
cwd = os.getcwd() | |
os.chdir(path) | |
vars = { | |
"containers": filter( | |
lambda l: l["name"] in scenario["containers"], data["containers"]), | |
"vars": scenario["vars"], | |
"playbook": "" if "playbook" not in scenario else scenario["playbook"], | |
} | |
output = render_template(".", "molecule.yml.j2", vars) | |
# yaml formatting | |
output = yaml.load(output) | |
with open("molecule.yml", "w") as f: | |
yaml.dump(output, f, default_flow_style=False) | |
result = call("molecule test", shell=True) | |
# Move back | |
os.chdir(cwd) | |
return result | |
def main(): | |
options = set_options().parse_args() | |
try: | |
with open("scenarios.yml", "r") as f: | |
data = yaml.load(f) | |
except IOError as e: | |
logging.error( | |
"Unable to find scenarios.yml in %s. Exiting due to %s", | |
options.path, e) | |
raise SystemExit(1) | |
results = {} | |
found = False | |
for scenario in data["scenarios"]: | |
if scenario["name"] != "" and scenario["name"] != options.name: | |
continue | |
rc = run_molecule(options.path, scenario) | |
print("RC: {}".format(rc)) | |
results[scenario["name"]] = False | |
if rc == 0: | |
results[scenario["name"]] = True | |
if scenario["name"] != "" and scenario["name"] == options.name: | |
found = True | |
break | |
if not found: | |
logging.error("Scenario %s not found", options.name) | |
raise SystemExit(3) | |
failed = False | |
passed = 0 | |
for name in results: | |
if results[name]: | |
print("{}Scenario {} passed{}".format(GREEN, name, ENDC)) | |
passed = passed + 1 | |
else: | |
print("{}Scenario {} failed{}".format(RED, name, ENDC)) | |
failed = True | |
if options.name == "": | |
print("{}{}/{} passed{}".format(BLUE, passed, len(results), ENDC)) | |
else: | |
print("{}1 passed, {} skipped{}".format(BLUE, len(results) - 1, ENDC)) | |
if failed: | |
raise SystemExit(2) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment