Home | History | Annotate | Download | only in common_lib
      1 #!/usr/bin/python
      2 # pylint: disable-msg=C0111
      3 
      4 import json
      5 import os, unittest
      6 
      7 import common
      8 
      9 from autotest_lib.client.common_lib import control_data, autotemp
     10 
     11 ControlData = control_data.ControlData
     12 
     13 CONTROL = """
     14 AUTHOR = 'Author'
     15 DEPENDENCIES = "console, power"
     16 DOC = \"\"\"\
     17 doc stuff\"\"\"
     18 # EXPERIMENTAL should implicitly be False
     19 NAME = 'nA' "mE"
     20 RUN_VERIFY = False
     21 SYNC_COUNT = 2
     22 TIME='short'
     23 TEST_CLASS=u'Kernel'
     24 TEST_CATEGORY='Stress'
     25 TEST_TYPE='client'
     26 RETRIES = 5
     27 REQUIRE_SSP = False
     28 ATTRIBUTES = "suite:smoke, suite:bvt"
     29 """
     30 
     31 
     32 class ParseControlTest(unittest.TestCase):
     33     def setUp(self):
     34         self.control_tmp = autotemp.tempfile(unique_id='control_unit',
     35                                              text=True)
     36         os.write(self.control_tmp.fd, CONTROL)
     37 
     38 
     39     def tearDown(self):
     40         self.control_tmp.clean()
     41 
     42 
     43     def test_parse_control(self):
     44         cd = control_data.parse_control(self.control_tmp.name, True)
     45         self.assertEquals(cd.author, "Author")
     46         self.assertEquals(cd.dependencies, set(['console', 'power']))
     47         self.assertEquals(cd.doc, "doc stuff")
     48         self.assertEquals(cd.experimental, False)
     49         self.assertEquals(cd.name, "nAmE")
     50         self.assertEquals(cd.run_verify, False)
     51         self.assertEquals(cd.sync_count, 2)
     52         self.assertEquals(cd.time, "short")
     53         self.assertEquals(cd.test_class, "kernel")
     54         self.assertEquals(cd.test_category, "stress")
     55         self.assertEquals(cd.test_type, "client")
     56         self.assertEquals(cd.retries, 5)
     57         self.assertEquals(cd.require_ssp, False)
     58         self.assertEquals(cd.attributes,
     59                           set(["suite:smoke","suite:bvt","subsystem:default"]))
     60 
     61 
     62 class ParseControlFileBugTemplate(unittest.TestCase):
     63     def setUp(self):
     64         self.control_tmp = autotemp.tempfile(unique_id='control_unit',
     65                                              text=True)
     66         self.bug_template = {
     67             'owner': 'someone (at] something.org',
     68             'labels': ['a', 'b'],
     69             'status': None,
     70             'summary': None,
     71             'title': None,
     72             'cc': ['a@something, b@something'],
     73         }
     74 
     75 
     76     def tearDown(self):
     77         self.control_tmp.clean()
     78 
     79 
     80     def insert_bug_template(self, control_file_string):
     81         """Insert a bug template into the control file string.
     82 
     83         @param control_file_string: A string of the control file contents
     84             this test will run on.
     85 
     86         @return: The control file string with the BUG_TEMPLATE line.
     87         """
     88         bug_template_line = 'BUG_TEMPLATE = %s' % json.dumps(self.bug_template)
     89         return control_file_string + bug_template_line
     90 
     91 
     92     def verify_bug_template(self, new_bug_template):
     93         """Verify that the bug template given matches the original.
     94 
     95         @param new_bug_template: A bug template pulled off parsing the
     96             control file.
     97 
     98         @raises AssetionError: If a value under a give key in the bug template
     99             doesn't match the value in self.bug_template.
    100         @raises KeyError: If a key in either bug template is missing.
    101         """
    102         for key, value in new_bug_template.iteritems():
    103             self.assertEqual(value, self.bug_template[key])
    104 
    105 
    106     def test_bug_template_parsing(self):
    107         """Basic parsing test for a bug templates in a test control file."""
    108         os.write(self.control_tmp.fd, self.insert_bug_template(CONTROL))
    109         cd = control_data.parse_control(self.control_tmp.name, True)
    110         self.verify_bug_template(cd.bug_template)
    111 
    112 
    113     def test_bug_template_list(self):
    114         """Test that lists in the bug template can handle other datatypes."""
    115         self.bug_template['labels'].append({'foo': 'bar'})
    116         os.write(self.control_tmp.fd, self.insert_bug_template(CONTROL))
    117         cd = control_data.parse_control(self.control_tmp.name, True)
    118         self.verify_bug_template(cd.bug_template)
    119 
    120 
    121     def test_bad_template(self):
    122         """Test that a bad bug template doesn't result in a bad control data."""
    123         self.bug_template = 'foobarbug_template'
    124         os.write(self.control_tmp.fd, self.insert_bug_template(CONTROL))
    125         cd = control_data.parse_control(self.control_tmp.name, True)
    126         self.assertFalse(hasattr(cd, 'bug_template'))
    127 
    128 
    129 class SetMethodTests(unittest.TestCase):
    130     def setUp(self):
    131         self.required_vars = control_data.REQUIRED_VARS
    132         control_data.REQUIRED_VARS = set()
    133 
    134 
    135     def tearDown(self):
    136         control_data.REQUIRED_VARS = self.required_vars
    137 
    138 
    139     def test_bool(self):
    140         cd = ControlData({}, 'filename')
    141         cd._set_bool('foo', 'False')
    142         self.assertEquals(cd.foo, False)
    143         cd._set_bool('foo', True)
    144         self.assertEquals(cd.foo, True)
    145         cd._set_bool('foo', 'FALSE')
    146         self.assertEquals(cd.foo, False)
    147         cd._set_bool('foo', 'true')
    148         self.assertEquals(cd.foo, True)
    149         self.assertRaises(ValueError, cd._set_bool, 'foo', '')
    150         self.assertRaises(ValueError, cd._set_bool, 'foo', 1)
    151         self.assertRaises(ValueError, cd._set_bool, 'foo', [])
    152         self.assertRaises(ValueError, cd._set_bool, 'foo', None)
    153 
    154 
    155     def test_int(self):
    156         cd = ControlData({}, 'filename')
    157         cd._set_int('foo', 0)
    158         self.assertEquals(cd.foo, 0)
    159         cd._set_int('foo', '0')
    160         self.assertEquals(cd.foo, 0)
    161         cd._set_int('foo', '-1', min=-2, max=10)
    162         self.assertEquals(cd.foo, -1)
    163         self.assertRaises(ValueError, cd._set_int, 'foo', 0, min=1)
    164         self.assertRaises(ValueError, cd._set_int, 'foo', 1, max=0)
    165         self.assertRaises(ValueError, cd._set_int, 'foo', 'x')
    166         self.assertRaises(ValueError, cd._set_int, 'foo', '')
    167         self.assertRaises(TypeError, cd._set_int, 'foo', None)
    168 
    169 
    170     def test_set(self):
    171         cd = ControlData({}, 'filename')
    172         cd._set_set('foo', 'a')
    173         self.assertEquals(cd.foo, set(['a']))
    174         cd._set_set('foo', 'a,b,c')
    175         self.assertEquals(cd.foo, set(['a', 'b', 'c']))
    176         cd._set_set('foo', ' a , b , c     ')
    177         self.assertEquals(cd.foo, set(['a', 'b', 'c']))
    178         cd._set_set('foo', None)
    179         self.assertEquals(cd.foo, set(['None']))
    180 
    181 
    182     def test_string(self):
    183         cd = ControlData({}, 'filename')
    184         cd._set_string('foo', 'a')
    185         self.assertEquals(cd.foo, 'a')
    186         cd._set_string('foo', 'b')
    187         self.assertEquals(cd.foo, 'b')
    188         cd._set_string('foo', 'B')
    189         self.assertEquals(cd.foo, 'B')
    190         cd._set_string('foo', 1)
    191         self.assertEquals(cd.foo, '1')
    192         cd._set_string('foo', None)
    193         self.assertEquals(cd.foo, 'None')
    194         cd._set_string('foo', [])
    195         self.assertEquals(cd.foo, '[]')
    196 
    197 
    198     def test_option(self):
    199         options = ['a', 'b']
    200         cd = ControlData({}, 'filename')
    201         cd._set_option('foo', 'a', options)
    202         self.assertEquals(cd.foo, 'a')
    203         cd._set_option('foo', 'b', options)
    204         self.assertEquals(cd.foo, 'b')
    205         cd._set_option('foo', 'B', options)
    206         self.assertEquals(cd.foo, 'B')
    207         self.assertRaises(ValueError, cd._set_option,
    208                           'foo', 'x', options)
    209         self.assertRaises(ValueError, cd._set_option,
    210                           'foo', 1, options)
    211         self.assertRaises(ValueError, cd._set_option,
    212                           'foo', [], options)
    213         self.assertRaises(ValueError, cd._set_option,
    214                           'foo', None, options)
    215 
    216 
    217     def test_set_attributes(self):
    218         cd = ControlData({}, 'filename')
    219         cd.set_attributes('suite:bvt')
    220         self.assertEquals(cd.attributes, set(['suite:bvt',
    221                                               'subsystem:default']))
    222         cd.set_attributes('suite:bvt, subsystem:network')
    223         self.assertEquals(cd.attributes, set(['suite:bvt',
    224                                               'subsystem:network']))
    225 
    226 
    227     def test_get_test_time_index(self):
    228         inputs = [time.upper() for time in
    229                   ControlData.TEST_TIME_LIST]
    230         time_min_index = [ControlData.get_test_time_index(time)
    231                           for time in inputs]
    232         expected_time_index = range(len(ControlData.TEST_TIME_LIST))
    233         self.assertEqual(time_min_index, expected_time_index)
    234 
    235 
    236     def test_get_test_time_index_failure(self):
    237         def fail():
    238             """Test function to raise ControlVariableException exception
    239             for invalid TIME setting."""
    240             index = ControlData.get_test_time_index('some invalid TIME')
    241 
    242         self.assertRaises(control_data.ControlVariableException, fail)
    243 
    244 
    245 # this is so the test can be run in standalone mode
    246 if __name__ == '__main__':
    247     unittest.main()
    248