blob: 02d5fe3b2c544f2c15c3e9c06b89d03e3315da23 [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
showard42d44982009-10-12 20:34:03 +00005from autotest_lib.client.common_lib.test_utils import mock
showarde39ebe92009-06-18 23:14:48 +00006from autotest_lib.scheduler import drone_manager, drones
7
8class MockDrone(drones._AbstractDrone):
9 def __init__(self, name, active_processes, max_processes):
10 super(MockDrone, self).__init__()
11 self.name = name
12 self.active_processes = active_processes
13 self.max_processes = max_processes
showard42d44982009-10-12 20:34:03 +000014 # 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
23 def send_file_to(self, drone, source_path, destination_path,
24 can_fail=False):
25 self._recorded_calls['send_file_to'].append(
26 (drone, source_path, destination_path))
27
28
29 # method for use by tests
30 def _check_for_recorded_call(self, method_name, arguments):
31 recorded_arg_list = self._recorded_calls[method_name]
32 was_called = arguments in recorded_arg_list
33 if not was_called:
34 print 'Recorded args:', recorded_arg_list
35 print 'Expected:', arguments
36 return was_called
37
38
39 def was_call_queued(self, method, *args, **kwargs):
40 return self._check_for_recorded_call('queue_call',
41 (method, args, kwargs))
42
43
44 def was_file_sent(self, drone, source_path, destination_path):
45 return self._check_for_recorded_call('send_file_to',
46 (drone, source_path,
47 destination_path))
showarde39ebe92009-06-18 23:14:48 +000048
49
50class DroneManager(unittest.TestCase):
showard42d44982009-10-12 20:34:03 +000051 _DRONE_INSTALL_DIR = '/drone/install/dir'
52 _RESULTS_DIR = '/results/dir'
53 _SOURCE_PATH = 'source/path'
54 _DESTINATION_PATH = 'destination/path'
55
showarde39ebe92009-06-18 23:14:48 +000056 def setUp(self):
showard42d44982009-10-12 20:34:03 +000057 self.god = mock.mock_god()
58 self.god.stub_with(drones, 'AUTOTEST_INSTALL_DIR',
59 self._DRONE_INSTALL_DIR)
showarde39ebe92009-06-18 23:14:48 +000060 self.manager = drone_manager.DroneManager()
showard42d44982009-10-12 20:34:03 +000061 self.god.stub_with(self.manager, '_results_dir', self._RESULTS_DIR)
62
63 # set up some dummy drones
64 self.mock_drone = MockDrone('mock_drone', 0, 10)
65 self.manager._drones[self.mock_drone.name] = self.mock_drone
66 self.results_drone = MockDrone('results_drone', 0, 10)
67 self.manager._results_drone = self.results_drone
68
69 self.mock_drone_process = drone_manager.Process(self.mock_drone.name, 0)
70
71
72 def tearDown(self):
73 self.god.unstub_all()
showarde39ebe92009-06-18 23:14:48 +000074
75
76 def _test_choose_drone_for_execution_helper(self, processes_info_list,
77 requested_processes):
78 for index, process_info in enumerate(processes_info_list):
79 active_processes, max_processes = process_info
80 self.manager._enqueue_drone(MockDrone(index, active_processes,
81 max_processes))
82
83 return self.manager._choose_drone_for_execution(requested_processes)
84
85
86 def test_choose_drone_for_execution(self):
87 drone = self._test_choose_drone_for_execution_helper([(1, 2), (0, 2)],
88 1)
89 self.assertEquals(drone.name, 1)
90
91
92 def test_choose_drone_for_execution_some_full(self):
93 drone = self._test_choose_drone_for_execution_helper([(0, 1), (1, 3)],
94 2)
95 self.assertEquals(drone.name, 1)
96
97
98 def test_choose_drone_for_execution_all_full(self):
99 drone = self._test_choose_drone_for_execution_helper([(2, 1), (3, 2)],
100 1)
101 self.assertEquals(drone.name, 1)
102
103
showard42d44982009-10-12 20:34:03 +0000104 def test_execute_command(self):
105 self.manager._enqueue_drone(self.mock_drone)
106
107 working_directory = 'working/directory'
108 pidfile_name = 'my_pidfile'
109 log_file = 'log_file'
110
111 pidfile_id = self.manager.execute_command(
112 command=['test', drone_manager.WORKING_DIRECTORY],
113 working_directory=working_directory,
114 pidfile_name=pidfile_name,
115 log_file=log_file)
116
117 full_working_directory = os.path.join(self._DRONE_INSTALL_DIR,
118 working_directory)
119 self.assertEquals(pidfile_id.path,
120 os.path.join(full_working_directory, pidfile_name))
121 self.assert_(self.mock_drone.was_call_queued(
122 'execute_command', ['test', full_working_directory],
123 full_working_directory,
124 os.path.join(self._DRONE_INSTALL_DIR, log_file), pidfile_name))
125
126
127 def test_copy_results_on_drone(self):
128 self.manager.copy_results_on_drone(self.mock_drone_process,
129 self._SOURCE_PATH,
130 self._DESTINATION_PATH)
131 self.assert_(self.mock_drone.was_call_queued(
132 'copy_file_or_directory',
133 os.path.join(self._DRONE_INSTALL_DIR, self._SOURCE_PATH),
134 os.path.join(self._DRONE_INSTALL_DIR, self._DESTINATION_PATH)))
135
136
137 def test_copy_to_results_repository(self):
138 self.manager.copy_to_results_repository(self.mock_drone_process,
139 self._SOURCE_PATH)
140 self.assert_(self.mock_drone.was_file_sent(
141 self.results_drone,
142 os.path.join(self._DRONE_INSTALL_DIR, self._SOURCE_PATH),
143 os.path.join(self._RESULTS_DIR, self._SOURCE_PATH)))
144
145
146 def test_write_lines_to_file(self):
147 file_path = 'file/path'
148 lines = ['line1', 'line2']
149 written_data = 'line1\nline2\n'
150
151 # write to results repository
152 self.manager.write_lines_to_file(file_path, lines)
153 self.assert_(self.results_drone.was_call_queued(
154 'write_to_file', os.path.join(self._RESULTS_DIR, file_path),
155 written_data))
156
157 # write to a drone
158 self.manager.write_lines_to_file(
159 file_path, lines, paired_with_process=self.mock_drone_process)
160 self.assert_(self.mock_drone.was_call_queued(
161 'write_to_file',
162 os.path.join(self._DRONE_INSTALL_DIR, file_path), written_data))
163
164
showarde39ebe92009-06-18 23:14:48 +0000165if __name__ == '__main__':
166 unittest.main()