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 if output: 56 if ('cannot find package "go.skia.org/infra' in output or 57 'gen_tasks.go:' in output): 58 return ('Failed to run gen_tests.go:\n\n%s\nMaybe you need to run:\n\n' 59 '$ go get -u go.skia.org/infra/...' % output) 60 return output 61 62 63def main(): 64 train = False 65 if '--train' in sys.argv: 66 train = True 67 68 tests = ( 69 python_unit_tests, 70 recipe_test, 71 gen_tasks_test, 72 ) 73 errs = [] 74 for t in tests: 75 err = t(train) 76 if err: 77 errs.append(err) 78 79 if len(errs) > 0: 80 print >> sys.stderr, 'Test failures:\n' 81 for err in errs: 82 print >> sys.stderr, '==============================' 83 print >> sys.stderr, err 84 print >> sys.stderr, '==============================' 85 sys.exit(1) 86 87 if train: 88 print 'Trained tests successfully.' 89 else: 90 print 'All tests passed!' 91 92 93if __name__ == '__main__': 94 main() 95