1
0

test_module_names.py 11 KB

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