File: test_client.py

package info (click to toggle)
python-manilaclient 5.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,768 kB
  • sloc: python: 49,541; makefile: 99; sh: 2
file content (256 lines) | stat: -rw-r--r-- 11,113 bytes parent folder | download
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# 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

import ddt
from oslo_utils import uuidutils

import manilaclient
from manilaclient import exceptions
from manilaclient.tests.unit import utils
from manilaclient.v2 import client


@ddt.ddt
class ClientTest(utils.TestCase):
    def setUp(self):
        super(self.__class__, self).setUp()
        self.catalog = {
            'share': [
                {'region': 'TestRegion', 'publicURL': 'http://1.2.3.4'},
            ],
        }

    def test_adapter_properties(self):
        # sample of properties, there are many more
        retries = 3
        base_url = uuidutils.generate_uuid(dashed=False)

        s = client.session.Session()
        c = client.Client(session=s,
                          api_version=manilaclient.API_MAX_VERSION,
                          service_catalog_url=base_url, retries=retries,
                          input_auth_token='token')

        self.assertEqual(base_url, c.client.endpoint_url)
        self.assertEqual(retries, c.client.retries)

    def test_auth_via_token_invalid(self):
        self.assertRaises(exceptions.ClientException, client.Client,
                          api_version=manilaclient.API_MAX_VERSION,
                          input_auth_token="token")

    def test_auth_via_token_and_session(self):
        s = client.session.Session()
        base_url = uuidutils.generate_uuid(dashed=False)

        c = client.Client(input_auth_token='token',
                          service_catalog_url=base_url, session=s,
                          api_version=manilaclient.API_MAX_VERSION)

        self.assertIsNotNone(c.client)
        self.assertIsNone(c.keystone_client)

    def test_auth_via_token(self):
        base_url = uuidutils.generate_uuid(dashed=False)

        c = client.Client(input_auth_token='token',
                          service_catalog_url=base_url,
                          api_version=manilaclient.API_MAX_VERSION)

        self.assertIsNotNone(c.client)
        self.assertIsNone(c.keystone_client)

    @mock.patch.object(client.Client, '_get_keystone_client', mock.Mock())
    def test_valid_region_name_v1(self):
        self.mock_object(client.httpclient, 'HTTPClient')
        kc = client.Client._get_keystone_client.return_value
        kc.service_catalog = mock.Mock()
        kc.service_catalog.get_endpoints = mock.Mock(return_value=self.catalog)
        c = client.Client(api_version=manilaclient.API_DEPRECATED_VERSION,
                          service_type="share",
                          region_name='TestRegion')
        self.assertTrue(client.Client._get_keystone_client.called)
        kc.service_catalog.get_endpoints.assert_called_with('share')
        client.httpclient.HTTPClient.assert_called_with(
            'http://1.2.3.4',
            mock.ANY,
            'python-manilaclient',
            insecure=False,
            cacert=None,
            cert=None,
            timeout=None,
            retries=None,
            http_log_debug=False,
            api_version=manilaclient.API_DEPRECATED_VERSION)
        self.assertIsNotNone(c.client)

    @mock.patch.object(client.Client, '_get_keystone_client', mock.Mock())
    def test_nonexistent_region_name(self):
        kc = client.Client._get_keystone_client.return_value
        kc.service_catalog = mock.Mock()
        kc.service_catalog.get_endpoints = mock.Mock(return_value=self.catalog)
        self.assertRaises(RuntimeError, client.Client,
                          api_version=manilaclient.API_MAX_VERSION,
                          region_name='FakeRegion')
        self.assertTrue(client.Client._get_keystone_client.called)
        kc.service_catalog.get_endpoints.assert_called_with('sharev2')

    @mock.patch.object(client.Client, '_get_keystone_client', mock.Mock())
    def test_regions_with_same_name(self):
        self.mock_object(client.httpclient, 'HTTPClient')
        catalog = {
            'sharev2': [
                {'region': 'FirstRegion', 'publicURL': 'http://1.2.3.4'},
                {'region': 'secondregion', 'publicURL': 'http://1.1.1.1'},
                {'region': 'SecondRegion', 'publicURL': 'http://2.2.2.2'},
            ],
        }
        kc = client.Client._get_keystone_client.return_value
        kc.service_catalog = mock.Mock()
        kc.service_catalog.get_endpoints = mock.Mock(return_value=catalog)
        c = client.Client(api_version=manilaclient.API_MIN_VERSION,
                          service_type='sharev2',
                          region_name='SecondRegion')
        self.assertTrue(client.Client._get_keystone_client.called)
        kc.service_catalog.get_endpoints.assert_called_with('sharev2')
        client.httpclient.HTTPClient.assert_called_with(
            'http://2.2.2.2',
            mock.ANY,
            'python-manilaclient',
            insecure=False,
            cacert=None,
            cert=None,
            timeout=None,
            retries=None,
            http_log_debug=False,
            api_version=manilaclient.API_MIN_VERSION)
        self.assertIsNotNone(c.client)

    def _get_client_args(self, **kwargs):
        client_args = {
            'auth_url': 'http://identity.example.com',
            'api_version': manilaclient.API_DEPRECATED_VERSION,
            'username': 'fake_username',
            'service_type': 'sharev2',
            'region_name': 'SecondRegion',
            'input_auth_token': None,
            'session': None,
            'service_catalog_url': None,
            'user_id': 'foo_user_id',
            'user_domain_name': 'foo_user_domain_name',
            'user_domain_id': 'foo_user_domain_id',
            'project_name': 'foo_project_name',
            'project_domain_name': 'foo_project_domain_name',
            'project_domain_id': 'foo_project_domain_id',
            'endpoint_type': 'publicUrl',
            'cert': 'foo_cert',
        }
        client_args.update(kwargs)
        return client_args

    @ddt.data(
        {'auth_url': 'http://identity.example.com',
         'password': 'password_backward_compat',
         'endpoint_type': 'publicURL',
         'project_id': 'foo_tenant_project_id'},
        {'password': 'renamed_api_key', 'endpoint_type': 'public',
         'tenant_id': 'foo_tenant_project_id'},
    )
    def test_client_init_no_session_no_auth_token(self, kwargs):
        def fake_url_for(version):
            if version == 'v3.0':
                return 'url_v3.0'
            else:
                return None

        self.mock_object(client.httpclient, 'HTTPClient')
        self.mock_object(client.ks_client, 'Client')
        self.mock_object(client.session.discover, 'Discover')
        self.mock_object(client.session, 'Session')
        client_args = self._get_client_args(**kwargs)
        client_args['api_version'] = manilaclient.API_MIN_VERSION
        self.auth_url = client_args['auth_url']
        catalog = {
            'share': [
                {'region': 'SecondRegion', 'region_id': 'SecondRegion',
                 'url': 'http://4.4.4.4', 'interface': 'public',
                 },
            ],
            'sharev2': [
                {'region': 'FirstRegion', 'interface': 'public',
                 'region_id': 'SecondRegion', 'url': 'http://1.1.1.1'},
                {'region': 'secondregion', 'interface': 'public',
                 'region_id': 'SecondRegion', 'url': 'http://2.2.2.2'},
                {'region': 'SecondRegion', 'interface': 'internal',
                 'region_id': 'SecondRegion', 'url': 'http://3.3.3.1'},
                {'region': 'SecondRegion', 'interface': 'public',
                 'region_id': 'SecondRegion', 'url': 'http://3.3.3.3'},
                {'region': 'SecondRegion', 'interface': 'admin',
                 'region_id': 'SecondRegion', 'url': 'http://3.3.3.2'},
            ],
        }
        client.session.discover.Discover.return_value.url_for.side_effect = (
            fake_url_for)
        client.ks_client.Client.return_value.auth_token.return_value = (
            'fake_token')
        mocked_ks_client = client.ks_client.Client.return_value
        mocked_ks_client.service_catalog.get_endpoints.return_value = catalog

        client.Client(**client_args)

        client.httpclient.HTTPClient.assert_called_with(
            'http://3.3.3.3', mock.ANY, 'python-manilaclient', insecure=False,
            cacert=None, cert=client_args['cert'], timeout=None, retries=None,
            http_log_debug=False, api_version=manilaclient.API_MIN_VERSION)

        client.ks_client.Client.assert_called_with(
            session=mock.ANY, version=(3, 0), auth_url='url_v3.0',
            username=client_args['username'],
            password=client_args.get('password'),
            user_id=client_args['user_id'],
            user_domain_name=client_args['user_domain_name'],
            user_domain_id=client_args['user_domain_id'],
            project_id=client_args.get('tenant_id',
                                       client_args.get('project_id')),
            project_name=client_args['project_name'],
            project_domain_name=client_args['project_domain_name'],
            project_domain_id=client_args['project_domain_id'],
            region_name=client_args['region_name'],
        )
        mocked_ks_client.service_catalog.get_endpoints.assert_called_with(
            client_args['service_type'])
        mocked_ks_client.authenticate.assert_called_with()

    @mock.patch.object(client.ks_client, 'Client', mock.Mock())
    @mock.patch.object(client.session.discover, 'Discover', mock.Mock())
    @mock.patch.object(client.session, 'Session', mock.Mock())
    def test_client_init_no_session_no_auth_token_endpoint_not_found(self):
        self.mock_object(client.httpclient, 'HTTPClient')
        client_args = self._get_client_args(
            auth_urli='fake_url',
            password='foo_password',
            tenant_id='foo_tenant_id')
        discover = client.session.discover.Discover
        discover.return_value.url_for.return_value = None
        mocked_ks_client = client.ks_client.Client.return_value

        self.assertRaises(
            exceptions.CommandError, client.Client, **client_args)

        self.assertTrue(client.session.Session.called)
        self.assertTrue(client.session.discover.Discover.called)
        self.assertFalse(client.httpclient.HTTPClient.called)
        self.assertFalse(client.ks_client.Client.called)
        self.assertFalse(mocked_ks_client.service_catalog.get_endpoints.called)
        self.assertFalse(mocked_ks_client.authenticate.called)