showard | 26b7ec7 | 2009-12-21 22:43:57 +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.afe import frontend_test_utils, rpc_utils |
jamesren | 9a6f5f6 | 2010-05-05 22:55:54 +0000 | [diff] [blame] | 7 | from autotest_lib.frontend.planner import models, model_attributes |
showard | 26b7ec7 | 2009-12-21 22:43:57 +0000 | [diff] [blame] | 8 | |
| 9 | |
| 10 | class ModelWithHashTestBase(frontend_test_utils.FrontendTestMixin): |
| 11 | def setUp(self): |
| 12 | self._frontend_common_setup(fill_data=False) |
| 13 | |
| 14 | |
| 15 | def tearDown(self): |
| 16 | self._frontend_common_teardown() |
| 17 | |
| 18 | |
| 19 | def _model_class(self): |
| 20 | raise NotImplementedError('Subclasses must override _model_class()') |
| 21 | |
| 22 | |
| 23 | def _test_data(self): |
| 24 | raise NotImplementedError('Subclasses must override _test_data()') |
| 25 | |
| 26 | |
| 27 | def test_disallowed_operations(self): |
| 28 | def _call_create(): |
| 29 | self._model_class().objects.create(**self._test_data()) |
| 30 | self.assertRaises(Exception, _call_create) |
| 31 | |
| 32 | model = self._model_class().objects.get_or_create( |
| 33 | **self._test_data())[0] |
| 34 | self.assertRaises(Exception, model.save) |
| 35 | |
| 36 | |
| 37 | def test_hash_field(self): |
| 38 | model = self._model_class().objects.get_or_create( |
| 39 | **self._test_data())[0] |
| 40 | self.assertNotEqual(model.id, None) |
| 41 | self.assertEqual(self._model_class()._compute_hash(**self._test_data()), |
| 42 | model.the_hash) |
| 43 | |
| 44 | |
| 45 | class ControlFileTest(ModelWithHashTestBase, unittest.TestCase): |
| 46 | def _model_class(self): |
| 47 | return models.ControlFile |
| 48 | |
| 49 | |
| 50 | def _test_data(self): |
| 51 | return {'contents' : 'test_control'} |
| 52 | |
| 53 | |
| 54 | class KeyValTest(ModelWithHashTestBase, unittest.TestCase): |
| 55 | def _model_class(self): |
| 56 | return models.KeyVal |
| 57 | |
| 58 | |
| 59 | def _test_data(self): |
| 60 | return {'key' : 'test_key', |
| 61 | 'value' : 'test_value'} |
| 62 | |
| 63 | |
jamesren | 9a6f5f6 | 2010-05-05 22:55:54 +0000 | [diff] [blame] | 64 | class AdditionalParameterTest(frontend_test_utils.FrontendTestMixin, |
| 65 | unittest.TestCase): |
| 66 | def setUp(self): |
| 67 | self._frontend_common_setup() |
| 68 | self.plan = models.Plan.objects.create(name='plan') |
| 69 | self.param_type = model_attributes.AdditionalParameterType.VERIFY |
| 70 | |
| 71 | def tearDown(self): |
| 72 | self._frontend_common_teardown() |
| 73 | |
| 74 | |
| 75 | def test_find_applicable_control_parameter_match(self): |
| 76 | parameter = models.AdditionalParameter.objects.create( |
| 77 | plan=self.plan, hostname_regex='host.*', |
| 78 | param_type=self.param_type, application_order=0) |
| 79 | found = models.AdditionalParameter.find_applicable_additional_parameter( |
| 80 | plan=self.plan, hostname='host1', param_type=self.param_type) |
| 81 | |
| 82 | self.assertEqual(parameter, found) |
| 83 | |
| 84 | |
| 85 | def test_find_applicable_additional_parameter_ordered(self): |
| 86 | additional1 = models.AdditionalParameter.objects.create( |
| 87 | plan=self.plan, hostname_regex='host.*', |
| 88 | param_type=self.param_type, application_order=0) |
| 89 | additional2 = models.AdditionalParameter.objects.create( |
| 90 | plan=self.plan, hostname_regex='.*', |
| 91 | param_type=self.param_type, application_order=1) |
| 92 | |
| 93 | found1 = ( |
| 94 | models.AdditionalParameter.find_applicable_additional_parameter( |
| 95 | plan=self.plan, hostname='host1', |
| 96 | param_type=self.param_type)) |
| 97 | found2 = ( |
| 98 | models.AdditionalParameter.find_applicable_additional_parameter( |
| 99 | plan=self.plan, hostname='other', |
| 100 | param_type=self.param_type)) |
| 101 | |
| 102 | self.assertEqual(additional1, found1) |
| 103 | self.assertEqual(additional2, found2) |
| 104 | |
| 105 | |
| 106 | def test_find_applicable_additional_parameter_no_match(self): |
| 107 | models.AdditionalParameter.objects.create( |
| 108 | plan=self.plan, hostname_regex='host.*', |
| 109 | param_type=self.param_type, application_order=0) |
| 110 | found = models.AdditionalParameter.find_applicable_additional_parameter( |
| 111 | plan=self.plan, hostname='other', param_type=self.param_type) |
| 112 | |
| 113 | self.assertEqual(None, found) |
| 114 | |
| 115 | |
showard | 26b7ec7 | 2009-12-21 22:43:57 +0000 | [diff] [blame] | 116 | if __name__ == '__main__': |
| 117 | unittest.main() |