test_win_wua.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # -*- coding: utf-8 -*-
  2. """
  3. Test the win_wua execution module
  4. """
  5. # Import Python Libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Libs
  8. import salt.modules.win_wua as win_wua
  9. import salt.utils.platform
  10. import salt.utils.win_update
  11. # Import Salt Testing Libs
  12. from tests.support.mock import patch
  13. from tests.support.unit import TestCase, skipIf
  14. UPDATES_LIST = {
  15. "ca3bb521-a8ea-4e26-a563-2ad6e3108b9a": {"KBs": ["KB4481252"]},
  16. "07609d43-d518-4e77-856e-d1b316d1b8a8": {"KBs": ["KB925673"]},
  17. "fbaa5360-a440-49d8-a3b6-0c4fc7ecaa19": {"KBs": ["KB4481252"]},
  18. "a873372b-7a5c-443c-8022-cd59a550bef4": {"KBs": ["KB3193497"]},
  19. "14075cbe-822e-4004-963b-f50e08d45563": {"KBs": ["KB4540723"]},
  20. "d931e99c-4dda-4d39-9905-0f6a73f7195f": {"KBs": ["KB3193497"]},
  21. "afda9e11-44a0-4602-9e9b-423af11ecaed": {"KBs": ["KB4541329"]},
  22. "a0f997b1-1abe-4a46-941f-b37f732f9fbd": {"KBs": ["KB3193497"]},
  23. "eac02b09-d745-4891-b80f-400e0e5e4b6d": {"KBs": ["KB4052623"]},
  24. "0689e74b-54d1-4f55-a916-96e3c737db90": {"KBs": ["KB890830"]},
  25. }
  26. UPDATES_SUMMARY = {"Installed": 10}
  27. class Updates(object):
  28. @staticmethod
  29. def list():
  30. return UPDATES_LIST
  31. @staticmethod
  32. def summary():
  33. return UPDATES_SUMMARY
  34. @skipIf(not salt.utils.platform.is_windows(), "System is not Windows")
  35. class WinWuaInstalledTestCase(TestCase):
  36. """
  37. Test the functions in the win_wua.installed function
  38. """
  39. def test_installed(self):
  40. """
  41. Test installed function default
  42. """
  43. expected = UPDATES_LIST
  44. with patch("salt.utils.winapi.Com", autospec=True), patch(
  45. "win32com.client.Dispatch", autospec=True
  46. ), patch.object(
  47. salt.utils.win_update.WindowsUpdateAgent, "refresh", autospec=True
  48. ), patch.object(
  49. salt.utils.win_update, "Updates", autospec=True, return_value=Updates()
  50. ):
  51. result = win_wua.installed()
  52. self.assertDictEqual(result, expected)
  53. def test_installed_summary(self):
  54. """
  55. Test installed function with summary=True
  56. """
  57. expected = UPDATES_SUMMARY
  58. # Remove all updates that are not installed
  59. with patch("salt.utils.winapi.Com", autospec=True), patch(
  60. "win32com.client.Dispatch", autospec=True
  61. ), patch.object(
  62. salt.utils.win_update.WindowsUpdateAgent, "refresh", autospec=True
  63. ), patch.object(
  64. salt.utils.win_update, "Updates", autospec=True, return_value=Updates()
  65. ):
  66. result = win_wua.installed(summary=True)
  67. self.assertDictEqual(result, expected)
  68. def test_installed_kbs_only(self):
  69. """
  70. Test installed function with kbs_only=True
  71. """
  72. expected = set()
  73. for update in UPDATES_LIST:
  74. expected.update(UPDATES_LIST[update]["KBs"])
  75. expected = sorted(expected)
  76. # Remove all updates that are not installed
  77. with patch("salt.utils.winapi.Com", autospec=True), patch(
  78. "win32com.client.Dispatch", autospec=True
  79. ), patch.object(
  80. salt.utils.win_update.WindowsUpdateAgent, "refresh", autospec=True
  81. ), patch.object(
  82. salt.utils.win_update, "Updates", autospec=True, return_value=Updates()
  83. ):
  84. result = win_wua.installed(kbs_only=True)
  85. self.assertListEqual(result, expected)