Test: Clean v2 client userwarning

Problem:
When we run nova client unit test, there are lots of UserWarnings
printed on the screen. These warnings mean to remind users not to
use v2.client directly.

Solution:
1. In top level tests such as some tests in test_auth_plugins.py
test_client.py and fixture_data/client.py, we use updated usage
novaclient.client instead of using novaclient.v2.client directly.
2. In v2 unit tests, we clean those warnings with setting direct_use
False.

Change-Id: I70682e54874860f1d67d29325811c9da616bb705
Closes-Bug: #1532711
This commit is contained in:
Chaozhe.Chen 2016-01-11 16:45:25 +08:00
parent ff980114b6
commit 14f63647e9
7 changed files with 55 additions and 45 deletions

View File

@ -15,7 +15,7 @@ from keystoneauth1 import fixture
from keystoneauth1 import loading
from keystoneauth1 import session
from novaclient.v2 import client as v2client
from novaclient import client
IDENTITY_URL = 'http://identityserver:5000/v2.0'
COMPUTE_URL = 'http://compute.host'
@ -55,10 +55,10 @@ class V1(fixtures.Fixture):
self.client = self.new_client()
def new_client(self):
return v2client.Client(username='xx',
api_key='xx',
project_id='xx',
auth_url=self.identity_url)
return client.Client("2", username='xx',
api_key='xx',
project_id='xx',
auth_url=self.identity_url)
class SessionV1(V1):
@ -68,4 +68,4 @@ class SessionV1(V1):
loader = loading.get_plugin_loader('password')
self.session.auth = loader.load_from_options(
auth_url=self.identity_url, username='xx', password='xx')
return v2client.Client(session=self.session)
return client.Client("2", session=self.session)

View File

@ -26,9 +26,9 @@ except ImportError:
import simplejson as json
from novaclient import auth_plugin
from novaclient import client
from novaclient import exceptions
from novaclient.tests.unit import utils
from novaclient.v2 import client
def mock_http_request(resp=None):
@ -80,7 +80,7 @@ class DeprecatedAuthPluginTest(utils.TestCase):
@mock.patch.object(requests, "request", mock_request)
def test_auth_call():
plugin = auth_plugin.DeprecatedAuthPlugin("fake")
cs = client.Client("username", "password", "project_id",
cs = client.Client("2", "username", "password", "project_id",
utils.AUTH_URL_V2, auth_system="fake",
auth_plugin=plugin)
cs.client.authenticate()
@ -110,7 +110,7 @@ class DeprecatedAuthPluginTest(utils.TestCase):
def test_auth_call():
auth_plugin.discover_auth_systems()
plugin = auth_plugin.DeprecatedAuthPlugin("notexists")
cs = client.Client("username", "password", "project_id",
cs = client.Client("2", "username", "password", "project_id",
utils.AUTH_URL_V2, auth_system="notexists",
auth_plugin=plugin)
self.assertRaises(exceptions.AuthSystemNotFound,
@ -158,7 +158,7 @@ class DeprecatedAuthPluginTest(utils.TestCase):
@mock.patch.object(requests, "request", mock_request)
def test_auth_call():
plugin = auth_plugin.DeprecatedAuthPlugin("fakewithauthurl")
cs = client.Client("username", "password", "project_id",
cs = client.Client("2", "username", "password", "project_id",
auth_system="fakewithauthurl",
auth_plugin=plugin)
cs.client.authenticate()
@ -186,7 +186,7 @@ class DeprecatedAuthPluginTest(utils.TestCase):
plugin = auth_plugin.DeprecatedAuthPlugin("fakewithauthurl")
self.assertRaises(
exceptions.EndpointNotFound,
client.Client, "username", "password", "project_id",
client.Client, "2", "username", "password", "project_id",
auth_system="fakewithauthurl", auth_plugin=plugin)
@ -213,7 +213,7 @@ class AuthPluginTest(utils.TestCase):
auth_plugin.discover_auth_systems()
plugin = auth_plugin.load_plugin("fake")
cs = client.Client("username", "password", "project_id",
cs = client.Client("2", "username", "password", "project_id",
utils.AUTH_URL_V2, auth_system="fake",
auth_plugin=plugin)
cs.client.authenticate()
@ -304,7 +304,7 @@ class AuthPluginTest(utils.TestCase):
auth_plugin.discover_auth_systems()
plugin = auth_plugin.load_plugin("fake")
cs = client.Client("username", "password", "project_id",
cs = client.Client("2", "username", "password", "project_id",
auth_system="fakewithauthurl",
auth_plugin=plugin)
self.assertEqual("http://faked/v2.0", cs.client.auth_url)
@ -330,7 +330,7 @@ class AuthPluginTest(utils.TestCase):
self.assertRaises(
exceptions.EndpointNotFound,
client.Client, "username", "password", "project_id",
client.Client, "2", "username", "password", "project_id",
auth_system="fake", auth_plugin=plugin)
@mock.patch.object(pkg_resources, "iter_entry_points")
@ -354,5 +354,5 @@ class AuthPluginTest(utils.TestCase):
self.assertRaises(
exceptions.EndpointNotFound,
client.Client, "username", "password", "project_id",
client.Client, "2", "username", "password", "project_id",
auth_system="fake", auth_plugin=plugin)

View File

@ -171,38 +171,38 @@ class ClientTest(utils.TestCase):
novaclient.client.get_client_class, '2.latest')
def test_client_with_os_cache_enabled(self):
cs = novaclient.v2.client.Client("user", "password", "project_id",
auth_url="foo/v2", os_cache=True)
cs = novaclient.client.Client("2", "user", "password", "project_id",
auth_url="foo/v2", os_cache=True)
self.assertTrue(cs.os_cache)
self.assertTrue(cs.client.os_cache)
def test_client_with_os_cache_disabled(self):
cs = novaclient.v2.client.Client("user", "password", "project_id",
auth_url="foo/v2", os_cache=False)
cs = novaclient.client.Client("2", "user", "password", "project_id",
auth_url="foo/v2", os_cache=False)
self.assertFalse(cs.os_cache)
self.assertFalse(cs.client.os_cache)
def test_client_with_no_cache_enabled(self):
cs = novaclient.v2.client.Client("user", "password", "project_id",
auth_url="foo/v2", no_cache=True)
cs = novaclient.client.Client("2", "user", "password", "project_id",
auth_url="foo/v2", no_cache=True)
self.assertFalse(cs.os_cache)
self.assertFalse(cs.client.os_cache)
def test_client_with_no_cache_disabled(self):
cs = novaclient.v2.client.Client("user", "password", "project_id",
auth_url="foo/v2", no_cache=False)
cs = novaclient.client.Client("2", "user", "password", "project_id",
auth_url="foo/v2", no_cache=False)
self.assertTrue(cs.os_cache)
self.assertTrue(cs.client.os_cache)
def test_client_set_management_url_v1_1(self):
cs = novaclient.v2.client.Client("user", "password", "project_id",
auth_url="foo/v2")
cs = novaclient.client.Client("2", "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.v2.client.Client("user", "password", "project_id",
auth_url="foo/v2")
cs = novaclient.client.Client("2", "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()))
@ -215,8 +215,8 @@ class ClientTest(utils.TestCase):
def test_contextmanager_v1_1(self, mock_http_client):
fake_client = mock.Mock()
mock_http_client.return_value = fake_client
with novaclient.v2.client.Client("user", "password", "project_id",
auth_url="foo/v2"):
with novaclient.client.Client("2", "user", "password", "project_id",
auth_url="foo/v2"):
pass
self.assertTrue(fake_client.open_session.called)
self.assertTrue(fake_client.close_session.called)

View File

@ -20,7 +20,8 @@ class FakeClient(fakes.FakeClient):
def __init__(self, *args, **kwargs):
client.Client.__init__(self, 'username', 'password',
'project_id', 'auth_url',
extensions=kwargs.get('extensions'))
extensions=kwargs.get('extensions'),
direct_use=False)
self.client = FakeHTTPClient(**kwargs)

View File

@ -51,7 +51,8 @@ class FakeClient(fakes.FakeClient, client.Client):
def __init__(self, api_version=None, *args, **kwargs):
client.Client.__init__(self, 'username', 'password',
'project_id', 'auth_url',
extensions=kwargs.get('extensions'))
extensions=kwargs.get('extensions'),
direct_use=False)
self.api_version = api_version
self.client = FakeHTTPClient(**kwargs)
@ -2425,7 +2426,7 @@ class FakeSessionClient(fakes.FakeClient, client.Client):
client.Client.__init__(self, 'username', 'password',
'project_id', 'auth_url',
extensions=kwargs.get('extensions'),
api_version=api_version)
api_version=api_version, direct_use=False)
self.client = FakeSessionMockClient(**kwargs)

View File

@ -36,7 +36,8 @@ class AuthenticateAgainstKeystoneTests(utils.TestCase):
def test_authenticate_success(self):
cs = client.Client("username", "password", "project_id",
utils.AUTH_URL_V2, service_type='compute')
utils.AUTH_URL_V2, service_type='compute',
direct_use=False)
resp = self.get_token()
auth_response = utils.TestResponse({
@ -83,7 +84,7 @@ class AuthenticateAgainstKeystoneTests(utils.TestCase):
def test_authenticate_failure(self):
cs = client.Client("username", "password", "project_id",
utils.AUTH_URL_V2)
utils.AUTH_URL_V2, direct_use=False)
resp = {"unauthorized": {"message": "Unauthorized", "code": "401"}}
auth_response = utils.TestResponse({
"status_code": 401,
@ -100,7 +101,8 @@ class AuthenticateAgainstKeystoneTests(utils.TestCase):
def test_v1_auth_redirect(self):
cs = client.Client("username", "password", "project_id",
utils.AUTH_URL_V1, service_type='compute')
utils.AUTH_URL_V1, service_type='compute',
direct_use=False)
dict_correct_response = self.get_token()
correct_response = json.dumps(dict_correct_response)
dict_responses = [
@ -165,7 +167,8 @@ class AuthenticateAgainstKeystoneTests(utils.TestCase):
def test_v2_auth_redirect(self):
cs = client.Client("username", "password", "project_id",
utils.AUTH_URL_V2, service_type='compute')
utils.AUTH_URL_V2, service_type='compute',
direct_use=False)
dict_correct_response = self.get_token()
correct_response = json.dumps(dict_correct_response)
dict_responses = [
@ -230,7 +233,8 @@ class AuthenticateAgainstKeystoneTests(utils.TestCase):
def test_ambiguous_endpoints(self):
cs = client.Client("username", "password", "project_id",
utils.AUTH_URL_V2, service_type='compute')
utils.AUTH_URL_V2, service_type='compute',
direct_use=False)
resp = self.get_token()
# duplicate existing service
@ -253,7 +257,8 @@ class AuthenticateAgainstKeystoneTests(utils.TestCase):
def test_authenticate_with_token_success(self):
cs = client.Client("username", None, "project_id",
utils.AUTH_URL_V2, service_type='compute')
utils.AUTH_URL_V2, service_type='compute',
direct_use=False)
cs.client.auth_token = "FAKE_ID"
resp = self.get_token(token_id="FAKE_ID")
auth_response = utils.TestResponse({
@ -295,7 +300,8 @@ class AuthenticateAgainstKeystoneTests(utils.TestCase):
self.assertEqual(cs.client.auth_token, token_id)
def test_authenticate_with_token_failure(self):
cs = client.Client("username", None, "project_id", utils.AUTH_URL_V2)
cs = client.Client("username", None, "project_id", utils.AUTH_URL_V2,
direct_use=False)
cs.client.auth_token = "FAKE_ID"
resp = {"unauthorized": {"message": "Unauthorized", "code": "401"}}
auth_response = utils.TestResponse({
@ -312,7 +318,7 @@ class AuthenticateAgainstKeystoneTests(utils.TestCase):
class AuthenticationTests(utils.TestCase):
def test_authenticate_success(self):
cs = client.Client("username", "password",
"project_id", utils.AUTH_URL)
"project_id", utils.AUTH_URL, direct_use=False)
management_url = 'https://localhost/v1.1/443470'
auth_response = utils.TestResponse({
'status_code': 204,
@ -348,7 +354,7 @@ class AuthenticationTests(utils.TestCase):
def test_authenticate_failure(self):
cs = client.Client("username", "password",
"project_id", utils.AUTH_URL)
"project_id", utils.AUTH_URL, direct_use=False)
auth_response = utils.TestResponse({'status_code': 401})
mock_request = mock.Mock(return_value=(auth_response))
@ -360,7 +366,7 @@ class AuthenticationTests(utils.TestCase):
def test_auth_automatic(self):
cs = client.Client("username", "password",
"project_id", utils.AUTH_URL)
"project_id", utils.AUTH_URL, direct_use=False)
http_client = cs.client
http_client.management_url = ''
http_client.get_service_url = mock.Mock(return_value='')
@ -377,7 +383,7 @@ class AuthenticationTests(utils.TestCase):
def test_auth_manual(self):
cs = client.Client("username", "password",
"project_id", utils.AUTH_URL)
"project_id", utils.AUTH_URL, direct_use=False)
@mock.patch.object(cs.client, 'authenticate')
def test_auth_call(m):

View File

@ -28,7 +28,8 @@ class ClientTest(utils.TestCase):
s = session.Session()
c = client.Client(session=s,
user_agent=user_agent,
endpoint_override=endpoint_override)
endpoint_override=endpoint_override,
direct_use=False)
self.assertEqual(user_agent, c.client.user_agent)
self.assertEqual(endpoint_override, c.client.endpoint_override)
@ -40,6 +41,7 @@ class ClientTest(utils.TestCase):
s = session.Session()
c = client.Client(session=s,
interface=interface,
endpoint_type=endpoint_type)
endpoint_type=endpoint_type,
direct_use=False)
self.assertEqual(interface, c.client.interface)