test_openbsdpkg.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Eric Radman <ericshane@eradman.com>
  4. '''
  5. # Import Python Libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  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. call,
  14. )
  15. # Import Salt Libs
  16. import salt.modules.openbsdpkg as openbsdpkg
  17. class ListPackages(object):
  18. def __init__(self):
  19. self._iteration = 0
  20. def __call__(self):
  21. pkg_lists = [
  22. {'vim': '7.4.1467p1-gtk2'},
  23. {'png': '1.6.23', 'vim': '7.4.1467p1-gtk2', 'ruby': '2.3.1p1'}
  24. ]
  25. pkgs = pkg_lists[self._iteration]
  26. self._iteration += 1
  27. return pkgs
  28. class OpenbsdpkgTestCase(TestCase, LoaderModuleMockMixin):
  29. '''
  30. Test cases for salt.modules.openbsdpkg
  31. '''
  32. def setup_loader_modules(self):
  33. return {openbsdpkg: {}}
  34. def test_list_pkgs(self):
  35. '''
  36. Test for listing installed packages.
  37. '''
  38. def _add_data(data, key, value):
  39. data[key] = value
  40. pkg_info_out = [
  41. 'png-1.6.23',
  42. 'vim-7.4.1467p1-gtk2', # vim--gtk2
  43. 'ruby-2.3.1p1' # ruby%2.3
  44. ]
  45. run_stdout_mock = MagicMock(return_value='\n'.join(pkg_info_out))
  46. patches = {
  47. 'cmd.run_stdout': run_stdout_mock,
  48. 'pkg_resource.add_pkg': _add_data,
  49. 'pkg_resource.sort_pkglist': MagicMock(),
  50. 'pkg_resource.stringify': MagicMock(),
  51. }
  52. with patch.dict(openbsdpkg.__salt__, patches):
  53. pkgs = openbsdpkg.list_pkgs()
  54. self.assertDictEqual(pkgs, {
  55. 'png': '1.6.23',
  56. 'vim--gtk2': '7.4.1467p1',
  57. 'ruby': '2.3.1p1'})
  58. run_stdout_mock.assert_called_once_with('pkg_info -q -a',
  59. output_loglevel='trace')
  60. def test_install_pkgs(self):
  61. '''
  62. Test package install behavior for the following conditions:
  63. - only base package name is given ('png')
  64. - a flavor is specified ('vim--gtk2')
  65. - a branch is specified ('ruby%2.3')
  66. '''
  67. parsed_targets = (
  68. {'vim--gtk2': None, 'png': None, 'ruby%2.3': None},
  69. "repository"
  70. )
  71. cmd_out = {
  72. 'retcode': 0,
  73. 'stdout': 'quirks-2.241 signed on 2016-07-26T16:56:10Z',
  74. 'stderr': ''
  75. }
  76. run_all_mock = MagicMock(return_value=cmd_out)
  77. patches = {
  78. 'cmd.run_all': run_all_mock,
  79. 'pkg_resource.parse_targets': MagicMock(return_value=parsed_targets),
  80. 'pkg_resource.stringify': MagicMock(),
  81. 'pkg_resource.sort_pkglist': MagicMock(),
  82. }
  83. with patch.dict(openbsdpkg.__salt__, patches):
  84. with patch('salt.modules.openbsdpkg.list_pkgs', ListPackages()):
  85. added = openbsdpkg.install()
  86. expected = {
  87. 'png': {'new': '1.6.23', 'old': ''},
  88. 'ruby': {'new': '2.3.1p1', 'old': ''}
  89. }
  90. self.assertDictEqual(added, expected)
  91. expected_calls = [
  92. call('pkg_add -x -I png--%', output_loglevel='trace', python_shell=False),
  93. call('pkg_add -x -I ruby--%2.3', output_loglevel='trace', python_shell=False),
  94. call('pkg_add -x -I vim--gtk2%', output_loglevel='trace', python_shell=False),
  95. ]
  96. run_all_mock.assert_has_calls(expected_calls, any_order=True)
  97. self.assertEqual(run_all_mock.call_count, 3)
  98. def test_upgrade_available(self):
  99. '''
  100. Test upgrade_available when an update is available.
  101. '''
  102. ret = MagicMock(return_value='5.4.2p0')
  103. with patch('salt.modules.openbsdpkg.latest_version', ret):
  104. self.assertTrue(openbsdpkg.upgrade_available('zsh'))
  105. def test_upgrade_not_available(self):
  106. '''
  107. Test upgrade_available when an update is not available.
  108. '''
  109. ret = MagicMock(return_value='')
  110. with patch('salt.modules.openbsdpkg.latest_version', ret):
  111. self.assertFalse(openbsdpkg.upgrade_available('zsh'))
  112. def test_upgrade(self):
  113. '''
  114. Test upgrading packages.
  115. '''
  116. ret = {}
  117. pkg_add_u_stdout = [
  118. 'quirks-2.402 signed on 2018-01-02T16:30:59Z',
  119. 'Read shared items: ok'
  120. ]
  121. ret['stdout'] = '\n'.join(pkg_add_u_stdout)
  122. ret['retcode'] = 0
  123. run_all_mock = MagicMock(return_value=ret)
  124. with patch.dict(openbsdpkg.__salt__, {'cmd.run_all': run_all_mock}):
  125. with patch('salt.modules.openbsdpkg.list_pkgs', ListPackages()):
  126. upgraded = openbsdpkg.upgrade()
  127. expected = {
  128. 'png': {'new': '1.6.23', 'old': ''},
  129. 'ruby': {'new': '2.3.1p1', 'old': ''}
  130. }
  131. self.assertDictEqual(upgraded, expected)