From 6c1dc5b82e8b40df98f2f1313eb92b534ab56812 Mon Sep 17 00:00:00 2001 From: Adrian Vladu Date: Thu, 30 May 2024 15:06:03 +0300 Subject: [PATCH] unit_tests: fix winrm listener asserts mock.Mock().method_name.return_value if not set specifically, it returns a mock, thus being equivalent to a logical True. Added a return value in either case, so that it is visible. Without the return value set in both cases, the unit test fail on Python 3.12. Also, the try-`finally` is run outside of the `with` context, thus moving the `assert_has_calls` after `with` checks. Change-Id: Ic33afb6d1403b1096e06406ffd6d36a969f823d3 Signed-off-by: Adrian Vladu --- .../tests/plugins/windows/test_winrmlistener.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/cloudbaseinit/tests/plugins/windows/test_winrmlistener.py b/cloudbaseinit/tests/plugins/windows/test_winrmlistener.py index 22092f01e..f14748617 100644 --- a/cloudbaseinit/tests/plugins/windows/test_winrmlistener.py +++ b/cloudbaseinit/tests/plugins/windows/test_winrmlistener.py @@ -102,9 +102,12 @@ def _test_check_uac_remote_restrictions(self, mock_SecurityUtils, mock_SecurityUtils.return_value = mock_security_utils mock_osutils = mock.Mock() mock_osutils.check_os_version.side_effect = [True, False] + mock_security_utils.get_uac_remote_restrictions.return_value = \ + disable_uac_remote_restrictions + expected_set_token_calls = [] if disable_uac_remote_restrictions: - mock_security_utils.get_uac_remote_restrictions.return_value = \ - disable_uac_remote_restrictions + expected_set_token_calls = [mock.call(enable=False), + mock.call(enable=True)] with self._winrmlistener._check_uac_remote_restrictions(mock_osutils): mock_SecurityUtils.assert_called_once_with() @@ -112,13 +115,9 @@ def _test_check_uac_remote_restrictions(self, mock_SecurityUtils, [mock.call(6, 0), mock.call(6, 2)]) (mock_security_utils.get_uac_remote_restrictions. assert_called_once_with()) - if disable_uac_remote_restrictions: - expected_set_token_calls = [mock.call(enable=True)] - else: - expected_set_token_calls = [mock.call(enable=False), - mock.call(enable=True)] - mock_security_utils.set_uac_remote_restrictions.has_calls( - expected_set_token_calls) + + mock_security_utils.set_uac_remote_restrictions.assert_has_calls( + expected_set_token_calls) def test_check_uac_remote_restrictions(self): self._test_check_uac_remote_restrictions(