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 |
| 6 | from autotest_lib.frontend.planner import planner_test_utils |
| 7 | from autotest_lib.frontend.afe import model_logic |
| 8 | from autotest_lib.frontend.afe import models as afe_models |
| 9 | from autotest_lib.frontend.planner import rpc_interface, models, rpc_utils |
| 10 | |
| 11 | |
| 12 | class RpcInterfaceTest(unittest.TestCase, |
| 13 | planner_test_utils.PlannerTestMixin): |
| 14 | def setUp(self): |
| 15 | self._planner_common_setup() |
| 16 | self.god.stub_function(rpc_utils, 'start_plan') |
| 17 | |
| 18 | |
| 19 | def tearDown(self): |
| 20 | self._planner_common_teardown() |
| 21 | |
| 22 | |
| 23 | def test_submit_plan_success(self): |
| 24 | hosts = ('host1', 'host2') |
| 25 | plan_name = self._PLAN_NAME + '2' |
| 26 | |
| 27 | rpc_utils.start_plan.expect_any_call() |
| 28 | rpc_interface.submit_plan(plan_name, hosts, ('label1',), ()) |
| 29 | |
| 30 | plan = models.Plan.objects.get(name=plan_name) |
| 31 | self.assertEqual( |
| 32 | set(afe_models.Host.objects.filter(hostname__in=hosts)), |
| 33 | set(plan.hosts.all())) |
| 34 | |
| 35 | self.assertEqual(1, plan.host_labels.all().count()) |
| 36 | self.assertEqual(afe_models.Label.objects.get(name='label1'), |
| 37 | plan.host_labels.all()[0]) |
| 38 | self.god.check_playback() |
| 39 | |
| 40 | |
| 41 | def test_submit_plan_duplicate(self): |
| 42 | self.assertRaises( |
| 43 | model_logic.ValidationError, rpc_interface.submit_plan, |
| 44 | self._PLAN_NAME, (), (), ()) |
| 45 | |
| 46 | |
| 47 | def test_submit_plan_bad_host(self): |
| 48 | self.assertRaises( |
| 49 | model_logic.ValidationError, rpc_interface.submit_plan, |
| 50 | self._PLAN_NAME + '2', ('fakehost'), (), ()) |
| 51 | |
| 52 | |
| 53 | def test_submit_plan_bad_label(self): |
| 54 | self.assertRaises( |
| 55 | model_logic.ValidationError, rpc_interface.submit_plan, |
| 56 | self._PLAN_NAME + '2', (), ('fakelabel'), ()) |
| 57 | |
| 58 | |
| 59 | def test_get_hosts(self): |
| 60 | hosts = rpc_interface.get_hosts(self._PLAN_NAME) |
| 61 | self.assertEqual(set(('host1', 'host2')), set(hosts)) |
| 62 | |
| 63 | afe_models.Host.objects.get(hostname='host3').labels.add( |
| 64 | afe_models.Label.objects.get(name='label1')) |
| 65 | |
| 66 | hosts = rpc_interface.get_hosts(self._PLAN_NAME) |
| 67 | self.assertEqual(set(('host1', 'host2', 'host3')), set(hosts)) |
| 68 | |
| 69 | afe_models.Host.objects.get(hostname='host3').labels.clear() |
| 70 | |
| 71 | hosts = rpc_interface.get_hosts(self._PLAN_NAME) |
| 72 | self.assertEqual(set(('host1', 'host2')), set(hosts)) |
| 73 | |
| 74 | |
| 75 | if __name__ == '__main__': |
| 76 | unittest.main() |