unit.py 16 KB

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