test_openbsdpkg.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 Libs
  8. import salt.modules.openbsdpkg as openbsdpkg
  9. # Import Salt Testing Libs
  10. from tests.support.mixins import LoaderModuleMockMixin
  11. from tests.support.mock import MagicMock, call, patch
  12. from tests.support.unit import TestCase
  13. class ListPackages(object):
  14. def __init__(self):
  15. self._iteration = 0
  16. def __call__(self):
  17. pkg_lists = [
  18. {"vim": "7.4.1467p1-gtk2"},
  19. {"png": "1.6.23", "vim": "7.4.1467p1-gtk2", "ruby": "2.3.1p1"},
  20. ]
  21. pkgs = pkg_lists[self._iteration]
  22. self._iteration += 1
  23. return pkgs
  24. class OpenbsdpkgTestCase(TestCase, LoaderModuleMockMixin):
  25. """
  26. Test cases for salt.modules.openbsdpkg
  27. """
  28. def setup_loader_modules(self):
  29. return {openbsdpkg: {}}
  30. def test_list_pkgs(self):
  31. """
  32. Test for listing installed packages.
  33. """
  34. def _add_data(data, key, value):
  35. data[key] = value
  36. pkg_info_out = [
  37. "png-1.6.23",
  38. "vim-7.4.1467p1-gtk2", # vim--gtk2
  39. "ruby-2.3.1p1", # ruby%2.3
  40. ]
  41. run_stdout_mock = MagicMock(return_value="\n".join(pkg_info_out))
  42. patches = {
  43. "cmd.run_stdout": run_stdout_mock,
  44. "pkg_resource.add_pkg": _add_data,
  45. "pkg_resource.sort_pkglist": MagicMock(),
  46. "pkg_resource.stringify": MagicMock(),
  47. }
  48. with patch.dict(openbsdpkg.__salt__, patches):
  49. pkgs = openbsdpkg.list_pkgs()
  50. self.assertDictEqual(
  51. pkgs, {"png": "1.6.23", "vim--gtk2": "7.4.1467p1", "ruby": "2.3.1p1"}
  52. )
  53. run_stdout_mock.assert_called_once_with(
  54. "pkg_info -q -a", output_loglevel="trace"
  55. )
  56. def test_install_pkgs(self):
  57. """
  58. Test package install behavior for the following conditions:
  59. - only base package name is given ('png')
  60. - a flavor is specified ('vim--gtk2')
  61. - a branch is specified ('ruby%2.3')
  62. """
  63. parsed_targets = (
  64. {"vim--gtk2": None, "png": None, "ruby%2.3": None},
  65. "repository",
  66. )
  67. cmd_out = {
  68. "retcode": 0,
  69. "stdout": "quirks-2.241 signed on 2016-07-26T16:56:10Z",
  70. "stderr": "",
  71. }
  72. run_all_mock = MagicMock(return_value=cmd_out)
  73. patches = {
  74. "cmd.run_all": run_all_mock,
  75. "pkg_resource.parse_targets": MagicMock(return_value=parsed_targets),
  76. "pkg_resource.stringify": MagicMock(),
  77. "pkg_resource.sort_pkglist": MagicMock(),
  78. }
  79. with patch.dict(openbsdpkg.__salt__, patches):
  80. with patch("salt.modules.openbsdpkg.list_pkgs", ListPackages()):
  81. added = openbsdpkg.install()
  82. expected = {
  83. "png": {"new": "1.6.23", "old": ""},
  84. "ruby": {"new": "2.3.1p1", "old": ""},
  85. }
  86. self.assertDictEqual(added, expected)
  87. expected_calls = [
  88. call("pkg_add -x -I png--%", output_loglevel="trace", python_shell=False),
  89. call(
  90. "pkg_add -x -I ruby--%2.3", output_loglevel="trace", python_shell=False
  91. ),
  92. call(
  93. "pkg_add -x -I vim--gtk2%", output_loglevel="trace", python_shell=False
  94. ),
  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)