blob: a739b1f2dd93ce23919e2dd220648a4bca830f35 [file] [log] [blame]
mblighe8819cd2008-02-15 16:48:40 +00001"""\
2Logic for control file generation.
3"""
4
5__author__ = 'showard@google.com (Steve Howard)'
6
showard9ca52702008-06-02 21:14:49 +00007import re, os
mbligh120351e2009-01-24 01:40:45 +00008
9import common
10from autotest_lib.frontend.afe import model_logic
mblighe8819cd2008-02-15 16:48:40 +000011import frontend.settings
12
13AUTOTEST_DIR = os.path.abspath(os.path.join(
14 os.path.dirname(frontend.settings.__file__), '..'))
15
mblighc86113b2009-04-28 18:32:51 +000016EMPTY_TEMPLATE = 'def step_init():\n'
mblighe8819cd2008-02-15 16:48:40 +000017
showard1d445e92008-03-12 21:33:31 +000018CLIENT_KERNEL_TEMPLATE = """\
mblighc86113b2009-04-28 18:32:51 +000019kernel_list = %(client_kernel_list)s
mbligh12eafff2008-11-05 23:42:42 +000020
mblighe8819cd2008-02-15 16:48:40 +000021def step_init():
mbligha3c58d22009-08-24 22:01:51 +000022 for kernel_info in kernel_list:
23 job.next_step(boot_kernel, kernel_info)
24 job.next_step(step_test, kernel_info['version'])
mblighe39c3c02008-11-18 15:00:06 +000025 if len(kernel_list) > 1:
26 job.set_run_number(1) # Include run numbers in output directory names.
27 job.show_kernel_in_test_tag(True) # Include kernel in output dir name.
mbligh12eafff2008-11-05 23:42:42 +000028
mbligha3c58d22009-08-24 22:01:51 +000029
30def boot_kernel(kernel_info):
mbligh3c0ea962009-11-06 03:02:38 +000031 # remove kernels (and associated data) not referenced by the bootloader
32 job.host.cleanup_kernels()
33
mbligha3c58d22009-08-24 22:01:51 +000034 testkernel = job.kernel(kernel_info['version'])
35 if kernel_info['config_file']:
36 testkernel.config(kernel_info['config_file'])
mbligheaa75e52009-11-06 03:08:08 +000037 testkernel.build()
mblighf5fdfab2008-06-16 23:57:25 +000038 testkernel.install()
mbligha3c58d22009-08-24 22:01:51 +000039
40 cmdline = ' '.join((kernel_info.get('cmdline', ''), '%(kernel_args)s'))
41 testkernel.boot(args=cmdline)
42
mblighe8819cd2008-02-15 16:48:40 +000043
mbligh12eafff2008-11-05 23:42:42 +000044def step_test(kernel_version):
mbligh6d2a6f92008-11-13 16:47:52 +000045 global kernel
46 kernel = kernel_version # Set the global in case anyone is using it.
mblighe8819cd2008-02-15 16:48:40 +000047"""
48
showard1d445e92008-03-12 21:33:31 +000049SERVER_KERNEL_TEMPLATE = """\
mblighc86113b2009-04-28 18:32:51 +000050kernel_list = %%(server_kernel_list)s
showard1d445e92008-03-12 21:33:31 +000051kernel_install_control = \"""
mblighf5fdfab2008-06-16 23:57:25 +000052%s pass
showard1d445e92008-03-12 21:33:31 +000053\"""
54
showarda5512cd2009-06-30 01:59:22 +000055from autotest_lib.client.common_lib import error
56
showard1d445e92008-03-12 21:33:31 +000057at = autotest.Autotest()
mbligha3c58d22009-08-24 22:01:51 +000058def install_kernel(machine, kernel_info):
jadmanski8d631c92008-08-18 21:12:40 +000059 host = hosts.create_host(machine)
mblighc86113b2009-04-28 18:32:51 +000060 at.install(host=host)
61 at.run(kernel_install_control %%%%
mbligha3c58d22009-08-24 22:01:51 +000062 {'client_kernel_list': repr([kernel_info])}, host=host)
63
showard1d445e92008-03-12 21:33:31 +000064
mbligh415dc212009-06-15 21:53:34 +000065num_machines_required = len(machines)
66if len(machines) > 4:
67 # Allow a large multi-host tests to proceed despite a couple of hosts
68 # failing to properly install the desired kernel (exclude those hosts).
69 # TODO(gps): Figure out how to get and use SYNC_COUNT here. It is defined
70 # within some control files and will end up inside of stepN functions below.
71 num_machines_required = len(machines) - 2
72
mbligha3c58d22009-08-24 22:01:51 +000073
mblighc86113b2009-04-28 18:32:51 +000074def step_init():
75 # a host object we use solely for the purpose of finding out the booted
76 # kernel version, we use machines[0] since we already check that the same
77 # kernel has been booted on all machines
78 if len(kernel_list) > 1:
79 kernel_host = hosts.create_host(machines[0])
80
mbligha3c58d22009-08-24 22:01:51 +000081 for kernel_info in kernel_list:
82 func = lambda machine: install_kernel(machine, kernel_info)
mbligh415dc212009-06-15 21:53:34 +000083 good_machines = job.parallel_on_machines(func, machines)
84 if len(good_machines) < num_machines_required:
85 raise error.TestError(
86 "kernel installed on only %%%%d of %%%%d machines."
87 %%%% (len(good_machines), num_machines_required))
88
89 # Replace the machines list that step_test() will use with the
90 # ones that successfully installed the kernel.
91 machines[:] = good_machines
mblighc86113b2009-04-28 18:32:51 +000092
93 # have server_job.run_test() automatically add the kernel version as
94 # a suffix to the test name otherwise we cannot run the same test on
95 # different kernel versions
96 if len(kernel_list) > 1:
mbligh7eacbc22009-07-28 23:13:56 +000097 job.set_test_tag_prefix(kernel_host.get_kernel_ver())
mblighc86113b2009-04-28 18:32:51 +000098 step_test()
99
mbligha3c58d22009-08-24 22:01:51 +0000100
mblighc86113b2009-04-28 18:32:51 +0000101def step_test():
showard1d445e92008-03-12 21:33:31 +0000102""" % CLIENT_KERNEL_TEMPLATE
103
mblighf5fdfab2008-06-16 23:57:25 +0000104CLIENT_STEP_TEMPLATE = " job.next_step('step%d')\n"
mblighc86113b2009-04-28 18:32:51 +0000105SERVER_STEP_TEMPLATE = ' step%d()\n'
showard9ca52702008-06-02 21:14:49 +0000106
showard1d445e92008-03-12 21:33:31 +0000107
mbligha3c58d22009-08-24 22:01:51 +0000108def kernel_config_file(kernel, platform):
jadmanski0afbb632008-06-06 21:10:57 +0000109 if (not kernel.endswith('.rpm') and platform and
110 platform.kernel_config):
mbligha3c58d22009-08-24 22:01:51 +0000111 return platform.kernel_config
112 return None
mblighe8819cd2008-02-15 16:48:40 +0000113
114
115def read_control_file(test):
jadmanski0afbb632008-06-06 21:10:57 +0000116 control_file = open(os.path.join(AUTOTEST_DIR, test.path))
117 control_contents = control_file.read()
118 control_file.close()
119 return control_contents
mblighe8819cd2008-02-15 16:48:40 +0000120
121
mbligh12eafff2008-11-05 23:42:42 +0000122def get_kernel_stanza(kernel_list, platform=None, kernel_args='',
123 is_server=False):
showard1d445e92008-03-12 21:33:31 +0000124
mbligha3c58d22009-08-24 22:01:51 +0000125 template_args = {'kernel_args' : kernel_args}
126
127 # add 'config_file' keys to the kernel_info dictionaries
128 new_kernel_list = []
129 for kernel_info in kernel_list:
130 config_file = kernel_config_file(kernel_info['version'], platform)
131 new_kernel_info = dict(kernel_info, config_file=config_file)
132 new_kernel_list.append(new_kernel_info)
mblighc86113b2009-04-28 18:32:51 +0000133
134 if is_server:
135 template = SERVER_KERNEL_TEMPLATE
136 # leave client_kernel_list as a placeholder
137 template_args['client_kernel_list'] = '%(client_kernel_list)s'
mbligha3c58d22009-08-24 22:01:51 +0000138 template_args['server_kernel_list'] = repr(new_kernel_list)
mblighc86113b2009-04-28 18:32:51 +0000139 else:
140 template = CLIENT_KERNEL_TEMPLATE
mbligha3c58d22009-08-24 22:01:51 +0000141 template_args['client_kernel_list'] = repr(new_kernel_list)
mblighc86113b2009-04-28 18:32:51 +0000142
143 return template % template_args
mblighe8819cd2008-02-15 16:48:40 +0000144
145
showard9ca52702008-06-02 21:14:49 +0000146def add_boilerplate_to_nested_steps(lines):
jadmanski0afbb632008-06-06 21:10:57 +0000147 # Look for a line that begins with 'def step_init():' while
148 # being flexible on spacing. If it's found, this will be
149 # a nested set of steps, so add magic to make it work.
150 # See client/bin/job.py's step_engine for more info.
151 if re.search(r'^(.*\n)*def\s+step_init\s*\(\s*\)\s*:', lines):
152 lines += '\nreturn locals() '
153 lines += '# Boilerplate magic for nested sets of steps'
154 return lines
showard9ca52702008-06-02 21:14:49 +0000155
156
157def format_step(item, lines):
mblighf5fdfab2008-06-16 23:57:25 +0000158 lines = indent_text(lines, ' ')
jadmanski0afbb632008-06-06 21:10:57 +0000159 lines = 'def step%d():\n%s' % (item, lines)
160 return lines
showard9ca52702008-06-02 21:14:49 +0000161
jadmanski0afbb632008-06-06 21:10:57 +0000162
mbligh120351e2009-01-24 01:40:45 +0000163def get_tests_stanza(tests, is_server, prepend=None, append=None,
164 client_control_file=''):
165 """ Constructs the control file test step code from a list of tests.
mbligh12eafff2008-11-05 23:42:42 +0000166
mbligh120351e2009-01-24 01:40:45 +0000167 @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 a
174 final client control file to run after tests. is_server must be False.
175
176 @returns The control file test code to be run.
mbligh12eafff2008-11-05 23:42:42 +0000177 """
mbligh120351e2009-01-24 01:40:45 +0000178 assert not (client_control_file and is_server)
mbligh12eafff2008-11-05 23:42:42 +0000179 if not prepend:
180 prepend = []
181 if not append:
182 append = []
jadmanski0afbb632008-06-06 21:10:57 +0000183 raw_control_files = [read_control_file(test) for test in tests]
mbligh120351e2009-01-24 01:40:45 +0000184 return _get_tests_stanza(raw_control_files, is_server, prepend, append,
185 client_control_file=client_control_file)
mblighc5ddfd12008-08-04 17:15:00 +0000186
187
mbligh120351e2009-01-24 01:40:45 +0000188def _get_tests_stanza(raw_control_files, is_server, prepend, append,
189 client_control_file=''):
190 """
191 Implements the common parts of get_test_stanza.
192
193 A site_control_file that wants to implement its own get_tests_stanza
194 likely wants to call this in the end.
195
196 @param raw_control_files A list of raw control file data to be combined
197 into a single control file.
198 @param is_server bool, Is this a server side test?
199 @param prepend A list of steps to prepend to each client test.
200 @param append A list of steps to append to each client test.
201 @param client_control_file If specified, use this text as the body of a
202 final client control file to append to raw_control_files after fixups.
203
204 @returns The combined mega control file.
205 """
mbligh120351e2009-01-24 01:40:45 +0000206 if client_control_file:
207 # 'return locals()' is always appended incase the user forgot, it
208 # is necessary to allow for nested step engine execution to work.
209 raw_control_files.append(client_control_file + '\nreturn locals()')
jadmanski0afbb632008-06-06 21:10:57 +0000210 raw_steps = prepend + [add_boilerplate_to_nested_steps(step)
211 for step in raw_control_files] + append
212 steps = [format_step(index, step)
213 for index, step in enumerate(raw_steps)]
mblighc86113b2009-04-28 18:32:51 +0000214 if is_server:
215 step_template = SERVER_STEP_TEMPLATE
216 footer = '\n\nstep_init()\n'
217 else:
218 step_template = CLIENT_STEP_TEMPLATE
219 footer = ''
220
221 header = ''.join(step_template % i for i in xrange(len(steps)))
222 return header + '\n' + '\n\n'.join(steps) + footer
mblighe8819cd2008-02-15 16:48:40 +0000223
224
225def indent_text(text, indent):
showardd2624152009-04-29 21:29:01 +0000226 """Indent given lines of python code avoiding indenting multiline
227 quoted content (only for triple " and ' quoting for now)."""
228 regex = re.compile('(\\\\*)("""|\'\'\')')
229
230 res = []
231 in_quote = None
232 for line in text.splitlines():
233 # if not within a multinline quote indent the line contents
234 if in_quote:
235 res.append(line)
236 else:
237 res.append(indent + line)
238
239 while line:
240 match = regex.search(line)
241 if match:
242 # for an even number of backslashes before the triple quote
243 if len(match.group(1)) % 2 == 0:
244 if not in_quote:
245 in_quote = match.group(2)[0]
246 elif in_quote == match.group(2)[0]:
247 # if we found a matching end triple quote
248 in_quote = None
249 line = line[match.end():]
250 else:
251 break
252
253 return '\n'.join(res)
mblighe8819cd2008-02-15 16:48:40 +0000254
255
showard91f85102009-10-12 20:34:52 +0000256def _get_profiler_commands(profilers, is_server, profile_only):
showard2b9a88b2008-06-13 20:55:03 +0000257 prepend, append = [], []
showard91f85102009-10-12 20:34:52 +0000258 if profile_only is not None:
259 prepend.append("job.set_default_profile_only(%r)" % profile_only)
showard2b9a88b2008-06-13 20:55:03 +0000260 for profiler in profilers:
261 prepend.append("job.profilers.add('%s')" % profiler.name)
262 append.append("job.profilers.delete('%s')" % profiler.name)
263 return prepend, append
264
265
mbligha3c58d22009-08-24 22:01:51 +0000266def _sanity_check_generate_control(is_server, client_control_file, kernels):
mbligh12eafff2008-11-05 23:42:42 +0000267 """
mbligh120351e2009-01-24 01:40:45 +0000268 Sanity check some of the parameters to generate_control().
269
270 This exists as its own function so that site_control_file may call it as
271 well from its own generate_control().
272
273 @raises ValidationError if any of the parameters do not make sense.
274 """
275 if is_server and client_control_file:
276 raise model_logic.ValidationError(
277 {'tests' : 'You cannot run server tests at the same time '
278 'as directly supplying a client-side control file.'})
279
mbligha3c58d22009-08-24 22:01:51 +0000280 if kernels:
281 # make sure that kernel is a list of dictionarions with at least
282 # the 'version' key in them
283 kernel_error = model_logic.ValidationError(
284 {'kernel': 'The kernel parameter must be a sequence of '
285 'dictionaries containing at least the "version" key '
286 '(got: %r)' % kernels})
287 try:
288 iter(kernels)
289 except TypeError:
290 raise kernel_error
291 for kernel_info in kernels:
292 if (not isinstance(kernel_info, dict) or
293 'version' not in kernel_info):
294 raise kernel_error
mbligh120351e2009-01-24 01:40:45 +0000295
mbligha3c58d22009-08-24 22:01:51 +0000296
297def generate_control(tests, kernels=None, platform=None, is_server=False,
showard91f85102009-10-12 20:34:52 +0000298 profilers=(), client_control_file='', profile_only=None):
mbligh120351e2009-01-24 01:40:45 +0000299 """
300 Generate a control file for a sequence of tests.
301
302 @param tests A sequence of test control files to run.
mbligha3c58d22009-08-24 22:01:51 +0000303 @param kernels A sequence of kernel info dictionaries configuring which
304 kernels to boot for this job and other options for them
mbligh120351e2009-01-24 01:40:45 +0000305 @param platform A platform object with a kernel_config attribute.
306 @param is_server bool, Is this a server control file rather than a client?
307 @param profilers A list of profiler objects to enable during the tests.
308 @param client_control_file Contents of a client control file to run as the
mbligha3c58d22009-08-24 22:01:51 +0000309 last test after everything in tests. Requires is_server=False.
showard91f85102009-10-12 20:34:52 +0000310 @param profile_only bool, should this control file run all tests in
311 profile_only mode by default
mbligh120351e2009-01-24 01:40:45 +0000312
313 @returns The control file text as a string.
314 """
mbligha3c58d22009-08-24 22:01:51 +0000315 _sanity_check_generate_control(is_server=is_server, kernels=kernels,
mbligh120351e2009-01-24 01:40:45 +0000316 client_control_file=client_control_file)
317
jadmanski0afbb632008-06-06 21:10:57 +0000318 control_file_text = ''
mbligha3c58d22009-08-24 22:01:51 +0000319 if kernels:
320 control_file_text = get_kernel_stanza(kernels, platform,
jadmanski0afbb632008-06-06 21:10:57 +0000321 is_server=is_server)
mblighc86113b2009-04-28 18:32:51 +0000322 else:
323 control_file_text = EMPTY_TEMPLATE
showard9ca52702008-06-02 21:14:49 +0000324
showard91f85102009-10-12 20:34:52 +0000325 prepend, append = _get_profiler_commands(profilers, is_server, profile_only)
showard2b9a88b2008-06-13 20:55:03 +0000326
mbligh120351e2009-01-24 01:40:45 +0000327 control_file_text += get_tests_stanza(tests, is_server, prepend, append,
328 client_control_file)
jadmanski0afbb632008-06-06 21:10:57 +0000329 return control_file_text