Ensure endpoint_id is set

The endpoint_id option is required because it is the key to look up
the target endpoint from Keystone catalog. This ensures more sensible
error is shown in case endpoint id is not set, instead of trying to
look up endpoint by 'None'.

Change-Id: I43859dec08040130e4e5fa6ad65b74be21489aff
This commit is contained in:
Takashi Kajinami 2023-10-06 10:58:44 +09:00
parent f28b01e652
commit ba8b9aba0b
3 changed files with 33 additions and 2 deletions

View File

@ -264,6 +264,8 @@ class _EnforcerUtils(object):
# get and cache endpoint info # get and cache endpoint info
endpoint_id = CONF.oslo_limit.endpoint_id endpoint_id = CONF.oslo_limit.endpoint_id
if not endpoint_id:
raise ValueError("endpoint_id is not configured")
self._endpoint = self.connection.get_endpoint(endpoint_id) self._endpoint = self.connection.get_endpoint(endpoint_id)
if not self._endpoint: if not self._endpoint:
raise ValueError("can't find endpoint for %s" % endpoint_id) raise ValueError("can't find endpoint for %s" % endpoint_id)

View File

@ -13,15 +13,28 @@
from oslotest import base from oslotest import base
from oslo_config import cfg
from oslo_config import fixture as config_fixture
from oslo_limit import exception from oslo_limit import exception
from oslo_limit import fixture from oslo_limit import fixture
from oslo_limit import limit from oslo_limit import limit
from oslo_limit import opts
CONF = cfg.CONF
class TestFixture(base.BaseTestCase): class TestFixture(base.BaseTestCase):
def setUp(self): def setUp(self):
super(TestFixture, self).setUp() super(TestFixture, self).setUp()
self.config_fixture = self.useFixture(config_fixture.Config(CONF))
self.config_fixture.config(
group='oslo_limit',
endpoint_id='ENDPOINT_ID'
)
opts.register_opts(CONF)
# Set up some default projects, registered limits, # Set up some default projects, registered limits,
# and project limits # and project limits
reglimits = {'widgets': 100, reglimits = {'widgets': 100,

View File

@ -46,6 +46,10 @@ class TestEnforcer(base.BaseTestCase):
group='oslo_limit', group='oslo_limit',
auth_type='password' auth_type='password'
) )
self.config_fixture.config(
group='oslo_limit',
endpoint_id='ENDPOINT_ID'
)
opts.register_opts(CONF) opts.register_opts(CONF)
self.config_fixture.config( self.config_fixture.config(
group='oslo_limit', group='oslo_limit',
@ -223,6 +227,12 @@ class TestEnforcer(base.BaseTestCase):
class TestFlatEnforcer(base.BaseTestCase): class TestFlatEnforcer(base.BaseTestCase):
def setUp(self): def setUp(self):
super(TestFlatEnforcer, self).setUp() super(TestFlatEnforcer, self).setUp()
self.config_fixture = self.useFixture(config_fixture.Config(CONF))
self.config_fixture.config(
group='oslo_limit',
endpoint_id='ENDPOINT_ID'
)
opts.register_opts(CONF)
self.mock_conn = mock.MagicMock() self.mock_conn = mock.MagicMock()
limit._SDK_CONNECTION = self.mock_conn limit._SDK_CONNECTION = self.mock_conn
@ -259,7 +269,7 @@ class TestFlatEnforcer(base.BaseTestCase):
enforcer = limit._FlatEnforcer(mock_usage) enforcer = limit._FlatEnforcer(mock_usage)
enforcer.enforce(project_id, deltas) enforcer.enforce(project_id, deltas)
self.mock_conn.get_endpoint.assert_called_once_with(None) self.mock_conn.get_endpoint.assert_called_once_with('ENDPOINT_ID')
mock_get_limits.assert_called_once_with(project_id, ["a", "b"]) mock_get_limits.assert_called_once_with(project_id, ["a", "b"])
mock_usage.assert_called_once_with(project_id, ["a", "b"]) mock_usage.assert_called_once_with(project_id, ["a", "b"])
@ -310,6 +320,12 @@ class TestFlatEnforcer(base.BaseTestCase):
class TestEnforcerUtils(base.BaseTestCase): class TestEnforcerUtils(base.BaseTestCase):
def setUp(self): def setUp(self):
super(TestEnforcerUtils, self).setUp() super(TestEnforcerUtils, self).setUp()
self.config_fixture = self.useFixture(config_fixture.Config(CONF))
self.config_fixture.config(
group='oslo_limit',
endpoint_id='ENDPOINT_ID'
)
opts.register_opts(CONF)
self.mock_conn = mock.MagicMock() self.mock_conn = mock.MagicMock()
limit._SDK_CONNECTION = self.mock_conn limit._SDK_CONNECTION = self.mock_conn
@ -320,7 +336,7 @@ class TestEnforcerUtils(base.BaseTestCase):
utils = limit._EnforcerUtils() utils = limit._EnforcerUtils()
self.assertEqual(fake_endpoint, utils._endpoint) self.assertEqual(fake_endpoint, utils._endpoint)
self.mock_conn.get_endpoint.assert_called_once_with(None) self.mock_conn.get_endpoint.assert_called_once_with('ENDPOINT_ID')
def test_get_registered_limit_empty(self): def test_get_registered_limit_empty(self):
self.mock_conn.registered_limits.return_value = iter([]) self.mock_conn.registered_limits.return_value = iter([])