"Fossies" - the Fresh Open Source Software Archive

Member "salt-3002.2/tests/unit/modules/test_win_wua.py" (18 Nov 2020, 8842 Bytes) of package /linux/misc/salt-3002.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "test_win_wua.py": 3002.1_vs_3002.2.

    1 """
    2 Test the win_wua execution module
    3 """
    4 import salt.modules.win_wua as win_wua
    5 import salt.utils.platform
    6 import salt.utils.win_update
    7 from tests.support.mock import MagicMock, patch
    8 from tests.support.unit import TestCase, skipIf
    9 
   10 UPDATES_LIST = {
   11     "ca3bb521-a8ea-4e26-a563-2ad6e3108b9a": {"KBs": ["KB4481252"]},
   12     "07609d43-d518-4e77-856e-d1b316d1b8a8": {"KBs": ["KB925673"]},
   13     "fbaa5360-a440-49d8-a3b6-0c4fc7ecaa19": {"KBs": ["KB4481252"]},
   14     "a873372b-7a5c-443c-8022-cd59a550bef4": {"KBs": ["KB3193497"]},
   15     "14075cbe-822e-4004-963b-f50e08d45563": {"KBs": ["KB4540723"]},
   16     "d931e99c-4dda-4d39-9905-0f6a73f7195f": {"KBs": ["KB3193497"]},
   17     "afda9e11-44a0-4602-9e9b-423af11ecaed": {"KBs": ["KB4541329"]},
   18     "a0f997b1-1abe-4a46-941f-b37f732f9fbd": {"KBs": ["KB3193497"]},
   19     "eac02b09-d745-4891-b80f-400e0e5e4b6d": {"KBs": ["KB4052623"]},
   20     "0689e74b-54d1-4f55-a916-96e3c737db90": {"KBs": ["KB890830"]},
   21 }
   22 UPDATES_SUMMARY = {"Installed": 10}
   23 
   24 
   25 class Updates:
   26     @staticmethod
   27     def list():
   28         return UPDATES_LIST
   29 
   30     @staticmethod
   31     def summary():
   32         return UPDATES_SUMMARY
   33 
   34 
   35 @skipIf(not salt.utils.platform.is_windows(), "System is not Windows")
   36 class WinWuaInstalledTestCase(TestCase):
   37     """
   38     Test the functions in the win_wua.installed function
   39     """
   40 
   41     service_auto = {"StartType": "Auto"}
   42     service_disabled = {"StartType": "Disabled"}
   43     service_manual = {"StartType": "Manual"}
   44 
   45     def test__virtual__not_windows(self):
   46         """
   47         Test __virtual__ function on Non-Windows
   48         """
   49         with patch("salt.utils.platform.is_windows", autospec=True, return_value=False):
   50             expected = (False, "WUA: Only available on Windows systems")
   51             result = win_wua.__virtual__()
   52             self.assertEqual(expected, result)
   53 
   54     def test__virtual__missing_pywin32(self):
   55         """
   56         Test __virtual__ function when pywin32 is not installed
   57         """
   58         with patch("salt.modules.win_wua.HAS_PYWIN32", False):
   59             expected = (False, "WUA: Requires PyWin32 libraries")
   60             result = win_wua.__virtual__()
   61             self.assertEqual(expected, result)
   62 
   63     def test__virtual__wuauserv_disabled(self):
   64         """
   65         Test __virtual__ function when the wuauserv service is disabled
   66         """
   67         mock_service_info = MagicMock(
   68             side_effect=[
   69                 self.service_disabled,  # wuauserv
   70                 self.service_auto,  # msiserver
   71                 self.service_auto,  # BITS
   72                 self.service_auto,  # CryptSvc
   73                 self.service_auto,  # TrustedInstaller
   74             ]
   75         )
   76         with patch("salt.utils.win_service.info", mock_service_info):
   77             expected = (
   78                 False,
   79                 "WUA: The Windows Update service (wuauserv) must not be disabled",
   80             )
   81             result = win_wua.__virtual__()
   82             self.assertEqual(expected, result)
   83 
   84     def test__virtual__msiserver_disabled(self):
   85         """
   86         Test __virtual__ function when the msiserver service is disabled
   87         """
   88         mock_service_info = MagicMock(
   89             side_effect=[
   90                 self.service_auto,  # wuauserv
   91                 self.service_disabled,  # msiserver
   92                 self.service_auto,  # BITS
   93                 self.service_auto,  # CryptSvc
   94                 self.service_auto,  # TrustedInstaller
   95             ]
   96         )
   97         with patch("salt.utils.win_service.info", mock_service_info):
   98             expected = (
   99                 False,
  100                 "WUA: The Windows Installer service (msiserver) must not be disabled",
  101             )
  102             result = win_wua.__virtual__()
  103             self.assertEqual(expected, result)
  104 
  105     def test__virtual__BITS_disabled(self):
  106         """
  107         Test __virtual__ function when the BITS service is disabled
  108         """
  109         mock_service_info = MagicMock(
  110             side_effect=[
  111                 self.service_auto,  # wuauserv
  112                 self.service_auto,  # msiserver
  113                 self.service_disabled,  # BITS
  114                 self.service_auto,  # CryptSvc
  115                 self.service_auto,  # TrustedInstaller
  116             ]
  117         )
  118         with patch("salt.utils.win_service.info", mock_service_info):
  119             expected = (
  120                 False,
  121                 "WUA: The Background Intelligent Transfer service (bits) must not be disabled",
  122             )
  123             result = win_wua.__virtual__()
  124             self.assertEqual(expected, result)
  125 
  126     def test__virtual__BITS_manual(self):
  127         """
  128         Test __virtual__ function when the BITS service is set to manual
  129         Should not disable the module (__virtual__ should return True)
  130         """
  131         mock_service_info = MagicMock(
  132             side_effect=[
  133                 self.service_auto,  # wuauserv
  134                 self.service_auto,  # msiserver
  135                 self.service_manual,  # BITS
  136                 self.service_auto,  # CryptSvc
  137                 self.service_auto,  # TrustedInstaller
  138             ]
  139         )
  140         with patch("salt.utils.win_service.info", mock_service_info):
  141             expected = True
  142             result = win_wua.__virtual__()
  143             self.assertEqual(expected, result)
  144 
  145     def test__virtual__CryptSvc_disabled(self):
  146         """
  147         Test __virtual__ function when the CryptSvc service is disabled
  148         """
  149         mock_service_info = MagicMock(
  150             side_effect=[
  151                 self.service_auto,  # wuauserv
  152                 self.service_auto,  # msiserver
  153                 self.service_auto,  # BITS
  154                 self.service_disabled,  # CryptSvc
  155                 self.service_auto,  # TrustedInstaller
  156             ]
  157         )
  158         with patch("salt.utils.win_service.info", mock_service_info):
  159             expected = (
  160                 False,
  161                 "WUA: The Cryptographic Services service (CryptSvc) must not be disabled",
  162             )
  163             result = win_wua.__virtual__()
  164             self.assertEqual(expected, result)
  165 
  166     def test__virtual__TrustedInstaller_disabled(self):
  167         """
  168         Test __virtual__ function when the TrustedInstaller service is disabled
  169         """
  170         mock_service_info = MagicMock(
  171             side_effect=[
  172                 self.service_auto,  # wuauserv
  173                 self.service_auto,  # msiserver
  174                 self.service_auto,  # BITS
  175                 self.service_auto,  # CryptSvc
  176                 self.service_disabled,  # TrustedInstaller
  177             ]
  178         )
  179         with patch("salt.utils.win_service.info", mock_service_info):
  180             expected = (
  181                 False,
  182                 "WUA: The Windows Module Installer service (TrustedInstaller) must not be disabled",
  183             )
  184             result = win_wua.__virtual__()
  185             self.assertEqual(expected, result)
  186 
  187     def test_installed(self):
  188         """
  189         Test installed function default
  190         """
  191         expected = UPDATES_LIST
  192         with patch("salt.utils.winapi.Com", autospec=True), patch(
  193             "win32com.client.Dispatch", autospec=True
  194         ), patch.object(
  195             salt.utils.win_update.WindowsUpdateAgent, "refresh", autospec=True
  196         ), patch.object(
  197             salt.utils.win_update, "Updates", autospec=True, return_value=Updates()
  198         ):
  199             result = win_wua.installed()
  200             self.assertDictEqual(result, expected)
  201 
  202     def test_installed_summary(self):
  203         """
  204         Test installed function with summary=True
  205         """
  206         expected = UPDATES_SUMMARY
  207         # Remove all updates that are not installed
  208         with patch("salt.utils.winapi.Com", autospec=True), patch(
  209             "win32com.client.Dispatch", autospec=True
  210         ), patch.object(
  211             salt.utils.win_update.WindowsUpdateAgent, "refresh", autospec=True
  212         ), patch.object(
  213             salt.utils.win_update, "Updates", autospec=True, return_value=Updates()
  214         ):
  215             result = win_wua.installed(summary=True)
  216             self.assertDictEqual(result, expected)
  217 
  218     def test_installed_kbs_only(self):
  219         """
  220         Test installed function with kbs_only=True
  221         """
  222         expected = set()
  223         for update in UPDATES_LIST:
  224             expected.update(UPDATES_LIST[update]["KBs"])
  225         expected = sorted(expected)
  226         # Remove all updates that are not installed
  227         with patch("salt.utils.winapi.Com", autospec=True), patch(
  228             "win32com.client.Dispatch", autospec=True
  229         ), patch.object(
  230             salt.utils.win_update.WindowsUpdateAgent, "refresh", autospec=True
  231         ), patch.object(
  232             salt.utils.win_update, "Updates", autospec=True, return_value=Updates()
  233         ):
  234             result = win_wua.installed(kbs_only=True)
  235             self.assertListEqual(result, expected)