| 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 |
| 39 |
| 40 class RunSlaveTest(unittest.TestCase): |
| 41 @mock.patch('subprocess.call') |
| 42 @mock.patch('os.execv', mock.Mock(side_effect=ExecvExecuted)) |
| 43 def test_run_slave_restart_after_gclient_sync(self, subprocess_call): |
| 44 """Tests if run_slave restarts itself after gclient sync.""" |
| 45 with self.assertRaises(ExecvExecuted): |
| 46 runpy.run_module("run_slave", run_name="__main__", alter_sys=True) |
| 47 |
| 48 # verify that gclient sync has been executed |
| 49 self.assertTrue(subprocess_call.called) |
| 50 call_cmd_args = _GetCallArgumentFromMock(subprocess_call, 0, 'args') |
| 51 self.assertEqual(call_cmd_args[0], run_slave.GetGClientPath()) |
| 52 self.assertEqual(call_cmd_args[1], 'sync') |
| 53 |
| 54 # verify that run_slave.py was execv()-ed with --no-gclient-sync |
| 55 run_slave_py_path = re.sub( |
| 56 r'pyc$', 'py', os.path.abspath(run_slave.__file__)) |
| 57 |
| 58 execv_cmd_args = _GetCallArgumentFromMock(os.execv, 1, 'args') |
| 59 self.assertIn(run_slave_py_path, execv_cmd_args[:2]) |
| 60 self.assertIn('--no-gclient-sync', execv_cmd_args[1:]) |
| 61 |
| 62 @mock.patch('subprocess.call') |
| 63 @mock.patch('subprocess.check_call') |
| 64 @mock.patch('subprocess.check_output') |
| 65 @mock.patch('twisted.scripts.twistd.run') |
| 66 @mock.patch('sys.argv', [run_slave.__file__, '--no-gclient-sync']) |
| 67 def test_run_slave_with_no_gclient_sync(self, twistd_run, |
| 68 subprocess_check_output, |
| 69 subprocess_check_call, |
| 70 subprocess_call): |
| 71 """Tests if twistd.run() gets invoked when --no-gclient-sync is given.""" |
| 72 os.environ['TESTING_MASTER'] = 'Master1' |
| 73 os.environ['TESTING_SLAVE'] = 'Slave1' |
| 74 runpy.run_module("run_slave", run_name="__main__", alter_sys=True) |
| 75 |
| 76 gclient_path = run_slave.GetGClientPath() |
| 77 for mock_call in (subprocess_call, subprocess_check_call, |
| 78 subprocess_check_output): |
| 79 if mock_call.called: |
| 80 cmd_args = _GetCallArgumentFromMock(mock_call, 0, 'args') |
| 81 |
| 82 # verify that gclient with sync option has not been called within |
| 83 # run_slave. |
| 84 if gclient_path == cmd_args[0]: |
| 85 self.assertNotEqual(cmd_args[1], 'sync') |
| 86 |
| 87 self.assertTrue(twistd_run.called) |
| 88 |
| 89 |
| 90 if __name__ == '__main__': |
| 91 unittest.main() |
| OLD | NEW |