Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import print_function
- from shovel import task
- import os, sys, subprocess
- from yaml import load, YAMLError
- try:
- from yaml import CLoader as Loader, CDumper as Dumper
- except ImportError:
- from yaml import Loader, Dumper
- @task
- def run(name=None, play=False):
- '''Runs tests on given file or all discovered files'''
- if name:
- run_tests(name, play)
- else:
- for root, dirs, files in os.walk('.'):
- if '.git' in root or '.vagrant' in root:
- continue
- play = False
- for fn in files:
- if root.endswith('plays'):
- play = True
- if fn.endswith('.yml'):
- run_tests(os.path.join(root, fn), play)
- def run_tests(name, is_playbook=False):
- checked = False
- if name and os.path.isfile(name):
- if name.endswith('.yml'):
- checked = True
- check_yaml(name)
- if is_playbook:
- checked = True
- check_syntax(name)
- check_lint(name)
- if not checked:
- print('WARNING: not-testable: {0}: no tests configured for this file'.format(name), file=sys.stderr)
- else:
- print('ERROR: no such file or directory: {0}'.format(name), file=sys.stderr)
- def check_yaml(name):
- try:
- data = load(file(name, 'r'), Loader=Loader)
- except YAMLError, exc:
- if hasattr(exc, 'problem_mark'):
- mark = exc.problem_mark
- print('ERROR: yaml: {0}: error at position {1}:{2}'.format(name, mark.line + 1, mark.column + 1), file=sys.stderr)
- return False
- else:
- print('ERROR: yaml: {0}: unknown parse error: {1}'.format(name, exc), file=sys.stderr)
- return False
- print('OK: yaml: {0}'.format(name))
- def check_syntax(name):
- '''Runs ansible syntax check on one file'''
- ansible_cmd = ['ansible-playbook', '-i', 'inv/tests.inv']
- try:
- ret = subprocess.check_call(ansible_cmd + ['--syntax-check', name])
- print('OK: ansible-check: {0}'.format(name))
- except subprocess.CalledProcessError, exc:
- print('ERROR: ansible-check: {0}: erorr running syntax check'.format(name), file=sys.stderr)
- def check_lint(name):
- '''Runs ansible linton one file'''
- lint_cmd = ['ansible-lint']
- try:
- ret = subprocess.check_call(lint_cmd + [name])
- print('OK: ansible-lint: {0}'.format(name))
- except subprocess.CalledProcessError, exc:
- print('ERROR: ansible-lint: {0}: erorr running lint'.format(name), file=sys.stderr)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement