1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
#!@PYTHON@
"""Convenience wrapper for running Pacemaker regression tests.
Usage: cts-regression [-h] [-V] [-v] [COMPONENT ...]
"""
__copyright__ = 'Copyright 2012-2023 the Pacemaker project contributors'
__license__ = 'GNU General Public License version 2 or later (GPLv2+) WITHOUT ANY WARRANTY'
import argparse
import os
import subprocess
import sys
import textwrap
# These imports allow running from a source checkout after running `make`.
# Note that while this doesn't necessarily mean it will successfully run tests,
# but being able to see --help output can be useful.
if os.path.exists("@abs_top_srcdir@/python"):
sys.path.insert(0, "@abs_top_srcdir@/python")
if os.path.exists("@abs_top_builddir@/python") and "@abs_top_builddir@" != "@abs_top_srcdir@":
sys.path.insert(0, "@abs_top_builddir@/python")
from pacemaker.buildoptions import BuildOptions
from pacemaker.exitstatus import ExitStatus
class Component():
"""A class for running regression tests on a component.
"Component" refers to a Pacemaker component, such as the scheduler.
:attribute name: The name of the component.
:type name: str
:attribute description: The description of the component.
:type description: str
:attribute requires_root: Whether the component's tests must be run
as root.
:type requires_root: bool
:attribute supports_valgrind: Whether the component's tests support
running under valgrind.
:type supports_valgrind: bool
:attribute cmd: The command to run the component's tests, along with
any required options.
:type cmd: list[str]
:method run([verbose=False], [valgrind=False]): Run the component's
regression tests and return the result.
"""
def __init__(self, name, description, test_home, requires_root=False,
supports_valgrind=False):
"""Constructor for the :class:`Component` class.
:param name: The name of the component.
:type name: str
:param description: The description of the component.
:type description: str
:param test_home: The directory where the component's tests
reside.
:type test_home: str
:param requires_root: Whether the component's tests must be run
as root.
:type requires_root: bool
:param supports_valgrind: Whether the component's tests support
running under valgrind.
:type supports_valgrind: bool
"""
self.name = name
self.description = description
self.requires_root = requires_root
self.supports_valgrind = supports_valgrind
if self.name == 'pacemaker_remote':
self.cmd = [os.path.join(test_home, 'cts-exec'), '-R']
else:
self.cmd = [os.path.join(test_home, 'cts-%s' % self.name)]
def run(self, verbose=False, valgrind=False):
"""Run the component's regression tests and return the result.
:param verbose: Whether to increase test output verbosity.
:type verbose: bool
:param valgrind: Whether to run the test under valgrind.
:type valgrind: bool
:return: The exit code from the component's test suite.
:rtype: :class:`ExitStatus`
"""
print('Executing the %s regression tests' % self.name)
print('=' * 60)
cmd = self.cmd
if self.requires_root and os.geteuid() != 0:
print('Enter the sudo password if prompted')
cmd = ['sudo'] + self.cmd
if verbose:
cmd.append('--verbose')
if self.supports_valgrind and valgrind:
cmd.append('--valgrind')
try:
rc = ExitStatus(subprocess.call(cmd))
except OSError as err:
error_print('Failed to execute %s tests: %s' % (self.name, err))
rc = ExitStatus.NOT_INSTALLED
print('=' * 60 + '\n\n')
return rc
class ComponentsArgAction(argparse.Action):
"""A class to handle `components` arguments.
This class handles special cases and cleans up the `components`
list. Specifically, it does the following:
* Enforce a default value of ['cli', 'scheduler'].
* Replace the 'all' alias with the components that it represents.
* Get rid of duplicates.
The main motivation is that when the `choices` argument of
:meth:`parser.add_argument()` is specified, the `default` argument
must contain exactly one value (not `None` and not a list). We want
our default to be a list of components, namely `cli` and
`scheduler`.
"""
def __call__(self, parser, namespace, values, option_string=None):
all_components = ['attrd', 'cli', 'exec', 'fencing', 'scheduler']
default_components = ['cli', 'scheduler']
if not values:
setattr(namespace, self.dest, default_components)
return
# If no argument is specified, the default gets passed as a
# string 'default' instead of as a list ['default']. Probably
# a bug in argparse. The below gives us a list.
if not isinstance(values, list):
values = [values]
components = set(values)
# If 'all', is found, replace it with the components it represents.
try:
components.remove('all')
components.update(set(all_components))
except KeyError:
pass
# Same for 'default'
try:
components.remove('default')
components.update(set(default_components))
except KeyError:
pass
setattr(namespace, self.dest, sorted(list(components)))
def error_print(msg):
"""Print an error message.
:param msg: Message to print.
:type msg: str
"""
print(' * ERROR: %s' % msg)
def run_components(components, verbose=False, valgrind=False):
"""Run components' regression tests and report results for each.
:param components: A list of names of components for which to run
tests.
:type components: list[:class:`Component`]
:return: :attr:`ExitStatus.OK` if all tests were successful,
:attr:`ExitStatus.ERROR` otherwise.
:rtype: :class:`ExitStatus`
"""
failed = []
for comp in components:
rc = comp.run(verbose, valgrind)
if rc != ExitStatus.OK:
error_print('%s regression tests failed (%s)' % (comp.name, rc))
failed.append(comp.name)
if failed:
print('Failed regression tests:', end='')
for comp in failed:
print(' %s' % comp, end='')
print()
return ExitStatus.ERROR
return ExitStatus.OK
def main():
"""Run Pacemaker regression tests as specified by arguments."""
try:
test_home = os.path.dirname(os.readlink(sys.argv[0]))
except OSError:
test_home = os.path.dirname(sys.argv[0])
# Available components
components = {
'attrd': Component(
'attrd',
'Attribute manager',
test_home,
requires_root=True,
supports_valgrind=False,
),
'cli': Component(
'cli',
'Command-line tools',
test_home,
requires_root=False,
supports_valgrind=True,
),
'exec': Component(
'exec',
'Local resource agent executor',
test_home,
requires_root=True,
supports_valgrind=False,
),
'fencing': Component(
'fencing',
'Fencer',
test_home,
requires_root=True,
supports_valgrind=False,
),
'scheduler': Component(
'scheduler',
'Action scheduler',
test_home,
requires_root=False,
supports_valgrind=True,
),
}
if BuildOptions.REMOTE_ENABLED:
components['pacemaker_remote'] = Component(
'pacemaker_remote',
'Resource agent executor in remote mode',
test_home,
requires_root=True,
supports_valgrind=False,
)
# Build up program description
description = textwrap.dedent('''\
Run Pacemaker regression tests.
Available components (default components are 'cli scheduler'):
''')
for name, comp in sorted(components.items()):
description += '\n {:<20} {}'.format(name, comp.description)
description += (
'\n {:<20} Synonym for "cli exec fencing scheduler"'.format('all')
)
# Parse the arguments
parser = argparse.ArgumentParser(
description=description,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
choices = sorted(components.keys()) + ['all', 'default']
parser.add_argument('-V', '--verbose', action='store_true',
help='Increase test verbosity')
parser.add_argument('-v', '--valgrind', action='store_true',
help='Run test commands under valgrind')
parser.add_argument('components', nargs='*', choices=choices,
default='default',
action=ComponentsArgAction, metavar='COMPONENT',
help="One of the components to test, or 'all'")
args = parser.parse_args()
# Run the tests
selected = [components[x] for x in args.components]
rc = run_components(selected, args.verbose, args.valgrind)
sys.exit(rc)
if __name__ == '__main__':
main()
|