test_pkgin.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. # -*- coding: utf-8 -*-
  2. # Import Python Libs
  3. from __future__ import absolute_import
  4. import os
  5. # Import Salt Libs
  6. import salt.modules.pkgin as pkgin
  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 PkginTestCase(TestCase, LoaderModuleMockMixin):
  12. """
  13. Test cases for salt.modules.pkgin
  14. """
  15. def setup_loader_modules(self):
  16. return {pkgin: {"__opts__": {"cachedir": "/tmp"}}}
  17. def test_search(self):
  18. """
  19. Test searching for a package
  20. """
  21. # Test searching for an available and uninstalled package
  22. pkgin_out = [
  23. "somepkg-1.0 Some package description here",
  24. "",
  25. "=: package is installed and up-to-date",
  26. "<: package is installed but newer version is available",
  27. ">: installed package has a greater version than available package",
  28. ]
  29. pkgin__get_version_mock = MagicMock(return_value=["0", "9", "0"])
  30. pkgin__check_pkgin_mock = MagicMock(return_value="/opt/pkg/bin/pkgin")
  31. pkgin_search_cmd = MagicMock(return_value=os.linesep.join(pkgin_out))
  32. with patch("salt.modules.pkgin._get_version", pkgin__get_version_mock), patch(
  33. "salt.modules.pkgin._check_pkgin", pkgin__check_pkgin_mock
  34. ), patch.dict(pkgin.__salt__, {"cmd.run": pkgin_search_cmd}):
  35. self.assertDictEqual(pkgin.search("somepkg"), {"somepkg": "1.0"})
  36. # Test searching for an available and installed package
  37. pkgin_out = [
  38. "somepkg-1.0 = Some package description here",
  39. "",
  40. "=: package is installed and up-to-date",
  41. "<: package is installed but newer version is available",
  42. ">: installed package has a greater version than available package",
  43. ]
  44. pkgin_search_cmd = MagicMock(return_value=os.linesep.join(pkgin_out))
  45. with patch("salt.modules.pkgin._get_version", pkgin__get_version_mock), patch(
  46. "salt.modules.pkgin._check_pkgin", pkgin__check_pkgin_mock
  47. ), patch.dict(pkgin.__salt__, {"cmd.run": pkgin_search_cmd}):
  48. self.assertDictEqual(pkgin.search("somepkg"), {"somepkg": "1.0"})
  49. def test_latest_version(self):
  50. """
  51. Test getting the latest version of a package
  52. """
  53. # Test getting the latest version of an uninstalled package
  54. pkgin_out = [
  55. "somepkg-1.0;;Some package description here",
  56. "",
  57. "=: package is installed and up-to-date",
  58. "<: package is installed but newer version is available",
  59. ">: installed package has a greater version than available package",
  60. ]
  61. pkgin__get_version_mock = MagicMock(return_value=["0", "9", "0"])
  62. pkgin__check_pkgin_mock = MagicMock(return_value="/opt/pkg/bin/pkgin")
  63. pkgin_refresh_db_mock = MagicMock(return_value=True)
  64. pkgin_search_cmd = MagicMock(return_value=os.linesep.join(pkgin_out))
  65. with patch("salt.modules.pkgin.refresh_db", pkgin_refresh_db_mock), patch(
  66. "salt.modules.pkgin._get_version", pkgin__get_version_mock
  67. ), patch(
  68. "salt.modules.pkgin._check_pkgin", pkgin__check_pkgin_mock
  69. ), patch.dict(
  70. pkgin.__salt__, {"cmd.run": pkgin_search_cmd}
  71. ):
  72. self.assertEqual(pkgin.latest_version("somepkg"), "1.0")
  73. # Test getting the latest version of an installed package
  74. pkgin_out = [
  75. "somepkg-1.1;<;Some package description here",
  76. "",
  77. "=: package is installed and up-to-date",
  78. "<: package is installed but newer version is available",
  79. ">: installed package has a greater version than available package",
  80. ]
  81. pkgin_refresh_db_mock = MagicMock(return_value=True)
  82. pkgin_search_cmd = MagicMock(return_value=os.linesep.join(pkgin_out))
  83. with patch("salt.modules.pkgin.refresh_db", pkgin_refresh_db_mock), patch(
  84. "salt.modules.pkgin._get_version", pkgin__get_version_mock
  85. ), patch(
  86. "salt.modules.pkgin._check_pkgin", pkgin__check_pkgin_mock
  87. ), patch.dict(
  88. pkgin.__salt__, {"cmd.run": pkgin_search_cmd}
  89. ):
  90. self.assertEqual(pkgin.latest_version("somepkg"), "1.1")
  91. # Test getting the latest version of a package that is already installed
  92. # and is already at the latest version
  93. pkgin_out = [
  94. "somepkg-1.2;=;Some package description here",
  95. "",
  96. "=: package is installed and up-to-date",
  97. "<: package is installed but newer version is available",
  98. ">: installed package has a greater version than available package",
  99. ]
  100. pkgin_refresh_db_mock = MagicMock(return_value=True)
  101. pkgin_search_cmd = MagicMock(return_value=os.linesep.join(pkgin_out))
  102. with patch("salt.modules.pkgin.refresh_db", pkgin_refresh_db_mock), patch(
  103. "salt.modules.pkgin._get_version", pkgin__get_version_mock
  104. ), patch(
  105. "salt.modules.pkgin._check_pkgin", pkgin__check_pkgin_mock
  106. ), patch.dict(
  107. pkgin.__salt__, {"cmd.run": pkgin_search_cmd}
  108. ):
  109. self.assertEqual(pkgin.latest_version("somepkg"), "1.2")
  110. # Test getting the latest version of a bogus package
  111. pkgin_out = "No results found for ^boguspkg$"
  112. pkgin_refresh_db_mock = MagicMock(return_value=True)
  113. pkgin_search_cmd = MagicMock(return_value=pkgin_out)
  114. with patch("salt.modules.pkgin.refresh_db", pkgin_refresh_db_mock), patch(
  115. "salt.modules.pkgin._get_version", pkgin__get_version_mock
  116. ), patch(
  117. "salt.modules.pkgin._check_pkgin", pkgin__check_pkgin_mock
  118. ), patch.dict(
  119. pkgin.__salt__, {"cmd.run": pkgin_search_cmd}
  120. ):
  121. self.assertEqual(pkgin.latest_version("boguspkg"), {})
  122. def test_file_dict(self):
  123. """
  124. Test that file_dict doesn't crash
  125. """
  126. pkg_info_stdout = [
  127. "/opt/pkg/bin/pkgin",
  128. "/opt/pkg/man/man1/pkgin.1",
  129. "/opt/pkg/share/examples/pkgin/preferred.conf.example",
  130. "/opt/pkg/share/examples/pkgin/repositories.conf.example",
  131. ]
  132. pkg_info_out = {
  133. "pid": 1234,
  134. "retcode": 0,
  135. "stderr": "",
  136. "stdout": os.linesep.join(pkg_info_stdout),
  137. }
  138. pkg_info_cmd = MagicMock(return_value=pkg_info_out)
  139. with patch.dict(pkgin.__salt__, {"cmd.run_all": pkg_info_cmd}):
  140. self.assertDictEqual(
  141. pkgin.file_dict("pkgin"),
  142. {
  143. "files": {
  144. "pkgin": [
  145. "/opt/pkg/bin/pkgin",
  146. "/opt/pkg/man/man1/pkgin.1",
  147. "/opt/pkg/share/examples/pkgin/preferred.conf.example",
  148. "/opt/pkg/share/examples/pkgin/repositories.conf.example",
  149. ]
  150. }
  151. },
  152. )