Commit f10cef01 authored by Stavros Sachtouris's avatar Stavros Sachtouris
Browse files

Test All cli.errors error classes

Refs: #4058
parent 0defc932
......@@ -40,17 +40,17 @@ class CLIError(Exception):
def __init__(self, message, details=[], importance=0):
"""
@message is the main message of the Error
@defaults is a list of previous errors
@importance of the output for the user
Suggested values: 0, 1, 2, 3
:param message: is the main message of the Error
:param defaults: is a list of previous errors
:param importance: of the output for the user (0, 1, 2, 3)
"""
message += '' if message and message[-1] == '\n' else '\n'
message += '' if message and message.endswith('\n') else '\n'
super(CLIError, self).__init__(message)
self.details = list(details) if isinstance(details, list)\
else [] if details is None else ['%s' % details]
self.details = (list(details) if (
isinstance(details, list) or isinstance(details, tuple)) else [
'%s' % details]) if details else []
try:
self.importance = int(importance)
self.importance = int(importance or 0)
except ValueError:
self.importance = 0
......@@ -73,15 +73,16 @@ class CLIUnimplemented(CLIError):
class CLIBaseUrlError(CLIError):
def __init__(self, message='', details=[], importance=2, service=None):
service = '%s' % (service or '')
message = message or 'No URL for %s' % service.lower()
details = details or [
'Two options to resolve this:',
'Two ways to resolve this:',
'(Use the correct cloud name, instead of "default")',
'A. (recommended) Let kamaki discover the endpoint URLs for all',
'A. (recommended) Let kamaki discover endpoint URLs for all',
'services by setting a single Authentication URL and token:',
' /config set cloud.default.url <AUTH_URL>',
' /config set cloud.default.token <t0k3n>',
'B. (advanced users) Explicitly set a valid %s endpoint URL' % (
'B. (advanced users) Explicitly set an %s endpoint URL' % (
service.upper()),
'Note: URL option has a higher priority, so delete it to',
'make that work',
......@@ -108,13 +109,6 @@ class CLICmdSpecError(CLIError):
super(CLICmdSpecError, self).__init__(message, details, importance)
class CLICmdIncompleteError(CLICmdSpecError):
def __init__(
self, message='Incomplete Command Error',
details=[], importance=1):
super(CLICmdSpecError, self).__init__(message, details, importance)
def raiseCLIError(err, message='', importance=0, details=[]):
"""
:param err: (Exception) the original error message, if None, a new
......@@ -133,38 +127,33 @@ def raiseCLIError(err, message='', importance=0, details=[]):
stack = ['%s' % type(err)] if err else ['<kamaki.cli.errors.CLIError>']
stack += format_stack()
try:
stack = [e for e in stack if e != stack[1]]
except KeyError:
log.debug('\n < '.join(stack))
details = ['%s' % details] if not isinstance(details, list)\
else list(details)
details = list(details) if (
isinstance(details, list) or isinstance(details, tuple)) else [
'%s' % details]
details += getattr(err, 'details', [])
if err:
origerr = '%s' % err
origerr = origerr if origerr else '%s' % type(err)
else:
origerr = stack[0]
message = '%s' % (message if message else origerr)
origerr = (('%s' % err) or '%s' % type(err)) if err else stack[0]
message = '%s' % message or origerr
try:
status = err.status or err.errno
except AttributeError:
status = None
try:
status = err.errno
except AttributeError:
status = None
if origerr not in details + [message]:
details.append(origerr)
message += '' if message and message[-1] == '\n' else '\n'
message += '' if message and message.endswith('\n') else '\n'
if status:
message = '(%s) %s' % (err.status, message)
message = '(%s) %s' % (status, message)
try:
status = int(err.status)
status = int(status)
except ValueError:
raise CLIError(message, details, importance)
importance = importance if importance else status // 100
raise CLIError(message, details, importance or 0)
importance = importance or status // 100
importance = getattr(err, 'importance', importance)
raise CLIError(message, details, importance)
......@@ -281,6 +281,111 @@ class LoggerMethods(TestCase):
GL.assert_called_once_with('my logger name')
_RET = None
class PseudoException(object):
def __init__(self, *args):
global _RET
_RET = args
class CLIError(TestCase):
@patch('__builtin__.super', return_value=PseudoException())
def test___init__(self, S):
from kamaki.cli.errors import CLIError
global _RET
for message, details, importance in (
('some msg', [], 0),
('some msg\n', 'details', 0),
('some msg', ['details1', 'details2'], 10)):
clie = CLIError(message, details, importance)
self.assertEqual(S.mock_calls[-1], call(CLIError, clie))
self.assertEqual(_RET[0], (message + '\n') if (
message and not message.endswith('\n')) else message)
self.assertEqual(clie.details, (list(details) if (
isinstance(details, list)) else ['%s' % details]) if (
details) else [])
self.assertEqual(clie.importance, int(importance))
clie = CLIError(message, details, 'non int')
self.assertEqual(clie.importance, 0)
class CLIUnimplemented(TestCase):
def test___init__(self):
from kamaki.cli.errors import CLIUnimplemented
cliu = CLIUnimplemented()
self.assertEqual(
'%s' % cliu,
'I \'M SORRY, DAVE.\nI \'M AFRAID I CAN\'T DO THAT.\n')
self.assertEqual(cliu.details, [
' _ |',
' _-- --_ |',
' -- -- |',
' -- . -- |',
' -_ _- |',
' -_ _- |',
' - |'])
self.assertEqual(cliu.importance, 3)
class CLIBaseUrlError(TestCase):
def test___init__(self):
from kamaki.cli.errors import CLIBaseUrlError
for service in ('', 'some service'):
clibue = CLIBaseUrlError(service=service)
self.assertEqual('%s' % clibue, 'No URL for %s\n' % service)
self.assertEqual(clibue.details, [
'Two ways to resolve this:',
'(Use the correct cloud name, instead of "default")',
'A. (recommended) Let kamaki discover endpoint URLs for all',
'services by setting a single Authentication URL and token:',
' /config set cloud.default.url <AUTH_URL>',
' /config set cloud.default.token <t0k3n>',
'B. (advanced users) Explicitly set an %s endpoint URL' % (
service.upper()),
'Note: URL option has a higher priority, so delete it to',
'make that work',
' /config delete cloud.default.url',
' /config set cloud.%s.url <%s_URL>' % (
service, service.upper())])
self.assertEqual(clibue.importance, 2)
class CLISyntaxError(TestCase):
def test___init__(self):
from kamaki.cli.errors import CLISyntaxError
clise = CLISyntaxError()
self.assertEqual('%s' % clise, 'Syntax Error\n')
self.assertEqual(clise.details, [])
self.assertEqual(clise.importance, 1)
class CLIUnknownCommand(TestCase):
def test___init__(self):
from kamaki.cli.errors import CLIUnknownCommand
cliec = CLIUnknownCommand()
self.assertEqual('%s' % cliec, 'Unknown Command\n')
self.assertEqual(cliec.details, [])
self.assertEqual(cliec.importance, 1)
class CLICmdSpecError(TestCase):
def test___init__(self):
from kamaki.cli.errors import CLICmdSpecError
clicse = CLICmdSpecError()
self.assertEqual('%s' % clicse, 'Command Specification Error\n')
self.assertEqual(clicse.details, [])
self.assertEqual(clicse.importance, 0)
# TestCase auxiliary methods
def runTestCase(cls, test_name, args=[], failure_collector=[]):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment