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