| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
import logging |
| 19 |
import os |
| 20 |
import sys |
| 21 |
import traceback |
| 22 |
from xml.etree import ElementTree |
| 23 |
import subprocess |
| 24 |
import tempfile |
| 25 |
import threading |
| 26 |
import codecs |
| 27 |
from pathlib import Path |
| 28 |
from pyocd.utility.compatibility import to_str_safe |
| 29 |
|
| 30 |
OBJCOPY = "arm-none-eabi-objcopy" |
| 31 |
|
| 32 |
TEST_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 33 |
PYOCD_DIR = os.path.dirname(TEST_DIR) |
| 34 |
TEST_DATA_DIR = os.path.join(TEST_DIR, "data") |
| 35 |
TEST_OUTPUT_DIR = os.path.join(TEST_DIR, "output") |
| 36 |
|
| 37 |
def get_test_binary_path(binary_name): |
| 38 |
if binary_name is None: |
| 39 |
binary_name = os.environ.get('PYOCD_TEST_BINARY') |
| 40 |
if binary_name is None: |
| 41 |
raise RuntimeError("no test binary available") |
| 42 |
if Path(binary_name).is_absolute(): |
| 43 |
return binary_name |
| 44 |
else: |
| 45 |
return os.path.join(TEST_DATA_DIR, "binaries", binary_name) |
| 46 |
|
| 47 |
def get_env_name(): |
| 48 |
return os.environ.get('TOX_ENV_NAME', '') |
| 49 |
|
| 50 |
def get_env_file_name(): |
| 51 |
env_name = get_env_name() |
| 52 |
return ("_" + env_name) if env_name else '' |
| 53 |
|
| 54 |
def ensure_output_dir(): |
| 55 |
if not os.path.isdir(TEST_OUTPUT_DIR): |
| 56 |
if os.path.exists(TEST_OUTPUT_DIR): |
| 57 |
raise RuntimeError("path '%s' already exists but is not a directory" % TEST_OUTPUT_DIR) |
| 58 |
os.mkdir(TEST_OUTPUT_DIR) |
| 59 |
|
| 60 |
|
| 61 |
def get_session_options(): |
| 62 |
return { |
| 63 |
|
| 64 |
'option_defaults': { |
| 65 |
'frequency': 1000000, |
| 66 |
'skip_test': False, |
| 67 |
}, |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
def get_target_test_params(session): |
| 76 |
target_type = session.board.target_type |
| 77 |
error_on_invalid_access = True |
| 78 |
if target_type.startswith("nrf"): |
| 79 |
|
| 80 |
test_clock = 1000000 |
| 81 |
error_on_invalid_access = False |
| 82 |
elif target_type == "ncs36510": |
| 83 |
|
| 84 |
test_clock = 1000000 |
| 85 |
else: |
| 86 |
|
| 87 |
test_clock = 4000000 |
| 88 |
return { |
| 89 |
'test_clock': test_clock, |
| 90 |
'error_on_invalid_access': error_on_invalid_access, |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
def binary_to_hex_file(binary_file, base_address): |
| 95 |
temp_test_hex_name = tempfile.mktemp('.hex') |
| 96 |
objcopyOutput = subprocess.check_output([OBJCOPY, |
| 97 |
"-v", "-I", "binary", "-O", "ihex", "-B", "arm", "-S", |
| 98 |
"--set-start", "0x%x" % base_address, |
| 99 |
"--change-addresses", "0x%x" % base_address, |
| 100 |
binary_file, temp_test_hex_name], stderr=subprocess.STDOUT) |
| 101 |
print(to_str_safe(objcopyOutput)) |
| 102 |
|
| 103 |
if sys.platform.startswith('win'): |
| 104 |
temp_test_hex_name = temp_test_hex_name.replace('\\', '\\\\') |
| 105 |
return temp_test_hex_name |
| 106 |
|
| 107 |
|
| 108 |
def binary_to_elf_file(binary_file, base_address): |
| 109 |
temp_test_elf_name = tempfile.mktemp('.elf') |
| 110 |
objcopyOutput = subprocess.check_output([OBJCOPY, |
| 111 |
"-v", "-I", "binary", "-O", "elf32-littlearm", "-B", "arm", "-S", |
| 112 |
"--set-start", "0x%x" % base_address, |
| 113 |
"--change-addresses", "0x%x" % base_address, |
| 114 |
binary_file, temp_test_elf_name], stderr=subprocess.STDOUT) |
| 115 |
print(to_str_safe(objcopyOutput)) |
| 116 |
|
| 117 |
if sys.platform.startswith('win'): |
| 118 |
temp_test_elf_name = temp_test_elf_name.replace('\\', '\\\\') |
| 119 |
return temp_test_elf_name |
| 120 |
|
| 121 |
def run_in_parallel(function, args_list): |
| 122 |
"""Create and run a thread in parallel for each element in args_list |
| 123 |
|
| 124 |
Wait until all threads finish executing. Throw an exception if an exception |
| 125 |
occurred on any of the threads. |
| 126 |
""" |
| 127 |
def _thread_helper(idx, func, args): |
| 128 |
"""Run the function and set result to True if there was not error""" |
| 129 |
func(*args) |
| 130 |
result_list[idx] = True |
| 131 |
|
| 132 |
result_list = [False] * len(args_list) |
| 133 |
thread_list = [] |
| 134 |
for idx, args in enumerate(args_list): |
| 135 |
thread = threading.Thread(target=_thread_helper, |
| 136 |
args=(idx, function, args)) |
| 137 |
thread.start() |
| 138 |
thread_list.append(thread) |
| 139 |
|
| 140 |
for thread in thread_list: |
| 141 |
thread.join() |
| 142 |
for result in result_list: |
| 143 |
if result is not True: |
| 144 |
raise Exception("Running in thread failed") |
| 145 |
|
| 146 |
def wait_with_deadline(process, timeout): |
| 147 |
try: |
| 148 |
from subprocess import TimeoutExpired |
| 149 |
try: |
| 150 |
process.wait(timeout=timeout) |
| 151 |
except TimeoutExpired as e: |
| 152 |
print('Timeout while waiting for process %s to exit: %s' % (process, e)) |
| 153 |
process.kill() |
| 154 |
return False |
| 155 |
except ImportError: |
| 156 |
|
| 157 |
|
| 158 |
process.wait() |
| 159 |
return True |
| 160 |
|
| 161 |
class IOTee(object): |
| 162 |
def __init__(self, *args): |
| 163 |
self.outputs = list(args) |
| 164 |
|
| 165 |
def add(self, output): |
| 166 |
self.outputs.append(output) |
| 167 |
|
| 168 |
def write(self, message): |
| 169 |
for out in self.outputs: |
| 170 |
encoding = out.encoding if getattr(out, 'encoding', None) else 'latin-1' |
| 171 |
|
| 172 |
try: |
| 173 |
|
| 174 |
|
| 175 |
b = codecs.encode(message, encoding=encoding, errors='backslashreplace') |
| 176 |
u = codecs.decode(b, encoding=encoding, errors='backslashreplace') |
| 177 |
out.write(u) |
| 178 |
except UnicodeEncodeError as err: |
| 179 |
out.write(f"<encode error: {err}>") |
| 180 |
|
| 181 |
def flush(self): |
| 182 |
for out in self.outputs: |
| 183 |
out.flush() |
| 184 |
|
| 185 |
def isatty(self): |
| 186 |
return False |
| 187 |
|
| 188 |
class RecordingLogHandler(logging.Handler): |
| 189 |
def __init__(self, iostream, level=logging.NOTSET): |
| 190 |
super(RecordingLogHandler, self).__init__(level) |
| 191 |
self.stream = iostream |
| 192 |
|
| 193 |
def emit(self, record): |
| 194 |
try: |
| 195 |
message = self.format(record) |
| 196 |
self.stream.write(message + "\n") |
| 197 |
except Exception: |
| 198 |
self.handleError(record) |
| 199 |
|
| 200 |
class TestResult(object): |
| 201 |
|
| 202 |
def __init__(self, test_board, test, result): |
| 203 |
self.passed = result |
| 204 |
self._board = test_board.target_type if test_board else 'unknown' |
| 205 |
self.board_name = test_board.name if test_board else "" |
| 206 |
self.test = test |
| 207 |
self.name = "test" |
| 208 |
self.time = 0 |
| 209 |
self.output = "" |
| 210 |
|
| 211 |
@property |
| 212 |
def board(self): |
| 213 |
return self._board |
| 214 |
|
| 215 |
@board.setter |
| 216 |
def board(self, newBoard): |
| 217 |
self._board = newBoard.target_type if newBoard else 'unknown' |
| 218 |
self.board_name = newBoard.name |
| 219 |
|
| 220 |
def get_test_case(self): |
| 221 |
if 'TOX_ENV_NAME' in os.environ: |
| 222 |
classname = "{}.{}.{}.{}".format(os.environ['TOX_ENV_NAME'], self.board_name, self.board, self.name) |
| 223 |
else: |
| 224 |
classname = "{}.{}.{}".format(self.board_name, self.board, self.name) |
| 225 |
case = ElementTree.Element('testcase', |
| 226 |
name=classname, |
| 227 |
classname=classname, |
| 228 |
status=("passed" if self.passed else "failed"), |
| 229 |
time="%.3f" % self.time |
| 230 |
) |
| 231 |
case.text = "\n" |
| 232 |
case.tail = "\n" |
| 233 |
if not self.passed: |
| 234 |
failed = ElementTree.SubElement(case, 'failure', |
| 235 |
message="failure", |
| 236 |
type="failure" |
| 237 |
) |
| 238 |
failed.text = self.filter_output(self.output) |
| 239 |
system_out = ElementTree.SubElement(case, 'system-out') |
| 240 |
system_out.text = self.filter_output(self.output) |
| 241 |
return case |
| 242 |
|
| 243 |
def filter_output(self, output): |
| 244 |
"""@brief Hex-encode null byte and control characters.""" |
| 245 |
result = str() |
| 246 |
for c in output: |
| 247 |
if (c not in ('\n', '\r', '\t')) and (0 <= ord(c) <= 31): |
| 248 |
result += "\\x{:02x}".format(ord(c)) |
| 249 |
else: |
| 250 |
result += c |
| 251 |
return result |
| 252 |
|
| 253 |
class Test(object): |
| 254 |
|
| 255 |
def __init__(self, name, function): |
| 256 |
self.name = name |
| 257 |
self.test_function = function |
| 258 |
self.n = 0 |
| 259 |
|
| 260 |
def run(self, board): |
| 261 |
""" |
| 262 |
Run test and return the result |
| 263 |
|
| 264 |
Override this function to return a custom result |
| 265 |
""" |
| 266 |
passed = False |
| 267 |
try: |
| 268 |
self.test_function(board.unique_id) |
| 269 |
passed = True |
| 270 |
except Exception as e: |
| 271 |
print("Exception %s when testing board %s" % (e, board.unique_id)) |
| 272 |
traceback.print_exc(file=sys.stdout) |
| 273 |
result = TestResult(board, self, passed) |
| 274 |
result.name = self.name |
| 275 |
return result |
| 276 |
|
| 277 |
def print_perf_info(self, result_list, output_file=None): |
| 278 |
""" |
| 279 |
Print performance info if any |
| 280 |
""" |
| 281 |
pass |
| 282 |
|
| 283 |
@staticmethod |
| 284 |
def print_results(result_list, output_file=None, ignored=[]): |
| 285 |
msg_format_str = "{:<15}{:<21}{:<15}{:<15}" |
| 286 |
print("\n\n------ TEST RESULTS ------") |
| 287 |
print(msg_format_str .format("Target", "Test", "Result", "Time"), |
| 288 |
file=output_file) |
| 289 |
print("", file=output_file) |
| 290 |
for result in result_list: |
| 291 |
status_str = "Pass" if result.passed else "Fail" |
| 292 |
if not result.passed and result.test.name in ignored: |
| 293 |
status_str += " [ignored]" |
| 294 |
print(msg_format_str.format(result.board, |
| 295 |
result.test.name, |
| 296 |
status_str, "%.3f" % result.time), |
| 297 |
file=output_file) |
| 298 |
|
| 299 |
@staticmethod |
| 300 |
def all_tests_pass(result_list, ignored=[]): |
| 301 |
passed = True |
| 302 |
for result in result_list: |
| 303 |
if not result.passed and result.test.name not in ignored: |
| 304 |
passed = False |
| 305 |
break |
| 306 |
if len(result_list) <= 0: |
| 307 |
passed = False |
| 308 |
return passed |