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(train): 29 if train: 30 return None 31 return test( 32 ['python', '-m', 'unittest', 'discover', '-s', '.', '-p', '*_test.py'], 33 INFRA_BOTS_DIR) 34 35 36def recipe_test(train): 37 cmd = [ 38 'python', os.path.join(INFRA_BOTS_DIR, 'recipes.py'), 'test'] 39 if train: 40 cmd.append('train') 41 else: 42 cmd.append('run') 43 return test(cmd, SKIA_DIR) 44 45 46def gen_tasks_test(train): 47 cmd = ['go', 'run', 'gen_tasks.go'] 48 if not train: 49 cmd.append('--test') 50 try: 51 output = test(cmd, INFRA_BOTS_DIR) 52 except OSError: 53 return ('Failed to run "%s"; do you have Go installed on your machine?' 54 % ' '.join(cmd)) 55 return output 56 57 58def main(): 59 train = False 60 if '--train' in sys.argv: 61 train = True 62 63 tests = ( 64 python_unit_tests, 65 recipe_test, 66 gen_tasks_test, 67 ) 68 errs = [] 69 for t in tests: 70 err = t(train) 71 if err: 72 errs.append(err) 73 74 if len(errs) > 0: 75 print >> sys.stderr, 'Test failures:\n' 76 for err in errs: 77 print >> sys.stderr, '==============================' 78 print >> sys.stderr, err 79 print >> sys.stderr, '==============================' 80 sys.exit(1) 81 82 if train: 83 print 'Trained tests successfully.' 84 else: 85 print 'All tests passed!' 86 87 88if __name__ == '__main__': 89 main() 90