| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
import argparse, os, sys |
| 17 |
from time import sleep, time |
| 18 |
from random import randrange |
| 19 |
import math |
| 20 |
import argparse |
| 21 |
import subprocess |
| 22 |
import json |
| 23 |
import traceback |
| 24 |
|
| 25 |
parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 26 |
sys.path.insert(0, parentdir) |
| 27 |
|
| 28 |
from pyocd import __version__ |
| 29 |
from pyocd.core.helpers import ConnectHelper |
| 30 |
from pyocd.utility.conversion import float32_to_u32 |
| 31 |
from test_util import Test, TestResult |
| 32 |
import logging |
| 33 |
from random import randrange |
| 34 |
|
| 35 |
class JsonListsTestResult(TestResult): |
| 36 |
def __init__(self): |
| 37 |
super(JsonListsTestResult, self).__init__(None, None, None) |
| 38 |
self.name = "json_lists" |
| 39 |
|
| 40 |
class JsonListsTest(Test): |
| 41 |
def __init__(self): |
| 42 |
super(JsonListsTest, self).__init__("Json Lists Test", json_lists_test) |
| 43 |
|
| 44 |
def print_perf_info(self, result_list, output_file=None): |
| 45 |
pass |
| 46 |
|
| 47 |
def run(self, board): |
| 48 |
try: |
| 49 |
result = self.test_function(board.unique_id) |
| 50 |
except Exception as e: |
| 51 |
result = JsonListsTestResult() |
| 52 |
result.passed = False |
| 53 |
print("Exception %s when testing board %s" % (e, board.unique_id)) |
| 54 |
traceback.print_exc(file=sys.stdout) |
| 55 |
result.board = board |
| 56 |
result.test = self |
| 57 |
return result |
| 58 |
|
| 59 |
def json_lists_test(board_id, testing_standalone=False): |
| 60 |
|
| 61 |
test_count = 0 |
| 62 |
test_pass_count = 0 |
| 63 |
|
| 64 |
def validate_basic_keys(data, minor_version=0): |
| 65 |
did_pass = True |
| 66 |
|
| 67 |
print('pyocd_version', end=' ') |
| 68 |
p = 'pyocd_version' in data |
| 69 |
if p: |
| 70 |
p = data['pyocd_version'] == __version__ |
| 71 |
if p: |
| 72 |
print("PASSED") |
| 73 |
else: |
| 74 |
did_pass = False |
| 75 |
print("FAILED") |
| 76 |
|
| 77 |
print('version', end=' ') |
| 78 |
p = 'version' in data |
| 79 |
if p: |
| 80 |
v = data['version'] |
| 81 |
p = 'major' in v and 'minor' in v |
| 82 |
if p: |
| 83 |
p = v['major'] == 1 and v['minor'] == minor_version |
| 84 |
if p: |
| 85 |
print("PASSED") |
| 86 |
else: |
| 87 |
did_pass = False |
| 88 |
print("FAILED") |
| 89 |
|
| 90 |
print('status', end=' ') |
| 91 |
p = 'status' in data |
| 92 |
if p: |
| 93 |
p = data['status'] == 0 |
| 94 |
if p: |
| 95 |
print("PASSED") |
| 96 |
else: |
| 97 |
did_pass = False |
| 98 |
print("FAILED") |
| 99 |
|
| 100 |
return did_pass |
| 101 |
|
| 102 |
def validate_boards(data): |
| 103 |
did_pass = True |
| 104 |
|
| 105 |
print('boards', end=' ') |
| 106 |
p = 'boards' in data and type(data['boards']) is list |
| 107 |
if p: |
| 108 |
b = data['boards'] |
| 109 |
if p: |
| 110 |
print("PASSED") |
| 111 |
else: |
| 112 |
did_pass = False |
| 113 |
print("FAILED") |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
if testing_standalone: |
| 121 |
try: |
| 122 |
all_sessions = ConnectHelper.get_sessions_for_all_connected_probes(blocking=False) |
| 123 |
all_mbeds = [x.board for x in all_sessions] |
| 124 |
p = len(all_mbeds) == len(b) |
| 125 |
matching_boards = 0 |
| 126 |
if p: |
| 127 |
for mbed in all_mbeds: |
| 128 |
for brd in b: |
| 129 |
if mbed.unique_id == brd['unique_id']: |
| 130 |
matching_boards += 1 |
| 131 |
p = 'info' in brd and 'target' in brd and 'board_name' in brd |
| 132 |
if not p: |
| 133 |
break |
| 134 |
if not p: |
| 135 |
break |
| 136 |
p = matching_boards == len(all_mbeds) |
| 137 |
if p: |
| 138 |
print("PASSED") |
| 139 |
else: |
| 140 |
did_pass = False |
| 141 |
print("FAILED") |
| 142 |
except Exception as e: |
| 143 |
print("FAILED") |
| 144 |
traceback.print_exc(file=sys.stdout) |
| 145 |
did_pass = False |
| 146 |
else: |
| 147 |
|
| 148 |
p = True |
| 149 |
for brd in b: |
| 150 |
p = ('unique_id' in brd and |
| 151 |
'info' in brd and |
| 152 |
'target' in brd and |
| 153 |
'board_name' in brd) |
| 154 |
if not p: |
| 155 |
break |
| 156 |
if p: |
| 157 |
print("PASSED") |
| 158 |
else: |
| 159 |
did_pass = False |
| 160 |
print("FAILED") |
| 161 |
|
| 162 |
return did_pass |
| 163 |
|
| 164 |
def validate_targets(data): |
| 165 |
did_pass = True |
| 166 |
|
| 167 |
print('targets', end=' ') |
| 168 |
p = 'targets' in data and type(data['targets']) is list |
| 169 |
if p: |
| 170 |
targets = data['targets'] |
| 171 |
for t in targets: |
| 172 |
p = 'name' in t and 'part_number' in t |
| 173 |
if not p: |
| 174 |
break |
| 175 |
if p: |
| 176 |
print("PASSED") |
| 177 |
else: |
| 178 |
did_pass = False |
| 179 |
print("FAILED") |
| 180 |
|
| 181 |
return did_pass |
| 182 |
|
| 183 |
|
| 184 |
result = JsonListsTestResult() |
| 185 |
|
| 186 |
print("\n\n----- TESTING PROBES LIST -----") |
| 187 |
out = subprocess.check_output(['pyocd', 'json', '--probes']) |
| 188 |
data = json.loads(out) |
| 189 |
test_count += 2 |
| 190 |
if validate_basic_keys(data, minor_version=1): |
| 191 |
test_pass_count += 1 |
| 192 |
if validate_boards(data): |
| 193 |
test_pass_count += 1 |
| 194 |
|
| 195 |
print("\n\n----- TESTING TARGETS LIST -----") |
| 196 |
out = subprocess.check_output(['pyocd', 'json', '--targets']) |
| 197 |
data = json.loads(out) |
| 198 |
test_count += 2 |
| 199 |
if validate_basic_keys(data, minor_version=2): |
| 200 |
test_pass_count += 1 |
| 201 |
if validate_targets(data): |
| 202 |
test_pass_count += 1 |
| 203 |
|
| 204 |
|
| 205 |
print("\n\n----- TESTING BOARDS LIST -----") |
| 206 |
out = subprocess.check_output(['pyocd', 'json', '--boards']) |
| 207 |
data = json.loads(out) |
| 208 |
test_count += 1 |
| 209 |
if validate_basic_keys(data, minor_version=1): |
| 210 |
test_pass_count += 1 |
| 211 |
|
| 212 |
|
| 213 |
print("\n\n----- TESTING FEATURES LIST -----") |
| 214 |
out = subprocess.check_output(['pyocd', 'json', '--features']) |
| 215 |
data = json.loads(out) |
| 216 |
test_count += 1 |
| 217 |
if validate_basic_keys(data, minor_version=1): |
| 218 |
test_pass_count += 1 |
| 219 |
|
| 220 |
result.passed = test_count == test_pass_count |
| 221 |
return result |
| 222 |
|
| 223 |
if __name__ == "__main__": |
| 224 |
parser = argparse.ArgumentParser(description='pyocd json output test') |
| 225 |
parser.add_argument('-d', '--debug', action="store_true", help='Enable debug logging') |
| 226 |
args = parser.parse_args() |
| 227 |
level = logging.DEBUG if args.debug else logging.INFO |
| 228 |
logging.basicConfig(level=level) |
| 229 |
json_lists_test(None, testing_standalone=True) |