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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
|
# Copyright 2011 OpenStack Foundation
# Copyright 2013 Rackspace Hosting
# Copyright 2013 Hewlett-Packard Development Company, L.P.
# 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.
import fixtures
from keystoneauth1 import adapter
import logging
import mock
import requests
import testtools
from troveclient.apiclient import client
from troveclient import client as other_client
from troveclient import exceptions
from troveclient import service_catalog
import troveclient.v1.client
class ClientTest(testtools.TestCase):
def test_get_client_class_v1(self):
version_map = other_client.get_version_map()
output = client.BaseClient.get_class('database',
'1.0', version_map)
self.assertEqual(troveclient.v1.client.Client, output)
def test_get_client_class_unknown(self):
version_map = other_client.get_version_map()
self.assertRaises(exceptions.UnsupportedVersion,
client.BaseClient.get_class, 'database',
'0', version_map)
def test_client_with_auth_system_without_auth_plugin(self):
self.assertRaisesRegexp(
exceptions.AuthSystemNotFound, "AuthSystemNotFound: 'something'",
other_client.HTTPClient, user='user', password='password',
projectid='project', timeout=2, auth_url="http://www.blah.com",
auth_system='something')
def test_client_with_auth_system_without_endpoint(self):
auth_plugin = mock.Mock()
auth_plugin.get_auth_url = mock.Mock(return_value=None)
self.assertRaises(
exceptions.EndpointNotFound,
other_client.HTTPClient, user='user', password='password',
projectid='project', timeout=2, auth_plugin=auth_plugin,
auth_url=None, auth_system='something')
def test_client_with_timeout(self):
instance = other_client.HTTPClient(user='user',
password='password',
projectid='project',
timeout=2,
auth_url="http://www.blah.com",
insecure=True)
self.assertEqual(2, instance.timeout)
mock_request = mock.Mock()
mock_request.return_value = requests.Response()
mock_request.return_value.status_code = 200
mock_request.return_value.headers = {
'x-server-management-url': 'blah.com',
'x-auth-token': 'blah',
}
with mock.patch('requests.request', mock_request):
instance.authenticate()
requests.request.assert_called_with(
mock.ANY, mock.ANY, timeout=2, headers=mock.ANY,
verify=mock.ANY)
def test_client_unauthorized(self):
instance = other_client.HTTPClient(user='user',
password='password',
projectid='project',
timeout=2,
auth_url="http://www.blah.com",
cacert=mock.Mock())
instance.auth_token = 'foobar'
instance.management_url = 'http://example.com'
instance.get_service_url = mock.Mock(return_value='http://example.com')
instance.version = 'v2.0'
mock_request = mock.Mock()
mock_request.side_effect = other_client.exceptions.Unauthorized(401)
with mock.patch('requests.request', mock_request):
self.assertRaises(
exceptions.Unauthorized, instance.get, '/instances')
def test_client_bad_request(self):
instance = other_client.HTTPClient(user='user',
password='password',
projectid='project',
timeout=2,
auth_url="http://www.blah.com")
instance.auth_token = 'foobar'
instance.management_url = 'http://example.com'
instance.get_service_url = mock.Mock(return_value='http://example.com')
instance.version = 'v2.0'
mock_request = mock.Mock()
mock_request.side_effect = other_client.exceptions.BadRequest()
with mock.patch('requests.request', mock_request):
self.assertRaises(
exceptions.BadRequest, instance.get, '/instances')
def test_client_with_client_exception(self):
instance = other_client.HTTPClient(user='user',
password='password',
projectid='project',
timeout=2,
auth_url="http://www.blah.com",
retries=2)
instance.auth_token = 'foobar'
instance.management_url = 'http://example.com'
instance.get_service_url = mock.Mock(return_value='http://example.com')
instance.version = 'v2.0'
mock_request = mock.Mock()
mock_request.side_effect = other_client.exceptions.ClientException()
type(mock_request.side_effect).code = mock.PropertyMock(
side_effect=[501, 111])
with mock.patch('requests.request', mock_request):
self.assertRaises(
exceptions.ClientException, instance.get, '/instances')
def test_client_connection_error(self):
instance = other_client.HTTPClient(user='user',
password='password',
projectid='project',
timeout=2,
auth_url="http://www.blah.com",
retries=2)
instance.auth_token = 'foobar'
instance.management_url = 'http://example.com'
instance.get_service_url = mock.Mock(return_value='http://example.com')
instance.version = 'v2.0'
mock_request = mock.Mock()
mock_request.side_effect = requests.exceptions.ConnectionError(
'connection refused')
with mock.patch('requests.request', mock_request):
self.assertRaisesRegexp(
exceptions.ClientException,
'Unable to establish connection: connection refused',
instance.get, '/instances')
@mock.patch.object(other_client.HTTPClient, 'request',
return_value=(200, "{'versions':[]}"))
def _check_version_url(self, management_url, version_url, mock_request):
projectid = '25e469aa1848471b875e68cde6531bc5'
instance = other_client.HTTPClient(user='user',
password='password',
projectid=projectid,
auth_url="http://www.blah.com")
instance.auth_token = 'foobar'
instance.management_url = management_url % projectid
mock_get_service_url = mock.Mock(return_value=instance.management_url)
instance.get_service_url = mock_get_service_url
instance.version = 'v2.0'
# If passing None as the part of url, a client accesses the url which
# doesn't include "v2/<projectid>" for getting API version info.
instance.get('')
mock_request.assert_called_once_with(instance.management_url, 'GET',
headers=mock.ANY)
mock_request.reset_mock()
# Otherwise, a client accesses the url which includes "v2/<projectid>".
instance.get('/instances')
url = instance.management_url + '/instances'
mock_request.assert_called_once_with(url, 'GET', headers=mock.ANY)
def test_client_version_url(self):
self._check_version_url('http://foo.com/v1/%s', 'http://foo.com/')
def test_client_version_url_with_tenant_name(self):
self._check_version_url('http://foo.com/trove/v1/%s',
'http://foo.com/trove/')
def test_log_req(self):
logger = self.useFixture(
fixtures.FakeLogger(
name='troveclient.client',
format="%(message)s",
level=logging.DEBUG,
nuke_handlers=True
)
)
cs = other_client.HTTPClient(user='user',
password='password',
projectid=None,
auth_url="http://www.blah.com",
http_log_debug=True)
cs.http_log_req(('/foo', 'GET'), {'headers': {}})
cs.http_log_req(('/foo', 'GET'),
{'headers': {'X-Auth-Token': 'totally_bogus'}})
cs.http_log_req(
('/foo', 'GET'),
{'headers': {},
'data': '{"auth": {"passwordCredentials": '
'{"password": "password"}}}'})
output = logger.output.split('\n')
self.assertIn("REQ: curl -i /foo -X GET", output)
self.assertIn(
"REQ: curl -i /foo -X GET -H "
'"X-Auth-Token: totally_bogus"',
output)
self.assertIn(
"REQ: curl -i /foo -X GET -d "
'\'{"auth": {"passwordCredentials": {"password":'
' "password"}}}\'',
output)
@mock.patch.object(service_catalog, 'ServiceCatalog')
def test_client_auth_token(self, mock_service_catalog):
auth_url = 'http://www.blah.com'
proxy_token = 'foobar'
proxy_tenant_id = 'user'
mock_service_catalog.return_value.get_token = mock.Mock(
return_value=proxy_token)
instance = other_client.HTTPClient(proxy_token=proxy_token,
proxy_tenant_id=proxy_tenant_id,
user=None,
password=None,
tenant_id=proxy_tenant_id,
projectid=None,
timeout=2,
auth_url=auth_url)
instance.management_url = 'http://example.com'
instance.get_service_url = mock.Mock(return_value='http://example.com')
instance.version = 'v2.0'
mock_request = mock.Mock()
mock_request.return_value = requests.Response()
mock_request.return_value.status_code = 200
mock_request.return_value.headers = {
'x-server-management-url': 'blah.com',
'x-auth-token': 'blah',
}
with mock.patch('requests.request', mock_request):
instance.authenticate()
mock_request.assert_called_with(
'GET', auth_url + '/tokens/foobar?belongsTo=user',
headers={'User-Agent': 'python-troveclient',
'Accept': 'application/json',
'X-Auth-Token': proxy_token},
timeout=2, verify=True)
@mock.patch.object(service_catalog, 'ServiceCatalog', side_effect=KeyError)
def test_client_auth_token_authorization_failure(self,
mock_service_catalog):
auth_url = 'http://www.blah.com'
proxy_token = 'foobar'
proxy_tenant_id = 'user'
mock_service_catalog.return_value.get_token = mock.Mock(
return_value=proxy_token)
instance = other_client.HTTPClient(proxy_token=proxy_token,
proxy_tenant_id=proxy_tenant_id,
user=None,
password=None,
tenant_id=proxy_tenant_id,
projectid=None,
timeout=2,
auth_url=auth_url)
instance.management_url = 'http://example.com'
instance.get_service_url = mock.Mock(return_value='http://example.com')
instance.version = 'v2.0'
mock_request = mock.Mock()
mock_request.return_value = requests.Response()
mock_request.return_value.status_code = 200
mock_request.return_value.headers = {
'x-server-management-url': 'blah.com',
'x-auth-token': 'blah',
}
with mock.patch('requests.request', mock_request):
self.assertRaises(exceptions.AuthorizationFailure,
instance.authenticate)
@mock.patch.object(service_catalog, 'ServiceCatalog',
side_effect=other_client.exceptions.EndpointNotFound)
def test_client_auth_token_endpoint_not_found(self, mock_service_catalog):
auth_url = 'http://www.blah.com'
proxy_token = 'foobar'
proxy_tenant_id = 'user'
mock_service_catalog.return_value.get_token = mock.Mock(
return_value=proxy_token)
instance = other_client.HTTPClient(proxy_token=proxy_token,
proxy_tenant_id=proxy_tenant_id,
user=None,
password=None,
tenant_id=proxy_tenant_id,
projectid=None,
timeout=2,
auth_url=auth_url)
instance.management_url = 'http://example.com'
instance.get_service_url = mock.Mock(return_value='http://example.com')
instance.version = 'v2.0'
mock_request = mock.Mock()
mock_request.return_value = requests.Response()
mock_request.return_value.status_code = 200
mock_request.return_value.headers = {
'x-server-management-url': 'blah.com',
'x-auth-token': 'blah',
}
with mock.patch('requests.request', mock_request):
self.assertRaises(exceptions.EndpointNotFound,
instance.authenticate)
@mock.patch.object(service_catalog, 'ServiceCatalog')
def test_client_auth_token_v1_auth_failure(self, mock_service_catalog):
auth_url = 'http://www.blah.com'
proxy_token = 'foobar'
proxy_tenant_id = 'user'
mock_service_catalog.return_value.get_token = mock.Mock(
return_value=proxy_token)
instance = other_client.HTTPClient(proxy_token=proxy_token,
proxy_tenant_id=proxy_tenant_id,
user=None,
password=None,
tenant_id=proxy_tenant_id,
projectid=None,
timeout=2,
auth_url=auth_url)
instance.management_url = 'http://example.com'
instance.get_service_url = mock.Mock(return_value='http://example.com')
instance.version = 'v1.0'
mock_request = mock.Mock()
mock_request.return_value = requests.Response()
mock_request.return_value.status_code = 200
mock_request.return_value.headers = {
'x-server-management-url': 'blah.com',
'x-auth-token': 'blah',
}
with mock.patch('requests.request', mock_request):
self.assertRaises(exceptions.NoTokenLookupException,
instance.authenticate)
@mock.patch.object(service_catalog, 'ServiceCatalog')
def test_client_auth_token_v1_auth(self, mock_service_catalog):
auth_url = 'http://www.blah.com'
proxy_token = 'foobar'
mock_service_catalog.return_value.get_token = mock.Mock(
return_value=proxy_token)
instance = other_client.HTTPClient(user='user',
password='password',
projectid='projectid',
timeout=2,
auth_url=auth_url)
instance.management_url = 'http://example.com'
instance.get_service_url = mock.Mock(return_value='http://example.com')
instance.version = 'v1.0'
mock_request = mock.Mock()
mock_request.return_value = requests.Response()
mock_request.return_value.status_code = 200
mock_request.return_value.headers = {
'x-server-management-url': 'blah.com',
}
headers = {'Content-Type': 'application/json',
'Accept': 'application/json',
'User-Agent': 'python-troveclient'}
with mock.patch('requests.request', mock_request):
instance.authenticate()
called_args, called_kwargs = mock_request.call_args
self.assertEqual(('POST', 'http://www.blah.com/v2.0/tokens'),
called_args)
self.assertEqual(headers, called_kwargs['headers'])
def test_client_get(self):
auth_url = 'http://www.blah.com'
instance = other_client.HTTPClient(user='user',
password='password',
projectid='project_id',
timeout=2,
auth_url=auth_url)
instance._cs_request = mock.Mock()
instance.get('clusters')
instance._cs_request.assert_called_with('clusters', 'GET')
def test_client_patch(self):
auth_url = 'http://www.blah.com'
body = mock.Mock()
instance = other_client.HTTPClient(user='user',
password='password',
projectid='project_id',
timeout=2,
auth_url=auth_url)
instance._cs_request = mock.Mock()
instance.patch('instances/dummy-instance-id', body=body)
instance._cs_request.assert_called_with(
'instances/dummy-instance-id', 'PATCH', body=body)
def test_client_post(self):
auth_url = 'http://www.blah.com'
body = {"add_shard": {}}
instance = other_client.HTTPClient(user='user',
password='password',
projectid='project_id',
timeout=2,
auth_url=auth_url)
instance._cs_request = mock.Mock()
instance.post('clusters/dummy-cluster-id', body=body)
instance._cs_request.assert_called_with(
'clusters/dummy-cluster-id', 'POST', body=body)
def test_client_put(self):
auth_url = 'http://www.blah.com'
body = {"user": {"password": "new_password"}}
instance = other_client.HTTPClient(user='user',
password='password',
projectid='project_id',
timeout=2,
auth_url=auth_url)
instance._cs_request = mock.Mock()
instance.put('instances/dummy-instance-id/user/dummy-user', body=body)
instance._cs_request.assert_called_with(
'instances/dummy-instance-id/user/dummy-user', 'PUT', body=body)
def test_client_delete(self):
auth_url = 'http://www.blah.com'
instance = other_client.HTTPClient(user='user',
password='password',
projectid='project_id',
timeout=2,
auth_url=auth_url)
instance._cs_request = mock.Mock()
instance.delete('/backups/dummy-backup-id')
instance._cs_request.assert_called_with('/backups/dummy-backup-id',
'DELETE')
@mock.patch.object(adapter.LegacyJsonAdapter, 'request')
def test_database_service_name(self, m_request):
m_request.return_value = (mock.MagicMock(status_code=200), None)
client = other_client.SessionClient(session=mock.MagicMock(),
auth=mock.MagicMock())
client.request("http://no.where", 'GET')
self.assertIsNone(client.database_service_name)
client = other_client.SessionClient(session=mock.MagicMock(),
auth=mock.MagicMock(),
database_service_name='myservice')
client.request("http://no.where", 'GET')
self.assertEqual('myservice', client.database_service_name)
@mock.patch.object(adapter.LegacyJsonAdapter, 'request')
@mock.patch.object(adapter.LegacyJsonAdapter, 'get_endpoint',
return_value=None)
def test_error_sessionclient(self, m_end_point, m_request):
m_request.return_value = (mock.MagicMock(status_code=200), None)
self.assertRaises(exceptions.EndpointNotFound,
other_client.SessionClient,
session=mock.MagicMock(),
auth=mock.MagicMock())
def test_construct_http_client(self):
mock_request = mock.Mock()
mock_request.return_value = requests.Response()
mock_request.return_value.status_code = 200
mock_request.return_value.headers = {
'x-server-management-url': 'blah.com',
'x-auth-token': 'blah',
}
with mock.patch('requests.request', mock_request):
self.assertIsInstance(other_client._construct_http_client(),
other_client.HTTPClient)
self.assertIsInstance(
other_client._construct_http_client(session=mock.Mock(),
auth=mock.Mock()),
other_client.SessionClient)
|