You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

257 lines
9.9 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. # Copyright 2017 LasLabs Inc.
  2. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
  3. import logging
  4. import os
  5. import tempfile
  6. import mock
  7. from odoo.modules import get_module_path
  8. from odoo.tests.common import TransactionCase
  9. from odoo.tools import mute_logger
  10. from .. import post_init_hook
  11. _logger = logging.getLogger(__name__)
  12. try:
  13. from checksumdir import dirhash
  14. except ImportError:
  15. _logger.debug('Cannot `import checksumdir`.')
  16. model = 'odoo.addons.module_auto_update.models.module_deprecated'
  17. class EndTestException(Exception):
  18. pass
  19. class TestModule(TransactionCase):
  20. def setUp(self):
  21. super(TestModule, self).setUp()
  22. module_name = 'module_auto_update'
  23. self.own_module = self.env['ir.module.module'].search([
  24. ('name', '=', module_name),
  25. ])
  26. self.own_dir_path = get_module_path(module_name)
  27. self.own_checksum = dirhash(
  28. self.own_dir_path,
  29. 'sha1',
  30. excluded_extensions=['pyc', 'pyo'],
  31. )
  32. self.own_writeable = os.access(self.own_dir_path, os.W_OK)
  33. @mock.patch('%s.get_module_path' % model)
  34. def create_test_module(self, vals, get_module_path_mock):
  35. get_module_path_mock.return_value = self.own_dir_path
  36. test_module = self.env['ir.module.module'].create(vals)
  37. return test_module
  38. def test_compute_checksum_dir(self):
  39. """It should compute the directory's SHA-1 hash"""
  40. self.assertEqual(
  41. self.own_module.checksum_dir, self.own_checksum,
  42. 'Module directory checksum not computed properly',
  43. )
  44. def test_compute_checksum_dir_ignore_excluded(self):
  45. """It should exclude .pyc/.pyo extensions from checksum
  46. calculations"""
  47. if not self.own_writeable:
  48. self.skipTest("Own directory not writeable")
  49. with tempfile.NamedTemporaryFile(
  50. suffix='.pyc', dir=self.own_dir_path):
  51. self.assertEqual(
  52. self.own_module.checksum_dir, self.own_checksum,
  53. 'SHA1 checksum does not ignore excluded extensions',
  54. )
  55. def test_compute_checksum_dir_recomputes_when_file_added(self):
  56. """It should return a different value when a non-.pyc/.pyo file is
  57. added to the module directory"""
  58. if not self.own_writeable:
  59. self.skipTest("Own directory not writeable")
  60. with tempfile.NamedTemporaryFile(
  61. suffix='.py', dir=self.own_dir_path):
  62. self.assertNotEqual(
  63. self.own_module.checksum_dir, self.own_checksum,
  64. 'SHA1 checksum not recomputed',
  65. )
  66. def test_store_checksum_installed_state_installed(self):
  67. """It should set the module's checksum_installed equal to
  68. checksum_dir when vals contain a ``latest_version`` str."""
  69. self.own_module.checksum_installed = 'test'
  70. self.own_module._store_checksum_installed({'latest_version': '1.0'})
  71. self.assertEqual(
  72. self.own_module.checksum_installed, self.own_module.checksum_dir,
  73. )
  74. def test_store_checksum_installed_state_uninstalled(self):
  75. """It should clear the module's checksum_installed when vals
  76. contain ``"latest_version": False``"""
  77. self.own_module.checksum_installed = 'test'
  78. self.own_module._store_checksum_installed({'latest_version': False})
  79. self.assertIs(self.own_module.checksum_installed, False)
  80. def test_store_checksum_installed_vals_contain_checksum_installed(self):
  81. """It should not set checksum_installed to False or checksum_dir when
  82. a checksum_installed is included in vals"""
  83. self.own_module.checksum_installed = 'test'
  84. self.own_module._store_checksum_installed({
  85. 'state': 'installed',
  86. 'checksum_installed': 'test',
  87. })
  88. self.assertEqual(
  89. self.own_module.checksum_installed, 'test',
  90. 'Providing checksum_installed in vals did not prevent overwrite',
  91. )
  92. def test_store_checksum_installed_with_retain_context(self):
  93. """It should not set checksum_installed to False or checksum_dir when
  94. self has context retain_checksum_installed=True"""
  95. self.own_module.checksum_installed = 'test'
  96. self.own_module.with_context(
  97. retain_checksum_installed=True,
  98. )._store_checksum_installed({'state': 'installed'})
  99. self.assertEqual(
  100. self.own_module.checksum_installed, 'test',
  101. 'Providing retain_checksum_installed context did not prevent '
  102. 'overwrite',
  103. )
  104. @mock.patch('%s.get_module_path' % model)
  105. def test_button_uninstall_no_recompute(self, module_path_mock):
  106. """It should not attempt update on `button_uninstall`."""
  107. module_path_mock.return_value = self.own_dir_path
  108. vals = {
  109. 'name': 'module_auto_update_test_module',
  110. 'state': 'installed',
  111. }
  112. test_module = self.create_test_module(vals)
  113. test_module.checksum_installed = 'test'
  114. uninstall_module = self.env['ir.module.module'].search([
  115. ('name', '=', 'web'),
  116. ])
  117. uninstall_module.button_uninstall()
  118. self.assertNotEqual(
  119. test_module.state, 'to upgrade',
  120. 'Auto update logic was triggered during uninstall.',
  121. )
  122. def test_button_immediate_uninstall_no_recompute(self):
  123. """It should not attempt update on `button_immediate_uninstall`."""
  124. uninstall_module = self.env['ir.module.module'].search([
  125. ('name', '=', 'web'),
  126. ])
  127. try:
  128. mk = mock.MagicMock()
  129. uninstall_module._patch_method('button_uninstall', mk)
  130. mk.side_effect = EndTestException
  131. with self.assertRaises(EndTestException):
  132. uninstall_module.button_immediate_uninstall()
  133. finally:
  134. uninstall_module._revert_method('button_uninstall')
  135. def test_button_uninstall_cancel(self):
  136. """It should preserve checksum_installed when cancelling uninstall"""
  137. self.own_module.write({'state': 'to remove'})
  138. self.own_module.checksum_installed = 'test'
  139. self.own_module.button_uninstall_cancel()
  140. self.assertEqual(
  141. self.own_module.checksum_installed, 'test',
  142. 'Uninstall cancellation does not preserve checksum_installed',
  143. )
  144. def test_button_upgrade_cancel(self):
  145. """It should preserve checksum_installed when cancelling upgrades"""
  146. self.own_module.write({'state': 'to upgrade'})
  147. self.own_module.checksum_installed = 'test'
  148. self.own_module.button_upgrade_cancel()
  149. self.assertEqual(
  150. self.own_module.checksum_installed, 'test',
  151. 'Upgrade cancellation does not preserve checksum_installed',
  152. )
  153. def test_create(self):
  154. """It should call _store_checksum_installed method"""
  155. _store_checksum_installed_mock = mock.MagicMock()
  156. try:
  157. self.env['ir.module.module']._patch_method(
  158. '_store_checksum_installed',
  159. _store_checksum_installed_mock,
  160. )
  161. vals = {
  162. 'name': 'module_auto_update_test_module',
  163. 'state': 'installed',
  164. }
  165. self.create_test_module(vals)
  166. _store_checksum_installed_mock.assert_called_once_with(vals)
  167. finally:
  168. self.env['ir.module.module']._revert_method(
  169. '_store_checksum_installed',
  170. )
  171. @mute_logger("openerp.modules.module")
  172. @mock.patch('%s.get_module_path' % model)
  173. def test_get_module_list(self, module_path_mock):
  174. """It should change the state of modules with different
  175. checksum_dir and checksum_installed to 'to upgrade'"""
  176. module_path_mock.return_value = self.own_dir_path
  177. vals = {
  178. 'name': 'module_auto_update_test_module',
  179. 'state': 'installed',
  180. }
  181. test_module = self.create_test_module(vals)
  182. test_module.checksum_installed = 'test'
  183. self.env['base.module.upgrade'].get_module_list()
  184. self.assertEqual(
  185. test_module.state, 'to upgrade',
  186. 'List update does not mark upgradeable modules "to upgrade"',
  187. )
  188. @mock.patch('%s.get_module_path' % model)
  189. def test_get_module_list_only_changes_installed(self, module_path_mock):
  190. """It should not change the state of a module with a former state
  191. other than 'installed' to 'to upgrade'"""
  192. module_path_mock.return_value = self.own_dir_path
  193. vals = {
  194. 'name': 'module_auto_update_test_module',
  195. 'state': 'uninstalled',
  196. }
  197. test_module = self.create_test_module(vals)
  198. self.env['base.module.upgrade'].get_module_list()
  199. self.assertNotEqual(
  200. test_module.state, 'to upgrade',
  201. 'List update changed state of an uninstalled module',
  202. )
  203. def test_write(self):
  204. """It should call _store_checksum_installed method"""
  205. _store_checksum_installed_mock = mock.MagicMock()
  206. self.env['ir.module.module']._patch_method(
  207. '_store_checksum_installed',
  208. _store_checksum_installed_mock,
  209. )
  210. vals = {'state': 'installed'}
  211. self.own_module.write(vals)
  212. _store_checksum_installed_mock.assert_called_once_with(vals)
  213. self.env['ir.module.module']._revert_method(
  214. '_store_checksum_installed',
  215. )
  216. def test_post_init_hook(self):
  217. """It should set checksum_installed equal to checksum_dir for all
  218. installed modules"""
  219. installed_modules = self.env['ir.module.module'].search([
  220. ('state', '=', 'installed'),
  221. ])
  222. post_init_hook(self.env.cr, None)
  223. self.assertListEqual(
  224. installed_modules.mapped('checksum_dir'),
  225. installed_modules.mapped('checksum_installed'),
  226. 'Installed modules did not have checksum_installed stored',
  227. )