blob: 9dd345becf93f15fd3b50e962a321996642108cd [file] [log] [blame]
jamesrenc3940222010-02-19 21:57:37 +00001#!/usr/bin/python
2
3import unittest
4import common
5from autotest_lib.frontend import setup_django_environment
jamesren62758242010-04-28 18:08:25 +00006from autotest_lib.frontend import setup_test_environment
jamesren3e9f6092010-03-11 21:32:10 +00007from autotest_lib.frontend.planner import planner_test_utils, model_attributes
8from autotest_lib.frontend.planner import rpc_interface, models, rpc_utils
jamesren4be631f2010-04-08 23:01:22 +00009from autotest_lib.frontend.planner import failure_actions
jamesren9a6f5f62010-05-05 22:55:54 +000010from autotest_lib.frontend.afe import model_logic, models as afe_models
11from autotest_lib.frontend.afe import rpc_interface as afe_rpc_interface
jamesren3e9f6092010-03-11 21:32:10 +000012from autotest_lib.frontend.tko import models as tko_models
jamesrenc3940222010-02-19 21:57:37 +000013
14
jamesrendbeebf82010-04-08 22:58:26 +000015class DummyTestConfig(object):
16 def __init__(self):
17 self.id = object()
18 self.alias = object()
19
20
jamesrenc3940222010-02-19 21:57:37 +000021class 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
jamesren3e9f6092010-03-11 21:32:10 +000084 def test_get_next_test_configs(self):
jamesrendbeebf82010-04-08 22:58:26 +000085 DUMMY_CONFIGS = {'host1': DummyTestConfig(),
86 'host2': DummyTestConfig()}
jamesren3e9f6092010-03-11 21:32:10 +000087 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'],
jamesrendbeebf82010-04-08 22:58:26 +0000106 DUMMY_CONFIGS[config['host']].id)
107 self.assertEqual(config['next_test_config_alias'],
108 DUMMY_CONFIGS[config['host']].alias)
jamesren3e9f6092010-03-11 21:32:10 +0000109
110
111 def test_update_test_runs(self):
jamesren8d0d3d52010-03-25 20:39:13 +0000112 self._setup_active_plan()
jamesren3e9f6092010-03-11 21:32:10 +0000113
114 self.god.stub_function(rpc_utils, 'compute_test_run_status')
115 self.god.stub_function(rpc_utils, 'add_test_run')
116
jamesren3e9f6092010-03-11 21:32:10 +0000117 # No TKO tests
118 self.assertEqual([], rpc_interface.update_test_runs(self._plan.id))
119 self.god.check_playback()
120
121 # active TKO test
jamesren8d0d3d52010-03-25 20:39:13 +0000122 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)
jamesren3e9f6092010-03-11 21:32:10 +0000126
127 rpc_utils.compute_test_run_status.expect_call(
jamesren8d0d3d52010-03-25 20:39:13 +0000128 self.RUNNING_STATUS_WORD).and_return(
jamesren3e9f6092010-03-11 21:32:10 +0000129 model_attributes.TestRunStatus.ACTIVE)
130 rpc_utils.add_test_run.expect_call(
jamesren8d0d3d52010-03-25 20:39:13 +0000131 self._plan, self._planner_job, tko_test, self._hostname,
jamesren3e9f6092010-03-11 21:32:10 +0000132 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,
jamesren8d0d3d52010-03-25 20:39:13 +0000136 'hostname': self._hostname}])
jamesren3e9f6092010-03-11 21:32:10 +0000137 self.god.check_playback()
138 test_run = models.TestRun.objects.create(
jamesren8d0d3d52010-03-25 20:39:13 +0000139 plan=self._plan, test_job=self._planner_job,
140 tko_test=tko_test, host=self._planner_host,
jamesren3e9f6092010-03-11 21:32:10 +0000141 status=model_attributes.TestRunStatus.ACTIVE)
142
143 # no change to TKO test
144 rpc_utils.compute_test_run_status.expect_call(
jamesren8d0d3d52010-03-25 20:39:13 +0000145 self.RUNNING_STATUS_WORD).and_return(
jamesren3e9f6092010-03-11 21:32:10 +0000146 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
jamesren8d0d3d52010-03-25 20:39:13 +0000151 tko_test.status = self._good_status
jamesren3e9f6092010-03-11 21:32:10 +0000152 tko_test.save()
153
154 rpc_utils.compute_test_run_status.expect_call(
jamesren8d0d3d52010-03-25 20:39:13 +0000155 self.GOOD_STATUS_WORD).and_return(
jamesren3e9f6092010-03-11 21:32:10 +0000156 model_attributes.TestRunStatus.PASSED)
157 rpc_utils.add_test_run.expect_call(
jamesren8d0d3d52010-03-25 20:39:13 +0000158 self._plan, self._planner_job, tko_test, self._hostname,
jamesren3e9f6092010-03-11 21:32:10 +0000159 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,
jamesren8d0d3d52010-03-25 20:39:13 +0000163 'hostname': self._hostname}])
jamesren3e9f6092010-03-11 21:32:10 +0000164 self.god.check_playback()
165
166
jamesren9a6f5f62010-05-05 22:55:54 +0000167 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
jamesren9af703a2010-05-06 19:41:21 +0000189 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
jamesrenc3940222010-02-19 21:57:37 +0000235if __name__ == '__main__':
236 unittest.main()