1
0

test_module_names.py 11 KB

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