blob: 1cd5677194d531e8b660c847cdb4e30321447747 [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
mbligh12eafff2008-11-05 23:42:42 +000016CLIENT_EMPTY_TEMPLATE = 'def step_init():\n'
mblighe8819cd2008-02-15 16:48:40 +000017
showard1d445e92008-03-12 21:33:31 +000018CLIENT_KERNEL_TEMPLATE = """\
mbligh12eafff2008-11-05 23:42:42 +000019kernel_list = %(kernel_list)r
20
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 = """\
mbligh15fe9952008-11-15 01:08:41 +000041kernel_list = %%(kernel_list)r
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()
47def install_kernel(machine):
jadmanski8d631c92008-08-18 21:12:40 +000048 host = hosts.create_host(machine)
mblighf5fdfab2008-06-16 23:57:25 +000049 at.run(kernel_install_control, host=host)
mbligh6437ff52008-04-17 15:24:38 +000050job.parallel_simple(install_kernel, machines)
showard1d445e92008-03-12 21:33:31 +000051
52""" % CLIENT_KERNEL_TEMPLATE
53
mblighf5fdfab2008-06-16 23:57:25 +000054CLIENT_STEP_TEMPLATE = " job.next_step('step%d')\n"
showard9ca52702008-06-02 21:14:49 +000055
showard1d445e92008-03-12 21:33:31 +000056
mblighe8819cd2008-02-15 16:48:40 +000057def kernel_config_line(kernel, platform):
jadmanski0afbb632008-06-06 21:10:57 +000058 if (not kernel.endswith('.rpm') and platform and
59 platform.kernel_config):
60 return "testkernel.config('%s')" % platform.kernel_config
61 return ''
mblighe8819cd2008-02-15 16:48:40 +000062
63
64def read_control_file(test):
jadmanski0afbb632008-06-06 21:10:57 +000065 control_file = open(os.path.join(AUTOTEST_DIR, test.path))
66 control_contents = control_file.read()
67 control_file.close()
68 return control_contents
mblighe8819cd2008-02-15 16:48:40 +000069
70
mbligh12eafff2008-11-05 23:42:42 +000071def get_kernel_stanza(kernel_list, platform=None, kernel_args='',
72 is_server=False):
jadmanski0afbb632008-06-06 21:10:57 +000073 if is_server:
74 template = SERVER_KERNEL_TEMPLATE
75 else:
76 template = CLIENT_KERNEL_TEMPLATE
showard1d445e92008-03-12 21:33:31 +000077
jadmanski0afbb632008-06-06 21:10:57 +000078 stanza = template % {
mbligh12eafff2008-11-05 23:42:42 +000079 'kernel_list' : kernel_list,
80 # XXX This always looks up the config line using the first kernel
81 # in the list rather than doing it for each kernel.
82 'kernel_config_line' : kernel_config_line(kernel_list[0], platform),
jadmanski0afbb632008-06-06 21:10:57 +000083 'kernel_args' : kernel_args}
84 return stanza
mblighe8819cd2008-02-15 16:48:40 +000085
86
showard9ca52702008-06-02 21:14:49 +000087def add_boilerplate_to_nested_steps(lines):
jadmanski0afbb632008-06-06 21:10:57 +000088 # Look for a line that begins with 'def step_init():' while
89 # being flexible on spacing. If it's found, this will be
90 # a nested set of steps, so add magic to make it work.
91 # See client/bin/job.py's step_engine for more info.
92 if re.search(r'^(.*\n)*def\s+step_init\s*\(\s*\)\s*:', lines):
93 lines += '\nreturn locals() '
94 lines += '# Boilerplate magic for nested sets of steps'
95 return lines
showard9ca52702008-06-02 21:14:49 +000096
97
98def format_step(item, lines):
mblighf5fdfab2008-06-16 23:57:25 +000099 lines = indent_text(lines, ' ')
jadmanski0afbb632008-06-06 21:10:57 +0000100 lines = 'def step%d():\n%s' % (item, lines)
101 return lines
showard9ca52702008-06-02 21:14:49 +0000102
jadmanski0afbb632008-06-06 21:10:57 +0000103
mbligh120351e2009-01-24 01:40:45 +0000104def get_tests_stanza(tests, is_server, prepend=None, append=None,
105 client_control_file=''):
106 """ Constructs the control file test step code from a list of tests.
mbligh12eafff2008-11-05 23:42:42 +0000107
mbligh120351e2009-01-24 01:40:45 +0000108 @param tests A sequence of test control files to run.
109 @param is_server bool, Is this a server side test?
110 @param prepend A list of steps to prepend to each client test.
111 Defaults to [].
112 @param append A list of steps to append to each client test.
113 Defaults to [].
114 @param client_control_file If specified, use this text as the body of a
115 final client control file to run after tests. is_server must be False.
116
117 @returns The control file test code to be run.
mbligh12eafff2008-11-05 23:42:42 +0000118 """
mbligh120351e2009-01-24 01:40:45 +0000119 assert not (client_control_file and is_server)
mbligh12eafff2008-11-05 23:42:42 +0000120 if not prepend:
121 prepend = []
122 if not append:
123 append = []
jadmanski0afbb632008-06-06 21:10:57 +0000124 raw_control_files = [read_control_file(test) for test in tests]
mbligh120351e2009-01-24 01:40:45 +0000125 return _get_tests_stanza(raw_control_files, is_server, prepend, append,
126 client_control_file=client_control_file)
mblighc5ddfd12008-08-04 17:15:00 +0000127
128
mbligh120351e2009-01-24 01:40:45 +0000129def _get_tests_stanza(raw_control_files, is_server, prepend, append,
130 client_control_file=''):
131 """
132 Implements the common parts of get_test_stanza.
133
134 A site_control_file that wants to implement its own get_tests_stanza
135 likely wants to call this in the end.
136
137 @param raw_control_files A list of raw control file data to be combined
138 into a single control file.
139 @param is_server bool, Is this a server side test?
140 @param prepend A list of steps to prepend to each client test.
141 @param append A list of steps to append to each client test.
142 @param client_control_file If specified, use this text as the body of a
143 final client control file to append to raw_control_files after fixups.
144
145 @returns The combined mega control file.
146 """
jadmanski0afbb632008-06-06 21:10:57 +0000147 if is_server:
jadmanski8ae7d3a2008-12-03 16:34:10 +0000148 return '\n'.join(prepend + raw_control_files + append)
mbligh120351e2009-01-24 01:40:45 +0000149 if client_control_file:
150 # 'return locals()' is always appended incase the user forgot, it
151 # is necessary to allow for nested step engine execution to work.
152 raw_control_files.append(client_control_file + '\nreturn locals()')
jadmanski0afbb632008-06-06 21:10:57 +0000153 raw_steps = prepend + [add_boilerplate_to_nested_steps(step)
154 for step in raw_control_files] + append
155 steps = [format_step(index, step)
156 for index, step in enumerate(raw_steps)]
157 header = ''.join(CLIENT_STEP_TEMPLATE % i for i in xrange(len(steps)))
158 return header + '\n' + '\n\n'.join(steps)
mblighe8819cd2008-02-15 16:48:40 +0000159
160
161def indent_text(text, indent):
jadmanski0afbb632008-06-06 21:10:57 +0000162 lines = [indent + line for line in text.splitlines()]
163 return '\n'.join(lines)
mblighe8819cd2008-02-15 16:48:40 +0000164
165
showard2b9a88b2008-06-13 20:55:03 +0000166def _get_profiler_commands(profilers, is_server):
showard2b9a88b2008-06-13 20:55:03 +0000167 prepend, append = [], []
showard2b9a88b2008-06-13 20:55:03 +0000168 for profiler in profilers:
169 prepend.append("job.profilers.add('%s')" % profiler.name)
170 append.append("job.profilers.delete('%s')" % profiler.name)
171 return prepend, append
172
173
mbligh12eafff2008-11-05 23:42:42 +0000174def split_kernel_list(kernel_string):
175 """Split the kernel(s) string from the user into a list of kernels.
176
177 We allow multiple kernels to be listed separated by a space or comma.
178 """
179 return re.split('[\s,]+', kernel_string.strip())
180
181
mbligh120351e2009-01-24 01:40:45 +0000182def _sanity_check_generate_control(is_server, client_control_file, kernel):
mbligh12eafff2008-11-05 23:42:42 +0000183 """
mbligh120351e2009-01-24 01:40:45 +0000184 Sanity check some of the parameters to generate_control().
185
186 This exists as its own function so that site_control_file may call it as
187 well from its own generate_control().
188
189 @raises ValidationError if any of the parameters do not make sense.
190 """
191 if is_server and client_control_file:
192 raise model_logic.ValidationError(
193 {'tests' : 'You cannot run server tests at the same time '
194 'as directly supplying a client-side control file.'})
195
196
197def generate_control(tests, kernel=None, platform=None, is_server=False,
198 profilers=(), client_control_file=''):
199 """
200 Generate a control file for a sequence of tests.
201
202 @param tests A sequence of test control files to run.
203 @param kernel A string listing one or more kernel versions to test
204 separated by spaces or commas.
205 @param platform A platform object with a kernel_config attribute.
206 @param is_server bool, Is this a server control file rather than a client?
207 @param profilers A list of profiler objects to enable during the tests.
208 @param client_control_file Contents of a client control file to run as the
209 last test after everything in tests. Requires is_server=False.
210
211 @returns The control file text as a string.
212 """
213 _sanity_check_generate_control(is_server=is_server, kernel=kernel,
214 client_control_file=client_control_file)
215
jadmanski0afbb632008-06-06 21:10:57 +0000216 control_file_text = ''
217 if kernel:
mbligh12eafff2008-11-05 23:42:42 +0000218 kernel_list = split_kernel_list(kernel)
219 control_file_text = get_kernel_stanza(kernel_list, platform,
jadmanski0afbb632008-06-06 21:10:57 +0000220 is_server=is_server)
221 elif not is_server:
mbligh12eafff2008-11-05 23:42:42 +0000222 control_file_text = CLIENT_EMPTY_TEMPLATE
showard9ca52702008-06-02 21:14:49 +0000223
showard2b9a88b2008-06-13 20:55:03 +0000224 prepend, append = _get_profiler_commands(profilers, is_server)
225
mbligh120351e2009-01-24 01:40:45 +0000226 control_file_text += get_tests_stanza(tests, is_server, prepend, append,
227 client_control_file)
jadmanski0afbb632008-06-06 21:10:57 +0000228 return control_file_text