test_mac_brew_pkg.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Nicole Thomas <nicole@saltstack.com>
  4. '''
  5. # Import Python Libs
  6. from __future__ import absolute_import, unicode_literals, print_function
  7. # Import Salt Testing Libs
  8. from tests.support.case import ModuleCase
  9. from tests.support.unit import skipIf
  10. # Import Salt Libs
  11. import salt.utils.path
  12. import salt.utils.platform
  13. from salt.exceptions import CommandExecutionError
  14. # Import 3rd-party libs
  15. import pytest
  16. from salt.ext import six
  17. # Brew doesn't support local package installation - So, let's
  18. # Grab some small packages available online for brew
  19. ADD_PKG = 'algol68g'
  20. DEL_PKG = 'acme'
  21. @pytest.mark.destructive_test
  22. @pytest.mark.skip_if_not_root
  23. @skipIf(not salt.utils.platform.is_darwin(), 'Test only applies to macOS')
  24. @skipIf(not salt.utils.path.which('brew'), 'This test requires the brew binary')
  25. class BrewModuleTest(ModuleCase):
  26. '''
  27. Integration tests for the brew module
  28. '''
  29. def test_brew_install(self):
  30. '''
  31. Tests the installation of packages
  32. '''
  33. try:
  34. self.run_function('pkg.install', [ADD_PKG])
  35. pkg_list = self.run_function('pkg.list_pkgs')
  36. try:
  37. self.assertIn(ADD_PKG, pkg_list)
  38. except AssertionError:
  39. self.run_function('pkg.remove', [ADD_PKG])
  40. raise
  41. except CommandExecutionError:
  42. self.run_function('pkg.remove', [ADD_PKG])
  43. raise
  44. def test_remove(self):
  45. '''
  46. Tests the removal of packages
  47. '''
  48. try:
  49. # Install a package to delete - If unsuccessful, skip the test
  50. self.run_function('pkg.install', [DEL_PKG])
  51. pkg_list = self.run_function('pkg.list_pkgs')
  52. if DEL_PKG not in pkg_list:
  53. self.run_function('pkg.install', [DEL_PKG])
  54. self.skipTest('Failed to install a package to delete')
  55. # Now remove the installed package
  56. self.run_function('pkg.remove', [DEL_PKG])
  57. del_list = self.run_function('pkg.list_pkgs')
  58. try:
  59. self.assertNotIn(DEL_PKG, del_list)
  60. except AssertionError:
  61. raise
  62. except CommandExecutionError:
  63. self.run_function('pkg.remove', [DEL_PKG])
  64. raise
  65. def test_version(self):
  66. '''
  67. Test pkg.version for mac. Installs a package and then checks we can get
  68. a version for the installed package.
  69. '''
  70. try:
  71. self.run_function('pkg.install', [ADD_PKG])
  72. pkg_list = self.run_function('pkg.list_pkgs')
  73. version = self.run_function('pkg.version', [ADD_PKG])
  74. try:
  75. self.assertTrue(version,
  76. msg=('version: {0} is empty,\
  77. or other issue is present'.format(version)))
  78. self.assertIn(ADD_PKG, pkg_list,
  79. msg=('package: {0} is not in\
  80. the list of installed packages: {1}'
  81. .format(ADD_PKG, pkg_list)))
  82. #make sure the version is accurate and is listed in the pkg_list
  83. self.assertIn(version, six.text_type(pkg_list[ADD_PKG]),
  84. msg=('The {0} version: {1} is \
  85. not listed in the pkg_list: {2}'
  86. .format(ADD_PKG, version, pkg_list[ADD_PKG])))
  87. except AssertionError:
  88. self.run_function('pkg.remove', [ADD_PKG])
  89. raise
  90. except CommandExecutionError:
  91. self.run_function('pkg.remove', [ADD_PKG])
  92. raise
  93. def test_latest_version(self):
  94. '''
  95. Test pkg.latest_version:
  96. - get the latest version available
  97. - install the package
  98. - get the latest version available
  99. - check that the latest version is empty after installing it
  100. '''
  101. try:
  102. self.run_function('pkg.remove', [ADD_PKG])
  103. uninstalled_latest = self.run_function('pkg.latest_version', [ADD_PKG])
  104. self.run_function('pkg.install', [ADD_PKG])
  105. installed_latest = self.run_function('pkg.latest_version', [ADD_PKG])
  106. version = self.run_function('pkg.version', [ADD_PKG])
  107. try:
  108. self.assertTrue(isinstance(uninstalled_latest, six.string_types))
  109. self.assertEqual(installed_latest, version)
  110. except AssertionError:
  111. self.run_function('pkg.remove', [ADD_PKG])
  112. raise
  113. except CommandExecutionError:
  114. self.run_function('pkg.remove', [ADD_PKG])
  115. raise
  116. def test_refresh_db(self):
  117. '''
  118. Integration test to ensure pkg.refresh_db works with brew
  119. '''
  120. refresh_brew = self.run_function('pkg.refresh_db')
  121. self.assertTrue(refresh_brew)
  122. def test_list_upgrades(self):
  123. '''
  124. Test pkg.list_upgrades: data is in the form {'name1': 'version1',
  125. 'name2': 'version2', ... }
  126. '''
  127. try:
  128. upgrades = self.run_function('pkg.list_upgrades')
  129. try:
  130. self.assertTrue(isinstance(upgrades, dict))
  131. if len(upgrades):
  132. for name in upgrades:
  133. self.assertTrue(isinstance(name, six.string_types))
  134. self.assertTrue(isinstance(upgrades[name], six.string_types))
  135. except AssertionError:
  136. self.run_function('pkg.remove', [ADD_PKG])
  137. raise
  138. except CommandExecutionError:
  139. self.run_function('pkg.remove', [ADD_PKG])
  140. raise
  141. def test_info_installed(self):
  142. '''
  143. Test pkg.info_installed: info returned has certain fields used by
  144. mac_brew.latest_version
  145. '''
  146. try:
  147. self.run_function('pkg.install', [ADD_PKG])
  148. info = self.run_function('pkg.info_installed', [ADD_PKG])
  149. try:
  150. self.assertTrue(ADD_PKG in info)
  151. self.assertTrue('versions' in info[ADD_PKG])
  152. self.assertTrue('revision' in info[ADD_PKG])
  153. self.assertTrue('stable' in info[ADD_PKG]['versions'])
  154. except AssertionError:
  155. self.run_function('pkg.remove', [ADD_PKG])
  156. raise
  157. except CommandExecutionError:
  158. self.run_function('pkg.remove', [ADD_PKG])
  159. raise
  160. def tearDown(self):
  161. '''
  162. Clean up after tests
  163. '''
  164. pkg_list = self.run_function('pkg.list_pkgs')
  165. # Remove any installed packages
  166. if ADD_PKG in pkg_list:
  167. self.run_function('pkg.remove', [ADD_PKG])
  168. if DEL_PKG in pkg_list:
  169. self.run_function('pkg.remove', [DEL_PKG])