1 # Unix SMB/CIFS implementation.
2 # Copyright © Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 from samba
.tests
import TestCase
, check_help_consistency
23 if 'SRCDIR_ABS' in os
.environ
:
24 BASEDIR
= os
.environ
['SRCDIR_ABS']
26 BASEDIR
= os
.path
.abspath(os
.path
.join(os
.path
.dirname(__file__
),
65 'script/autobuild.py', # defaults to mount /memdisk/
66 'script/bisect-test.py',
67 'ctdb/utils/etcd/ctdb_etcd_lock',
68 'selftest/filter-subunit',
69 'selftest/format-subunit',
70 'bin/gen_output.py', # too much output!
71 'source4/scripting/bin/gen_output.py',
72 'lib/ldb/tests/python/index.py',
73 'lib/ldb/tests/python/api.py',
74 'source4/selftest/tests.py',
76 'selftest/tap2subunit',
77 'script/show_test_time',
78 'source4/scripting/bin/subunitrun',
79 'bin/samba_downgrade_db',
80 'source4/scripting/bin/samba_downgrade_db',
81 'source3/selftest/tests.py',
83 'python/samba/subunit/run.py',
84 'bin/python/samba/subunit/run.py',
85 'lib/compression/tests/scripts/three-byte-hash',
89 'selftest/tap2subunit',
91 'wintest/test-s4-howto.py',
96 'source3/script/tests',
98 'source4/dsdb/tests/python',
100 'bin/python/samba/tests',
101 'bin/python/samba/tests/dcerpc',
102 'bin/python/samba/tests/krb5',
103 'python/samba/tests',
104 'python/samba/tests/bin',
105 'python/samba/tests/dcerpc',
106 'python/samba/tests/krb5',
110 def _init_git_file_finder():
111 """Generate a function that quickly answers the question:
112 'is this a git file?'
114 git_file_cache
= set()
115 p
= subprocess
.run(['git',
119 stdout
=subprocess
.PIPE
)
120 if p
.returncode
== 0:
121 for fn
in p
.stdout
.split(b
'\0'):
122 git_file_cache
.add(os
.path
.join(BASEDIR
, fn
.decode('utf-8')))
123 return git_file_cache
.__contains
__
126 is_git_file
= _init_git_file_finder()
129 def script_iterator(d
=BASEDIR
, cache
=None,
131 filename_filter
=None,
136 safename
= re
.compile(r
'\W+').sub
137 for subdir
in subdirs
:
138 sd
= os
.path
.join(d
, subdir
)
139 for root
, dirs
, files
in os
.walk(sd
, followlinks
=False):
143 if fn
.endswith('.inst'):
145 ffn
= os
.path
.join(root
, fn
)
148 except FileNotFoundError
:
150 if not s
.st_mode
& stat
.S_IXUSR
:
152 if not (subdir
== 'bin' or is_git_file(ffn
)):
155 if filename_filter
is not None:
156 if not filename_filter(ffn
):
159 if shebang_filter
is not None:
163 print("could not open %s: %s" % (ffn
, e
))
167 if not shebang_filter(line
):
170 name
= safename('_', fn
)
177 # For ELF we only look at /bin/* top level.
178 def elf_file_name(fn
):
179 fn
= fn
.partition('bin/')[2]
180 return fn
and '/' not in fn
and 'test' not in fn
and 'ldb' in fn
183 return x
[:4] == b
'\x7fELF'
187 return script_iterator(BASEDIR
, elf_cache
,
188 shebang_filter
=elf_shebang
,
189 filename_filter
=elf_file_name
,
193 perl_shebang
= re
.compile(br
'#!.+perl').match
195 perl_script_cache
= {}
196 def perl_script_iterator():
197 return script_iterator(BASEDIR
, perl_script_cache
, perl_shebang
)
200 python_shebang
= re
.compile(br
'#!.+python').match
202 python_script_cache
= {}
203 def python_script_iterator():
204 return script_iterator(BASEDIR
, python_script_cache
, python_shebang
)
207 class PerlScriptUsageTests(TestCase
):
208 """Perl scripts run without arguments should print a usage string,
209 not fail with a traceback.
214 for name
, filename
in perl_script_iterator():
215 print(name
, filename
)
218 class PythonScriptUsageTests(TestCase
):
219 """Python scripts run without arguments should print a usage string,
220 not fail with a traceback.
225 for name
, filename
in python_script_iterator():
226 # We add the actual tests after the class definition so we
227 # can give individual names to them, so we can have a
229 fn
= filename
.replace(BASEDIR
, '').lstrip('/')
231 if fn
in EXCLUDE_USAGE
:
232 print("skipping %s (EXCLUDE_USAGE)" % filename
)
235 if os
.path
.dirname(fn
) in EXCLUDE_DIRS
:
236 print("skipping %s (EXCLUDE_DIRS)" % filename
)
239 def _f(self
, filename
=filename
):
242 p
= subprocess
.Popen(['python3', filename
],
243 stderr
=subprocess
.PIPE
,
244 stdout
=subprocess
.PIPE
)
245 out
, err
= p
.communicate(timeout
=5)
247 self
.fail("Error: %s" % e
)
248 except subprocess
.SubprocessError
as e
:
249 self
.fail("Subprocess error: %s" % e
)
251 err
= err
.decode('utf-8')
252 out
= out
.decode('utf-8')
253 self
.assertNotIn('Traceback', err
)
255 self
.assertIn('usage', out
.lower() + err
.lower(),
256 'stdout:\n%s\nstderr:\n%s' % (out
, err
))
258 setattr(cls
, 'test_%s' % name
, _f
)
261 class HelpTestSuper(TestCase
):
262 """Python scripts run with -h or --help should print a help string,
263 and exit with success.
265 check_return_code
= True
266 check_consistency
= True
267 check_contains_usage
= True
268 check_multiline
= True
269 check_merged_out_and_err
= False
276 raise NotImplementedError("Subclass this "
277 "and add an iterator function!")
281 for name
, filename
in cls
.iterator():
282 # We add the actual tests after the class definition so we
283 # can give individual names to them, so we can have a
285 fn
= filename
.replace(BASEDIR
, '').lstrip('/')
287 if fn
in EXCLUDE_HELP
:
288 print("skipping %s (EXCLUDE_HELP)" % filename
)
291 if os
.path
.dirname(fn
) in EXCLUDE_DIRS
:
292 print("skipping %s (EXCLUDE_DIRS)" % filename
)
295 def _f(self
, filename
=filename
):
297 for h
in ('--help', '-h'):
300 cmd
.insert(0, self
.interpreter
)
302 p
= subprocess
.Popen(cmd
,
303 stderr
=subprocess
.PIPE
,
304 stdout
=subprocess
.PIPE
)
305 out
, err
= p
.communicate(timeout
=5)
307 self
.fail("Error: %s" % e
)
308 except subprocess
.SubprocessError
as e
:
309 self
.fail("Subprocess error: %s" % e
)
311 err
= err
.decode('utf-8')
312 out
= out
.decode('utf-8')
313 if self
.check_merged_out_and_err
:
314 out
= "%s\n%s" % (out
, err
)
316 outl
= out
[:500].lower()
318 # These assertions are heuristics, not policy.
319 # If your script fails this test when it shouldn't
320 # just add it to EXCLUDE_HELP above or change the
323 # --help should produce:
324 # * multiple lines of help on stdout (not stderr),
325 # * including a "Usage:" string,
326 # * not contradict itself or repeat options,
327 # * and return success.
328 #print(out.encode('utf8'))
329 #print(err.encode('utf8'))
330 if self
.check_consistency
:
331 errors
= check_help_consistency(out
,
334 if errors
is not None:
337 if self
.check_return_code
:
338 self
.assertEqual(p
.returncode
, 0,
339 "%s %s\nreturncode should not be %d\n"
340 "err:\n%s\nout:\n%s" %
341 (filename
, h
, p
.returncode
, err
, out
))
342 if self
.check_contains_usage
:
343 self
.assertIn('usage', outl
, 'lacks "Usage:"\n')
344 if self
.check_multiline
:
345 self
.assertIn('\n', out
, 'expected multi-line output')
347 setattr(cls
, 'test_%s' % name
, _f
)
350 class PythonScriptHelpTests(HelpTestSuper
):
351 """Python scripts run with -h or --help should print a help string,
352 and exit with success.
354 iterator
= python_script_iterator
355 interpreter
= 'python3'
358 class ElfHelpTests(HelpTestSuper
):
359 """ELF binaries run with -h or --help should print a help string,
360 and exit with success.
362 iterator
= elf_iterator
363 check_return_code
= False
364 check_merged_out_and_err
= True
367 PerlScriptUsageTests
.initialise()
368 PythonScriptUsageTests
.initialise()
369 PythonScriptHelpTests
.initialise()
370 ElfHelpTests
.initialise()