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
8
9"""Run all infrastructure-related tests."""
10
11
12import os
13import subprocess
14import sys
15
16
17INFRA_BOTS_DIR = os.path.dirname(os.path.realpath(__file__))
18SKIA_DIR = os.path.abspath(os.path.join(INFRA_BOTS_DIR, os.pardir, os.pardir))
19
20
21def test(cmd, cwd):
22  try:
23    subprocess.check_output(cmd, cwd=cwd, stderr=subprocess.STDOUT)
24  except subprocess.CalledProcessError as e:
25    return e.output
26
27
28def python_unit_tests():
29  return test(
30      ['python', '-m', 'unittest', 'discover', '-s', '.', '-p', '*_test.py'],
31      INFRA_BOTS_DIR)
32
33
34def recipe_simulation_test():
35  return test(
36      ['python', os.path.join(INFRA_BOTS_DIR, 'recipes.py'), 'simulation_test'],
37      SKIA_DIR)
38
39
40def gen_tasks_test():
41  cmd = ['go', 'run', 'gen_tasks.go', '--test']
42  try:
43    output = test(cmd, INFRA_BOTS_DIR)
44  except OSError:
45    return ('Failed to run "%s"; do you have Go installed on your machine?'
46            % ' '.join(cmd))
47  if output and 'cannot find package "go.skia.org/infra' in output:
48    return ('Failed to run gen_tests.go:\n\n%s\nMaybe you need to run:\n\n'
49            '$ go get -u go.skia.org/infra/...' % output)
50  return output
51
52
53def main():
54  tests = (
55      python_unit_tests,
56      recipe_simulation_test,
57      gen_tasks_test,
58  )
59  errs = []
60  for t in tests:
61    err = t()
62    if err:
63      errs.append(err)
64
65  if len(errs) > 0:
66    print >> sys.stderr, 'Test failures:\n'
67    for err in errs:
68      print >> sys.stderr, '=============================='
69      print >> sys.stderr, err
70      print >> sys.stderr, '=============================='
71    sys.exit(1)
72
73  print 'All tests passed!'
74
75
76if __name__ == '__main__':
77  main()
78