test_pkg.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import, print_function, unicode_literals
  3. import salt.utils.pkg
  4. from salt.utils.pkg import rpm
  5. from tests.support.mock import MagicMock, patch
  6. from tests.support.unit import TestCase
  7. class PkgUtilsTestCase(TestCase):
  8. """
  9. TestCase for salt.utils.pkg module
  10. """
  11. test_parameters = [
  12. ("16.0.0.49153-0+f1", "", "16.0.0.49153-0+f1"),
  13. ("> 15.0.0", ">", "15.0.0"),
  14. ("< 15.0.0", "<", "15.0.0"),
  15. ("<< 15.0.0", "<<", "15.0.0"),
  16. (">> 15.0.0", ">>", "15.0.0"),
  17. (">= 15.0.0", ">=", "15.0.0"),
  18. ("<= 15.0.0", "<=", "15.0.0"),
  19. ("!= 15.0.0", "!=", "15.0.0"),
  20. ("<=> 15.0.0", "<=>", "15.0.0"),
  21. ("<> 15.0.0", "<>", "15.0.0"),
  22. ("= 15.0.0", "=", "15.0.0"),
  23. (">15.0.0", ">", "15.0.0"),
  24. ("<15.0.0", "<", "15.0.0"),
  25. ("<<15.0.0", "<<", "15.0.0"),
  26. (">>15.0.0", ">>", "15.0.0"),
  27. (">=15.0.0", ">=", "15.0.0"),
  28. ("<=15.0.0", "<=", "15.0.0"),
  29. ("!=15.0.0", "!=", "15.0.0"),
  30. ("<=>15.0.0", "<=>", "15.0.0"),
  31. ("<>15.0.0", "<>", "15.0.0"),
  32. ("=15.0.0", "=", "15.0.0"),
  33. ("", "", ""),
  34. ]
  35. def test_split_comparison(self):
  36. """
  37. Tests salt.utils.pkg.split_comparison
  38. """
  39. for test_parameter in self.test_parameters:
  40. oper, verstr = salt.utils.pkg.split_comparison(test_parameter[0])
  41. self.assertEqual(test_parameter[1], oper)
  42. self.assertEqual(test_parameter[2], verstr)
  43. class PkgRPMTestCase(TestCase):
  44. """
  45. Test case for pkg.rpm utils
  46. """
  47. @patch("salt.utils.path.which", MagicMock(return_value=True))
  48. def test_get_osarch_by_rpm(self):
  49. """
  50. Get os_arch if RPM package is installed.
  51. :return:
  52. """
  53. subprocess_mock = MagicMock()
  54. subprocess_mock.Popen = MagicMock()
  55. subprocess_mock.Popen().communicate = MagicMock(return_value=["Z80"])
  56. with patch("salt.utils.pkg.rpm.subprocess", subprocess_mock):
  57. assert rpm.get_osarch() == "Z80"
  58. assert subprocess_mock.Popen.call_count == 2 # One within the mock
  59. assert subprocess_mock.Popen.call_args[1]["close_fds"]
  60. assert subprocess_mock.Popen.call_args[1]["shell"]
  61. assert len(subprocess_mock.Popen.call_args_list) == 2
  62. assert subprocess_mock.Popen.call_args[0][0] == 'rpm --eval "%{_host_cpu}"'
  63. @patch("salt.utils.path.which", MagicMock(return_value=False))
  64. @patch("salt.utils.pkg.rpm.subprocess", MagicMock(return_value=False))
  65. @patch(
  66. "salt.utils.pkg.rpm.platform.uname",
  67. MagicMock(
  68. return_value=(
  69. "Sinclair BASIC",
  70. "motophone",
  71. "1982 Sinclair Research Ltd",
  72. "1.0",
  73. "ZX81",
  74. "Z80",
  75. )
  76. ),
  77. )
  78. def test_get_osarch_by_platform(self):
  79. """
  80. Get os_arch if RPM package is not installed (inird image, for example).
  81. :return:
  82. """
  83. assert rpm.get_osarch() == "Z80"
  84. @patch("salt.utils.path.which", MagicMock(return_value=False))
  85. @patch("salt.utils.pkg.rpm.subprocess", MagicMock(return_value=False))
  86. @patch(
  87. "salt.utils.pkg.rpm.platform.uname",
  88. MagicMock(
  89. return_value=(
  90. "Sinclair BASIC",
  91. "motophone",
  92. "1982 Sinclair Research Ltd",
  93. "1.0",
  94. "ZX81",
  95. "",
  96. )
  97. ),
  98. )
  99. def test_get_osarch_by_platform_no_cpu_arch(self):
  100. """
  101. Get os_arch if RPM package is not installed (inird image, for example) but cpu arch cannot be determined.
  102. :return:
  103. """
  104. assert rpm.get_osarch() == "ZX81"
  105. @patch("salt.utils.path.which", MagicMock(return_value=False))
  106. @patch("salt.utils.pkg.rpm.subprocess", MagicMock(return_value=False))
  107. @patch(
  108. "salt.utils.pkg.rpm.platform.uname",
  109. MagicMock(
  110. return_value=(
  111. "Sinclair BASIC",
  112. "motophone",
  113. "1982 Sinclair Research Ltd",
  114. "1.0",
  115. "",
  116. "",
  117. )
  118. ),
  119. )
  120. def test_get_osarch_by_platform_no_cpu_arch_no_machine(self):
  121. """
  122. Get os_arch if RPM package is not installed (inird image, for example)
  123. where both cpu arch and machine cannot be determined.
  124. :return:
  125. """
  126. assert rpm.get_osarch() == "unknown"