From cf7cd655a2aaa7f44b94dca0ad7a15073a774a48 Mon Sep 17 00:00:00 2001 From: Wen Zhi Yu Date: Wed, 21 Oct 2015 13:38:22 +0800 Subject: [PATCH] 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 --- nova/cmd/baseproxy.py | 3 ++- nova/tests/unit/cmd/test_baseproxy.py | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/nova/cmd/baseproxy.py b/nova/cmd/baseproxy.py index 6b08a2665d5d..a05534b8d6f5 100644 --- a/nova/cmd/baseproxy.py +++ b/nova/cmd/baseproxy.py @@ -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): diff --git a/nova/tests/unit/cmd/test_baseproxy.py b/nova/tests/unit/cmd/test_baseproxy.py index d3cec2fc1220..b8a16af3d075 100644 --- a/nova/tests/unit/cmd/test_baseproxy.py +++ b/nova/tests/unit/cmd/test_baseproxy.py @@ -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)