test_mac_brew_pkg.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Nicole Thomas <nicole@saltstack.com>
  4. """
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Libs
  8. import salt.modules.mac_brew_pkg as mac_brew
  9. import salt.utils.pkg
  10. from salt.exceptions import CommandExecutionError
  11. # Import Salt Testing Libs
  12. from tests.support.mixins import LoaderModuleMockMixin
  13. from tests.support.mock import MagicMock, Mock, patch
  14. from tests.support.unit import TestCase
  15. TAPS_STRING = "homebrew/dupes\nhomebrew/science\nhomebrew/x11"
  16. TAPS_LIST = ["homebrew/dupes", "homebrew/science", "homebrew/x11"]
  17. HOMEBREW_BIN = "/usr/local/bin/brew"
  18. class BrewTestCase(TestCase, LoaderModuleMockMixin):
  19. """
  20. TestCase for salt.modules.mac_brew module
  21. """
  22. def setup_loader_modules(self):
  23. return {mac_brew: {"__opts__": {"user": MagicMock(return_value="bar")}}}
  24. # '_list_taps' function tests: 1
  25. def test_list_taps(self):
  26. """
  27. Tests the return of the list of taps
  28. """
  29. mock_taps = MagicMock(return_value={"stdout": TAPS_STRING, "retcode": 0})
  30. mock_user = MagicMock(return_value="foo")
  31. mock_cmd = MagicMock(return_value="")
  32. with patch.dict(
  33. mac_brew.__salt__,
  34. {"file.get_user": mock_user, "cmd.run_all": mock_taps, "cmd.run": mock_cmd},
  35. ):
  36. self.assertEqual(mac_brew._list_taps(), TAPS_LIST)
  37. # '_tap' function tests: 3
  38. def test_tap_installed(self):
  39. """
  40. Tests if tap argument is already installed or not
  41. """
  42. with patch(
  43. "salt.modules.mac_brew_pkg._list_taps", MagicMock(return_value=TAPS_LIST)
  44. ):
  45. self.assertTrue(mac_brew._tap("homebrew/science"))
  46. def test_tap_failure(self):
  47. """
  48. Tests if the tap installation failed
  49. """
  50. mock_failure = MagicMock(
  51. return_value={"stdout": "", "stderr": "", "retcode": 1}
  52. )
  53. mock_user = MagicMock(return_value="foo")
  54. mock_cmd = MagicMock(return_value="")
  55. with patch.dict(
  56. mac_brew.__salt__,
  57. {
  58. "cmd.run_all": mock_failure,
  59. "file.get_user": mock_user,
  60. "cmd.run": mock_cmd,
  61. },
  62. ), patch("salt.modules.mac_brew_pkg._list_taps", MagicMock(return_value={})):
  63. self.assertFalse(mac_brew._tap("homebrew/test"))
  64. def test_tap(self):
  65. """
  66. Tests adding unofficial GitHub repos to the list of brew taps
  67. """
  68. mock_failure = MagicMock(return_value={"retcode": 0})
  69. mock_user = MagicMock(return_value="foo")
  70. mock_cmd = MagicMock(return_value="")
  71. with patch.dict(
  72. mac_brew.__salt__,
  73. {
  74. "cmd.run_all": mock_failure,
  75. "file.get_user": mock_user,
  76. "cmd.run": mock_cmd,
  77. },
  78. ), patch(
  79. "salt.modules.mac_brew_pkg._list_taps", MagicMock(return_value=TAPS_LIST)
  80. ):
  81. self.assertTrue(mac_brew._tap("homebrew/test"))
  82. # '_homebrew_bin' function tests: 1
  83. def test_homebrew_bin(self):
  84. """
  85. Tests the path to the homebrew binary
  86. """
  87. mock_path = MagicMock(return_value="/usr/local")
  88. with patch.dict(mac_brew.__salt__, {"cmd.run": mock_path}):
  89. self.assertEqual(mac_brew._homebrew_bin(), "/usr/local/bin/brew")
  90. # 'list_pkgs' function tests: 2
  91. # Only tested a few basics
  92. # Full functionality should be tested in integration phase
  93. def test_list_pkgs_removed(self):
  94. """
  95. Tests removed implementation
  96. """
  97. self.assertEqual(mac_brew.list_pkgs(removed=True), {})
  98. def test_list_pkgs_versions_true(self):
  99. """
  100. Tests if pkg.list_pkgs is already in context and is a list
  101. """
  102. mock_context = {"foo": ["bar"]}
  103. with patch.dict(mac_brew.__context__, {"pkg.list_pkgs": mock_context}):
  104. self.assertEqual(mac_brew.list_pkgs(versions_as_list=True), mock_context)
  105. def test_list_pkgs_homebrew_cask_pakages(self):
  106. """
  107. Tests if pkg.list_pkgs list properly homebrew cask packages
  108. """
  109. def custom_call_brew(cmd, failhard=True):
  110. result = dict()
  111. if cmd == "info --json=v1 --installed":
  112. result = {
  113. "stdout": '[{"name":"zsh","full_name":"zsh","oldname":null,'
  114. '"aliases":[],"homepage":"https://www.zsh.org/",'
  115. '"versions":{"stable":"5.7.1","devel":null,"head":"HEAD","bottle":true},'
  116. '"installed":[{"version":"5.7.1","used_options":[],'
  117. '"built_as_bottle":true,"poured_from_bottle":true,'
  118. '"runtime_dependencies":[{"full_name":"ncurses","version":"6.1"},'
  119. '{"full_name":"pcre","version":"8.42"}],'
  120. '"installed_as_dependency":false,"installed_on_request":true}]}]',
  121. "stderr": "",
  122. "retcode": 0,
  123. }
  124. elif cmd == "cask list --versions":
  125. result = {
  126. "stdout": "macvim 8.1.151\nfont-firacode-nerd-font 2.0.0",
  127. "stderr": "",
  128. "retcode": 0,
  129. }
  130. elif cmd == "cask info macvim":
  131. result = {
  132. "stdout": "macvim: 8.1.1517,156 (auto_updates)\n"
  133. "https://github.com/macvim-dev/macvim\n"
  134. "/usr/local/Caskroom/macvim/8.1.151 (64B)\n"
  135. "From: https://github.com/Homebrew/homebrew-cask/blob/master/Casks/macvim.rb\n"
  136. "==> Name\n"
  137. "MacVim",
  138. "stderr": "",
  139. "retcode": 0,
  140. }
  141. elif cmd == "cask info font-firacode-nerd-font":
  142. result = {
  143. "stdout": "font-firacode-nerd-font: 2.0.0\n"
  144. "https://github.com/ryanoasis/nerd-fonts\n"
  145. "/usr/local/Caskroom/font-firacode-nerd-font/2.0.0 (35 files, 64.8MB)\n"
  146. "From: https://github.com/Homebrew/homebrew-cask-fonts/blob/master/Casks/font-firacode-nerd-font.rb\n"
  147. "==> Name\n"
  148. "FuraCode Nerd Font (FiraCode)",
  149. "stderr": "",
  150. "retcode": "",
  151. }
  152. return result
  153. def custom_add_pkg(ret, name, newest_version):
  154. ret[name] = newest_version
  155. return ret
  156. expected_pkgs = {
  157. "zsh": "5.7.1",
  158. "homebrew/cask/macvim": "8.1.151",
  159. "homebrew/cask-fonts/font-firacode-nerd-font": "2.0.0",
  160. }
  161. with patch(
  162. "salt.modules.mac_brew_pkg._call_brew", custom_call_brew
  163. ), patch.dict(
  164. mac_brew.__salt__,
  165. {
  166. "pkg_resource.add_pkg": custom_add_pkg,
  167. "pkg_resource.sort_pkglist": MagicMock(),
  168. },
  169. ):
  170. self.assertEqual(mac_brew.list_pkgs(versions_as_list=True), expected_pkgs)
  171. # 'version' function tests: 1
  172. def test_version(self):
  173. """
  174. Tests version name returned
  175. """
  176. mock_version = MagicMock(return_value="0.1.5")
  177. with patch.dict(mac_brew.__salt__, {"pkg_resource.version": mock_version}):
  178. self.assertEqual(mac_brew.version("foo"), "0.1.5")
  179. # 'latest_version' function tests: 0
  180. # It has not been fully implemented
  181. # 'remove' function tests: 1
  182. # Only tested a few basics
  183. # Full functionality should be tested in integration phase
  184. def test_remove(self):
  185. """
  186. Tests if package to be removed exists
  187. """
  188. mock_params = MagicMock(return_value=({"foo": None}, "repository"))
  189. with patch(
  190. "salt.modules.mac_brew_pkg.list_pkgs", return_value={"test": "0.1.5"}
  191. ), patch.dict(mac_brew.__salt__, {"pkg_resource.parse_targets": mock_params}):
  192. self.assertEqual(mac_brew.remove("foo"), {})
  193. # 'refresh_db' function tests: 2
  194. def test_refresh_db_failure(self):
  195. """
  196. Tests an update of homebrew package repository failure
  197. """
  198. mock_user = MagicMock(return_value="foo")
  199. mock_failure = MagicMock(
  200. return_value={"stdout": "", "stderr": "", "retcode": 1}
  201. )
  202. with patch.dict(
  203. mac_brew.__salt__, {"file.get_user": mock_user, "cmd.run_all": mock_failure}
  204. ), patch(
  205. "salt.modules.mac_brew_pkg._homebrew_bin",
  206. MagicMock(return_value=HOMEBREW_BIN),
  207. ):
  208. with patch.object(salt.utils.pkg, "clear_rtag", Mock()):
  209. self.assertRaises(CommandExecutionError, mac_brew.refresh_db)
  210. def test_refresh_db(self):
  211. """
  212. Tests a successful update of homebrew package repository
  213. """
  214. mock_user = MagicMock(return_value="foo")
  215. mock_success = MagicMock(return_value={"retcode": 0})
  216. with patch.dict(
  217. mac_brew.__salt__, {"file.get_user": mock_user, "cmd.run_all": mock_success}
  218. ), patch(
  219. "salt.modules.mac_brew_pkg._homebrew_bin",
  220. MagicMock(return_value=HOMEBREW_BIN),
  221. ):
  222. with patch.object(salt.utils.pkg, "clear_rtag", Mock()):
  223. self.assertTrue(mac_brew.refresh_db())
  224. # 'install' function tests: 1
  225. # Only tested a few basics
  226. # Full functionality should be tested in integration phase
  227. def test_install(self):
  228. """
  229. Tests if package to be installed exists
  230. """
  231. mock_params = MagicMock(return_value=[None, None])
  232. with patch.dict(mac_brew.__salt__, {"pkg_resource.parse_targets": mock_params}):
  233. self.assertEqual(mac_brew.install("name=foo"), {})