1#!/usr/bin/env vpython3 2# Copyright 2024 The Chromium Authors 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5"""File for testing test_env_setup.py.""" 6 7import os 8import subprocess 9import time 10import unittest 11 12from compatible_utils import running_unattended 13 14# TODO(crbug.com/352409265): Try to run the following tests on trybot. 15 16 17# Test names should be self-explained, no point of adding function docstring. 18# pylint: disable=missing-function-docstring 19 20 21class TestEnvSetupTests(unittest.TestCase): 22 """Test class.""" 23 24 @staticmethod 25 def _merge_env(env: dict, env2: dict) -> dict: 26 if env and env2: 27 return {**env, **env2} 28 # Always copy to avoid changing os.environ. 29 if env: 30 return {**env} 31 if env2: 32 return {**env2} 33 return {} 34 35 def _start_test_env(self, env: dict = None) -> subprocess.Popen: 36 proc = subprocess.Popen([ 37 os.path.join(os.path.dirname(__file__), 'test_env_setup.py'), 38 '--logs-dir', '/tmp/test_env_setup' 39 ], 40 env=TestEnvSetupTests._merge_env( 41 os.environ, env)) 42 while not os.path.isfile('/tmp/test_env_setup/test_env_setup.' + 43 str(proc.pid) + '.pid'): 44 proc.poll() 45 self.assertIsNone(proc.returncode) 46 time.sleep(1) 47 return proc 48 49 def _run_without_packages(self, env: dict = None): 50 proc = self._start_test_env(env) 51 proc.terminate() 52 proc.wait() 53 54 def test_run_without_packages(self): 55 if running_unattended(): 56 # The test needs sdk and images to run and it's not designed to work 57 # on platforms other than linux. 58 return 59 self._run_without_packages() 60 61 def test_run_without_packages_unattended(self): 62 if running_unattended(): 63 return 64 # Do not impact the environment of the current process. 65 self._run_without_packages({'CHROME_HEADLESS': '1'}) 66 67 def _run_with_base_tests(self, env: dict = None): 68 env = TestEnvSetupTests._merge_env( 69 {'FFX_ISOLATE_DIR': '/tmp/test_env_setup/daemon'}, env) 70 proc = self._start_test_env(env) 71 try: 72 subprocess.run([ 73 os.path.join(os.path.dirname(__file__), 74 '../../out/fuchsia/bin/run_base_unittests'), 75 '--logs-dir', '/tmp/test_env_setup', '--device' 76 ], 77 env=TestEnvSetupTests._merge_env(os.environ, env), 78 check=True, 79 stdout=subprocess.DEVNULL, 80 stderr=subprocess.DEVNULL) 81 finally: 82 proc.terminate() 83 proc.wait() 84 85 def test_run_with_base_tests(self): 86 if running_unattended(): 87 return 88 self._run_with_base_tests() 89 90 def test_run_with_base_tests_unattended(self): 91 if running_unattended(): 92 return 93 self._run_with_base_tests({'CHROME_HEADLESS': '1'}) 94 95 96if __name__ == '__main__': 97 unittest.main() 98