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 | 6275824 | 2010-04-28 18:08:25 +0000 | [diff] [blame] | 6 | from autotest_lib.frontend import setup_test_environment |
jamesren | c394022 | 2010-02-19 21:57:37 +0000 | [diff] [blame] | 7 | from autotest_lib.frontend.afe import model_logic, models as afe_models |
jamesren | 6275824 | 2010-04-28 18:08:25 +0000 | [diff] [blame] | 8 | from autotest_lib.frontend.planner import planner_test_utils, model_attributes |
jamesren | 4be631f | 2010-04-08 23:01:22 +0000 | [diff] [blame] | 9 | from autotest_lib.frontend.planner import models, rpc_utils, failure_actions |
jamesren | 6275824 | 2010-04-28 18:08:25 +0000 | [diff] [blame] | 10 | from autotest_lib.frontend.tko import models as tko_models |
jamesren | 8d0d3d5 | 2010-03-25 20:39:13 +0000 | [diff] [blame] | 11 | from autotest_lib.client.common_lib import utils, host_queue_entry_states |
jamesren | c394022 | 2010-02-19 21:57:37 +0000 | [diff] [blame] | 12 | |
| 13 | |
| 14 | class RpcUtilsTest(unittest.TestCase, |
| 15 | planner_test_utils.PlannerTestMixin): |
| 16 | def setUp(self): |
| 17 | self._planner_common_setup() |
| 18 | |
| 19 | |
| 20 | def tearDown(self): |
| 21 | self._planner_common_teardown() |
| 22 | |
| 23 | |
| 24 | def test_create_plan_label(self): |
| 25 | label, group = self._create_label_helper() |
| 26 | |
| 27 | label.delete() |
| 28 | group.invalid = True |
| 29 | group.save() |
| 30 | |
| 31 | label, group = self._create_label_helper() |
| 32 | |
| 33 | self.assertRaises(model_logic.ValidationError, |
| 34 | rpc_utils.create_plan_label, self._plan) |
| 35 | |
| 36 | |
| 37 | def _create_label_helper(self): |
| 38 | label = rpc_utils.create_plan_label(self._plan) |
| 39 | group = afe_models.AtomicGroup.objects.get( |
| 40 | name=rpc_utils.PLANNER_ATOMIC_GROUP_NAME) |
| 41 | self.assertFalse(group.invalid) |
| 42 | self.assertEqual(label.atomic_group, group) |
| 43 | |
| 44 | return (label, group) |
| 45 | |
| 46 | |
| 47 | def test_lazy_load(self): |
| 48 | self.god.stub_function(utils, 'read_file') |
| 49 | |
| 50 | DUMMY_PATH_1 = object() |
| 51 | DUMMY_PATH_2 = object() |
| 52 | DUMMY_FILE_1 = object() |
| 53 | DUMMY_FILE_2 = object() |
| 54 | |
| 55 | utils.read_file.expect_call(DUMMY_PATH_1).and_return(DUMMY_FILE_1) |
| 56 | self.assertEqual(DUMMY_FILE_1, rpc_utils.lazy_load(DUMMY_PATH_1)) |
| 57 | self.god.check_playback() |
| 58 | |
| 59 | # read_file should not be called again for this path |
| 60 | self.assertEqual(DUMMY_FILE_1, rpc_utils.lazy_load(DUMMY_PATH_1)) |
| 61 | self.god.check_playback() |
| 62 | |
| 63 | # new file; read_file must be called again |
| 64 | utils.read_file.expect_call(DUMMY_PATH_2).and_return(DUMMY_FILE_2) |
| 65 | self.assertEqual(DUMMY_FILE_2, rpc_utils.lazy_load(DUMMY_PATH_2)) |
| 66 | self.god.check_playback() |
| 67 | |
| 68 | |
jamesren | 8d0d3d5 | 2010-03-25 20:39:13 +0000 | [diff] [blame] | 69 | def test_update_hosts_table(self): |
| 70 | label = self.labels[3] |
| 71 | default_hosts = set(self._plan.hosts.all()) |
| 72 | |
| 73 | rpc_utils.update_hosts_table(self._plan) |
| 74 | self.assertEqual(default_hosts, set(self._plan.hosts.all())) |
| 75 | self.assertEqual(set(), self._get_added_by_label_hosts()) |
| 76 | |
| 77 | self._plan.host_labels.add(label) |
| 78 | rpc_utils.update_hosts_table(self._plan) |
| 79 | self.assertEqual(default_hosts.union(label.host_set.all()), |
| 80 | set(self._plan.hosts.all())) |
| 81 | self.assertEqual(set(label.host_set.all()), |
| 82 | self._get_added_by_label_hosts()) |
| 83 | |
| 84 | self._plan.host_labels.remove(label) |
| 85 | rpc_utils.update_hosts_table(self._plan) |
| 86 | self.assertEqual(default_hosts, set(self._plan.hosts.all())) |
| 87 | self.assertEqual(set(), self._get_added_by_label_hosts()) |
| 88 | |
| 89 | |
| 90 | def _get_added_by_label_hosts(self): |
| 91 | return set(host.host for host in models.Host.objects.filter( |
| 92 | plan=self._plan, added_by_label=True)) |
| 93 | |
| 94 | |
| 95 | def test_compute_next_test_config(self): |
| 96 | self._setup_active_plan() |
| 97 | test_config = models.TestConfig.objects.create( |
| 98 | plan=self._plan, alias='config2', control_file=self._control, |
| 99 | execution_order=2, estimated_runtime=1) |
| 100 | |
| 101 | self.assertEqual(1, self._afe_job.hostqueueentry_set.count()) |
| 102 | self.assertEqual( |
| 103 | None, rpc_utils.compute_next_test_config(self._plan, |
| 104 | self._planner_host)) |
| 105 | self.assertFalse(self._planner_host.complete) |
| 106 | |
| 107 | hqe = self._afe_job.hostqueueentry_set.all()[0] |
| 108 | hqe.status = host_queue_entry_states.Status.COMPLETED |
| 109 | hqe.save() |
| 110 | |
| 111 | self.assertEqual( |
jamesren | dbeebf8 | 2010-04-08 22:58:26 +0000 | [diff] [blame] | 112 | test_config, |
jamesren | 8d0d3d5 | 2010-03-25 20:39:13 +0000 | [diff] [blame] | 113 | rpc_utils.compute_next_test_config(self._plan, |
| 114 | self._planner_host)) |
| 115 | self.assertFalse(self._planner_host.complete) |
| 116 | |
| 117 | afe_job = self._create_job(hosts=(1,)) |
| 118 | planner_job = models.Job.objects.create(plan=self._plan, |
| 119 | test_config=test_config, |
| 120 | afe_job=afe_job) |
| 121 | |
| 122 | self.assertEqual( |
| 123 | None, rpc_utils.compute_next_test_config(self._plan, |
| 124 | self._planner_host)) |
| 125 | self.assertFalse(self._planner_host.complete) |
| 126 | |
| 127 | hqe = afe_job.hostqueueentry_set.all()[0] |
| 128 | hqe.status = host_queue_entry_states.Status.COMPLETED |
| 129 | hqe.save() |
| 130 | |
| 131 | self.assertEqual( |
| 132 | None, rpc_utils.compute_next_test_config(self._plan, |
| 133 | self._planner_host)) |
| 134 | self.assertTrue(self._planner_host.complete) |
| 135 | |
| 136 | |
jamesren | 6275824 | 2010-04-28 18:08:25 +0000 | [diff] [blame] | 137 | def test_process_failure(self): |
| 138 | self._setup_active_plan() |
| 139 | tko_test = tko_models.Test.objects.create(job=self._tko_job, |
| 140 | machine=self._tko_machine, |
| 141 | kernel=self._tko_kernel, |
| 142 | status=self._running_status) |
| 143 | failure = models.TestRun.objects.create( |
| 144 | plan=self._plan, |
| 145 | test_job=self._planner_job, |
| 146 | tko_test=tko_test, |
| 147 | host=self._planner_host, |
| 148 | status=model_attributes.TestRunStatus.FAILED, |
| 149 | finalized=True, seen=False, triaged=False) |
| 150 | host_action = failure_actions.HostAction.UNBLOCK |
| 151 | test_action = failure_actions.TestAction.SKIP |
| 152 | labels = ['label1', 'label2'] |
| 153 | keyvals = {'key1': 'value1', |
| 154 | 'key2': 'value2'} |
| 155 | bugs = ['bug1', 'bug2'] |
| 156 | reason = 'overriden reason' |
| 157 | invalidate = True |
| 158 | |
| 159 | self.god.stub_function(rpc_utils, '_process_host_action') |
| 160 | self.god.stub_function(rpc_utils, '_process_test_action') |
| 161 | |
| 162 | rpc_utils._process_host_action.expect_call(self._planner_host, |
| 163 | host_action) |
| 164 | rpc_utils._process_test_action.expect_call(self._planner_job, |
| 165 | test_action) |
| 166 | |
| 167 | rpc_utils.process_failure( |
| 168 | failure_id=failure.id, host_action=host_action, |
| 169 | test_action=test_action, labels=labels, keyvals=keyvals, |
| 170 | bugs=bugs, reason=reason, invalidate=invalidate) |
| 171 | failure = models.TestRun.objects.get(id=failure.id) |
| 172 | |
| 173 | self.assertEqual( |
| 174 | set(failure.tko_test.testlabel_set.all()), |
| 175 | set(tko_models.TestLabel.objects.filter(name__in=labels))) |
| 176 | self.assertEqual( |
| 177 | set(failure.tko_test.job.jobkeyval_set.all()), |
| 178 | set(tko_models.JobKeyval.objects.filter( |
| 179 | key__in=keyvals.iterkeys()))) |
| 180 | self.assertEqual(set(failure.bugs.all()), |
| 181 | set(models.Bug.objects.filter(external_uid__in=bugs))) |
| 182 | self.assertEqual(failure.tko_test.reason, reason) |
| 183 | self.assertEqual(failure.invalidated, invalidate) |
| 184 | self.assertTrue(failure.seen) |
| 185 | self.assertTrue(failure.triaged) |
| 186 | self.god.check_playback() |
| 187 | |
| 188 | |
jamesren | 4be631f | 2010-04-08 23:01:22 +0000 | [diff] [blame] | 189 | def _replace_site_process_host_action(self, replacement): |
| 190 | self.god.stub_function(utils, 'import_site_function') |
| 191 | utils.import_site_function.expect_any_call().and_return(replacement) |
| 192 | |
| 193 | |
| 194 | def _remove_site_process_host_action(self): |
| 195 | def _site_process_host_action_dummy(host, action): |
| 196 | return False |
| 197 | self._replace_site_process_host_action(_site_process_host_action_dummy) |
| 198 | |
| 199 | |
| 200 | def test_process_host_action_block(self): |
| 201 | self._remove_site_process_host_action() |
| 202 | host = models.Host.objects.create(plan=self._plan, host=self.hosts[0], |
| 203 | blocked=False) |
| 204 | assert not host.blocked |
| 205 | |
jamesren | 6275824 | 2010-04-28 18:08:25 +0000 | [diff] [blame] | 206 | rpc_utils._process_host_action(host, failure_actions.HostAction.BLOCK) |
jamesren | 4be631f | 2010-04-08 23:01:22 +0000 | [diff] [blame] | 207 | host = models.Host.objects.get(id=host.id) |
| 208 | |
| 209 | self.assertTrue(host.blocked) |
| 210 | self.god.check_playback() |
| 211 | |
| 212 | |
| 213 | def test_process_host_action_unblock(self): |
| 214 | self._remove_site_process_host_action() |
| 215 | host = models.Host.objects.create(plan=self._plan, host=self.hosts[0], |
| 216 | blocked=True) |
| 217 | assert host.blocked |
| 218 | |
jamesren | 6275824 | 2010-04-28 18:08:25 +0000 | [diff] [blame] | 219 | rpc_utils._process_host_action(host, failure_actions.HostAction.UNBLOCK) |
jamesren | 4be631f | 2010-04-08 23:01:22 +0000 | [diff] [blame] | 220 | host = models.Host.objects.get(id=host.id) |
| 221 | |
| 222 | self.assertFalse(host.blocked) |
| 223 | self.god.check_playback() |
| 224 | |
| 225 | |
| 226 | def test_process_host_action_site(self): |
| 227 | self._remove_site_process_host_action |
| 228 | action = object() |
| 229 | failure_actions.HostAction.values.append(action) |
| 230 | host = models.Host.objects.create(plan=self._plan, host=self.hosts[0]) |
| 231 | |
jamesren | 6275824 | 2010-04-28 18:08:25 +0000 | [diff] [blame] | 232 | self.assertRaises(AssertionError, rpc_utils._process_host_action, |
jamesren | 4be631f | 2010-04-08 23:01:22 +0000 | [diff] [blame] | 233 | host, action) |
| 234 | self.god.check_playback() |
| 235 | |
| 236 | self._called = False |
| 237 | def _site_process_host_action(host, action): |
| 238 | self._called = True |
| 239 | return True |
| 240 | self._replace_site_process_host_action(_site_process_host_action) |
| 241 | |
jamesren | 6275824 | 2010-04-28 18:08:25 +0000 | [diff] [blame] | 242 | rpc_utils._process_host_action(host, action) |
jamesren | 4be631f | 2010-04-08 23:01:22 +0000 | [diff] [blame] | 243 | |
| 244 | self.assertTrue(self._called) |
| 245 | self.god.check_playback() |
| 246 | |
| 247 | |
| 248 | def test_process_test_action_skip(self): |
| 249 | self._setup_active_plan() |
| 250 | planner_job = self._planner_job |
| 251 | assert not planner_job.requires_rerun |
| 252 | |
jamesren | 6275824 | 2010-04-28 18:08:25 +0000 | [diff] [blame] | 253 | rpc_utils._process_test_action(planner_job, |
| 254 | failure_actions.TestAction.SKIP) |
jamesren | 4be631f | 2010-04-08 23:01:22 +0000 | [diff] [blame] | 255 | planner_job = models.Job.objects.get(id=planner_job.id) |
| 256 | |
| 257 | self.assertFalse(planner_job.requires_rerun) |
| 258 | |
| 259 | |
| 260 | def test_process_test_action_rerun(self): |
| 261 | self._setup_active_plan() |
| 262 | planner_job = self._planner_job |
| 263 | assert not planner_job.requires_rerun |
| 264 | |
jamesren | 6275824 | 2010-04-28 18:08:25 +0000 | [diff] [blame] | 265 | rpc_utils._process_test_action(planner_job, |
| 266 | failure_actions.TestAction.RERUN) |
jamesren | 4be631f | 2010-04-08 23:01:22 +0000 | [diff] [blame] | 267 | planner_job = models.Job.objects.get(id=planner_job.id) |
| 268 | |
| 269 | self.assertTrue(planner_job.requires_rerun) |
| 270 | |
| 271 | |
jamesren | 9a6f5f6 | 2010-05-05 22:55:54 +0000 | [diff] [blame] | 272 | def test_set_additional_parameters(self): |
| 273 | hostname_regex = 'host[0-9]' |
| 274 | param_type = model_attributes.AdditionalParameterType.VERIFY |
| 275 | param_values = {'key1': 'value1', |
| 276 | 'key2': []} |
| 277 | |
| 278 | additional_parameters = {'hostname_regex': hostname_regex, |
| 279 | 'param_type': param_type, |
| 280 | 'param_values': param_values} |
| 281 | |
| 282 | rpc_utils.set_additional_parameters(self._plan, [additional_parameters]) |
| 283 | |
| 284 | additional_parameters_query = ( |
| 285 | models.AdditionalParameter.objects.filter(plan=self._plan)) |
| 286 | self.assertEqual(additional_parameters_query.count(), 1) |
| 287 | |
| 288 | additional_parameter = additional_parameters_query[0] |
| 289 | self.assertEqual(additional_parameter.hostname_regex, hostname_regex) |
| 290 | self.assertEqual(additional_parameter.param_type, param_type) |
| 291 | self.assertEqual(additional_parameter.application_order, 0) |
| 292 | |
| 293 | values_query = additional_parameter.additionalparametervalue_set.all() |
| 294 | self.assertEqual(values_query.count(), 2) |
| 295 | |
| 296 | value_query1 = values_query.filter(key='key1') |
| 297 | value_query2 = values_query.filter(key='key2') |
| 298 | self.assertEqual(value_query1.count(), 1) |
| 299 | self.assertEqual(value_query2.count(), 1) |
| 300 | |
| 301 | self.assertEqual(value_query1[0].value, repr('value1')) |
| 302 | self.assertEqual(value_query2[0].value, repr([])) |
| 303 | |
| 304 | |
| 305 | def test_get_wrap_arguments(self): |
| 306 | hostname_regex = '.*' |
| 307 | param_type = model_attributes.AdditionalParameterType.VERIFY |
| 308 | |
| 309 | additional_param = models.AdditionalParameter.objects.create( |
| 310 | plan=self._plan, hostname_regex=hostname_regex, |
| 311 | param_type=param_type, application_order=0) |
| 312 | models.AdditionalParameterValue.objects.create( |
| 313 | additional_parameter=additional_param, |
| 314 | key='key1', value=repr('value1')) |
| 315 | models.AdditionalParameterValue.objects.create( |
| 316 | additional_parameter=additional_param, |
| 317 | key='key2', value=repr([])) |
| 318 | |
| 319 | actual = rpc_utils.get_wrap_arguments(self._plan, 'host', param_type) |
| 320 | expected = {'key1': repr('value1'), |
| 321 | 'key2': repr([])} |
| 322 | |
| 323 | self.assertEqual(actual, expected) |
| 324 | |
jamesren | 2e48bcb | 2010-05-18 20:41:36 +0000 | [diff] [blame] | 325 | |
jamesren | 06e59f1 | 2010-05-24 17:05:06 +0000 | [diff] [blame^] | 326 | def test_compute_test_config_status_scheduled(self): |
jamesren | 2e48bcb | 2010-05-18 20:41:36 +0000 | [diff] [blame] | 327 | self._setup_active_plan() |
jamesren | 06e59f1 | 2010-05-24 17:05:06 +0000 | [diff] [blame^] | 328 | self._planner_job.delete() |
| 329 | |
| 330 | self.assertEqual( |
| 331 | rpc_utils.compute_test_config_status(self._planner_host), |
| 332 | rpc_utils.ComputeTestConfigStatusResult.SCHEDULED) |
jamesren | 2e48bcb | 2010-05-18 20:41:36 +0000 | [diff] [blame] | 333 | |
| 334 | |
jamesren | 06e59f1 | 2010-05-24 17:05:06 +0000 | [diff] [blame^] | 335 | def test_compute_test_config_status_running(self): |
| 336 | self._setup_active_plan() |
| 337 | self.god.stub_function(models.Job, 'active') |
| 338 | models.Job.active.expect_call().and_return(True) |
| 339 | |
| 340 | self.assertEqual( |
| 341 | rpc_utils.compute_test_config_status(self._planner_host), |
| 342 | rpc_utils.ComputeTestConfigStatusResult.RUNNING) |
| 343 | self.god.check_playback() |
| 344 | |
| 345 | |
| 346 | def test_compute_test_config_status_good(self): |
jamesren | 2e48bcb | 2010-05-18 20:41:36 +0000 | [diff] [blame] | 347 | self._setup_active_plan() |
| 348 | tko_test = self._tko_job.test_set.create(kernel=self._tko_kernel, |
| 349 | status=self._good_status, |
| 350 | machine=self._tko_machine) |
| 351 | self._plan.testrun_set.create(test_job=self._planner_job, |
| 352 | tko_test=tko_test, |
| 353 | host=self._planner_host) |
| 354 | self._planner_host.complete = True |
| 355 | self._planner_host.save() |
jamesren | 06e59f1 | 2010-05-24 17:05:06 +0000 | [diff] [blame^] | 356 | self.god.stub_function(models.Job, 'active') |
| 357 | models.Job.active.expect_call().and_return(False) |
jamesren | 2e48bcb | 2010-05-18 20:41:36 +0000 | [diff] [blame] | 358 | |
jamesren | 06e59f1 | 2010-05-24 17:05:06 +0000 | [diff] [blame^] | 359 | self.assertEqual( |
| 360 | rpc_utils.compute_test_config_status(self._planner_host), |
| 361 | rpc_utils.ComputeTestConfigStatusResult.PASS) |
| 362 | self.god.check_playback() |
jamesren | 2e48bcb | 2010-05-18 20:41:36 +0000 | [diff] [blame] | 363 | |
| 364 | |
jamesren | 06e59f1 | 2010-05-24 17:05:06 +0000 | [diff] [blame^] | 365 | def test_compute_test_config_status_bad(self): |
jamesren | 2e48bcb | 2010-05-18 20:41:36 +0000 | [diff] [blame] | 366 | self._setup_active_plan() |
| 367 | tko_test = self._tko_job.test_set.create(kernel=self._tko_kernel, |
| 368 | status=self._fail_status, |
| 369 | machine=self._tko_machine) |
| 370 | self._plan.testrun_set.create(test_job=self._planner_job, |
| 371 | tko_test=tko_test, |
| 372 | host=self._planner_host) |
| 373 | self._planner_host.complete = True |
| 374 | self._planner_host.save() |
jamesren | 06e59f1 | 2010-05-24 17:05:06 +0000 | [diff] [blame^] | 375 | self.god.stub_function(models.Job, 'active') |
| 376 | models.Job.active.expect_call().and_return(False) |
jamesren | 2e48bcb | 2010-05-18 20:41:36 +0000 | [diff] [blame] | 377 | |
jamesren | 06e59f1 | 2010-05-24 17:05:06 +0000 | [diff] [blame^] | 378 | self.assertEqual( |
| 379 | rpc_utils.compute_test_config_status(self._planner_host), |
| 380 | rpc_utils.ComputeTestConfigStatusResult.FAIL) |
| 381 | self.god.check_playback() |
jamesren | 2e48bcb | 2010-05-18 20:41:36 +0000 | [diff] [blame] | 382 | |
| 383 | |
jamesren | c394022 | 2010-02-19 21:57:37 +0000 | [diff] [blame] | 384 | if __name__ == '__main__': |
| 385 | unittest.main() |