From 4fb6c109297229a9205f76fd934d2b34a5476c9b Mon Sep 17 00:00:00 2001 From: Takashi NATSUME Date: Mon, 15 Aug 2016 18:14:31 +0900 Subject: [PATCH] Remove nova/api/validator.py nova/api/validator.py is not used any more since I8bf7cbaa7015bb61656ab90ccc8f944aaeebb095. So remove it and unit tests for it. TrivialFix Change-Id: I1b91753846a2be011216a418d3f95f907fe2abe7 --- nova/api/validator.py | 120 -------------------------- nova/tests/unit/api/test_validator.py | 85 ------------------ tests-py3.txt | 1 - 3 files changed, 206 deletions(-) delete mode 100644 nova/api/validator.py delete mode 100644 nova/tests/unit/api/test_validator.py diff --git a/nova/api/validator.py b/nova/api/validator.py deleted file mode 100644 index 12a0433c0f08..000000000000 --- a/nova/api/validator.py +++ /dev/null @@ -1,120 +0,0 @@ -# Copyright 2011 Cloudscaling, Inc. -# All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import base64 - -from oslo_log import log as logging -import rfc3986 -import six - - -LOG = logging.getLogger(__name__) - - -def validate_str(max_length=None): - - def _do(val): - if not isinstance(val, six.string_types): - return False - if max_length and len(val) > max_length: - return False - return True - - return _do - - -def validate_int(max_value=None): - - def _do(val): - if not isinstance(val, int): - return False - if max_value and val > max_value: - return False - return True - - return _do - - -def validate_url_path(val): - """True if val is matched by the path component grammar in rfc3986.""" - - if not validate_str()(val): - return False - - uri = rfc3986.URIReference(None, None, val, None, None) - - return uri.path_is_valid() and val.startswith('/') - - -def validate_image_path(val): - if not validate_str()(val): - return False - - bucket_name = val.split('/')[0] - manifest_path = val[len(bucket_name) + 1:] - if not len(bucket_name) or not len(manifest_path): - return False - - if val[0] == '/': - return False - - # make sure the image path is rfc3986 compliant - # prepend '/' to make input validate - if not validate_url_path('/' + val): - return False - - return True - - -def validate_user_data(user_data): - """Check if the user_data is encoded properly.""" - try: - base64.b64decode(user_data) - except TypeError: - return False - return True - - -def validate(args, validator): - """Validate values of args against validators in validator. - - :param args: Dict of values to be validated. - :param validator: A dict where the keys map to keys in args - and the values are validators. - Applies each validator to ``args[key]`` - :returns: True if validation succeeds. Otherwise False. - - A validator should be a callable which accepts 1 argument and which - returns True if the argument passes validation. False otherwise. - A validator should not raise an exception to indicate validity of the - argument. - - Only validates keys which show up in both args and validator. - - """ - - for key in validator: - if key not in args: - continue - - f = validator[key] - assert callable(f) - - if not f(args[key]): - LOG.debug("%(key)s with value %(value)s failed" - " validator %(name)s", - {'key': key, 'value': args[key], 'name': f.__name__}) - return False - return True diff --git a/nova/tests/unit/api/test_validator.py b/nova/tests/unit/api/test_validator.py deleted file mode 100644 index 55c922bef995..000000000000 --- a/nova/tests/unit/api/test_validator.py +++ /dev/null @@ -1,85 +0,0 @@ -# Copyright 2011 Cloudscaling, Inc. -# All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import base64 - -from nova.api import validator -from nova import test - - -class ValidatorTestCase(test.NoDBTestCase): - - def test_validate(self): - fixture = { - 'foo': lambda val: val is True - } - - self.assertTrue( - validator.validate({'foo': True}, fixture)) - self.assertFalse( - validator.validate({'foo': False}, fixture)) - - def test_only_tests_intersect(self): - """Test that validator.validate only tests the intersect of keys - from args and validator. - """ - - fixture = { - 'foo': lambda val: True, - 'bar': lambda val: True - } - - self.assertTrue( - validator.validate({'foo': True}, fixture)) - self.assertTrue( - validator.validate({'foo': True, 'bar': True}, fixture)) - self.assertTrue( - validator.validate({'foo': True, 'bar': True, 'baz': True}, - fixture)) - - def test_validate_str(self): - self.assertTrue(validator.validate_str()('foo')) - self.assertFalse(validator.validate_str()(1)) - self.assertTrue(validator.validate_str(4)('foo')) - self.assertFalse(validator.validate_str(2)('foo')) - self.assertFalse(validator.validate_str()(None)) - self.assertTrue(validator.validate_str()(u'foo')) - - def test_validate_int(self): - self.assertTrue(validator.validate_int()(1)) - self.assertFalse(validator.validate_int()('foo')) - self.assertTrue(validator.validate_int(100)(1)) - self.assertFalse(validator.validate_int(4)(5)) - self.assertFalse(validator.validate_int()(None)) - - def test_validate_url_path(self): - self.assertTrue(validator.validate_url_path('/path/to/file')) - self.assertFalse(validator.validate_url_path('path/to/file')) - self.assertFalse( - validator.validate_url_path('#this is not a path!@#$%^&*()') - ) - self.assertFalse(validator.validate_url_path(None)) - self.assertFalse(validator.validate_url_path(123)) - - def test_validate_image_path(self): - self.assertTrue(validator.validate_image_path('path/to/file')) - self.assertFalse(validator.validate_image_path('/path/to/file')) - self.assertFalse(validator.validate_image_path('path')) - - def test_validate_user_data(self): - fixture = base64.b64encode('foo') - self.assertTrue(validator.validate_user_data(fixture)) - self.assertFalse(validator.validate_user_data(False)) - self.assertFalse(validator.validate_user_data('hello, world!')) diff --git a/tests-py3.txt b/tests-py3.txt index 2da21c52c486..71b1dce04ffc 100644 --- a/tests-py3.txt +++ b/tests-py3.txt @@ -32,7 +32,6 @@ nova.tests.unit.api.openstack.compute.test_versions.VersionsTestV21WithV2Compati nova.tests.unit.api.openstack.compute.test_volumes.BootFromVolumeTest nova.tests.unit.api.openstack.compute.test_volumes.VolumeApiTestV21 nova.tests.unit.api.test_compute_req_id.RequestIdTest -nova.tests.unit.api.test_validator.ValidatorTestCase nova.tests.unit.api.test_wsgi.Test nova.tests.unit.compute.test_compute.ComputeAPITestCase.test_create_with_base64_user_data nova.tests.unit.compute.test_compute.ComputeTestCase.test_finish_resize_with_volumes