tradefed_test: metric gathering should not raise

Metric gathering will fail when the test is skipped. This CL fix it.

BUG=chromium:1059313
TEST=Run `PYTHONPATH=$AUTOTEST/venv python2 \
$AUTOTEST/server/cros/tradefed/tradefed_utils_unittest.py`

Change-Id: Iabfe95743b0c364f8c8af9bfc4ce0425439cf3e9
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/autotest/+/2104730
Tested-by: Boleyn Su <boleynsu@chromium.org>
Auto-Submit: Boleyn Su <boleynsu@chromium.org>
Commit-Queue: George Engelbrecht <engeg@google.com>
Reviewed-by: Kazuhiro Inaba <kinaba@chromium.org>
(cherry picked from commit 66ea1276b0b4682d4d3d7e80e85fc7c51f0454b2)
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/autotest/+/2113398
Commit-Queue: Kazuhiro Inaba <kinaba@chromium.org>
Tested-by: Kazuhiro Inaba <kinaba@chromium.org>
Auto-Submit: Kazuhiro Inaba <kinaba@chromium.org>
diff --git a/server/cros/tradefed/tradefed_utils.py b/server/cros/tradefed/tradefed_utils.py
index f435418..72c95cb 100644
--- a/server/cros/tradefed/tradefed_utils.py
+++ b/server/cros/tradefed/tradefed_utils.py
@@ -193,35 +193,48 @@
             os.environ['PATH'] = (
                 os.path.join(java, 'bin') + os.pathsep + os.environ['PATH'])
 
+
 # A similar implementation in Java can be found at
 # https://android.googlesource.com/platform/test/suite_harness/+/refs/heads/\
 # pie-cts-dev/common/util/src/com/android/compatibility/common/util/\
 # ResultHandler.java
 def get_test_result_xml_path(results_destination):
-    """Get the path of test_result.xml from the last session."""
-    last_result_path = None
-    for dir in os.listdir(results_destination):
-        result_dir = os.path.join(results_destination, dir)
-        result_path = os.path.join(result_dir, 'test_result.xml')
-        # We use the lexicographically largest path, because |dir| are
-        # of format YYYY.MM.DD_HH.MM.SS. The last session will always
-        # have the latest date which leads to the lexicographically
-        # largest path.
-        if last_result_path and last_result_path > result_path:
-            continue
-        # We need to check for `islink` as `isdir` returns true if |result_dir|
-        # is a symbolic link to a directory.
-        if not os.path.isdir(result_dir) or os.path.islink(result_dir):
-            continue
-        if not os.path.exists(result_path):
-            continue
-        last_result_path = result_path
-    return last_result_path
+    """Get the path of test_result.xml from the last session.
+    Raises:
+        Should never raise!
+    """
+    try:
+        last_result_path = None
+        for dir in os.listdir(results_destination):
+            result_dir = os.path.join(results_destination, dir)
+            result_path = os.path.join(result_dir, 'test_result.xml')
+            # We use the lexicographically largest path, because |dir| are
+            # of format YYYY.MM.DD_HH.MM.SS. The last session will always
+            # have the latest date which leads to the lexicographically
+            # largest path.
+            if last_result_path and last_result_path > result_path:
+                continue
+            # We need to check for `islink` as `isdir` returns true if
+            # |result_dir| is a symbolic link to a directory.
+            if not os.path.isdir(result_dir) or os.path.islink(result_dir):
+                continue
+            if not os.path.exists(result_path):
+                continue
+            last_result_path = result_path
+        return last_result_path
+    except Exception as e:
+        logging.warning(
+            'Exception raised in '
+            '|tradefed_utils.get_test_result_xml_path|: {'
+            '0}'.format(e))
 
 
 def get_perf_metrics_from_test_result_xml(result_path, resultsdir):
     """Parse test_result.xml and each <Metric /> is mapped to a dict that
-    can be used as kwargs of |TradefedTest.output_perf_value|."""
+    can be used as kwargs of |TradefedTest.output_perf_value|.
+    Raises:
+        Should never raise!
+    """
     try:
         root = ElementTree.parse(result_path)
         for module in root.iter('Module'):
diff --git a/server/cros/tradefed/tradefed_utils_unittest.py b/server/cros/tradefed/tradefed_utils_unittest.py
index 5c44d2a..e7967fd 100644
--- a/server/cros/tradefed/tradefed_utils_unittest.py
+++ b/server/cros/tradefed/tradefed_utils_unittest.py
@@ -141,6 +141,11 @@
             'tradefed_utils_unittest_data', 'results', '2019.11.07_10.14.55',
             'test_result.xml'))
 
+        # assertNoRaises
+        tradefed_utils.get_test_result_xml_path(os.path.join(
+            os.path.dirname(os.path.realpath(__file__)),
+            'tradefed_utils_unittest_data', 'not_exist'))
+
     def test_get_perf_metrics_from_test_result_xml(self):
         perf_result = tradefed_utils.get_perf_metrics_from_test_result_xml(
             os.path.join(os.path.dirname(os.path.realpath(__file__)),
@@ -357,6 +362,14 @@
             os.path.join('/', 'resultsdir'))
         self.assertListEqual(list(perf_result), [])
 
+        # assertNoRaises
+        tradefed_utils.get_perf_metrics_from_test_result_xml(
+            os.path.join(os.path.dirname(os.path.realpath(__file__)),
+                         'tradefed_utils_unittest_data',
+                         'not_exist'),
+            os.path.join('/', 'resultsdir'))
+
+
 
 if __name__ == '__main__':
     unittest.main()