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='******', 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_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='******', 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": "******"}}}' }) 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)
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)
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)
def test_client_get(self): auth_url = 'http://www.blah.com' instance = other_client.HTTPClient(user='******', 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_delete(self): auth_url = 'http://www.blah.com' instance = other_client.HTTPClient(user='******', 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')
def test_client_put(self): auth_url = 'http://www.blah.com' body = {"user": {"password": "******"}} instance = other_client.HTTPClient(user='******', 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_post(self): auth_url = 'http://www.blah.com' body = {"add_shard": {}} instance = other_client.HTTPClient(user='******', 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_patch(self): auth_url = 'http://www.blah.com' body = mock.Mock() instance = other_client.HTTPClient(user='******', 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_bad_request(self): instance = other_client.HTTPClient(user='******', 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='******', 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='******', 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')
def test_client_with_timeout(self): instance = other_client.HTTPClient(user='******', 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 _check_version_url(self, management_url, version_url, mock_request): projectid = '25e469aa1848471b875e68cde6531bc5' instance = other_client.HTTPClient(user='******', 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 __init__(self, username, password, project_id=None, auth_url='', insecure=False, timeout=None, tenant_id=None, proxy_tenant_id=None, proxy_token=None, region_name=None, endpoint_type='publicURL', extensions=None, service_type='database', service_name=None, database_service_name=None, retries=None, http_log_debug=False, cacert=None, bypass_url=None): # self.limits = limits.LimitsManager(self) # extensions self.flavors = Flavors(self) self.users = Users(self) self.databases = Databases(self) self.backups = Backups(self) self.instances = Instances(self) self.limits = Limits(self) self.root = Root(self) self.security_group_rules = SecurityGroupRules(self) self.security_groups = SecurityGroups(self) #self.hosts = Hosts(self) #self.quota = Quotas(self) #self.storage = StorageInfo(self) #self.management = Management(self) #self.mgmt_flavor = MgmtFlavors(self) #self.accounts = Accounts(self) #self.diagnostics = DiagnosticsInterrogator(self) #self.hwinfo = HwInfoInterrogator(self) # Add in any extensions... if extensions: for extension in extensions: if extension.manager_class: setattr(self, extension.name, extension.manager_class(self)) self.client = client.HTTPClient( username, password, project_id, auth_url, insecure=insecure, timeout=timeout, tenant_id=tenant_id, proxy_token=proxy_token, proxy_tenant_id=proxy_tenant_id, region_name=region_name, endpoint_type=endpoint_type, service_type=service_type, service_name=service_name, database_service_name=database_service_name, retries=retries, http_log_debug=http_log_debug, cacert=cacert, bypass_url=bypass_url)