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 | 42d4498 | 2009-10-12 20:34:03 +0000 | [diff] [blame] | 5 | from autotest_lib.client.common_lib.test_utils import mock |
showard | 2aafd90 | 2009-10-14 16:20:14 +0000 | [diff] [blame] | 6 | from autotest_lib.scheduler import drone_manager, drone_utility, drones |
showard | e39ebe9 | 2009-06-18 23:14:48 +0000 | [diff] [blame] | 7 | |
| 8 | class MockDrone(drones._AbstractDrone): |
showard | 202343e | 2009-10-14 16:20:24 +0000 | [diff] [blame^] | 9 | def __init__(self, name, active_processes=0, max_processes=10): |
showard | e39ebe9 | 2009-06-18 23:14:48 +0000 | [diff] [blame] | 10 | super(MockDrone, self).__init__() |
| 11 | self.name = name |
| 12 | self.active_processes = active_processes |
| 13 | self.max_processes = max_processes |
showard | 42d4498 | 2009-10-12 20:34:03 +0000 | [diff] [blame] | 14 | # maps method names list of tuples containing method arguments |
| 15 | self._recorded_calls = {'queue_call': [], |
| 16 | 'send_file_to': []} |
| 17 | |
| 18 | |
| 19 | def queue_call(self, method, *args, **kwargs): |
| 20 | self._recorded_calls['queue_call'].append((method, args, kwargs)) |
| 21 | |
| 22 | |
showard | 2aafd90 | 2009-10-14 16:20:14 +0000 | [diff] [blame] | 23 | def call(self, method, *args, **kwargs): |
| 24 | # don't bother differentiating between call() and queue_call() |
| 25 | return self.queue_call(method, *args, **kwargs) |
| 26 | |
| 27 | |
showard | 42d4498 | 2009-10-12 20:34:03 +0000 | [diff] [blame] | 28 | def send_file_to(self, drone, source_path, destination_path, |
| 29 | can_fail=False): |
| 30 | self._recorded_calls['send_file_to'].append( |
| 31 | (drone, source_path, destination_path)) |
| 32 | |
| 33 | |
| 34 | # method for use by tests |
| 35 | def _check_for_recorded_call(self, method_name, arguments): |
| 36 | recorded_arg_list = self._recorded_calls[method_name] |
| 37 | was_called = arguments in recorded_arg_list |
| 38 | if not was_called: |
| 39 | print 'Recorded args:', recorded_arg_list |
| 40 | print 'Expected:', arguments |
| 41 | return was_called |
| 42 | |
| 43 | |
| 44 | def was_call_queued(self, method, *args, **kwargs): |
| 45 | return self._check_for_recorded_call('queue_call', |
| 46 | (method, args, kwargs)) |
| 47 | |
| 48 | |
| 49 | def was_file_sent(self, drone, source_path, destination_path): |
| 50 | return self._check_for_recorded_call('send_file_to', |
| 51 | (drone, source_path, |
| 52 | destination_path)) |
showard | e39ebe9 | 2009-06-18 23:14:48 +0000 | [diff] [blame] | 53 | |
| 54 | |
| 55 | class DroneManager(unittest.TestCase): |
showard | 42d4498 | 2009-10-12 20:34:03 +0000 | [diff] [blame] | 56 | _DRONE_INSTALL_DIR = '/drone/install/dir' |
showard | c75fded | 2009-10-14 16:20:02 +0000 | [diff] [blame] | 57 | _DRONE_RESULTS_DIR = os.path.join(_DRONE_INSTALL_DIR, 'results') |
showard | 42d4498 | 2009-10-12 20:34:03 +0000 | [diff] [blame] | 58 | _RESULTS_DIR = '/results/dir' |
| 59 | _SOURCE_PATH = 'source/path' |
| 60 | _DESTINATION_PATH = 'destination/path' |
showard | 2aafd90 | 2009-10-14 16:20:14 +0000 | [diff] [blame] | 61 | _WORKING_DIRECTORY = 'working/directory' |
showard | 42d4498 | 2009-10-12 20:34:03 +0000 | [diff] [blame] | 62 | |
showard | e39ebe9 | 2009-06-18 23:14:48 +0000 | [diff] [blame] | 63 | def setUp(self): |
showard | 42d4498 | 2009-10-12 20:34:03 +0000 | [diff] [blame] | 64 | self.god = mock.mock_god() |
| 65 | self.god.stub_with(drones, 'AUTOTEST_INSTALL_DIR', |
| 66 | self._DRONE_INSTALL_DIR) |
showard | e39ebe9 | 2009-06-18 23:14:48 +0000 | [diff] [blame] | 67 | self.manager = drone_manager.DroneManager() |
showard | 42d4498 | 2009-10-12 20:34:03 +0000 | [diff] [blame] | 68 | self.god.stub_with(self.manager, '_results_dir', self._RESULTS_DIR) |
| 69 | |
showard | 2aafd90 | 2009-10-14 16:20:14 +0000 | [diff] [blame] | 70 | # we don't want this to ever actually get called |
| 71 | self.god.stub_function(drones, 'get_drone') |
| 72 | |
showard | 42d4498 | 2009-10-12 20:34:03 +0000 | [diff] [blame] | 73 | # set up some dummy drones |
showard | 202343e | 2009-10-14 16:20:24 +0000 | [diff] [blame^] | 74 | self.mock_drone = MockDrone('mock_drone') |
showard | 42d4498 | 2009-10-12 20:34:03 +0000 | [diff] [blame] | 75 | self.manager._drones[self.mock_drone.name] = self.mock_drone |
| 76 | self.results_drone = MockDrone('results_drone', 0, 10) |
| 77 | self.manager._results_drone = self.results_drone |
| 78 | |
| 79 | self.mock_drone_process = drone_manager.Process(self.mock_drone.name, 0) |
| 80 | |
| 81 | |
| 82 | def tearDown(self): |
| 83 | self.god.unstub_all() |
showard | e39ebe9 | 2009-06-18 23:14:48 +0000 | [diff] [blame] | 84 | |
| 85 | |
| 86 | def _test_choose_drone_for_execution_helper(self, processes_info_list, |
| 87 | requested_processes): |
| 88 | for index, process_info in enumerate(processes_info_list): |
| 89 | active_processes, max_processes = process_info |
| 90 | self.manager._enqueue_drone(MockDrone(index, active_processes, |
| 91 | max_processes)) |
| 92 | |
| 93 | return self.manager._choose_drone_for_execution(requested_processes) |
| 94 | |
| 95 | |
| 96 | def test_choose_drone_for_execution(self): |
| 97 | drone = self._test_choose_drone_for_execution_helper([(1, 2), (0, 2)], |
| 98 | 1) |
| 99 | self.assertEquals(drone.name, 1) |
| 100 | |
| 101 | |
| 102 | def test_choose_drone_for_execution_some_full(self): |
| 103 | drone = self._test_choose_drone_for_execution_helper([(0, 1), (1, 3)], |
| 104 | 2) |
| 105 | self.assertEquals(drone.name, 1) |
| 106 | |
| 107 | |
| 108 | def test_choose_drone_for_execution_all_full(self): |
| 109 | drone = self._test_choose_drone_for_execution_helper([(2, 1), (3, 2)], |
| 110 | 1) |
| 111 | self.assertEquals(drone.name, 1) |
| 112 | |
| 113 | |
showard | 2aafd90 | 2009-10-14 16:20:14 +0000 | [diff] [blame] | 114 | def test_initialize(self): |
| 115 | self.god.stub_function(drones, 'set_temporary_directory') |
| 116 | results_hostname = 'results_repo' |
| 117 | |
| 118 | drones.set_temporary_directory.expect_call( |
| 119 | os.path.join(self._DRONE_RESULTS_DIR, |
| 120 | drone_utility._TEMPORARY_DIRECTORY)) |
| 121 | (drones.get_drone.expect_call(self.mock_drone.name) |
| 122 | .and_return(self.mock_drone)) |
showard | 202343e | 2009-10-14 16:20:24 +0000 | [diff] [blame^] | 123 | |
| 124 | results_drone = MockDrone('results_drone') |
| 125 | self.god.stub_function(results_drone, 'set_autotest_install_dir') |
| 126 | drones.get_drone.expect_call(results_hostname).and_return(results_drone) |
| 127 | results_drone.set_autotest_install_dir.expect_call(self._RESULTS_DIR) |
showard | 2aafd90 | 2009-10-14 16:20:14 +0000 | [diff] [blame] | 128 | |
| 129 | self.manager.initialize(base_results_dir=self._RESULTS_DIR, |
| 130 | drone_hostnames=[self.mock_drone.name], |
| 131 | results_repository_hostname=results_hostname) |
| 132 | |
| 133 | self.assert_(self.mock_drone.was_call_queued( |
| 134 | 'initialize', self._DRONE_RESULTS_DIR + '/')) |
| 135 | |
| 136 | |
showard | 42d4498 | 2009-10-12 20:34:03 +0000 | [diff] [blame] | 137 | def test_execute_command(self): |
| 138 | self.manager._enqueue_drone(self.mock_drone) |
| 139 | |
showard | 42d4498 | 2009-10-12 20:34:03 +0000 | [diff] [blame] | 140 | pidfile_name = 'my_pidfile' |
| 141 | log_file = 'log_file' |
| 142 | |
| 143 | pidfile_id = self.manager.execute_command( |
| 144 | command=['test', drone_manager.WORKING_DIRECTORY], |
showard | 2aafd90 | 2009-10-14 16:20:14 +0000 | [diff] [blame] | 145 | working_directory=self._WORKING_DIRECTORY, |
showard | 42d4498 | 2009-10-12 20:34:03 +0000 | [diff] [blame] | 146 | pidfile_name=pidfile_name, |
| 147 | log_file=log_file) |
| 148 | |
showard | c75fded | 2009-10-14 16:20:02 +0000 | [diff] [blame] | 149 | full_working_directory = os.path.join(self._DRONE_RESULTS_DIR, |
showard | 2aafd90 | 2009-10-14 16:20:14 +0000 | [diff] [blame] | 150 | self._WORKING_DIRECTORY) |
showard | 42d4498 | 2009-10-12 20:34:03 +0000 | [diff] [blame] | 151 | self.assertEquals(pidfile_id.path, |
| 152 | os.path.join(full_working_directory, pidfile_name)) |
| 153 | self.assert_(self.mock_drone.was_call_queued( |
| 154 | 'execute_command', ['test', full_working_directory], |
| 155 | full_working_directory, |
showard | c75fded | 2009-10-14 16:20:02 +0000 | [diff] [blame] | 156 | os.path.join(self._DRONE_RESULTS_DIR, log_file), pidfile_name)) |
showard | 42d4498 | 2009-10-12 20:34:03 +0000 | [diff] [blame] | 157 | |
| 158 | |
showard | 2aafd90 | 2009-10-14 16:20:14 +0000 | [diff] [blame] | 159 | def test_attach_file_to_execution(self): |
| 160 | self.manager._enqueue_drone(self.mock_drone) |
| 161 | |
| 162 | contents = 'my\ncontents' |
| 163 | attached_path = self.manager.attach_file_to_execution( |
| 164 | self._WORKING_DIRECTORY, contents) |
| 165 | self.manager.execute_command(command=['test'], |
| 166 | working_directory=self._WORKING_DIRECTORY, |
| 167 | pidfile_name='mypidfile') |
| 168 | |
| 169 | self.assert_(self.mock_drone.was_call_queued( |
| 170 | 'write_to_file', |
| 171 | os.path.join(self._DRONE_RESULTS_DIR, attached_path), |
| 172 | contents)) |
| 173 | |
| 174 | |
showard | 42d4498 | 2009-10-12 20:34:03 +0000 | [diff] [blame] | 175 | def test_copy_results_on_drone(self): |
| 176 | self.manager.copy_results_on_drone(self.mock_drone_process, |
| 177 | self._SOURCE_PATH, |
| 178 | self._DESTINATION_PATH) |
| 179 | self.assert_(self.mock_drone.was_call_queued( |
| 180 | 'copy_file_or_directory', |
showard | c75fded | 2009-10-14 16:20:02 +0000 | [diff] [blame] | 181 | os.path.join(self._DRONE_RESULTS_DIR, self._SOURCE_PATH), |
| 182 | os.path.join(self._DRONE_RESULTS_DIR, self._DESTINATION_PATH))) |
showard | 42d4498 | 2009-10-12 20:34:03 +0000 | [diff] [blame] | 183 | |
| 184 | |
| 185 | def test_copy_to_results_repository(self): |
| 186 | self.manager.copy_to_results_repository(self.mock_drone_process, |
| 187 | self._SOURCE_PATH) |
| 188 | self.assert_(self.mock_drone.was_file_sent( |
| 189 | self.results_drone, |
showard | c75fded | 2009-10-14 16:20:02 +0000 | [diff] [blame] | 190 | os.path.join(self._DRONE_RESULTS_DIR, self._SOURCE_PATH), |
showard | 42d4498 | 2009-10-12 20:34:03 +0000 | [diff] [blame] | 191 | os.path.join(self._RESULTS_DIR, self._SOURCE_PATH))) |
| 192 | |
| 193 | |
| 194 | def test_write_lines_to_file(self): |
| 195 | file_path = 'file/path' |
| 196 | lines = ['line1', 'line2'] |
| 197 | written_data = 'line1\nline2\n' |
| 198 | |
| 199 | # write to results repository |
| 200 | self.manager.write_lines_to_file(file_path, lines) |
| 201 | self.assert_(self.results_drone.was_call_queued( |
| 202 | 'write_to_file', os.path.join(self._RESULTS_DIR, file_path), |
| 203 | written_data)) |
| 204 | |
| 205 | # write to a drone |
| 206 | self.manager.write_lines_to_file( |
| 207 | file_path, lines, paired_with_process=self.mock_drone_process) |
| 208 | self.assert_(self.mock_drone.was_call_queued( |
| 209 | 'write_to_file', |
showard | c75fded | 2009-10-14 16:20:02 +0000 | [diff] [blame] | 210 | os.path.join(self._DRONE_RESULTS_DIR, file_path), written_data)) |
showard | 42d4498 | 2009-10-12 20:34:03 +0000 | [diff] [blame] | 211 | |
| 212 | |
showard | e39ebe9 | 2009-06-18 23:14:48 +0000 | [diff] [blame] | 213 | if __name__ == '__main__': |
| 214 | unittest.main() |