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 | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 6 | from autotest_lib.frontend.planner import planner_test_utils, model_attributes |
| 7 | from autotest_lib.frontend.planner import rpc_interface, models, rpc_utils |
jamesren | 4be631f | 2010-04-08 23:01:22 +0000 | [diff] [blame^] | 8 | from autotest_lib.frontend.planner import failure_actions |
jamesren | c394022 | 2010-02-19 21:57:37 +0000 | [diff] [blame] | 9 | from autotest_lib.frontend.afe import model_logic |
| 10 | from autotest_lib.frontend.afe import models as afe_models |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 11 | from autotest_lib.frontend.tko import models as tko_models |
jamesren | c394022 | 2010-02-19 21:57:37 +0000 | [diff] [blame] | 12 | |
| 13 | |
jamesren | dbeebf8 | 2010-04-08 22:58:26 +0000 | [diff] [blame] | 14 | class DummyTestConfig(object): |
| 15 | def __init__(self): |
| 16 | self.id = object() |
| 17 | self.alias = object() |
| 18 | |
| 19 | |
jamesren | c394022 | 2010-02-19 21:57:37 +0000 | [diff] [blame] | 20 | class RpcInterfaceTest(unittest.TestCase, |
| 21 | planner_test_utils.PlannerTestMixin): |
| 22 | def setUp(self): |
| 23 | self._planner_common_setup() |
| 24 | self.god.stub_function(rpc_utils, 'start_plan') |
| 25 | |
| 26 | |
| 27 | def tearDown(self): |
| 28 | self._planner_common_teardown() |
| 29 | |
| 30 | |
| 31 | def test_submit_plan_success(self): |
| 32 | hosts = ('host1', 'host2') |
| 33 | plan_name = self._PLAN_NAME + '2' |
| 34 | |
| 35 | rpc_utils.start_plan.expect_any_call() |
| 36 | rpc_interface.submit_plan(plan_name, hosts, ('label1',), ()) |
| 37 | |
| 38 | plan = models.Plan.objects.get(name=plan_name) |
| 39 | self.assertEqual( |
| 40 | set(afe_models.Host.objects.filter(hostname__in=hosts)), |
| 41 | set(plan.hosts.all())) |
| 42 | |
| 43 | self.assertEqual(1, plan.host_labels.all().count()) |
| 44 | self.assertEqual(afe_models.Label.objects.get(name='label1'), |
| 45 | plan.host_labels.all()[0]) |
| 46 | self.god.check_playback() |
| 47 | |
| 48 | |
| 49 | def test_submit_plan_duplicate(self): |
| 50 | self.assertRaises( |
| 51 | model_logic.ValidationError, rpc_interface.submit_plan, |
| 52 | self._PLAN_NAME, (), (), ()) |
| 53 | |
| 54 | |
| 55 | def test_submit_plan_bad_host(self): |
| 56 | self.assertRaises( |
| 57 | model_logic.ValidationError, rpc_interface.submit_plan, |
| 58 | self._PLAN_NAME + '2', ('fakehost'), (), ()) |
| 59 | |
| 60 | |
| 61 | def test_submit_plan_bad_label(self): |
| 62 | self.assertRaises( |
| 63 | model_logic.ValidationError, rpc_interface.submit_plan, |
| 64 | self._PLAN_NAME + '2', (), ('fakelabel'), ()) |
| 65 | |
| 66 | |
| 67 | def test_get_hosts(self): |
| 68 | hosts = rpc_interface.get_hosts(self._PLAN_NAME) |
| 69 | self.assertEqual(set(('host1', 'host2')), set(hosts)) |
| 70 | |
| 71 | afe_models.Host.objects.get(hostname='host3').labels.add( |
| 72 | afe_models.Label.objects.get(name='label1')) |
| 73 | |
| 74 | hosts = rpc_interface.get_hosts(self._PLAN_NAME) |
| 75 | self.assertEqual(set(('host1', 'host2', 'host3')), set(hosts)) |
| 76 | |
| 77 | afe_models.Host.objects.get(hostname='host3').labels.clear() |
| 78 | |
| 79 | hosts = rpc_interface.get_hosts(self._PLAN_NAME) |
| 80 | self.assertEqual(set(('host1', 'host2')), set(hosts)) |
| 81 | |
| 82 | |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 83 | def test_get_next_test_configs(self): |
jamesren | dbeebf8 | 2010-04-08 22:58:26 +0000 | [diff] [blame] | 84 | DUMMY_CONFIGS = {'host1': DummyTestConfig(), |
| 85 | 'host2': DummyTestConfig()} |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 86 | DUMMY_COMPLETE = object() |
| 87 | self.god.stub_function(rpc_utils, 'compute_next_test_config') |
| 88 | |
| 89 | for host in models.Host.objects.filter(plan=self._plan): |
| 90 | rpc_utils.compute_next_test_config.expect_call( |
| 91 | self._plan, host).and_return( |
| 92 | DUMMY_CONFIGS[host.host.hostname]) |
| 93 | |
| 94 | def _dummy_check_for_completion(plan): |
| 95 | plan.complete = DUMMY_COMPLETE |
| 96 | rpc_utils.check_for_completion = _dummy_check_for_completion |
| 97 | |
| 98 | result = rpc_interface.get_next_test_configs(self._plan.id) |
| 99 | |
| 100 | self.god.check_playback() |
| 101 | self.assertEqual(result['complete'], DUMMY_COMPLETE) |
| 102 | for config in result['next_configs']: |
| 103 | self.assertTrue(config['host'] in DUMMY_CONFIGS) |
| 104 | self.assertEqual(config['next_test_config_id'], |
jamesren | dbeebf8 | 2010-04-08 22:58:26 +0000 | [diff] [blame] | 105 | DUMMY_CONFIGS[config['host']].id) |
| 106 | self.assertEqual(config['next_test_config_alias'], |
| 107 | DUMMY_CONFIGS[config['host']].alias) |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 108 | |
| 109 | |
| 110 | def test_update_test_runs(self): |
jamesren | 8d0d3d5 | 2010-03-25 20:39:13 +0000 | [diff] [blame] | 111 | self._setup_active_plan() |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 112 | |
| 113 | self.god.stub_function(rpc_utils, 'compute_test_run_status') |
| 114 | self.god.stub_function(rpc_utils, 'add_test_run') |
| 115 | |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 116 | # No TKO tests |
| 117 | self.assertEqual([], rpc_interface.update_test_runs(self._plan.id)) |
| 118 | self.god.check_playback() |
| 119 | |
| 120 | # active TKO test |
jamesren | 8d0d3d5 | 2010-03-25 20:39:13 +0000 | [diff] [blame] | 121 | tko_test = tko_models.Test.objects.create(job=self._tko_job, |
| 122 | machine=self._tko_machine, |
| 123 | kernel=self._tko_kernel, |
| 124 | status=self._running_status) |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 125 | |
| 126 | rpc_utils.compute_test_run_status.expect_call( |
jamesren | 8d0d3d5 | 2010-03-25 20:39:13 +0000 | [diff] [blame] | 127 | self.RUNNING_STATUS_WORD).and_return( |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 128 | model_attributes.TestRunStatus.ACTIVE) |
| 129 | rpc_utils.add_test_run.expect_call( |
jamesren | 8d0d3d5 | 2010-03-25 20:39:13 +0000 | [diff] [blame] | 130 | self._plan, self._planner_job, tko_test, self._hostname, |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 131 | model_attributes.TestRunStatus.ACTIVE) |
| 132 | self.assertEqual(rpc_interface.update_test_runs(self._plan.id), |
| 133 | [{'status': model_attributes.TestRunStatus.ACTIVE, |
| 134 | 'tko_test_idx': tko_test.test_idx, |
jamesren | 8d0d3d5 | 2010-03-25 20:39:13 +0000 | [diff] [blame] | 135 | 'hostname': self._hostname}]) |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 136 | self.god.check_playback() |
| 137 | test_run = models.TestRun.objects.create( |
jamesren | 8d0d3d5 | 2010-03-25 20:39:13 +0000 | [diff] [blame] | 138 | plan=self._plan, test_job=self._planner_job, |
| 139 | tko_test=tko_test, host=self._planner_host, |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 140 | status=model_attributes.TestRunStatus.ACTIVE) |
| 141 | |
| 142 | # no change to TKO test |
| 143 | rpc_utils.compute_test_run_status.expect_call( |
jamesren | 8d0d3d5 | 2010-03-25 20:39:13 +0000 | [diff] [blame] | 144 | self.RUNNING_STATUS_WORD).and_return( |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 145 | model_attributes.TestRunStatus.ACTIVE) |
| 146 | self.assertEqual([], rpc_interface.update_test_runs(self._plan.id)) |
| 147 | self.god.check_playback() |
| 148 | |
| 149 | # TKO test is now complete, passed |
jamesren | 8d0d3d5 | 2010-03-25 20:39:13 +0000 | [diff] [blame] | 150 | tko_test.status = self._good_status |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 151 | tko_test.save() |
| 152 | |
| 153 | rpc_utils.compute_test_run_status.expect_call( |
jamesren | 8d0d3d5 | 2010-03-25 20:39:13 +0000 | [diff] [blame] | 154 | self.GOOD_STATUS_WORD).and_return( |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 155 | model_attributes.TestRunStatus.PASSED) |
| 156 | rpc_utils.add_test_run.expect_call( |
jamesren | 8d0d3d5 | 2010-03-25 20:39:13 +0000 | [diff] [blame] | 157 | self._plan, self._planner_job, tko_test, self._hostname, |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 158 | model_attributes.TestRunStatus.PASSED) |
| 159 | self.assertEqual(rpc_interface.update_test_runs(self._plan.id), |
| 160 | [{'status': model_attributes.TestRunStatus.PASSED, |
| 161 | 'tko_test_idx': tko_test.test_idx, |
jamesren | 8d0d3d5 | 2010-03-25 20:39:13 +0000 | [diff] [blame] | 162 | 'hostname': self._hostname}]) |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 163 | self.god.check_playback() |
| 164 | |
| 165 | |
jamesren | 4be631f | 2010-04-08 23:01:22 +0000 | [diff] [blame^] | 166 | def test_process_failure(self): |
| 167 | self._setup_active_plan() |
| 168 | tko_test = tko_models.Test.objects.create(job=self._tko_job, |
| 169 | machine=self._tko_machine, |
| 170 | kernel=self._tko_kernel, |
| 171 | status=self._running_status) |
| 172 | failure = models.TestRun.objects.create( |
| 173 | plan=self._plan, |
| 174 | test_job=self._planner_job, |
| 175 | tko_test=tko_test, |
| 176 | host=self._planner_host, |
| 177 | status=model_attributes.TestRunStatus.FAILED, |
| 178 | finalized=True, seen=False, triaged=False) |
| 179 | host_action = failure_actions.HostAction.UNBLOCK |
| 180 | test_action = failure_actions.TestAction.SKIP |
| 181 | labels = ['label1', 'label2'] |
| 182 | keyvals = {'key1': 'value1', |
| 183 | 'key2': 'value2'} |
| 184 | bugs = ['bug1', 'bug2'] |
| 185 | reason = 'overriden reason' |
| 186 | invalidate = True |
| 187 | |
| 188 | self.god.stub_function(rpc_utils, 'process_host_action') |
| 189 | self.god.stub_function(rpc_utils, 'process_test_action') |
| 190 | |
| 191 | rpc_utils.process_host_action.expect_call(self._planner_host, |
| 192 | host_action) |
| 193 | rpc_utils.process_test_action.expect_call(self._planner_job, |
| 194 | test_action) |
| 195 | |
| 196 | rpc_interface.process_failure(failure.id, host_action, test_action, |
| 197 | labels, keyvals, bugs, reason, invalidate) |
| 198 | failure = models.TestRun.objects.get(id=failure.id) |
| 199 | |
| 200 | self.assertEqual( |
| 201 | set(failure.tko_test.testlabel_set.all()), |
| 202 | set(tko_models.TestLabel.objects.filter(name__in=labels))) |
| 203 | self.assertEqual( |
| 204 | set(failure.tko_test.job.jobkeyval_set.all()), |
| 205 | set(tko_models.JobKeyval.objects.filter( |
| 206 | key__in=keyvals.iterkeys()))) |
| 207 | self.assertEqual(set(failure.bugs.all()), |
| 208 | set(models.Bug.objects.filter(external_uid__in=bugs))) |
| 209 | self.assertEqual(failure.tko_test.reason, reason) |
| 210 | self.assertEqual(failure.invalidated, invalidate) |
| 211 | self.assertTrue(failure.seen) |
| 212 | self.assertTrue(failure.triaged) |
| 213 | self.god.check_playback() |
| 214 | |
| 215 | |
jamesren | c394022 | 2010-02-19 21:57:37 +0000 | [diff] [blame] | 216 | if __name__ == '__main__': |
| 217 | unittest.main() |