unit.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Pedro Algarvio (pedro@algarvio.me)
  4. ============================
  5. Unittest Compatibility Layer
  6. ============================
  7. Compatibility layer to use :mod:`unittest <python2:unittest>` under Python
  8. 2.7 or `unittest2`_ under Python 2.6 without having to worry about which is
  9. in use.
  10. .. attention::
  11. Please refer to Python's :mod:`unittest <python2:unittest>`
  12. documentation as the ultimate source of information, this is just a
  13. compatibility layer.
  14. .. _`unittest2`: https://pypi.python.org/pypi/unittest2
  15. """
  16. # pylint: disable=unused-import,blacklisted-module,deprecated-method
  17. # Import python libs
  18. from __future__ import absolute_import, print_function, unicode_literals
  19. import logging
  20. import os
  21. import sys
  22. from unittest import TestCase as _TestCase
  23. from unittest import TestLoader as _TestLoader
  24. from unittest import TestResult
  25. from unittest import TestSuite as _TestSuite
  26. from unittest import TextTestResult as _TextTestResult
  27. from unittest import TextTestRunner as _TextTestRunner
  28. from unittest import expectedFailure, skip, skipIf
  29. from unittest.case import SkipTest, _id
  30. from salt.ext import six
  31. try:
  32. import psutil
  33. HAS_PSUTIL = True
  34. except ImportError:
  35. HAS_PSUTIL = False
  36. log = logging.getLogger(__name__)
  37. # Set SHOW_PROC to True to show
  38. # process details when running in verbose mode
  39. # i.e. [CPU:15.1%|MEM:48.3%|Z:0]
  40. SHOW_PROC = "NO_SHOW_PROC" not in os.environ
  41. LOREM_IPSUM = """\
  42. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque eget urna a arcu lacinia sagittis.
  43. Sed scelerisque, lacus eget malesuada vestibulum, justo diam facilisis tortor, in sodales dolor
  44. nibh eu urna. Aliquam iaculis massa risus, sed elementum risus accumsan id. Suspendisse mattis,
  45. metus sed lacinia dictum, leo orci dapibus sapien, at porttitor sapien nulla ac velit.
  46. Duis ac cursus leo, non varius metus. Sed laoreet felis magna, vel tempor diam malesuada nec.
  47. Quisque cursus odio tortor. In consequat augue nisl, eget lacinia odio vestibulum eget.
  48. Donec venenatis elementum arcu at rhoncus. Nunc pharetra erat in lacinia convallis. Ut condimentum
  49. eu mauris sit amet convallis. Morbi vulputate vel odio non laoreet. Nullam in suscipit tellus.
  50. Sed quis posuere urna."""
  51. class TestSuite(_TestSuite):
  52. def _handleClassSetUp(self, test, result):
  53. previousClass = getattr(result, "_previousTestClass", None)
  54. currentClass = test.__class__
  55. if (
  56. currentClass == previousClass
  57. or getattr(currentClass, "setUpClass", None) is None
  58. ):
  59. return super(TestSuite, self)._handleClassSetUp(test, result)
  60. # Store a reference to all class attributes before running the setUpClass method
  61. initial_class_attributes = dir(test.__class__)
  62. super(TestSuite, self)._handleClassSetUp(test, result)
  63. # Store the difference in in a variable in order to check later if they were deleted
  64. test.__class__._prerun_class_attributes = [
  65. attr for attr in dir(test.__class__) if attr not in initial_class_attributes
  66. ]
  67. def _tearDownPreviousClass(self, test, result):
  68. # Run any tearDownClass code defined
  69. super(TestSuite, self)._tearDownPreviousClass(test, result)
  70. previousClass = getattr(result, "_previousTestClass", None)
  71. currentClass = test.__class__
  72. if currentClass == previousClass:
  73. return
  74. # See if the previous class attributes have been cleaned
  75. if previousClass and getattr(previousClass, "tearDownClass", None):
  76. prerun_class_attributes = getattr(
  77. previousClass, "_prerun_class_attributes", None
  78. )
  79. if prerun_class_attributes is not None:
  80. previousClass._prerun_class_attributes = None
  81. del previousClass._prerun_class_attributes
  82. for attr in prerun_class_attributes:
  83. if hasattr(previousClass, attr):
  84. attr_value = getattr(previousClass, attr, None)
  85. if attr_value is None:
  86. continue
  87. if isinstance(
  88. attr_value, (bool,) + six.string_types + six.integer_types
  89. ):
  90. setattr(previousClass, attr, None)
  91. continue
  92. log.warning(
  93. "Deleting extra class attribute after test run: %s.%s(%s). "
  94. "Please consider using 'del self.%s' on the test class "
  95. "'tearDownClass()' method",
  96. previousClass.__name__,
  97. attr,
  98. str(getattr(previousClass, attr))[:100],
  99. attr,
  100. )
  101. delattr(previousClass, attr)
  102. class TestLoader(_TestLoader):
  103. # We're just subclassing to make sure tha tour TestSuite class is the one used
  104. suiteClass = TestSuite
  105. class TestCase(_TestCase):
  106. # pylint: disable=expected-an-indented-block-comment,too-many-leading-hastag-for-block-comment
  107. ## Commented out because it may be causing tests to hang
  108. ## at the end of the run
  109. #
  110. # _cwd = os.getcwd()
  111. # _chdir_counter = 0
  112. # @classmethod
  113. # def tearDownClass(cls):
  114. # '''
  115. # Overriden method for tearing down all classes in salttesting
  116. #
  117. # This hard-resets the environment between test classes
  118. # '''
  119. # # Compare where we are now compared to where we were when we began this family of tests
  120. # if not cls._cwd == os.getcwd() and cls._chdir_counter > 0:
  121. # os.chdir(cls._cwd)
  122. # print('\nWARNING: A misbehaving test has modified the working directory!\nThe test suite has reset the working directory '
  123. # 'on tearDown() to {0}\n'.format(cls._cwd))
  124. # cls._chdir_counter += 1
  125. # pylint: enable=expected-an-indented-block-comment,too-many-leading-hastag-for-block-comment
  126. def run(self, result=None):
  127. self._prerun_instance_attributes = dir(self)
  128. self.maxDiff = None
  129. outcome = super(TestCase, self).run(result=result)
  130. for attr in dir(self):
  131. if attr == "_prerun_instance_attributes":
  132. continue
  133. if attr in getattr(self.__class__, "_prerun_class_attributes", ()):
  134. continue
  135. if attr not in self._prerun_instance_attributes:
  136. attr_value = getattr(self, attr, None)
  137. if attr_value is None:
  138. continue
  139. if isinstance(
  140. attr_value, (bool,) + six.string_types + six.integer_types
  141. ):
  142. setattr(self, attr, None)
  143. continue
  144. log.warning(
  145. "Deleting extra class attribute after test run: %s.%s(%s). "
  146. "Please consider using 'del self.%s' on the test case "
  147. "'tearDown()' method",
  148. self.__class__.__name__,
  149. attr,
  150. getattr(self, attr),
  151. attr,
  152. )
  153. delattr(self, attr)
  154. self._prerun_instance_attributes = None
  155. del self._prerun_instance_attributes
  156. return outcome
  157. def shortDescription(self):
  158. desc = _TestCase.shortDescription(self)
  159. if HAS_PSUTIL and SHOW_PROC:
  160. show_zombie_processes = "SHOW_PROC_ZOMBIES" in os.environ
  161. proc_info = "[CPU:{0}%|MEM:{1}%".format(
  162. psutil.cpu_percent(), psutil.virtual_memory().percent
  163. )
  164. if show_zombie_processes:
  165. found_zombies = 0
  166. try:
  167. for proc in psutil.process_iter():
  168. if proc.status == psutil.STATUS_ZOMBIE:
  169. found_zombies += 1
  170. except Exception: # pylint: disable=broad-except
  171. pass
  172. proc_info += "|Z:{0}".format(found_zombies)
  173. proc_info += "] {short_desc}".format(short_desc=desc if desc else "")
  174. return proc_info
  175. else:
  176. return _TestCase.shortDescription(self)
  177. def assertEquals(self, *args, **kwargs):
  178. raise DeprecationWarning(
  179. "The {0}() function is deprecated. Please start using {1}() "
  180. "instead.".format("assertEquals", "assertEqual")
  181. )
  182. # return _TestCase.assertEquals(self, *args, **kwargs)
  183. def assertNotEquals(self, *args, **kwargs):
  184. raise DeprecationWarning(
  185. "The {0}() function is deprecated. Please start using {1}() "
  186. "instead.".format("assertNotEquals", "assertNotEqual")
  187. )
  188. # return _TestCase.assertNotEquals(self, *args, **kwargs)
  189. def assert_(self, *args, **kwargs):
  190. # The unittest2 library uses this deprecated method, we can't raise
  191. # the exception.
  192. raise DeprecationWarning(
  193. "The {0}() function is deprecated. Please start using {1}() "
  194. "instead.".format("assert_", "assertTrue")
  195. )
  196. # return _TestCase.assert_(self, *args, **kwargs)
  197. def assertAlmostEquals(self, *args, **kwargs):
  198. raise DeprecationWarning(
  199. "The {0}() function is deprecated. Please start using {1}() "
  200. "instead.".format("assertAlmostEquals", "assertAlmostEqual")
  201. )
  202. # return _TestCase.assertAlmostEquals(self, *args, **kwargs)
  203. def assertNotAlmostEquals(self, *args, **kwargs):
  204. raise DeprecationWarning(
  205. "The {0}() function is deprecated. Please start using {1}() "
  206. "instead.".format("assertNotAlmostEquals", "assertNotAlmostEqual")
  207. )
  208. # return _TestCase.assertNotAlmostEquals(self, *args, **kwargs)
  209. def repack_state_returns(self, state_ret):
  210. """
  211. Accepts a state return dict and returns it back with the top level key
  212. names rewritten such that the ID declaration is the key instead of the
  213. State's unique tag. For example: 'foo' instead of
  214. 'file_|-foo_|-/etc/foo.conf|-managed'
  215. This makes it easier to work with state returns when crafting asserts
  216. after running states.
  217. """
  218. assert isinstance(state_ret, dict), state_ret
  219. return {x.split("_|-")[1]: y for x, y in six.iteritems(state_ret)}
  220. def failUnlessEqual(self, *args, **kwargs):
  221. raise DeprecationWarning(
  222. "The {0}() function is deprecated. Please start using {1}() "
  223. "instead.".format("failUnlessEqual", "assertEqual")
  224. )
  225. # return _TestCase.failUnlessEqual(self, *args, **kwargs)
  226. def failIfEqual(self, *args, **kwargs):
  227. raise DeprecationWarning(
  228. "The {0}() function is deprecated. Please start using {1}() "
  229. "instead.".format("failIfEqual", "assertNotEqual")
  230. )
  231. # return _TestCase.failIfEqual(self, *args, **kwargs)
  232. def failUnless(self, *args, **kwargs):
  233. raise DeprecationWarning(
  234. "The {0}() function is deprecated. Please start using {1}() "
  235. "instead.".format("failUnless", "assertTrue")
  236. )
  237. # return _TestCase.failUnless(self, *args, **kwargs)
  238. def failIf(self, *args, **kwargs):
  239. raise DeprecationWarning(
  240. "The {0}() function is deprecated. Please start using {1}() "
  241. "instead.".format("failIf", "assertFalse")
  242. )
  243. # return _TestCase.failIf(self, *args, **kwargs)
  244. def failUnlessRaises(self, *args, **kwargs):
  245. raise DeprecationWarning(
  246. "The {0}() function is deprecated. Please start using {1}() "
  247. "instead.".format("failUnlessRaises", "assertRaises")
  248. )
  249. # return _TestCase.failUnlessRaises(self, *args, **kwargs)
  250. def failUnlessAlmostEqual(self, *args, **kwargs):
  251. raise DeprecationWarning(
  252. "The {0}() function is deprecated. Please start using {1}() "
  253. "instead.".format("failUnlessAlmostEqual", "assertAlmostEqual")
  254. )
  255. # return _TestCase.failUnlessAlmostEqual(self, *args, **kwargs)
  256. def failIfAlmostEqual(self, *args, **kwargs):
  257. raise DeprecationWarning(
  258. "The {0}() function is deprecated. Please start using {1}() "
  259. "instead.".format("failIfAlmostEqual", "assertNotAlmostEqual")
  260. )
  261. # return _TestCase.failIfAlmostEqual(self, *args, **kwargs)
  262. @staticmethod
  263. def assert_called_once(mock):
  264. """
  265. mock.assert_called_once only exists in PY3 in 3.6 and newer
  266. """
  267. try:
  268. mock.assert_called_once()
  269. except AttributeError:
  270. log.warning("assert_called_once invoked, but not available")
  271. if six.PY2:
  272. def assertRegexpMatches(self, *args, **kwds):
  273. raise DeprecationWarning(
  274. "The {0}() function will be deprecated in python 3. Please start "
  275. "using {1}() instead.".format("assertRegexpMatches", "assertRegex")
  276. )
  277. def assertRegex(
  278. self, text, regex, msg=None
  279. ): # pylint: disable=arguments-differ
  280. # In python 2, alias to the future python 3 function
  281. return _TestCase.assertRegexpMatches(self, text, regex, msg=msg)
  282. def assertNotRegexpMatches(self, *args, **kwds):
  283. raise DeprecationWarning(
  284. "The {0}() function will be deprecated in python 3. Please start "
  285. "using {1}() instead.".format(
  286. "assertNotRegexpMatches", "assertNotRegex"
  287. )
  288. )
  289. def assertNotRegex(
  290. self, text, regex, msg=None
  291. ): # pylint: disable=arguments-differ
  292. # In python 2, alias to the future python 3 function
  293. return _TestCase.assertNotRegexpMatches(self, text, regex, msg=msg)
  294. def assertRaisesRegexp(self, *args, **kwds):
  295. raise DeprecationWarning(
  296. "The {0}() function will be deprecated in python 3. Please start "
  297. "using {1}() instead.".format("assertRaisesRegexp", "assertRaisesRegex")
  298. )
  299. def assertRaisesRegex(
  300. self, exception, regexp, *args, **kwds
  301. ): # pylint: disable=arguments-differ
  302. # In python 2, alias to the future python 3 function
  303. return _TestCase.assertRaisesRegexp(self, exception, regexp, *args, **kwds)
  304. else:
  305. def assertRegexpMatches(self, *args, **kwds):
  306. raise DeprecationWarning(
  307. "The {0}() function is deprecated. Please start using {1}() "
  308. "instead.".format("assertRegexpMatches", "assertRegex")
  309. )
  310. def assertNotRegexpMatches(self, *args, **kwds):
  311. raise DeprecationWarning(
  312. "The {0}() function is deprecated. Please start using {1}() "
  313. "instead.".format("assertNotRegexpMatches", "assertNotRegex")
  314. )
  315. def assertRaisesRegexp(self, *args, **kwds):
  316. raise DeprecationWarning(
  317. "The {0}() function is deprecated. Please start using {1}() "
  318. "instead.".format("assertRaisesRegexp", "assertRaisesRegex")
  319. )
  320. class TextTestResult(_TextTestResult):
  321. """
  322. Custom TestResult class whith logs the start and the end of a test
  323. """
  324. def startTest(self, test):
  325. log.debug(">>>>> START >>>>> {0}".format(test.id()))
  326. return super(TextTestResult, self).startTest(test)
  327. def stopTest(self, test):
  328. log.debug("<<<<< END <<<<<<< {0}".format(test.id()))
  329. return super(TextTestResult, self).stopTest(test)
  330. class TextTestRunner(_TextTestRunner):
  331. """
  332. Custom Text tests runner to log the start and the end of a test case
  333. """
  334. resultclass = TextTestResult
  335. __all__ = [
  336. "TestLoader",
  337. "TextTestRunner",
  338. "TestCase",
  339. "expectedFailure",
  340. "TestSuite",
  341. "skipIf",
  342. "TestResult",
  343. ]