Advertisement
Guest User

Untitled

a guest
May 23rd, 2015
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. from __future__ import print_function
  2. from shovel import task
  3. import os, sys, subprocess
  4. from yaml import load, YAMLError
  5. try:
  6. from yaml import CLoader as Loader, CDumper as Dumper
  7. except ImportError:
  8. from yaml import Loader, Dumper
  9.  
  10. @task
  11. def run(name=None, play=False):
  12. '''Runs tests on given file or all discovered files'''
  13. if name:
  14. run_tests(name, play)
  15. else:
  16. for root, dirs, files in os.walk('.'):
  17. if '.git' in root or '.vagrant' in root:
  18. continue
  19. play = False
  20. for fn in files:
  21. if root.endswith('plays'):
  22. play = True
  23. if fn.endswith('.yml'):
  24. run_tests(os.path.join(root, fn), play)
  25.  
  26. def run_tests(name, is_playbook=False):
  27. checked = False
  28. if name and os.path.isfile(name):
  29. if name.endswith('.yml'):
  30. checked = True
  31. check_yaml(name)
  32. if is_playbook:
  33. checked = True
  34. check_syntax(name)
  35. check_lint(name)
  36. if not checked:
  37. print('WARNING: not-testable: {0}: no tests configured for this file'.format(name), file=sys.stderr)
  38. else:
  39. print('ERROR: no such file or directory: {0}'.format(name), file=sys.stderr)
  40. def check_yaml(name):
  41. try:
  42. data = load(file(name, 'r'), Loader=Loader)
  43. except YAMLError, exc:
  44. if hasattr(exc, 'problem_mark'):
  45. mark = exc.problem_mark
  46. print('ERROR: yaml: {0}: error at position {1}:{2}'.format(name, mark.line + 1, mark.column + 1), file=sys.stderr)
  47. return False
  48. else:
  49. print('ERROR: yaml: {0}: unknown parse error: {1}'.format(name, exc), file=sys.stderr)
  50. return False
  51. print('OK: yaml: {0}'.format(name))
  52.  
  53.  
  54. def check_syntax(name):
  55. '''Runs ansible syntax check on one file'''
  56. ansible_cmd = ['ansible-playbook', '-i', 'inv/tests.inv']
  57. try:
  58. ret = subprocess.check_call(ansible_cmd + ['--syntax-check', name])
  59. print('OK: ansible-check: {0}'.format(name))
  60. except subprocess.CalledProcessError, exc:
  61. print('ERROR: ansible-check: {0}: erorr running syntax check'.format(name), file=sys.stderr)
  62.  
  63. def check_lint(name):
  64. '''Runs ansible linton one file'''
  65. lint_cmd = ['ansible-lint']
  66. try:
  67. ret = subprocess.check_call(lint_cmd + [name])
  68. print('OK: ansible-lint: {0}'.format(name))
  69. except subprocess.CalledProcessError, exc:
  70. print('ERROR: ansible-lint: {0}: erorr running lint'.format(name), file=sys.stderr)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement