2013-03-13 18:09:17 -04:00
|
|
|
# Copyright 2012 OpenStack Foundation
|
2013-01-11 21:44:56 -08:00
|
|
|
# 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 mock
|
|
|
|
import requests
|
2011-12-29 15:37:05 -05:00
|
|
|
|
|
|
|
import novaclient.client
|
2013-09-09 16:28:12 +03:00
|
|
|
import novaclient.extension
|
|
|
|
import novaclient.tests.fakes as fakes
|
2013-12-11 17:38:56 +00:00
|
|
|
from novaclient.tests import utils
|
2011-12-29 15:37:05 -05:00
|
|
|
import novaclient.v1_1.client
|
2013-09-09 16:28:12 +03:00
|
|
|
import novaclient.v3.client
|
2011-12-29 15:37:05 -05:00
|
|
|
|
2013-10-03 21:32:22 +08:00
|
|
|
import json
|
|
|
|
|
2011-12-29 15:37:05 -05:00
|
|
|
|
2014-03-26 15:22:03 +04:00
|
|
|
class ClientConnectionPoolTest(utils.TestCase):
|
|
|
|
|
|
|
|
@mock.patch("novaclient.client.adapters.HTTPAdapter")
|
|
|
|
def test_get(self, mock_http_adapter):
|
|
|
|
mock_http_adapter.side_effect = lambda: mock.Mock()
|
|
|
|
pool = novaclient.client._ClientConnectionPool()
|
|
|
|
self.assertEqual(pool.get("abc"), pool.get("abc"))
|
|
|
|
self.assertNotEqual(pool.get("abc"), pool.get("def"))
|
|
|
|
|
|
|
|
|
2011-12-29 15:37:05 -05:00
|
|
|
class ClientTest(utils.TestCase):
|
|
|
|
|
2013-01-11 21:44:56 -08:00
|
|
|
def test_client_with_timeout(self):
|
|
|
|
instance = novaclient.client.HTTPClient(user='user',
|
|
|
|
password='password',
|
|
|
|
projectid='project',
|
|
|
|
timeout=2,
|
|
|
|
auth_url="http://www.blah.com")
|
|
|
|
self.assertEqual(instance.timeout, 2)
|
|
|
|
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',
|
|
|
|
}
|
2014-03-26 15:22:03 +04:00
|
|
|
with mock.patch('requests.request', mock_request):
|
2013-01-11 21:44:56 -08:00
|
|
|
instance.authenticate()
|
2014-03-26 15:22:03 +04:00
|
|
|
requests.request.assert_called_with(mock.ANY, mock.ANY,
|
2013-04-18 18:01:54 +03:00
|
|
|
timeout=2,
|
|
|
|
headers=mock.ANY,
|
|
|
|
verify=mock.ANY)
|
2013-01-11 21:44:56 -08:00
|
|
|
|
2013-06-19 18:27:24 +00:00
|
|
|
def test_client_reauth(self):
|
|
|
|
instance = novaclient.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.version = 'v2.0'
|
|
|
|
mock_request = mock.Mock()
|
|
|
|
mock_request.side_effect = novaclient.exceptions.Unauthorized(401)
|
2014-03-26 15:22:03 +04:00
|
|
|
with mock.patch('requests.request', mock_request):
|
2013-06-19 18:27:24 +00:00
|
|
|
try:
|
|
|
|
instance.get('/servers/detail')
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
get_headers = {'X-Auth-Project-Id': 'project',
|
|
|
|
'X-Auth-Token': 'foobar',
|
|
|
|
'User-Agent': 'python-novaclient',
|
|
|
|
'Accept': 'application/json'}
|
|
|
|
reauth_headers = {'Content-Type': 'application/json',
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'User-Agent': 'python-novaclient'}
|
2013-10-03 21:32:22 +08:00
|
|
|
data = {
|
|
|
|
"auth": {
|
|
|
|
"tenantName": "project",
|
|
|
|
"passwordCredentials": {
|
|
|
|
"username": "user",
|
|
|
|
"password": "password"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-19 18:27:24 +00:00
|
|
|
expected = [mock.call('GET',
|
|
|
|
'http://example.com/servers/detail',
|
|
|
|
timeout=mock.ANY,
|
|
|
|
headers=get_headers,
|
|
|
|
verify=mock.ANY),
|
|
|
|
mock.call('POST', 'http://www.blah.com/tokens',
|
|
|
|
timeout=mock.ANY,
|
|
|
|
headers=reauth_headers,
|
|
|
|
allow_redirects=mock.ANY,
|
2013-10-03 21:32:22 +08:00
|
|
|
data=json.dumps(data),
|
2013-06-19 18:27:24 +00:00
|
|
|
verify=mock.ANY)]
|
|
|
|
self.assertEqual(mock_request.call_args_list, expected)
|
|
|
|
|
2013-09-09 16:28:12 +03:00
|
|
|
def test_get_client_class_v3(self):
|
|
|
|
output = novaclient.client.get_client_class('3')
|
|
|
|
self.assertEqual(output, novaclient.v3.client.Client)
|
|
|
|
|
2011-12-29 15:37:05 -05:00
|
|
|
def test_get_client_class_v2(self):
|
|
|
|
output = novaclient.client.get_client_class('2')
|
|
|
|
self.assertEqual(output, novaclient.v1_1.client.Client)
|
|
|
|
|
|
|
|
def test_get_client_class_v2_int(self):
|
|
|
|
output = novaclient.client.get_client_class(2)
|
|
|
|
self.assertEqual(output, novaclient.v1_1.client.Client)
|
|
|
|
|
|
|
|
def test_get_client_class_v1_1(self):
|
|
|
|
output = novaclient.client.get_client_class('1.1')
|
|
|
|
self.assertEqual(output, novaclient.v1_1.client.Client)
|
|
|
|
|
|
|
|
def test_get_client_class_unknown(self):
|
|
|
|
self.assertRaises(novaclient.exceptions.UnsupportedVersion,
|
|
|
|
novaclient.client.get_client_class, '0')
|
2012-12-07 11:47:49 -05:00
|
|
|
|
|
|
|
def test_client_with_os_cache_enabled(self):
|
|
|
|
cs = novaclient.v1_1.client.Client("user", "password", "project_id",
|
|
|
|
auth_url="foo/v2", os_cache=True)
|
|
|
|
self.assertEqual(True, cs.os_cache)
|
|
|
|
self.assertEqual(True, cs.client.os_cache)
|
|
|
|
|
|
|
|
def test_client_with_os_cache_disabled(self):
|
|
|
|
cs = novaclient.v1_1.client.Client("user", "password", "project_id",
|
|
|
|
auth_url="foo/v2", os_cache=False)
|
|
|
|
self.assertEqual(False, cs.os_cache)
|
|
|
|
self.assertEqual(False, cs.client.os_cache)
|
|
|
|
|
|
|
|
def test_client_with_no_cache_enabled(self):
|
|
|
|
cs = novaclient.v1_1.client.Client("user", "password", "project_id",
|
|
|
|
auth_url="foo/v2", no_cache=True)
|
|
|
|
self.assertEqual(False, cs.os_cache)
|
|
|
|
self.assertEqual(False, cs.client.os_cache)
|
|
|
|
|
|
|
|
def test_client_with_no_cache_disabled(self):
|
|
|
|
cs = novaclient.v1_1.client.Client("user", "password", "project_id",
|
|
|
|
auth_url="foo/v2", no_cache=False)
|
|
|
|
self.assertEqual(True, cs.os_cache)
|
|
|
|
self.assertEqual(True, cs.client.os_cache)
|
2013-09-09 12:11:32 +03:00
|
|
|
|
|
|
|
def test_client_set_management_url_v1_1(self):
|
|
|
|
cs = novaclient.v1_1.client.Client("user", "password", "project_id",
|
|
|
|
auth_url="foo/v2")
|
|
|
|
cs.set_management_url("blabla")
|
|
|
|
self.assertEqual("blabla", cs.client.management_url)
|
|
|
|
|
|
|
|
def test_client_get_reset_timings_v1_1(self):
|
|
|
|
cs = novaclient.v1_1.client.Client("user", "password", "project_id",
|
|
|
|
auth_url="foo/v2")
|
|
|
|
self.assertEqual(0, len(cs.get_timings()))
|
|
|
|
cs.client.times.append("somevalue")
|
|
|
|
self.assertEqual(1, len(cs.get_timings()))
|
|
|
|
self.assertEqual("somevalue", cs.get_timings()[0])
|
|
|
|
|
|
|
|
cs.reset_timings()
|
|
|
|
self.assertEqual(0, len(cs.get_timings()))
|
2013-09-09 16:28:12 +03:00
|
|
|
|
|
|
|
def test_client_set_management_url_v3(self):
|
|
|
|
cs = novaclient.v3.client.Client("user", "password", "project_id",
|
|
|
|
auth_url="foo/v2")
|
|
|
|
cs.set_management_url("blabla")
|
|
|
|
self.assertEqual("blabla", cs.client.management_url)
|
|
|
|
|
|
|
|
def test_client_get_reset_timings_v3(self):
|
|
|
|
cs = novaclient.v3.client.Client("user", "password", "project_id",
|
|
|
|
auth_url="foo/v2")
|
|
|
|
self.assertEqual(0, len(cs.get_timings()))
|
|
|
|
cs.client.times.append("somevalue")
|
|
|
|
self.assertEqual(["somevalue"], cs.get_timings())
|
|
|
|
|
|
|
|
cs.reset_timings()
|
2013-09-30 22:25:17 +02:00
|
|
|
self.assertEqual(0, len(cs.get_timings()))
|
2013-09-09 16:28:12 +03:00
|
|
|
|
|
|
|
def test_clent_extensions_v3(self):
|
|
|
|
fake_attribute_name1 = "FakeAttribute1"
|
|
|
|
fake_attribute_name2 = "FakeAttribute2"
|
|
|
|
extensions = [
|
|
|
|
novaclient.extension.Extension(fake_attribute_name1,
|
|
|
|
fakes),
|
|
|
|
novaclient.extension.Extension(fake_attribute_name2,
|
|
|
|
utils),
|
|
|
|
]
|
|
|
|
|
|
|
|
cs = novaclient.v3.client.Client("user", "password", "project_id",
|
|
|
|
auth_url="foo/v2",
|
|
|
|
extensions=extensions)
|
2014-01-15 10:42:19 +08:00
|
|
|
self.assertIsInstance(getattr(cs, fake_attribute_name1, None),
|
|
|
|
fakes.FakeManager)
|
2013-09-09 16:28:12 +03:00
|
|
|
self.assertFalse(hasattr(cs, fake_attribute_name2))
|
|
|
|
|
|
|
|
@mock.patch.object(novaclient.client.HTTPClient, 'authenticate')
|
|
|
|
def test_authenticate_call_v3(self, mock_authenticate):
|
|
|
|
cs = novaclient.v3.client.Client("user", "password", "project_id",
|
|
|
|
auth_url="foo/v2")
|
|
|
|
cs.authenticate()
|
|
|
|
self.assertTrue(mock_authenticate.called)
|
2013-12-19 19:26:06 +00:00
|
|
|
|
2014-03-26 15:22:03 +04:00
|
|
|
@mock.patch('novaclient.client.HTTPClient')
|
|
|
|
def test_contextmanager_v1_1(self, mock_http_client):
|
|
|
|
fake_client = mock.Mock()
|
|
|
|
mock_http_client.return_value = fake_client
|
|
|
|
with novaclient.v1_1.client.Client("user", "password", "project_id",
|
|
|
|
auth_url="foo/v2") as client:
|
|
|
|
pass
|
|
|
|
self.assertTrue(fake_client.open_session.called)
|
|
|
|
self.assertTrue(fake_client.close_session.called)
|
|
|
|
|
|
|
|
@mock.patch('novaclient.client.HTTPClient')
|
|
|
|
def test_contextmanager_v3(self, mock_http_client):
|
|
|
|
fake_client = mock.Mock()
|
|
|
|
mock_http_client.return_value = fake_client
|
|
|
|
with novaclient.v3.client.Client("user", "password", "project_id",
|
|
|
|
auth_url="foo/v2") as client:
|
|
|
|
pass
|
|
|
|
self.assertTrue(fake_client.open_session.called)
|
|
|
|
self.assertTrue(fake_client.close_session.called)
|
|
|
|
|
2013-12-19 19:26:06 +00:00
|
|
|
def test_get_password_simple(self):
|
|
|
|
cs = novaclient.client.HTTPClient("user", "password", "", "")
|
|
|
|
cs.password_func = mock.Mock()
|
|
|
|
self.assertEqual(cs._get_password(), "password")
|
|
|
|
self.assertFalse(cs.password_func.called)
|
|
|
|
|
|
|
|
def test_get_password_none(self):
|
|
|
|
cs = novaclient.client.HTTPClient("user", None, "", "")
|
2014-02-17 20:09:28 +08:00
|
|
|
self.assertIsNone(cs._get_password())
|
2013-12-19 19:26:06 +00:00
|
|
|
|
|
|
|
def test_get_password_func(self):
|
|
|
|
cs = novaclient.client.HTTPClient("user", None, "", "")
|
|
|
|
cs.password_func = mock.Mock(return_value="password")
|
|
|
|
self.assertEqual(cs._get_password(), "password")
|
|
|
|
cs.password_func.assert_called_once_with()
|
|
|
|
|
|
|
|
cs.password_func = mock.Mock()
|
|
|
|
self.assertEqual(cs._get_password(), "password")
|
|
|
|
self.assertFalse(cs.password_func.called)
|
2014-03-18 00:21:21 +09:00
|
|
|
|
|
|
|
def test_auth_url_rstrip_slash(self):
|
|
|
|
cs = novaclient.client.HTTPClient("user", "password", "project_id",
|
|
|
|
auth_url="foo/v2/")
|
|
|
|
self.assertEqual(cs.auth_url, "foo/v2")
|
|
|
|
|
|
|
|
def test_token_and_bypass_url(self):
|
|
|
|
cs = novaclient.client.HTTPClient(None, None, None,
|
|
|
|
auth_token="12345",
|
|
|
|
bypass_url="compute/v100/")
|
|
|
|
self.assertIsNone(cs.auth_url)
|
|
|
|
self.assertEqual(cs.auth_token, "12345")
|
|
|
|
self.assertEqual(cs.bypass_url, "compute/v100")
|
|
|
|
self.assertEqual(cs.management_url, "compute/v100")
|
2014-03-26 15:22:03 +04:00
|
|
|
|
|
|
|
@mock.patch("novaclient.client.requests.Session")
|
|
|
|
def test_session(self, mock_session):
|
|
|
|
fake_session = mock.Mock()
|
|
|
|
mock_session.return_value = fake_session
|
|
|
|
cs = novaclient.client.HTTPClient("user", None, "", "")
|
|
|
|
cs.open_session()
|
|
|
|
self.assertEqual(cs._session, fake_session)
|
|
|
|
cs.close_session()
|
|
|
|
self.assertIsNone(cs._session)
|
|
|
|
|
|
|
|
def test_session_connection_pool(self):
|
|
|
|
cs = novaclient.client.HTTPClient("user", None, "",
|
|
|
|
"", connection_pool=True)
|
|
|
|
cs.open_session()
|
|
|
|
self.assertIsNone(cs._session)
|
|
|
|
cs.close_session()
|
|
|
|
self.assertIsNone(cs._session)
|
|
|
|
|
|
|
|
def test_get_session(self):
|
|
|
|
cs = novaclient.client.HTTPClient("user", None, "", "")
|
|
|
|
self.assertIsNone(cs._get_session("http://nooooooooo.com"))
|
|
|
|
|
|
|
|
@mock.patch("novaclient.client.requests.Session")
|
|
|
|
def test_get_session_open_session(self, mock_session):
|
|
|
|
fake_session = mock.Mock()
|
|
|
|
mock_session.return_value = fake_session
|
|
|
|
cs = novaclient.client.HTTPClient("user", None, "", "")
|
|
|
|
cs.open_session()
|
|
|
|
self.assertEqual(fake_session, cs._get_session("http://example.com"))
|
|
|
|
|
|
|
|
@mock.patch("novaclient.client.requests.Session")
|
|
|
|
@mock.patch("novaclient.client._ClientConnectionPool")
|
|
|
|
def test_get_session_connection_pool(self, mock_pool, mock_session):
|
|
|
|
service_url = "http://example.com"
|
|
|
|
|
|
|
|
pool = mock.MagicMock()
|
|
|
|
pool.get.return_value = "http_adapter"
|
|
|
|
mock_pool.return_value = pool
|
|
|
|
cs = novaclient.client.HTTPClient("user", None, "",
|
|
|
|
"", connection_pool=True)
|
|
|
|
cs._current_url = "http://another.com"
|
|
|
|
|
|
|
|
session = cs._get_session(service_url)
|
|
|
|
self.assertEqual(session, mock_session.return_value)
|
|
|
|
pool.get.assert_called_once_with(service_url)
|
|
|
|
mock_session().mount.assert_called_once_with(service_url,
|
|
|
|
'http_adapter')
|
|
|
|
|
|
|
|
def test_init_without_connection_pool(self):
|
|
|
|
cs = novaclient.client.HTTPClient("user", None, "", "")
|
|
|
|
self.assertIsNone(cs._connection_pool)
|
|
|
|
|
|
|
|
@mock.patch("novaclient.client._ClientConnectionPool")
|
|
|
|
def test_init_with_proper_connection_pool(self, mock_pool):
|
|
|
|
fake_pool = mock.Mock()
|
|
|
|
mock_pool.return_value = fake_pool
|
|
|
|
cs = novaclient.client.HTTPClient("user", None, "",
|
|
|
|
connection_pool=True)
|
|
|
|
self.assertEqual(cs._connection_pool, fake_pool)
|