summaryrefslogtreecommitdiff
path: root/nova/tests/unit/virt/ironic/test_client_wrapper.py
blob: 9c2ffe3dca0c8ce70e10baedb889039722ff70a1 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# Copyright 2014 Red Hat, Inc.
# All Rights Reserved.
#
#    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 ironicclient import client as ironic_client
from ironicclient import exc as ironic_exception
from keystoneauth1 import discover as ksa_disc
import keystoneauth1.session
import mock
from oslo_config import cfg

import nova.conf
from nova import exception
from nova import test
from nova.tests.unit.virt.ironic import utils as ironic_utils
from nova.virt.ironic import client_wrapper

CONF = nova.conf.CONF

FAKE_CLIENT = ironic_utils.FakeClient()


def get_new_fake_client(*args, **kwargs):
    return ironic_utils.FakeClient()


class IronicClientWrapperTestCase(test.NoDBTestCase):

    def setUp(self):
        super(IronicClientWrapperTestCase, self).setUp()
        self.ironicclient = client_wrapper.IronicClientWrapper()
        # Do not waste time sleeping
        cfg.CONF.set_override('api_retry_interval', 0, 'ironic')
        cfg.CONF.set_override('region_name', 'RegionOne', 'ironic')
        get_ksa_adapter_p = mock.patch('nova.utils.get_ksa_adapter')
        self.addCleanup(get_ksa_adapter_p.stop)
        self.get_ksa_adapter = get_ksa_adapter_p.start()
        get_auth_plugin_p = mock.patch('nova.virt.ironic.client_wrapper.'
                                       'IronicClientWrapper._get_auth_plugin')
        self.addCleanup(get_auth_plugin_p.stop)
        self.get_auth_plugin = get_auth_plugin_p.start()

    @mock.patch.object(client_wrapper.IronicClientWrapper, '_multi_getattr')
    @mock.patch.object(client_wrapper.IronicClientWrapper, '_get_client')
    def test_call_good_no_args(self, mock_get_client, mock_multi_getattr):
        mock_get_client.return_value = FAKE_CLIENT
        self.ironicclient.call("node.list")
        mock_get_client.assert_called_once_with(retry_on_conflict=True)
        mock_multi_getattr.assert_called_once_with(FAKE_CLIENT, "node.list")
        mock_multi_getattr.return_value.assert_called_once_with()

    @mock.patch.object(client_wrapper.IronicClientWrapper, '_multi_getattr')
    @mock.patch.object(client_wrapper.IronicClientWrapper, '_get_client')
    def test_call_good_with_args(self, mock_get_client, mock_multi_getattr):
        mock_get_client.return_value = FAKE_CLIENT
        self.ironicclient.call("node.list", 'test', associated=True)
        mock_get_client.assert_called_once_with(retry_on_conflict=True)
        mock_multi_getattr.assert_called_once_with(FAKE_CLIENT, "node.list")
        mock_multi_getattr.return_value.assert_called_once_with(
            'test', associated=True)

    @mock.patch.object(keystoneauth1.session, 'Session')
    @mock.patch.object(ironic_client, 'get_client')
    def test__get_client_session(self, mock_ir_cli, mock_session):
        """An Ironicclient is called with a keystoneauth1 Session"""
        mock_session.return_value = 'session'
        ironicclient = client_wrapper.IronicClientWrapper()
        # dummy call to have _get_client() called
        ironicclient.call("node.list")
        self.get_ksa_adapter.assert_called_once_with(
            'baremetal', ksa_auth=self.get_auth_plugin.return_value,
            ksa_session='session', min_version=(1, 0),
            max_version=(1, ksa_disc.LATEST))
        expected = {'session': 'session',
            'max_retries': CONF.ironic.api_max_retries,
            'retry_interval': CONF.ironic.api_retry_interval,
            'os_ironic_api_version': ['1.46', '1.38'],
            'endpoint':
                self.get_ksa_adapter.return_value.get_endpoint.return_value,
            'interface': ['internal', 'public']}
        mock_ir_cli.assert_called_once_with(1, **expected)

    @mock.patch.object(keystoneauth1.session, 'Session')
    @mock.patch.object(ironic_client, 'get_client')
    def test__get_client_session_service_not_found(self, mock_ir_cli,
                                                   mock_session):
        """Validate behavior when get_endpoint_data raises."""
        mock_session.return_value = 'session'
        self.get_ksa_adapter.side_effect = (
            exception.ConfGroupForServiceTypeNotFound(stype='baremetal'))
        ironicclient = client_wrapper.IronicClientWrapper()
        # dummy call to have _get_client() called
        ironicclient.call("node.list")
        self.get_ksa_adapter.assert_called_once_with(
            'baremetal', ksa_auth=self.get_auth_plugin.return_value,
            ksa_session='session', min_version=(1, 0),
            max_version=(1, ksa_disc.LATEST))
        # When get_endpoint_data raises any ServiceNotFound, None is returned.
        expected = {'session': 'session',
                    'max_retries': CONF.ironic.api_max_retries,
                    'retry_interval': CONF.ironic.api_retry_interval,
                    'os_ironic_api_version': ['1.46', '1.38'],
                    'endpoint': None,
                    'region_name': CONF.ironic.region_name,
                    'interface': ['internal', 'public']}
        mock_ir_cli.assert_called_once_with(1, **expected)

    @mock.patch.object(keystoneauth1.session, 'Session')
    @mock.patch.object(ironic_client, 'get_client')
    def test__get_client_and_valid_interfaces(self, mock_ir_cli, mock_session):
        """Confirm explicit setting of valid_interfaces."""
        mock_session.return_value = 'session'
        endpoint = 'https://baremetal.example.com/endpoint'
        self.get_ksa_adapter.return_value.get_endpoint.return_value = endpoint
        self.flags(endpoint_override=endpoint, group='ironic')
        self.flags(valid_interfaces='admin', group='ironic')
        ironicclient = client_wrapper.IronicClientWrapper()
        # dummy call to have _get_client() called
        ironicclient.call("node.list")
        expected = {'session': 'session',
                    'max_retries': CONF.ironic.api_max_retries,
                    'retry_interval': CONF.ironic.api_retry_interval,
                    'os_ironic_api_version': ['1.46', '1.38'],
                    'endpoint': endpoint,
                    'interface': ['admin']}
        mock_ir_cli.assert_called_once_with(1, **expected)

    @mock.patch.object(client_wrapper.IronicClientWrapper, '_multi_getattr')
    @mock.patch.object(client_wrapper.IronicClientWrapper, '_get_client')
    def test_call_fail_exception(self, mock_get_client, mock_multi_getattr):
        test_obj = mock.Mock()
        test_obj.side_effect = ironic_exception.HTTPNotFound
        mock_multi_getattr.return_value = test_obj
        mock_get_client.return_value = FAKE_CLIENT
        self.assertRaises(ironic_exception.HTTPNotFound,
                          self.ironicclient.call, "node.list")

    @mock.patch.object(ironic_client, 'get_client')
    def test__get_client_unauthorized(self, mock_get_client):
        mock_get_client.side_effect = ironic_exception.Unauthorized
        self.assertRaises(exception.NovaException,
                          self.ironicclient._get_client)

    @mock.patch.object(ironic_client, 'get_client')
    def test__get_client_unexpected_exception(self, mock_get_client):
        mock_get_client.side_effect = ironic_exception.ConnectionRefused
        self.assertRaises(ironic_exception.ConnectionRefused,
                          self.ironicclient._get_client)

    def test__multi_getattr_good(self):
        response = self.ironicclient._multi_getattr(FAKE_CLIENT, "node.list")
        self.assertEqual(FAKE_CLIENT.node.list, response)

    def test__multi_getattr_fail(self):
        self.assertRaises(AttributeError, self.ironicclient._multi_getattr,
                          FAKE_CLIENT, "nonexistent")

    @mock.patch.object(ironic_client, 'get_client')
    def test__client_is_cached(self, mock_get_client):
        mock_get_client.side_effect = get_new_fake_client
        ironicclient = client_wrapper.IronicClientWrapper()
        first_client = ironicclient._get_client()
        second_client = ironicclient._get_client()
        self.assertEqual(id(first_client), id(second_client))

    @mock.patch.object(ironic_client, 'get_client')
    def test_call_uses_cached_client(self, mock_get_client):
        mock_get_client.side_effect = get_new_fake_client
        ironicclient = client_wrapper.IronicClientWrapper()
        for n in range(0, 4):
            ironicclient.call("node.list")
        self.assertEqual(1, mock_get_client.call_count)