|
| 1 | +# Copyright (c) 2017 Cloudbase Solutions Srl |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | +# not use this file except in compliance with the License. You may obtain |
| 5 | +# a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | +# License for the specific language governing permissions and limitations |
| 13 | +# under the License. |
| 14 | + |
| 15 | +import importlib |
| 16 | +import unittest |
| 17 | + |
| 18 | +try: |
| 19 | + import unittest.mock as mock |
| 20 | +except ImportError: |
| 21 | + import mock |
| 22 | + |
| 23 | +from cloudbaseinit import conf as cloudbaseinit_conf |
| 24 | +from cloudbaseinit.plugins.common import base |
| 25 | +from cloudbaseinit.tests import testutils |
| 26 | + |
| 27 | +CONF = cloudbaseinit_conf.CONF |
| 28 | +MODPATH = "cloudbaseinit.plugins.windows.updates" |
| 29 | + |
| 30 | + |
| 31 | +class WindowsAutoUpdatesPluginTest(unittest.TestCase): |
| 32 | + |
| 33 | + def setUp(self): |
| 34 | + self.mock_win32com = mock.MagicMock() |
| 35 | + patcher = mock.patch.dict( |
| 36 | + "sys.modules", |
| 37 | + { |
| 38 | + "win32com": self.mock_win32com |
| 39 | + } |
| 40 | + ) |
| 41 | + patcher.start() |
| 42 | + self.addCleanup(patcher.stop) |
| 43 | + updates = importlib.import_module(MODPATH) |
| 44 | + self._updates_plugin = updates.WindowsAutoUpdatesPlugin() |
| 45 | + self.snatcher = testutils.LogSnatcher(MODPATH) |
| 46 | + |
| 47 | + @testutils.ConfPatcher("enable_automatic_updates", True) |
| 48 | + @mock.patch("cloudbaseinit.utils.windows.updates.set_automatic_updates") |
| 49 | + def test_execute(self, mock_set_updates): |
| 50 | + mock_service = mock.Mock() |
| 51 | + mock_shared_data = mock.Mock() |
| 52 | + mock_service.get_enable_automatic_updates.return_value = True |
| 53 | + |
| 54 | + expected_res = (base.PLUGIN_EXECUTION_DONE, False) |
| 55 | + expected_logs = ["Configuring automatic updates: %s" % True] |
| 56 | + with self.snatcher: |
| 57 | + res = self._updates_plugin.execute(mock_service, mock_shared_data) |
| 58 | + self.assertEqual(res, expected_res) |
| 59 | + self.assertEqual(self.snatcher.output, expected_logs) |
| 60 | + mock_service.get_enable_automatic_updates.assert_called_once_with() |
| 61 | + mock_set_updates.assert_called_once_with(True) |
| 62 | + |
| 63 | + def test_get_os_requirements(self): |
| 64 | + expected_res = ('win32', (5, 2)) |
| 65 | + requirements_res = self._updates_plugin.get_os_requirements() |
| 66 | + self.assertEqual(requirements_res, expected_res) |
0 commit comments