Move image filtering to tester job.
authorIgor Stoppa <igor.stoppa@intel.com>
Tue, 4 Nov 2014 14:50:47 +0000 (16:50 +0200)
committerIgor Stoppa <igor.stoppa@intel.com>
Tue, 11 Nov 2014 08:33:04 +0000 (10:33 +0200)
To simplify the interaction between jobs, the tester job_will
be executed with all the images produced.

The tester job will perform the filtering, deciding which images to
download and test.

Empty data will be providedi for images that are not supported.

Change-Id: I554e51e9125dadc522e45351e14b70df4f757775

job_imager.py
job_test_build.py

index 6c3c0a5..f22cdb4 100755 (executable)
@@ -2,7 +2,6 @@
 
 """This script is used to create image"""
 
-import re
 import os
 import sys
 import shutil
@@ -184,17 +183,10 @@ def main():
 
     if status == 'success':
         print "The build was successful."
-        testable_images = re.compile(os.getenv("TESTABLE_IMAGES"))
-        if testable_images.match(fields["name"]):
-            print ("The target HW is supported "
-                   "and automated testing will follow.")
-            fields["image_xml"] = xml_string
-            fields["status"] = status
-            fields["url"] = url
-            trigger_next("IMAGE-TESTING", fields)
-        else:
-            print "However the target HW is NOT supported for automated testing."
-        return 0
+        fields["image_xml"] = xml_string
+        fields["status"] = status
+        fields["url"] = url
+        trigger_next("IMAGE-TESTING", fields)
     else:
         return -1
 
index 7dedbd5..dab7bd2 100755 (executable)
@@ -9,20 +9,10 @@ import logging
 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} ."\
@@ -38,13 +28,13 @@ def fetch_image(url):
             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:
@@ -61,10 +51,27 @@ def test_image(image):
                      "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")
@@ -94,32 +101,53 @@ def get_test_dict(xml_file):
     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)