process_rename_test.py.in 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  2. #
  3. # Permission to use, copy, modify, and distribute this software for any
  4. # purpose with or without fee is hereby granted, provided that the above
  5. # copyright notice and this permission notice appear in all copies.
  6. #
  7. # THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
  8. # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  9. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  10. # INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  12. # FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  14. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. """Tests to see if every python process renames itself."""
  16. import unittest
  17. import os
  18. import os.path
  19. import isc.util.process
  20. import re
  21. class TestRename(unittest.TestCase):
  22. """Test scanning all python scripts if they rename themself."""
  23. def __scan(self, directory, script, fun):
  24. # Scan one script if it contains call to the renaming function
  25. filename = os.path.join(directory, script)
  26. with open(filename) as f:
  27. data = ''.join(f.readlines())
  28. prettyname = 'src' + filename[filename.rfind('../') + 2:]
  29. self.assertTrue(fun.search(data),
  30. "Didn't find a call to isc.util.process.rename in " + prettyname)
  31. def test_calls(self):
  32. """
  33. Test if every script renames itself.
  34. Scan all Makefile and look for scripts.
  35. Then scan them by looking at the source text
  36. (without actually running them)
  37. """
  38. # Scripts nameds contained in this list are not expected to be
  39. # renamed and should be excluded from the list.
  40. EXCLUDED_SCRIPTS = ['isc-sysinfo']
  41. # Regexp to find all the *_SCRIPTS = something lines (except for
  42. # noinst_SCRIPTS, which are scripts for tests), including line
  43. # continuations (backslash and newline)
  44. excluded_lines = re.compile(r'^(noinst_SCRIPTS.*$)', re.MULTILINE)
  45. lines = re.compile(r'^\w+_SCRIPTS\s*=\s*((.|\\\n)*)$',
  46. re.MULTILINE)
  47. # Script name regular expression
  48. scripts = re.compile(r'((\w|[-.0-9])+)')
  49. # Line with the call
  50. fun = re.compile(r'^\s*isc\.util\.process\.rename\s*\(.*\)\s*(|#.*)$',
  51. re.MULTILINE)
  52. # Find all Makefile and extract names of scripts
  53. for (d, _, fs) in os.walk('@top_builddir@'):
  54. if 'Makefile' in fs:
  55. with open(os.path.join(d, "Makefile")) as f:
  56. makefile = ''.join(f.readlines())
  57. for (var, _) in lines.findall(re.sub(excluded_lines, '',
  58. makefile)):
  59. for (script, _) in scripts.findall(var):
  60. if script in EXCLUDED_SCRIPTS:
  61. continue
  62. self.__scan(d, script, fun)
  63. if __name__ == "__main__":
  64. unittest.main()