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 | c394022 | 2010-02-19 21:57:37 +0000 | [diff] [blame] | 8 | from autotest_lib.frontend.afe import model_logic |
| 9 | from autotest_lib.frontend.afe import models as afe_models |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 10 | from autotest_lib.frontend.tko import models as tko_models |
jamesren | c394022 | 2010-02-19 21:57:37 +0000 | [diff] [blame] | 11 | |
| 12 | |
jamesren | dbeebf8 | 2010-04-08 22:58:26 +0000 | [diff] [blame^] | 13 | class DummyTestConfig(object): |
| 14 | def __init__(self): |
| 15 | self.id = object() |
| 16 | self.alias = object() |
| 17 | |
| 18 | |
jamesren | c394022 | 2010-02-19 21:57:37 +0000 | [diff] [blame] | 19 | class RpcInterfaceTest(unittest.TestCase, |
| 20 | planner_test_utils.PlannerTestMixin): |
| 21 | def setUp(self): |
| 22 | self._planner_common_setup() |
| 23 | self.god.stub_function(rpc_utils, 'start_plan') |
| 24 | |
| 25 | |
| 26 | def tearDown(self): |
| 27 | self._planner_common_teardown() |
| 28 | |
| 29 | |
| 30 | def test_submit_plan_success(self): |
| 31 | hosts = ('host1', 'host2') |
| 32 | plan_name = self._PLAN_NAME + '2' |
| 33 | |
| 34 | rpc_utils.start_plan.expect_any_call() |
| 35 | rpc_interface.submit_plan(plan_name, hosts, ('label1',), ()) |
| 36 | |
| 37 | plan = models.Plan.objects.get(name=plan_name) |
| 38 | self.assertEqual( |
| 39 | set(afe_models.Host.objects.filter(hostname__in=hosts)), |
| 40 | set(plan.hosts.all())) |
| 41 | |
| 42 | self.assertEqual(1, plan.host_labels.all().count()) |
| 43 | self.assertEqual(afe_models.Label.objects.get(name='label1'), |
| 44 | plan.host_labels.all()[0]) |
| 45 | self.god.check_playback() |
| 46 | |
| 47 | |
| 48 | def test_submit_plan_duplicate(self): |
| 49 | self.assertRaises( |
| 50 | model_logic.ValidationError, rpc_interface.submit_plan, |
| 51 | self._PLAN_NAME, (), (), ()) |
| 52 | |
| 53 | |
| 54 | def test_submit_plan_bad_host(self): |
| 55 | self.assertRaises( |
| 56 | model_logic.ValidationError, rpc_interface.submit_plan, |
| 57 | self._PLAN_NAME + '2', ('fakehost'), (), ()) |
| 58 | |
| 59 | |
| 60 | def test_submit_plan_bad_label(self): |
| 61 | self.assertRaises( |
| 62 | model_logic.ValidationError, rpc_interface.submit_plan, |
| 63 | self._PLAN_NAME + '2', (), ('fakelabel'), ()) |
| 64 | |
| 65 | |
| 66 | def test_get_hosts(self): |
| 67 | hosts = rpc_interface.get_hosts(self._PLAN_NAME) |
| 68 | self.assertEqual(set(('host1', 'host2')), set(hosts)) |
| 69 | |
| 70 | afe_models.Host.objects.get(hostname='host3').labels.add( |
| 71 | afe_models.Label.objects.get(name='label1')) |
| 72 | |
| 73 | hosts = rpc_interface.get_hosts(self._PLAN_NAME) |
| 74 | self.assertEqual(set(('host1', 'host2', 'host3')), set(hosts)) |
| 75 | |
| 76 | afe_models.Host.objects.get(hostname='host3').labels.clear() |
| 77 | |
| 78 | hosts = rpc_interface.get_hosts(self._PLAN_NAME) |
| 79 | self.assertEqual(set(('host1', 'host2')), set(hosts)) |
| 80 | |
| 81 | |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 82 | def test_get_next_test_configs(self): |
jamesren | dbeebf8 | 2010-04-08 22:58:26 +0000 | [diff] [blame^] | 83 | DUMMY_CONFIGS = {'host1': DummyTestConfig(), |
| 84 | 'host2': DummyTestConfig()} |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 85 | DUMMY_COMPLETE = object() |
| 86 | self.god.stub_function(rpc_utils, 'compute_next_test_config') |
| 87 | |
| 88 | for host in models.Host.objects.filter(plan=self._plan): |
| 89 | rpc_utils.compute_next_test_config.expect_call( |
| 90 | self._plan, host).and_return( |
| 91 | DUMMY_CONFIGS[host.host.hostname]) |
| 92 | |
| 93 | def _dummy_check_for_completion(plan): |
| 94 | plan.complete = DUMMY_COMPLETE |
| 95 | rpc_utils.check_for_completion = _dummy_check_for_completion |
| 96 | |
| 97 | result = rpc_interface.get_next_test_configs(self._plan.id) |
| 98 | |
| 99 | self.god.check_playback() |
| 100 | self.assertEqual(result['complete'], DUMMY_COMPLETE) |
| 101 | for config in result['next_configs']: |
| 102 | self.assertTrue(config['host'] in DUMMY_CONFIGS) |
| 103 | self.assertEqual(config['next_test_config_id'], |
jamesren | dbeebf8 | 2010-04-08 22:58:26 +0000 | [diff] [blame^] | 104 | DUMMY_CONFIGS[config['host']].id) |
| 105 | self.assertEqual(config['next_test_config_alias'], |
| 106 | DUMMY_CONFIGS[config['host']].alias) |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 107 | |
| 108 | |
| 109 | def test_update_test_runs(self): |
jamesren | 8d0d3d5 | 2010-03-25 20:39:13 +0000 | [diff] [blame] | 110 | self._setup_active_plan() |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 111 | |
| 112 | self.god.stub_function(rpc_utils, 'compute_test_run_status') |
| 113 | self.god.stub_function(rpc_utils, 'add_test_run') |
| 114 | |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 115 | # No TKO tests |
| 116 | self.assertEqual([], rpc_interface.update_test_runs(self._plan.id)) |
| 117 | self.god.check_playback() |
| 118 | |
| 119 | # active TKO test |
jamesren | 8d0d3d5 | 2010-03-25 20:39:13 +0000 | [diff] [blame] | 120 | tko_test = tko_models.Test.objects.create(job=self._tko_job, |
| 121 | machine=self._tko_machine, |
| 122 | kernel=self._tko_kernel, |
| 123 | status=self._running_status) |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 124 | |
| 125 | rpc_utils.compute_test_run_status.expect_call( |
jamesren | 8d0d3d5 | 2010-03-25 20:39:13 +0000 | [diff] [blame] | 126 | self.RUNNING_STATUS_WORD).and_return( |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 127 | model_attributes.TestRunStatus.ACTIVE) |
| 128 | rpc_utils.add_test_run.expect_call( |
jamesren | 8d0d3d5 | 2010-03-25 20:39:13 +0000 | [diff] [blame] | 129 | self._plan, self._planner_job, tko_test, self._hostname, |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 130 | model_attributes.TestRunStatus.ACTIVE) |
| 131 | self.assertEqual(rpc_interface.update_test_runs(self._plan.id), |
| 132 | [{'status': model_attributes.TestRunStatus.ACTIVE, |
| 133 | 'tko_test_idx': tko_test.test_idx, |
jamesren | 8d0d3d5 | 2010-03-25 20:39:13 +0000 | [diff] [blame] | 134 | 'hostname': self._hostname}]) |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 135 | self.god.check_playback() |
| 136 | test_run = models.TestRun.objects.create( |
jamesren | 8d0d3d5 | 2010-03-25 20:39:13 +0000 | [diff] [blame] | 137 | plan=self._plan, test_job=self._planner_job, |
| 138 | tko_test=tko_test, host=self._planner_host, |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 139 | status=model_attributes.TestRunStatus.ACTIVE) |
| 140 | |
| 141 | # no change to TKO test |
| 142 | rpc_utils.compute_test_run_status.expect_call( |
jamesren | 8d0d3d5 | 2010-03-25 20:39:13 +0000 | [diff] [blame] | 143 | self.RUNNING_STATUS_WORD).and_return( |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 144 | model_attributes.TestRunStatus.ACTIVE) |
| 145 | self.assertEqual([], rpc_interface.update_test_runs(self._plan.id)) |
| 146 | self.god.check_playback() |
| 147 | |
| 148 | # TKO test is now complete, passed |
jamesren | 8d0d3d5 | 2010-03-25 20:39:13 +0000 | [diff] [blame] | 149 | tko_test.status = self._good_status |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 150 | tko_test.save() |
| 151 | |
| 152 | rpc_utils.compute_test_run_status.expect_call( |
jamesren | 8d0d3d5 | 2010-03-25 20:39:13 +0000 | [diff] [blame] | 153 | self.GOOD_STATUS_WORD).and_return( |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 154 | model_attributes.TestRunStatus.PASSED) |
| 155 | rpc_utils.add_test_run.expect_call( |
jamesren | 8d0d3d5 | 2010-03-25 20:39:13 +0000 | [diff] [blame] | 156 | self._plan, self._planner_job, tko_test, self._hostname, |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 157 | model_attributes.TestRunStatus.PASSED) |
| 158 | self.assertEqual(rpc_interface.update_test_runs(self._plan.id), |
| 159 | [{'status': model_attributes.TestRunStatus.PASSED, |
| 160 | 'tko_test_idx': tko_test.test_idx, |
jamesren | 8d0d3d5 | 2010-03-25 20:39:13 +0000 | [diff] [blame] | 161 | 'hostname': self._hostname}]) |
jamesren | 3e9f609 | 2010-03-11 21:32:10 +0000 | [diff] [blame] | 162 | self.god.check_playback() |
| 163 | |
| 164 | |
jamesren | c394022 | 2010-02-19 21:57:37 +0000 | [diff] [blame] | 165 | if __name__ == '__main__': |
| 166 | unittest.main() |