blob: a44e7294b9081191fc819867b716a50e0f4d04c8 [file] [log] [blame]
showarde39ebe92009-06-18 23:14:48 +00001#!/usr/bin/python
2
showard42d44982009-10-12 20:34:03 +00003import os, unittest
showarde39ebe92009-06-18 23:14:48 +00004import common
showardac5b0002009-10-19 18:34:00 +00005from autotest_lib.client.common_lib import global_config
showard42d44982009-10-12 20:34:03 +00006from autotest_lib.client.common_lib.test_utils import mock
showard2aafd902009-10-14 16:20:14 +00007from autotest_lib.scheduler import drone_manager, drone_utility, drones
showardac5b0002009-10-19 18:34:00 +00008from autotest_lib.scheduler import scheduler_config
showarde39ebe92009-06-18 23:14:48 +00009
10class MockDrone(drones._AbstractDrone):
showard9bb960b2009-11-19 01:02:11 +000011 def __init__(self, name, active_processes=0, max_processes=10,
12 allowed_users=None):
showarde39ebe92009-06-18 23:14:48 +000013 super(MockDrone, self).__init__()
14 self.name = name
15 self.active_processes = active_processes
16 self.max_processes = max_processes
showard9bb960b2009-11-19 01:02:11 +000017 self.allowed_users = allowed_users
showard42d44982009-10-12 20:34:03 +000018 # maps method names list of tuples containing method arguments
19 self._recorded_calls = {'queue_call': [],
20 'send_file_to': []}
21
22
23 def queue_call(self, method, *args, **kwargs):
24 self._recorded_calls['queue_call'].append((method, args, kwargs))
25
26
showard2aafd902009-10-14 16:20:14 +000027 def call(self, method, *args, **kwargs):
28 # don't bother differentiating between call() and queue_call()
29 return self.queue_call(method, *args, **kwargs)
30
31
showard42d44982009-10-12 20:34:03 +000032 def send_file_to(self, drone, source_path, destination_path,
33 can_fail=False):
34 self._recorded_calls['send_file_to'].append(
35 (drone, source_path, destination_path))
36
37
38 # method for use by tests
39 def _check_for_recorded_call(self, method_name, arguments):
40 recorded_arg_list = self._recorded_calls[method_name]
41 was_called = arguments in recorded_arg_list
42 if not was_called:
43 print 'Recorded args:', recorded_arg_list
44 print 'Expected:', arguments
45 return was_called
46
47
48 def was_call_queued(self, method, *args, **kwargs):
49 return self._check_for_recorded_call('queue_call',
50 (method, args, kwargs))
51
52
53 def was_file_sent(self, drone, source_path, destination_path):
54 return self._check_for_recorded_call('send_file_to',
55 (drone, source_path,
56 destination_path))
showarde39ebe92009-06-18 23:14:48 +000057
58
59class DroneManager(unittest.TestCase):
showard42d44982009-10-12 20:34:03 +000060 _DRONE_INSTALL_DIR = '/drone/install/dir'
showardc75fded2009-10-14 16:20:02 +000061 _DRONE_RESULTS_DIR = os.path.join(_DRONE_INSTALL_DIR, 'results')
showard42d44982009-10-12 20:34:03 +000062 _RESULTS_DIR = '/results/dir'
63 _SOURCE_PATH = 'source/path'
64 _DESTINATION_PATH = 'destination/path'
showard2aafd902009-10-14 16:20:14 +000065 _WORKING_DIRECTORY = 'working/directory'
showard9bb960b2009-11-19 01:02:11 +000066 _USERNAME = 'my_user'
showard42d44982009-10-12 20:34:03 +000067
showarde39ebe92009-06-18 23:14:48 +000068 def setUp(self):
showard42d44982009-10-12 20:34:03 +000069 self.god = mock.mock_god()
70 self.god.stub_with(drones, 'AUTOTEST_INSTALL_DIR',
71 self._DRONE_INSTALL_DIR)
showarde39ebe92009-06-18 23:14:48 +000072 self.manager = drone_manager.DroneManager()
showard42d44982009-10-12 20:34:03 +000073 self.god.stub_with(self.manager, '_results_dir', self._RESULTS_DIR)
74
showard2aafd902009-10-14 16:20:14 +000075 # we don't want this to ever actually get called
76 self.god.stub_function(drones, 'get_drone')
showardac5b0002009-10-19 18:34:00 +000077 # we don't want the DroneManager to go messing with global config
78 def do_nothing():
79 pass
80 self.god.stub_with(self.manager, 'refresh_drone_configs', do_nothing)
showard2aafd902009-10-14 16:20:14 +000081
showard42d44982009-10-12 20:34:03 +000082 # set up some dummy drones
showard202343e2009-10-14 16:20:24 +000083 self.mock_drone = MockDrone('mock_drone')
showard42d44982009-10-12 20:34:03 +000084 self.manager._drones[self.mock_drone.name] = self.mock_drone
85 self.results_drone = MockDrone('results_drone', 0, 10)
86 self.manager._results_drone = self.results_drone
87
88 self.mock_drone_process = drone_manager.Process(self.mock_drone.name, 0)
89
90
91 def tearDown(self):
92 self.god.unstub_all()
showarde39ebe92009-06-18 23:14:48 +000093
94
95 def _test_choose_drone_for_execution_helper(self, processes_info_list,
96 requested_processes):
97 for index, process_info in enumerate(processes_info_list):
98 active_processes, max_processes = process_info
99 self.manager._enqueue_drone(MockDrone(index, active_processes,
100 max_processes))
101
showard9bb960b2009-11-19 01:02:11 +0000102 return self.manager._choose_drone_for_execution(requested_processes,
103 self._USERNAME)
showarde39ebe92009-06-18 23:14:48 +0000104
105
106 def test_choose_drone_for_execution(self):
107 drone = self._test_choose_drone_for_execution_helper([(1, 2), (0, 2)],
108 1)
109 self.assertEquals(drone.name, 1)
110
111
112 def test_choose_drone_for_execution_some_full(self):
113 drone = self._test_choose_drone_for_execution_helper([(0, 1), (1, 3)],
114 2)
115 self.assertEquals(drone.name, 1)
116
117
118 def test_choose_drone_for_execution_all_full(self):
119 drone = self._test_choose_drone_for_execution_helper([(2, 1), (3, 2)],
120 1)
121 self.assertEquals(drone.name, 1)
122
123
showard9bb960b2009-11-19 01:02:11 +0000124 def test_user_restrictions(self):
125 # this drone is restricted to a different user
126 self.manager._enqueue_drone(MockDrone(1, max_processes=10,
127 allowed_users=['fakeuser']))
128 # this drone is allowed but has lower capacity
129 self.manager._enqueue_drone(MockDrone(2, max_processes=2,
130 allowed_users=[self._USERNAME]))
131
132 self.assertEquals(2,
133 self.manager.max_runnable_processes(self._USERNAME))
134 drone = self.manager._choose_drone_for_execution(
135 1, username=self._USERNAME)
136 self.assertEquals(drone.name, 2)
137
138
showard2aafd902009-10-14 16:20:14 +0000139 def test_initialize(self):
showard2aafd902009-10-14 16:20:14 +0000140 results_hostname = 'results_repo'
showardac5b0002009-10-19 18:34:00 +0000141 results_install_dir = '/results/install'
142 global_config.global_config.override_config_value(
143 scheduler_config.CONFIG_SECTION,
144 'results_host_installation_directory', results_install_dir)
showard2aafd902009-10-14 16:20:14 +0000145
showard2aafd902009-10-14 16:20:14 +0000146 (drones.get_drone.expect_call(self.mock_drone.name)
147 .and_return(self.mock_drone))
showard202343e2009-10-14 16:20:24 +0000148
149 results_drone = MockDrone('results_drone')
150 self.god.stub_function(results_drone, 'set_autotest_install_dir')
151 drones.get_drone.expect_call(results_hostname).and_return(results_drone)
showardac5b0002009-10-19 18:34:00 +0000152 results_drone.set_autotest_install_dir.expect_call(results_install_dir)
showard2aafd902009-10-14 16:20:14 +0000153
154 self.manager.initialize(base_results_dir=self._RESULTS_DIR,
155 drone_hostnames=[self.mock_drone.name],
156 results_repository_hostname=results_hostname)
157
158 self.assert_(self.mock_drone.was_call_queued(
159 'initialize', self._DRONE_RESULTS_DIR + '/'))
showardac5b0002009-10-19 18:34:00 +0000160 self.god.check_playback()
showard2aafd902009-10-14 16:20:14 +0000161
162
showard42d44982009-10-12 20:34:03 +0000163 def test_execute_command(self):
164 self.manager._enqueue_drone(self.mock_drone)
165
showard42d44982009-10-12 20:34:03 +0000166 pidfile_name = 'my_pidfile'
167 log_file = 'log_file'
168
169 pidfile_id = self.manager.execute_command(
170 command=['test', drone_manager.WORKING_DIRECTORY],
showard2aafd902009-10-14 16:20:14 +0000171 working_directory=self._WORKING_DIRECTORY,
showard42d44982009-10-12 20:34:03 +0000172 pidfile_name=pidfile_name,
173 log_file=log_file)
174
showardc75fded2009-10-14 16:20:02 +0000175 full_working_directory = os.path.join(self._DRONE_RESULTS_DIR,
showard2aafd902009-10-14 16:20:14 +0000176 self._WORKING_DIRECTORY)
showard42d44982009-10-12 20:34:03 +0000177 self.assertEquals(pidfile_id.path,
178 os.path.join(full_working_directory, pidfile_name))
179 self.assert_(self.mock_drone.was_call_queued(
180 'execute_command', ['test', full_working_directory],
181 full_working_directory,
showardc75fded2009-10-14 16:20:02 +0000182 os.path.join(self._DRONE_RESULTS_DIR, log_file), pidfile_name))
showard42d44982009-10-12 20:34:03 +0000183
184
showard2aafd902009-10-14 16:20:14 +0000185 def test_attach_file_to_execution(self):
186 self.manager._enqueue_drone(self.mock_drone)
187
188 contents = 'my\ncontents'
189 attached_path = self.manager.attach_file_to_execution(
190 self._WORKING_DIRECTORY, contents)
191 self.manager.execute_command(command=['test'],
192 working_directory=self._WORKING_DIRECTORY,
193 pidfile_name='mypidfile')
194
195 self.assert_(self.mock_drone.was_call_queued(
196 'write_to_file',
197 os.path.join(self._DRONE_RESULTS_DIR, attached_path),
198 contents))
199
200
showard42d44982009-10-12 20:34:03 +0000201 def test_copy_results_on_drone(self):
202 self.manager.copy_results_on_drone(self.mock_drone_process,
203 self._SOURCE_PATH,
204 self._DESTINATION_PATH)
205 self.assert_(self.mock_drone.was_call_queued(
206 'copy_file_or_directory',
showardc75fded2009-10-14 16:20:02 +0000207 os.path.join(self._DRONE_RESULTS_DIR, self._SOURCE_PATH),
208 os.path.join(self._DRONE_RESULTS_DIR, self._DESTINATION_PATH)))
showard42d44982009-10-12 20:34:03 +0000209
210
211 def test_copy_to_results_repository(self):
212 self.manager.copy_to_results_repository(self.mock_drone_process,
213 self._SOURCE_PATH)
214 self.assert_(self.mock_drone.was_file_sent(
215 self.results_drone,
showardc75fded2009-10-14 16:20:02 +0000216 os.path.join(self._DRONE_RESULTS_DIR, self._SOURCE_PATH),
showard42d44982009-10-12 20:34:03 +0000217 os.path.join(self._RESULTS_DIR, self._SOURCE_PATH)))
218
219
220 def test_write_lines_to_file(self):
221 file_path = 'file/path'
222 lines = ['line1', 'line2']
223 written_data = 'line1\nline2\n'
224
225 # write to results repository
226 self.manager.write_lines_to_file(file_path, lines)
227 self.assert_(self.results_drone.was_call_queued(
228 'write_to_file', os.path.join(self._RESULTS_DIR, file_path),
229 written_data))
230
231 # write to a drone
232 self.manager.write_lines_to_file(
233 file_path, lines, paired_with_process=self.mock_drone_process)
234 self.assert_(self.mock_drone.was_call_queued(
235 'write_to_file',
showardc75fded2009-10-14 16:20:02 +0000236 os.path.join(self._DRONE_RESULTS_DIR, file_path), written_data))
showard42d44982009-10-12 20:34:03 +0000237
238
showarde39ebe92009-06-18 23:14:48 +0000239if __name__ == '__main__':
240 unittest.main()