Alexandre Rames | b78f139 | 2016-07-01 14:22:22 +0100 | [diff] [blame] | 1 | # Copyright 2015, VIXL authors |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 2 | # All rights reserved. |
| 3 | # |
| 4 | # Redistribution and use in source and binary forms, with or without |
| 5 | # modification, are permitted provided that the following conditions are met: |
| 6 | # |
| 7 | # * Redistributions of source code must retain the above copyright notice, |
| 8 | # this list of conditions and the following disclaimer. |
| 9 | # * Redistributions in binary form must reproduce the above copyright notice, |
| 10 | # this list of conditions and the following disclaimer in the documentation |
| 11 | # and/or other materials provided with the distribution. |
| 12 | # * Neither the name of ARM Limited nor the names of its contributors may be |
| 13 | # used to endorse or promote products derived from this software without |
| 14 | # specific prior written permission. |
| 15 | # |
| 16 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND |
| 17 | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE |
| 20 | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 21 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 22 | # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 23 | # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | |
| 27 | import multiprocessing |
| 28 | import re |
| 29 | import signal |
| 30 | import subprocess |
| 31 | import sys |
| 32 | import time |
| 33 | |
Anton Kirilov | 88a3376 | 2016-08-26 15:46:33 +0100 | [diff] [blame] | 34 | from known_test_failures import FilterKnownTestFailures |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 35 | import printer |
| 36 | import util |
| 37 | |
| 38 | # Catch SIGINT to gracefully exit when ctrl+C is pressed. |
| 39 | def SigIntHandler(signal, frame): |
| 40 | sys.exit(1) |
| 41 | |
| 42 | signal.signal(signal.SIGINT, SigIntHandler) |
| 43 | |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 44 | # Scan matching tests and return a test manifest. |
| 45 | def GetTests(runner, filters = []): |
| 46 | rc, output = util.getstatusoutput(runner + ' --list') |
| 47 | if rc != 0: util.abort('Failed to list all tests') |
| 48 | |
| 49 | tests = output.split() |
| 50 | for f in filters: |
| 51 | print f |
| 52 | tests = filter(re.compile(f).search, tests) |
| 53 | |
| 54 | return tests |
| 55 | |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 56 | def RunTest(test): |
Anthony Barbier | b5f7239 | 2019-02-15 15:33:48 +0000 | [diff] [blame^] | 57 | p = subprocess.Popen(test.command, |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 58 | stdout=subprocess.PIPE, |
| 59 | stderr=subprocess.STDOUT) |
| 60 | p_out, p_err = p.communicate() |
| 61 | rc = p.poll() |
| 62 | |
| 63 | if rc == 0: |
Anthony Barbier | b5f7239 | 2019-02-15 15:33:48 +0000 | [diff] [blame^] | 64 | with Test.n_tests_passed.get_lock(): Test.n_tests_passed.value += 1 |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 65 | else: |
Anthony Barbier | b5f7239 | 2019-02-15 15:33:48 +0000 | [diff] [blame^] | 66 | with Test.n_tests_failed.get_lock(): Test.n_tests_failed.value += 1 |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 67 | |
| 68 | printer.__print_lock__.acquire() |
| 69 | |
Anthony Barbier | b5f7239 | 2019-02-15 15:33:48 +0000 | [diff] [blame^] | 70 | printer.UpdateProgress(test.shared.start_time, |
| 71 | Test.n_tests_passed.value, |
| 72 | Test.n_tests_failed.value, |
| 73 | test.shared.n_tests, |
| 74 | test.name, |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 75 | prevent_next_overwrite = (rc != 0), |
| 76 | has_lock = True, |
Anthony Barbier | b5f7239 | 2019-02-15 15:33:48 +0000 | [diff] [blame^] | 77 | prefix = test.shared.progress_prefix) |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 78 | |
| 79 | if rc != 0: |
Anthony Barbier | b5f7239 | 2019-02-15 15:33:48 +0000 | [diff] [blame^] | 80 | printer.Print('FAILED: ' + test.name, has_lock = True) |
| 81 | printer.Print(printer.COLOUR_RED + ' '.join(test.command) + printer.NO_COLOUR, |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 82 | has_lock = True) |
| 83 | printer.Print(p_out, has_lock = True) |
| 84 | |
| 85 | printer.__print_lock__.release() |
| 86 | |
Anthony Barbier | b5f7239 | 2019-02-15 15:33:48 +0000 | [diff] [blame^] | 87 | class Test(object): |
| 88 | # Shared state for multiprocessing. Ideally the context should be passed with |
| 89 | # arguments, but constraints from the multiprocessing module prevent us from |
| 90 | # doing so: the shared variables (multiprocessing.Value) must be either global |
| 91 | # or static, or no work is started. |
| 92 | n_tests_passed = multiprocessing.Value('i', 0) |
| 93 | n_tests_failed = multiprocessing.Value('i', 0) |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 94 | |
Anthony Barbier | b5f7239 | 2019-02-15 15:33:48 +0000 | [diff] [blame^] | 95 | def __init__(self, test_runner, test, runtime_options, use_valgrind, shared): |
| 96 | self.command = [test_runner, test] + runtime_options |
| 97 | self.name = test |
| 98 | self.shared = shared |
| 99 | if use_valgrind: |
| 100 | self.command = ['valgrind'] + self.command |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 101 | |
Anthony Barbier | b5f7239 | 2019-02-15 15:33:48 +0000 | [diff] [blame^] | 102 | class TestQueue(object): |
| 103 | def __init__(self, under_valgrind = False, prefix = ''): |
| 104 | self.progress_prefix = prefix |
| 105 | self.under_valgrind = under_valgrind |
| 106 | self.queue = [] |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 107 | |
Anthony Barbier | b5f7239 | 2019-02-15 15:33:48 +0000 | [diff] [blame^] | 108 | def Add(self, test_runner_command, filters, runtime_options): |
| 109 | tests = GetTests(test_runner_command, filters) |
| 110 | tests = FilterKnownTestFailures(tests, under_valgrind = self.under_valgrind) |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 111 | |
Anthony Barbier | b5f7239 | 2019-02-15 15:33:48 +0000 | [diff] [blame^] | 112 | if len(tests) == 0: |
| 113 | printer.Print('No tests to run.') |
| 114 | return |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 115 | |
Anthony Barbier | b5f7239 | 2019-02-15 15:33:48 +0000 | [diff] [blame^] | 116 | for test in tests: |
| 117 | self.queue.append(Test(test_runner_command, test, |
| 118 | runtime_options, self.under_valgrind, self)) |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 119 | |
Anthony Barbier | b5f7239 | 2019-02-15 15:33:48 +0000 | [diff] [blame^] | 120 | # Run the specified tests. |
| 121 | # This function won't run in parallel due to constraints from the |
| 122 | # multiprocessing module. |
| 123 | __run_tests_lock__ = multiprocessing.Lock() |
| 124 | def Run(self, jobs): |
| 125 | with TestQueue.__run_tests_lock__: |
| 126 | # Initialisation. |
| 127 | self.start_time = time.time() |
| 128 | self.n_tests = len(self.queue) |
| 129 | if self.n_tests == 0: |
| 130 | printer.Print('No tests to run.') |
| 131 | return 0 |
| 132 | Test.n_tests_passed.value = 0 |
| 133 | Test.n_tests_failed.value = 0 |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 134 | |
Anthony Barbier | b5f7239 | 2019-02-15 15:33:48 +0000 | [diff] [blame^] | 135 | pool = multiprocessing.Pool(jobs) |
| 136 | # The '.get(9999999)' is a workaround to allow killing the test script with |
| 137 | # ctrl+C from the shell. This bug is documented at |
| 138 | # http://bugs.python.org/issue8296. |
| 139 | work = pool.map_async(RunTest, self.queue).get(9999999) |
| 140 | pool.close() |
| 141 | pool.join() |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 142 | |
Anthony Barbier | b5f7239 | 2019-02-15 15:33:48 +0000 | [diff] [blame^] | 143 | printer.UpdateProgress(self.start_time, |
| 144 | Test.n_tests_passed.value, |
| 145 | Test.n_tests_failed.value, |
| 146 | self.n_tests, |
| 147 | '== Done ==', |
| 148 | prevent_next_overwrite = True, |
| 149 | prefix = self.progress_prefix) |
| 150 | |
| 151 | # Empty the queue now that the tests have been run. |
| 152 | self.queue = [] |
| 153 | # `0` indicates success |
| 154 | return Test.n_tests_failed.value |
| 155 | |