1
0

test_module_names.py 10 KB

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