Merge "Handle unhandled ValueError Exception"

This commit is contained in:
Jenkins 2016-10-21 08:38:05 +00:00 committed by Gerrit Code Review
commit f0775849b4
2 changed files with 52 additions and 2 deletions

View File

@ -36,8 +36,11 @@ DOCKER_MINIMUM_MEMORY = 4 * 1024 * 1024
def validate_limit(limit):
if limit is not None and int(limit) <= 0:
raise wsme.exc.ClientSideError(_("Limit must be positive"))
try:
if limit is not None and int(limit) <= 0:
raise wsme.exc.ClientSideError(_("Limit must be positive integer"))
except ValueError:
raise wsme.exc.ClientSideError(_("Limit must be positive integer"))
if limit is not None:
return min(CONF.api.max_limit, int(limit))

View File

@ -0,0 +1,47 @@
# 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 wsme
from zun.api.utils import validate_limit
from zun.api.utils import validate_sort_dir
from zun.tests import base
class TestUtils(base.BaseTestCase):
"""Test cases for zun.api.utils"""
def test_validate_limit(self):
self.assertEqual(1000, validate_limit(None))
self.assertEqual(1000, validate_limit(1001))
self.assertEqual(50, validate_limit(50))
with self.assertRaisesRegexp(wsme.exc.ClientSideError,
"Limit must be positive"):
validate_limit(-1)
with self.assertRaisesRegexp(wsme.exc.ClientSideError,
"Limit must be positive"):
validate_limit(0)
with self.assertRaisesRegexp(wsme.exc.ClientSideError,
"Limit must be positive integer"):
validate_limit('a')
with self.assertRaisesRegexp(wsme.exc.ClientSideError,
"Limit must be positive integer"):
validate_limit('5.5')
def test_validate_sort_dir(self):
self.assertEqual('asc', validate_sort_dir('asc'))
self.assertEqual('desc', validate_sort_dir('desc'))
with self.assertRaisesRegexp(wsme.exc.ClientSideError,
"Invalid sort direction"):
validate_sort_dir(None)
with self.assertRaisesRegexp(wsme.exc.ClientSideError,
"Invalid sort direction"):
validate_sort_dir('abc')