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