import os
import sys
import glob
+import logging
import tarfile
import subprocess
from lxml import etree
def dump_result(outcome="TESTER_FAILURE", image="Not found"):
""""Saves the results to a file, for injection"""
- assert outcome in {"TESTER_FAILURE", "PASS", "FAIL"}
+ if outcome not in {"TESTER_FAILURE", "PASS", "FAIL"}:
+ logging.critical("Unexpected result '{0}' - forcing FAILURE."\
+ .format(outcome))
+ outcome = "TESTER_FAILURE"
with open("Results.ENV", "w") as results:
results.write("TEST_RESULT=" + outcome + "\n")
results.write("IMAGE=" + image + "\n")
def fetch_image(url):
"""Tries to download locally the image that will be tested."""
- print "Attempt retrieving flashable image from %s ..." % url,
+ logging.info("Attempt retrieving flashable image from {0} ."\
+ .format(url))
try:
command = ["/usr/bin/wget", "-r", "-l1", "-np", "-nd", "-q", "-A",
os.getenv("TESTABLE_IMAGE_DOWNLOADABLES"), url]
subprocess.check_output(command)
image = glob.glob(os.getenv("TESTABLE_IMAGE_FILTER"))[0]
- print "done."
+ logging.info("Image retrieved successfully.")
return image
except (IndexError, ValueError, subprocess.CalledProcessError,
OSError) as error:
- print "failed."
- print error
+ logging.critical("Failure retrieving the image.")
+ logging.critical(error)
dump_result("TESTER_FAILURE")
return None
output = subprocess.check_output(command)
except OSError as error:
result = "TESTER_FAILURE"
- print "Failed to flash the image."
- print error
+ logging.critical("Failed to flash the image.\n" +
+ "Error: {0}".format(error))
except (ValueError, subprocess.CalledProcessError) as error:
result = "FAIL"
- print "Either the device didn't boot or some test failed."
- print error
+ logging.error("Either the device didn't boot." +
+ "Error: {0}".format(error))
else:
- print output
result = "PASS"
- print "The device can boot and all the tests succeeded."
+ logging.info("The device can boot and all the tests were run." +
+ "Output: {0}".format(output))
return result
def pack_results(results_folder):
"""Creates a tarball containing the test results"""
if not results_folder:
return
- print "Packing test results ... ",
+ logging.info("Packing test results.")
try:
tar = tarfile.open("results.tar.gz", "w:gz")
tar.add(results_folder)
except (ValueError, OSError) as error:
- print "failed."
- print error
+ logging.critical("Failed packing test results." +
+ "Error: {0}".format(error))
else:
- print "done."
+ logging.info("Completed packing test results.")
def xml_tree_to_dict(tree):
"""Converts junit xml file to a dictionary"""