Improve invalidate uuid tests
- Removed the internal is_uuid_like function and replaced with oslo implementation directly instead. Change-Id: I5e2e18176a1d3cad06dfedd1972f659b6d73ad95
This commit is contained in:
@@ -15,6 +15,7 @@
|
|||||||
# under the License.
|
# under the License.
|
||||||
from oslo_log import log as logging
|
from oslo_log import log as logging
|
||||||
import oslo_messaging as messaging
|
import oslo_messaging as messaging
|
||||||
|
from oslo_utils import uuidutils
|
||||||
import stevedore.exception
|
import stevedore.exception
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
@@ -27,7 +28,6 @@ from designate import objects
|
|||||||
from designate.objects.adapters import DesignateAdapter
|
from designate.objects.adapters import DesignateAdapter
|
||||||
from designate import policy
|
from designate import policy
|
||||||
from designate import rpc
|
from designate import rpc
|
||||||
from designate import utils
|
|
||||||
|
|
||||||
|
|
||||||
CONF = designate.conf.CONF
|
CONF = designate.conf.CONF
|
||||||
@@ -74,7 +74,7 @@ class PoolCommands(base.Commands):
|
|||||||
self.output_message.append('-------------------')
|
self.output_message.append('-------------------')
|
||||||
|
|
||||||
try:
|
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)
|
self.output_message.append('Not a valid uuid: %s' % pool_id)
|
||||||
raise SystemExit(1)
|
raise SystemExit(1)
|
||||||
|
|
||||||
@@ -164,7 +164,7 @@ class PoolCommands(base.Commands):
|
|||||||
def _get_pool(self, pool_data):
|
def _get_pool(self, pool_data):
|
||||||
if 'id' in pool_data:
|
if 'id' in pool_data:
|
||||||
pool_id = pool_data['id']
|
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)
|
self.output_message.append('Not a valid uuid: %s' % pool_id)
|
||||||
raise SystemExit(1)
|
raise SystemExit(1)
|
||||||
|
|
||||||
|
@@ -33,6 +33,37 @@ class TestUtils(oslotest.base.BaseTestCase):
|
|||||||
self.useFixture(cfg_fixture.Config(CONF))
|
self.useFixture(cfg_fixture.Config(CONF))
|
||||||
self.useFixture(self.stdlog)
|
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.exists')
|
||||||
@mock.patch('os.path.abspath')
|
@mock.patch('os.path.abspath')
|
||||||
def test_find_config(self, mock_abspath, mock_path_exists):
|
def test_find_config(self, mock_abspath, mock_path_exists):
|
||||||
@@ -213,14 +244,6 @@ class TestUtils(oslotest.base.BaseTestCase):
|
|||||||
|
|
||||||
self.assertEqual('Hello World', result)
|
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):
|
def test_split_host_port(self):
|
||||||
host, port = utils.split_host_port('abc:25')
|
host, port = utils.split_host_port('abc:25')
|
||||||
self.assertEqual(('abc', 25), (host, port))
|
self.assertEqual(('abc', 25), (host, port))
|
||||||
|
@@ -139,16 +139,6 @@ def generate_uuid():
|
|||||||
return uuidutils.generate_uuid(dashed=True)
|
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):
|
def validate_uuid(*check):
|
||||||
"""
|
"""
|
||||||
A wrapper to ensure that API controller methods arguments are valid UUID's.
|
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
|
# /v2/zones/<UUID - valid or invalid>/invalid
|
||||||
# get, patch and delete return a 404, but Pecan returns a 405
|
# get, patch and delete return a 404, but Pecan returns a 405
|
||||||
# for a POST at the same URL
|
# for a POST at the same URL
|
||||||
if (len(arg_spec) != len(args)):
|
if len(arg_spec) != len(args):
|
||||||
raise exceptions.NotFound()
|
raise exceptions.NotFound()
|
||||||
|
|
||||||
# Ensure that we have non-empty parameters in the cases where we
|
# 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
|
# This is for URLs like /v2/zones/nameservers
|
||||||
# Ideally Pecan should be handling these cases, but until then
|
# Ideally Pecan should be handling these cases, but until then
|
||||||
# we handle those cases here.
|
# we handle those cases here.
|
||||||
if (len(args) <= len(check)):
|
if len(args) <= len(check):
|
||||||
raise exceptions.NotFound()
|
raise exceptions.NotFound()
|
||||||
|
|
||||||
for name in check:
|
for name in check:
|
||||||
pos = arg_spec.index(name)
|
pos = arg_spec.index(name)
|
||||||
if not is_uuid_like(args[pos]):
|
if not uuidutils.is_uuid_like(args[pos]):
|
||||||
msg = f'Invalid UUID {name}: {args[pos]}'
|
raise exceptions.InvalidUUID(
|
||||||
raise exceptions.InvalidUUID(msg)
|
f'Invalid UUID {name}: {args[pos]}'
|
||||||
|
)
|
||||||
return f(*args, **kwargs)
|
return f(*args, **kwargs)
|
||||||
return functools.wraps(f)(wrapper)
|
return functools.wraps(f)(wrapper)
|
||||||
return inner
|
return inner
|
||||||
|
Reference in New Issue
Block a user