test_pkgng.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import
  4. import textwrap
  5. # Import Salt Testing Libs
  6. from tests.support.mixins import LoaderModuleMockMixin
  7. from tests.support.unit import TestCase
  8. from tests.support.mock import (
  9. MagicMock,
  10. patch)
  11. # Import Salt Libs
  12. import salt.modules.pkgng as pkgng
  13. class PkgNgTestCase(TestCase, LoaderModuleMockMixin):
  14. '''
  15. Test cases for salt.modules.pkgng
  16. '''
  17. @classmethod
  18. def setup_loader_modules(cls):
  19. return {
  20. pkgng: {
  21. '__salt__': {}
  22. }
  23. }
  24. def test_lock(self):
  25. '''
  26. Test pkgng.lock
  27. '''
  28. lock_cmd = MagicMock(return_value={
  29. 'stdout': ('pkga-1.0\n'
  30. 'pkgb-2.0\n'),
  31. 'retcode': 0
  32. })
  33. with patch.dict(pkgng.__salt__, {'cmd.run_all': lock_cmd}):
  34. result = pkgng.lock('pkga')
  35. self.assertTrue(result)
  36. lock_cmd.assert_called_with(
  37. ['pkg', 'lock', '-y', '--quiet', '--show-locked', 'pkga'],
  38. output_loglevel='trace', python_shell=False
  39. )
  40. result = pkgng.lock('dummy')
  41. self.assertFalse(result)
  42. lock_cmd.assert_called_with(
  43. ['pkg', 'lock', '-y', '--quiet', '--show-locked', 'dummy'],
  44. output_loglevel='trace', python_shell=False
  45. )
  46. def test_unlock(self):
  47. '''
  48. Test pkgng.unlock
  49. '''
  50. unlock_cmd = MagicMock(return_value={
  51. 'stdout': ('pkga-1.0\n'
  52. 'pkgb-2.0\n'),
  53. 'retcode': 0
  54. })
  55. with patch.dict(pkgng.__salt__, {'cmd.run_all': unlock_cmd}):
  56. result = pkgng.unlock('pkga')
  57. self.assertFalse(result)
  58. unlock_cmd.assert_called_with(
  59. ['pkg', 'unlock', '-y', '--quiet', '--show-locked', 'pkga'],
  60. output_loglevel='trace', python_shell=False
  61. )
  62. result = pkgng.unlock('dummy')
  63. self.assertTrue(result)
  64. unlock_cmd.assert_called_with(
  65. ['pkg', 'unlock', '-y', '--quiet', '--show-locked', 'dummy'],
  66. output_loglevel='trace', python_shell=False
  67. )
  68. def test_locked(self):
  69. '''
  70. Test pkgng.unlock
  71. '''
  72. lock_cmd = MagicMock(return_value={
  73. 'stdout': ('pkga-1.0\n'
  74. 'pkgb-2.0\n'),
  75. 'retcode': 0
  76. })
  77. with patch.dict(pkgng.__salt__, {'cmd.run_all': lock_cmd}):
  78. result = pkgng.locked('pkga')
  79. self.assertTrue(result)
  80. lock_cmd.assert_called_with(
  81. ['pkg', 'lock', '-y', '--quiet', '--show-locked'],
  82. output_loglevel='trace', python_shell=False
  83. )
  84. result = pkgng.locked('dummy')
  85. self.assertFalse(result)
  86. lock_cmd.assert_called_with(
  87. ['pkg', 'lock', '-y', '--quiet', '--show-locked'],
  88. output_loglevel='trace', python_shell=False
  89. )
  90. def test_list_upgrades_present(self):
  91. '''
  92. Test pkgng.list_upgrades with upgrades available
  93. '''
  94. pkg_cmd = MagicMock(return_value=textwrap.dedent(
  95. """
  96. The following 6 package(s) will be affected (of 0 checked):
  97. Installed packages to be UPGRADED:
  98. pkga: 1.0 -> 1.1
  99. pkgb: 2.0 -> 2.1 [FreeBSD]
  100. pkgc: 3.0 -> 3.1 [FreeBSD] (dependency changed)
  101. pkgd: 4.0 -> 4.1 (dependency changed)
  102. New packages to be INSTALLED:
  103. pkge: 1.0
  104. pkgf: 2.0 [FreeBSD]
  105. pkgg: 3.0 [FreeBSD] (dependency changed)
  106. pkgh: 4.0 (dependency changed)
  107. Installed packages to be REINSTALLED:
  108. pkgi-1.0
  109. pkgj-2.0 [FreeBSD]
  110. pkgk-3.0 [FreeBSD] (direct dependency changed: pkga)
  111. pkgl-4.0 (direct dependency changed: pkgb)
  112. Installed packages to be DOWNGRADED:
  113. pkgm: 1.1 -> 1.0
  114. pkgn: 2.1 -> 2.0 [FreeBSD]
  115. pkgo: 3.1 -> 3.0 [FreeBSD] (dependency changed)
  116. pkgp: 4.1 -> 4.0 (dependency changed)
  117. Installed packages to be REMOVED:
  118. pkgq-1.0
  119. pkgr-2.0 [FreeBSD]
  120. pkgs-3.0 [FreeBSD] (direct dependency changed: pkga)
  121. pkgt-4.0 (direct dependency changed: pkgb)
  122. Number of packages to be upgraded: 2
  123. Number of packages to be reinstalled: 2
  124. The process will require 14 MiB more space.
  125. 22 MiB to be downloaded.
  126. """
  127. ))
  128. with patch.dict(pkgng.__salt__, {'cmd.run_stdout': pkg_cmd}):
  129. result = pkgng.list_upgrades(refresh=False)
  130. self.assertDictEqual(result, {'pkga': '1.1', 'pkgb': '2.1', 'pkgc': '3.1', 'pkgd': '4.1'})
  131. pkg_cmd.assert_called_with(
  132. ['pkg', 'upgrade', '--dry-run', '--quiet', '--no-repo-update'],
  133. output_loglevel='trace', python_shell=False, ignore_retcode=True
  134. )
  135. def test_list_upgrades_absent(self):
  136. '''
  137. Test pkgng.list_upgrades with no upgrades available
  138. '''
  139. pkg_cmd = MagicMock(return_value='')
  140. with patch.dict(pkgng.__salt__, {'cmd.run_stdout': pkg_cmd}):
  141. result = pkgng.list_upgrades(refresh=False)
  142. self.assertDictEqual(result, {})
  143. pkg_cmd.assert_called_with(
  144. ['pkg', 'upgrade', '--dry-run', '--quiet', '--no-repo-update'],
  145. output_loglevel='trace', python_shell=False, ignore_retcode=True
  146. )