Best Python code snippet using autotest_python
control_file.py
Source:control_file.py
...160def format_step(item, lines):161 lines = indent_text(lines, ' ')162 lines = 'def step%d():\n%s' % (item, lines)163 return lines164def get_tests_stanza(tests, is_server, prepend=None, append=None,165 client_control_file=''):166 """ Constructs the control file test step code from a list of tests.167 @param tests A sequence of test control files to run.168 @param is_server bool, Is this a server side test?169 @param prepend A list of steps to prepend to each client test.170 Defaults to [].171 @param append A list of steps to append to each client test.172 Defaults to [].173 @param client_control_file If specified, use this text as the body of a174 final client control file to run after tests. is_server must be False.175 @returns The control file test code to be run.176 """177 assert not (client_control_file and is_server)178 if not prepend:179 prepend = []180 if not append:181 append = []182 raw_control_files = [read_control_file(test) for test in tests]183 return _get_tests_stanza(raw_control_files, is_server, prepend, append,184 client_control_file=client_control_file)185def _get_tests_stanza(raw_control_files, is_server, prepend, append,186 client_control_file=''):187 """188 Implements the common parts of get_test_stanza.189 A site_control_file that wants to implement its own get_tests_stanza190 likely wants to call this in the end.191 @param raw_control_files A list of raw control file data to be combined192 into a single control file.193 @param is_server bool, Is this a server side test?194 @param prepend A list of steps to prepend to each client test.195 @param append A list of steps to append to each client test.196 @param client_control_file If specified, use this text as the body of a197 final client control file to append to raw_control_files after fixups.198 @returns The combined mega control file.199 """200 if client_control_file:201 # 'return locals()' is always appended incase the user forgot, it202 # is necessary to allow for nested step engine execution to work.203 raw_control_files.append(client_control_file + '\nreturn locals()')204 raw_steps = prepend + [add_boilerplate_to_nested_steps(step)205 for step in raw_control_files] + append206 steps = [format_step(index, step)207 for index, step in enumerate(raw_steps)]208 if is_server:209 step_template = SERVER_STEP_TEMPLATE210 footer = '\n\nstep_init()\n'211 else:212 step_template = CLIENT_STEP_TEMPLATE213 footer = ''214 header = ''.join(step_template % i for i in xrange(len(steps)))215 return header + '\n' + '\n\n'.join(steps) + footer216def indent_text(text, indent):217 """Indent given lines of python code avoiding indenting multiline218 quoted content (only for triple " and ' quoting for now)."""219 regex = re.compile('(\\\\*)("""|\'\'\')')220 res = []221 in_quote = None222 for line in text.splitlines():223 # if not within a multinline quote indent the line contents224 if in_quote:225 res.append(line)226 else:227 res.append(indent + line)228 while line:229 match = regex.search(line)230 if match:231 # for an even number of backslashes before the triple quote232 if len(match.group(1)) % 2 == 0:233 if not in_quote:234 in_quote = match.group(2)[0]235 elif in_quote == match.group(2)[0]:236 # if we found a matching end triple quote237 in_quote = None238 line = line[match.end():]239 else:240 break241 return '\n'.join(res)242def _get_profiler_commands(profilers, is_server, profile_only):243 prepend, append = [], []244 if profile_only is not None:245 prepend.append("job.default_profile_only = %r" % profile_only)246 for profiler in profilers:247 prepend.append("job.profilers.add('%s')" % profiler.name)248 append.append("job.profilers.delete('%s')" % profiler.name)249 return prepend, append250def _sanity_check_generate_control(is_server, client_control_file, kernels,251 upload_kernel_config):252 """253 Sanity check some of the parameters to generate_control().254 This exists as its own function so that site_control_file may call it as255 well from its own generate_control().256 @raises ValidationError if any of the parameters do not make sense.257 """258 if is_server and client_control_file:259 raise model_logic.ValidationError(260 {'tests' : 'You cannot run server tests at the same time '261 'as directly supplying a client-side control file.'})262 if kernels:263 # make sure that kernel is a list of dictionarions with at least264 # the 'version' key in them265 kernel_error = model_logic.ValidationError(266 {'kernel': 'The kernel parameter must be a sequence of '267 'dictionaries containing at least the "version" key '268 '(got: %r)' % kernels})269 try:270 iter(kernels)271 except TypeError:272 raise kernel_error273 for kernel_info in kernels:274 if (not isinstance(kernel_info, dict) or275 'version' not in kernel_info):276 raise kernel_error277 if upload_kernel_config and not is_server:278 raise model_logic.ValidationError(279 {'upload_kernel_config': 'Cannot use upload_kernel_config '280 'with client side tests'})281def generate_control(tests, kernels=None, platform=None, is_server=False,282 profilers=(), client_control_file='', profile_only=None,283 upload_kernel_config=False):284 """285 Generate a control file for a sequence of tests.286 @param tests A sequence of test control files to run.287 @param kernels A sequence of kernel info dictionaries configuring which288 kernels to boot for this job and other options for them289 @param platform A platform object with a kernel_config attribute.290 @param is_server bool, Is this a server control file rather than a client?291 @param profilers A list of profiler objects to enable during the tests.292 @param client_control_file Contents of a client control file to run as the293 last test after everything in tests. Requires is_server=False.294 @param profile_only bool, should this control file run all tests in295 profile_only mode by default296 @param upload_kernel_config: if enabled it will generate server control297 file code that uploads the kernel config file to the client and298 tells the client of the new (local) path when compiling the kernel;299 the tests must be server side tests300 @returns The control file text as a string.301 """302 _sanity_check_generate_control(is_server=is_server, kernels=kernels,303 client_control_file=client_control_file,304 upload_kernel_config=upload_kernel_config)305 control_file_text = ''306 if kernels:307 control_file_text = get_kernel_stanza(308 kernels, platform, is_server=is_server,309 upload_kernel_config=upload_kernel_config)310 else:311 control_file_text = EMPTY_TEMPLATE312 prepend, append = _get_profiler_commands(profilers, is_server, profile_only)313 control_file_text += get_tests_stanza(tests, is_server, prepend, append,314 client_control_file)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!