From e799dabb56aff65c9fe4b5277f0656c630d9f0e0 Mon Sep 17 00:00:00 2001 From: Matt Riedemann Date: Mon, 23 Feb 2015 10:13:55 -0800 Subject: [PATCH] console: add unit tests for baseproxy Commit 97d63d8745cd9b3b391ce96b94b4da263b3a053d introduced a TypeError in baseproxy. That's fixed in commit 4fe64143cb4ceafbfa8580a34cfa75d599d48151 but while reviewing that change it was noticed that there is no unit test coverage for the baseproxy module, so it's added here. Related-Bug: #1424532 Change-Id: If53409df2a8e3679a86454228cba27a50c005abe --- nova/tests/unit/cmd/test_baseproxy.py | 64 +++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 nova/tests/unit/cmd/test_baseproxy.py diff --git a/nova/tests/unit/cmd/test_baseproxy.py b/nova/tests/unit/cmd/test_baseproxy.py new file mode 100644 index 000000000000..9d30fdc6cdba --- /dev/null +++ b/nova/tests/unit/cmd/test_baseproxy.py @@ -0,0 +1,64 @@ +# Copyright 2015 IBM Corp. +# +# 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 mock +from oslo_log import log as logging + +from nova.cmd import baseproxy +from nova import config +from nova.console import websocketproxy +from nova.openstack.common.report import guru_meditation_report as gmr +from nova import test +from nova import version + + +@mock.patch.object(config, 'parse_args', new=lambda *args, **kwargs: None) +class BaseProxyTestCase(test.NoDBTestCase): + + @mock.patch('os.path.exists', return_value=False) + # NOTE(mriedem): sys.exit raises TestingException so we can actually exit + # the test normally. + @mock.patch('sys.exit', side_effect=test.TestingException) + def test_proxy_ssl_without_cert(self, mock_exit, mock_exists): + self.flags(ssl_only=True) + self.assertRaises(test.TestingException, baseproxy.proxy, + '0.0.0.0', '6080') + mock_exit.assert_called_once_with(-1) + + @mock.patch('os.path.exists', return_value=False) + @mock.patch('sys.exit', side_effect=test.TestingException) + def test_proxy_web_dir_does_not_exist(self, mock_exit, mock_exists): + self.flags(web='/my/fake/webserver/') + self.assertRaises(test.TestingException, baseproxy.proxy, + '0.0.0.0', '6080') + mock_exit.assert_called_once_with(-1) + + @mock.patch('os.path.exists', return_value=True) + @mock.patch.object(logging, 'setup') + @mock.patch.object(gmr.TextGuruMeditation, 'setup_autorun') + @mock.patch.object(websocketproxy.NovaWebSocketProxy, '__init__', + return_value=None) + @mock.patch.object(websocketproxy.NovaWebSocketProxy, 'start_server') + def test_proxy(self, mock_start, mock_init, mock_gmr, mock_log, + mock_exists): + baseproxy.proxy('0.0.0.0', '6080') + mock_log.assert_called_once_with(baseproxy.CONF, 'nova') + mock_gmr.mock_assert_called_once_with(version) + mock_init.assert_called_once_with( + listen_host='0.0.0.0', listen_port='6080', source_is_ipv6=False, + verbose=False, cert='self.pem', key=None, ssl_only=False, + daemon=False, record=False, traffic=False, + web='/usr/share/spice-html5', file_only=True, + RequestHandlerClass=websocketproxy.NovaProxyRequestHandler) + mock_start.assert_called_once_with()