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.

256 lines
9.8 KiB

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