test_module_names.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. # -*- coding: utf-8 -*-
  2. '''
  3. tests.unit.test_test_module_name
  4. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. '''
  6. # Import Python libs
  7. from __future__ import absolute_import
  8. import fnmatch
  9. import os
  10. # Import Salt libs
  11. import salt.utils.path
  12. import salt.utils.stringutils
  13. # Import Salt Testing libs
  14. from tests.support.unit import TestCase
  15. from tests.support.paths import CODE_DIR, test_mods
  16. EXCLUDED_DIRS = [
  17. os.path.join('tests', 'pkg'),
  18. os.path.join('tests', 'perf'),
  19. os.path.join('tests', 'support'),
  20. os.path.join('tests', 'unit', 'utils', 'cache_mods'),
  21. os.path.join('tests', 'unit', 'modules', 'inspectlib'),
  22. os.path.join('tests', 'unit', 'modules', 'zypp'),
  23. os.path.join('tests', 'unit', 'templates', 'files'),
  24. os.path.join('tests', 'integration', 'files'),
  25. os.path.join('tests', 'unit', 'files'),
  26. os.path.join('tests', 'integration', 'cloud', 'helpers'),
  27. os.path.join('tests', 'kitchen', 'tests'),
  28. ]
  29. INCLUDED_DIRS = [
  30. os.path.join('tests', 'kitchen', 'tests', '*', 'tests', '*'),
  31. ]
  32. EXCLUDED_FILES = [
  33. os.path.join('tests', 'eventlisten.py'),
  34. os.path.join('tests', 'buildpackage.py'),
  35. os.path.join('tests', 'saltsh.py'),
  36. os.path.join('tests', 'minionswarm.py'),
  37. os.path.join('tests', 'wheeltest.py'),
  38. os.path.join('tests', 'runtests.py'),
  39. os.path.join('tests', 'jenkins.py'),
  40. os.path.join('tests', 'salt-tcpdump.py'),
  41. os.path.join('tests', 'conftest.py'),
  42. os.path.join('tests', 'packdump.py'),
  43. os.path.join('tests', 'consist.py'),
  44. os.path.join('tests', 'modparser.py'),
  45. os.path.join('tests', 'virtualname.py'),
  46. os.path.join('tests', 'committer_parser.py'),
  47. os.path.join('tests', 'zypp_plugin.py'),
  48. os.path.join('tests', 'tox-helper.py'),
  49. os.path.join('tests', 'unit', 'transport', 'mixins.py'),
  50. os.path.join('tests', 'integration', 'utils', 'testprogram.py'),
  51. ]
  52. class BadTestModuleNamesTestCase(TestCase):
  53. '''
  54. Unit test case for testing bad names for test modules
  55. '''
  56. maxDiff = None
  57. def _match_dirs(self, reldir, matchdirs):
  58. return any(fnmatch.fnmatchcase(reldir, mdir) for mdir in matchdirs)
  59. def test_module_name(self):
  60. '''
  61. Make sure all test modules conform to the test_*.py naming scheme
  62. '''
  63. excluded_dirs, included_dirs = tuple(EXCLUDED_DIRS), tuple(INCLUDED_DIRS)
  64. tests_dir = os.path.join(CODE_DIR, 'tests')
  65. bad_names = []
  66. for root, dirs, files in salt.utils.path.os_walk(tests_dir):
  67. reldir = os.path.relpath(root, CODE_DIR)
  68. if (reldir.startswith(excluded_dirs) and not self._match_dirs(reldir, included_dirs)) \
  69. or reldir.endswith('__pycache__'):
  70. continue
  71. for fname in files:
  72. if fname == '__init__.py' or not fname.endswith('.py'):
  73. continue
  74. relpath = os.path.join(reldir, fname)
  75. if relpath in EXCLUDED_FILES:
  76. continue
  77. if not fname.startswith('test_'):
  78. bad_names.append(relpath)
  79. error_msg = '\n\nPlease rename the following files:\n'
  80. for path in bad_names:
  81. directory, filename = path.rsplit(os.sep, 1)
  82. filename, ext = os.path.splitext(filename)
  83. error_msg += ' {} -> {}/test_{}.py\n'.format(path, directory, filename.split('_test')[0])
  84. error_msg += '\nIf you believe one of the entries above should be ignored, please add it to either\n'
  85. error_msg += '\'EXCLUDED_DIRS\' or \'EXCLUDED_FILES\' in \'tests/unit/test_module_names.py\'.\n'
  86. error_msg += 'If it is a tests module, then please rename as suggested.'
  87. self.assertEqual([], bad_names, error_msg)
  88. def test_module_name_source_match(self):
  89. '''
  90. Check all the test mods and check if they correspond to actual files in
  91. the codebase. If this test fails, then a test module is likely not
  92. named correctly, and should be adjusted.
  93. If a test module doesn't have a natural name match (as does this very
  94. file), then its should be included in the "ignore" tuple below.
  95. However, if there is no matching source code file, then you should
  96. consider mapping it to files manually via tests/filename_map.yml.
  97. '''
  98. ignore = (
  99. 'unit.test_doc',
  100. 'unit.test_mock',
  101. 'unit.test_module_names',
  102. 'unit.test_virtualname',
  103. 'unit.test_simple',
  104. 'unit.test_zypp_plugins',
  105. 'unit.test_proxy_minion',
  106. 'unit.cache.test_cache',
  107. 'unit.serializers.test_serializers',
  108. 'unit.states.test_postgres',
  109. 'integration.cli.test_custom_module',
  110. 'integration.cli.test_grains',
  111. 'integration.client.test_kwarg',
  112. 'integration.client.test_runner',
  113. 'integration.client.test_standard',
  114. 'integration.client.test_syndic',
  115. 'integration.cloud.test_cloud',
  116. 'integration.doc.test_man',
  117. 'integration.externalapi.test_venafiapi',
  118. 'integration.grains.test_custom',
  119. 'integration.loader.test_ext_grains',
  120. 'integration.loader.test_ext_modules',
  121. 'integration.logging.test_jid_logging',
  122. 'integration.master.test_event_return',
  123. 'integration.minion.test_blackout',
  124. 'integration.minion.test_pillar',
  125. 'integration.minion.test_timeout',
  126. 'integration.modules.test_decorators',
  127. 'integration.modules.test_pkg',
  128. 'integration.modules.test_state_jinja_filters',
  129. 'integration.modules.test_sysctl',
  130. 'integration.netapi.test_client',
  131. 'integration.netapi.rest_tornado.test_app',
  132. 'integration.netapi.rest_cherrypy.test_app_pam',
  133. 'integration.output.test_output',
  134. 'integration.pillar.test_pillar_include',
  135. 'integration.proxy.test_shell',
  136. 'integration.proxy.test_simple',
  137. 'integration.reactor.test_reactor',
  138. 'integration.returners.test_noop_return',
  139. 'integration.runners.test_runner_returns',
  140. 'integration.scheduler.test_error',
  141. 'integration.scheduler.test_eval',
  142. 'integration.scheduler.test_postpone',
  143. 'integration.scheduler.test_skip',
  144. 'integration.scheduler.test_maxrunning',
  145. 'integration.scheduler.test_helpers',
  146. 'integration.scheduler.test_run_job',
  147. 'integration.shell.test_spm',
  148. 'integration.shell.test_cp',
  149. 'integration.shell.test_syndic',
  150. 'integration.shell.test_proxy',
  151. 'integration.shell.test_auth',
  152. 'integration.shell.test_call',
  153. 'integration.shell.test_arguments',
  154. 'integration.shell.test_matcher',
  155. 'integration.shell.test_master_tops',
  156. 'integration.shell.test_saltcli',
  157. 'integration.shell.test_master',
  158. 'integration.shell.test_key',
  159. 'integration.shell.test_runner',
  160. 'integration.shell.test_cloud',
  161. 'integration.shell.test_enabled',
  162. 'integration.shell.test_minion',
  163. 'integration.spm.test_build',
  164. 'integration.spm.test_files',
  165. 'integration.spm.test_info',
  166. 'integration.spm.test_install',
  167. 'integration.spm.test_remove',
  168. 'integration.spm.test_repo',
  169. 'integration.ssh.test_deploy',
  170. 'integration.ssh.test_grains',
  171. 'integration.ssh.test_jinja_filters',
  172. 'integration.ssh.test_master',
  173. 'integration.ssh.test_mine',
  174. 'integration.ssh.test_pillar',
  175. 'integration.ssh.test_raw',
  176. 'integration.ssh.test_state',
  177. 'integration.states.test_compiler',
  178. 'integration.states.test_handle_error',
  179. 'integration.states.test_handle_iorder',
  180. 'integration.states.test_match',
  181. 'integration.states.test_renderers',
  182. 'integration.wheel.test_client',
  183. 'multimaster.minion.test_event',
  184. )
  185. errors = []
  186. def _format_errors(errors):
  187. msg = (
  188. 'The following {0} test module(s) could not be matched to a '
  189. 'source code file:\n\n'.format(len(errors))
  190. )
  191. msg += ''.join(errors)
  192. return msg
  193. for mod_name in test_mods():
  194. if mod_name in ignore:
  195. # Test module is being ignored, skip it
  196. continue
  197. # Separate the test_foo away from the rest of the mod name, because
  198. # we'll need to remove the "test_" from the beginning and add .py
  199. stem, flower = mod_name.rsplit('.', 1)
  200. # Lop off the integration/unit from the beginning of the mod name
  201. try:
  202. stem = stem.split('.', 1)[1]
  203. except IndexError:
  204. # This test mod was in the root of the unit/integration dir
  205. stem = ''
  206. # The path from the root of the repo
  207. relpath = salt.utils.path.join(
  208. 'salt',
  209. stem.replace('.', os.sep),
  210. '.'.join((flower[5:], 'py')))
  211. # The full path to the file we expect to find
  212. abspath = salt.utils.path.join(CODE_DIR, relpath)
  213. if not os.path.isfile(abspath):
  214. # Maybe this is in a dunder init?
  215. alt_relpath = salt.utils.path.join(relpath[:-3], '__init__.py')
  216. alt_abspath = salt.utils.path.join(abspath[:-3], '__init__.py')
  217. if os.path.isfile(alt_abspath):
  218. # Yep, it is. Carry on!
  219. continue
  220. errors.append(
  221. '{0} (expected: {1})\n'.format(mod_name, relpath)
  222. )
  223. assert not errors, _format_errors(errors)