Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(231)

Side by Side Diff: slave/tests/run_slave_test.py

Issue 2398263003: Added unittests for run_slave (Closed)
Patch Set: reverted a change skipping other presubmit tests to shorten debugging time. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « PRESUBMIT.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 from mock import MagicMock, patch
dsansome 2016/10/10 05:25:18 The style guide says not to import functions and c
ddoman1 2016/10/12 00:42:23 Done.
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 print RUN_SLAVE_PATH
dsansome 2016/10/10 05:25:18 Remove this line
ddoman1 2016/10/12 00:42:23 Done.
20 sys.path.append(RUN_SLAVE_PATH)
21
22 import run_slave
23
24 # import twisted to mock run()
25 _, tw_ver = run_slave.GetThirdPartyVersions(None)
26 sys.path.append(os.path.join(RUN_SLAVE_PATH, 'third_party', tw_ver))
dsansome 2016/10/10 05:25:18 Other tests use scripts/common/env.py to do this i
dsansome 2016/10/12 03:54:04 Did you try this? Did it work?
27 import twisted.scripts.twistd
28
29
30 class ExecvExecuted(Exception):
31 # This exeception is raised within a mocked os.execv() so that
dsansome 2016/10/10 05:25:18 typo: s/exeception/exception/
ddoman1 2016/10/12 00:42:23 Done.
32 # the execution flow gets interrupted.
33 pass
34
35
36 class RunSlaveTest(unittest.TestCase):
37 def test_run_slave_restart_after_gclient_sync(self):
38 """Tests if run_slave restarts itself after gclient sync."""
39 def MockExecv(executable_path, argv):
dsansome 2016/10/10 05:25:18 Can you use mock.Mock instead? mock_execv = mock.
ddoman1 2016/10/12 00:42:23 Good point!
40 MockExecv.executable_path = executable_path
41 MockExecv.argv = argv
42 raise ExecvExecuted()
43
44 # test if ryn_slave.py restarts itself after gclient sync
dsansome 2016/10/10 05:25:18 typo: s/ryn/run/
ddoman1 2016/10/12 00:42:23 Done.
45 with patch('subprocess.call') as subprocess_call, \
46 patch('os.execv', MockExecv), \
47 self.assertRaises(ExecvExecuted):
48 runpy.run_module("run_slave", run_name="__main__", alter_sys=True)
dsansome 2016/10/10 05:25:18 Should be 2-space indentation here
ddoman1 2016/10/12 00:42:23 Done.
49
50 # verify that gclient sync has been executed
51 self.assertTrue(subprocess_call.called)
52 args, kwargs = subprocess_call.call_args
53 cmd_args = kwargs['args'] if 'args' in kwargs else args[0]
54 self.assertTrue(cmd_args[0] == run_slave.GetGClientPath() and
dsansome 2016/10/10 05:25:18 Avoid using assertTrue if possible. You'll get be
ddoman1 2016/10/12 00:42:23 Done.
55 cmd_args[1] == 'sync')
56
57 # verify that run_slave.py was execv()-ed with --no-gclient-sync
58 run_slave_py_path = re.sub(
59 r'pyc$', 'py', os.path.abspath(run_slave.__file__))
60
61 self.assertTrue(run_slave_py_path in MockExecv.argv[:2])
62 self.assertTrue('--no-gclient-sync' in MockExecv.argv)
63
64 def test_run_slave_with_no_gclient_sync(self):
65 """Tests if twistd.run() gets invoked when --no-gclient-sync is given."""
66 with patch('subprocess.call') as subprocess_call, \
dsansome 2016/10/10 05:25:18 This is a bit long, consider using mock.patch as a
ddoman1 2016/10/12 00:42:23 That looks much cleaner. Done
67 patch('subprocess.check_call') as subprocess_check_call, \
68 patch('subprocess.check_output') as subprocess_check_output, \
69 patch('twisted.scripts.twistd.run') as twistd_run, \
70 patch('sys.argv', [run_slave.__file__, '--no-gclient-sync']):
71 os.environ['TESTING_MASTER'] = 'Master1'
dsansome 2016/10/10 05:25:18 Also 2-space indentation
ddoman1 2016/10/12 00:42:23 Done.
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 in (subprocess_call, subprocess_check_call,
77 subprocess_check_output):
78 args, kwargs = mock.call_args if mock.called else ([["", ""]], {})
79 cmd_args = kwargs['args'] if 'args' in kwargs else args[0]
80 # check if gclient has not been called with sync option.
81 self.assertFalse(gclient_path == cmd_args[0] and 'sync' == args[1])
dsansome 2016/10/10 05:25:18 Use assertEqual
ddoman1 2016/10/12 00:42:23 Done.
82 self.assertTrue(twistd_run.called)
83
84
85 if __name__ == '__main__':
86 unittest.main()
OLDNEW
« no previous file with comments | « PRESUBMIT.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698