showard | e39ebe9 | 2009-06-18 23:14:48 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
showard | 42d4498 | 2009-10-12 20:34:03 +0000 | [diff] [blame] | 3 | import os, unittest |
showard | e39ebe9 | 2009-06-18 23:14:48 +0000 | [diff] [blame] | 4 | import common |
showard | ac5b000 | 2009-10-19 18:34:00 +0000 | [diff] [blame] | 5 | from autotest_lib.client.common_lib import global_config |
showard | 42d4498 | 2009-10-12 20:34:03 +0000 | [diff] [blame] | 6 | from autotest_lib.client.common_lib.test_utils import mock |
showard | 2aafd90 | 2009-10-14 16:20:14 +0000 | [diff] [blame] | 7 | from autotest_lib.scheduler import drone_manager, drone_utility, drones |
showard | ac5b000 | 2009-10-19 18:34:00 +0000 | [diff] [blame] | 8 | from autotest_lib.scheduler import scheduler_config |
showard | e39ebe9 | 2009-06-18 23:14:48 +0000 | [diff] [blame] | 9 | |
| 10 | class MockDrone(drones._AbstractDrone): |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 11 | def __init__(self, name, active_processes=0, max_processes=10, |
| 12 | allowed_users=None): |
showard | e39ebe9 | 2009-06-18 23:14:48 +0000 | [diff] [blame] | 13 | super(MockDrone, self).__init__() |
| 14 | self.name = name |
| 15 | self.active_processes = active_processes |
| 16 | self.max_processes = max_processes |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 17 | self.allowed_users = allowed_users |
showard | 42d4498 | 2009-10-12 20:34:03 +0000 | [diff] [blame] | 18 | # 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 | |
showard | 2aafd90 | 2009-10-14 16:20:14 +0000 | [diff] [blame] | 27 | 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 | |
showard | 42d4498 | 2009-10-12 20:34:03 +0000 | [diff] [blame] | 32 | 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)) |
showard | e39ebe9 | 2009-06-18 23:14:48 +0000 | [diff] [blame] | 57 | |
| 58 | |
| 59 | class DroneManager(unittest.TestCase): |
showard | 42d4498 | 2009-10-12 20:34:03 +0000 | [diff] [blame] | 60 | _DRONE_INSTALL_DIR = '/drone/install/dir' |
showard | c75fded | 2009-10-14 16:20:02 +0000 | [diff] [blame] | 61 | _DRONE_RESULTS_DIR = os.path.join(_DRONE_INSTALL_DIR, 'results') |
showard | 42d4498 | 2009-10-12 20:34:03 +0000 | [diff] [blame] | 62 | _RESULTS_DIR = '/results/dir' |
| 63 | _SOURCE_PATH = 'source/path' |
| 64 | _DESTINATION_PATH = 'destination/path' |
showard | 2aafd90 | 2009-10-14 16:20:14 +0000 | [diff] [blame] | 65 | _WORKING_DIRECTORY = 'working/directory' |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 66 | _USERNAME = 'my_user' |
showard | 42d4498 | 2009-10-12 20:34:03 +0000 | [diff] [blame] | 67 | |
showard | e39ebe9 | 2009-06-18 23:14:48 +0000 | [diff] [blame] | 68 | def setUp(self): |
showard | 42d4498 | 2009-10-12 20:34:03 +0000 | [diff] [blame] | 69 | self.god = mock.mock_god() |
| 70 | self.god.stub_with(drones, 'AUTOTEST_INSTALL_DIR', |
| 71 | self._DRONE_INSTALL_DIR) |
showard | e39ebe9 | 2009-06-18 23:14:48 +0000 | [diff] [blame] | 72 | self.manager = drone_manager.DroneManager() |
showard | 42d4498 | 2009-10-12 20:34:03 +0000 | [diff] [blame] | 73 | self.god.stub_with(self.manager, '_results_dir', self._RESULTS_DIR) |
| 74 | |
showard | 2aafd90 | 2009-10-14 16:20:14 +0000 | [diff] [blame] | 75 | # we don't want this to ever actually get called |
| 76 | self.god.stub_function(drones, 'get_drone') |
showard | ac5b000 | 2009-10-19 18:34:00 +0000 | [diff] [blame] | 77 | # 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) |
showard | 2aafd90 | 2009-10-14 16:20:14 +0000 | [diff] [blame] | 81 | |
showard | 42d4498 | 2009-10-12 20:34:03 +0000 | [diff] [blame] | 82 | # set up some dummy drones |
showard | 202343e | 2009-10-14 16:20:24 +0000 | [diff] [blame] | 83 | self.mock_drone = MockDrone('mock_drone') |
showard | 42d4498 | 2009-10-12 20:34:03 +0000 | [diff] [blame] | 84 | 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() |
showard | e39ebe9 | 2009-06-18 23:14:48 +0000 | [diff] [blame] | 93 | |
| 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 | |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 102 | return self.manager._choose_drone_for_execution(requested_processes, |
| 103 | self._USERNAME) |
showard | e39ebe9 | 2009-06-18 23:14:48 +0000 | [diff] [blame] | 104 | |
| 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 | |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 124 | 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 | |
showard | 2aafd90 | 2009-10-14 16:20:14 +0000 | [diff] [blame] | 139 | def test_initialize(self): |
showard | 2aafd90 | 2009-10-14 16:20:14 +0000 | [diff] [blame] | 140 | results_hostname = 'results_repo' |
showard | ac5b000 | 2009-10-19 18:34:00 +0000 | [diff] [blame] | 141 | 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) |
showard | 2aafd90 | 2009-10-14 16:20:14 +0000 | [diff] [blame] | 145 | |
showard | 2aafd90 | 2009-10-14 16:20:14 +0000 | [diff] [blame] | 146 | (drones.get_drone.expect_call(self.mock_drone.name) |
| 147 | .and_return(self.mock_drone)) |
showard | 202343e | 2009-10-14 16:20:24 +0000 | [diff] [blame] | 148 | |
| 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) |
showard | ac5b000 | 2009-10-19 18:34:00 +0000 | [diff] [blame] | 152 | results_drone.set_autotest_install_dir.expect_call(results_install_dir) |
showard | 2aafd90 | 2009-10-14 16:20:14 +0000 | [diff] [blame] | 153 | |
| 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 + '/')) |
showard | ac5b000 | 2009-10-19 18:34:00 +0000 | [diff] [blame] | 160 | self.god.check_playback() |
showard | 2aafd90 | 2009-10-14 16:20:14 +0000 | [diff] [blame] | 161 | |
| 162 | |
showard | 42d4498 | 2009-10-12 20:34:03 +0000 | [diff] [blame] | 163 | def test_execute_command(self): |
| 164 | self.manager._enqueue_drone(self.mock_drone) |
| 165 | |
showard | 42d4498 | 2009-10-12 20:34:03 +0000 | [diff] [blame] | 166 | pidfile_name = 'my_pidfile' |
| 167 | log_file = 'log_file' |
| 168 | |
| 169 | pidfile_id = self.manager.execute_command( |
| 170 | command=['test', drone_manager.WORKING_DIRECTORY], |
showard | 2aafd90 | 2009-10-14 16:20:14 +0000 | [diff] [blame] | 171 | working_directory=self._WORKING_DIRECTORY, |
showard | 42d4498 | 2009-10-12 20:34:03 +0000 | [diff] [blame] | 172 | pidfile_name=pidfile_name, |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 173 | num_processes=1, |
showard | 42d4498 | 2009-10-12 20:34:03 +0000 | [diff] [blame] | 174 | log_file=log_file) |
| 175 | |
showard | c75fded | 2009-10-14 16:20:02 +0000 | [diff] [blame] | 176 | full_working_directory = os.path.join(self._DRONE_RESULTS_DIR, |
showard | 2aafd90 | 2009-10-14 16:20:14 +0000 | [diff] [blame] | 177 | self._WORKING_DIRECTORY) |
showard | 42d4498 | 2009-10-12 20:34:03 +0000 | [diff] [blame] | 178 | self.assertEquals(pidfile_id.path, |
| 179 | os.path.join(full_working_directory, pidfile_name)) |
| 180 | self.assert_(self.mock_drone.was_call_queued( |
| 181 | 'execute_command', ['test', full_working_directory], |
| 182 | full_working_directory, |
showard | c75fded | 2009-10-14 16:20:02 +0000 | [diff] [blame] | 183 | os.path.join(self._DRONE_RESULTS_DIR, log_file), pidfile_name)) |
showard | 42d4498 | 2009-10-12 20:34:03 +0000 | [diff] [blame] | 184 | |
| 185 | |
showard | 2aafd90 | 2009-10-14 16:20:14 +0000 | [diff] [blame] | 186 | def test_attach_file_to_execution(self): |
| 187 | self.manager._enqueue_drone(self.mock_drone) |
| 188 | |
| 189 | contents = 'my\ncontents' |
| 190 | attached_path = self.manager.attach_file_to_execution( |
| 191 | self._WORKING_DIRECTORY, contents) |
| 192 | self.manager.execute_command(command=['test'], |
| 193 | working_directory=self._WORKING_DIRECTORY, |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 194 | pidfile_name='mypidfile', |
| 195 | num_processes=1) |
showard | 2aafd90 | 2009-10-14 16:20:14 +0000 | [diff] [blame] | 196 | |
| 197 | self.assert_(self.mock_drone.was_call_queued( |
| 198 | 'write_to_file', |
| 199 | os.path.join(self._DRONE_RESULTS_DIR, attached_path), |
| 200 | contents)) |
| 201 | |
| 202 | |
showard | 42d4498 | 2009-10-12 20:34:03 +0000 | [diff] [blame] | 203 | def test_copy_results_on_drone(self): |
| 204 | self.manager.copy_results_on_drone(self.mock_drone_process, |
| 205 | self._SOURCE_PATH, |
| 206 | self._DESTINATION_PATH) |
| 207 | self.assert_(self.mock_drone.was_call_queued( |
| 208 | 'copy_file_or_directory', |
showard | c75fded | 2009-10-14 16:20:02 +0000 | [diff] [blame] | 209 | os.path.join(self._DRONE_RESULTS_DIR, self._SOURCE_PATH), |
| 210 | os.path.join(self._DRONE_RESULTS_DIR, self._DESTINATION_PATH))) |
showard | 42d4498 | 2009-10-12 20:34:03 +0000 | [diff] [blame] | 211 | |
| 212 | |
| 213 | def test_copy_to_results_repository(self): |
| 214 | self.manager.copy_to_results_repository(self.mock_drone_process, |
| 215 | self._SOURCE_PATH) |
| 216 | self.assert_(self.mock_drone.was_file_sent( |
| 217 | self.results_drone, |
showard | c75fded | 2009-10-14 16:20:02 +0000 | [diff] [blame] | 218 | os.path.join(self._DRONE_RESULTS_DIR, self._SOURCE_PATH), |
showard | 42d4498 | 2009-10-12 20:34:03 +0000 | [diff] [blame] | 219 | os.path.join(self._RESULTS_DIR, self._SOURCE_PATH))) |
| 220 | |
| 221 | |
| 222 | def test_write_lines_to_file(self): |
| 223 | file_path = 'file/path' |
| 224 | lines = ['line1', 'line2'] |
| 225 | written_data = 'line1\nline2\n' |
| 226 | |
| 227 | # write to results repository |
| 228 | self.manager.write_lines_to_file(file_path, lines) |
| 229 | self.assert_(self.results_drone.was_call_queued( |
| 230 | 'write_to_file', os.path.join(self._RESULTS_DIR, file_path), |
| 231 | written_data)) |
| 232 | |
| 233 | # write to a drone |
| 234 | self.manager.write_lines_to_file( |
| 235 | file_path, lines, paired_with_process=self.mock_drone_process) |
| 236 | self.assert_(self.mock_drone.was_call_queued( |
| 237 | 'write_to_file', |
showard | c75fded | 2009-10-14 16:20:02 +0000 | [diff] [blame] | 238 | os.path.join(self._DRONE_RESULTS_DIR, file_path), written_data)) |
showard | 42d4498 | 2009-10-12 20:34:03 +0000 | [diff] [blame] | 239 | |
| 240 | |
showard | d349624 | 2009-12-10 21:41:43 +0000 | [diff] [blame^] | 241 | def test_pidfile_expiration(self): |
| 242 | self.god.stub_with(self.manager, '_get_max_pidfile_refreshes', |
| 243 | lambda: 0) |
| 244 | pidfile_id = self.manager.get_pidfile_id_from('tag', 'name') |
| 245 | self.manager.register_pidfile(pidfile_id) |
| 246 | self.manager._drop_old_pidfiles() |
| 247 | self.manager._drop_old_pidfiles() |
| 248 | self.assertFalse(self.manager._registered_pidfile_info) |
| 249 | |
| 250 | |
showard | e39ebe9 | 2009-06-18 23:14:48 +0000 | [diff] [blame] | 251 | if __name__ == '__main__': |
| 252 | unittest.main() |