import tarfile
import subprocess
from lxml import etree
+import xml.etree.ElementTree as ET
from common.buildtrigger import trigger_info, trigger_next
-def dump_result(outcome="TESTER_FAILURE", image="Not found"):
- """"Saves the results to a file, for injection"""
- 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."""
logging.info("Attempt retrieving flashable image from {0} ."\
OSError) as error:
logging.critical("Failure retrieving the image.")
logging.critical(error)
- dump_result("TESTER_FAILURE")
return None
def test_image(image):
"""Flashes the target device and runs the smoke test."""
try:
+ logging.info("Testing image {0}.".format(image))
command = ["/usr/bin/tztestrobot.run", image]
output = subprocess.check_output(command)
except OSError as error:
"Output: {0}".format(output))
return result
+
+def create_empty_results():
+ """
+ In case the testing is not possible (image not supported,
+ not booting, etc.) create empty dataset.
+ """
+ results_folder = "empty_results_dir"
+ try:
+ os.makedirs(results_folder)
+ with open(os.path.join(results_folder, "results.xml"), "w") as res:
+ res.write('<?xml version="1.0" encoding="utf-8"?>\n'
+ '<testsuite errors="0" failures="0" '
+ 'name="none" skips="0" tests="0" time="0">\n'
+ '</testsuite>\n')
+ except OSError:
+ logging.critical("Cannot create empty directory: " + results_folder)
+ return results_folder
+
+
def pack_results(results_folder):
"""Creates a tarball containing the test results"""
- if not results_folder:
- return
logging.info("Packing test results.")
try:
tar = tarfile.open("results.tar.gz", "w:gz")
return dic
+def image_supported(image):
+ """Check if the tester can test this specific image."""
+ try:
+ command = ["/usr/bin/tztestrobot.run", "--testable", image]
+ logging.info("Verifying testability of image {0}.".format(image))
+ subprocess.check_output(command)
+ except subprocess.CalledProcessError:
+ return False
+ return True
+
+
def main():
"""The main body"""
# Note: the script, by design, will not return errors
# Rather, it will inject the error status in the environment
# A Groovy post-build script will adjust the build status
+ logging.basicConfig(filename='tester.log', level=logging.DEBUG)
fields = trigger_info(os.getenv("TRIGGER_INFO"))
- url = fields["url"]
- image = fetch_image(url)
-
- if not image:
- return
-
- result = test_image(image)
- fields["test_result"] = result
- dump_result(result, image)
+# for key in fields.keys():
+# logging.info("Fields: {0} -> {1}".format(key, fields[key]))
+ file_name = ET.fromstring(fields["image_xml"])\
+ .find("storage").find("disk").get("file")
+ logging.info("Image to test: {0}".format(file_name))
+
+ if not image_supported(file_name):
+ logging.info("Image not supported by the tester.")
+ image = ""
+ fields["test_result"] = "IMAGE_NOT_SUPPORTED"
+ else:
+ image = fetch_image(fields["url"])
+ if image == "":
+ fields["test_result"] = "IMAGE_NOT_FOUND"
+ else:
+ fields["test_result"] = test_image(image)
- results_folder = glob.glob("tztestrobot-results.*")
+ with open("Results.ENV", "w") as results:
+ results.write("TEST_RESULT=" + fields["test_result"] + "\n")
+ results.write("IMAGE=" + image + "\n")
+ results_folder = glob.glob("tztestrobot_results.*")
if results_folder != []:
results_folder = results_folder[0]
- pack_results(results_folder)
- fields["test_details"] = \
- get_test_dict(results_folder + "/results.xml")
else:
- fields["test_details"] = {}
+ results_folder = create_empty_results()
+ pack_results(results_folder)
+ fields["test_details"] = get_test_dict(results_folder + "/results.xml")
trigger_next("RESULTS-PUBLISHING", fields)