xref: /aosp_15_r20/external/angle/build/fuchsia/test/isolate_daemon.py (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1*8975f5c5SAndroid Build Coastguard Worker# Copyright 2023 The Chromium Authors
2*8975f5c5SAndroid Build Coastguard Worker# Use of this source code is governed by a BSD-style license that can be
3*8975f5c5SAndroid Build Coastguard Worker# found in the LICENSE file.
4*8975f5c5SAndroid Build Coastguard Worker"""Sets up the isolate daemon environment to run test on the bots."""
5*8975f5c5SAndroid Build Coastguard Worker
6*8975f5c5SAndroid Build Coastguard Workerimport os
7*8975f5c5SAndroid Build Coastguard Workerimport tempfile
8*8975f5c5SAndroid Build Coastguard Worker
9*8975f5c5SAndroid Build Coastguard Workerfrom typing import Optional
10*8975f5c5SAndroid Build Coastguard Worker
11*8975f5c5SAndroid Build Coastguard Workerfrom contextlib import AbstractContextManager
12*8975f5c5SAndroid Build Coastguard Worker
13*8975f5c5SAndroid Build Coastguard Workerfrom common import has_ffx_isolate_dir, set_ffx_isolate_dir, \
14*8975f5c5SAndroid Build Coastguard Worker                   is_daemon_running, start_ffx_daemon, stop_ffx_daemon
15*8975f5c5SAndroid Build Coastguard Workerfrom ffx_integration import ScopedFfxConfig
16*8975f5c5SAndroid Build Coastguard Workerfrom modification_waiter import ModificationWaiter
17*8975f5c5SAndroid Build Coastguard Worker
18*8975f5c5SAndroid Build Coastguard Worker
19*8975f5c5SAndroid Build Coastguard Workerclass IsolateDaemon(AbstractContextManager):
20*8975f5c5SAndroid Build Coastguard Worker    """Sets up the environment of an isolate ffx daemon."""
21*8975f5c5SAndroid Build Coastguard Worker
22*8975f5c5SAndroid Build Coastguard Worker    class IsolateDir(AbstractContextManager):
23*8975f5c5SAndroid Build Coastguard Worker        """Sets up the ffx isolate dir to a temporary folder if it's not set."""
24*8975f5c5SAndroid Build Coastguard Worker        def __init__(self):
25*8975f5c5SAndroid Build Coastguard Worker            if has_ffx_isolate_dir():
26*8975f5c5SAndroid Build Coastguard Worker                self._temp_dir = None
27*8975f5c5SAndroid Build Coastguard Worker            else:
28*8975f5c5SAndroid Build Coastguard Worker                self._temp_dir = tempfile.TemporaryDirectory()
29*8975f5c5SAndroid Build Coastguard Worker
30*8975f5c5SAndroid Build Coastguard Worker        def __enter__(self):
31*8975f5c5SAndroid Build Coastguard Worker            if self._temp_dir:
32*8975f5c5SAndroid Build Coastguard Worker                set_ffx_isolate_dir(self._temp_dir.__enter__())
33*8975f5c5SAndroid Build Coastguard Worker            return self
34*8975f5c5SAndroid Build Coastguard Worker
35*8975f5c5SAndroid Build Coastguard Worker        def __exit__(self, exc_type, exc_value, traceback):
36*8975f5c5SAndroid Build Coastguard Worker            if self._temp_dir:
37*8975f5c5SAndroid Build Coastguard Worker                try:
38*8975f5c5SAndroid Build Coastguard Worker                    self._temp_dir.__exit__(exc_type, exc_value, traceback)
39*8975f5c5SAndroid Build Coastguard Worker                except OSError:
40*8975f5c5SAndroid Build Coastguard Worker                    # Ignore the errors when cleaning up the temporary folder.
41*8975f5c5SAndroid Build Coastguard Worker                    pass
42*8975f5c5SAndroid Build Coastguard Worker            return False
43*8975f5c5SAndroid Build Coastguard Worker
44*8975f5c5SAndroid Build Coastguard Worker    def __init__(self, logs_dir: Optional[str]):
45*8975f5c5SAndroid Build Coastguard Worker        assert not has_ffx_isolate_dir() or not is_daemon_running()
46*8975f5c5SAndroid Build Coastguard Worker        self._inits = [
47*8975f5c5SAndroid Build Coastguard Worker            self.IsolateDir(),
48*8975f5c5SAndroid Build Coastguard Worker            ModificationWaiter(logs_dir),
49*8975f5c5SAndroid Build Coastguard Worker            # Keep the alphabetical order.
50*8975f5c5SAndroid Build Coastguard Worker            ScopedFfxConfig('ffx.isolated', 'true'),
51*8975f5c5SAndroid Build Coastguard Worker            ScopedFfxConfig('daemon.autostart', 'false'),
52*8975f5c5SAndroid Build Coastguard Worker            # fxb/126212: The timeout rate determines the timeout for each file
53*8975f5c5SAndroid Build Coastguard Worker            # transfer based on the size of the file / this rate (in MB).
54*8975f5c5SAndroid Build Coastguard Worker            # Decreasing the rate to 1 (from 5) increases the timeout in
55*8975f5c5SAndroid Build Coastguard Worker            # swarming, where large files can take longer to transfer.
56*8975f5c5SAndroid Build Coastguard Worker            ScopedFfxConfig('fastboot.flash.timeout_rate', '1'),
57*8975f5c5SAndroid Build Coastguard Worker            ScopedFfxConfig('fastboot.reboot.reconnect_timeout', '120'),
58*8975f5c5SAndroid Build Coastguard Worker            ScopedFfxConfig('fastboot.usb.disabled', 'true'),
59*8975f5c5SAndroid Build Coastguard Worker            ScopedFfxConfig('log.level', 'debug'),
60*8975f5c5SAndroid Build Coastguard Worker            ScopedFfxConfig('repository.server.listen', '"[::]:0"'),
61*8975f5c5SAndroid Build Coastguard Worker        ]
62*8975f5c5SAndroid Build Coastguard Worker        if logs_dir:
63*8975f5c5SAndroid Build Coastguard Worker            self._inits.append(ScopedFfxConfig('log.dir', logs_dir))
64*8975f5c5SAndroid Build Coastguard Worker
65*8975f5c5SAndroid Build Coastguard Worker    # Updating configurations to meet the requirement of isolate.
66*8975f5c5SAndroid Build Coastguard Worker    def __enter__(self):
67*8975f5c5SAndroid Build Coastguard Worker        # This environment variable needs to be set before stopping ffx daemon
68*8975f5c5SAndroid Build Coastguard Worker        # to avoid sending unnecessary analytics.
69*8975f5c5SAndroid Build Coastguard Worker        os.environ['FUCHSIA_ANALYTICS_DISABLED'] = '1'
70*8975f5c5SAndroid Build Coastguard Worker        stop_ffx_daemon()
71*8975f5c5SAndroid Build Coastguard Worker        for init in self._inits:
72*8975f5c5SAndroid Build Coastguard Worker            init.__enter__()
73*8975f5c5SAndroid Build Coastguard Worker        start_ffx_daemon()
74*8975f5c5SAndroid Build Coastguard Worker        return self
75*8975f5c5SAndroid Build Coastguard Worker
76*8975f5c5SAndroid Build Coastguard Worker    def __exit__(self, exc_type, exc_value, traceback):
77*8975f5c5SAndroid Build Coastguard Worker        for init in self._inits:
78*8975f5c5SAndroid Build Coastguard Worker            init.__exit__(exc_type, exc_value, traceback)
79*8975f5c5SAndroid Build Coastguard Worker        stop_ffx_daemon()
80