blob: e9f1f5368d62dcc6f4c9cc4f9b47fe45fb5400d8 [file] [log] [blame]
showarde39ebe92009-06-18 23:14:48 +00001#!/usr/bin/python
2
Prashanth B340fd1e2014-06-22 12:44:10 -07003import cPickle
showard42d44982009-10-12 20:34:03 +00004import os, unittest
showarde39ebe92009-06-18 23:14:48 +00005import common
Prashanth B340fd1e2014-06-22 12:44:10 -07006from autotest_lib.client.bin import local_host
showardac5b0002009-10-19 18:34:00 +00007from autotest_lib.client.common_lib import global_config
Prashanth B340fd1e2014-06-22 12:44:10 -07008from autotest_lib.client.common_lib import utils
showard42d44982009-10-12 20:34:03 +00009from autotest_lib.client.common_lib.test_utils import mock
showard2aafd902009-10-14 16:20:14 +000010from autotest_lib.scheduler import drone_manager, drone_utility, drones
Simran Basi882f15b2013-10-29 14:59:34 -070011from autotest_lib.scheduler import scheduler_config, site_drone_manager
Prashanth B340fd1e2014-06-22 12:44:10 -070012from autotest_lib.scheduler import thread_lib
13from autotest_lib.scheduler import pidfile_monitor
14from autotest_lib.server.hosts import ssh_host
15
showarde39ebe92009-06-18 23:14:48 +000016
17class MockDrone(drones._AbstractDrone):
showard9bb960b2009-11-19 01:02:11 +000018 def __init__(self, name, active_processes=0, max_processes=10,
19 allowed_users=None):
showarde39ebe92009-06-18 23:14:48 +000020 super(MockDrone, self).__init__()
21 self.name = name
jamesren76fcf192010-04-21 20:39:50 +000022 self.hostname = name
showarde39ebe92009-06-18 23:14:48 +000023 self.active_processes = active_processes
24 self.max_processes = max_processes
showard9bb960b2009-11-19 01:02:11 +000025 self.allowed_users = allowed_users
showard42d44982009-10-12 20:34:03 +000026 # maps method names list of tuples containing method arguments
27 self._recorded_calls = {'queue_call': [],
28 'send_file_to': []}
29
30
31 def queue_call(self, method, *args, **kwargs):
32 self._recorded_calls['queue_call'].append((method, args, kwargs))
33
34
showard2aafd902009-10-14 16:20:14 +000035 def call(self, method, *args, **kwargs):
36 # don't bother differentiating between call() and queue_call()
37 return self.queue_call(method, *args, **kwargs)
38
39
showard42d44982009-10-12 20:34:03 +000040 def send_file_to(self, drone, source_path, destination_path,
41 can_fail=False):
42 self._recorded_calls['send_file_to'].append(
43 (drone, source_path, destination_path))
44
45
46 # method for use by tests
47 def _check_for_recorded_call(self, method_name, arguments):
48 recorded_arg_list = self._recorded_calls[method_name]
49 was_called = arguments in recorded_arg_list
50 if not was_called:
51 print 'Recorded args:', recorded_arg_list
52 print 'Expected:', arguments
53 return was_called
54
55
56 def was_call_queued(self, method, *args, **kwargs):
57 return self._check_for_recorded_call('queue_call',
58 (method, args, kwargs))
59
60
61 def was_file_sent(self, drone, source_path, destination_path):
62 return self._check_for_recorded_call('send_file_to',
63 (drone, source_path,
64 destination_path))
showarde39ebe92009-06-18 23:14:48 +000065
66
67class DroneManager(unittest.TestCase):
showard42d44982009-10-12 20:34:03 +000068 _DRONE_INSTALL_DIR = '/drone/install/dir'
showardc75fded2009-10-14 16:20:02 +000069 _DRONE_RESULTS_DIR = os.path.join(_DRONE_INSTALL_DIR, 'results')
showard42d44982009-10-12 20:34:03 +000070 _RESULTS_DIR = '/results/dir'
71 _SOURCE_PATH = 'source/path'
72 _DESTINATION_PATH = 'destination/path'
showard2aafd902009-10-14 16:20:14 +000073 _WORKING_DIRECTORY = 'working/directory'
showard9bb960b2009-11-19 01:02:11 +000074 _USERNAME = 'my_user'
showard42d44982009-10-12 20:34:03 +000075
showarde39ebe92009-06-18 23:14:48 +000076 def setUp(self):
showard42d44982009-10-12 20:34:03 +000077 self.god = mock.mock_god()
78 self.god.stub_with(drones, 'AUTOTEST_INSTALL_DIR',
79 self._DRONE_INSTALL_DIR)
showarde39ebe92009-06-18 23:14:48 +000080 self.manager = drone_manager.DroneManager()
showard42d44982009-10-12 20:34:03 +000081 self.god.stub_with(self.manager, '_results_dir', self._RESULTS_DIR)
82
showard2aafd902009-10-14 16:20:14 +000083 # we don't want this to ever actually get called
84 self.god.stub_function(drones, 'get_drone')
showardac5b0002009-10-19 18:34:00 +000085 # we don't want the DroneManager to go messing with global config
86 def do_nothing():
87 pass
88 self.god.stub_with(self.manager, 'refresh_drone_configs', do_nothing)
showard2aafd902009-10-14 16:20:14 +000089
showard42d44982009-10-12 20:34:03 +000090 # set up some dummy drones
showard202343e2009-10-14 16:20:24 +000091 self.mock_drone = MockDrone('mock_drone')
showard42d44982009-10-12 20:34:03 +000092 self.manager._drones[self.mock_drone.name] = self.mock_drone
93 self.results_drone = MockDrone('results_drone', 0, 10)
94 self.manager._results_drone = self.results_drone
95
96 self.mock_drone_process = drone_manager.Process(self.mock_drone.name, 0)
97
98
99 def tearDown(self):
100 self.god.unstub_all()
showarde39ebe92009-06-18 23:14:48 +0000101
102
103 def _test_choose_drone_for_execution_helper(self, processes_info_list,
104 requested_processes):
105 for index, process_info in enumerate(processes_info_list):
106 active_processes, max_processes = process_info
107 self.manager._enqueue_drone(MockDrone(index, active_processes,
108 max_processes))
109
showard9bb960b2009-11-19 01:02:11 +0000110 return self.manager._choose_drone_for_execution(requested_processes,
jamesren76fcf192010-04-21 20:39:50 +0000111 self._USERNAME, None)
showarde39ebe92009-06-18 23:14:48 +0000112
113
114 def test_choose_drone_for_execution(self):
115 drone = self._test_choose_drone_for_execution_helper([(1, 2), (0, 2)],
116 1)
117 self.assertEquals(drone.name, 1)
118
119
120 def test_choose_drone_for_execution_some_full(self):
121 drone = self._test_choose_drone_for_execution_helper([(0, 1), (1, 3)],
122 2)
123 self.assertEquals(drone.name, 1)
124
125
126 def test_choose_drone_for_execution_all_full(self):
127 drone = self._test_choose_drone_for_execution_helper([(2, 1), (3, 2)],
128 1)
129 self.assertEquals(drone.name, 1)
130
131
jamesren37b50452010-03-25 20:38:56 +0000132 def test_choose_drone_for_execution_all_full_same_percentage_capacity(self):
133 drone = self._test_choose_drone_for_execution_helper([(5, 3), (10, 6)],
134 1)
135 self.assertEquals(drone.name, 1)
136
137
showard9bb960b2009-11-19 01:02:11 +0000138 def test_user_restrictions(self):
139 # this drone is restricted to a different user
140 self.manager._enqueue_drone(MockDrone(1, max_processes=10,
141 allowed_users=['fakeuser']))
142 # this drone is allowed but has lower capacity
143 self.manager._enqueue_drone(MockDrone(2, max_processes=2,
144 allowed_users=[self._USERNAME]))
145
146 self.assertEquals(2,
jamesren76fcf192010-04-21 20:39:50 +0000147 self.manager.max_runnable_processes(self._USERNAME,
148 None))
showard9bb960b2009-11-19 01:02:11 +0000149 drone = self.manager._choose_drone_for_execution(
jamesren76fcf192010-04-21 20:39:50 +0000150 1, username=self._USERNAME, drone_hostnames_allowed=None)
showard9bb960b2009-11-19 01:02:11 +0000151 self.assertEquals(drone.name, 2)
152
153
jamesren37b50452010-03-25 20:38:56 +0000154 def test_user_restrictions_with_full_drone(self):
155 # this drone is restricted to a different user
156 self.manager._enqueue_drone(MockDrone(1, max_processes=10,
157 allowed_users=['fakeuser']))
158 # this drone is allowed but is full
159 self.manager._enqueue_drone(MockDrone(2, active_processes=3,
160 max_processes=2,
161 allowed_users=[self._USERNAME]))
162
163 self.assertEquals(0,
jamesren76fcf192010-04-21 20:39:50 +0000164 self.manager.max_runnable_processes(self._USERNAME,
165 None))
jamesren37b50452010-03-25 20:38:56 +0000166 drone = self.manager._choose_drone_for_execution(
jamesren76fcf192010-04-21 20:39:50 +0000167 1, username=self._USERNAME, drone_hostnames_allowed=None)
jamesren37b50452010-03-25 20:38:56 +0000168 self.assertEquals(drone.name, 2)
169
170
jamesren76fcf192010-04-21 20:39:50 +0000171 def _setup_test_drone_restrictions(self, active_processes=0):
172 self.manager._enqueue_drone(MockDrone(
173 1, active_processes=active_processes, max_processes=10))
174 self.manager._enqueue_drone(MockDrone(
175 2, active_processes=active_processes, max_processes=5))
176 self.manager._enqueue_drone(MockDrone(
177 3, active_processes=active_processes, max_processes=2))
178
179
180 def test_drone_restrictions_allow_any(self):
181 self._setup_test_drone_restrictions()
182 self.assertEquals(10,
183 self.manager.max_runnable_processes(self._USERNAME,
184 None))
185 drone = self.manager._choose_drone_for_execution(
186 1, username=self._USERNAME, drone_hostnames_allowed=None)
187 self.assertEqual(drone.name, 1)
188
189
190 def test_drone_restrictions_under_capacity(self):
191 self._setup_test_drone_restrictions()
192 drone_hostnames_allowed = (2, 3)
193 self.assertEquals(
194 5, self.manager.max_runnable_processes(self._USERNAME,
195 drone_hostnames_allowed))
196 drone = self.manager._choose_drone_for_execution(
197 1, username=self._USERNAME,
198 drone_hostnames_allowed=drone_hostnames_allowed)
199
200 self.assertEqual(drone.name, 2)
201
202
203 def test_drone_restrictions_over_capacity(self):
204 self._setup_test_drone_restrictions(active_processes=6)
205 drone_hostnames_allowed = (2, 3)
206 self.assertEquals(
207 0, self.manager.max_runnable_processes(self._USERNAME,
208 drone_hostnames_allowed))
209 drone = self.manager._choose_drone_for_execution(
210 7, username=self._USERNAME,
211 drone_hostnames_allowed=drone_hostnames_allowed)
212 self.assertEqual(drone.name, 2)
213
214
215 def test_drone_restrictions_allow_none(self):
216 self._setup_test_drone_restrictions()
217 drone_hostnames_allowed = ()
218 self.assertEquals(
219 0, self.manager.max_runnable_processes(self._USERNAME,
220 drone_hostnames_allowed))
221 drone = self.manager._choose_drone_for_execution(
222 1, username=self._USERNAME,
223 drone_hostnames_allowed=drone_hostnames_allowed)
224 self.assertEqual(drone, None)
225
226
showard2aafd902009-10-14 16:20:14 +0000227 def test_initialize(self):
showard2aafd902009-10-14 16:20:14 +0000228 results_hostname = 'results_repo'
showardac5b0002009-10-19 18:34:00 +0000229 results_install_dir = '/results/install'
230 global_config.global_config.override_config_value(
231 scheduler_config.CONFIG_SECTION,
232 'results_host_installation_directory', results_install_dir)
showard2aafd902009-10-14 16:20:14 +0000233
showard2aafd902009-10-14 16:20:14 +0000234 (drones.get_drone.expect_call(self.mock_drone.name)
235 .and_return(self.mock_drone))
showard202343e2009-10-14 16:20:24 +0000236
237 results_drone = MockDrone('results_drone')
238 self.god.stub_function(results_drone, 'set_autotest_install_dir')
239 drones.get_drone.expect_call(results_hostname).and_return(results_drone)
showardac5b0002009-10-19 18:34:00 +0000240 results_drone.set_autotest_install_dir.expect_call(results_install_dir)
showard2aafd902009-10-14 16:20:14 +0000241
242 self.manager.initialize(base_results_dir=self._RESULTS_DIR,
243 drone_hostnames=[self.mock_drone.name],
244 results_repository_hostname=results_hostname)
245
246 self.assert_(self.mock_drone.was_call_queued(
247 'initialize', self._DRONE_RESULTS_DIR + '/'))
showardac5b0002009-10-19 18:34:00 +0000248 self.god.check_playback()
showard2aafd902009-10-14 16:20:14 +0000249
250
showard42d44982009-10-12 20:34:03 +0000251 def test_execute_command(self):
252 self.manager._enqueue_drone(self.mock_drone)
253
showard42d44982009-10-12 20:34:03 +0000254 pidfile_name = 'my_pidfile'
255 log_file = 'log_file'
256
257 pidfile_id = self.manager.execute_command(
258 command=['test', drone_manager.WORKING_DIRECTORY],
showard2aafd902009-10-14 16:20:14 +0000259 working_directory=self._WORKING_DIRECTORY,
showard42d44982009-10-12 20:34:03 +0000260 pidfile_name=pidfile_name,
showard418785b2009-11-23 20:19:59 +0000261 num_processes=1,
showard42d44982009-10-12 20:34:03 +0000262 log_file=log_file)
263
showardc75fded2009-10-14 16:20:02 +0000264 full_working_directory = os.path.join(self._DRONE_RESULTS_DIR,
showard2aafd902009-10-14 16:20:14 +0000265 self._WORKING_DIRECTORY)
showard42d44982009-10-12 20:34:03 +0000266 self.assertEquals(pidfile_id.path,
267 os.path.join(full_working_directory, pidfile_name))
268 self.assert_(self.mock_drone.was_call_queued(
269 'execute_command', ['test', full_working_directory],
270 full_working_directory,
showardc75fded2009-10-14 16:20:02 +0000271 os.path.join(self._DRONE_RESULTS_DIR, log_file), pidfile_name))
showard42d44982009-10-12 20:34:03 +0000272
273
showard2aafd902009-10-14 16:20:14 +0000274 def test_attach_file_to_execution(self):
275 self.manager._enqueue_drone(self.mock_drone)
276
277 contents = 'my\ncontents'
278 attached_path = self.manager.attach_file_to_execution(
279 self._WORKING_DIRECTORY, contents)
280 self.manager.execute_command(command=['test'],
281 working_directory=self._WORKING_DIRECTORY,
showard418785b2009-11-23 20:19:59 +0000282 pidfile_name='mypidfile',
jamesren76fcf192010-04-21 20:39:50 +0000283 num_processes=1,
284 drone_hostnames_allowed=None)
showard2aafd902009-10-14 16:20:14 +0000285
286 self.assert_(self.mock_drone.was_call_queued(
287 'write_to_file',
288 os.path.join(self._DRONE_RESULTS_DIR, attached_path),
289 contents))
290
291
showard42d44982009-10-12 20:34:03 +0000292 def test_copy_results_on_drone(self):
293 self.manager.copy_results_on_drone(self.mock_drone_process,
294 self._SOURCE_PATH,
295 self._DESTINATION_PATH)
296 self.assert_(self.mock_drone.was_call_queued(
297 'copy_file_or_directory',
showardc75fded2009-10-14 16:20:02 +0000298 os.path.join(self._DRONE_RESULTS_DIR, self._SOURCE_PATH),
299 os.path.join(self._DRONE_RESULTS_DIR, self._DESTINATION_PATH)))
showard42d44982009-10-12 20:34:03 +0000300
301
302 def test_copy_to_results_repository(self):
Simran Basi882f15b2013-10-29 14:59:34 -0700303 site_drone_manager.ENABLE_ARCHIVING = True
showard42d44982009-10-12 20:34:03 +0000304 self.manager.copy_to_results_repository(self.mock_drone_process,
305 self._SOURCE_PATH)
306 self.assert_(self.mock_drone.was_file_sent(
307 self.results_drone,
showardc75fded2009-10-14 16:20:02 +0000308 os.path.join(self._DRONE_RESULTS_DIR, self._SOURCE_PATH),
showard42d44982009-10-12 20:34:03 +0000309 os.path.join(self._RESULTS_DIR, self._SOURCE_PATH)))
310
311
312 def test_write_lines_to_file(self):
313 file_path = 'file/path'
314 lines = ['line1', 'line2']
315 written_data = 'line1\nline2\n'
316
317 # write to results repository
318 self.manager.write_lines_to_file(file_path, lines)
319 self.assert_(self.results_drone.was_call_queued(
320 'write_to_file', os.path.join(self._RESULTS_DIR, file_path),
321 written_data))
322
323 # write to a drone
324 self.manager.write_lines_to_file(
325 file_path, lines, paired_with_process=self.mock_drone_process)
326 self.assert_(self.mock_drone.was_call_queued(
327 'write_to_file',
showardc75fded2009-10-14 16:20:02 +0000328 os.path.join(self._DRONE_RESULTS_DIR, file_path), written_data))
showard42d44982009-10-12 20:34:03 +0000329
330
showardd3496242009-12-10 21:41:43 +0000331 def test_pidfile_expiration(self):
332 self.god.stub_with(self.manager, '_get_max_pidfile_refreshes',
333 lambda: 0)
334 pidfile_id = self.manager.get_pidfile_id_from('tag', 'name')
335 self.manager.register_pidfile(pidfile_id)
336 self.manager._drop_old_pidfiles()
337 self.manager._drop_old_pidfiles()
338 self.assertFalse(self.manager._registered_pidfile_info)
339
340
Prashanth B340fd1e2014-06-22 12:44:10 -0700341class ThreadedDroneTest(unittest.TestCase):
342 _DRONE_INSTALL_DIR = '/drone/install/dir'
343 _RESULTS_DIR = '/results/dir'
344 _DRONE_CLASS = drones._RemoteDrone
345 _DRONE_HOST = ssh_host.SSHHost
346
347
348 def create_drone(self, drone_hostname, mock_hostname):
349 """Create and initialize a Remote Drone.
350
351 @return: A remote drone instance.
352 """
353 mock_host = self.god.create_mock_class(self._DRONE_HOST, mock_hostname)
354 self.god.stub_function(drones.drone_utility, 'create_host')
355 drones.drone_utility.create_host.expect_call(drone_hostname).and_return(
356 mock_host)
357 mock_host.is_up.expect_call().and_return(True)
358 return self._DRONE_CLASS(drone_hostname)
359
360
361 def create_fake_pidfile_info(self, tag='tag', name='name'):
362 pidfile_id = self.manager.get_pidfile_id_from(tag, name)
363 self.manager.register_pidfile(pidfile_id)
364 return self.manager._registered_pidfile_info
365
366
367 def setUp(self):
368 self.god = mock.mock_god()
369 self.god.stub_with(drones, 'AUTOTEST_INSTALL_DIR',
370 self._DRONE_INSTALL_DIR)
371 self.manager = drone_manager.DroneManager()
372 self.god.stub_with(self.manager, '_results_dir', self._RESULTS_DIR)
373
374 # we don't want this to ever actually get called
375 self.god.stub_function(drones, 'get_drone')
376 # we don't want the DroneManager to go messing with global config
377 def do_nothing():
378 pass
379 self.god.stub_with(self.manager, 'refresh_drone_configs', do_nothing)
380
381 self.results_drone = MockDrone('results_drone', 0, 10)
382 self.manager._results_drone = self.results_drone
383 self.drone_utility_path = 'mock-drone-utility-path'
384 self.mock_return = {'results': ['mock results'],
385 'warnings': []}
386
387
388 def tearDown(self):
389 self.god.unstub_all()
390
391 def test_trigger_refresh(self):
392 """Test drone manager trigger refresh."""
393 self.god.stub_with(self._DRONE_CLASS, '_drone_utility_path',
394 self.drone_utility_path)
395 mock_drone = self.create_drone('fakedrone1', 'fakehost1')
396 self.manager._drones[mock_drone.hostname] = mock_drone
397
398 # Create some fake pidfiles and confirm that a refresh call is
399 # executed on each drone host, with the same pidfile paths. Then
400 # check that each drone gets a key in the returned results dictionary.
401 for i in range(0, 1):
402 pidfile_info = self.create_fake_pidfile_info(
403 'tag%s' % i, 'name%s' %i)
404 pidfile_paths = [pidfile.path for pidfile in pidfile_info.keys()]
405 refresh_call = drone_utility.call('refresh', pidfile_paths)
406 expected_results = {}
407 mock_result = utils.CmdResult(
408 stdout=cPickle.dumps(self.mock_return))
409 for drone in self.manager.get_drones():
410 drone._host.run.expect_call(
411 'python %s' % self.drone_utility_path,
412 stdin=cPickle.dumps([refresh_call]), stdout_tee=None,
413 connect_timeout=mock.is_instance_comparator(int)
414 ).and_return(mock_result)
415 expected_results[drone] = self.mock_return['results']
416 self.manager.trigger_refresh()
417 self.assertTrue(self.manager._refresh_task_queue.get_results() ==
418 expected_results)
419 self.god.check_playback()
420
421
422 def test_sync_refresh(self):
423 """Test drone manager sync refresh."""
424
425 mock_drone = self.create_drone('fakedrone1', 'fakehost1')
426 self.manager._drones[mock_drone.hostname] = mock_drone
427
428 # Insert some drone_utility results into the results queue, then
429 # check that get_results returns it in the right format, and that
430 # the rest of sync_refresh populates the right datastructures for
431 # correct handling of agents. Also confirm that this method of
432 # syncing is sufficient for the monitor to pick up the exit status
433 # of the process in the same way it would in handle_agents.
434 pidfile_path = 'results/hosts/host_id/job_id-name/.autoserv_execute'
435 pidfiles = {pidfile_path: '123\n12\n0\n'}
436 drone_utility_results = {
437 'pidfiles': pidfiles,
438 'autoserv_processes':{},
439 'all_processes':{},
440 'parse_processes':{},
441 'pidfiles_second_read':pidfiles,
442 }
443 # Our manager instance isn't the drone manager singletone that the
444 # pidfile_monitor will use by default, becuase setUp doesn't call
445 # drone_manager.instance().
Jakob Jülich36accc62014-07-23 10:26:55 -0700446 self.god.stub_with(drone_manager, '_the_instance', self.manager)
Prashanth B340fd1e2014-06-22 12:44:10 -0700447 monitor = pidfile_monitor.PidfileRunMonitor()
448 monitor.pidfile_id = drone_manager.PidfileId(pidfile_path)
449 self.manager.register_pidfile(monitor.pidfile_id)
450 self.assertTrue(monitor._state.exit_status == None)
451
452 self.manager._refresh_task_queue.results_queue.put(
453 thread_lib.ThreadedTaskQueue.result(
454 mock_drone, [drone_utility_results]))
455 self.manager.sync_refresh()
456 pidfiles = self.manager._pidfiles
457 pidfile_id = pidfiles.keys()[0]
458 pidfile_contents = pidfiles[pidfile_id]
459
460 self.assertTrue(
461 pidfile_id.path == pidfile_path and
462 pidfile_contents.process.pid == 123 and
463 pidfile_contents.process.hostname ==
464 mock_drone.hostname and
465 pidfile_contents.exit_status == 12 and
466 pidfile_contents.num_tests_failed == 0)
467 self.assertTrue(monitor.exit_code() == 12)
468 self.god.check_playback()
469
470
471class ThreadedLocalhostDroneTest(ThreadedDroneTest):
472 _DRONE_CLASS = drones._LocalDrone
473 _DRONE_HOST = local_host.LocalHost
474
475
476 def create_drone(self, drone_hostname, mock_hostname):
477 """Create and initialize a Remote Drone.
478
479 @return: A remote drone instance.
480 """
481 mock_host = self.god.create_mock_class(self._DRONE_HOST, mock_hostname)
482 self.god.stub_function(drones.drone_utility, 'create_host')
483 local_drone = self._DRONE_CLASS()
484 self.god.stub_with(local_drone, '_host', mock_host)
485 return local_drone
486
487
showarde39ebe92009-06-18 23:14:48 +0000488if __name__ == '__main__':
489 unittest.main()