test_macpackage.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. # Import Salt Libs
  5. import salt.modules.macpackage as macpackage
  6. # Import Salt Testing Libs
  7. from tests.support.mixins import LoaderModuleMockMixin
  8. from tests.support.mock import MagicMock, call, patch
  9. from tests.support.unit import TestCase
  10. class MacPackageTestCase(TestCase, LoaderModuleMockMixin):
  11. def setup_loader_modules(self):
  12. return {macpackage: {}}
  13. def test_install(self):
  14. """
  15. Test installing a PKG file
  16. """
  17. mock = MagicMock()
  18. with patch.dict(macpackage.__salt__, {"cmd.run_all": mock}):
  19. macpackage.install("/path/to/file.pkg")
  20. mock.assert_called_once_with(
  21. "installer -pkg /path/to/file.pkg -target LocalSystem",
  22. python_shell=False,
  23. )
  24. def test_install_wildcard(self):
  25. """
  26. Test installing a PKG file with a wildcard
  27. """
  28. mock = MagicMock()
  29. with patch.dict(macpackage.__salt__, {"cmd.run_all": mock}):
  30. macpackage.install("/path/to/*.pkg")
  31. mock.assert_called_once_with(
  32. "installer -pkg /path/to/*.pkg -target LocalSystem", python_shell=True
  33. )
  34. def test_install_with_extras(self):
  35. """
  36. Test installing a PKG file with extra options
  37. """
  38. mock = MagicMock()
  39. with patch.dict(macpackage.__salt__, {"cmd.run_all": mock}):
  40. macpackage.install("/path/to/file.pkg", store=True, allow_untrusted=True)
  41. mock.assert_called_once_with(
  42. "installer -pkg /path/to/file.pkg -target LocalSystem -store -allowUntrusted",
  43. python_shell=False,
  44. )
  45. def test_install_app(self):
  46. """
  47. Test installing an APP package
  48. """
  49. mock = MagicMock()
  50. with patch.dict(macpackage.__salt__, {"cmd.run": mock}):
  51. macpackage.install_app("/path/to/file.app")
  52. mock.assert_called_once_with(
  53. 'rsync -a --delete "/path/to/file.app/" ' '"/Applications/file.app"'
  54. )
  55. def test_install_app_specify_target(self):
  56. """
  57. Test installing an APP package with a specific target
  58. """
  59. mock = MagicMock()
  60. with patch.dict(macpackage.__salt__, {"cmd.run": mock}):
  61. macpackage.install_app("/path/to/file.app", "/Applications/new.app")
  62. mock.assert_called_once_with(
  63. 'rsync -a --delete "/path/to/file.app/" ' '"/Applications/new.app"'
  64. )
  65. def test_install_app_with_slash(self):
  66. """
  67. Test installing an APP package with a specific target
  68. """
  69. mock = MagicMock()
  70. with patch.dict(macpackage.__salt__, {"cmd.run": mock}):
  71. macpackage.install_app("/path/to/file.app/")
  72. mock.assert_called_once_with(
  73. 'rsync -a --delete "/path/to/file.app/" ' '"/Applications/file.app"'
  74. )
  75. def test_uninstall(self):
  76. """
  77. Test Uninstalling an APP package with a specific target
  78. """
  79. mock = MagicMock()
  80. with patch.dict(macpackage.__salt__, {"file.remove": mock}):
  81. macpackage.uninstall_app("/path/to/file.app")
  82. mock.assert_called_once_with("/path/to/file.app")
  83. def test_mount(self):
  84. """
  85. Test mounting an dmg file to a temporary location
  86. """
  87. cmd_mock = MagicMock()
  88. temp_mock = MagicMock(return_value="dmg-ABCDEF")
  89. with patch.dict(
  90. macpackage.__salt__, {"cmd.run": cmd_mock, "temp.dir": temp_mock}
  91. ):
  92. macpackage.mount("/path/to/file.dmg")
  93. temp_mock.assert_called_once_with(prefix="dmg-")
  94. cmd_mock.assert_called_once_with(
  95. "hdiutil attach -readonly -nobrowse -mountpoint "
  96. 'dmg-ABCDEF "/path/to/file.dmg"'
  97. )
  98. def test_unmount(self):
  99. """
  100. Test Unmounting an dmg file to a temporary location
  101. """
  102. mock = MagicMock()
  103. with patch.dict(macpackage.__salt__, {"cmd.run": mock}):
  104. macpackage.unmount("/path/to/file.dmg")
  105. mock.assert_called_once_with('hdiutil detach "/path/to/file.dmg"')
  106. def test_installed_pkgs(self):
  107. """
  108. Test getting a list of the installed packages
  109. """
  110. expected = ["com.apple.this", "com.salt.that"]
  111. mock = MagicMock(return_value="com.apple.this\ncom.salt.that")
  112. with patch.dict(macpackage.__salt__, {"cmd.run": mock}):
  113. out = macpackage.installed_pkgs()
  114. mock.assert_called_once_with("pkgutil --pkgs")
  115. self.assertEqual(out, expected)
  116. def test_get_pkg_id_with_files(self):
  117. """
  118. Test getting a the id for a package
  119. """
  120. with patch(
  121. "salt.modules.macpackage._get_pkg_id_from_pkginfo"
  122. ) as pkg_id_pkginfo_mock:
  123. expected = ["com.apple.this"]
  124. cmd_mock = MagicMock(
  125. side_effect=[
  126. "/path/to/PackageInfo\n/path/to/some/other/fake/PackageInfo",
  127. "",
  128. "",
  129. ]
  130. )
  131. pkg_id_pkginfo_mock.side_effect = [["com.apple.this"], []]
  132. temp_mock = MagicMock(return_value="/tmp/dmg-ABCDEF")
  133. remove_mock = MagicMock()
  134. with patch.dict(
  135. macpackage.__salt__,
  136. {
  137. "cmd.run": cmd_mock,
  138. "temp.dir": temp_mock,
  139. "file.remove": remove_mock,
  140. },
  141. ):
  142. out = macpackage.get_pkg_id("/path/to/file.pkg")
  143. temp_mock.assert_called_once_with(prefix="pkg-")
  144. cmd_calls = [
  145. call(
  146. "xar -t -f /path/to/file.pkg | grep PackageInfo",
  147. python_shell=True,
  148. output_loglevel="quiet",
  149. ),
  150. call(
  151. "xar -x -f /path/to/file.pkg /path/to/PackageInfo /path/to/some/other/fake/PackageInfo",
  152. cwd="/tmp/dmg-ABCDEF",
  153. output_loglevel="quiet",
  154. ),
  155. ]
  156. cmd_mock.assert_has_calls(cmd_calls)
  157. pkg_id_pkginfo_calls = [
  158. call("/path/to/PackageInfo"),
  159. call("/path/to/some/other/fake/PackageInfo"),
  160. ]
  161. pkg_id_pkginfo_mock.assert_has_calls(pkg_id_pkginfo_calls)
  162. remove_mock.assert_called_once_with("/tmp/dmg-ABCDEF")
  163. self.assertEqual(out, expected)
  164. def test_get_pkg_id_with_dir(self):
  165. """
  166. Test getting a the id for a package with a directory
  167. """
  168. with patch("salt.modules.macpackage._get_pkg_id_dir") as pkg_id_dir_mock:
  169. expected = ["com.apple.this"]
  170. pkg_id_dir_mock.return_value = ["com.apple.this"]
  171. cmd_mock = MagicMock(return_value="Error opening /path/to/file.pkg")
  172. temp_mock = MagicMock(return_value="/tmp/dmg-ABCDEF")
  173. remove_mock = MagicMock()
  174. with patch.dict(
  175. macpackage.__salt__,
  176. {
  177. "cmd.run": cmd_mock,
  178. "temp.dir": temp_mock,
  179. "file.remove": remove_mock,
  180. },
  181. ):
  182. out = macpackage.get_pkg_id("/path/to/file.pkg")
  183. temp_mock.assert_called_once_with(prefix="pkg-")
  184. cmd_mock.assert_called_once_with(
  185. "xar -t -f /path/to/file.pkg | grep PackageInfo",
  186. python_shell=True,
  187. output_loglevel="quiet",
  188. )
  189. pkg_id_dir_mock.assert_called_once_with("/path/to/file.pkg")
  190. remove_mock.assert_called_once_with("/tmp/dmg-ABCDEF")
  191. self.assertEqual(out, expected)
  192. def test_get_mpkg_ids(self):
  193. """
  194. Test getting the ids of a mpkg file
  195. """
  196. with patch("salt.modules.macpackage.get_pkg_id") as get_pkg_id_mock:
  197. expected = ["com.apple.this", "com.salt.other"]
  198. mock = MagicMock(return_value="/tmp/dmg-X/file.pkg\n/tmp/dmg-X/other.pkg")
  199. get_pkg_id_mock.side_effect = [["com.apple.this"], ["com.salt.other"]]
  200. with patch.dict(macpackage.__salt__, {"cmd.run": mock}):
  201. out = macpackage.get_mpkg_ids("/path/to/file.mpkg")
  202. mock.assert_called_once_with(
  203. "find /path/to -name *.pkg", python_shell=True
  204. )
  205. calls = [call("/tmp/dmg-X/file.pkg"), call("/tmp/dmg-X/other.pkg")]
  206. get_pkg_id_mock.assert_has_calls(calls)
  207. self.assertEqual(out, expected)
  208. def test_get_pkg_id_from_pkginfo(self):
  209. """
  210. Test getting a package id from pkginfo files
  211. """
  212. expected = ["com.apple.this", "com.apple.that"]
  213. mock = MagicMock(return_value="com.apple.this\ncom.apple.that")
  214. with patch.dict(macpackage.__salt__, {"cmd.run": mock}):
  215. out = macpackage._get_pkg_id_from_pkginfo("/tmp/dmg-X/PackageInfo")
  216. cmd = (
  217. "cat /tmp/dmg-X/PackageInfo | grep -Eo 'identifier=\"[a-zA-Z.0-9\\-]*\"' | "
  218. "cut -c 13- | tr -d '\"'"
  219. )
  220. mock.assert_called_once_with(cmd, python_shell=True)
  221. self.assertEqual(out, expected)
  222. def test_get_pkg_id_from_pkginfo_no_file(self):
  223. """
  224. Test getting a package id from pkginfo file when it doesn't exist
  225. """
  226. expected = []
  227. mock = MagicMock(return_value="No such file")
  228. with patch.dict(macpackage.__salt__, {"cmd.run": mock}):
  229. out = macpackage._get_pkg_id_from_pkginfo("/tmp/dmg-X/PackageInfo")
  230. cmd = (
  231. "cat /tmp/dmg-X/PackageInfo | grep -Eo 'identifier=\"[a-zA-Z.0-9\\-]*\"' | "
  232. "cut -c 13- | tr -d '\"'"
  233. )
  234. mock.assert_called_once_with(cmd, python_shell=True)
  235. self.assertEqual(out, expected)
  236. def test_get_pkg_id_dir(self):
  237. """
  238. Test getting a package id from a directory
  239. """
  240. expected = ["com.apple.this"]
  241. mock = MagicMock(return_value="com.apple.this")
  242. with patch.dict(macpackage.__salt__, {"cmd.run": mock}):
  243. out = macpackage._get_pkg_id_dir("/tmp/dmg-X/")
  244. cmd = '/usr/libexec/PlistBuddy -c "print :CFBundleIdentifier" /tmp/dmg-X/Contents/Info.plist'
  245. mock.assert_called_once_with(cmd, python_shell=False)
  246. self.assertEqual(out, expected)
  247. def test_get_pkg_id_dir_wildcard(self):
  248. """
  249. Test getting a package id from a directory with a wildcard
  250. """
  251. expected = ["com.apple.this"]
  252. mock = MagicMock(return_value="com.apple.this")
  253. with patch.dict(macpackage.__salt__, {"cmd.run": mock}):
  254. out = macpackage._get_pkg_id_dir("/tmp/dmg-X/*.pkg/")
  255. cmd = "/usr/libexec/PlistBuddy -c \"print :CFBundleIdentifier\" '/tmp/dmg-X/*.pkg/Contents/Info.plist'"
  256. mock.assert_called_once_with(cmd, python_shell=True)
  257. self.assertEqual(out, expected)