borenet | 2dbbfa5 | 2016-10-14 06:32:09 -0700 | [diff] [blame] | 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 | |
| 12 | import os |
| 13 | import subprocess |
| 14 | import sys |
| 15 | |
| 16 | |
| 17 | INFRA_BOTS_DIR = os.path.dirname(os.path.realpath(__file__)) |
| 18 | SKIA_DIR = os.path.abspath(os.path.join(INFRA_BOTS_DIR, os.pardir, os.pardir)) |
| 19 | |
| 20 | |
| 21 | def 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 | |
Eric Boren | 2ce36ea | 2017-04-13 10:22:11 -0400 | [diff] [blame] | 28 | def python_unit_tests(train): |
| 29 | if train: |
| 30 | return None |
borenet | 2dbbfa5 | 2016-10-14 06:32:09 -0700 | [diff] [blame] | 31 | return test( |
| 32 | ['python', '-m', 'unittest', 'discover', '-s', '.', '-p', '*_test.py'], |
| 33 | INFRA_BOTS_DIR) |
| 34 | |
| 35 | |
Eric Boren | a0481b9 | 2017-04-18 09:15:53 -0400 | [diff] [blame] | 36 | def recipe_test(train): |
Eric Boren | 2ce36ea | 2017-04-13 10:22:11 -0400 | [diff] [blame] | 37 | cmd = [ |
Eric Boren | 89cd357 | 2017-06-28 13:50:22 -0400 | [diff] [blame^] | 38 | 'python', os.path.join(INFRA_BOTS_DIR, 'recipes.py'), 'test'] |
Eric Boren | 2ce36ea | 2017-04-13 10:22:11 -0400 | [diff] [blame] | 39 | if train: |
Eric Boren | 89cd357 | 2017-06-28 13:50:22 -0400 | [diff] [blame^] | 40 | cmd.append('train') |
| 41 | else: |
| 42 | cmd.append('run') |
Eric Boren | 2ce36ea | 2017-04-13 10:22:11 -0400 | [diff] [blame] | 43 | return test(cmd, SKIA_DIR) |
borenet | 2dbbfa5 | 2016-10-14 06:32:09 -0700 | [diff] [blame] | 44 | |
| 45 | |
Eric Boren | 2ce36ea | 2017-04-13 10:22:11 -0400 | [diff] [blame] | 46 | def gen_tasks_test(train): |
| 47 | cmd = ['go', 'run', 'gen_tasks.go'] |
| 48 | if not train: |
| 49 | cmd.append('--test') |
borenet | 2dbbfa5 | 2016-10-14 06:32:09 -0700 | [diff] [blame] | 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 and 'cannot find package "go.skia.org/infra' in output: |
| 56 | return ('Failed to run gen_tests.go:\n\n%s\nMaybe you need to run:\n\n' |
| 57 | '$ go get -u go.skia.org/infra/...' % output) |
| 58 | return output |
| 59 | |
| 60 | |
| 61 | def main(): |
Eric Boren | 2ce36ea | 2017-04-13 10:22:11 -0400 | [diff] [blame] | 62 | train = False |
| 63 | if '--train' in sys.argv: |
| 64 | train = True |
| 65 | |
borenet | 2dbbfa5 | 2016-10-14 06:32:09 -0700 | [diff] [blame] | 66 | tests = ( |
| 67 | python_unit_tests, |
Eric Boren | a0481b9 | 2017-04-18 09:15:53 -0400 | [diff] [blame] | 68 | recipe_test, |
borenet | 2dbbfa5 | 2016-10-14 06:32:09 -0700 | [diff] [blame] | 69 | gen_tasks_test, |
| 70 | ) |
| 71 | errs = [] |
| 72 | for t in tests: |
Eric Boren | 2ce36ea | 2017-04-13 10:22:11 -0400 | [diff] [blame] | 73 | err = t(train) |
borenet | 2dbbfa5 | 2016-10-14 06:32:09 -0700 | [diff] [blame] | 74 | if err: |
| 75 | errs.append(err) |
| 76 | |
| 77 | if len(errs) > 0: |
| 78 | print >> sys.stderr, 'Test failures:\n' |
| 79 | for err in errs: |
| 80 | print >> sys.stderr, '==============================' |
| 81 | print >> sys.stderr, err |
| 82 | print >> sys.stderr, '==============================' |
| 83 | sys.exit(1) |
| 84 | |
Eric Boren | 2ce36ea | 2017-04-13 10:22:11 -0400 | [diff] [blame] | 85 | if train: |
| 86 | print 'Trained tests successfully.' |
| 87 | else: |
| 88 | print 'All tests passed!' |
borenet | 2dbbfa5 | 2016-10-14 06:32:09 -0700 | [diff] [blame] | 89 | |
| 90 | |
| 91 | if __name__ == '__main__': |
| 92 | main() |