blob: c78010e31df3b3d2854ff7f6c98f5052774c1b51 [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
mblighc86113b2009-04-28 18:32:51 +000053def step_init():
54 # a host object we use solely for the purpose of finding out the booted
55 # kernel version, we use machines[0] since we already check that the same
56 # kernel has been booted on all machines
57 if len(kernel_list) > 1:
58 kernel_host = hosts.create_host(machines[0])
59
60 for kernel_version in kernel_list:
61 func = lambda machine: install_kernel(machine, kernel_version)
62 job.parallel_simple(func, machines)
63
64 # have server_job.run_test() automatically add the kernel version as
65 # a suffix to the test name otherwise we cannot run the same test on
66 # different kernel versions
67 if len(kernel_list) > 1:
68 job.set_test_tag(kernel_host.get_kernel_ver())
69 step_test()
70
71def step_test():
showard1d445e92008-03-12 21:33:31 +000072""" % CLIENT_KERNEL_TEMPLATE
73
mblighf5fdfab2008-06-16 23:57:25 +000074CLIENT_STEP_TEMPLATE = " job.next_step('step%d')\n"
mblighc86113b2009-04-28 18:32:51 +000075SERVER_STEP_TEMPLATE = ' step%d()\n'
showard9ca52702008-06-02 21:14:49 +000076
showard1d445e92008-03-12 21:33:31 +000077
mblighe8819cd2008-02-15 16:48:40 +000078def kernel_config_line(kernel, platform):
jadmanski0afbb632008-06-06 21:10:57 +000079 if (not kernel.endswith('.rpm') and platform and
80 platform.kernel_config):
81 return "testkernel.config('%s')" % platform.kernel_config
82 return ''
mblighe8819cd2008-02-15 16:48:40 +000083
84
85def read_control_file(test):
jadmanski0afbb632008-06-06 21:10:57 +000086 control_file = open(os.path.join(AUTOTEST_DIR, test.path))
87 control_contents = control_file.read()
88 control_file.close()
89 return control_contents
mblighe8819cd2008-02-15 16:48:40 +000090
91
mbligh12eafff2008-11-05 23:42:42 +000092def get_kernel_stanza(kernel_list, platform=None, kernel_args='',
93 is_server=False):
showard1d445e92008-03-12 21:33:31 +000094
mblighc86113b2009-04-28 18:32:51 +000095 template_args = {
mbligh12eafff2008-11-05 23:42:42 +000096 # XXX This always looks up the config line using the first kernel
97 # in the list rather than doing it for each kernel.
98 'kernel_config_line' : kernel_config_line(kernel_list[0], platform),
jadmanski0afbb632008-06-06 21:10:57 +000099 'kernel_args' : kernel_args}
mblighc86113b2009-04-28 18:32:51 +0000100
101 if is_server:
102 template = SERVER_KERNEL_TEMPLATE
103 # leave client_kernel_list as a placeholder
104 template_args['client_kernel_list'] = '%(client_kernel_list)s'
105 template_args['server_kernel_list'] = repr(kernel_list)
106 else:
107 template = CLIENT_KERNEL_TEMPLATE
108 template_args['client_kernel_list'] = repr(kernel_list)
109
110 return template % template_args
mblighe8819cd2008-02-15 16:48:40 +0000111
112
showard9ca52702008-06-02 21:14:49 +0000113def add_boilerplate_to_nested_steps(lines):
jadmanski0afbb632008-06-06 21:10:57 +0000114 # Look for a line that begins with 'def step_init():' while
115 # being flexible on spacing. If it's found, this will be
116 # a nested set of steps, so add magic to make it work.
117 # See client/bin/job.py's step_engine for more info.
118 if re.search(r'^(.*\n)*def\s+step_init\s*\(\s*\)\s*:', lines):
119 lines += '\nreturn locals() '
120 lines += '# Boilerplate magic for nested sets of steps'
121 return lines
showard9ca52702008-06-02 21:14:49 +0000122
123
124def format_step(item, lines):
mblighf5fdfab2008-06-16 23:57:25 +0000125 lines = indent_text(lines, ' ')
jadmanski0afbb632008-06-06 21:10:57 +0000126 lines = 'def step%d():\n%s' % (item, lines)
127 return lines
showard9ca52702008-06-02 21:14:49 +0000128
jadmanski0afbb632008-06-06 21:10:57 +0000129
mbligh120351e2009-01-24 01:40:45 +0000130def get_tests_stanza(tests, is_server, prepend=None, append=None,
131 client_control_file=''):
132 """ Constructs the control file test step code from a list of tests.
mbligh12eafff2008-11-05 23:42:42 +0000133
mbligh120351e2009-01-24 01:40:45 +0000134 @param tests A sequence of test control files to run.
135 @param is_server bool, Is this a server side test?
136 @param prepend A list of steps to prepend to each client test.
137 Defaults to [].
138 @param append A list of steps to append to each client test.
139 Defaults to [].
140 @param client_control_file If specified, use this text as the body of a
141 final client control file to run after tests. is_server must be False.
142
143 @returns The control file test code to be run.
mbligh12eafff2008-11-05 23:42:42 +0000144 """
mbligh120351e2009-01-24 01:40:45 +0000145 assert not (client_control_file and is_server)
mbligh12eafff2008-11-05 23:42:42 +0000146 if not prepend:
147 prepend = []
148 if not append:
149 append = []
jadmanski0afbb632008-06-06 21:10:57 +0000150 raw_control_files = [read_control_file(test) for test in tests]
mbligh120351e2009-01-24 01:40:45 +0000151 return _get_tests_stanza(raw_control_files, is_server, prepend, append,
152 client_control_file=client_control_file)
mblighc5ddfd12008-08-04 17:15:00 +0000153
154
mbligh120351e2009-01-24 01:40:45 +0000155def _get_tests_stanza(raw_control_files, is_server, prepend, append,
156 client_control_file=''):
157 """
158 Implements the common parts of get_test_stanza.
159
160 A site_control_file that wants to implement its own get_tests_stanza
161 likely wants to call this in the end.
162
163 @param raw_control_files A list of raw control file data to be combined
164 into a single control file.
165 @param is_server bool, Is this a server side test?
166 @param prepend A list of steps to prepend to each client test.
167 @param append A list of steps to append to each client test.
168 @param client_control_file If specified, use this text as the body of a
169 final client control file to append to raw_control_files after fixups.
170
171 @returns The combined mega control file.
172 """
mbligh120351e2009-01-24 01:40:45 +0000173 if client_control_file:
174 # 'return locals()' is always appended incase the user forgot, it
175 # is necessary to allow for nested step engine execution to work.
176 raw_control_files.append(client_control_file + '\nreturn locals()')
jadmanski0afbb632008-06-06 21:10:57 +0000177 raw_steps = prepend + [add_boilerplate_to_nested_steps(step)
178 for step in raw_control_files] + append
179 steps = [format_step(index, step)
180 for index, step in enumerate(raw_steps)]
mblighc86113b2009-04-28 18:32:51 +0000181 if is_server:
182 step_template = SERVER_STEP_TEMPLATE
183 footer = '\n\nstep_init()\n'
184 else:
185 step_template = CLIENT_STEP_TEMPLATE
186 footer = ''
187
188 header = ''.join(step_template % i for i in xrange(len(steps)))
189 return header + '\n' + '\n\n'.join(steps) + footer
mblighe8819cd2008-02-15 16:48:40 +0000190
191
192def indent_text(text, indent):
jadmanski0afbb632008-06-06 21:10:57 +0000193 lines = [indent + line for line in text.splitlines()]
194 return '\n'.join(lines)
mblighe8819cd2008-02-15 16:48:40 +0000195
196
showard2b9a88b2008-06-13 20:55:03 +0000197def _get_profiler_commands(profilers, is_server):
showard2b9a88b2008-06-13 20:55:03 +0000198 prepend, append = [], []
showard2b9a88b2008-06-13 20:55:03 +0000199 for profiler in profilers:
200 prepend.append("job.profilers.add('%s')" % profiler.name)
201 append.append("job.profilers.delete('%s')" % profiler.name)
202 return prepend, append
203
204
mbligh12eafff2008-11-05 23:42:42 +0000205def split_kernel_list(kernel_string):
206 """Split the kernel(s) string from the user into a list of kernels.
207
208 We allow multiple kernels to be listed separated by a space or comma.
209 """
210 return re.split('[\s,]+', kernel_string.strip())
211
212
mbligh120351e2009-01-24 01:40:45 +0000213def _sanity_check_generate_control(is_server, client_control_file, kernel):
mbligh12eafff2008-11-05 23:42:42 +0000214 """
mbligh120351e2009-01-24 01:40:45 +0000215 Sanity check some of the parameters to generate_control().
216
217 This exists as its own function so that site_control_file may call it as
218 well from its own generate_control().
219
220 @raises ValidationError if any of the parameters do not make sense.
221 """
222 if is_server and client_control_file:
223 raise model_logic.ValidationError(
224 {'tests' : 'You cannot run server tests at the same time '
225 'as directly supplying a client-side control file.'})
226
227
228def generate_control(tests, kernel=None, platform=None, is_server=False,
229 profilers=(), client_control_file=''):
230 """
231 Generate a control file for a sequence of tests.
232
233 @param tests A sequence of test control files to run.
234 @param kernel A string listing one or more kernel versions to test
235 separated by spaces or commas.
236 @param platform A platform object with a kernel_config attribute.
237 @param is_server bool, Is this a server control file rather than a client?
238 @param profilers A list of profiler objects to enable during the tests.
239 @param client_control_file Contents of a client control file to run as the
240 last test after everything in tests. Requires is_server=False.
241
242 @returns The control file text as a string.
243 """
244 _sanity_check_generate_control(is_server=is_server, kernel=kernel,
245 client_control_file=client_control_file)
246
jadmanski0afbb632008-06-06 21:10:57 +0000247 control_file_text = ''
248 if kernel:
mbligh12eafff2008-11-05 23:42:42 +0000249 kernel_list = split_kernel_list(kernel)
250 control_file_text = get_kernel_stanza(kernel_list, platform,
jadmanski0afbb632008-06-06 21:10:57 +0000251 is_server=is_server)
mblighc86113b2009-04-28 18:32:51 +0000252 else:
253 control_file_text = EMPTY_TEMPLATE
showard9ca52702008-06-02 21:14:49 +0000254
showard2b9a88b2008-06-13 20:55:03 +0000255 prepend, append = _get_profiler_commands(profilers, is_server)
256
mbligh120351e2009-01-24 01:40:45 +0000257 control_file_text += get_tests_stanza(tests, is_server, prepend, append,
258 client_control_file)
jadmanski0afbb632008-06-06 21:10:57 +0000259 return control_file_text