infra_tests.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2016 Google Inc.
  4. #
  5. # Use of this source code is governed by a BSD-style license that can be
  6. # found in the LICENSE file.
  7. """Run all infrastructure-related tests."""
  8. import os
  9. import subprocess
  10. import sys
  11. INFRA_BOTS_DIR = os.path.dirname(os.path.realpath(__file__))
  12. SKIA_DIR = os.path.abspath(os.path.join(INFRA_BOTS_DIR, os.pardir, os.pardir))
  13. def test(cmd, cwd):
  14. try:
  15. subprocess.check_output(cmd, cwd=cwd, stderr=subprocess.STDOUT)
  16. except subprocess.CalledProcessError as e:
  17. return e.output
  18. def python_unit_tests(train):
  19. if train:
  20. return None
  21. return test(
  22. ['python', '-m', 'unittest', 'discover', '-s', '.', '-p', '*_test.py'],
  23. INFRA_BOTS_DIR)
  24. def recipe_test(train):
  25. cmd = [
  26. 'python', os.path.join(INFRA_BOTS_DIR, 'recipes.py'), 'test']
  27. if train:
  28. cmd.append('train')
  29. else:
  30. cmd.append('run')
  31. return test(cmd, SKIA_DIR)
  32. def gen_tasks_test(train):
  33. cmd = ['go', 'run', 'gen_tasks.go']
  34. if not train:
  35. cmd.append('--test')
  36. try:
  37. output = test(cmd, INFRA_BOTS_DIR)
  38. except OSError:
  39. return ('Failed to run "%s"; do you have Go installed on your machine?'
  40. % ' '.join(cmd))
  41. if output:
  42. if ('cannot find package "go.skia.org/infra' in output or
  43. 'gen_tasks.go:' in output):
  44. return ('Failed to run gen_tests.go:\n\n%s\nMaybe you need to run:\n\n'
  45. '$ go get -u go.skia.org/infra/...' % output)
  46. return output
  47. def main():
  48. train = False
  49. if '--train' in sys.argv:
  50. train = True
  51. tests = (
  52. python_unit_tests,
  53. recipe_test,
  54. gen_tasks_test,
  55. )
  56. errs = []
  57. for t in tests:
  58. err = t(train)
  59. if err:
  60. errs.append(err)
  61. if len(errs) > 0:
  62. print >> sys.stderr, 'Test failures:\n'
  63. for err in errs:
  64. print >> sys.stderr, '=============================='
  65. print >> sys.stderr, err
  66. print >> sys.stderr, '=============================='
  67. sys.exit(1)
  68. if train:
  69. print 'Trained tests successfully.'
  70. else:
  71. print 'All tests passed!'
  72. if __name__ == '__main__':
  73. main()