1
0

test_pkg.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import, unicode_literals, print_function
  3. from tests.support.unit import TestCase
  4. from tests.support.mock import MagicMock, patch
  5. import salt.utils.pkg
  6. from salt.utils.pkg import rpm
  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('salt.utils.pkg.rpm.platform.uname', MagicMock(
  66. return_value=('Sinclair BASIC', 'motophone', '1982 Sinclair Research Ltd', '1.0', 'ZX81', 'Z80')))
  67. def test_get_osarch_by_platform(self):
  68. '''
  69. Get os_arch if RPM package is not installed (inird image, for example).
  70. :return:
  71. '''
  72. assert rpm.get_osarch() == 'Z80'
  73. @patch('salt.utils.path.which', MagicMock(return_value=False))
  74. @patch('salt.utils.pkg.rpm.subprocess', MagicMock(return_value=False))
  75. @patch('salt.utils.pkg.rpm.platform.uname', MagicMock(
  76. return_value=('Sinclair BASIC', 'motophone', '1982 Sinclair Research Ltd', '1.0', 'ZX81', '')))
  77. def test_get_osarch_by_platform_no_cpu_arch(self):
  78. '''
  79. Get os_arch if RPM package is not installed (inird image, for example) but cpu arch cannot be determined.
  80. :return:
  81. '''
  82. assert rpm.get_osarch() == 'ZX81'
  83. @patch('salt.utils.path.which', MagicMock(return_value=False))
  84. @patch('salt.utils.pkg.rpm.subprocess', MagicMock(return_value=False))
  85. @patch('salt.utils.pkg.rpm.platform.uname', MagicMock(
  86. return_value=('Sinclair BASIC', 'motophone', '1982 Sinclair Research Ltd', '1.0', '', '')))
  87. def test_get_osarch_by_platform_no_cpu_arch_no_machine(self):
  88. '''
  89. Get os_arch if RPM package is not installed (inird image, for example)
  90. where both cpu arch and machine cannot be determined.
  91. :return:
  92. '''
  93. assert rpm.get_osarch() == 'unknown'