Merge "Use unittest.mock instead of mock"

This commit is contained in:
Zuul 2021-12-16 10:22:05 +00:00 committed by Gerrit Code Review
commit 074bb1f26c
8 changed files with 38 additions and 14 deletions

View File

@ -1,5 +1,4 @@
- project: - project:
templates: templates:
- python35-charm-jobs
- openstack-python3-ussuri-jobs - openstack-python3-ussuri-jobs
- openstack-cover-jobs - openstack-cover-jobs

View File

@ -12,8 +12,8 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import mock from unittest import mock
from mock import patch from unittest.mock import patch
from test_utils import CharmTestCase from test_utils import CharmTestCase

View File

@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from mock import patch from unittest.mock import patch
import os import os
os.environ['JUJU_UNIT_NAME'] = 'keystone' os.environ['JUJU_UNIT_NAME'] = 'keystone'

View File

@ -16,7 +16,7 @@ import collections
import importlib import importlib
import os import os
from mock import patch, MagicMock, ANY from unittest.mock import patch, MagicMock, ANY
with patch('charmhelpers.contrib.openstack.' with patch('charmhelpers.contrib.openstack.'
'utils.snap_install_requested') as snap_install_requested: 'utils.snap_install_requested') as snap_install_requested:
snap_install_requested.return_value = False snap_install_requested.return_value = False
@ -549,39 +549,59 @@ class TestKeystoneContexts(CharmTestCase):
@patch.object(context, 'log') @patch.object(context, 'log')
def test__decode_password_security_compliance_string_pre_newton( def test__decode_password_security_compliance_string_pre_newton(
self, mock_log): self, mock_log):
self.log_message = None
def _mock_log(message, level=None):
self.log_message = message
mock_log.side_effect = _mock_log
self.os_release.return_value = 'mitaka' self.os_release.return_value = 'mitaka'
self.assertIsNone( self.assertIsNone(
context. context.
KeystoneContext. KeystoneContext.
_decode_password_security_compliance_string("")) _decode_password_security_compliance_string(""))
mock_log.assert_called_once_with(ANY, level='ERROR') mock_log.assert_called_once_with(ANY, level='ERROR')
self.assertIn("Newton", mock_log.call_args.args[0]) self.assertIn("Newton", self.log_message)
@patch.object(context, 'log') @patch.object(context, 'log')
def test__decode_password_security_compliance_string_invalid_yaml( def test__decode_password_security_compliance_string_invalid_yaml(
self, mock_log): self, mock_log):
self.log_message = None
def _mock_log(message, level=None):
self.log_message = message
mock_log.side_effect = _mock_log
self.os_release.return_value = 'ocata' self.os_release.return_value = 'ocata'
self.assertIsNone( self.assertIsNone(
context. context.
KeystoneContext. KeystoneContext.
_decode_password_security_compliance_string("hello: this: one")) _decode_password_security_compliance_string("hello: this: one"))
mock_log.assert_called_once_with(ANY, level='ERROR') mock_log.assert_called_once_with(ANY, level='ERROR')
self.assertIn("Invalid YAML", mock_log.call_args.args[0]) self.assertIn("Invalid YAML", self.log_message)
@patch.object(context, 'log') @patch.object(context, 'log')
def test__decode_password_security_compliance_string_yaml_not_dict( def test__decode_password_security_compliance_string_yaml_not_dict(
self, mock_log): self, mock_log):
self.log_message = None
def _mock_log(message, level=None):
self.log_message = message
mock_log.side_effect = _mock_log
self.os_release.return_value = 'pike' self.os_release.return_value = 'pike'
self.assertIsNone( self.assertIsNone(
context. context.
KeystoneContext. KeystoneContext.
_decode_password_security_compliance_string("hello")) _decode_password_security_compliance_string("hello"))
mock_log.assert_called_once_with(ANY, level='ERROR') mock_log.assert_called_once_with(ANY, level='ERROR')
self.assertIn("dictionary", mock_log.call_args.args[0]) self.assertIn("dictionary", self.log_message)
@patch.object(context, 'log') @patch.object(context, 'log')
def test__decode_password_security_compliance_string_invalid_key( def test__decode_password_security_compliance_string_invalid_key(
self, mock_log): self, mock_log):
self.log_message = None
def _mock_log(message, level=None):
self.log_message = message
mock_log.side_effect = _mock_log
self.os_release.return_value = 'queens' self.os_release.return_value = 'queens'
self.assertIsNone( self.assertIsNone(
context. context.
@ -589,11 +609,16 @@ class TestKeystoneContexts(CharmTestCase):
_decode_password_security_compliance_string( _decode_password_security_compliance_string(
"lockout_failure_attempts: 5\nlookout_duration: 180\n")) "lockout_failure_attempts: 5\nlookout_duration: 180\n"))
mock_log.assert_called_once_with(ANY, level='ERROR') mock_log.assert_called_once_with(ANY, level='ERROR')
self.assertIn("Invalid config key(s)", mock_log.call_args.args[0]) self.assertIn("Invalid config key(s)", self.log_message)
@patch.object(context, 'log') @patch.object(context, 'log')
def test__decode_password_security_compliance_string_invalid_type( def test__decode_password_security_compliance_string_invalid_type(
self, mock_log): self, mock_log):
self.log_message = None
def _mock_log(message, level=None):
self.log_message = message
mock_log.side_effect = _mock_log
self.os_release.return_value = 'rocky' self.os_release.return_value = 'rocky'
self.assertIsNone( self.assertIsNone(
context. context.
@ -601,7 +626,7 @@ class TestKeystoneContexts(CharmTestCase):
_decode_password_security_compliance_string( _decode_password_security_compliance_string(
"lockout_failure_attempts: hello")) "lockout_failure_attempts: hello"))
mock_log.assert_called_once_with(ANY, level='ERROR') mock_log.assert_called_once_with(ANY, level='ERROR')
self.assertIn("Invalid config value", mock_log.call_args.args[0]) self.assertIn("Invalid config value", self.log_message)
@patch.object(context, 'log') @patch.object(context, 'log')
def test__decode_password_security_compliance_string_valid( def test__decode_password_security_compliance_string_valid(

View File

@ -18,7 +18,7 @@ import sys
import charmhelpers.contrib.openstack.utils as os_utils import charmhelpers.contrib.openstack.utils as os_utils
from mock import call, patch, MagicMock, ANY from unittest.mock import call, patch, MagicMock, ANY
from test_utils import CharmTestCase from test_utils import CharmTestCase
# python-apt is not installed as part of test-requirements but is imported by # python-apt is not installed as part of test-requirements but is imported by

View File

@ -15,7 +15,7 @@
import builtins import builtins
import collections import collections
import copy import copy
from mock import ANY, patch, call, MagicMock, mock_open, Mock from unittest.mock import ANY, patch, call, MagicMock, mock_open, Mock
import json import json
import os import os
import subprocess import subprocess

View File

@ -14,7 +14,7 @@
import sys import sys
from mock import patch from unittest.mock import patch
from test_utils import CharmTestCase from test_utils import CharmTestCase

View File

@ -17,7 +17,7 @@ import os
import unittest import unittest
import yaml import yaml
from mock import patch from unittest.mock import patch
patch('charmhelpers.contrib.openstack.utils.set_os_workload_status').start() patch('charmhelpers.contrib.openstack.utils.set_os_workload_status').start()
patch('charmhelpers.core.hookenv.status_set').start() patch('charmhelpers.core.hookenv.status_set').start()