test_rpm_lowpkg.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
  4. '''
  5. # Import Python Libs
  6. from __future__ import absolute_import
  7. # Import Salt Testing Libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.unit import TestCase
  10. from tests.support.mock import (
  11. MagicMock,
  12. patch,
  13. )
  14. # Import Salt Libs
  15. import salt.modules.rpm_lowpkg as rpm
  16. class RpmTestCase(TestCase, LoaderModuleMockMixin):
  17. '''
  18. Test cases for salt.modules.rpm
  19. '''
  20. def setup_loader_modules(self):
  21. return {rpm: {'rpm': MagicMock(return_value=MagicMock)}}
  22. # 'list_pkgs' function tests: 1
  23. def test_list_pkgs(self):
  24. '''
  25. Test if it list the packages currently installed in a dict
  26. '''
  27. mock = MagicMock(return_value='')
  28. with patch.dict(rpm.__salt__, {'cmd.run': mock}):
  29. self.assertDictEqual(rpm.list_pkgs(), {})
  30. # 'verify' function tests: 1
  31. def test_verify(self):
  32. '''
  33. Test if it runs an rpm -Va on a system,
  34. and returns the results in a dict
  35. '''
  36. mock = MagicMock(return_value={'stdout': '',
  37. 'stderr': '',
  38. 'retcode': 0,
  39. 'pid': 12345})
  40. with patch.dict(rpm.__salt__, {'cmd.run_all': mock}):
  41. self.assertDictEqual(rpm.verify('httpd'), {})
  42. # 'file_list' function tests: 1
  43. def test_file_list(self):
  44. '''
  45. Test if it list the files that belong to a package.
  46. '''
  47. mock = MagicMock(return_value='')
  48. with patch.dict(rpm.__salt__, {'cmd.run': mock}):
  49. self.assertDictEqual(rpm.file_list('httpd'),
  50. {'errors': [], 'files': []})
  51. # 'file_dict' function tests: 1
  52. def test_file_dict(self):
  53. '''
  54. Test if it list the files that belong to a package
  55. '''
  56. mock = MagicMock(return_value='')
  57. with patch.dict(rpm.__salt__, {'cmd.run': mock}):
  58. self.assertDictEqual(rpm.file_dict('httpd'),
  59. {'errors': [], 'packages': {}})
  60. # 'owner' function tests: 1
  61. def test_owner(self):
  62. '''
  63. Test if it return the name of the package that owns the file.
  64. '''
  65. self.assertEqual(rpm.owner(), '')
  66. ret = 'file /usr/bin/salt-jenkins-build is not owned by any package'
  67. mock = MagicMock(return_value=ret)
  68. with patch.dict(rpm.__salt__, {'cmd.run_stdout': mock}):
  69. self.assertEqual(rpm.owner('/usr/bin/salt-jenkins-build'), '')
  70. ret = {'/usr/bin/vim': 'vim-enhanced-7.4.160-1.e17.x86_64',
  71. '/usr/bin/python': 'python-2.7.5-16.e17.x86_64'}
  72. mock = MagicMock(side_effect=['python-2.7.5-16.e17.x86_64',
  73. 'vim-enhanced-7.4.160-1.e17.x86_64'])
  74. with patch.dict(rpm.__salt__, {'cmd.run_stdout': mock}):
  75. self.assertDictEqual(rpm.owner('/usr/bin/python', '/usr/bin/vim'),
  76. ret)
  77. # 'checksum' function tests: 1
  78. def test_checksum(self):
  79. '''
  80. Test if checksum validate as expected
  81. '''
  82. ret = {
  83. "file1.rpm": True,
  84. "file2.rpm": False,
  85. "file3.rpm": False,
  86. }
  87. mock = MagicMock(side_effect=[True, 0, True, 1, False, 0])
  88. with patch.dict(rpm.__salt__, {'file.file_exists': mock, 'cmd.retcode': mock}):
  89. self.assertDictEqual(rpm.checksum("file1.rpm", "file2.rpm", "file3.rpm"), ret)
  90. def test_version_cmp_rpm(self):
  91. '''
  92. Test package version is called RPM version if RPM-Python is installed
  93. :return:
  94. '''
  95. with patch('salt.modules.rpm_lowpkg.rpm.labelCompare', MagicMock(return_value=0)), \
  96. patch('salt.modules.rpm_lowpkg.HAS_RPM', True):
  97. self.assertEqual(0, rpm.version_cmp('1', '2')) # mock returns 0, which means RPM was called
  98. def test_version_cmp_fallback(self):
  99. '''
  100. Test package version is called RPM version if RPM-Python is installed
  101. :return:
  102. '''
  103. with patch('salt.modules.rpm_lowpkg.rpm.labelCompare', MagicMock(return_value=0)), \
  104. patch('salt.modules.rpm_lowpkg.HAS_RPM', False):
  105. self.assertEqual(-1, rpm.version_cmp('1', '2')) # mock returns -1, a python implementation was called