Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python | |
| 2 # Copyright 2016 The Chromium Authors. All rights reserved. | |
| 3 # Use of this source code is governed by a BSD-style license that can be | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 """Starts run_slave and verify if it starts successfully. | |
| 7 """ | |
| 8 | |
| 9 import mock | |
| 10 import os | |
| 11 import re | |
| 12 import runpy | |
| 13 import subprocess | |
| 14 import sys | |
| 15 import unittest | |
| 16 | |
| 17 | |
| 18 RUN_SLAVE_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..") | |
| 19 sys.path.append(RUN_SLAVE_PATH) | |
| 20 | |
| 21 import run_slave | |
| 22 | |
| 23 # import twisted to mock run() | |
| 24 _, tw_ver = run_slave.GetThirdPartyVersions(None) | |
| 25 sys.path.append(os.path.join(RUN_SLAVE_PATH, 'third_party', tw_ver)) | |
| 26 import twisted.scripts.twistd | |
| 27 | |
| 28 | |
| 29 class ExecvExecuted(Exception): | |
| 30 # This exception is raised within a mocked os.execv() so that | |
| 31 # the execution flow gets interrupted. | |
| 32 pass | |
| 33 | |
| 34 | |
| 35 def _GetCallArgumentFromMock(mock_call, position, keyword=None): | |
| 36 args, kwargs = mock_call.call_args | |
| 37 return kwargs[keyword] if keyword and keyword in kwargs else args[position] | |
| 38 | |
|
dsansome
2016/10/12 03:54:04
2 newlines between top-level definitions
ddoman1
2016/10/12 20:08:17
Done.
| |
| 39 class RunSlaveTest(unittest.TestCase): | |
| 40 @mock.patch('subprocess.call') | |
| 41 @mock.patch('os.execv', mock.Mock(side_effect=ExecvExecuted)) | |
| 42 def test_run_slave_restart_after_gclient_sync(self, subprocess_call): | |
| 43 """Tests if run_slave restarts itself after gclient sync.""" | |
| 44 with self.assertRaises(ExecvExecuted): | |
| 45 runpy.run_module("run_slave", run_name="__main__", alter_sys=True) | |
| 46 | |
| 47 # verify that gclient sync has been executed | |
| 48 self.assertTrue(subprocess_call.called) | |
| 49 call_cmd_args = _GetCallArgumentFromMock(subprocess_call, 0, 'args') | |
| 50 self.assertEqual(call_cmd_args[0], run_slave.GetGClientPath()) | |
| 51 self.assertEqual(call_cmd_args[1], 'sync') | |
| 52 | |
| 53 # verify that run_slave.py was execv()-ed with --no-gclient-sync | |
| 54 run_slave_py_path = re.sub( | |
| 55 r'pyc$', 'py', os.path.abspath(run_slave.__file__)) | |
| 56 | |
| 57 execv_cmd_args = _GetCallArgumentFromMock(os.execv, 1, 'args') | |
| 58 self.assertIn(run_slave_py_path, execv_cmd_args[:2]) | |
| 59 self.assertIn('--no-gclient-sync', execv_cmd_args[1:]) | |
| 60 | |
| 61 @mock.patch('subprocess.call') | |
| 62 @mock.patch('subprocess.check_call') | |
| 63 @mock.patch('subprocess.check_output') | |
| 64 @mock.patch('twisted.scripts.twistd.run') | |
| 65 @mock.patch('sys.argv', [run_slave.__file__, '--no-gclient-sync']) | |
| 66 def test_run_slave_with_no_gclient_sync(self, twistd_run, | |
| 67 subprocess_check_output, | |
| 68 subprocess_check_call, | |
| 69 subprocess_call): | |
| 70 """Tests if twistd.run() gets invoked when --no-gclient-sync is given.""" | |
| 71 os.environ['TESTING_MASTER'] = 'Master1' | |
| 72 os.environ['TESTING_SLAVE'] = 'Slave1' | |
| 73 runpy.run_module("run_slave", run_name="__main__", alter_sys=True) | |
| 74 | |
| 75 gclient_path = run_slave.GetGClientPath() | |
| 76 for mock_call in (subprocess_call, subprocess_check_call, | |
| 77 subprocess_check_output): | |
| 78 if mock_call.called: | |
| 79 cmd_args = _GetCallArgumentFromMock(mock_call, 0, 'args') | |
| 80 | |
| 81 # verify that gclient with sync option has not been called within | |
| 82 # run_slave. | |
| 83 if gclient_path == cmd_args[0]: | |
| 84 self.assertNotEqual(cmd_args[1], 'sync') | |
| 85 | |
| 86 self.assertTrue(twistd_run.called) | |
| 87 | |
| 88 | |
| 89 if __name__ == '__main__': | |
| 90 unittest.main() | |
| OLD | NEW |