1#!/usr/bin/env vpython3 2# Copyright 2020 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 6import json 7import os 8import shutil 9import sys 10import tempfile 11from textwrap import dedent 12import unittest 13 14# The following non-std imports are fetched via vpython. See the list at 15# //.vpython3 16import mock # pylint: disable=import-error 17from parameterized import parameterized # pylint: disable=import-error 18 19import test_runner 20 21_TAST_TEST_RESULTS_JSON = { 22 "name": "login.Chrome", 23 "errors": None, 24 "start": "2020-01-01T15:41:30.799228462-08:00", 25 "end": "2020-01-01T15:41:53.318914698-08:00", 26 "skipReason": "" 27} 28 29 30class TestRunnerTest(unittest.TestCase): 31 32 def setUp(self): 33 self._tmp_dir = tempfile.mkdtemp() 34 self.mock_rdb = mock.patch.object( 35 test_runner.result_sink, 'TryInitClient', return_value=None) 36 self.mock_rdb.start() 37 self.mock_env = mock.patch.dict( 38 os.environ, {'SWARMING_BOT_ID': 'cros-chrome-chromeos8-row29'}) 39 self.mock_env.start() 40 41 def tearDown(self): 42 shutil.rmtree(self._tmp_dir, ignore_errors=True) 43 self.mock_rdb.stop() 44 self.mock_env.stop() 45 46 def safeAssertItemsEqual(self, list1, list2): 47 """A Py3 safe version of assertItemsEqual. 48 49 See https://bugs.python.org/issue17866. 50 """ 51 self.assertSetEqual(set(list1), set(list2)) 52 53 54class TastTests(TestRunnerTest): 55 56 def get_common_tast_args(self, use_vm, fetch_cros_hostname): 57 return [ 58 'script_name', 59 'tast', 60 '--suite-name=chrome_all_tast_tests', 61 '--board=eve', 62 '--flash', 63 '--path-to-outdir=out_eve/Release', 64 '--logs-dir=%s' % self._tmp_dir, 65 '--use-vm' if use_vm else 66 ('--fetch-cros-hostname' 67 if fetch_cros_hostname else '--device=localhost:2222'), 68 ] 69 70 def get_common_tast_expectations(self, 71 use_vm, 72 fetch_cros_hostname, 73 is_lacros=False): 74 expectation = [ 75 test_runner.CROS_RUN_TEST_PATH, 76 '--board', 77 'eve', 78 '--cache-dir', 79 test_runner.DEFAULT_CROS_CACHE, 80 '--results-dest-dir', 81 '%s/system_logs' % self._tmp_dir, 82 '--flash', 83 '--build-dir', 84 'out_eve/Release', 85 '--results-dir', 86 self._tmp_dir, 87 '--tast-total-shards=1', 88 '--tast-shard-index=0', 89 ] 90 expectation.extend(['--start', '--copy-on-write'] if use_vm else ( 91 ['--device', 'chrome-chromeos8-row29'] 92 if fetch_cros_hostname else ['--device', 'localhost:2222'])) 93 for p in test_runner.SYSTEM_LOG_LOCATIONS: 94 expectation.extend(['--results-src', p]) 95 96 if not is_lacros: 97 expectation += [ 98 '--mount', 99 '--deploy', 100 '--nostrip', 101 ] 102 return expectation 103 104 def test_tast_gtest_filter(self): 105 """Tests running tast tests with a gtest-style filter.""" 106 with open(os.path.join(self._tmp_dir, 'streamed_results.jsonl'), 'w') as f: 107 json.dump(_TAST_TEST_RESULTS_JSON, f) 108 109 args = self.get_common_tast_args(False, False) + [ 110 '--attr-expr=( "group:mainline" && "dep:chrome" && !informational)', 111 '--gtest_filter=login.Chrome:ui.WindowControl', 112 ] 113 with mock.patch.object(sys, 'argv', args),\ 114 mock.patch.object(test_runner.subprocess, 'Popen') as mock_popen: 115 mock_popen.return_value.returncode = 0 116 117 test_runner.main() 118 # The gtest filter should cause the Tast expr to be replaced with a list 119 # of the tests in the filter. 120 expected_cmd = self.get_common_tast_expectations(False, False) + [ 121 '--tast=("name:login.Chrome" || "name:ui.WindowControl")' 122 ] 123 124 self.safeAssertItemsEqual(expected_cmd, mock_popen.call_args[0][0]) 125 126 @parameterized.expand([ 127 [True, False], 128 [False, True], 129 [False, False], 130 ]) 131 def test_tast_attr_expr(self, use_vm, fetch_cros_hostname): 132 """Tests running a tast tests specified by an attribute expression.""" 133 with open(os.path.join(self._tmp_dir, 'streamed_results.jsonl'), 'w') as f: 134 json.dump(_TAST_TEST_RESULTS_JSON, f) 135 136 args = self.get_common_tast_args(use_vm, fetch_cros_hostname) + [ 137 '--attr-expr=( "group:mainline" && "dep:chrome" && !informational)', 138 ] 139 with mock.patch.object(sys, 'argv', args),\ 140 mock.patch.object(test_runner.subprocess, 'Popen') as mock_popen: 141 mock_popen.return_value.returncode = 0 142 143 test_runner.main() 144 expected_cmd = self.get_common_tast_expectations( 145 use_vm, fetch_cros_hostname) + [ 146 '--tast=( "group:mainline" && "dep:chrome" && !informational)', 147 ] 148 149 self.safeAssertItemsEqual(expected_cmd, mock_popen.call_args[0][0]) 150 151 @parameterized.expand([ 152 [True, False], 153 [False, True], 154 [False, False], 155 ]) 156 def test_tast_lacros(self, use_vm, fetch_cros_hostname): 157 """Tests running a tast tests for Lacros.""" 158 with open(os.path.join(self._tmp_dir, 'streamed_results.jsonl'), 'w') as f: 159 json.dump(_TAST_TEST_RESULTS_JSON, f) 160 161 args = self.get_common_tast_args(use_vm, fetch_cros_hostname) + [ 162 '-t=lacros.Basic', 163 '--deploy-lacros', 164 ] 165 166 with mock.patch.object(sys, 'argv', args),\ 167 mock.patch.object(test_runner.subprocess, 'Popen') as mock_popen: 168 mock_popen.return_value.returncode = 0 169 170 test_runner.main() 171 expected_cmd = self.get_common_tast_expectations( 172 use_vm, fetch_cros_hostname, is_lacros=True) + [ 173 '--tast', 174 'lacros.Basic', 175 '--deploy-lacros', 176 '--lacros-launcher-script', 177 test_runner.LACROS_LAUNCHER_SCRIPT_PATH, 178 ] 179 180 self.safeAssertItemsEqual(expected_cmd, mock_popen.call_args[0][0]) 181 182 @parameterized.expand([ 183 [True, False], 184 [False, True], 185 [False, False], 186 ]) 187 def test_tast_with_vars(self, use_vm, fetch_cros_hostname): 188 """Tests running a tast tests with runtime variables.""" 189 with open(os.path.join(self._tmp_dir, 'streamed_results.jsonl'), 'w') as f: 190 json.dump(_TAST_TEST_RESULTS_JSON, f) 191 192 args = self.get_common_tast_args(use_vm, fetch_cros_hostname) + [ 193 '-t=login.Chrome', 194 '--tast-var=key=value', 195 ] 196 with mock.patch.object(sys, 'argv', args),\ 197 mock.patch.object(test_runner.subprocess, 'Popen') as mock_popen: 198 mock_popen.return_value.returncode = 0 199 test_runner.main() 200 expected_cmd = self.get_common_tast_expectations( 201 use_vm, fetch_cros_hostname) + [ 202 '--tast', 'login.Chrome', '--tast-var', 'key=value' 203 ] 204 205 self.safeAssertItemsEqual(expected_cmd, mock_popen.call_args[0][0]) 206 207 @parameterized.expand([ 208 [True, False], 209 [False, True], 210 [False, False], 211 ]) 212 def test_tast_retries(self, use_vm, fetch_cros_hostname): 213 """Tests running a tast tests with retries.""" 214 with open(os.path.join(self._tmp_dir, 'streamed_results.jsonl'), 'w') as f: 215 json.dump(_TAST_TEST_RESULTS_JSON, f) 216 217 args = self.get_common_tast_args(use_vm, fetch_cros_hostname) + [ 218 '-t=login.Chrome', 219 '--tast-retries=1', 220 ] 221 with mock.patch.object(sys, 'argv', args),\ 222 mock.patch.object(test_runner.subprocess, 'Popen') as mock_popen: 223 mock_popen.return_value.returncode = 0 224 test_runner.main() 225 expected_cmd = self.get_common_tast_expectations( 226 use_vm, 227 fetch_cros_hostname) + ['--tast', 'login.Chrome', '--tast-retries=1'] 228 229 self.safeAssertItemsEqual(expected_cmd, mock_popen.call_args[0][0]) 230 231 @parameterized.expand([ 232 [True, False], 233 [False, True], 234 [False, False], 235 ]) 236 def test_tast(self, use_vm, fetch_cros_hostname): 237 """Tests running a tast tests.""" 238 with open(os.path.join(self._tmp_dir, 'streamed_results.jsonl'), 'w') as f: 239 json.dump(_TAST_TEST_RESULTS_JSON, f) 240 241 args = self.get_common_tast_args(use_vm, fetch_cros_hostname) + [ 242 '-t=login.Chrome', 243 ] 244 with mock.patch.object(sys, 'argv', args),\ 245 mock.patch.object(test_runner.subprocess, 'Popen') as mock_popen: 246 mock_popen.return_value.returncode = 0 247 248 test_runner.main() 249 expected_cmd = self.get_common_tast_expectations( 250 use_vm, fetch_cros_hostname) + ['--tast', 'login.Chrome'] 251 252 self.safeAssertItemsEqual(expected_cmd, mock_popen.call_args[0][0]) 253 254 255class GTestTest(TestRunnerTest): 256 257 @parameterized.expand([ 258 [True, True, True, False], 259 [True, False, False, False], 260 [False, True, True, True], 261 [False, False, False, True], 262 [False, True, True, False], 263 [False, False, False, False], 264 ]) 265 def test_gtest(self, use_vm, stop_ui, use_test_sudo_helper, 266 fetch_cros_hostname): 267 """Tests running a gtest.""" 268 fd_mock = mock.mock_open() 269 270 args = [ 271 'script_name', 272 'gtest', 273 '--test-exe=out_eve/Release/base_unittests', 274 '--board=eve', 275 '--path-to-outdir=out_eve/Release', 276 '--use-vm' if use_vm else 277 ('--fetch-cros-hostname' 278 if fetch_cros_hostname else '--device=localhost:2222'), 279 ] 280 if stop_ui: 281 args.append('--stop-ui') 282 if use_test_sudo_helper: 283 args.append('--run-test-sudo-helper') 284 285 with mock.patch.object(sys, 'argv', args),\ 286 mock.patch.object(test_runner.subprocess, 'Popen') as mock_popen,\ 287 mock.patch.object(os, 'fdopen', fd_mock),\ 288 mock.patch.object(os, 'remove') as mock_remove,\ 289 mock.patch.object(tempfile, 'mkstemp', 290 return_value=(3, 'out_eve/Release/device_script.sh')),\ 291 mock.patch.object(os, 'fchmod'): 292 mock_popen.return_value.returncode = 0 293 294 test_runner.main() 295 self.assertEqual(1, mock_popen.call_count) 296 expected_cmd = [ 297 'vpython3', test_runner.CROS_RUN_TEST_PATH, '--board', 'eve', 298 '--cache-dir', test_runner.DEFAULT_CROS_CACHE, '--remote-cmd', 299 '--cwd', 'out_eve/Release', '--files', 300 'out_eve/Release/device_script.sh' 301 ] 302 if not stop_ui: 303 expected_cmd.append('--as-chronos') 304 expected_cmd.extend(['--start', '--copy-on-write'] if use_vm else ( 305 ['--device', 'chrome-chromeos8-row29'] 306 if fetch_cros_hostname else ['--device', 'localhost:2222'])) 307 expected_cmd.extend(['--', './device_script.sh']) 308 self.safeAssertItemsEqual(expected_cmd, mock_popen.call_args[0][0]) 309 310 expected_device_script = dedent("""\ 311 #!/bin/sh 312 export HOME=/usr/local/tmp 313 export TMPDIR=/usr/local/tmp 314 """) 315 316 core_cmd = 'LD_LIBRARY_PATH=./ ./out_eve/Release/base_unittests'\ 317 ' --test-launcher-shard-index=0 --test-launcher-total-shards=1' 318 319 if use_test_sudo_helper: 320 expected_device_script += dedent("""\ 321 TEST_SUDO_HELPER_PATH=$(mktemp) 322 ./test_sudo_helper.py --socket-path=${TEST_SUDO_HELPER_PATH} & 323 TEST_SUDO_HELPER_PID=$! 324 """) 325 core_cmd += ' --test-sudo-helper-socket-path=${TEST_SUDO_HELPER_PATH}' 326 327 if stop_ui: 328 dbus_cmd = 'dbus-send --system --type=method_call'\ 329 ' --dest=org.chromium.PowerManager'\ 330 ' /org/chromium/PowerManager'\ 331 ' org.chromium.PowerManager.HandleUserActivity int32:0' 332 expected_device_script += dedent("""\ 333 stop ui 334 {0} 335 chown -R chronos: ../.. 336 sudo -E -u chronos -- /bin/bash -c \"{1}\" 337 TEST_RETURN_CODE=$? 338 start ui 339 """).format(dbus_cmd, core_cmd) 340 else: 341 expected_device_script += dedent("""\ 342 {0} 343 TEST_RETURN_CODE=$? 344 """).format(core_cmd) 345 346 if use_test_sudo_helper: 347 expected_device_script += dedent("""\ 348 pkill -P $TEST_SUDO_HELPER_PID 349 kill $TEST_SUDO_HELPER_PID 350 unlink ${TEST_SUDO_HELPER_PATH} 351 """) 352 expected_device_script += dedent("""\ 353 exit $TEST_RETURN_CODE 354 """) 355 self.assertEqual(1, fd_mock().write.call_count) 356 # Split the strings to make failure messages easier to read. 357 self.assertListEqual( 358 expected_device_script.split('\n'), 359 fd_mock().write.call_args[0][0].split('\n')) 360 mock_remove.assert_called_once_with('out_eve/Release/device_script.sh') 361 362 def test_gtest_with_vpython(self): 363 """Tests building a gtest with --vpython-dir.""" 364 args = mock.MagicMock() 365 args.test_exe = 'base_unittests' 366 args.test_launcher_summary_output = None 367 args.trace_dir = None 368 args.runtime_deps_path = None 369 args.path_to_outdir = self._tmp_dir 370 args.vpython_dir = self._tmp_dir 371 args.logs_dir = self._tmp_dir 372 373 # With vpython_dir initially empty, the test_runner should error out 374 # due to missing vpython binaries. 375 gtest = test_runner.GTestTest(args, None) 376 with self.assertRaises(test_runner.TestFormatError): 377 gtest.build_test_command() 378 379 # Create the two expected tools, and the test should be ready to run. 380 with open(os.path.join(args.vpython_dir, 'vpython3'), 'w'): 381 pass # Just touch the file. 382 os.mkdir(os.path.join(args.vpython_dir, 'bin')) 383 with open(os.path.join(args.vpython_dir, 'bin', 'python3'), 'w'): 384 pass 385 gtest = test_runner.GTestTest(args, None) 386 gtest.build_test_command() 387 388 389class HostCmdTests(TestRunnerTest): 390 391 @parameterized.expand([ 392 [True, False, True], 393 [False, True, True], 394 [True, True, False], 395 [False, True, False], 396 ]) 397 def test_host_cmd(self, is_lacros, is_ash, strip_chrome): 398 args = [ 399 'script_name', 400 'host-cmd', 401 '--board=eve', 402 '--flash', 403 '--path-to-outdir=out/Release', 404 '--device=localhost:2222', 405 ] 406 if is_lacros: 407 args += ['--deploy-lacros'] 408 if is_ash: 409 args += ['--deploy-chrome'] 410 if strip_chrome: 411 args += ['--strip-chrome'] 412 args += [ 413 '--', 414 'fake_cmd', 415 ] 416 with mock.patch.object(sys, 'argv', args),\ 417 mock.patch.object(test_runner.subprocess, 'Popen') as mock_popen: 418 mock_popen.return_value.returncode = 0 419 420 test_runner.main() 421 expected_cmd = [ 422 test_runner.CROS_RUN_TEST_PATH, 423 '--board', 424 'eve', 425 '--cache-dir', 426 test_runner.DEFAULT_CROS_CACHE, 427 '--flash', 428 '--device', 429 'localhost:2222', 430 '--build-dir', 431 os.path.join(test_runner.CHROMIUM_SRC_PATH, 'out/Release'), 432 '--host-cmd', 433 ] 434 if is_lacros: 435 expected_cmd += [ 436 '--deploy-lacros', 437 '--lacros-launcher-script', 438 test_runner.LACROS_LAUNCHER_SCRIPT_PATH, 439 ] 440 if is_ash: 441 expected_cmd += ['--mount', '--deploy'] 442 if not strip_chrome: 443 expected_cmd += ['--nostrip'] 444 445 expected_cmd += [ 446 '--', 447 'fake_cmd', 448 ] 449 450 self.safeAssertItemsEqual(expected_cmd, mock_popen.call_args[0][0]) 451 452 453if __name__ == '__main__': 454 unittest.main() 455