test_pkg.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. # -*- coding: utf-8 -*-
  2. '''
  3. tests for pkg state
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import logging
  8. import os
  9. import time
  10. # Import Salt Testing libs
  11. from tests.support.case import ModuleCase
  12. from tests.support.mixins import SaltReturnAssertsMixin
  13. from tests.support.unit import skipIf
  14. from tests.support.helpers import (
  15. destructiveTest,
  16. requires_salt_modules,
  17. requires_salt_states,
  18. requires_system_grains,
  19. runs_on,
  20. not_runs_on)
  21. # Import Salt libs
  22. import salt.utils.files
  23. import salt.utils.path
  24. import salt.utils.pkg.rpm
  25. import salt.utils.platform
  26. # Import 3rd-party libs
  27. from salt.ext import six
  28. from salt.ext.six.moves import range # pylint: disable=import-error,redefined-builtin
  29. log = logging.getLogger(__name__)
  30. @destructiveTest
  31. class PkgTest(ModuleCase, SaltReturnAssertsMixin):
  32. _PKG_EPOCH_TARGETS = []
  33. _PKG_32_TARGETS = []
  34. _PKG_CAP_TARGETS = []
  35. _PKG_DOT_TARGETS = []
  36. _WILDCARDS_SUPPORTED = False
  37. _VERSION_SPEC_SUPPORTED = True
  38. @classmethod
  39. @requires_system_grains
  40. def setUpClass(cls, grains=None): # pylint:disable=W0221
  41. cls.ctx = {}
  42. cls._PKG_TARGETS = ['figlet', 'sl']
  43. if grains['os'] == 'Windows':
  44. cls._PKG_TARGETS = ['7zip', 'putty']
  45. elif grains['os'] == 'freebsd':
  46. cls._VERSION_SPEC_SUPPORTED = False
  47. elif grains['os_family'] in ('Arch', 'Debian'):
  48. cls._WILDCARDS_SUPPORTED = True
  49. elif grains['os'] == 'Amazon':
  50. cls._PKG_TARGETS = ['lynx', 'gnuplot']
  51. elif grains['os_family'] == 'RedHat':
  52. cls._PKG_TARGETS = ['units', 'zsh-html']
  53. cls._WILDCARDS_SUPPORTED = True
  54. if grains['os'] == 'CentOS':
  55. if grains['osmajorrelease'] == 5:
  56. cls._PKG_32_TARGETS = ['xz-devel.i386']
  57. else:
  58. cls._PKG_32_TARGETS.append('xz-devel.i686')
  59. if grains['osmajorrelease'] == 5:
  60. cls._PKG_DOT_TARGETS = ['python-migrate0.5']
  61. elif grains['osmajorrelease'] == 6:
  62. cls._PKG_DOT_TARGETS = ['tomcat6-el-2.1-api']
  63. elif grains['osmajorrelease'] == 7:
  64. cls._PKG_DOT_TARGETS = ['tomcat-el-2.2-api']
  65. cls._PKG_EPOCH_TARGETS = ['comps-extras']
  66. elif grains['os_family'] == 'Suse':
  67. cls._PKG_TARGETS = ['lynx', 'htop']
  68. if grains['os'] == 'SUSE':
  69. cls._PKG_CAP_TARGETS = [('perl(ZNC)', 'znc-perl')]
  70. @classmethod
  71. def tearDownClass(cls):
  72. del cls.ctx
  73. def latest_version(self, *names):
  74. '''
  75. Helper function which ensures that we don't make any unnecessary calls to
  76. pkg.latest_version to figure out what version we need to install. This
  77. won't stop pkg.latest_version from being run in a pkg.latest state, but it
  78. will reduce the amount of times we check the latest version here in the
  79. test suite.
  80. '''
  81. key = 'latest_version'
  82. if key not in self.ctx:
  83. self.ctx[key] = dict()
  84. targets = [x for x in names if x not in self.ctx[key]]
  85. if targets:
  86. result = self.run_function('pkg.latest_version', targets, refresh=False)
  87. try:
  88. self.ctx[key].update(result)
  89. except ValueError:
  90. # Only a single target, pkg.latest_version returned a string
  91. self.ctx[key][targets[0]] = result
  92. ret = dict([(x, self.ctx[key].get(x, '')) for x in names])
  93. if len(names) == 1:
  94. return ret[names[0]]
  95. return ret
  96. @requires_system_grains
  97. def setUp(self, grains=None): # pylint:disable=W0221
  98. super(PkgTest, self).setUp()
  99. if 'refresh' not in self.ctx:
  100. self.run_function('pkg.refresh_db')
  101. self.ctx['refresh'] = True
  102. # If this is Arch Linux, check if pacman is in use by another process
  103. if grains['os_family'] == 'Arch':
  104. for _ in range(12):
  105. if not os.path.isfile('/var/lib/pacman/db.lck'):
  106. break
  107. else:
  108. time.sleep(5)
  109. else:
  110. raise Exception('Package database locked after 60 seconds, bailing out')
  111. @requires_salt_modules('pkg.version')
  112. @requires_salt_states('pkg.installed', 'pkg.removed')
  113. def test_pkg_001_installed(self):
  114. '''
  115. This is a destructive test as it installs and then removes a package
  116. '''
  117. target = self._PKG_TARGETS[0]
  118. version = self.run_function('pkg.version', [target])
  119. # If this assert fails, we need to find new targets, this test needs to
  120. # be able to test successful installation of packages, so this package
  121. # needs to not be installed before we run the states below
  122. self.assertFalse(version)
  123. ret = self.run_state('pkg.installed', name=target, refresh=False)
  124. self.assertSaltTrueReturn(ret)
  125. ret = self.run_state('pkg.removed', name=target)
  126. self.assertSaltTrueReturn(ret)
  127. @skipIf(not _VERSION_SPEC_SUPPORTED, 'Version specification not supported')
  128. @requires_salt_states('pkg.installed', 'pkg.removed')
  129. def test_pkg_002_installed_with_version(self):
  130. '''
  131. This is a destructive test as it installs and then removes a package
  132. '''
  133. target = self._PKG_TARGETS[0]
  134. version = self.latest_version(target)
  135. # If this assert fails, we need to find new targets, this test needs to
  136. # be able to test successful installation of packages, so this package
  137. # needs to not be installed before we run the states below
  138. self.assertTrue(version)
  139. ret = self.run_state('pkg.installed',
  140. name=target,
  141. version=version,
  142. refresh=False)
  143. self.assertSaltTrueReturn(ret)
  144. ret = self.run_state('pkg.removed', name=target)
  145. self.assertSaltTrueReturn(ret)
  146. @requires_salt_states('pkg.installed', 'pkg.removed')
  147. def test_pkg_003_installed_multipkg(self):
  148. '''
  149. This is a destructive test as it installs and then removes two packages
  150. '''
  151. version = self.run_function('pkg.version', self._PKG_TARGETS)
  152. # If this assert fails, we need to find new targets, this test needs to
  153. # be able to test successful installation of packages, so these
  154. # packages need to not be installed before we run the states below
  155. self.assertFalse(any(version.values()))
  156. self.assertSaltTrueReturn(self.run_state('pkg.removed', name=None, pkgs=self._PKG_TARGETS))
  157. try:
  158. ret = self.run_state('pkg.installed',
  159. name=None,
  160. pkgs=self._PKG_TARGETS,
  161. refresh=False)
  162. self.assertSaltTrueReturn(ret)
  163. finally:
  164. ret = self.run_state('pkg.removed', name=None, pkgs=self._PKG_TARGETS)
  165. self.assertSaltTrueReturn(ret)
  166. @skipIf(not _VERSION_SPEC_SUPPORTED, 'Version specification not supported')
  167. @requires_salt_states('pkg.installed', 'pkg.removed')
  168. def test_pkg_004_installed_multipkg_with_version(self):
  169. '''
  170. This is a destructive test as it installs and then removes two packages
  171. '''
  172. version = self.latest_version(self._PKG_TARGETS[0])
  173. # If this assert fails, we need to find new targets, this test needs to
  174. # be able to test successful installation of packages, so these
  175. # packages need to not be installed before we run the states below
  176. self.assertTrue(bool(version))
  177. pkgs = [{self._PKG_TARGETS[0]: version}, self._PKG_TARGETS[1]]
  178. try:
  179. ret = self.run_state('pkg.installed',
  180. name=None,
  181. pkgs=pkgs,
  182. refresh=False)
  183. self.assertSaltTrueReturn(ret)
  184. finally:
  185. ret = self.run_state('pkg.removed', name=None, pkgs=self._PKG_TARGETS)
  186. self.assertSaltTrueReturn(ret)
  187. @skipIf(not _PKG_32_TARGETS, 'No 32 bit packages have been specified for testing')
  188. @requires_salt_modules('pkg.version')
  189. @requires_salt_states('pkg.installed', 'pkg.removed')
  190. def test_pkg_005_installed_32bit(self):
  191. '''
  192. This is a destructive test as it installs and then removes a package
  193. '''
  194. target = self._PKG_32_TARGETS[0]
  195. # _PKG_TARGETS_32 is only populated for platforms for which Salt has to
  196. # munge package names for 32-bit-on-x86_64 (Currently only Ubuntu and
  197. # RHEL-based). Don't actually perform this test on other platforms.
  198. version = self.run_function('pkg.version', [target])
  199. # If this assert fails, we need to find a new target. This test
  200. # needs to be able to test successful installation of packages, so
  201. # the target needs to not be installed before we run the states
  202. # below
  203. self.assertFalse(version)
  204. ret = self.run_state('pkg.installed',
  205. name=target,
  206. refresh=False)
  207. self.assertSaltTrueReturn(ret)
  208. ret = self.run_state('pkg.removed', name=target)
  209. self.assertSaltTrueReturn(ret)
  210. @skipIf(not _PKG_32_TARGETS, 'No 32 bit packages have been specified for testing')
  211. @requires_salt_states('pkg.installed', 'pkg.removed')
  212. def test_pkg_006_installed_32bit_with_version(self):
  213. '''
  214. This is a destructive test as it installs and then removes a package
  215. '''
  216. target = self._PKG_32_TARGETS[0]
  217. # _PKG_TARGETS_32 is only populated for platforms for which Salt has to
  218. # munge package names for 32-bit-on-x86_64 (Currently only Ubuntu and
  219. # RHEL-based). Don't actually perform this test on other platforms.
  220. version = self.latest_version(target)
  221. # If this assert fails, we need to find a new target. This test
  222. # needs to be able to test successful installation of the package, so
  223. # the target needs to not be installed before we run the states
  224. # below
  225. self.assertTrue(version)
  226. ret = self.run_state('pkg.installed',
  227. name=target,
  228. version=version,
  229. refresh=False)
  230. self.assertSaltTrueReturn(ret)
  231. ret = self.run_state('pkg.removed', name=target)
  232. self.assertSaltTrueReturn(ret)
  233. @skipIf(not _PKG_DOT_TARGETS, 'No packages with "." in their name have been configured for')
  234. @requires_salt_states('pkg.installed', 'pkg.removed')
  235. def test_pkg_007_with_dot_in_pkgname(self=None):
  236. '''
  237. This tests for the regression found in the following issue:
  238. https://github.com/saltstack/salt/issues/8614
  239. This is a destructive test as it installs a package
  240. '''
  241. target = self._PKG_DOT_TARGETS[0]
  242. version = self.latest_version(target)
  243. # If this assert fails, we need to find a new target. This test
  244. # needs to be able to test successful installation of the package, so
  245. # the target needs to not be installed before we run the
  246. # pkg.installed state below
  247. self.assertTrue(bool(version))
  248. ret = self.run_state('pkg.installed', name=target, refresh=False)
  249. self.assertSaltTrueReturn(ret)
  250. ret = self.run_state('pkg.removed', name=target)
  251. self.assertSaltTrueReturn(ret)
  252. @skipIf(not _PKG_EPOCH_TARGETS, 'No targets have been configured with "epoch" in the version')
  253. @requires_salt_states('pkg.installed', 'pkg.removed')
  254. def test_pkg_008_epoch_in_version(self):
  255. '''
  256. This tests for the regression found in the following issue:
  257. https://github.com/saltstack/salt/issues/8614
  258. This is a destructive test as it installs a package
  259. '''
  260. target = self._PKG_EPOCH_TARGETS[0]
  261. version = self.latest_version(target)
  262. # If this assert fails, we need to find a new target. This test
  263. # needs to be able to test successful installation of the package, so
  264. # the target needs to not be installed before we run the
  265. # pkg.installed state below
  266. self.assertTrue(version)
  267. ret = self.run_state('pkg.installed',
  268. name=target,
  269. version=version,
  270. refresh=False)
  271. self.assertSaltTrueReturn(ret)
  272. ret = self.run_state('pkg.removed', name=target)
  273. self.assertSaltTrueReturn(ret)
  274. @requires_salt_modules('pkg.version', 'pkg.info_installed')
  275. @requires_salt_states('pkg.installed', 'pkg.removed')
  276. @runs_on(kernel='linux')
  277. @not_runs_on(os='Amazon')
  278. def test_pkg_009_latest_with_epoch(self):
  279. '''
  280. This tests for the following issue:
  281. https://github.com/saltstack/salt/issues/31014
  282. This is a destructive test as it installs a package
  283. '''
  284. package = 'bash-completion'
  285. pkgquery = 'version'
  286. ret = self.run_state('pkg.installed',
  287. name=package,
  288. refresh=False)
  289. self.assertSaltTrueReturn(ret)
  290. ret = self.run_function('pkg.info_installed', [package])
  291. self.assertTrue(pkgquery in six.text_type(ret))
  292. @requires_salt_states('pkg.latest', 'pkg.removed')
  293. def test_pkg_010_latest(self):
  294. '''
  295. This tests pkg.latest with a package that has no epoch (or a zero
  296. epoch).
  297. '''
  298. target = self._PKG_TARGETS[0]
  299. version = self.latest_version(target)
  300. # If this assert fails, we need to find new targets, this test needs to
  301. # be able to test successful installation of packages, so this package
  302. # needs to not be installed before we run the states below
  303. self.assertTrue(version)
  304. ret = self.run_state('pkg.latest', name=target, refresh=False)
  305. self.assertSaltTrueReturn(ret)
  306. ret = self.run_state('pkg.removed', name=target)
  307. self.assertSaltTrueReturn(ret)
  308. @requires_salt_modules('pkg.list_pkgs', 'pkg.list_upgrades', 'pkg.version')
  309. @requires_salt_states('pkg.latest')
  310. @runs_on(kernel='linux', os_family='Debian')
  311. def test_pkg_011_latest_only_upgrade(self):
  312. '''
  313. WARNING: This test will pick a package with an available upgrade (if
  314. there is one) and upgrade it to the latest version.
  315. '''
  316. target = self._PKG_TARGETS[0]
  317. # If this assert fails, we need to find new targets, this test needs to
  318. # be able to test that the state fails when you try to run the state
  319. # with only_upgrade=True on a package which is not already installed,
  320. # so the targeted package needs to not be installed before we run the
  321. # state below.
  322. version = self.latest_version(target)
  323. self.assertTrue(version)
  324. ret = self.run_state('pkg.latest', name=target, refresh=False, only_upgrade=True)
  325. self.assertSaltFalseReturn(ret)
  326. # Now look for updates and try to run the state on a package which is already up-to-date.
  327. installed_pkgs = self.run_function('pkg.list_pkgs')
  328. updates = self.run_function('pkg.list_upgrades', refresh=False)
  329. for pkgname in updates:
  330. if pkgname in installed_pkgs:
  331. target = pkgname
  332. break
  333. else:
  334. target = ''
  335. log.warning(
  336. 'No available upgrades to installed packages, skipping '
  337. 'only_upgrade=True test with already-installed package. For '
  338. 'best results run this test on a machine with upgrades '
  339. 'available.'
  340. )
  341. if target:
  342. ret = self.run_state('pkg.latest', name=target, refresh=False,
  343. only_upgrade=True)
  344. self.assertSaltTrueReturn(ret)
  345. new_version = self.run_function('pkg.version', [target])
  346. self.assertEqual(new_version, updates[target])
  347. ret = self.run_state('pkg.latest', name=target, refresh=False,
  348. only_upgrade=True)
  349. self.assertEqual(
  350. ret['pkg_|-{0}_|-{0}_|-latest'.format(target)]['comment'],
  351. 'Package {0} is already up-to-date'.format(target)
  352. )
  353. @skipIf(not _WILDCARDS_SUPPORTED, 'Wildcards in pkg.install are not supported')
  354. @requires_salt_modules('pkg.version')
  355. @requires_salt_states('pkg.installed', 'pkg.removed')
  356. def test_pkg_012_installed_with_wildcard_version(self):
  357. '''
  358. This is a destructive test as it installs and then removes a package
  359. '''
  360. target = self._PKG_TARGETS[0]
  361. version = self.run_function('pkg.version', [target])
  362. # If this assert fails, we need to find new targets, this test needs to
  363. # be able to test successful installation of packages, so this package
  364. # needs to not be installed before we run the states below
  365. self.assertFalse(version)
  366. ret = self.run_state(
  367. 'pkg.installed',
  368. name=target,
  369. version='*',
  370. refresh=False,
  371. )
  372. self.assertSaltTrueReturn(ret)
  373. # Repeat state, should pass
  374. ret = self.run_state(
  375. 'pkg.installed',
  376. name=target,
  377. version='*',
  378. refresh=False,
  379. )
  380. expected_comment = (
  381. 'All specified packages are already installed and are at the '
  382. 'desired version'
  383. )
  384. self.assertSaltTrueReturn(ret)
  385. self.assertEqual(ret[next(iter(ret))]['comment'], expected_comment)
  386. # Repeat one more time with unavailable version, test should fail
  387. ret = self.run_state(
  388. 'pkg.installed',
  389. name=target,
  390. version='93413*',
  391. refresh=False,
  392. )
  393. self.assertSaltFalseReturn(ret)
  394. # Clean up
  395. ret = self.run_state('pkg.removed', name=target)
  396. self.assertSaltTrueReturn(ret)
  397. @requires_salt_modules('pkg.version', 'pkg.latest_version')
  398. @requires_salt_states('pkg.installed', 'pkg.removed')
  399. @runs_on(kernel='linux', os_family=['Debian', 'RedHat'])
  400. def test_pkg_013_installed_with_comparison_operator(self):
  401. '''
  402. This is a destructive test as it installs and then removes a package
  403. '''
  404. target = self._PKG_TARGETS[0]
  405. version = self.run_function('pkg.version', [target])
  406. # If this assert fails, we need to find new targets, this test needs to
  407. # be able to test successful installation of packages, so this package
  408. # needs to not be installed before we run the states below
  409. self.assertFalse(version)
  410. latest_version = self.run_function(
  411. 'pkg.latest_version',
  412. [target],
  413. refresh=False)
  414. try:
  415. ret = self.run_state(
  416. 'pkg.installed',
  417. name=target,
  418. version='<9999999',
  419. refresh=False,
  420. )
  421. self.assertSaltTrueReturn(ret)
  422. # The version that was installed should be the latest available
  423. version = self.run_function('pkg.version', [target])
  424. self.assertTrue(version, latest_version)
  425. finally:
  426. # Clean up
  427. ret = self.run_state('pkg.removed', name=target)
  428. self.assertSaltTrueReturn(ret)
  429. @requires_salt_modules('pkg.version')
  430. @requires_salt_states('pkg.installed', 'pkg.removed')
  431. @runs_on(kernel='linux', os_familiy='RedHat')
  432. def test_pkg_014_installed_missing_release(self):
  433. '''
  434. Tests that a version number missing the release portion still resolves
  435. as correctly installed. For example, version 2.0.2 instead of 2.0.2-1.el7
  436. '''
  437. target = self._PKG_TARGETS[0]
  438. version = self.run_function('pkg.version', [target])
  439. # If this assert fails, we need to find new targets, this test needs to
  440. # be able to test successful installation of packages, so this package
  441. # needs to not be installed before we run the states below
  442. self.assertFalse(version)
  443. ret = self.run_state(
  444. 'pkg.installed',
  445. name=target,
  446. version=salt.utils.pkg.rpm.version_to_evr(version)[1],
  447. refresh=False,
  448. )
  449. self.assertSaltTrueReturn(ret)
  450. # Clean up
  451. ret = self.run_state('pkg.removed', name=target)
  452. self.assertSaltTrueReturn(ret)
  453. @requires_salt_modules('pkg.hold', 'pkg.unhold', 'pkg.version', 'pkg.list_pkgs')
  454. @requires_salt_states('pkg.installed', 'pkg.removed')
  455. @requires_system_grains
  456. def test_pkg_015_installed_held(self, grains=None):
  457. '''
  458. Tests that a package can be held even when the package is already installed.
  459. '''
  460. versionlock_pkg = None
  461. if grains['os_family'] == 'RedHat':
  462. pkgs = {p for p in self.run_function('pkg.list_pkgs') if '-versionlock' in p}
  463. if not pkgs:
  464. self.skipTest('No versionlock package found in repositories')
  465. for versionlock_pkg in pkgs:
  466. ret = self.run_state('pkg.installed', name=versionlock_pkg, refresh=False)
  467. # Exit loop if a versionlock package installed correctly
  468. try:
  469. self.assertSaltTrueReturn(ret)
  470. log.debug('Installed versionlock package: {}'.format(versionlock_pkg))
  471. break
  472. except AssertionError as e:
  473. log.debug('Versionlock package not found:\n{}'.format(e))
  474. else:
  475. self.fail('Could not install versionlock package from {}'.format(pkgs))
  476. target = self._PKG_TARGETS[0]
  477. # First we ensure that the package is installed
  478. ret = self.run_state(
  479. 'pkg.installed',
  480. name=target,
  481. refresh=False,
  482. )
  483. self.assertSaltTrueReturn(ret)
  484. # Then we check that the package is now held
  485. ret = self.run_state(
  486. 'pkg.installed',
  487. name=target,
  488. hold=True,
  489. refresh=False,
  490. )
  491. if versionlock_pkg and '-versionlock is not installed' in str(ret):
  492. self.skipTest('{} `{}` is installed'.format(ret, versionlock_pkg))
  493. # changes from pkg.hold for Red Hat family are different
  494. target_changes = {}
  495. if grains['os_family'] == 'RedHat':
  496. target_changes = {'new': 'hold', 'old': ''}
  497. elif grains['os_family'] == 'Debian':
  498. target_changes = {'new': 'hold', 'old': 'install'}
  499. try:
  500. tag = 'pkg_|-{0}_|-{0}_|-installed'.format(target)
  501. self.assertSaltTrueReturn(ret)
  502. self.assertIn(tag, ret)
  503. self.assertIn('changes', ret[tag])
  504. self.assertIn(target, ret[tag]['changes'])
  505. if not target_changes:
  506. self.skipTest(
  507. 'Test needs to be configured for {}: {}'.format(grains['os'], ret[tag]['changes'][target]))
  508. self.assertEqual(ret[tag]['changes'][target], target_changes)
  509. finally:
  510. # Clean up, unhold package and remove
  511. self.run_function('pkg.unhold', name=target)
  512. ret = self.run_state('pkg.removed', name=target)
  513. self.assertSaltTrueReturn(ret)
  514. if versionlock_pkg:
  515. ret = self.run_state('pkg.removed', name=versionlock_pkg)
  516. self.assertSaltTrueReturn(ret)
  517. @skipIf(not _PKG_CAP_TARGETS, 'Capability not provided')
  518. @requires_salt_modules('pkg.version')
  519. @requires_salt_states('pkg.installed', 'pkg.removed')
  520. def test_pkg_cap_001_installed(self):
  521. '''
  522. This is a destructive test as it installs and then removes a package
  523. '''
  524. target, realpkg = self._PKG_CAP_TARGETS[0]
  525. version = self.run_function('pkg.version', [target])
  526. realver = self.run_function('pkg.version', [realpkg])
  527. # If this condition is False, we need to find new targets.
  528. # This needs to be able to test successful installation of packages.
  529. # These packages need to not be installed before we run the states below
  530. if not (version and realver):
  531. self.skipTest('TODO: New pkg cap targets required')
  532. try:
  533. ret = self.run_state('pkg.installed', name=target, refresh=False, resolve_capabilities=True, test=True)
  534. self.assertInSaltComment("The following packages would be installed/updated: {0}".format(realpkg), ret)
  535. ret = self.run_state('pkg.installed', name=target, refresh=False, resolve_capabilities=True)
  536. self.assertSaltTrueReturn(ret)
  537. finally:
  538. ret = self.run_state('pkg.removed', name=realpkg)
  539. self.assertSaltTrueReturn(ret)
  540. @skipIf(not _PKG_CAP_TARGETS, 'Capability not available')
  541. @requires_salt_states('pkg.installed', 'pkg.removed')
  542. def test_pkg_cap_002_already_installed(self):
  543. '''
  544. This is a destructive test as it installs and then removes a package
  545. '''
  546. target, realpkg = self._PKG_CAP_TARGETS[0]
  547. version = self.run_function('pkg.version', [target])
  548. realver = self.run_function('pkg.version', [realpkg])
  549. # If this condition is False, we need to find new targets.
  550. # This needs to be able to test successful installation of packages.
  551. # These packages need to not be installed before we run the states below
  552. if not (version and realver):
  553. self.skipTest('TODO: New pkg cap targets required')
  554. try:
  555. # install the package
  556. ret = self.run_state('pkg.installed', name=realpkg, refresh=False)
  557. self.assertSaltTrueReturn(ret)
  558. # Try to install again. Nothing should be installed this time.
  559. ret = self.run_state('pkg.installed', name=target, refresh=False, resolve_capabilities=True, test=True)
  560. self.assertInSaltComment("All specified packages are already installed", ret)
  561. ret = self.run_state('pkg.installed', name=target, refresh=False, resolve_capabilities=True)
  562. self.assertSaltTrueReturn(ret)
  563. self.assertInSaltComment("packages are already installed", ret)
  564. finally:
  565. ret = self.run_state('pkg.removed', name=realpkg)
  566. self.assertSaltTrueReturn(ret)
  567. @skipIf(not _PKG_CAP_TARGETS, 'Capability not available')
  568. @skipIf(not _VERSION_SPEC_SUPPORTED, 'Version specification not supported')
  569. @requires_salt_states('pkg.installed', 'pkg.removed')
  570. def test_pkg_cap_003_installed_multipkg_with_version(self):
  571. '''
  572. This is a destructive test as it installs and then removes two packages
  573. '''
  574. target, realpkg = self._PKG_CAP_TARGETS[0]
  575. version = self.latest_version(target)
  576. realver = self.latest_version(realpkg)
  577. # If this condition is False, we need to find new targets.
  578. # This needs to be able to test successful installation of packages.
  579. # These packages need to not be installed before we run the states below
  580. if not (version and realver):
  581. self.skipTest('TODO: New pkg cap targets required')
  582. cleanup_pkgs = self._PKG_TARGETS
  583. try:
  584. pkgs = [{self._PKG_TARGETS[0]: version}, self._PKG_TARGETS[1], {target: realver}]
  585. ret = self.run_state('pkg.installed',
  586. name='test_pkg_cap_003_installed_multipkg_with_version-install',
  587. pkgs=pkgs,
  588. refresh=False)
  589. self.assertSaltFalseReturn(ret)
  590. ret = self.run_state('pkg.installed',
  591. name='test_pkg_cap_003_installed_multipkg_with_version-install-capability',
  592. pkgs=pkgs,
  593. refresh=False, resolve_capabilities=True, test=True)
  594. self.assertInSaltComment("packages would be installed/updated", ret)
  595. self.assertInSaltComment("{0}={1}".format(realpkg, realver), ret)
  596. ret = self.run_state('pkg.installed',
  597. name='test_pkg_cap_003_installed_multipkg_with_version-install-capability',
  598. pkgs=pkgs,
  599. refresh=False, resolve_capabilities=True)
  600. self.assertSaltTrueReturn(ret)
  601. cleanup_pkgs.append(realpkg)
  602. finally:
  603. ret = self.run_state('pkg.removed',
  604. name='test_pkg_cap_003_installed_multipkg_with_version-remove',
  605. pkgs=cleanup_pkgs)
  606. self.assertSaltTrueReturn(ret)
  607. @skipIf(not _PKG_CAP_TARGETS, 'Capability not available')
  608. @requires_salt_modules('pkg.version')
  609. @requires_salt_states('pkg.latest', 'pkg.removed')
  610. def test_pkg_cap_004_latest(self):
  611. '''
  612. This tests pkg.latest with a package that has no epoch (or a zero
  613. epoch).
  614. '''
  615. target, realpkg = self._PKG_CAP_TARGETS[0]
  616. version = self.run_function('pkg.version', [target])
  617. realver = self.run_function('pkg.version', [realpkg])
  618. # If this condition is False, we need to find new targets.
  619. # This needs to be able to test successful installation of packages.
  620. # These packages need to not be installed before we run the states below
  621. if not (version and realver):
  622. self.skipTest('TODO: New pkg cap targets required')
  623. try:
  624. ret = self.run_state('pkg.latest', name=target, refresh=False, resolve_capabilities=True, test=True)
  625. self.assertInSaltComment("The following packages would be installed/upgraded: {0}".format(realpkg), ret)
  626. ret = self.run_state('pkg.latest', name=target, refresh=False, resolve_capabilities=True)
  627. self.assertSaltTrueReturn(ret)
  628. ret = self.run_state('pkg.latest', name=target, refresh=False, resolve_capabilities=True)
  629. self.assertSaltTrueReturn(ret)
  630. self.assertInSaltComment("is already up-to-date", ret)
  631. finally:
  632. ret = self.run_state('pkg.removed', name=realpkg)
  633. self.assertSaltTrueReturn(ret)
  634. @skipIf(not _PKG_CAP_TARGETS, 'Capability not available')
  635. @requires_salt_modules('pkg.version')
  636. @requires_salt_states('pkg.installed', 'pkg.removed', 'pkg.downloaded')
  637. def test_pkg_cap_005_downloaded(self):
  638. '''
  639. This is a destructive test as it installs and then removes a package
  640. '''
  641. target, realpkg = self._PKG_CAP_TARGETS[0]
  642. version = self.run_function('pkg.version', [target])
  643. realver = self.run_function('pkg.version', [realpkg])
  644. # If this condition is False, we need to find new targets.
  645. # This needs to be able to test successful installation of packages.
  646. # These packages need to not be installed before we run the states below
  647. if not (version and realver):
  648. self.skipTest('TODO: New pkg cap targets required')
  649. ret = self.run_state('pkg.downloaded', name=target, refresh=False)
  650. self.assertSaltFalseReturn(ret)
  651. ret = self.run_state('pkg.downloaded', name=target, refresh=False, resolve_capabilities=True, test=True)
  652. self.assertInSaltComment("The following packages would be downloaded: {0}".format(realpkg), ret)
  653. ret = self.run_state('pkg.downloaded', name=target, refresh=False, resolve_capabilities=True)
  654. self.assertSaltTrueReturn(ret)
  655. @skipIf(not _PKG_CAP_TARGETS, 'Capability not available')
  656. @requires_salt_modules('pkg.version')
  657. @requires_salt_states('pkg.installed', 'pkg.removed', 'pkg.uptodate')
  658. def test_pkg_cap_006_uptodate(self):
  659. '''
  660. This is a destructive test as it installs and then removes a package
  661. '''
  662. target, realpkg = self._PKG_CAP_TARGETS[0]
  663. version = self.run_function('pkg.version', [target])
  664. realver = self.run_function('pkg.version', [realpkg])
  665. # If this condition is False, we need to find new targets.
  666. # This needs to be able to test successful installation of packages.
  667. # These packages need to not be installed before we run the states below
  668. if not (version and realver):
  669. self.skipTest('TODO: New pkg cap targets required')
  670. try:
  671. ret = self.run_state('pkg.installed', name=target,
  672. refresh=False, resolve_capabilities=True)
  673. self.assertSaltTrueReturn(ret)
  674. ret = self.run_state('pkg.uptodate',
  675. name='test_pkg_cap_006_uptodate',
  676. pkgs=[target],
  677. refresh=False,
  678. resolve_capabilities=True)
  679. self.assertSaltTrueReturn(ret)
  680. self.assertInSaltComment("System is already up-to-date", ret)
  681. finally:
  682. ret = self.run_state('pkg.removed', name=realpkg)
  683. self.assertSaltTrueReturn(ret)