blob: 0362ef3e22bbef65f8754301603074bdd814c515 [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):
mbligh12eafff2008-11-05 23:42:42 +000024 testkernel = job.kernel(kernel_version)
mblighf5fdfab2008-06-16 23:57:25 +000025 %(kernel_config_line)s
26 testkernel.install()
27 testkernel.boot(args='%(kernel_args)s')
mblighe8819cd2008-02-15 16:48:40 +000028
mbligh12eafff2008-11-05 23:42:42 +000029def step_test(kernel_version):
mbligh6d2a6f92008-11-13 16:47:52 +000030 global kernel
31 kernel = kernel_version # Set the global in case anyone is using it.
mbligh1429e7f2008-11-14 20:53:43 +000032 if len(kernel_list) > 1:
33 job.set_test_tag_prefix(kernel_version) # Separate output by kernel.
mblighe8819cd2008-02-15 16:48:40 +000034"""
35
showard1d445e92008-03-12 21:33:31 +000036SERVER_KERNEL_TEMPLATE = """\
mbligh15fe9952008-11-15 01:08:41 +000037kernel_list = %%(kernel_list)r
showard1d445e92008-03-12 21:33:31 +000038kernel_install_control = \"""
mblighf5fdfab2008-06-16 23:57:25 +000039%s pass
showard1d445e92008-03-12 21:33:31 +000040\"""
41
42at = autotest.Autotest()
43def install_kernel(machine):
jadmanski8d631c92008-08-18 21:12:40 +000044 host = hosts.create_host(machine)
mblighf5fdfab2008-06-16 23:57:25 +000045 at.run(kernel_install_control, host=host)
mbligh6437ff52008-04-17 15:24:38 +000046job.parallel_simple(install_kernel, machines)
showard1d445e92008-03-12 21:33:31 +000047
48""" % CLIENT_KERNEL_TEMPLATE
49
mblighf5fdfab2008-06-16 23:57:25 +000050CLIENT_STEP_TEMPLATE = " job.next_step('step%d')\n"
showard9ca52702008-06-02 21:14:49 +000051
showard1d445e92008-03-12 21:33:31 +000052
mblighe8819cd2008-02-15 16:48:40 +000053def kernel_config_line(kernel, platform):
jadmanski0afbb632008-06-06 21:10:57 +000054 if (not kernel.endswith('.rpm') and platform and
55 platform.kernel_config):
56 return "testkernel.config('%s')" % platform.kernel_config
57 return ''
mblighe8819cd2008-02-15 16:48:40 +000058
59
60def read_control_file(test):
jadmanski0afbb632008-06-06 21:10:57 +000061 control_file = open(os.path.join(AUTOTEST_DIR, test.path))
62 control_contents = control_file.read()
63 control_file.close()
64 return control_contents
mblighe8819cd2008-02-15 16:48:40 +000065
66
mbligh12eafff2008-11-05 23:42:42 +000067def get_kernel_stanza(kernel_list, platform=None, kernel_args='',
68 is_server=False):
jadmanski0afbb632008-06-06 21:10:57 +000069 if is_server:
70 template = SERVER_KERNEL_TEMPLATE
71 else:
72 template = CLIENT_KERNEL_TEMPLATE
showard1d445e92008-03-12 21:33:31 +000073
jadmanski0afbb632008-06-06 21:10:57 +000074 stanza = template % {
mbligh12eafff2008-11-05 23:42:42 +000075 'kernel_list' : kernel_list,
76 # XXX This always looks up the config line using the first kernel
77 # in the list rather than doing it for each kernel.
78 'kernel_config_line' : kernel_config_line(kernel_list[0], platform),
jadmanski0afbb632008-06-06 21:10:57 +000079 'kernel_args' : kernel_args}
80 return stanza
mblighe8819cd2008-02-15 16:48:40 +000081
82
showard9ca52702008-06-02 21:14:49 +000083def add_boilerplate_to_nested_steps(lines):
jadmanski0afbb632008-06-06 21:10:57 +000084 # Look for a line that begins with 'def step_init():' while
85 # being flexible on spacing. If it's found, this will be
86 # a nested set of steps, so add magic to make it work.
87 # See client/bin/job.py's step_engine for more info.
88 if re.search(r'^(.*\n)*def\s+step_init\s*\(\s*\)\s*:', lines):
89 lines += '\nreturn locals() '
90 lines += '# Boilerplate magic for nested sets of steps'
91 return lines
showard9ca52702008-06-02 21:14:49 +000092
93
94def format_step(item, lines):
mblighf5fdfab2008-06-16 23:57:25 +000095 lines = indent_text(lines, ' ')
jadmanski0afbb632008-06-06 21:10:57 +000096 lines = 'def step%d():\n%s' % (item, lines)
97 return lines
showard9ca52702008-06-02 21:14:49 +000098
jadmanski0afbb632008-06-06 21:10:57 +000099
mbligh12eafff2008-11-05 23:42:42 +0000100def get_tests_stanza(tests, is_server, prepend=None, append=None):
101 """Constructs the control file test step code from a list of tests.
102
103 Args:
104 tests: A sequence of test control files to run.
105 is_server: Boolean - is this a server side test?
106 prepend: A list of steps to prepend to each client test. Defaults to [].
107 append: A list of steps to append to each client test. Defaults to [].
108 Returns:
109 The control file test code to be run.
110 """
111 if not prepend:
112 prepend = []
113 if not append:
114 append = []
jadmanski0afbb632008-06-06 21:10:57 +0000115 raw_control_files = [read_control_file(test) for test in tests]
mblighc5ddfd12008-08-04 17:15:00 +0000116 return _get_tests_stanza(raw_control_files, is_server, prepend, append)
117
118
119def _get_tests_stanza(raw_control_files, is_server, prepend, append):
jadmanski0afbb632008-06-06 21:10:57 +0000120 if is_server:
121 return '\n'.join(raw_control_files)
122 raw_steps = prepend + [add_boilerplate_to_nested_steps(step)
123 for step in raw_control_files] + append
124 steps = [format_step(index, step)
125 for index, step in enumerate(raw_steps)]
126 header = ''.join(CLIENT_STEP_TEMPLATE % i for i in xrange(len(steps)))
127 return header + '\n' + '\n\n'.join(steps)
mblighe8819cd2008-02-15 16:48:40 +0000128
129
130def indent_text(text, indent):
jadmanski0afbb632008-06-06 21:10:57 +0000131 lines = [indent + line for line in text.splitlines()]
132 return '\n'.join(lines)
mblighe8819cd2008-02-15 16:48:40 +0000133
134
showard2b9a88b2008-06-13 20:55:03 +0000135def _get_profiler_commands(profilers, is_server):
showard2b9a88b2008-06-13 20:55:03 +0000136 prepend, append = [], []
137 if is_server:
138 return prepend, append
139 for profiler in profilers:
140 prepend.append("job.profilers.add('%s')" % profiler.name)
141 append.append("job.profilers.delete('%s')" % profiler.name)
142 return prepend, append
143
144
mbligh12eafff2008-11-05 23:42:42 +0000145def split_kernel_list(kernel_string):
146 """Split the kernel(s) string from the user into a list of kernels.
147
148 We allow multiple kernels to be listed separated by a space or comma.
149 """
150 return re.split('[\s,]+', kernel_string.strip())
151
152
showard2b9a88b2008-06-13 20:55:03 +0000153def generate_control(tests, kernel=None, platform=None, is_server=False,
mbligh12eafff2008-11-05 23:42:42 +0000154 profilers=()):
155 """Generate a control file for a sequence of tests.
156
157 Args:
158 tests: A sequence of test control files to run.
159 kernel: A string listing one or more kernel versions to test separated
160 by spaces or commas.
161 platform: A platform object with a kernel_config attribute.
162 is_server: Boolean - is a server control file rather than a client?
163 profilers: A list of profiler objects to enable during the tests.
164
165 Returns:
166 The control file text as a string.
167 """
jadmanski0afbb632008-06-06 21:10:57 +0000168 control_file_text = ''
169 if kernel:
mbligh12eafff2008-11-05 23:42:42 +0000170 kernel_list = split_kernel_list(kernel)
171 control_file_text = get_kernel_stanza(kernel_list, platform,
jadmanski0afbb632008-06-06 21:10:57 +0000172 is_server=is_server)
173 elif not is_server:
mbligh12eafff2008-11-05 23:42:42 +0000174 control_file_text = CLIENT_EMPTY_TEMPLATE
showard9ca52702008-06-02 21:14:49 +0000175
showard2b9a88b2008-06-13 20:55:03 +0000176 prepend, append = _get_profiler_commands(profilers, is_server)
177
178 control_file_text += get_tests_stanza(tests, is_server, prepend, append)
jadmanski0afbb632008-06-06 21:10:57 +0000179 return control_file_text