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