oci: fix auth config loading

When testing, I guess I didn't actually test loading the token
from config, and relied upon mocking. However, turns out the code
used the wrong load command (loads, versus load), which passed
unit testing, but didn't work when I gave the config a try.

Fixes the call and the testing so it properly passes now.

Change-Id: I4750a82ea07bc803600fddebd16f14a201ae406e
This commit is contained in:
Julia Kreger 2025-02-12 11:46:09 -08:00
parent 5262536417
commit 6aaa84f11e
2 changed files with 19 additions and 26 deletions

View File

@ -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)

View File

@ -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)