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.

255 lines
9.8 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
  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'
  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. self.env['ir.module.module']._patch_method(
  157. '_store_checksum_installed',
  158. _store_checksum_installed_mock,
  159. )
  160. vals = {
  161. 'name': 'module_auto_update_test_module',
  162. 'state': 'installed',
  163. }
  164. self.create_test_module(vals)
  165. _store_checksum_installed_mock.assert_called_once_with(vals)
  166. self.env['ir.module.module']._revert_method(
  167. '_store_checksum_installed',
  168. )
  169. @mute_logger("openerp.modules.module")
  170. @mock.patch('%s.get_module_path' % model)
  171. def test_get_module_list(self, module_path_mock):
  172. """It should change the state of modules with different
  173. checksum_dir and checksum_installed to 'to upgrade'"""
  174. module_path_mock.return_value = self.own_dir_path
  175. vals = {
  176. 'name': 'module_auto_update_test_module',
  177. 'state': 'installed',
  178. }
  179. test_module = self.create_test_module(vals)
  180. test_module.checksum_installed = 'test'
  181. self.env['base.module.upgrade'].get_module_list()
  182. self.assertEqual(
  183. test_module.state, 'to upgrade',
  184. 'List update does not mark upgradeable modules "to upgrade"',
  185. )
  186. @mock.patch('%s.get_module_path' % model)
  187. def test_get_module_list_only_changes_installed(self, module_path_mock):
  188. """It should not change the state of a module with a former state
  189. other than 'installed' to 'to upgrade'"""
  190. module_path_mock.return_value = self.own_dir_path
  191. vals = {
  192. 'name': 'module_auto_update_test_module',
  193. 'state': 'uninstalled',
  194. }
  195. test_module = self.create_test_module(vals)
  196. self.env['base.module.upgrade'].get_module_list()
  197. self.assertNotEqual(
  198. test_module.state, 'to upgrade',
  199. 'List update changed state of an uninstalled module',
  200. )
  201. def test_write(self):
  202. """It should call _store_checksum_installed method"""
  203. _store_checksum_installed_mock = mock.MagicMock()
  204. self.env['ir.module.module']._patch_method(
  205. '_store_checksum_installed',
  206. _store_checksum_installed_mock,
  207. )
  208. vals = {'state': 'installed'}
  209. self.own_module.write(vals)
  210. _store_checksum_installed_mock.assert_called_once_with(vals)
  211. self.env['ir.module.module']._revert_method(
  212. '_store_checksum_installed',
  213. )
  214. def test_post_init_hook(self):
  215. """It should set checksum_installed equal to checksum_dir for all
  216. installed modules"""
  217. installed_modules = self.env['ir.module.module'].search([
  218. ('state', '=', 'installed'),
  219. ])
  220. post_init_hook(self.env.cr, None)
  221. self.assertListEqual(
  222. installed_modules.mapped('checksum_dir'),
  223. installed_modules.mapped('checksum_installed'),
  224. 'Installed modules did not have checksum_installed stored',
  225. )