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, models as afe_models |
jamesren | 4be631f | 2010-04-08 23:01:22 +0000 | [diff] [blame^] | 8 | from autotest_lib.frontend.planner import models, rpc_utils, failure_actions |
jamesren | 8d0d3d5 | 2010-03-25 20:39:13 +0000 | [diff] [blame] | 9 | from autotest_lib.client.common_lib import utils, host_queue_entry_states |
jamesren | c394022 | 2010-02-19 21:57:37 +0000 | [diff] [blame] | 10 | |
| 11 | |
| 12 | class RpcUtilsTest(unittest.TestCase, |
| 13 | planner_test_utils.PlannerTestMixin): |
| 14 | def setUp(self): |
| 15 | self._planner_common_setup() |
| 16 | |
| 17 | |
| 18 | def tearDown(self): |
| 19 | self._planner_common_teardown() |
| 20 | |
| 21 | |
| 22 | def test_create_plan_label(self): |
| 23 | label, group = self._create_label_helper() |
| 24 | |
| 25 | label.delete() |
| 26 | group.invalid = True |
| 27 | group.save() |
| 28 | |
| 29 | label, group = self._create_label_helper() |
| 30 | |
| 31 | self.assertRaises(model_logic.ValidationError, |
| 32 | rpc_utils.create_plan_label, self._plan) |
| 33 | |
| 34 | |
| 35 | def _create_label_helper(self): |
| 36 | label = rpc_utils.create_plan_label(self._plan) |
| 37 | group = afe_models.AtomicGroup.objects.get( |
| 38 | name=rpc_utils.PLANNER_ATOMIC_GROUP_NAME) |
| 39 | self.assertFalse(group.invalid) |
| 40 | self.assertEqual(label.atomic_group, group) |
| 41 | |
| 42 | return (label, group) |
| 43 | |
| 44 | |
| 45 | def test_lazy_load(self): |
| 46 | self.god.stub_function(utils, 'read_file') |
| 47 | |
| 48 | DUMMY_PATH_1 = object() |
| 49 | DUMMY_PATH_2 = object() |
| 50 | DUMMY_FILE_1 = object() |
| 51 | DUMMY_FILE_2 = object() |
| 52 | |
| 53 | utils.read_file.expect_call(DUMMY_PATH_1).and_return(DUMMY_FILE_1) |
| 54 | self.assertEqual(DUMMY_FILE_1, rpc_utils.lazy_load(DUMMY_PATH_1)) |
| 55 | self.god.check_playback() |
| 56 | |
| 57 | # read_file should not be called again for this path |
| 58 | self.assertEqual(DUMMY_FILE_1, rpc_utils.lazy_load(DUMMY_PATH_1)) |
| 59 | self.god.check_playback() |
| 60 | |
| 61 | # new file; read_file must be called again |
| 62 | utils.read_file.expect_call(DUMMY_PATH_2).and_return(DUMMY_FILE_2) |
| 63 | self.assertEqual(DUMMY_FILE_2, rpc_utils.lazy_load(DUMMY_PATH_2)) |
| 64 | self.god.check_playback() |
| 65 | |
| 66 | |
jamesren | 8d0d3d5 | 2010-03-25 20:39:13 +0000 | [diff] [blame] | 67 | def test_update_hosts_table(self): |
| 68 | label = self.labels[3] |
| 69 | default_hosts = set(self._plan.hosts.all()) |
| 70 | |
| 71 | rpc_utils.update_hosts_table(self._plan) |
| 72 | self.assertEqual(default_hosts, set(self._plan.hosts.all())) |
| 73 | self.assertEqual(set(), self._get_added_by_label_hosts()) |
| 74 | |
| 75 | self._plan.host_labels.add(label) |
| 76 | rpc_utils.update_hosts_table(self._plan) |
| 77 | self.assertEqual(default_hosts.union(label.host_set.all()), |
| 78 | set(self._plan.hosts.all())) |
| 79 | self.assertEqual(set(label.host_set.all()), |
| 80 | self._get_added_by_label_hosts()) |
| 81 | |
| 82 | self._plan.host_labels.remove(label) |
| 83 | rpc_utils.update_hosts_table(self._plan) |
| 84 | self.assertEqual(default_hosts, set(self._plan.hosts.all())) |
| 85 | self.assertEqual(set(), self._get_added_by_label_hosts()) |
| 86 | |
| 87 | |
| 88 | def _get_added_by_label_hosts(self): |
| 89 | return set(host.host for host in models.Host.objects.filter( |
| 90 | plan=self._plan, added_by_label=True)) |
| 91 | |
| 92 | |
| 93 | def test_compute_next_test_config(self): |
| 94 | self._setup_active_plan() |
| 95 | test_config = models.TestConfig.objects.create( |
| 96 | plan=self._plan, alias='config2', control_file=self._control, |
| 97 | execution_order=2, estimated_runtime=1) |
| 98 | |
| 99 | self.assertEqual(1, self._afe_job.hostqueueentry_set.count()) |
| 100 | self.assertEqual( |
| 101 | None, rpc_utils.compute_next_test_config(self._plan, |
| 102 | self._planner_host)) |
| 103 | self.assertFalse(self._planner_host.complete) |
| 104 | |
| 105 | hqe = self._afe_job.hostqueueentry_set.all()[0] |
| 106 | hqe.status = host_queue_entry_states.Status.COMPLETED |
| 107 | hqe.save() |
| 108 | |
| 109 | self.assertEqual( |
jamesren | dbeebf8 | 2010-04-08 22:58:26 +0000 | [diff] [blame] | 110 | test_config, |
jamesren | 8d0d3d5 | 2010-03-25 20:39:13 +0000 | [diff] [blame] | 111 | rpc_utils.compute_next_test_config(self._plan, |
| 112 | self._planner_host)) |
| 113 | self.assertFalse(self._planner_host.complete) |
| 114 | |
| 115 | afe_job = self._create_job(hosts=(1,)) |
| 116 | planner_job = models.Job.objects.create(plan=self._plan, |
| 117 | test_config=test_config, |
| 118 | afe_job=afe_job) |
| 119 | |
| 120 | self.assertEqual( |
| 121 | None, rpc_utils.compute_next_test_config(self._plan, |
| 122 | self._planner_host)) |
| 123 | self.assertFalse(self._planner_host.complete) |
| 124 | |
| 125 | hqe = afe_job.hostqueueentry_set.all()[0] |
| 126 | hqe.status = host_queue_entry_states.Status.COMPLETED |
| 127 | hqe.save() |
| 128 | |
| 129 | self.assertEqual( |
| 130 | None, rpc_utils.compute_next_test_config(self._plan, |
| 131 | self._planner_host)) |
| 132 | self.assertTrue(self._planner_host.complete) |
| 133 | |
| 134 | |
jamesren | 4be631f | 2010-04-08 23:01:22 +0000 | [diff] [blame^] | 135 | def _replace_site_process_host_action(self, replacement): |
| 136 | self.god.stub_function(utils, 'import_site_function') |
| 137 | utils.import_site_function.expect_any_call().and_return(replacement) |
| 138 | |
| 139 | |
| 140 | def _remove_site_process_host_action(self): |
| 141 | def _site_process_host_action_dummy(host, action): |
| 142 | return False |
| 143 | self._replace_site_process_host_action(_site_process_host_action_dummy) |
| 144 | |
| 145 | |
| 146 | def test_process_host_action_block(self): |
| 147 | self._remove_site_process_host_action() |
| 148 | host = models.Host.objects.create(plan=self._plan, host=self.hosts[0], |
| 149 | blocked=False) |
| 150 | assert not host.blocked |
| 151 | |
| 152 | rpc_utils.process_host_action(host, failure_actions.HostAction.BLOCK) |
| 153 | host = models.Host.objects.get(id=host.id) |
| 154 | |
| 155 | self.assertTrue(host.blocked) |
| 156 | self.god.check_playback() |
| 157 | |
| 158 | |
| 159 | def test_process_host_action_unblock(self): |
| 160 | self._remove_site_process_host_action() |
| 161 | host = models.Host.objects.create(plan=self._plan, host=self.hosts[0], |
| 162 | blocked=True) |
| 163 | assert host.blocked |
| 164 | |
| 165 | rpc_utils.process_host_action(host, failure_actions.HostAction.UNBLOCK) |
| 166 | host = models.Host.objects.get(id=host.id) |
| 167 | |
| 168 | self.assertFalse(host.blocked) |
| 169 | self.god.check_playback() |
| 170 | |
| 171 | |
| 172 | def test_process_host_action_site(self): |
| 173 | self._remove_site_process_host_action |
| 174 | action = object() |
| 175 | failure_actions.HostAction.values.append(action) |
| 176 | host = models.Host.objects.create(plan=self._plan, host=self.hosts[0]) |
| 177 | |
| 178 | self.assertRaises(AssertionError, rpc_utils.process_host_action, |
| 179 | host, action) |
| 180 | self.god.check_playback() |
| 181 | |
| 182 | self._called = False |
| 183 | def _site_process_host_action(host, action): |
| 184 | self._called = True |
| 185 | return True |
| 186 | self._replace_site_process_host_action(_site_process_host_action) |
| 187 | |
| 188 | rpc_utils.process_host_action(host, action) |
| 189 | |
| 190 | self.assertTrue(self._called) |
| 191 | self.god.check_playback() |
| 192 | |
| 193 | |
| 194 | def test_process_test_action_skip(self): |
| 195 | self._setup_active_plan() |
| 196 | planner_job = self._planner_job |
| 197 | assert not planner_job.requires_rerun |
| 198 | |
| 199 | rpc_utils.process_test_action(planner_job, |
| 200 | failure_actions.TestAction.SKIP) |
| 201 | planner_job = models.Job.objects.get(id=planner_job.id) |
| 202 | |
| 203 | self.assertFalse(planner_job.requires_rerun) |
| 204 | |
| 205 | |
| 206 | def test_process_test_action_rerun(self): |
| 207 | self._setup_active_plan() |
| 208 | planner_job = self._planner_job |
| 209 | assert not planner_job.requires_rerun |
| 210 | |
| 211 | rpc_utils.process_test_action(planner_job, |
| 212 | failure_actions.TestAction.RERUN) |
| 213 | planner_job = models.Job.objects.get(id=planner_job.id) |
| 214 | |
| 215 | self.assertTrue(planner_job.requires_rerun) |
| 216 | |
| 217 | |
jamesren | c394022 | 2010-02-19 21:57:37 +0000 | [diff] [blame] | 218 | if __name__ == '__main__': |
| 219 | unittest.main() |