Fix error handling in nova.cmd.baseproxy

If an error occurs in nova.cmd.baseproxy the method exit_with_error
will be executed that looks as follows:

def exit_with_error(msg, errno=-1):
	print(msg) and sys.exit(errno)

However, in python2.7 this method terminates the application without
printing anything(unable to flush on time) and in Python3.4 it does
strange things because print() returns None.

This commit modifies exit_with_error method to output error message
with stderr(which in unbuffered).

Change-Id: I519b68f8c2bc62988de87bdd2847d5f3be7e532d
Closes-Bug: #1506213
This commit is contained in:
Wen Zhi Yu 2015-10-21 13:38:22 +08:00
parent 2c3f9c339c
commit cf7cd655a2
2 changed files with 13 additions and 1 deletions

View File

@ -39,7 +39,8 @@ CONF.import_opt('web', 'nova.cmd.novnc')
def exit_with_error(msg, errno=-1):
print(msg) and sys.exit(errno)
sys.stderr.write(msg + '\n')
sys.exit(errno)
def proxy(host, port):

View File

@ -65,3 +65,14 @@ class BaseProxyTestCase(test.NoDBTestCase):
web='/usr/share/spice-html5', file_only=True,
RequestHandlerClass=websocketproxy.NovaProxyRequestHandler)
mock_start.assert_called_once_with()
@mock.patch('sys.stderr.write')
@mock.patch('os.path.exists', return_value=False)
@mock.patch('sys.exit', side_effect=test.TestingException)
def test_proxy_exit_with_error(self, mock_exit, mock_exists, mock_stderr):
self.flags(ssl_only=True)
self.assertRaises(test.TestingException, baseproxy.proxy,
'0.0.0.0', '6080')
mock_stderr.assert_called_once_with(
'SSL only and self.pem not found\n')
mock_exit.assert_called_once_with(-1)