Skip to content

Commit b2611fb

Browse files
Qinghao ShiQinghao Shi
authored andcommitted
TEST: update python script to enable example smoke test
* it will check examples.json if contains 'test', 'compare_log', 'baud_rate' keys * it will dump test_spec.json test in examples compiled successfully
1 parent 46603f8 commit b2611fb

File tree

1 file changed

+55
-8
lines changed

1 file changed

+55
-8
lines changed

tools/test/examples/examples_lib.py

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
limitations
1717
"""
1818
import os
19-
from os.path import dirname, abspath, basename
19+
from os.path import dirname, abspath, basename, join, normpath
2020
import os.path
2121
import sys
2222
import subprocess
2323
from shutil import rmtree
24+
import json
2425

2526
""" Import and bulid a bunch of example programs
2627
@@ -369,6 +370,7 @@ def compile_repos(config, toolchains, targets, profile, verbose, examples):
369370
370371
"""
371372
results = {}
373+
test_json = {"builds":{}}
372374
valid_examples = set(examples)
373375
print("\nCompiling example repos....\n")
374376
for example in config['examples']:
@@ -380,31 +382,60 @@ def compile_repos(config, toolchains, targets, profile, verbose, examples):
380382
successes = []
381383
compiled = True
382384
pass_status = True
385+
if example.has_key('test') and example.has_key('baud_rate') and example.has_key('compare_log'):
386+
test_example = True
387+
else:
388+
test_example = False
383389
if example['compile']:
384390
for repo_info in get_repo_list(example):
385391
name = basename(repo_info['repo'])
386392
os.chdir(name)
387-
388393
# Check that the target, toolchain and features combinations are valid and return a
389394
# list of valid combinations to work through
390395
for target, toolchain in target_cross_toolchain(valid_choices(example['targets'], targets),
391396
valid_choices(example['toolchains'], toolchains),
392397
example['features']):
393-
print("Compiling %s for %s, %s" % (name, target, toolchain))
398+
394399
build_command = ["mbed-cli", "compile", "-t", toolchain, "-m", target] + (['-v'] if verbose else [])
395-
396400
if profile:
397401
build_command.append("--profile")
398402
build_command.append(profile)
399-
400-
proc = subprocess.Popen(build_command)
401-
402-
proc.wait()
403+
404+
print("Compiling [%s] for [%s] with toolchain [%s]\n\n> %s" % (name, target, toolchain, " ".join(build_command)))
405+
406+
proc = subprocess.Popen(build_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
407+
408+
std_out, std_err = proc.communicate()
409+
print ("\n#### STDOUT ####\n%s\n#### STDERR ####\n%s\n#### End of STDOUT/STDERR ####\n" % (std_out,std_err))
410+
411+
if test_example:
412+
log = example['compare_log'].pop(0)
413+
image = fetch_output_image(std_out)
414+
if image:
415+
image_info = [{"binary_type": "bootable","path": normpath(join(name,image)),"compare_log":log}]
416+
else:
417+
print ("Warning: could not found built image for example %s" % name)
418+
403419
example_summary = "{} {} {}".format(name, target, toolchain)
404420
if proc.returncode:
405421
failures.append(example_summary)
406422
else:
407423
successes.append(example_summary)
424+
if test_example:
425+
test_group = "{}-{}-{}".format(target, toolchain, example['baud_rate'])
426+
if example['test'] and image:
427+
if not test_json['builds'].has_key(test_group):
428+
test_json['builds'][test_group] = {
429+
"platform":target ,
430+
"toolchain": toolchain ,
431+
"base_path": os.getcwd() ,
432+
"baud_rate": int(example['baud_rate']),
433+
"tests":{} }
434+
test_json['builds'][test_group]['tests'][name]={"binaries":image_info}
435+
else:
436+
print("Warning: Test for %s will not be generated." % name)
437+
print("One or more of 'test' 'baud_rate' and 'compare_log' keys are missing from the json file\n")
438+
408439
os.chdir("..")
409440

410441
# If there are any compilation failures for the example 'set' then the overall status is fail.
@@ -415,6 +446,7 @@ def compile_repos(config, toolchains, targets, profile, verbose, examples):
415446

416447
results[example['name']] = [compiled, pass_status, successes, failures]
417448

449+
save_test_spec(test_json)
418450
return results
419451

420452

@@ -444,3 +476,18 @@ def update_mbedos_version(config, tag, examples):
444476

445477
return 0
446478

479+
def save_test_spec(json_spec, name="test_spec.json"):
480+
"""save the given json data to test_spec.json"""
481+
print ("Dumping json test_specs file {}".format(name))
482+
with open(name, 'w') as outfile:
483+
json.dump(json_spec, outfile , indent=4)
484+
485+
def fetch_output_image(output):
486+
"""find the mbed build image from thet last 5 lines of a given log """
487+
lines = output.splitlines()
488+
for index in range(-1,-6,-1):
489+
if lines[index].startswith("Image:"):
490+
image = lines[index][7:]
491+
if os.path.isfile(image):
492+
return image
493+
return False

0 commit comments

Comments
 (0)