blob: 35ae4f54dc73fff6c4b9fbee44eb25f654dc72af [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
mblighe8819cd2008-02-15 16:48:40 +00008import frontend.settings
9
10AUTOTEST_DIR = os.path.abspath(os.path.join(
11 os.path.dirname(frontend.settings.__file__), '..'))
12
mbligh12eafff2008-11-05 23:42:42 +000013CLIENT_EMPTY_TEMPLATE = 'def step_init():\n'
mblighe8819cd2008-02-15 16:48:40 +000014
showard1d445e92008-03-12 21:33:31 +000015CLIENT_KERNEL_TEMPLATE = """\
mbligh12eafff2008-11-05 23:42:42 +000016kernel_list = %(kernel_list)r
17
mblighe8819cd2008-02-15 16:48:40 +000018def step_init():
mbligh12eafff2008-11-05 23:42:42 +000019 for kernel_version in kernel_list:
20 job.next_step(boot_kernel, kernel_version)
21 job.next_step(step_test, kernel_version)
mblighe39c3c02008-11-18 15:00:06 +000022 if len(kernel_list) > 1:
23 job.set_run_number(1) # Include run numbers in output directory names.
24 job.show_kernel_in_test_tag(True) # Include kernel in output dir name.
mbligh12eafff2008-11-05 23:42:42 +000025
26def boot_kernel(kernel_version):
mbligh12eafff2008-11-05 23:42:42 +000027 testkernel = job.kernel(kernel_version)
mblighf5fdfab2008-06-16 23:57:25 +000028 %(kernel_config_line)s
29 testkernel.install()
30 testkernel.boot(args='%(kernel_args)s')
mblighe8819cd2008-02-15 16:48:40 +000031
mbligh12eafff2008-11-05 23:42:42 +000032def step_test(kernel_version):
mbligh6d2a6f92008-11-13 16:47:52 +000033 global kernel
34 kernel = kernel_version # Set the global in case anyone is using it.
mblighe8819cd2008-02-15 16:48:40 +000035"""
36
showard1d445e92008-03-12 21:33:31 +000037SERVER_KERNEL_TEMPLATE = """\
mbligh15fe9952008-11-15 01:08:41 +000038kernel_list = %%(kernel_list)r
showard1d445e92008-03-12 21:33:31 +000039kernel_install_control = \"""
mblighf5fdfab2008-06-16 23:57:25 +000040%s pass
showard1d445e92008-03-12 21:33:31 +000041\"""
42
43at = autotest.Autotest()
44def install_kernel(machine):
jadmanski8d631c92008-08-18 21:12:40 +000045 host = hosts.create_host(machine)
mblighf5fdfab2008-06-16 23:57:25 +000046 at.run(kernel_install_control, host=host)
mbligh6437ff52008-04-17 15:24:38 +000047job.parallel_simple(install_kernel, machines)
showard1d445e92008-03-12 21:33:31 +000048
49""" % CLIENT_KERNEL_TEMPLATE
50
mblighf5fdfab2008-06-16 23:57:25 +000051CLIENT_STEP_TEMPLATE = " job.next_step('step%d')\n"
showard9ca52702008-06-02 21:14:49 +000052
showard1d445e92008-03-12 21:33:31 +000053
mblighe8819cd2008-02-15 16:48:40 +000054def kernel_config_line(kernel, platform):
jadmanski0afbb632008-06-06 21:10:57 +000055 if (not kernel.endswith('.rpm') and platform and
56 platform.kernel_config):
57 return "testkernel.config('%s')" % platform.kernel_config
58 return ''
mblighe8819cd2008-02-15 16:48:40 +000059
60
61def read_control_file(test):
jadmanski0afbb632008-06-06 21:10:57 +000062 control_file = open(os.path.join(AUTOTEST_DIR, test.path))
63 control_contents = control_file.read()
64 control_file.close()
65 return control_contents
mblighe8819cd2008-02-15 16:48:40 +000066
67
mbligh12eafff2008-11-05 23:42:42 +000068def get_kernel_stanza(kernel_list, platform=None, kernel_args='',
69 is_server=False):
jadmanski0afbb632008-06-06 21:10:57 +000070 if is_server:
71 template = SERVER_KERNEL_TEMPLATE
72 else:
73 template = CLIENT_KERNEL_TEMPLATE
showard1d445e92008-03-12 21:33:31 +000074
jadmanski0afbb632008-06-06 21:10:57 +000075 stanza = template % {
mbligh12eafff2008-11-05 23:42:42 +000076 'kernel_list' : kernel_list,
77 # XXX This always looks up the config line using the first kernel
78 # in the list rather than doing it for each kernel.
79 'kernel_config_line' : kernel_config_line(kernel_list[0], platform),
jadmanski0afbb632008-06-06 21:10:57 +000080 'kernel_args' : kernel_args}
81 return stanza
mblighe8819cd2008-02-15 16:48:40 +000082
83
showard9ca52702008-06-02 21:14:49 +000084def add_boilerplate_to_nested_steps(lines):
jadmanski0afbb632008-06-06 21:10:57 +000085 # Look for a line that begins with 'def step_init():' while
86 # being flexible on spacing. If it's found, this will be
87 # a nested set of steps, so add magic to make it work.
88 # See client/bin/job.py's step_engine for more info.
89 if re.search(r'^(.*\n)*def\s+step_init\s*\(\s*\)\s*:', lines):
90 lines += '\nreturn locals() '
91 lines += '# Boilerplate magic for nested sets of steps'
92 return lines
showard9ca52702008-06-02 21:14:49 +000093
94
95def format_step(item, lines):
mblighf5fdfab2008-06-16 23:57:25 +000096 lines = indent_text(lines, ' ')
jadmanski0afbb632008-06-06 21:10:57 +000097 lines = 'def step%d():\n%s' % (item, lines)
98 return lines
showard9ca52702008-06-02 21:14:49 +000099
jadmanski0afbb632008-06-06 21:10:57 +0000100
mbligh12eafff2008-11-05 23:42:42 +0000101def get_tests_stanza(tests, is_server, prepend=None, append=None):
102 """Constructs the control file test step code from a list of tests.
103
104 Args:
105 tests: A sequence of test control files to run.
106 is_server: Boolean - is this a server side test?
107 prepend: A list of steps to prepend to each client test. Defaults to [].
108 append: A list of steps to append to each client test. Defaults to [].
109 Returns:
110 The control file test code to be run.
111 """
112 if not prepend:
113 prepend = []
114 if not append:
115 append = []
jadmanski0afbb632008-06-06 21:10:57 +0000116 raw_control_files = [read_control_file(test) for test in tests]
mblighc5ddfd12008-08-04 17:15:00 +0000117 return _get_tests_stanza(raw_control_files, is_server, prepend, append)
118
119
120def _get_tests_stanza(raw_control_files, is_server, prepend, append):
jadmanski0afbb632008-06-06 21:10:57 +0000121 if is_server:
jadmanski8ae7d3a2008-12-03 16:34:10 +0000122 return '\n'.join(prepend + raw_control_files + append)
jadmanski0afbb632008-06-06 21:10:57 +0000123 raw_steps = prepend + [add_boilerplate_to_nested_steps(step)
124 for step in raw_control_files] + append
125 steps = [format_step(index, step)
126 for index, step in enumerate(raw_steps)]
127 header = ''.join(CLIENT_STEP_TEMPLATE % i for i in xrange(len(steps)))
128 return header + '\n' + '\n\n'.join(steps)
mblighe8819cd2008-02-15 16:48:40 +0000129
130
131def indent_text(text, indent):
jadmanski0afbb632008-06-06 21:10:57 +0000132 lines = [indent + line for line in text.splitlines()]
133 return '\n'.join(lines)
mblighe8819cd2008-02-15 16:48:40 +0000134
135
showard2b9a88b2008-06-13 20:55:03 +0000136def _get_profiler_commands(profilers, is_server):
showard2b9a88b2008-06-13 20:55:03 +0000137 prepend, append = [], []
showard2b9a88b2008-06-13 20:55:03 +0000138 for profiler in profilers:
139 prepend.append("job.profilers.add('%s')" % profiler.name)
140 append.append("job.profilers.delete('%s')" % profiler.name)
141 return prepend, append
142
143
mbligh12eafff2008-11-05 23:42:42 +0000144def split_kernel_list(kernel_string):
145 """Split the kernel(s) string from the user into a list of kernels.
146
147 We allow multiple kernels to be listed separated by a space or comma.
148 """
149 return re.split('[\s,]+', kernel_string.strip())
150
151
showard2b9a88b2008-06-13 20:55:03 +0000152def generate_control(tests, kernel=None, platform=None, is_server=False,
mbligh12eafff2008-11-05 23:42:42 +0000153 profilers=()):
154 """Generate a control file for a sequence of tests.
155
156 Args:
157 tests: A sequence of test control files to run.
158 kernel: A string listing one or more kernel versions to test separated
159 by spaces or commas.
160 platform: A platform object with a kernel_config attribute.
161 is_server: Boolean - is a server control file rather than a client?
162 profilers: A list of profiler objects to enable during the tests.
163
164 Returns:
165 The control file text as a string.
166 """
jadmanski0afbb632008-06-06 21:10:57 +0000167 control_file_text = ''
168 if kernel:
mbligh12eafff2008-11-05 23:42:42 +0000169 kernel_list = split_kernel_list(kernel)
170 control_file_text = get_kernel_stanza(kernel_list, platform,
jadmanski0afbb632008-06-06 21:10:57 +0000171 is_server=is_server)
172 elif not is_server:
mbligh12eafff2008-11-05 23:42:42 +0000173 control_file_text = CLIENT_EMPTY_TEMPLATE
showard9ca52702008-06-02 21:14:49 +0000174
showard2b9a88b2008-06-13 20:55:03 +0000175 prepend, append = _get_profiler_commands(profilers, is_server)
176
177 control_file_text += get_tests_stanza(tests, is_server, prepend, append)
jadmanski0afbb632008-06-06 21:10:57 +0000178 return control_file_text