aboutsummaryrefslogtreecommitdiff
path: root/automated/android/multinode/tradefed/utils.py
blob: c3b748f382ffa99dea4d64fba01b9ae6fc43f721 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import logging
import re
import shutil
import subprocess
import time


class Device:
    tcpip_device_re = re.compile(
        r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,5}$"
    )
    EXEC_IN_LAVA = shutil.which("lava-send") is not None

    def __init__(
        self, serial_or_address, logcat_output_filename, worker_job_id=None
    ):
        self.serial_or_address = serial_or_address
        self.is_tcpip_device = bool(
            Device.tcpip_device_re.match(self.serial_or_address)
        )
        self.logcat_output_file = open(logcat_output_filename, "w")
        self.logcat = subprocess.Popen(
            ["adb", "-s", serial_or_address, "logcat"],
            stdout=self.logcat_output_file,
        )
        self.worker_job_id = worker_job_id
        self.worker_handshake_iteration = 1
        self._is_available = True

    def ensure_available(self, logger, timeout_secs=30):
        """
        High level function that encapsulates all logic for ensuring that a device is accessible.
        Returns a boolean indicating if this function succeeded. This function will only return once
        the device is available or no other options for reestablishing a connection are known.

        Keyword arguments:
        tradefed_pexpect -- pexpect spawnu object that allows to communicate with TradeFed
        logger -- logging.getLogger() object to paste some debug information
        """
        if self.check_available(timeout_secs=timeout_secs):
            self._is_available = True
            logger.info("adb device %s is alive" % self.serial_or_address)
            # Tell the hosting worker that everything is fine
            self.worker_handshake("continue")
            return self._is_available

        self._is_available = False

        logger.debug(
            "adb connection to %s lost! Trying to reconnect..."
            % self.serial_or_address
        )

        # Tell the hosting worker that something is broken
        # This call will only return once the device is up and running again, if possible.
        self.worker_handshake("reconnect")

        if not self.try_reconnect():
            logger.warning(
                "adb connection to %s lost and reconnect failed!"
                % self.serial_or_address
            )
            return self._is_available

        logger.debug("Successfully reconnected to %s!" % self.serial_or_address)

        # TODO should check if TradeFed detected the device.

        self._is_available = True
        return self._is_available

    def is_available(self):
        """
        High level function that checks if the last ensure_available()
        invocation led to a positive result.
        """
        return self._is_available

    def check_available(self, timeout_secs=30):
        return (
            subprocess.run(
                [
                    "timeout",
                    str(timeout_secs),
                    "adb",
                    "-s",
                    self.serial_or_address,
                    "shell",
                    "echo",
                    "%s:" % self.serial_or_address,
                    "OK",
                ]
            ).returncode == 0
        )

    def try_reconnect(self, reconnectTimeoutSecs=60):
        # NOTE: When running inside LAVA, self.is_tcpip_device == (self.worker_job_id is not None).
        # However, when running this script directly, there is no such thing as a remote worker ID,
        # and reconnect attempts to remote devices may still be useful.
        if not self.is_tcpip_device:
            # On local devices, we can currently only try to recover from fastboot.
            # This would be a good point for a hard reset.
            # NOTE: If the boot/reboot process takes longer than the specified timeout, this
            # function will return failure, but the device can still become accessible in the next
            # iteration of device availability checks.
            fastbootRebootTimeoutSecs = (
                10
            )  # There is no point in waiting longer for fastboot
            subprocess.run(
                [
                    "timeout",
                    str(fastbootRebootTimeoutSecs),
                    "fastboot",
                    "-s",
                    self.serial_or_address,
                    "reboot",
                ]
            )
            bootTimeoutSecs = max(
                10, int(reconnectTimeoutSecs) - fastbootRebootTimeoutSecs
            )
            return (
                subprocess.run(
                    [
                        "sh",
                        "-c",
                        ". ../../../lib/sh-test-lib && . ../../../lib/android-test-lib && "
                        'export ANDROID_SERIAL="%s" && wait_boot_completed %s'
                        % (self.serial_or_address, bootTimeoutSecs),
                    ]
                ).returncode == 0
            )

        # adb may not yet have realized that the connection is broken
        subprocess.run(["adb", "disconnect", self.serial_or_address])
        time.sleep(
            5
        )  # adb connect ~often~ fails when called ~directly~ after disconnect.

        if (
            subprocess.run(
                [
                    "timeout",
                    str(reconnectTimeoutSecs),
                    "adb",
                    "connect",
                    self.serial_or_address,
                ]
            ).returncode != 0
        ):
            return False
        if not self.check_available():
            return False
        # reestablish logcat connection
        self.logcat.kill()
        self.logcat = subprocess.Popen(
            ["adb", "-s", self.serial_or_address, "logcat"],
            stdout=self.logcat_output_file,
        )
        return True

    def release(self):
        self.logcat.kill()
        self.logcat_output_file.close()
        self.worker_handshake("release")

    def worker_handshake(self, command):
        """
        This function implements the counterpart of wait-and-keep-local-device-accessible.yaml
        It is basically a no-op when running outside LAVA.

        """

        # Nothing to do for local devices and nothing to do when not called by LAVA.
        if self.worker_job_id is None or not Device.EXEC_IN_LAVA:
            self.worker_handshake_iteration += 1
            return True

        # All commands except release are followed by a lava-send from the worker side.
        wait_for_acc = command != "release"

        subprocess.run(
            [
                "lava-send",
                "master-sync-%s-%s"
                % (self.worker_job_id, str(self.worker_handshake_iteration)),
                "command=%s" % command,
            ]
        )
        if wait_for_acc:
            subprocess.run(
                [
                    "lava-wait",
                    "worker-sync-%s-%s"
                    % (
                        self.worker_job_id,
                        str(self.worker_handshake_iteration),
                    ),
                ]
            )
            # TODO could check result variable from MultiNode cache
        self.worker_handshake_iteration += 1
        return True


class RetryCheck:
    def __init__(self, total_max_retries, retries_if_unchanged):
        self.total_max_retries = total_max_retries
        self.retries_if_unchanged = retries_if_unchanged
        self.current_retry = 0
        self.current_unchanged = 0
        self.last_value = None

    def post_result(self, value):
        self.current_retry += 1
        if value == self.last_value:
            self.current_unchanged += 1
        else:
            self.current_unchanged = 1
            self.last_value = value

    def should_continue(self):
        return (
            self.current_retry < self.total_max_retries and self.current_unchanged < self.retries_if_unchanged
        )


class ResultSummary:
    def __init__(
        self, failure_count, modules_completed, modules_total, timestamp
    ):
        self.failure_count = int(failure_count)
        self.modules_completed = int(modules_completed)
        self.modules_total = int(modules_total)
        self.timestamp = timestamp

    def was_successful(self):
        return self.failure_count == 0 and self.all_modules_completed()

    def all_modules_completed(self):
        return self.modules_completed == self.modules_total

    def __eq__(self, other):
        if isinstance(self, other.__class__):
            return self.__dict__ == other.__dict__
        return NotImplemented