tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_env_setup.py (2783B)


      1 #!/usr/bin/env vpython3
      2 
      3 # Copyright 2024 The Chromium Authors
      4 # Use of this source code is governed by a BSD-style license that can be
      5 # found in the LICENSE file.
      6 """Provides a way to setup the test environment without running the tests. It
      7   covers starting up daemon, starting up emulator or flashing physical device,
      8   setting up fuchsia package repository, publishing the packages and resolving
      9   the packages on the target.
     10 
     11   It shares most of the logic with the regular run_test.py, but overriding its
     12   _get_test_runner to wait_for_sigterm instead of running tests. So it's
     13   blocking until being killed.
     14 
     15   Since the setup_env does not run the tests, caller should use the pid file to
     16   detect when the environment is ready.
     17 
     18   Killing the process running the setup_env() function would tear down the
     19   test environment."""
     20 
     21 import argparse
     22 import os
     23 import sys
     24 
     25 from subprocess import CompletedProcess
     26 from typing import List
     27 
     28 import run_test
     29 from common import catch_sigterm, wait_for_sigterm
     30 from test_runner import TestRunner
     31 
     32 
     33 class _Blocker(TestRunner):
     34    """A TestRunner implementation to block the process until sigterm is
     35    received."""
     36 
     37    # private, use run_tests.get_test_runner function instead.
     38    def __init__(self, out_dir: str, target_id: str, package_deps: List[str],
     39                 pid_file: str):
     40        super().__init__(out_dir, [], [], target_id, package_deps)
     41        self.pid_file = pid_file
     42 
     43    def run_test(self) -> CompletedProcess:
     44        open(self.pid_file, 'w').close()
     45        try:
     46            wait_for_sigterm()
     47            return CompletedProcess(args='', returncode=0)
     48        finally:
     49            os.remove(self.pid_file)
     50 
     51 
     52 def setup_env(mypid: int = 0) -> int:
     53    """Sets up the environment and blocks until sigterm is received.
     54 
     55       Args:
     56           mypid: The script creates the file at logs-dir/$mypid.pid when the
     57                  environment is ready.
     58                  Since this script won't run tests directly, caller can use
     59                  this file to decide when to start running the tests."""
     60    catch_sigterm()
     61 
     62    if mypid == 0:
     63        mypid = os.getpid()
     64 
     65    # The 'setup-environment' is a place holder and has no specific meaning; the
     66    # run_test._get_test_runner is overridden.
     67    sys.argv.append('setup-environment')
     68 
     69    def get_test_runner(runner_args: argparse.Namespace, *_) -> TestRunner:
     70        return _Blocker(
     71            runner_args.out_dir, runner_args.target_id, runner_args.packages,
     72            os.path.join(runner_args.logs_dir,
     73                         'test_env_setup.' + str(mypid) + '.pid'))
     74 
     75    # pylint: disable=protected-access
     76    run_test._get_test_runner = get_test_runner
     77    return run_test.main()
     78 
     79 
     80 if __name__ == '__main__':
     81    sys.exit(setup_env())