blob: 3ce068653d3d7470b4f819aa50f5a18176a407d1 [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
showarda5512cd2009-06-30 01:59:22 +000046from autotest_lib.client.common_lib import error
47
showard1d445e92008-03-12 21:33:31 +000048at = autotest.Autotest()
mblighc86113b2009-04-28 18:32:51 +000049def install_kernel(machine, kernel_version):
jadmanski8d631c92008-08-18 21:12:40 +000050 host = hosts.create_host(machine)
mblighc86113b2009-04-28 18:32:51 +000051 at.install(host=host)
52 at.run(kernel_install_control %%%%
53 {'client_kernel_list': repr([kernel_version])}, host=host)
showard1d445e92008-03-12 21:33:31 +000054
mbligh415dc212009-06-15 21:53:34 +000055num_machines_required = len(machines)
56if len(machines) > 4:
57 # Allow a large multi-host tests to proceed despite a couple of hosts
58 # failing to properly install the desired kernel (exclude those hosts).
59 # TODO(gps): Figure out how to get and use SYNC_COUNT here. It is defined
60 # within some control files and will end up inside of stepN functions below.
61 num_machines_required = len(machines) - 2
62
mblighc86113b2009-04-28 18:32:51 +000063def step_init():
64 # a host object we use solely for the purpose of finding out the booted
65 # kernel version, we use machines[0] since we already check that the same
66 # kernel has been booted on all machines
67 if len(kernel_list) > 1:
68 kernel_host = hosts.create_host(machines[0])
69
70 for kernel_version in kernel_list:
71 func = lambda machine: install_kernel(machine, kernel_version)
mbligh415dc212009-06-15 21:53:34 +000072 good_machines = job.parallel_on_machines(func, machines)
73 if len(good_machines) < num_machines_required:
74 raise error.TestError(
75 "kernel installed on only %%%%d of %%%%d machines."
76 %%%% (len(good_machines), num_machines_required))
77
78 # Replace the machines list that step_test() will use with the
79 # ones that successfully installed the kernel.
80 machines[:] = good_machines
mblighc86113b2009-04-28 18:32:51 +000081
82 # have server_job.run_test() automatically add the kernel version as
83 # a suffix to the test name otherwise we cannot run the same test on
84 # different kernel versions
85 if len(kernel_list) > 1:
mbligh7eacbc22009-07-28 23:13:56 +000086 job.set_test_tag_prefix(kernel_host.get_kernel_ver())
mblighc86113b2009-04-28 18:32:51 +000087 step_test()
88
89def step_test():
showard1d445e92008-03-12 21:33:31 +000090""" % CLIENT_KERNEL_TEMPLATE
91
mblighf5fdfab2008-06-16 23:57:25 +000092CLIENT_STEP_TEMPLATE = " job.next_step('step%d')\n"
mblighc86113b2009-04-28 18:32:51 +000093SERVER_STEP_TEMPLATE = ' step%d()\n'
showard9ca52702008-06-02 21:14:49 +000094
showard1d445e92008-03-12 21:33:31 +000095
mblighe8819cd2008-02-15 16:48:40 +000096def kernel_config_line(kernel, platform):
jadmanski0afbb632008-06-06 21:10:57 +000097 if (not kernel.endswith('.rpm') and platform and
98 platform.kernel_config):
99 return "testkernel.config('%s')" % platform.kernel_config
100 return ''
mblighe8819cd2008-02-15 16:48:40 +0000101
102
103def read_control_file(test):
jadmanski0afbb632008-06-06 21:10:57 +0000104 control_file = open(os.path.join(AUTOTEST_DIR, test.path))
105 control_contents = control_file.read()
106 control_file.close()
107 return control_contents
mblighe8819cd2008-02-15 16:48:40 +0000108
109
mbligh12eafff2008-11-05 23:42:42 +0000110def get_kernel_stanza(kernel_list, platform=None, kernel_args='',
111 is_server=False):
showard1d445e92008-03-12 21:33:31 +0000112
mblighc86113b2009-04-28 18:32:51 +0000113 template_args = {
mbligh12eafff2008-11-05 23:42:42 +0000114 # XXX This always looks up the config line using the first kernel
115 # in the list rather than doing it for each kernel.
116 'kernel_config_line' : kernel_config_line(kernel_list[0], platform),
jadmanski0afbb632008-06-06 21:10:57 +0000117 'kernel_args' : kernel_args}
mblighc86113b2009-04-28 18:32:51 +0000118
119 if is_server:
120 template = SERVER_KERNEL_TEMPLATE
121 # leave client_kernel_list as a placeholder
122 template_args['client_kernel_list'] = '%(client_kernel_list)s'
123 template_args['server_kernel_list'] = repr(kernel_list)
124 else:
125 template = CLIENT_KERNEL_TEMPLATE
126 template_args['client_kernel_list'] = repr(kernel_list)
127
128 return template % template_args
mblighe8819cd2008-02-15 16:48:40 +0000129
130
showard9ca52702008-06-02 21:14:49 +0000131def add_boilerplate_to_nested_steps(lines):
jadmanski0afbb632008-06-06 21:10:57 +0000132 # Look for a line that begins with 'def step_init():' while
133 # being flexible on spacing. If it's found, this will be
134 # a nested set of steps, so add magic to make it work.
135 # See client/bin/job.py's step_engine for more info.
136 if re.search(r'^(.*\n)*def\s+step_init\s*\(\s*\)\s*:', lines):
137 lines += '\nreturn locals() '
138 lines += '# Boilerplate magic for nested sets of steps'
139 return lines
showard9ca52702008-06-02 21:14:49 +0000140
141
142def format_step(item, lines):
mblighf5fdfab2008-06-16 23:57:25 +0000143 lines = indent_text(lines, ' ')
jadmanski0afbb632008-06-06 21:10:57 +0000144 lines = 'def step%d():\n%s' % (item, lines)
145 return lines
showard9ca52702008-06-02 21:14:49 +0000146
jadmanski0afbb632008-06-06 21:10:57 +0000147
mbligh120351e2009-01-24 01:40:45 +0000148def get_tests_stanza(tests, is_server, prepend=None, append=None,
149 client_control_file=''):
150 """ Constructs the control file test step code from a list of tests.
mbligh12eafff2008-11-05 23:42:42 +0000151
mbligh120351e2009-01-24 01:40:45 +0000152 @param tests A sequence of test control files to run.
153 @param is_server bool, Is this a server side test?
154 @param prepend A list of steps to prepend to each client test.
155 Defaults to [].
156 @param append A list of steps to append to each client test.
157 Defaults to [].
158 @param client_control_file If specified, use this text as the body of a
159 final client control file to run after tests. is_server must be False.
160
161 @returns The control file test code to be run.
mbligh12eafff2008-11-05 23:42:42 +0000162 """
mbligh120351e2009-01-24 01:40:45 +0000163 assert not (client_control_file and is_server)
mbligh12eafff2008-11-05 23:42:42 +0000164 if not prepend:
165 prepend = []
166 if not append:
167 append = []
jadmanski0afbb632008-06-06 21:10:57 +0000168 raw_control_files = [read_control_file(test) for test in tests]
mbligh120351e2009-01-24 01:40:45 +0000169 return _get_tests_stanza(raw_control_files, is_server, prepend, append,
170 client_control_file=client_control_file)
mblighc5ddfd12008-08-04 17:15:00 +0000171
172
mbligh120351e2009-01-24 01:40:45 +0000173def _get_tests_stanza(raw_control_files, is_server, prepend, append,
174 client_control_file=''):
175 """
176 Implements the common parts of get_test_stanza.
177
178 A site_control_file that wants to implement its own get_tests_stanza
179 likely wants to call this in the end.
180
181 @param raw_control_files A list of raw control file data to be combined
182 into a single control file.
183 @param is_server bool, Is this a server side test?
184 @param prepend A list of steps to prepend to each client test.
185 @param append A list of steps to append to each client test.
186 @param client_control_file If specified, use this text as the body of a
187 final client control file to append to raw_control_files after fixups.
188
189 @returns The combined mega control file.
190 """
mbligh120351e2009-01-24 01:40:45 +0000191 if client_control_file:
192 # 'return locals()' is always appended incase the user forgot, it
193 # is necessary to allow for nested step engine execution to work.
194 raw_control_files.append(client_control_file + '\nreturn locals()')
jadmanski0afbb632008-06-06 21:10:57 +0000195 raw_steps = prepend + [add_boilerplate_to_nested_steps(step)
196 for step in raw_control_files] + append
197 steps = [format_step(index, step)
198 for index, step in enumerate(raw_steps)]
mblighc86113b2009-04-28 18:32:51 +0000199 if is_server:
200 step_template = SERVER_STEP_TEMPLATE
201 footer = '\n\nstep_init()\n'
202 else:
203 step_template = CLIENT_STEP_TEMPLATE
204 footer = ''
205
206 header = ''.join(step_template % i for i in xrange(len(steps)))
207 return header + '\n' + '\n\n'.join(steps) + footer
mblighe8819cd2008-02-15 16:48:40 +0000208
209
210def indent_text(text, indent):
showardd2624152009-04-29 21:29:01 +0000211 """Indent given lines of python code avoiding indenting multiline
212 quoted content (only for triple " and ' quoting for now)."""
213 regex = re.compile('(\\\\*)("""|\'\'\')')
214
215 res = []
216 in_quote = None
217 for line in text.splitlines():
218 # if not within a multinline quote indent the line contents
219 if in_quote:
220 res.append(line)
221 else:
222 res.append(indent + line)
223
224 while line:
225 match = regex.search(line)
226 if match:
227 # for an even number of backslashes before the triple quote
228 if len(match.group(1)) % 2 == 0:
229 if not in_quote:
230 in_quote = match.group(2)[0]
231 elif in_quote == match.group(2)[0]:
232 # if we found a matching end triple quote
233 in_quote = None
234 line = line[match.end():]
235 else:
236 break
237
238 return '\n'.join(res)
mblighe8819cd2008-02-15 16:48:40 +0000239
240
showard2b9a88b2008-06-13 20:55:03 +0000241def _get_profiler_commands(profilers, is_server):
showard2b9a88b2008-06-13 20:55:03 +0000242 prepend, append = [], []
showard2b9a88b2008-06-13 20:55:03 +0000243 for profiler in profilers:
244 prepend.append("job.profilers.add('%s')" % profiler.name)
245 append.append("job.profilers.delete('%s')" % profiler.name)
246 return prepend, append
247
248
mbligh12eafff2008-11-05 23:42:42 +0000249def split_kernel_list(kernel_string):
250 """Split the kernel(s) string from the user into a list of kernels.
251
252 We allow multiple kernels to be listed separated by a space or comma.
253 """
254 return re.split('[\s,]+', kernel_string.strip())
255
256
mbligh120351e2009-01-24 01:40:45 +0000257def _sanity_check_generate_control(is_server, client_control_file, kernel):
mbligh12eafff2008-11-05 23:42:42 +0000258 """
mbligh120351e2009-01-24 01:40:45 +0000259 Sanity check some of the parameters to generate_control().
260
261 This exists as its own function so that site_control_file may call it as
262 well from its own generate_control().
263
264 @raises ValidationError if any of the parameters do not make sense.
265 """
266 if is_server and client_control_file:
267 raise model_logic.ValidationError(
268 {'tests' : 'You cannot run server tests at the same time '
269 'as directly supplying a client-side control file.'})
270
271
272def generate_control(tests, kernel=None, platform=None, is_server=False,
273 profilers=(), client_control_file=''):
274 """
275 Generate a control file for a sequence of tests.
276
277 @param tests A sequence of test control files to run.
278 @param kernel A string listing one or more kernel versions to test
279 separated by spaces or commas.
280 @param platform A platform object with a kernel_config attribute.
281 @param is_server bool, Is this a server control file rather than a client?
282 @param profilers A list of profiler objects to enable during the tests.
283 @param client_control_file Contents of a client control file to run as the
284 last test after everything in tests. Requires is_server=False.
285
286 @returns The control file text as a string.
287 """
288 _sanity_check_generate_control(is_server=is_server, kernel=kernel,
289 client_control_file=client_control_file)
290
jadmanski0afbb632008-06-06 21:10:57 +0000291 control_file_text = ''
292 if kernel:
mbligh12eafff2008-11-05 23:42:42 +0000293 kernel_list = split_kernel_list(kernel)
294 control_file_text = get_kernel_stanza(kernel_list, platform,
jadmanski0afbb632008-06-06 21:10:57 +0000295 is_server=is_server)
mblighc86113b2009-04-28 18:32:51 +0000296 else:
297 control_file_text = EMPTY_TEMPLATE
showard9ca52702008-06-02 21:14:49 +0000298
showard2b9a88b2008-06-13 20:55:03 +0000299 prepend, append = _get_profiler_commands(profilers, is_server)
300
mbligh120351e2009-01-24 01:40:45 +0000301 control_file_text += get_tests_stanza(tests, is_server, prepend, append,
302 client_control_file)
jadmanski0afbb632008-06-06 21:10:57 +0000303 return control_file_text