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