oslo-incubator/tests/unit/test_exception.py
Michael Still 4c00615aad Move utils.execute to its own module.
Also move the exceptions raised to being local to the executils
module.

Change-Id: Ibb2445f92840b9ce4b52373b0b09adb3d6a4a897
2012-11-01 15:00:55 +01:00

100 lines
3.0 KiB
Python

# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2011 OpenStack LLC.
# 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 unittest
from openstack.common import exception
def good_function():
return "Is Bueno!"
def bad_function_error():
raise exception.Error()
def bad_function_exception():
raise Exception()
class WrapExceptionTest(unittest.TestCase):
def test_wrap_exception_good_return(self):
wrapped = exception.wrap_exception
self.assertEquals(good_function(), wrapped(good_function)())
def test_wrap_exception_throws_error(self):
wrapped = exception.wrap_exception
self.assertRaises(exception.Error, wrapped(bad_function_error))
def test_wrap_exception_throws_exception(self):
wrapped = exception.wrap_exception
self.assertRaises(Exception, wrapped(bad_function_exception))
class ApiErrorTest(unittest.TestCase):
def test_without_code(self):
err = exception.ApiError('fake error')
self.assertEqual(err.__str__(), 'Unknown: fake error')
self.assertEqual(err.code, 'Unknown')
self.assertEqual(err.message, 'fake error')
def test_with_code(self):
err = exception.ApiError('fake error', 'blah code')
self.assertEqual(err.__str__(), 'blah code: fake error')
self.assertEqual(err.code, 'blah code')
self.assertEqual(err.message, 'fake error')
class BadStoreUriTest(unittest.TestCase):
def test(self):
uri = 'http:///etc/passwd'
reason = 'Permission DENIED!'
err = exception.BadStoreUri(uri, reason)
self.assertTrue(uri in str(err.message))
self.assertTrue(reason in str(err.message))
class UnknownSchemeTest(unittest.TestCase):
def test(self):
scheme = 'http'
err = exception.UnknownScheme(scheme)
self.assertTrue(scheme in str(err.message))
class OpenstackExceptionTest(unittest.TestCase):
class TestException(exception.OpenstackException):
message = '%(test)s'
def test_format_error_string(self):
test_message = 'Know Your Meme'
err = self.TestException(test=test_message)
self.assertEqual(err._error_string, test_message)
def test_error_forating_error_string(self):
err = self.TestException(lol='U mad brah')
self.assertEqual(err._error_string, self.TestException.message)
def test_str(self):
message = 'Y u no fail'
err = self.TestException(test=message)
self.assertEqual(str(err), message)