blob: 501045d6b61c741d77ccfe1f490b9ab5930b8d74 [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():
mbligh12eafff2008-11-05 23:42:42 +000022 for kernel_version in kernel_list:
23 job.next_step(boot_kernel, kernel_version)
24 job.next_step(step_test, kernel_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
29def boot_kernel(kernel_version):
mbligh12eafff2008-11-05 23:42:42 +000030 testkernel = job.kernel(kernel_version)
mblighf5fdfab2008-06-16 23:57:25 +000031 %(kernel_config_line)s
32 testkernel.install()
33 testkernel.boot(args='%(kernel_args)s')
mblighe8819cd2008-02-15 16:48:40 +000034
mbligh12eafff2008-11-05 23:42:42 +000035def step_test(kernel_version):
mbligh6d2a6f92008-11-13 16:47:52 +000036 global kernel
37 kernel = kernel_version # Set the global in case anyone is using it.
mblighe8819cd2008-02-15 16:48:40 +000038"""
39
showard1d445e92008-03-12 21:33:31 +000040SERVER_KERNEL_TEMPLATE = """\
mblighc86113b2009-04-28 18:32:51 +000041kernel_list = %%(server_kernel_list)s
showard1d445e92008-03-12 21:33:31 +000042kernel_install_control = \"""
mblighf5fdfab2008-06-16 23:57:25 +000043%s pass
showard1d445e92008-03-12 21:33:31 +000044\"""
45
46at = autotest.Autotest()
mblighc86113b2009-04-28 18:32:51 +000047def install_kernel(machine, kernel_version):
jadmanski8d631c92008-08-18 21:12:40 +000048 host = hosts.create_host(machine)
mblighc86113b2009-04-28 18:32:51 +000049 at.install(host=host)
50 at.run(kernel_install_control %%%%
51 {'client_kernel_list': repr([kernel_version])}, host=host)
showard1d445e92008-03-12 21:33:31 +000052
mbligh415dc212009-06-15 21:53:34 +000053num_machines_required = len(machines)
54if len(machines) > 4:
55 # Allow a large multi-host tests to proceed despite a couple of hosts
56 # failing to properly install the desired kernel (exclude those hosts).
57 # TODO(gps): Figure out how to get and use SYNC_COUNT here. It is defined
58 # within some control files and will end up inside of stepN functions below.
59 num_machines_required = len(machines) - 2
60
mblighc86113b2009-04-28 18:32:51 +000061def step_init():
62 # a host object we use solely for the purpose of finding out the booted
63 # kernel version, we use machines[0] since we already check that the same
64 # kernel has been booted on all machines
65 if len(kernel_list) > 1:
66 kernel_host = hosts.create_host(machines[0])
67
68 for kernel_version in kernel_list:
69 func = lambda machine: install_kernel(machine, kernel_version)
mbligh415dc212009-06-15 21:53:34 +000070 good_machines = job.parallel_on_machines(func, machines)
71 if len(good_machines) < num_machines_required:
72 raise error.TestError(
73 "kernel installed on only %%%%d of %%%%d machines."
74 %%%% (len(good_machines), num_machines_required))
75
76 # Replace the machines list that step_test() will use with the
77 # ones that successfully installed the kernel.
78 machines[:] = good_machines
mblighc86113b2009-04-28 18:32:51 +000079
80 # have server_job.run_test() automatically add the kernel version as
81 # a suffix to the test name otherwise we cannot run the same test on
82 # different kernel versions
83 if len(kernel_list) > 1:
84 job.set_test_tag(kernel_host.get_kernel_ver())
85 step_test()
86
87def step_test():
showard1d445e92008-03-12 21:33:31 +000088""" % CLIENT_KERNEL_TEMPLATE
89
mblighf5fdfab2008-06-16 23:57:25 +000090CLIENT_STEP_TEMPLATE = " job.next_step('step%d')\n"
mblighc86113b2009-04-28 18:32:51 +000091SERVER_STEP_TEMPLATE = ' step%d()\n'
showard9ca52702008-06-02 21:14:49 +000092
showard1d445e92008-03-12 21:33:31 +000093
mblighe8819cd2008-02-15 16:48:40 +000094def kernel_config_line(kernel, platform):
jadmanski0afbb632008-06-06 21:10:57 +000095 if (not kernel.endswith('.rpm') and platform and
96 platform.kernel_config):
97 return "testkernel.config('%s')" % platform.kernel_config
98 return ''
mblighe8819cd2008-02-15 16:48:40 +000099
100
101def read_control_file(test):
jadmanski0afbb632008-06-06 21:10:57 +0000102 control_file = open(os.path.join(AUTOTEST_DIR, test.path))
103 control_contents = control_file.read()
104 control_file.close()
105 return control_contents
mblighe8819cd2008-02-15 16:48:40 +0000106
107
mbligh12eafff2008-11-05 23:42:42 +0000108def get_kernel_stanza(kernel_list, platform=None, kernel_args='',
109 is_server=False):
showard1d445e92008-03-12 21:33:31 +0000110
mblighc86113b2009-04-28 18:32:51 +0000111 template_args = {
mbligh12eafff2008-11-05 23:42:42 +0000112 # XXX This always looks up the config line using the first kernel
113 # in the list rather than doing it for each kernel.
114 'kernel_config_line' : kernel_config_line(kernel_list[0], platform),
jadmanski0afbb632008-06-06 21:10:57 +0000115 'kernel_args' : kernel_args}
mblighc86113b2009-04-28 18:32:51 +0000116
117 if is_server:
118 template = SERVER_KERNEL_TEMPLATE
119 # leave client_kernel_list as a placeholder
120 template_args['client_kernel_list'] = '%(client_kernel_list)s'
121 template_args['server_kernel_list'] = repr(kernel_list)
122 else:
123 template = CLIENT_KERNEL_TEMPLATE
124 template_args['client_kernel_list'] = repr(kernel_list)
125
126 return template % template_args
mblighe8819cd2008-02-15 16:48:40 +0000127
128
showard9ca52702008-06-02 21:14:49 +0000129def add_boilerplate_to_nested_steps(lines):
jadmanski0afbb632008-06-06 21:10:57 +0000130 # Look for a line that begins with 'def step_init():' while
131 # being flexible on spacing. If it's found, this will be
132 # a nested set of steps, so add magic to make it work.
133 # See client/bin/job.py's step_engine for more info.
134 if re.search(r'^(.*\n)*def\s+step_init\s*\(\s*\)\s*:', lines):
135 lines += '\nreturn locals() '
136 lines += '# Boilerplate magic for nested sets of steps'
137 return lines
showard9ca52702008-06-02 21:14:49 +0000138
139
140def format_step(item, lines):
mblighf5fdfab2008-06-16 23:57:25 +0000141 lines = indent_text(lines, ' ')
jadmanski0afbb632008-06-06 21:10:57 +0000142 lines = 'def step%d():\n%s' % (item, lines)
143 return lines
showard9ca52702008-06-02 21:14:49 +0000144
jadmanski0afbb632008-06-06 21:10:57 +0000145
mbligh120351e2009-01-24 01:40:45 +0000146def get_tests_stanza(tests, is_server, prepend=None, append=None,
147 client_control_file=''):
148 """ Constructs the control file test step code from a list of tests.
mbligh12eafff2008-11-05 23:42:42 +0000149
mbligh120351e2009-01-24 01:40:45 +0000150 @param tests A sequence of test control files to run.
151 @param is_server bool, Is this a server side test?
152 @param prepend A list of steps to prepend to each client test.
153 Defaults to [].
154 @param append A list of steps to append to each client test.
155 Defaults to [].
156 @param client_control_file If specified, use this text as the body of a
157 final client control file to run after tests. is_server must be False.
158
159 @returns The control file test code to be run.
mbligh12eafff2008-11-05 23:42:42 +0000160 """
mbligh120351e2009-01-24 01:40:45 +0000161 assert not (client_control_file and is_server)
mbligh12eafff2008-11-05 23:42:42 +0000162 if not prepend:
163 prepend = []
164 if not append:
165 append = []
jadmanski0afbb632008-06-06 21:10:57 +0000166 raw_control_files = [read_control_file(test) for test in tests]
mbligh120351e2009-01-24 01:40:45 +0000167 return _get_tests_stanza(raw_control_files, is_server, prepend, append,
168 client_control_file=client_control_file)
mblighc5ddfd12008-08-04 17:15:00 +0000169
170
mbligh120351e2009-01-24 01:40:45 +0000171def _get_tests_stanza(raw_control_files, is_server, prepend, append,
172 client_control_file=''):
173 """
174 Implements the common parts of get_test_stanza.
175
176 A site_control_file that wants to implement its own get_tests_stanza
177 likely wants to call this in the end.
178
179 @param raw_control_files A list of raw control file data to be combined
180 into a single control file.
181 @param is_server bool, Is this a server side test?
182 @param prepend A list of steps to prepend to each client test.
183 @param append A list of steps to append to each client test.
184 @param client_control_file If specified, use this text as the body of a
185 final client control file to append to raw_control_files after fixups.
186
187 @returns The combined mega control file.
188 """
mbligh120351e2009-01-24 01:40:45 +0000189 if client_control_file:
190 # 'return locals()' is always appended incase the user forgot, it
191 # is necessary to allow for nested step engine execution to work.
192 raw_control_files.append(client_control_file + '\nreturn locals()')
jadmanski0afbb632008-06-06 21:10:57 +0000193 raw_steps = prepend + [add_boilerplate_to_nested_steps(step)
194 for step in raw_control_files] + append
195 steps = [format_step(index, step)
196 for index, step in enumerate(raw_steps)]
mblighc86113b2009-04-28 18:32:51 +0000197 if is_server:
198 step_template = SERVER_STEP_TEMPLATE
199 footer = '\n\nstep_init()\n'
200 else:
201 step_template = CLIENT_STEP_TEMPLATE
202 footer = ''
203
204 header = ''.join(step_template % i for i in xrange(len(steps)))
205 return header + '\n' + '\n\n'.join(steps) + footer
mblighe8819cd2008-02-15 16:48:40 +0000206
207
208def indent_text(text, indent):
showardd2624152009-04-29 21:29:01 +0000209 """Indent given lines of python code avoiding indenting multiline
210 quoted content (only for triple " and ' quoting for now)."""
211 regex = re.compile('(\\\\*)("""|\'\'\')')
212
213 res = []
214 in_quote = None
215 for line in text.splitlines():
216 # if not within a multinline quote indent the line contents
217 if in_quote:
218 res.append(line)
219 else:
220 res.append(indent + line)
221
222 while line:
223 match = regex.search(line)
224 if match:
225 # for an even number of backslashes before the triple quote
226 if len(match.group(1)) % 2 == 0:
227 if not in_quote:
228 in_quote = match.group(2)[0]
229 elif in_quote == match.group(2)[0]:
230 # if we found a matching end triple quote
231 in_quote = None
232 line = line[match.end():]
233 else:
234 break
235
236 return '\n'.join(res)
mblighe8819cd2008-02-15 16:48:40 +0000237
238
showard2b9a88b2008-06-13 20:55:03 +0000239def _get_profiler_commands(profilers, is_server):
showard2b9a88b2008-06-13 20:55:03 +0000240 prepend, append = [], []
showard2b9a88b2008-06-13 20:55:03 +0000241 for profiler in profilers:
242 prepend.append("job.profilers.add('%s')" % profiler.name)
243 append.append("job.profilers.delete('%s')" % profiler.name)
244 return prepend, append
245
246
mbligh12eafff2008-11-05 23:42:42 +0000247def split_kernel_list(kernel_string):
248 """Split the kernel(s) string from the user into a list of kernels.
249
250 We allow multiple kernels to be listed separated by a space or comma.
251 """
252 return re.split('[\s,]+', kernel_string.strip())
253
254
mbligh120351e2009-01-24 01:40:45 +0000255def _sanity_check_generate_control(is_server, client_control_file, kernel):
mbligh12eafff2008-11-05 23:42:42 +0000256 """
mbligh120351e2009-01-24 01:40:45 +0000257 Sanity check some of the parameters to generate_control().
258
259 This exists as its own function so that site_control_file may call it as
260 well from its own generate_control().
261
262 @raises ValidationError if any of the parameters do not make sense.
263 """
264 if is_server and client_control_file:
265 raise model_logic.ValidationError(
266 {'tests' : 'You cannot run server tests at the same time '
267 'as directly supplying a client-side control file.'})
268
269
270def generate_control(tests, kernel=None, platform=None, is_server=False,
271 profilers=(), client_control_file=''):
272 """
273 Generate a control file for a sequence of tests.
274
275 @param tests A sequence of test control files to run.
276 @param kernel A string listing one or more kernel versions to test
277 separated by spaces or commas.
278 @param platform A platform object with a kernel_config attribute.
279 @param is_server bool, Is this a server control file rather than a client?
280 @param profilers A list of profiler objects to enable during the tests.
281 @param client_control_file Contents of a client control file to run as the
282 last test after everything in tests. Requires is_server=False.
283
284 @returns The control file text as a string.
285 """
286 _sanity_check_generate_control(is_server=is_server, kernel=kernel,
287 client_control_file=client_control_file)
288
jadmanski0afbb632008-06-06 21:10:57 +0000289 control_file_text = ''
290 if kernel:
mbligh12eafff2008-11-05 23:42:42 +0000291 kernel_list = split_kernel_list(kernel)
292 control_file_text = get_kernel_stanza(kernel_list, platform,
jadmanski0afbb632008-06-06 21:10:57 +0000293 is_server=is_server)
mblighc86113b2009-04-28 18:32:51 +0000294 else:
295 control_file_text = EMPTY_TEMPLATE
showard9ca52702008-06-02 21:14:49 +0000296
showard2b9a88b2008-06-13 20:55:03 +0000297 prepend, append = _get_profiler_commands(profilers, is_server)
298
mbligh120351e2009-01-24 01:40:45 +0000299 control_file_text += get_tests_stanza(tests, is_server, prepend, append,
300 client_control_file)
jadmanski0afbb632008-06-06 21:10:57 +0000301 return control_file_text