Jenkins Build Testing script
authorIgor Stoppa <igor.stoppa@intel.com>
Wed, 7 May 2014 13:20:23 +0000 (16:20 +0300)
committerLin Yang <lin.a.yang@intel.com>
Thu, 5 Jun 2014 04:07:38 +0000 (12:07 +0800)
Downloads the image, flashes it to the appropriate target device
and runs the related smoke test.

Change-Id: I6b58a37608f970402d038f248e6f61f8051ee728

job_test_build.py [new file with mode: 0755]
packaging/jenkins-scripts.spec

diff --git a/job_test_build.py b/job_test_build.py
new file mode 100755 (executable)
index 0000000..13c921e
--- /dev/null
@@ -0,0 +1,67 @@
+#!/usr/bin/env python
+
+"""This script is used to test images on real target devices"""
+
+import os
+import sys
+import glob
+import subprocess
+from common.buildtrigger import trigger_info
+
+
+def dump_result(outcome="TESTER_FAILURE", image="Not found"):
+    """"Saves the results to a file, for injection"""
+    assert outcome in {"TESTER_FAILURE", "PASS", "FAIL"}
+    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."""
+    try:
+        command = ["/usr/bin/wget", "-r", "-l1", "-np", "-nd", "-A",
+            os.getenv("TESTABLE_IMAGE_DOWNLOADABLES"), url]
+        subprocess.check_output(command, stderr = subprocess.STDOUT)
+        image = glob.glob(os.getenv("TESTABLE_IMAGE_FILTER"))[0]
+        return image
+    except (IndexError, ValueError, subprocess.CalledProcessError, OSError):
+        dump_result("TESTER_FAILURE")
+        print "Failed to retrieve image."
+        return None
+
+
+def test_image(image):
+    """Flashes the target device and runs the smoke test."""
+    try:
+        command = ["/usr/bin/tztestrobot.run", image]
+        subprocess.check_output(command)
+    except OSError:
+        result = "TESTER_FAILURE"
+        print "Failed to flash the image and run the tests."
+    except (ValueError, subprocess.CalledProcessError):
+        result = "FAIL"
+        print "One or more tests failed."
+    else:
+        result = "PASS"
+        print "All the tests succeeded."
+    dump_result(result, image)
+    return result
+
+def main():
+    """The main body"""
+    # Note: the script, by design, will not return errors
+    # Rather, it will inject the error status in the environment
+
+    content = trigger_info(os.getenv("TRIGGER_INFO"))
+    url = content.get("url", "")
+    image = fetch_image(url)
+
+    if not image:
+        return
+
+    dump_result(test_image(image), image)
+
+
+if __name__ == "__main__":
+    sys.exit(main())
index 2b6ef2f..331c714 100644 (file)
@@ -93,6 +93,7 @@ fi
 %{destdir}/job_policycheck.py
 %{destdir}/job_pre_release_obs.py
 %{destdir}/job_request.py
+%{destdir}/job_test_build.py
 %{destdir}/requests
 %{destdir}/common/tempbuildpkg.py