summaryrefslogtreecommitdiff
path: root/nova/tests/unit/cmd/test_baseproxy.py
blob: 34f911cd838fef95f9a30d4fe4be59c4e0268d1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# 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.

from io import StringIO

import fixtures
import mock
from oslo_log import log as logging
from oslo_reports import guru_meditation_report as gmr

from nova.cmd import baseproxy
from nova import config
from nova.console import websocketproxy
from nova import test
from nova import version


@mock.patch.object(config, 'parse_args', new=lambda *args, **kwargs: None)
class BaseProxyTestCase(test.NoDBTestCase):

    def setUp(self):
        super(BaseProxyTestCase, self).setUp()
        self.stderr = StringIO()
        self.useFixture(fixtures.MonkeyPatch('sys.stderr', self.stderr))

    @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)
        self.assertEqual(self.stderr.getvalue(),
                         "SSL only and self.pem not found\n")

    @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('nova.console.websocketproxy.NovaWebSocketProxy.__init__',
                return_value=None)
    @mock.patch('nova.console.websocketproxy.NovaWebSocketProxy.start_server')
    @mock.patch('websockify.websocketproxy.select_ssl_version',
                return_value=None)
    def test_proxy(self, mock_select_ssl_version, 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.assert_called_once_with(version, conf=baseproxy.CONF)
        mock_init.assert_called_once_with(
            listen_host='0.0.0.0', listen_port='6080', source_is_ipv6=False,
            cert='self.pem', key=None, ssl_only=False, ssl_ciphers=None,
            ssl_minimum_version='default', daemon=False, record=None,
            security_proxy=None, traffic=True,
            web='/usr/share/spice-html5', file_only=True,
            RequestHandlerClass=websocketproxy.NovaProxyRequestHandler)
        mock_start.assert_called_once_with()

    @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):
        self.flags(ssl_only=True)
        self.assertRaises(test.TestingException, baseproxy.proxy,
                          '0.0.0.0', '6080')
        self.assertEqual(self.stderr.getvalue(),
                         "SSL only and self.pem not found\n")
        mock_exit.assert_called_once_with(-1)

    @mock.patch('os.path.exists', return_value=True)
    @mock.patch('nova.console.websocketproxy.NovaWebSocketProxy.__init__',
                return_value=None)
    @mock.patch('nova.console.websocketproxy.NovaWebSocketProxy.start_server')
    def test_proxy_ssl_settings(self, mock_start, mock_init, mock_exists):
        self.flags(ssl_minimum_version='tlsv1_3', group='console')
        self.flags(ssl_ciphers='ALL:!aNULL', group='console')
        baseproxy.proxy('0.0.0.0', '6080')
        mock_init.assert_called_once_with(
            listen_host='0.0.0.0', listen_port='6080', source_is_ipv6=False,
            cert='self.pem', key=None, ssl_only=False,
            ssl_ciphers='ALL:!aNULL', ssl_minimum_version='tlsv1_3',
            daemon=False, record=None, security_proxy=None, traffic=True,
            web='/usr/share/spice-html5', file_only=True,
            RequestHandlerClass=websocketproxy.NovaProxyRequestHandler)