test_pkg.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. import os
  5. # Import Salt Testing libs
  6. from tests.support.case import ModuleCase
  7. from tests.support.mixins import SaltReturnAssertsMixin
  8. from tests.support.helpers import (
  9. destructiveTest,
  10. flaky,
  11. requires_network,
  12. requires_salt_modules,
  13. )
  14. from tests.support.unit import skipIf
  15. # Import Salt libs
  16. import salt.utils.pkg
  17. import salt.utils.platform
  18. @flaky
  19. class PkgModuleTest(ModuleCase, SaltReturnAssertsMixin):
  20. '''
  21. Validate the pkg module
  22. '''
  23. def setUp(self):
  24. if salt.utils.platform.is_windows():
  25. self.run_function('pkg.refresh_db')
  26. os_release = self.run_function('grains.get', ['osrelease'])
  27. if salt.utils.platform.is_darwin() and int(os_release.split('.')[1]) >= 13:
  28. self.pkg = 'wget'
  29. elif salt.utils.platform.is_windows():
  30. self.pkg = 'putty'
  31. else:
  32. self.pkg = 'htop'
  33. def test_list(self):
  34. '''
  35. verify that packages are installed
  36. '''
  37. ret = self.run_function('pkg.list_pkgs')
  38. self.assertNotEqual(len(ret.keys()), 0)
  39. @requires_salt_modules('pkg.version_cmp')
  40. def test_version_cmp(self):
  41. '''
  42. test package version comparison on supported platforms
  43. '''
  44. func = 'pkg.version_cmp'
  45. os_family = self.run_function('grains.item', ['os_family'])['os_family']
  46. if os_family == 'Debian':
  47. lt = ['0.2.4-0ubuntu1', '0.2.4.1-0ubuntu1']
  48. eq = ['0.2.4-0ubuntu1', '0.2.4-0ubuntu1']
  49. gt = ['0.2.4.1-0ubuntu1', '0.2.4-0ubuntu1']
  50. self.assertEqual(self.run_function(func, lt), -1)
  51. self.assertEqual(self.run_function(func, eq), 0)
  52. self.assertEqual(self.run_function(func, gt), 1)
  53. elif os_family == 'Suse':
  54. lt = ['2.3.0-1', '2.3.1-15.1']
  55. eq = ['2.3.1-15.1', '2.3.1-15.1']
  56. gt = ['2.3.2-15.1', '2.3.1-15.1']
  57. self.assertEqual(self.run_function(func, lt), -1)
  58. self.assertEqual(self.run_function(func, eq), 0)
  59. self.assertEqual(self.run_function(func, gt), 1)
  60. else:
  61. self.skipTest('{0} is unavailable on {1}'.format(func, os_family))
  62. @requires_salt_modules('pkg.mod_repo', 'pkg.del_repo')
  63. @requires_network()
  64. @destructiveTest
  65. def test_mod_del_repo(self):
  66. '''
  67. test modifying and deleting a software repository
  68. '''
  69. os_grain = self.run_function('grains.item', ['os'])['os']
  70. repo = None
  71. try:
  72. if os_grain == 'Ubuntu':
  73. repo = 'ppa:otto-kesselgulasch/gimp-edge'
  74. uri = 'http://ppa.launchpad.net/otto-kesselgulasch/gimp-edge/ubuntu'
  75. ret = self.run_function('pkg.mod_repo', [repo, 'comps=main'])
  76. self.assertNotEqual(ret, {})
  77. ret = self.run_function('pkg.get_repo', [repo])
  78. self.assertEqual(ret['uri'], uri)
  79. elif os_grain == 'CentOS':
  80. major_release = int(
  81. self.run_function(
  82. 'grains.item',
  83. ['osmajorrelease']
  84. )['osmajorrelease']
  85. )
  86. repo = 'saltstack'
  87. name = 'SaltStack repo for RHEL/CentOS {0}'.format(major_release)
  88. baseurl = 'http://repo.saltstack.com/yum/redhat/{0}/x86_64/latest/'.format(major_release)
  89. gpgkey = 'https://repo.saltstack.com/yum/rhel{0}/SALTSTACK-GPG-KEY.pub'.format(major_release)
  90. gpgcheck = 1
  91. enabled = 1
  92. ret = self.run_function(
  93. 'pkg.mod_repo',
  94. [repo],
  95. name=name,
  96. baseurl=baseurl,
  97. gpgkey=gpgkey,
  98. gpgcheck=gpgcheck,
  99. enabled=enabled,
  100. )
  101. # return data from pkg.mod_repo contains the file modified at
  102. # the top level, so use next(iter(ret)) to get that key
  103. self.assertNotEqual(ret, {})
  104. repo_info = ret[next(iter(ret))]
  105. self.assertIn(repo, repo_info)
  106. self.assertEqual(repo_info[repo]['baseurl'], baseurl)
  107. ret = self.run_function('pkg.get_repo', [repo])
  108. self.assertEqual(ret['baseurl'], baseurl)
  109. finally:
  110. if repo is not None:
  111. self.run_function('pkg.del_repo', [repo])
  112. @requires_salt_modules('pkg.owner')
  113. def test_owner(self):
  114. '''
  115. test finding the package owning a file
  116. '''
  117. func = 'pkg.owner'
  118. available = self.run_function('sys.doc', [func])
  119. if available:
  120. ret = self.run_function(func, ['/bin/ls'])
  121. self.assertNotEqual(len(ret), 0)
  122. else:
  123. os_grain = self.run_function('grains.item', ['os'])['os']
  124. self.skipTest('{0} is unavailable on {1}'.format(func, os_grain))
  125. @requires_network()
  126. @destructiveTest
  127. def test_install_remove(self):
  128. '''
  129. successfully install and uninstall a package
  130. '''
  131. version = self.run_function('pkg.version', [self.pkg])
  132. def test_install():
  133. install_ret = self.run_function('pkg.install', [self.pkg])
  134. self.assertIn(self.pkg, install_ret)
  135. def test_remove():
  136. remove_ret = self.run_function('pkg.remove', [self.pkg])
  137. self.assertIn(self.pkg, remove_ret)
  138. if version and isinstance(version, dict):
  139. version = version[self.pkg]
  140. if version:
  141. test_remove()
  142. test_install()
  143. else:
  144. test_install()
  145. test_remove()
  146. @skipIf(salt.utils.platform.is_windows(), "Skip on windows")
  147. @requires_salt_modules('pkg.hold', 'pkg.unhold')
  148. @requires_network()
  149. @destructiveTest
  150. def test_hold_unhold(self):
  151. '''
  152. test holding and unholding a package
  153. '''
  154. os_family = self.run_function('grains.item', ['os_family'])['os_family']
  155. available = self.run_function('sys.doc', ['pkg.hold'])
  156. version_lock = None
  157. lock_pkg = 'yum-plugin-versionlock'
  158. if available:
  159. self.run_function('pkg.install', [self.pkg])
  160. if os_family == 'RedHat':
  161. version_lock = self.run_function('pkg.version', [lock_pkg])
  162. if not version_lock:
  163. self.run_function('pkg.install', [lock_pkg])
  164. hold_ret = self.run_function('pkg.hold', [self.pkg])
  165. self.assertIn(self.pkg, hold_ret)
  166. self.assertTrue(hold_ret[self.pkg]['result'])
  167. unhold_ret = self.run_function('pkg.unhold', [self.pkg])
  168. self.assertIn(self.pkg, unhold_ret)
  169. self.assertTrue(unhold_ret[self.pkg]['result'])
  170. if os_family == 'RedHat':
  171. if not version_lock:
  172. self.run_function('pkg.remove', [lock_pkg])
  173. self.run_function('pkg.remove', [self.pkg])
  174. else:
  175. os_grain = self.run_function('grains.item', ['os'])['os']
  176. self.skipTest('{0} is unavailable on {1}'.format('pkg.hold', os_grain))
  177. @requires_network()
  178. @destructiveTest
  179. def test_refresh_db(self):
  180. '''
  181. test refreshing the package database
  182. '''
  183. func = 'pkg.refresh_db'
  184. os_family = self.run_function('grains.item', ['os_family'])['os_family']
  185. rtag = salt.utils.pkg.rtag(self.minion_opts)
  186. salt.utils.pkg.write_rtag(self.minion_opts)
  187. self.assertTrue(os.path.isfile(rtag))
  188. if os_family == 'RedHat':
  189. ret = self.run_function(func)
  190. self.assertIn(ret, (True, None))
  191. elif os_family == 'Suse':
  192. ret = self.run_function(func)
  193. if not isinstance(ret, dict):
  194. self.skipTest('Upstream repo did not return coherent results. Skipping test.')
  195. self.assertNotEqual(ret, {})
  196. elif os_family == 'Debian':
  197. ret = self.run_function(func)
  198. if not isinstance(ret, dict):
  199. self.skipTest('{0} encountered an error: {1}'.format(func, ret))
  200. self.assertNotEqual(ret, {})
  201. if not isinstance(ret, dict):
  202. self.skipTest('Upstream repo did not return coherent results. Skipping test.')
  203. for source, state in ret.items():
  204. self.assertIn(state, (True, False, None))
  205. else:
  206. os_grain = self.run_function('grains.item', ['os'])['os']
  207. self.skipTest('{0} is unavailable on {1}'.format(func, os_grain))
  208. self.assertFalse(os.path.isfile(rtag))
  209. @requires_salt_modules('pkg.info_installed')
  210. def test_pkg_info(self):
  211. '''
  212. Test returning useful information on Ubuntu systems.
  213. '''
  214. func = 'pkg.info_installed'
  215. os_family = self.run_function('grains.item', ['os_family'])['os_family']
  216. if os_family == 'Debian':
  217. ret = self.run_function(func, ['bash-completion', 'dpkg'])
  218. keys = ret.keys()
  219. self.assertIn('bash-completion', keys)
  220. self.assertIn('dpkg', keys)
  221. elif os_family == 'RedHat':
  222. ret = self.run_function(func, ['rpm', 'bash'])
  223. keys = ret.keys()
  224. self.assertIn('rpm', keys)
  225. self.assertIn('bash', keys)
  226. elif os_family == 'Suse':
  227. ret = self.run_function(func, ['less', 'zypper'])
  228. keys = ret.keys()
  229. self.assertIn('less', keys)
  230. self.assertIn('zypper', keys)
  231. @requires_network()
  232. @destructiveTest
  233. @skipIf(salt.utils.platform.is_windows(), 'pkg.upgrade not available on Windows')
  234. def test_pkg_upgrade_has_pending_upgrades(self):
  235. '''
  236. Test running a system upgrade when there are packages that need upgrading
  237. '''
  238. func = 'pkg.upgrade'
  239. os_family = self.run_function('grains.item', ['os_family'])['os_family']
  240. # First make sure that an up-to-date copy of the package db is available
  241. self.run_function('pkg.refresh_db')
  242. if os_family == 'Suse':
  243. # This test assumes that there are multiple possible versions of a
  244. # package available. That makes it brittle if you pick just one
  245. # target, as changes in the available packages will break the test.
  246. # Therefore, we'll choose from several packages to make sure we get
  247. # one that is suitable for this test.
  248. packages = ('hwinfo', 'avrdude', 'diffoscope', 'vim')
  249. available = self.run_function('pkg.list_repo_pkgs', packages)
  250. for package in packages:
  251. try:
  252. new, old = available[package][:2]
  253. except (KeyError, ValueError):
  254. # Package not available, or less than 2 versions
  255. # available. This is not a suitable target.
  256. continue
  257. else:
  258. target = package
  259. break
  260. else:
  261. # None of the packages have more than one version available, so
  262. # we need to find new package(s). pkg.list_repo_pkgs can be
  263. # used to get an overview of the available packages. We should
  264. # try to find packages with few dependencies and small download
  265. # sizes, to keep this test from taking longer than necessary.
  266. self.fail('No suitable package found for this test')
  267. # Make sure we have the 2nd-oldest available version installed
  268. ret = self.run_function('pkg.install', [target], version=old)
  269. if not isinstance(ret, dict):
  270. if ret.startswith('ERROR'):
  271. self.skipTest(
  272. 'Could not install older {0} to complete '
  273. 'test.'.format(target)
  274. )
  275. # Run a system upgrade, which should catch the fact that the
  276. # targeted package needs upgrading, and upgrade it.
  277. ret = self.run_function(func)
  278. # The changes dictionary should not be empty.
  279. if 'changes' in ret:
  280. self.assertIn(target, ret['changes'])
  281. else:
  282. self.assertIn(target, ret)
  283. else:
  284. ret = self.run_function('pkg.list_upgrades')
  285. if ret == '' or ret == {}:
  286. self.skipTest('No updates available for this machine. Skipping pkg.upgrade test.')
  287. else:
  288. args = []
  289. if os_family == 'Debian':
  290. args = ['dist_upgrade=True']
  291. ret = self.run_function(func, args)
  292. self.assertNotEqual(ret, {})
  293. @destructiveTest
  294. @skipIf(salt.utils.platform.is_windows(), 'minion is windows')
  295. @skipIf(salt.utils.platform.is_darwin(), 'minion is mac')
  296. def test_pkg_latest_version(self):
  297. '''
  298. Check that pkg.latest_version returns the latest version of the uninstalled package.
  299. The package is not installed. Only the package version is checked.
  300. '''
  301. grains = self.run_function('grains.items')
  302. remove = False
  303. if salt.utils.platform.is_windows():
  304. cmd_info = self.run_function('pkg.version', [self.pkg])
  305. remove = False if cmd_info == '' else True
  306. else:
  307. cmd_info = self.run_function('pkg.info_installed', [self.pkg])
  308. if cmd_info != 'ERROR: package {0} is not installed'.format(self.pkg):
  309. remove = True
  310. # remove package if its installed
  311. if remove:
  312. self.run_function('pkg.remove', [self.pkg])
  313. cmd_pkg = []
  314. if grains['os_family'] == 'RedHat':
  315. cmd_pkg = self.run_function('cmd.run', ['yum list {0}'.format(self.pkg)])
  316. elif salt.utils.platform.is_windows():
  317. cmd_pkg = self.run_function('pkg.list_available', [self.pkg])
  318. elif grains['os_family'] == 'Debian':
  319. cmd_pkg = self.run_function('cmd.run', ['apt list {0}'.format(self.pkg)])
  320. elif grains['os_family'] == 'Arch':
  321. cmd_pkg = self.run_function('cmd.run', ['pacman -Si {0}'.format(self.pkg)])
  322. elif grains['os_family'] == 'Suse':
  323. cmd_pkg = self.run_function('cmd.run', ['zypper info {0}'.format(self.pkg)])
  324. pkg_latest = self.run_function('pkg.latest_version', [self.pkg])
  325. self.assertIn(pkg_latest, cmd_pkg)