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