Merge "Improve invalidate uuid tests"

This commit is contained in:
Zuul 2024-06-29 00:02:06 +00:00 committed by Gerrit Code Review
commit d68fcfdbe8
3 changed files with 40 additions and 26 deletions

View File

@ -15,6 +15,7 @@
# under the License.
from oslo_log import log as logging
import oslo_messaging as messaging
from oslo_utils import uuidutils
import stevedore.exception
import yaml
@ -27,7 +28,6 @@ from designate import objects
from designate.objects.adapters import DesignateAdapter
from designate import policy
from designate import rpc
from designate import utils
CONF = designate.conf.CONF
@ -74,7 +74,7 @@ class PoolCommands(base.Commands):
self.output_message.append('-------------------')
try:
if not utils.is_uuid_like(pool_id):
if not uuidutils.is_uuid_like(pool_id):
self.output_message.append('Not a valid uuid: %s' % pool_id)
raise SystemExit(1)
@ -164,7 +164,7 @@ class PoolCommands(base.Commands):
def _get_pool(self, pool_data):
if 'id' in pool_data:
pool_id = pool_data['id']
if not utils.is_uuid_like(pool_id):
if not uuidutils.is_uuid_like(pool_id):
self.output_message.append('Not a valid uuid: %s' % pool_id)
raise SystemExit(1)

View File

@ -33,6 +33,37 @@ class TestUtils(oslotest.base.BaseTestCase):
self.useFixture(cfg_fixture.Config(CONF))
self.useFixture(self.stdlog)
def test_validate_uuid(self):
@utils.validate_uuid('zone_id')
def validate_uuid(cls, zone_id):
return True
self.assertTrue(
validate_uuid(None, '6a8caf9d-679e-4fc1-80e9-13496f6a783f')
)
def test_validate_invalid_uuid(self):
@utils.validate_uuid('zone_id')
def validate_uuid(cls, zone_id):
return True
self.assertRaisesRegex(
exceptions.InvalidUUID,
'Invalid UUID zone_id: 62f89e5f088c7',
validate_uuid, None, '62f89e5f088c7'
)
def test_validate_uuid_no_arguments(self):
@utils.validate_uuid('zone_id')
def validate_uuid(cls):
return
self.assertRaises(exceptions.NotFound, validate_uuid)
def test_invalid_uuid_no_argument_provided(self):
@utils.validate_uuid('zone_id')
def validate_uuid(cls):
return
self.assertRaises(exceptions.NotFound, validate_uuid, None)
@mock.patch('os.path.exists')
@mock.patch('os.path.abspath')
def test_find_config(self, mock_abspath, mock_path_exists):
@ -213,14 +244,6 @@ class TestUtils(oslotest.base.BaseTestCase):
self.assertEqual('Hello World', result)
def test_is_uuid_like(self):
self.assertTrue(
utils.is_uuid_like('ce9fcd6b-d546-4397-8a49-8ceaec37cb64')
)
def test_is_not_uuid_like(self):
self.assertFalse(utils.is_uuid_like('678'))
def test_split_host_port(self):
host, port = utils.split_host_port('abc:25')
self.assertEqual(('abc', 25), (host, port))

View File

@ -139,16 +139,6 @@ def generate_uuid():
return uuidutils.generate_uuid(dashed=True)
def is_uuid_like(val):
"""Returns validation of a value as a UUID.
For our purposes, a UUID is a canonical form string:
aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa
"""
return uuidutils.is_uuid_like(val)
def validate_uuid(*check):
"""
A wrapper to ensure that API controller methods arguments are valid UUID's.
@ -167,7 +157,7 @@ def validate_uuid(*check):
# /v2/zones/<UUID - valid or invalid>/invalid
# get, patch and delete return a 404, but Pecan returns a 405
# for a POST at the same URL
if (len(arg_spec) != len(args)):
if len(arg_spec) != len(args):
raise exceptions.NotFound()
# Ensure that we have non-empty parameters in the cases where we
@ -175,14 +165,15 @@ def validate_uuid(*check):
# This is for URLs like /v2/zones/nameservers
# Ideally Pecan should be handling these cases, but until then
# we handle those cases here.
if (len(args) <= len(check)):
if len(args) <= len(check):
raise exceptions.NotFound()
for name in check:
pos = arg_spec.index(name)
if not is_uuid_like(args[pos]):
msg = f'Invalid UUID {name}: {args[pos]}'
raise exceptions.InvalidUUID(msg)
if not uuidutils.is_uuid_like(args[pos]):
raise exceptions.InvalidUUID(
f'Invalid UUID {name}: {args[pos]}'
)
return f(*args, **kwargs)
return functools.wraps(f)(wrapper)
return inner