jamesren | c394022 | 2010-02-19 21:57:37 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import unittest |
| 4 | import common |
| 5 | from autotest_lib.frontend import setup_django_environment |
jamesren | 6275824 | 2010-04-28 18:08:25 +0000 | [diff] [blame] | 6 | from autotest_lib.frontend import setup_test_environment |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 7 | from autotest_lib.frontend.planner import planner_test_utils, model_attributes |
| 8 | from autotest_lib.frontend.planner import rpc_interface, models, rpc_utils |
jamesren | 4be631f | 2010-04-08 23:01:22 +0000 | [diff] [blame] | 9 | from autotest_lib.frontend.planner import failure_actions |
jamesren | 9a6f5f6 | 2010-05-05 22:55:54 +0000 | [diff] [blame] | 10 | from autotest_lib.frontend.afe import model_logic, models as afe_models |
| 11 | from autotest_lib.frontend.afe import rpc_interface as afe_rpc_interface |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 12 | from autotest_lib.frontend.tko import models as tko_models |
jamesren | c394022 | 2010-02-19 21:57:37 +0000 | [diff] [blame] | 13 | |
| 14 | |
jamesren | dbeebf8 | 2010-04-08 22:58:26 +0000 | [diff] [blame] | 15 | class DummyTestConfig(object): |
| 16 | def __init__(self): |
| 17 | self.id = object() |
| 18 | self.alias = object() |
| 19 | |
| 20 | |
jamesren | c394022 | 2010-02-19 21:57:37 +0000 | [diff] [blame] | 21 | class RpcInterfaceTest(unittest.TestCase, |
| 22 | planner_test_utils.PlannerTestMixin): |
| 23 | def setUp(self): |
| 24 | self._planner_common_setup() |
| 25 | self.god.stub_function(rpc_utils, 'start_plan') |
| 26 | |
| 27 | |
| 28 | def tearDown(self): |
| 29 | self._planner_common_teardown() |
| 30 | |
| 31 | |
| 32 | def test_submit_plan_success(self): |
| 33 | hosts = ('host1', 'host2') |
| 34 | plan_name = self._PLAN_NAME + '2' |
| 35 | |
| 36 | rpc_utils.start_plan.expect_any_call() |
| 37 | rpc_interface.submit_plan(plan_name, hosts, ('label1',), ()) |
| 38 | |
| 39 | plan = models.Plan.objects.get(name=plan_name) |
| 40 | self.assertEqual( |
| 41 | set(afe_models.Host.objects.filter(hostname__in=hosts)), |
| 42 | set(plan.hosts.all())) |
| 43 | |
| 44 | self.assertEqual(1, plan.host_labels.all().count()) |
| 45 | self.assertEqual(afe_models.Label.objects.get(name='label1'), |
| 46 | plan.host_labels.all()[0]) |
| 47 | self.god.check_playback() |
| 48 | |
| 49 | |
| 50 | def test_submit_plan_duplicate(self): |
| 51 | self.assertRaises( |
| 52 | model_logic.ValidationError, rpc_interface.submit_plan, |
| 53 | self._PLAN_NAME, (), (), ()) |
| 54 | |
| 55 | |
| 56 | def test_submit_plan_bad_host(self): |
| 57 | self.assertRaises( |
| 58 | model_logic.ValidationError, rpc_interface.submit_plan, |
| 59 | self._PLAN_NAME + '2', ('fakehost'), (), ()) |
| 60 | |
| 61 | |
| 62 | def test_submit_plan_bad_label(self): |
| 63 | self.assertRaises( |
| 64 | model_logic.ValidationError, rpc_interface.submit_plan, |
| 65 | self._PLAN_NAME + '2', (), ('fakelabel'), ()) |
| 66 | |
| 67 | |
| 68 | def test_get_hosts(self): |
| 69 | hosts = rpc_interface.get_hosts(self._PLAN_NAME) |
| 70 | self.assertEqual(set(('host1', 'host2')), set(hosts)) |
| 71 | |
| 72 | afe_models.Host.objects.get(hostname='host3').labels.add( |
| 73 | afe_models.Label.objects.get(name='label1')) |
| 74 | |
| 75 | hosts = rpc_interface.get_hosts(self._PLAN_NAME) |
| 76 | self.assertEqual(set(('host1', 'host2', 'host3')), set(hosts)) |
| 77 | |
| 78 | afe_models.Host.objects.get(hostname='host3').labels.clear() |
| 79 | |
| 80 | hosts = rpc_interface.get_hosts(self._PLAN_NAME) |
| 81 | self.assertEqual(set(('host1', 'host2')), set(hosts)) |
| 82 | |
| 83 | |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 84 | def test_get_next_test_configs(self): |
jamesren | dbeebf8 | 2010-04-08 22:58:26 +0000 | [diff] [blame] | 85 | DUMMY_CONFIGS = {'host1': DummyTestConfig(), |
| 86 | 'host2': DummyTestConfig()} |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 87 | DUMMY_COMPLETE = object() |
| 88 | self.god.stub_function(rpc_utils, 'compute_next_test_config') |
| 89 | |
| 90 | for host in models.Host.objects.filter(plan=self._plan): |
| 91 | rpc_utils.compute_next_test_config.expect_call( |
| 92 | self._plan, host).and_return( |
| 93 | DUMMY_CONFIGS[host.host.hostname]) |
| 94 | |
| 95 | def _dummy_check_for_completion(plan): |
| 96 | plan.complete = DUMMY_COMPLETE |
| 97 | rpc_utils.check_for_completion = _dummy_check_for_completion |
| 98 | |
| 99 | result = rpc_interface.get_next_test_configs(self._plan.id) |
| 100 | |
| 101 | self.god.check_playback() |
| 102 | self.assertEqual(result['complete'], DUMMY_COMPLETE) |
| 103 | for config in result['next_configs']: |
| 104 | self.assertTrue(config['host'] in DUMMY_CONFIGS) |
| 105 | self.assertEqual(config['next_test_config_id'], |
jamesren | dbeebf8 | 2010-04-08 22:58:26 +0000 | [diff] [blame] | 106 | DUMMY_CONFIGS[config['host']].id) |
| 107 | self.assertEqual(config['next_test_config_alias'], |
| 108 | DUMMY_CONFIGS[config['host']].alias) |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 109 | |
| 110 | |
| 111 | def test_update_test_runs(self): |
jamesren | 8d0d3d5 | 2010-03-25 20:39:13 +0000 | [diff] [blame] | 112 | self._setup_active_plan() |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 113 | |
| 114 | self.god.stub_function(rpc_utils, 'compute_test_run_status') |
| 115 | self.god.stub_function(rpc_utils, 'add_test_run') |
| 116 | |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 117 | # No TKO tests |
| 118 | self.assertEqual([], rpc_interface.update_test_runs(self._plan.id)) |
| 119 | self.god.check_playback() |
| 120 | |
| 121 | # active TKO test |
jamesren | 8d0d3d5 | 2010-03-25 20:39:13 +0000 | [diff] [blame] | 122 | tko_test = tko_models.Test.objects.create(job=self._tko_job, |
| 123 | machine=self._tko_machine, |
| 124 | kernel=self._tko_kernel, |
| 125 | status=self._running_status) |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 126 | |
| 127 | rpc_utils.compute_test_run_status.expect_call( |
jamesren | 8d0d3d5 | 2010-03-25 20:39:13 +0000 | [diff] [blame] | 128 | self.RUNNING_STATUS_WORD).and_return( |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 129 | model_attributes.TestRunStatus.ACTIVE) |
| 130 | rpc_utils.add_test_run.expect_call( |
jamesren | 8d0d3d5 | 2010-03-25 20:39:13 +0000 | [diff] [blame] | 131 | self._plan, self._planner_job, tko_test, self._hostname, |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 132 | model_attributes.TestRunStatus.ACTIVE) |
| 133 | self.assertEqual(rpc_interface.update_test_runs(self._plan.id), |
| 134 | [{'status': model_attributes.TestRunStatus.ACTIVE, |
| 135 | 'tko_test_idx': tko_test.test_idx, |
jamesren | 8d0d3d5 | 2010-03-25 20:39:13 +0000 | [diff] [blame] | 136 | 'hostname': self._hostname}]) |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 137 | self.god.check_playback() |
| 138 | test_run = models.TestRun.objects.create( |
jamesren | 8d0d3d5 | 2010-03-25 20:39:13 +0000 | [diff] [blame] | 139 | plan=self._plan, test_job=self._planner_job, |
| 140 | tko_test=tko_test, host=self._planner_host, |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 141 | status=model_attributes.TestRunStatus.ACTIVE) |
| 142 | |
| 143 | # no change to TKO test |
| 144 | rpc_utils.compute_test_run_status.expect_call( |
jamesren | 8d0d3d5 | 2010-03-25 20:39:13 +0000 | [diff] [blame] | 145 | self.RUNNING_STATUS_WORD).and_return( |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 146 | model_attributes.TestRunStatus.ACTIVE) |
| 147 | self.assertEqual([], rpc_interface.update_test_runs(self._plan.id)) |
| 148 | self.god.check_playback() |
| 149 | |
| 150 | # TKO test is now complete, passed |
jamesren | 8d0d3d5 | 2010-03-25 20:39:13 +0000 | [diff] [blame] | 151 | tko_test.status = self._good_status |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 152 | tko_test.save() |
| 153 | |
| 154 | rpc_utils.compute_test_run_status.expect_call( |
jamesren | 8d0d3d5 | 2010-03-25 20:39:13 +0000 | [diff] [blame] | 155 | self.GOOD_STATUS_WORD).and_return( |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 156 | model_attributes.TestRunStatus.PASSED) |
| 157 | rpc_utils.add_test_run.expect_call( |
jamesren | 8d0d3d5 | 2010-03-25 20:39:13 +0000 | [diff] [blame] | 158 | self._plan, self._planner_job, tko_test, self._hostname, |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 159 | model_attributes.TestRunStatus.PASSED) |
| 160 | self.assertEqual(rpc_interface.update_test_runs(self._plan.id), |
| 161 | [{'status': model_attributes.TestRunStatus.PASSED, |
| 162 | 'tko_test_idx': tko_test.test_idx, |
jamesren | 8d0d3d5 | 2010-03-25 20:39:13 +0000 | [diff] [blame] | 163 | 'hostname': self._hostname}]) |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 164 | self.god.check_playback() |
| 165 | |
| 166 | |
jamesren | 9a6f5f6 | 2010-05-05 22:55:54 +0000 | [diff] [blame] | 167 | def test_generate_test_config(self): |
| 168 | control = {'control_file': object(), |
| 169 | 'is_server': object()} |
| 170 | test = 'test' |
| 171 | alias = 'test alias' |
| 172 | estimated_runtime = object() |
| 173 | |
| 174 | self.god.stub_function(afe_rpc_interface, 'generate_control_file') |
| 175 | afe_rpc_interface.generate_control_file.expect_call( |
| 176 | tests=[test]).and_return(control) |
| 177 | |
| 178 | result = rpc_interface.generate_test_config( |
| 179 | alias=alias, afe_test_name=test, |
| 180 | estimated_runtime=estimated_runtime) |
| 181 | |
| 182 | self.assertEqual(result['alias'], 'test_alias') |
| 183 | self.assertEqual(result['control_file'], control['control_file']) |
| 184 | self.assertEqual(result['is_server'], control['is_server']) |
| 185 | self.assertEqual(result['estimated_runtime'], estimated_runtime) |
| 186 | self.god.check_playback() |
| 187 | |
| 188 | |
jamesren | 9af703a | 2010-05-06 19:41:21 +0000 | [diff] [blame] | 189 | def test_get_machine_view_data(self): |
| 190 | self._setup_active_plan() |
| 191 | |
| 192 | host1_expected = {'machine': 'host1', |
| 193 | 'status': 'Running', |
| 194 | 'tests_run': [], |
| 195 | 'bug_ids': []} |
| 196 | host2_expected = {'machine': 'host2', |
| 197 | 'status': 'Running', |
| 198 | 'tests_run': [], |
| 199 | 'bug_ids': []} |
| 200 | |
| 201 | expected = (host1_expected, host2_expected) |
| 202 | actual = rpc_interface.get_machine_view_data(plan_id=self._plan.id) |
| 203 | self.assertEqual(sorted(actual), sorted(expected)) |
| 204 | |
| 205 | # active TKO test |
| 206 | tko_test = tko_models.Test.objects.create(job=self._tko_job, |
| 207 | test='test', |
| 208 | machine=self._tko_machine, |
| 209 | kernel=self._tko_kernel, |
| 210 | status=self._running_status) |
| 211 | testrun = models.TestRun.objects.create(plan=self._plan, |
| 212 | test_job=self._planner_job, |
| 213 | host=self._planner_host, |
| 214 | tko_test=tko_test, |
| 215 | finalized=True) |
| 216 | |
| 217 | host1_expected['tests_run'] = [{'test_name': 'test', |
| 218 | 'success': False}] |
| 219 | actual = rpc_interface.get_machine_view_data(plan_id=self._plan.id) |
| 220 | self.assertEqual(sorted(actual), sorted(expected)) |
| 221 | |
| 222 | # TKO test complete, passed, with bug filed |
| 223 | tko_test.status = self._good_status |
| 224 | tko_test.save() |
| 225 | bug = models.Bug.objects.create(external_uid='bug') |
| 226 | testrun.bugs.add(bug) |
| 227 | |
| 228 | host1_expected['tests_run'] = [{'test_name': 'test', |
| 229 | 'success': True}] |
| 230 | host1_expected['bug_ids'] = ['bug'] |
| 231 | actual = rpc_interface.get_machine_view_data(plan_id=self._plan.id) |
| 232 | self.assertEqual(sorted(actual), sorted(expected)) |
| 233 | |
| 234 | |
jamesren | c394022 | 2010-02-19 21:57:37 +0000 | [diff] [blame] | 235 | if __name__ == '__main__': |
| 236 | unittest.main() |