summaryrefslogtreecommitdiff
path: root/nova/tests/unit/cmd/test_nova_api.py
blob: a4f7d82105eebefde5fecbf8bb847bc0ab3af3c4 (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
# Copyright 2015 HPE, Inc.
#
# 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 unittest import mock

from nova.cmd import api
from nova import config
from nova import exception
from nova import test


# required because otherwise oslo early parse_args dies
@mock.patch.object(config, 'parse_args', new=lambda *args, **kwargs: None)
# required so we don't set the global service version cache
@mock.patch('nova.objects.Service.enable_min_version_cache')
class TestNovaAPI(test.NoDBTestCase):

    def test_continues_on_failure(self, version_cache):
        count = [1]

        fake_server = mock.MagicMock()
        fake_server.workers = 123

        def fake_service(api, **kw):
            while count:
                count.pop()
                raise exception.PasteAppNotFound(name=api, path='/')
            return fake_server

        self.flags(enabled_apis=['osapi_compute', 'metadata'])
        with mock.patch.object(api, 'service') as mock_service:
            mock_service.WSGIService.side_effect = fake_service
            api.main()
            mock_service.WSGIService.assert_has_calls([
                mock.call('osapi_compute', use_ssl=False),
                mock.call('metadata', use_ssl=False),
            ])
            launcher = mock_service.process_launcher.return_value
            launcher.launch_service.assert_called_once_with(
                fake_server, workers=123)
        self.assertTrue(version_cache.called)

    @mock.patch('sys.exit')
    def test_fails_if_none_started(self, mock_exit, version_cache):
        mock_exit.side_effect = test.TestingException
        self.flags(enabled_apis=[])
        with mock.patch.object(api, 'service') as mock_service:
            self.assertRaises(test.TestingException, api.main)
            mock_exit.assert_called_once_with(1)
            launcher = mock_service.process_launcher.return_value
            self.assertFalse(launcher.wait.called)
        self.assertFalse(version_cache.called)

    @mock.patch('sys.exit')
    def test_fails_if_all_failed(self, mock_exit, version_cache):
        mock_exit.side_effect = test.TestingException
        self.flags(enabled_apis=['osapi_compute', 'metadata'])
        with mock.patch.object(api, 'service') as mock_service:
            mock_service.WSGIService.side_effect = exception.PasteAppNotFound(
                name='foo', path='/')
            self.assertRaises(test.TestingException, api.main)
            mock_exit.assert_called_once_with(1)
            launcher = mock_service.process_launcher.return_value
            self.assertFalse(launcher.wait.called)
        self.assertTrue(version_cache.called)