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): |
| 102 | GOOD_STATUS_WORD = 'GOOD' |
| 103 | RUNNING_STATUS_WORD = 'RUNNING' |
| 104 | hostname = self.hosts[0].hostname |
| 105 | |
| 106 | self.god.stub_function(rpc_utils, 'compute_test_run_status') |
| 107 | self.god.stub_function(rpc_utils, 'add_test_run') |
| 108 | |
| 109 | control, _ = models.ControlFile.objects.get_or_create( |
| 110 | contents='test_control') |
| 111 | test_config = models.TestConfig.objects.create(plan=self._plan, |
| 112 | alias='config', |
| 113 | control_file=control, |
| 114 | execution_order=1, |
| 115 | estimated_runtime=1) |
| 116 | afe_job = self._create_job(hosts=(1,)) |
| 117 | planner_host = models.Host.objects.create(plan=self._plan, |
| 118 | host=self.hosts[0]) |
| 119 | planner_job = models.Job.objects.create(plan=self._plan, |
| 120 | test_config=test_config, |
| 121 | afe_job=afe_job) |
| 122 | tko_machine = tko_models.Machine.objects.create(hostname=hostname) |
| 123 | tko_job = tko_models.Job.objects.create(tag='job', |
| 124 | machine=tko_machine, |
| 125 | afe_job_id=afe_job.id) |
| 126 | tko_kernel = tko_models.Kernel.objects.create() |
| 127 | running_status = tko_models.Status.objects.create( |
| 128 | word=RUNNING_STATUS_WORD) |
| 129 | good_status = tko_models.Status.objects.create(word=GOOD_STATUS_WORD) |
| 130 | |
| 131 | # No TKO tests |
| 132 | self.assertEqual([], rpc_interface.update_test_runs(self._plan.id)) |
| 133 | self.god.check_playback() |
| 134 | |
| 135 | # active TKO test |
| 136 | tko_test = tko_models.Test.objects.create(job=tko_job, |
| 137 | machine=tko_machine, |
| 138 | kernel=tko_kernel, |
| 139 | status=running_status) |
| 140 | |
| 141 | rpc_utils.compute_test_run_status.expect_call( |
| 142 | RUNNING_STATUS_WORD).and_return( |
| 143 | model_attributes.TestRunStatus.ACTIVE) |
| 144 | rpc_utils.add_test_run.expect_call( |
| 145 | self._plan, planner_job, tko_test, hostname, |
| 146 | model_attributes.TestRunStatus.ACTIVE) |
| 147 | self.assertEqual(rpc_interface.update_test_runs(self._plan.id), |
| 148 | [{'status': model_attributes.TestRunStatus.ACTIVE, |
| 149 | 'tko_test_idx': tko_test.test_idx, |
| 150 | 'hostname': hostname}]) |
| 151 | self.god.check_playback() |
| 152 | test_run = models.TestRun.objects.create( |
| 153 | plan=self._plan, test_job=planner_job, |
| 154 | tko_test=tko_test, host=planner_host, |
| 155 | status=model_attributes.TestRunStatus.ACTIVE) |
| 156 | |
| 157 | # no change to TKO test |
| 158 | rpc_utils.compute_test_run_status.expect_call( |
| 159 | RUNNING_STATUS_WORD).and_return( |
| 160 | model_attributes.TestRunStatus.ACTIVE) |
| 161 | self.assertEqual([], rpc_interface.update_test_runs(self._plan.id)) |
| 162 | self.god.check_playback() |
| 163 | |
| 164 | # TKO test is now complete, passed |
| 165 | tko_test.status = good_status |
| 166 | tko_test.save() |
| 167 | |
| 168 | rpc_utils.compute_test_run_status.expect_call( |
| 169 | GOOD_STATUS_WORD).and_return( |
| 170 | model_attributes.TestRunStatus.PASSED) |
| 171 | rpc_utils.add_test_run.expect_call( |
| 172 | self._plan, planner_job, tko_test, hostname, |
| 173 | model_attributes.TestRunStatus.PASSED) |
| 174 | self.assertEqual(rpc_interface.update_test_runs(self._plan.id), |
| 175 | [{'status': model_attributes.TestRunStatus.PASSED, |
| 176 | 'tko_test_idx': tko_test.test_idx, |
| 177 | 'hostname': hostname}]) |
| 178 | self.god.check_playback() |
| 179 | |
| 180 | |
jamesren | c394022 | 2010-02-19 21:57:37 +0000 | [diff] [blame] | 181 | if __name__ == '__main__': |
| 182 | unittest.main() |