|
| 1 | +from argparse import ArgumentParser |
| 2 | +import copy |
| 3 | +import os |
| 4 | +import re |
| 5 | +import signal |
| 6 | +import subprocess |
| 7 | +import sys |
| 8 | +import threading |
| 9 | + |
| 10 | + |
| 11 | +class TestApplicationBase(object): |
| 12 | + TEST_PASSED = 0 |
| 13 | + TEST_FAILED = 1 |
| 14 | + ARGUMENT_ERROR = 2 |
| 15 | + EXECUTION_ERROR = 3 |
| 16 | + NONZERO_EXIT = 4 |
| 17 | + |
| 18 | + DEFAULT_ROOT_DIR = os.path.abspath(os.path.normpath(os.path.join(os.path.dirname(__file__), '..'))) |
| 19 | + DEFAULT_COMMAND = ['%(path)s', '%(polaris_api_key)s', '%(unique_id)s'] |
| 20 | + |
| 21 | + def __init__(self, application_name, root_dir=None): |
| 22 | + if root_dir is None: |
| 23 | + self.root_dir = self.DEFAULT_ROOT_DIR |
| 24 | + else: |
| 25 | + self.root_dir = root_dir |
| 26 | + |
| 27 | + self.application_name = application_name |
| 28 | + |
| 29 | + # Define and parse arguments. |
| 30 | + self.parser = ArgumentParser(usage='%(prog)s [OPTIONS]...') |
| 31 | + |
| 32 | + self.parser.add_argument( |
| 33 | + '-p', '--path', metavar='PATH', |
| 34 | + help="The path to the application to be run.") |
| 35 | + self.parser.add_argument( |
| 36 | + '--polaris-api-key', metavar='KEY', |
| 37 | + help="The Polaris API key to be used. If not set, defaults to the POLARIS_API_KEY environment variable if " |
| 38 | + "specified.") |
| 39 | + self.parser.add_argument( |
| 40 | + '-t', '--timeout', metavar='SEC', type=float, default=30.0, |
| 41 | + help="The maximum test duration (in seconds).") |
| 42 | + self.parser.add_argument( |
| 43 | + '--tool', metavar='TOOL', default='bazel', |
| 44 | + help="The tool used to compile the application (bazel, cmake, make), used to determine the default " |
| 45 | + "application path. Ignored if --path is specified.") |
| 46 | + self.parser.add_argument( |
| 47 | + '--unique-id', metavar='ID', default=application_name, |
| 48 | + help="The unique ID to assign to this instance. The ID will be prepended with PREFIX if --unique-id-prefix " |
| 49 | + "is specified.") |
| 50 | + self.parser.add_argument( |
| 51 | + '--unique-id-prefix', metavar='PREFIX', |
| 52 | + help="An optional prefix to prepend to the unique ID.") |
| 53 | + |
| 54 | + self.options = None |
| 55 | + |
| 56 | + self.program_args = [] |
| 57 | + |
| 58 | + self.proc = None |
| 59 | + |
| 60 | + def parse_args(self): |
| 61 | + self.options = self.parser.parse_args() |
| 62 | + |
| 63 | + if self.options.polaris_api_key is None: |
| 64 | + self.options.polaris_api_key = os.getenv('POLARIS_API_KEY') |
| 65 | + if self.options.polaris_api_key is None: |
| 66 | + print('Error: Polaris API key not specified.') |
| 67 | + sys.exit(self.ARGUMENT_ERROR) |
| 68 | + |
| 69 | + if self.options.path is None: |
| 70 | + if self.options.tool == 'bazel': |
| 71 | + self.options.path = os.path.join(self.root_dir, 'bazel-bin/examples', self.application_name) |
| 72 | + elif self.options.tool == 'cmake': |
| 73 | + for build_dir in ('build', 'cmake_build'): |
| 74 | + path = os.path.join(self.root_dir, build_dir, 'examples', self.application_name) |
| 75 | + if os.path.exists(path): |
| 76 | + self.options.path = path |
| 77 | + break |
| 78 | + if self.options.path is None: |
| 79 | + print('Error: Unable to locate CMake build directory.') |
| 80 | + sys.exit(self.ARGUMENT_ERROR) |
| 81 | + elif self.options.tool == 'make': |
| 82 | + self.options.path = os.path.join(self.root_dir, 'examples', self.application_name) |
| 83 | + else: |
| 84 | + print('Error: Unsupported --tool value.') |
| 85 | + sys.exit(self.ARGUMENT_ERROR) |
| 86 | + |
| 87 | + if self.options.unique_id_prefix is not None: |
| 88 | + self.options.unique_id = self.options.unique_id_prefix + self.options.unique_id |
| 89 | + if len(self.options.unique_id) > 36: |
| 90 | + self.options.unique_id = self.options.unique_id[:36] |
| 91 | + print("Unique ID too long. Truncating to '%s'." % self.options.unique_id) |
| 92 | + |
| 93 | + return self.options |
| 94 | + |
| 95 | + def run(self, return_result=False): |
| 96 | + # Setup the command to be run. |
| 97 | + command = copy.deepcopy(self.DEFAULT_COMMAND) |
| 98 | + command.extend(self.program_args) |
| 99 | + api_key_standin = '%s...' % self.options.polaris_api_key[:4] |
| 100 | + for i in range(len(command)): |
| 101 | + if command[i].endswith('%(polaris_api_key)s'): |
| 102 | + # We temporarily replace the API key placeholder with the first 4 chars of the key before printing to |
| 103 | + # the console to avoid printing the actual key to the console. It will be swapped with the real key |
| 104 | + # below. |
| 105 | + command[i] = command[i].replace('%(polaris_api_key)s', api_key_standin) |
| 106 | + else: |
| 107 | + command[i] = command[i] % self.options.__dict__ |
| 108 | + |
| 109 | + print('Executing: %s' % ' '.join(command)) |
| 110 | + |
| 111 | + command.insert(0, 'stdbuf') |
| 112 | + command.insert(1, '-o0') |
| 113 | + for i in range(len(command)): |
| 114 | + if command[i].endswith(api_key_standin): |
| 115 | + command[i] = command[i].replace(api_key_standin, self.options.polaris_api_key) |
| 116 | + |
| 117 | + # Run the command. |
| 118 | + def ignore_signal(sig, frame): |
| 119 | + signal.signal(sig, signal.SIG_DFL) |
| 120 | + |
| 121 | + def preexec_function(): |
| 122 | + # Disable forwarding of SIGINT/SIGTERM from the parent process (this script) to the child process (the |
| 123 | + # application under test). |
| 124 | + os.setpgrp() |
| 125 | + signal.signal(signal.SIGINT, ignore_signal) |
| 126 | + signal.signal(signal.SIGTERM, ignore_signal) |
| 127 | + |
| 128 | + self.proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, encoding='utf-8', |
| 129 | + preexec_fn=preexec_function) |
| 130 | + |
| 131 | + # Capture SIGINT and SIGTERM and shutdown the application gracefully. |
| 132 | + def request_shutdown(sig, frame): |
| 133 | + self.stop() |
| 134 | + signal.signal(sig, signal.SIG_DFL) |
| 135 | + |
| 136 | + signal.signal(signal.SIGINT, request_shutdown) |
| 137 | + signal.signal(signal.SIGTERM, request_shutdown) |
| 138 | + |
| 139 | + # Stop the test after a max duration. |
| 140 | + def timeout_elapsed(): |
| 141 | + print('Maximum test duration (%.1f sec) elapsed.' % self.options.timeout) |
| 142 | + self.stop() |
| 143 | + |
| 144 | + watchdog = threading.Timer(self.options.timeout, timeout_elapsed) |
| 145 | + watchdog.start() |
| 146 | + |
| 147 | + # Check for a pass/fail condition and forward output to the console. |
| 148 | + while True: |
| 149 | + try: |
| 150 | + line = self.proc.stdout.readline().rstrip('\n') |
| 151 | + if line != '': |
| 152 | + print(line.rstrip('\n')) |
| 153 | + self.on_stdout(line) |
| 154 | + elif self.proc.poll() is not None: |
| 155 | + exit_code = self.proc.poll() |
| 156 | + break |
| 157 | + except KeyboardInterrupt: |
| 158 | + print('Execution interrupted unexpectedly.') |
| 159 | + if return_result: |
| 160 | + return self.EXECUTION_ERROR |
| 161 | + else: |
| 162 | + sys.exit(self.EXECUTION_ERROR) |
| 163 | + |
| 164 | + watchdog.cancel() |
| 165 | + self.proc = None |
| 166 | + |
| 167 | + result = self.check_pass_fail(exit_code) |
| 168 | + if result == self.TEST_PASSED: |
| 169 | + print('Test result: success') |
| 170 | + else: |
| 171 | + print('Test result: FAIL') |
| 172 | + |
| 173 | + if return_result: |
| 174 | + return result |
| 175 | + else: |
| 176 | + sys.exit(result) |
| 177 | + |
| 178 | + def stop(self): |
| 179 | + if self.proc is not None: |
| 180 | + print('Sending shutdown request to the application.') |
| 181 | + self.proc.terminate() |
| 182 | + |
| 183 | + def check_pass_fail(self, exit_code): |
| 184 | + if exit_code != 0: |
| 185 | + print('Application exited with non-zero exit code %s.' % repr(exit_code)) |
| 186 | + return self.NONZERO_EXIT |
| 187 | + else: |
| 188 | + return self.TEST_PASSED |
| 189 | + |
| 190 | + def on_stdout(self, line): |
| 191 | + pass |
| 192 | + |
| 193 | + |
| 194 | +class StandardApplication(TestApplicationBase): |
| 195 | + """! |
| 196 | + @brief Unit test for an example application that prints a data received |
| 197 | + message and requires no outside input. |
| 198 | +
|
| 199 | + The data received message must be formatted as: |
| 200 | + ``` |
| 201 | + Application received N bytes. |
| 202 | + ``` |
| 203 | + """ |
| 204 | + |
| 205 | + def __init__(self, application_name): |
| 206 | + super().__init__(application_name=application_name) |
| 207 | + self.data_received = False |
| 208 | + |
| 209 | + def check_pass_fail(self, exit_code): |
| 210 | + # Note: There is currently a race condition when the subprocess is shutdown (SIGTERM) where either the |
| 211 | + # application itself exits cleanly with code 0 as expected, or the Python fork running it exits first with |
| 212 | + # -SIGTERM before the application gets a chance to exit. The preexec stuff above doesn't seem to be enough to |
| 213 | + # fix it. For now, we simply treat the combination of -SIGTERM + data received as a pass. |
| 214 | + if exit_code == 0 or exit_code == -signal.SIGTERM: |
| 215 | + if self.data_received: |
| 216 | + return self.TEST_PASSED |
| 217 | + else: |
| 218 | + print('No corrections data received.') |
| 219 | + return self.TEST_FAILED |
| 220 | + else: |
| 221 | + return super().check_pass_fail(exit_code) |
| 222 | + |
| 223 | + def on_stdout(self, line): |
| 224 | + if re.match(r'.*Application received \d+ bytes.', line): |
| 225 | + print('Corrections data detected.') |
| 226 | + self.data_received = True |
| 227 | + self.stop() |
0 commit comments