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