blob: f5f0dc77029df30dc23610dbae48d608a9debd1c [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)
22
23def boot_kernel(kernel_version):
24 global kernel
25 kernel = kernel_version # Set the global in case anyone is using it.
26 testkernel = job.kernel(kernel_version)
mblighf5fdfab2008-06-16 23:57:25 +000027 %(kernel_config_line)s
28 testkernel.install()
29 testkernel.boot(args='%(kernel_args)s')
mblighe8819cd2008-02-15 16:48:40 +000030
mbligh12eafff2008-11-05 23:42:42 +000031def step_test(kernel_version):
32 job.set_test_tag_prefix(kernel_version) # Separate run output by kernel.
mblighe8819cd2008-02-15 16:48:40 +000033"""
34
showard1d445e92008-03-12 21:33:31 +000035SERVER_KERNEL_TEMPLATE = """\
showard9ca52702008-06-02 21:14:49 +000036kernel = '%%(kernel)s'
showard1d445e92008-03-12 21:33:31 +000037kernel_install_control = \"""
mblighf5fdfab2008-06-16 23:57:25 +000038%s pass
showard1d445e92008-03-12 21:33:31 +000039\"""
40
41at = autotest.Autotest()
42def install_kernel(machine):
jadmanski8d631c92008-08-18 21:12:40 +000043 host = hosts.create_host(machine)
mblighf5fdfab2008-06-16 23:57:25 +000044 at.run(kernel_install_control, host=host)
mbligh6437ff52008-04-17 15:24:38 +000045job.parallel_simple(install_kernel, machines)
showard1d445e92008-03-12 21:33:31 +000046
47""" % CLIENT_KERNEL_TEMPLATE
48
mblighf5fdfab2008-06-16 23:57:25 +000049CLIENT_STEP_TEMPLATE = " job.next_step('step%d')\n"
showard9ca52702008-06-02 21:14:49 +000050
showard1d445e92008-03-12 21:33:31 +000051
mblighe8819cd2008-02-15 16:48:40 +000052def kernel_config_line(kernel, platform):
jadmanski0afbb632008-06-06 21:10:57 +000053 if (not kernel.endswith('.rpm') and platform and
54 platform.kernel_config):
55 return "testkernel.config('%s')" % platform.kernel_config
56 return ''
mblighe8819cd2008-02-15 16:48:40 +000057
58
59def read_control_file(test):
jadmanski0afbb632008-06-06 21:10:57 +000060 control_file = open(os.path.join(AUTOTEST_DIR, test.path))
61 control_contents = control_file.read()
62 control_file.close()
63 return control_contents
mblighe8819cd2008-02-15 16:48:40 +000064
65
mbligh12eafff2008-11-05 23:42:42 +000066def get_kernel_stanza(kernel_list, platform=None, kernel_args='',
67 is_server=False):
jadmanski0afbb632008-06-06 21:10:57 +000068 if is_server:
69 template = SERVER_KERNEL_TEMPLATE
70 else:
71 template = CLIENT_KERNEL_TEMPLATE
showard1d445e92008-03-12 21:33:31 +000072
jadmanski0afbb632008-06-06 21:10:57 +000073 stanza = template % {
mbligh12eafff2008-11-05 23:42:42 +000074 'kernel_list' : kernel_list,
75 # XXX This always looks up the config line using the first kernel
76 # in the list rather than doing it for each kernel.
77 'kernel_config_line' : kernel_config_line(kernel_list[0], platform),
jadmanski0afbb632008-06-06 21:10:57 +000078 'kernel_args' : kernel_args}
79 return stanza
mblighe8819cd2008-02-15 16:48:40 +000080
81
showard9ca52702008-06-02 21:14:49 +000082def add_boilerplate_to_nested_steps(lines):
jadmanski0afbb632008-06-06 21:10:57 +000083 # Look for a line that begins with 'def step_init():' while
84 # being flexible on spacing. If it's found, this will be
85 # a nested set of steps, so add magic to make it work.
86 # See client/bin/job.py's step_engine for more info.
87 if re.search(r'^(.*\n)*def\s+step_init\s*\(\s*\)\s*:', lines):
88 lines += '\nreturn locals() '
89 lines += '# Boilerplate magic for nested sets of steps'
90 return lines
showard9ca52702008-06-02 21:14:49 +000091
92
93def format_step(item, lines):
mblighf5fdfab2008-06-16 23:57:25 +000094 lines = indent_text(lines, ' ')
jadmanski0afbb632008-06-06 21:10:57 +000095 lines = 'def step%d():\n%s' % (item, lines)
96 return lines
showard9ca52702008-06-02 21:14:49 +000097
jadmanski0afbb632008-06-06 21:10:57 +000098
mbligh12eafff2008-11-05 23:42:42 +000099def get_tests_stanza(tests, is_server, prepend=None, append=None):
100 """Constructs the control file test step code from a list of tests.
101
102 Args:
103 tests: A sequence of test control files to run.
104 is_server: Boolean - is this a server side test?
105 prepend: A list of steps to prepend to each client test. Defaults to [].
106 append: A list of steps to append to each client test. Defaults to [].
107 Returns:
108 The control file test code to be run.
109 """
110 if not prepend:
111 prepend = []
112 if not append:
113 append = []
jadmanski0afbb632008-06-06 21:10:57 +0000114 raw_control_files = [read_control_file(test) for test in tests]
mblighc5ddfd12008-08-04 17:15:00 +0000115 return _get_tests_stanza(raw_control_files, is_server, prepend, append)
116
117
118def _get_tests_stanza(raw_control_files, is_server, prepend, append):
jadmanski0afbb632008-06-06 21:10:57 +0000119 if is_server:
120 return '\n'.join(raw_control_files)
121 raw_steps = prepend + [add_boilerplate_to_nested_steps(step)
122 for step in raw_control_files] + append
123 steps = [format_step(index, step)
124 for index, step in enumerate(raw_steps)]
125 header = ''.join(CLIENT_STEP_TEMPLATE % i for i in xrange(len(steps)))
126 return header + '\n' + '\n\n'.join(steps)
mblighe8819cd2008-02-15 16:48:40 +0000127
128
129def indent_text(text, indent):
jadmanski0afbb632008-06-06 21:10:57 +0000130 lines = [indent + line for line in text.splitlines()]
131 return '\n'.join(lines)
mblighe8819cd2008-02-15 16:48:40 +0000132
133
showard2b9a88b2008-06-13 20:55:03 +0000134def _get_profiler_commands(profilers, is_server):
showard2b9a88b2008-06-13 20:55:03 +0000135 prepend, append = [], []
136 if is_server:
137 return prepend, append
138 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