test_module_names.py 11 KB

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