|
| 1 | +"""Utilities for handling STS-specific behavior in TradeFed. |
| 2 | +
|
| 3 | +The following behavior was noted at least in the 2018-09 version of STS for |
| 4 | +Android 7. When running STS, it manipulates the logged device fingerprint to |
| 5 | +show up as a 'user' build with 'release-keys', even when using the required |
| 6 | +setup with either 'userdebug' or 'eng' build. That behavior breaks the TradeFed |
| 7 | +rerun feature, as the fingerprint read from the device will not match the logged |
| 8 | +fingerprint of a previous run. |
| 9 | +
|
| 10 | +StsUtil works around this behavior by reverting the manipulated fingerprint in |
| 11 | +the log file to the string reported by the device. tradefed-runner-multinode.py |
| 12 | +uses this module to apply STS workarounds automatically when STS is run. |
| 13 | +""" |
| 14 | + |
| 15 | +import os |
| 16 | +import shutil |
| 17 | +import subprocess |
| 18 | +import xml.etree.ElementTree as ET |
| 19 | + |
| 20 | + |
| 21 | +class StsUtil: |
| 22 | + """Interface for STS related workarounds when automating TradeFed. |
| 23 | +
|
| 24 | + For applying StsUtil, use one instance per TradeFed STS invocation. Ideally, |
| 25 | + construct it before running any tests, so when the passed device is in a |
| 26 | + good known state. Call fix_result_file_fingerprints() after each completed |
| 27 | + run, before rerunning. |
| 28 | +
|
| 29 | + Applying StsUtil to non-STS TradeFed runs does not help, but should also not |
| 30 | + affect the results in any way. |
| 31 | + """ |
| 32 | + |
| 33 | + def __init__( |
| 34 | + self, device_serial_or_address, logger, device_access_timeout_secs=60 |
| 35 | + ): |
| 36 | + """Construct a StsUtil instance for a TradeFed invocation. |
| 37 | +
|
| 38 | + Args: |
| 39 | + device_serial_or_address (str): |
| 40 | + Serial number of network address if the device that will be used |
| 41 | + to determine the reference fingerprint. |
| 42 | + logger (logging.Logger) |
| 43 | + Logger instance to redirect messages to. |
| 44 | + device_access_timeout_secs (int): |
| 45 | + Timeout in seconds for `adb` calls. |
| 46 | + """ |
| 47 | + |
| 48 | + self.device_serial_or_address = device_serial_or_address |
| 49 | + self.logger = logger |
| 50 | + self.device_access_timeout_secs = device_access_timeout_secs |
| 51 | + # Try reading the device fingerprint now. There is a better chance that |
| 52 | + # the device is in a good state now than after a test run. If reading |
| 53 | + # fails here, however, we can still retry in |
| 54 | + # fix_result_file_fingerprints(). |
| 55 | + try: |
| 56 | + self.device_fingerprint = self.read_device_fingerprint() |
| 57 | + except subprocess.CalledProcessError: |
| 58 | + self.device_fingerprint = None |
| 59 | + |
| 60 | + def read_device_fingerprint(self): |
| 61 | + """Read the fingerprint of device_serial_or_address via adb. |
| 62 | +
|
| 63 | + Returns: |
| 64 | + str: |
| 65 | + Fingerprint of the device. |
| 66 | +
|
| 67 | + Raises: |
| 68 | + subprocess.CalledProcessError: |
| 69 | + If the communication with `adb` does not lead to |
| 70 | + expected results. |
| 71 | + """ |
| 72 | + |
| 73 | + fingerprint = subprocess.check_output( |
| 74 | + [ |
| 75 | + "adb", |
| 76 | + "-s", |
| 77 | + self.device_serial_or_address, |
| 78 | + "shell", |
| 79 | + "getprop", |
| 80 | + "ro.build.fingerprint", |
| 81 | + ], |
| 82 | + universal_newlines=True, |
| 83 | + timeout=self.device_access_timeout_secs, |
| 84 | + ).rstrip() |
| 85 | + |
| 86 | + self.logger.debug("Device reports fingerprint '%s'", fingerprint) |
| 87 | + |
| 88 | + return fingerprint |
| 89 | + |
| 90 | + def fix_result_file_fingerprints(self, result_dir): |
| 91 | + """Fix STS-manipulated device fingerprints in result files. |
| 92 | +
|
| 93 | + This will replace the fingerprint in the result files with the correct |
| 94 | + fingerprint as reported by the device. |
| 95 | +
|
| 96 | + Args: |
| 97 | + result_dir (str): |
| 98 | + Path to the result directory of the STS run to fix. This folder |
| 99 | + must contain a test_result.xml and test_result_failures.html, |
| 100 | + which are both present in a result folder of a completed |
| 101 | + TradeFed run. |
| 102 | +
|
| 103 | + Raises: |
| 104 | + subprocess.CalledProcessError: |
| 105 | + If the device fingerprint could not be determined via adb. |
| 106 | + """ |
| 107 | + |
| 108 | + if self.device_fingerprint is None: |
| 109 | + self.device_fingerprint = self.read_device_fingerprint() |
| 110 | + |
| 111 | + test_result_path = os.path.join(result_dir, "test_result.xml") |
| 112 | + test_result_path_orig = test_result_path + ".orig" |
| 113 | + shutil.move(test_result_path, test_result_path_orig) |
| 114 | + |
| 115 | + test_result_failures_path = os.path.join( |
| 116 | + result_dir, "test_result_failures.html" |
| 117 | + ) |
| 118 | + test_result_failures_path_orig = test_result_failures_path + ".orig" |
| 119 | + shutil.move(test_result_failures_path, test_result_failures_path_orig) |
| 120 | + |
| 121 | + # Find the manipulated fingerprint in the result XML. |
| 122 | + test_result_tree = ET.parse(test_result_path_orig) |
| 123 | + result_build_node = test_result_tree.getroot().find("Build") |
| 124 | + manipulated_fingerprint = result_build_node.get("build_fingerprint") |
| 125 | + |
| 126 | + self.logger.debug( |
| 127 | + "Reverting STS manipulated device fingerprint: '%s' -> '%s'", |
| 128 | + manipulated_fingerprint, |
| 129 | + self.device_fingerprint, |
| 130 | + ) |
| 131 | + |
| 132 | + # Fix the fingerprint in the result file. |
| 133 | + result_build_node.set("build_fingerprint", self.device_fingerprint) |
| 134 | + test_result_tree.write(test_result_path) |
| 135 | + |
| 136 | + # Fix the fingerprint in the failures overview HTML. |
| 137 | + with open( |
| 138 | + test_result_failures_path_orig, "r" |
| 139 | + ) as test_result_failures_file: |
| 140 | + test_result_failures = test_result_failures_file.read().replace( |
| 141 | + manipulated_fingerprint, self.device_fingerprint |
| 142 | + ) |
| 143 | + with open(test_result_failures_path, "w") as test_result_failures_file: |
| 144 | + test_result_failures_file.write(test_result_failures) |
0 commit comments