test_opkg.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. # -*- coding: utf-8 -*-
  2. """
  3. :synopsis: Unit Tests for Package Management module 'module.opkg'
  4. :platform: Linux
  5. """
  6. # pylint: disable=import-error,3rd-party-module-not-gated
  7. # Import Python libs
  8. from __future__ import absolute_import, print_function, unicode_literals
  9. import collections
  10. import copy
  11. import salt.modules.opkg as opkg
  12. # Import Salt Libs
  13. from salt.ext import six
  14. # Import Salt Testing Libs
  15. from tests.support.mixins import LoaderModuleMockMixin
  16. from tests.support.mock import MagicMock, patch
  17. from tests.support.unit import TestCase
  18. # pylint: disable=import-error,3rd-party-module-not-gated
  19. OPKG_VIM_INFO = {
  20. "vim": {
  21. "Package": "vim",
  22. "Version": "7.4.769-r0.31",
  23. "Status": "install ok installed",
  24. }
  25. }
  26. OPKG_VIM_FILES = {
  27. "errors": [],
  28. "packages": {
  29. "vim": [
  30. "/usr/bin/view",
  31. "/usr/bin/vim.vim",
  32. "/usr/bin/xxd",
  33. "/usr/bin/vimdiff",
  34. "/usr/bin/rview",
  35. "/usr/bin/rvim",
  36. "/usr/bin/ex",
  37. ]
  38. },
  39. }
  40. INSTALLED = {"vim": {"new": "7.4", "old": six.text_type()}}
  41. REMOVED = {"vim": {"new": six.text_type(), "old": "7.4"}}
  42. PACKAGES = {"vim": "7.4"}
  43. class OpkgTestCase(TestCase, LoaderModuleMockMixin):
  44. """
  45. Test cases for salt.modules.opkg
  46. """
  47. def setup_loader_modules(self): # pylint: disable=no-self-use
  48. """
  49. Tested modules
  50. """
  51. return {opkg: {}}
  52. def test_version(self):
  53. """
  54. Test - Returns a string representing the package version or an empty string if
  55. not installed.
  56. """
  57. version = OPKG_VIM_INFO["vim"]["Version"]
  58. mock = MagicMock(return_value=version)
  59. with patch.dict(opkg.__salt__, {"pkg_resource.version": mock}):
  60. self.assertEqual(opkg.version(*["vim"]), version)
  61. def test_upgrade_available(self):
  62. """
  63. Test - Check whether or not an upgrade is available for a given package.
  64. """
  65. with patch("salt.modules.opkg.latest_version", MagicMock(return_value="")):
  66. self.assertFalse(opkg.upgrade_available("vim"))
  67. def test_file_dict(self):
  68. """
  69. Test - List the files that belong to a package, grouped by package.
  70. """
  71. std_out = "\n".join(OPKG_VIM_FILES["packages"]["vim"])
  72. ret_value = {"stdout": std_out}
  73. mock = MagicMock(return_value=ret_value)
  74. with patch.dict(opkg.__salt__, {"cmd.run_all": mock}):
  75. self.assertEqual(opkg.file_dict("vim"), OPKG_VIM_FILES)
  76. def test_file_list(self):
  77. """
  78. Test - List the files that belong to a package.
  79. """
  80. std_out = "\n".join(OPKG_VIM_FILES["packages"]["vim"])
  81. ret_value = {"stdout": std_out}
  82. mock = MagicMock(return_value=ret_value)
  83. files = {
  84. "errors": OPKG_VIM_FILES["errors"],
  85. "files": OPKG_VIM_FILES["packages"]["vim"],
  86. }
  87. with patch.dict(opkg.__salt__, {"cmd.run_all": mock}):
  88. self.assertEqual(opkg.file_list("vim"), files)
  89. def test_owner(self):
  90. """
  91. Test - Return the name of the package that owns the file.
  92. """
  93. paths = ["/usr/bin/vimdiff"]
  94. mock = MagicMock(return_value="vim - version - info")
  95. with patch.dict(opkg.__salt__, {"cmd.run_stdout": mock}):
  96. self.assertEqual(opkg.owner(*paths), "vim")
  97. def test_install(self):
  98. """
  99. Test - Install packages.
  100. """
  101. with patch(
  102. "salt.modules.opkg.list_pkgs", MagicMock(side_effect=[{}, PACKAGES])
  103. ):
  104. ret_value = {"retcode": 0}
  105. mock = MagicMock(return_value=ret_value)
  106. patch_kwargs = {
  107. "__salt__": {
  108. "cmd.run_all": mock,
  109. "pkg_resource.parse_targets": MagicMock(
  110. return_value=({"vim": "7.4"}, "repository")
  111. ),
  112. "restartcheck.restartcheck": MagicMock(
  113. return_value="No packages seem to need to be restarted"
  114. ),
  115. }
  116. }
  117. with patch.multiple(opkg, **patch_kwargs):
  118. self.assertEqual(opkg.install("vim:7.4"), INSTALLED)
  119. def test_install_noaction(self):
  120. """
  121. Test - Install packages.
  122. """
  123. with patch("salt.modules.opkg.list_pkgs", MagicMock(return_value=({}))):
  124. ret_value = {"retcode": 0}
  125. mock = MagicMock(return_value=ret_value)
  126. patch_kwargs = {
  127. "__salt__": {
  128. "cmd.run_all": mock,
  129. "pkg_resource.parse_targets": MagicMock(
  130. return_value=({"vim": "7.4"}, "repository")
  131. ),
  132. "restartcheck.restartcheck": MagicMock(
  133. return_value="No packages seem to need to be restarted"
  134. ),
  135. }
  136. }
  137. with patch.multiple(opkg, **patch_kwargs):
  138. self.assertEqual(opkg.install("vim:7.4", test=True), {})
  139. def test_remove(self):
  140. """
  141. Test - Remove packages.
  142. """
  143. with patch(
  144. "salt.modules.opkg.list_pkgs", MagicMock(side_effect=[PACKAGES, {}])
  145. ):
  146. ret_value = {"retcode": 0}
  147. mock = MagicMock(return_value=ret_value)
  148. patch_kwargs = {
  149. "__salt__": {
  150. "cmd.run_all": mock,
  151. "pkg_resource.parse_targets": MagicMock(
  152. return_value=({"vim": "7.4"}, "repository")
  153. ),
  154. "restartcheck.restartcheck": MagicMock(
  155. return_value="No packages seem to need to be restarted"
  156. ),
  157. }
  158. }
  159. with patch.multiple(opkg, **patch_kwargs):
  160. self.assertEqual(opkg.remove("vim"), REMOVED)
  161. def test_remove_noaction(self):
  162. """
  163. Test - Remove packages.
  164. """
  165. with patch("salt.modules.opkg.list_pkgs", MagicMock(return_value=({}))):
  166. ret_value = {"retcode": 0}
  167. mock = MagicMock(return_value=ret_value)
  168. patch_kwargs = {
  169. "__salt__": {
  170. "cmd.run_all": mock,
  171. "pkg_resource.parse_targets": MagicMock(
  172. return_value=({"vim": "7.4"}, "repository")
  173. ),
  174. "restartcheck.restartcheck": MagicMock(
  175. return_value="No packages seem to need to be restarted"
  176. ),
  177. }
  178. }
  179. with patch.multiple(opkg, **patch_kwargs):
  180. self.assertEqual(opkg.remove("vim:7.4", test=True), {})
  181. def test_info_installed(self):
  182. """
  183. Test - Return the information of the named package(s) installed on the system.
  184. """
  185. installed = copy.deepcopy(OPKG_VIM_INFO["vim"])
  186. del installed["Package"]
  187. ordered_info = collections.OrderedDict(sorted(installed.items()))
  188. expected_dict = {"vim": {k.lower(): v for k, v in ordered_info.items()}}
  189. std_out = "\n".join([k + ": " + v for k, v in OPKG_VIM_INFO["vim"].items()])
  190. ret_value = {"stdout": std_out, "retcode": 0}
  191. mock = MagicMock(return_value=ret_value)
  192. with patch.dict(opkg.__salt__, {"cmd.run_all": mock}):
  193. self.assertEqual(opkg.info_installed("vim"), expected_dict)
  194. def test_version_clean(self):
  195. """
  196. Test - Return the information of version_clean
  197. """
  198. self.assertEqual(opkg.version_clean("1.0.1"), "1.0.1")
  199. def test_check_extra_requirements(self):
  200. """
  201. Test - Return the information of check_extra_requirements
  202. """
  203. self.assertEqual(opkg.check_extra_requirements("vim", "1.0.1"), True)