Skip to content

Commit 7d0cc69

Browse files
authored
Merge pull request #10521 from jamesbeyond/example_test
TEST: update python script to enable examples smoke test
2 parents 02eaad4 + 35eeef5 commit 7d0cc69

File tree

1 file changed

+60
-10
lines changed

1 file changed

+60
-10
lines changed

tools/test/examples/examples_lib.py

Lines changed: 60 additions & 10 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
@@ -37,6 +38,7 @@
3738
from tools.export import EXPORTERS
3839
from tools.project import EXPORTER_ALIASES
3940
from tools.toolchains import TOOLCHAINS
41+
from tools.utils import write_json_to_file
4042

4143
SUPPORTED_TOOLCHAINS = list(TOOLCHAINS - set(u'uARM'))
4244
SUPPORTED_IDES = [exp for exp in EXPORTERS.keys() + EXPORTER_ALIASES.keys()
@@ -369,7 +371,9 @@ def compile_repos(config, toolchains, targets, profile, verbose, examples):
369371
370372
"""
371373
results = {}
374+
test_json = {"builds":{}}
372375
valid_examples = set(examples)
376+
base_path = os.getcwd()
373377
print("\nCompiling example repos....\n")
374378
for example in config['examples']:
375379
example_names = [basename(x['repo']) for x in get_repo_list(example)]
@@ -380,31 +384,66 @@ def compile_repos(config, toolchains, targets, profile, verbose, examples):
380384
successes = []
381385
compiled = True
382386
pass_status = True
387+
if example.has_key('test') and example['test'] and example.has_key('baud_rate') and example.has_key('compare_log'):
388+
test_example = True
389+
else:
390+
test_example = False
383391
if example['compile']:
384392
for repo_info in get_repo_list(example):
385393
name = basename(repo_info['repo'])
386394
os.chdir(name)
387-
388395
# Check that the target, toolchain and features combinations are valid and return a
389396
# list of valid combinations to work through
390397
for target, toolchain in target_cross_toolchain(valid_choices(example['targets'], targets),
391398
valid_choices(example['toolchains'], toolchains),
392399
example['features']):
393-
print("Compiling %s for %s, %s" % (name, target, toolchain))
400+
394401
build_command = ["mbed-cli", "compile", "-t", toolchain, "-m", target] + (['-v'] if verbose else [])
395-
396402
if profile:
397403
build_command.append("--profile")
398404
build_command.append(profile)
399-
400-
proc = subprocess.Popen(build_command)
401-
402-
proc.wait()
405+
406+
print("Compiling [%s] for [%s] with toolchain [%s]\n\n> %s" % (name, target, toolchain, " ".join(build_command)))
407+
408+
proc = subprocess.Popen(build_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
409+
410+
std_out, std_err = proc.communicate()
411+
print ("\n#### STDOUT ####\n%s\n#### STDERR ####\n%s\n#### End of STDOUT/STDERR ####\n" % (std_out,std_err))
412+
413+
if test_example:
414+
log = example['compare_log'].pop(0)
415+
# example['compare_log'] is a list of log file/files, which matches each examples/sub-examples from same repo.
416+
# pop the log file out of list regardless the compilation for each example pass of fail
417+
image = fetch_output_image(std_out)
418+
if image:
419+
image_info = [{"binary_type": "bootable","path": normpath(join(name,image)),"compare_log":log}]
420+
else:
421+
print ("Warning: could not find built image for example %s" % name)
422+
403423
example_summary = "{} {} {}".format(name, target, toolchain)
404424
if proc.returncode:
405425
failures.append(example_summary)
406426
else:
407-
successes.append(example_summary)
427+
if test_example:
428+
test_group = "{}-{}-{}".format(target, toolchain, example['baud_rate'])
429+
if image:
430+
if not test_json['builds'].has_key(test_group):
431+
test_json['builds'][test_group] = {
432+
"platform":target ,
433+
"toolchain": toolchain ,
434+
"base_path": base_path ,
435+
"baud_rate": int(example['baud_rate']),
436+
"tests":{} }
437+
test_json['builds'][test_group]['tests'][name]={"binaries":image_info}
438+
test_status = "TEST_ON"
439+
else:
440+
test_status = "NO_IMAGE"
441+
else:
442+
print("Warning: Test for %s will not be generated." % name)
443+
print("One or more of 'test', 'baud_rate', and 'compare_log' keys are missing from the example config json file\n")
444+
test_status = "TEST_OFF"
445+
successes.append(example_summary + " " + test_status)
446+
408447
os.chdir("..")
409448

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

416455
results[example['name']] = [compiled, pass_status, successes, failures]
417456

457+
write_json_to_file(test_json, "test_spec.json")
418458
return results
419459

420460

@@ -443,4 +483,14 @@ def update_mbedos_version(config, tag, examples):
443483
return result
444484

445485
return 0
446-
486+
487+
def fetch_output_image(output):
488+
"""Find the build image from the last 5 lines of a given log"""
489+
lines = output.splitlines()
490+
last_index = -6 if len(lines)>4 else (-1 - len(lines))
491+
for index in range(-1,last_index,-1):
492+
if lines[index].startswith("Image:"):
493+
image = lines[index][7:]
494+
if os.path.isfile(image):
495+
return image
496+
return False

0 commit comments

Comments
 (0)