test_pkgng.py 5.8 KB

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