diff --git a/ironic/common/oci_registry.py b/ironic/common/oci_registry.py index af139cf097..c435cea32d 100644 --- a/ironic/common/oci_registry.py +++ b/ironic/common/oci_registry.py @@ -224,7 +224,7 @@ class RegistrySessionHelper(object): auth = None try: with open(CONF.oci.authentication_config, 'r') as auth_file: - auth_dict = json.loads(auth_file) + auth_dict = json.load(auth_file) except OSError as e: LOG.error('Failed to load pre-shared authentication token ' 'data: %s', e) diff --git a/ironic/tests/unit/common/test_oci_registry.py b/ironic/tests/unit/common/test_oci_registry.py index 8f17809c50..b1f0fa1a81 100644 --- a/ironic/tests/unit/common/test_oci_registry.py +++ b/ironic/tests/unit/common/test_oci_registry.py @@ -12,7 +12,6 @@ # License for the specific language governing permissions and limitations # under the License. -import builtins import hashlib import io import json @@ -866,38 +865,32 @@ class TestRegistrySessionHelper(base.TestCase): oci_registry.RegistrySessionHelper.get_token_from_config( 'foo.bar')) - @mock.patch.object(builtins, 'open', autospec=True) - def test_get_token_from_config(self, mock_open): + def test_get_token_from_config(self): CONF.set_override('authentication_config', '/tmp/foo', group='oci') - mock_file = mock.MagicMock(spec=io.BytesIO) - mock_file.__enter__.return_value = \ - """{"auths": {"foo.fqdn": {"auth": "secret"}}}""" - mock_open.return_value = mock_file - res = oci_registry.RegistrySessionHelper.get_token_from_config( - 'foo.fqdn') + read_data = """{"auths": {"foo.fqdn": {"auth": "secret"}}}""" + with mock.patch('builtins.open', mock.mock_open( + read_data=read_data)): + res = oci_registry.RegistrySessionHelper.get_token_from_config( + 'foo.fqdn') self.assertEqual('secret', res) - @mock.patch.object(builtins, 'open', autospec=True) - def test_get_token_from_config_no_match(self, mock_open): + def test_get_token_from_config_no_match(self): CONF.set_override('authentication_config', '/tmp/foo', group='oci') - mock_file = mock.MagicMock(spec=io.BytesIO) - mock_file.__enter__.return_value = \ - """{"auths": {"bar.fqdn": {}}}""" - mock_open.return_value = mock_file - res = oci_registry.RegistrySessionHelper.get_token_from_config( - 'foo.fqdn') + read_data = """{"auths": {"bar.fqdn": {}}}""" + with mock.patch('builtins.open', mock.mock_open( + read_data=read_data)): + res = oci_registry.RegistrySessionHelper.get_token_from_config( + 'foo.fqdn') self.assertIsNone(res) - @mock.patch.object(builtins, 'open', autospec=True) - def test_get_token_from_config_bad_file(self, mock_open): + def test_get_token_from_config_bad_file(self): CONF.set_override('authentication_config', '/tmp/foo', group='oci') - mock_file = mock.MagicMock(spec=io.BytesIO) - mock_file.__enter__.return_value = \ - """{"auths":...""" - mock_open.return_value = mock_file - res = oci_registry.RegistrySessionHelper.get_token_from_config( - 'foo.fqdn') + read_data = """{"auths":...""" + with mock.patch('builtins.open', mock.mock_open( + read_data=read_data)): + res = oci_registry.RegistrySessionHelper.get_token_from_config( + 'foo.fqdn') self.assertIsNone(res)