test_pacmanpkg.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Eric Vz <eric@base10.org>
  4. """
  5. # Import Python Libs
  6. from __future__ import absolute_import
  7. # Import Salt Libs
  8. import salt.modules.pacmanpkg as pacman
  9. # Import Salt Testing Libs
  10. from tests.support.mixins import LoaderModuleMockMixin
  11. from tests.support.mock import MagicMock, patch
  12. from tests.support.unit import TestCase
  13. class PacmanTestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. Test cases for salt.modules.pacman
  16. """
  17. def setup_loader_modules(self):
  18. return {pacman: {}}
  19. def test_list_pkgs(self):
  20. """
  21. Test if it list the packages currently installed in a dict
  22. """
  23. cmdmock = MagicMock(return_value="A 1.0\nB 2.0")
  24. sortmock = MagicMock()
  25. stringifymock = MagicMock()
  26. mock_ret = {"A": ["1.0"], "B": ["2.0"]}
  27. with patch.dict(
  28. pacman.__salt__,
  29. {
  30. "cmd.run": cmdmock,
  31. "pkg_resource.add_pkg": lambda pkgs, name, version: pkgs.setdefault(
  32. name, []
  33. ).append(version),
  34. "pkg_resource.sort_pkglist": sortmock,
  35. "pkg_resource.stringify": stringifymock,
  36. },
  37. ):
  38. self.assertDictEqual(pacman.list_pkgs(), mock_ret)
  39. sortmock.assert_called_with(mock_ret)
  40. stringifymock.assert_called_with(mock_ret)
  41. def test_list_pkgs_as_list(self):
  42. """
  43. Test if it lists the packages currently installed in a dict
  44. """
  45. cmdmock = MagicMock(return_value="A 1.0\nB 2.0")
  46. sortmock = MagicMock()
  47. stringifymock = MagicMock()
  48. mock_ret = {"A": ["1.0"], "B": ["2.0"]}
  49. with patch.dict(
  50. pacman.__salt__,
  51. {
  52. "cmd.run": cmdmock,
  53. "pkg_resource.add_pkg": lambda pkgs, name, version: pkgs.setdefault(
  54. name, []
  55. ).append(version),
  56. "pkg_resource.sort_pkglist": sortmock,
  57. "pkg_resource.stringify": stringifymock,
  58. },
  59. ):
  60. self.assertDictEqual(pacman.list_pkgs(True), mock_ret)
  61. sortmock.assert_called_with(mock_ret)
  62. self.assertTrue(stringifymock.call_count == 0)
  63. def test_group_list(self):
  64. """
  65. Test if it lists the available groups
  66. """
  67. def cmdlist(cmd, **kwargs):
  68. """
  69. Handle several different commands being run
  70. """
  71. if cmd == ["pacman", "-Sgg"]:
  72. return "group-a pkg1\ngroup-a pkg2\ngroup-f pkg9\ngroup-c pkg3\ngroup-b pkg4"
  73. elif cmd == ["pacman", "-Qg"]:
  74. return "group-a pkg1\ngroup-b pkg4"
  75. else:
  76. return "Untested command ({0}, {1})!".format(cmd, kwargs)
  77. cmdmock = MagicMock(side_effect=cmdlist)
  78. sortmock = MagicMock()
  79. with patch.dict(
  80. pacman.__salt__, {"cmd.run": cmdmock, "pkg_resource.sort_pkglist": sortmock}
  81. ):
  82. self.assertDictEqual(
  83. pacman.group_list(),
  84. {
  85. "available": ["group-c", "group-f"],
  86. "installed": ["group-b"],
  87. "partially_installed": ["group-a"],
  88. },
  89. )
  90. def test_group_info(self):
  91. """
  92. Test if it shows the packages in a group
  93. """
  94. def cmdlist(cmd, **kwargs):
  95. """
  96. Handle several different commands being run
  97. """
  98. if cmd == ["pacman", "-Sgg", "testgroup"]:
  99. return "testgroup pkg1\ntestgroup pkg2"
  100. else:
  101. return "Untested command ({0}, {1})!".format(cmd, kwargs)
  102. cmdmock = MagicMock(side_effect=cmdlist)
  103. sortmock = MagicMock()
  104. with patch.dict(
  105. pacman.__salt__, {"cmd.run": cmdmock, "pkg_resource.sort_pkglist": sortmock}
  106. ):
  107. self.assertEqual(
  108. pacman.group_info("testgroup")["default"], ["pkg1", "pkg2"]
  109. )
  110. def test_group_diff(self):
  111. """
  112. Test if it shows the difference between installed and target group contents
  113. """
  114. listmock = MagicMock(return_value={"A": ["1.0"], "B": ["2.0"]})
  115. groupmock = MagicMock(
  116. return_value={
  117. "mandatory": [],
  118. "optional": [],
  119. "default": ["A", "C"],
  120. "conditional": [],
  121. }
  122. )
  123. with patch.dict(
  124. pacman.__salt__, {"pkg.list_pkgs": listmock, "pkg.group_info": groupmock}
  125. ):
  126. results = pacman.group_diff("testgroup")
  127. self.assertEqual(
  128. results["default"], {"installed": ["A"], "not installed": ["C"]}
  129. )