Merge "Assisted snapshot: use Keystone Session with Nova Client"

This commit is contained in:
Jenkins 2016-05-25 22:07:41 +00:00 committed by Gerrit Code Review
commit f60df10699
3 changed files with 62 additions and 26 deletions

View File

@ -16,7 +16,8 @@
Handles all requests to Nova. Handles all requests to Nova.
""" """
import keystoneauth1.loading
import keystoneauth1.session
from novaclient import client as nova_client from novaclient import client as nova_client
from novaclient import exceptions as nova_exceptions from novaclient import exceptions as nova_exceptions
from novaclient import service_catalog from novaclient import service_catalog
@ -134,11 +135,17 @@ def novaclient(context, admin_endpoint=False, privileged_user=False,
LOG.debug('Nova client connection created using URL: %s', url) LOG.debug('Nova client connection created using URL: %s', url)
# Now that we have the correct auth_url, username, password and
# project_name, let's build a Keystone session.
loader = keystoneauth1.loading.get_plugin_loader('password')
auth = loader.load_from_options(auth_url=url,
username=context.user_id,
password=context.auth_token,
project_name=context.project_name)
keystone_session = keystoneauth1.session.Session(auth=auth)
c = nova_client.Client(NOVA_API_VERSION, c = nova_client.Client(NOVA_API_VERSION,
context.user_id, session=keystone_session,
context.auth_token,
context.project_name,
auth_url=url,
insecure=CONF.nova_api_insecure, insecure=CONF.nova_api_insecure,
timeout=timeout, timeout=timeout,
region_name=CONF.os_region_name, region_name=CONF.os_region_name,

View File

@ -39,55 +39,86 @@ class NovaClientTestCase(test.TestCase):
self.override_config('os_privileged_user_password', 'strongpassword') self.override_config('os_privileged_user_password', 'strongpassword')
@mock.patch('novaclient.client.Client') @mock.patch('novaclient.client.Client')
def test_nova_client_regular(self, p_client): @mock.patch('keystoneauth1.loading.get_plugin_loader')
@mock.patch('keystoneauth1.session.Session')
def test_nova_client_regular(self, p_session, p_plugin_loader, p_client):
nova.novaclient(self.ctx) nova.novaclient(self.ctx)
p_plugin_loader.return_value.load_from_options.assert_called_once_with(
auth_url='http://novahost:8774/v2/e3f0833dc08b4cea',
password='token', project_name=None, username='regularuser'
)
p_client.assert_called_once_with( p_client.assert_called_once_with(
nova.NOVA_API_VERSION, nova.NOVA_API_VERSION,
'regularuser', 'token', None, region_name=None, session=p_session.return_value, region_name=None,
auth_url='http://novahost:8774/v2/e3f0833dc08b4cea',
insecure=False, endpoint_type='publicURL', cacert=None, insecure=False, endpoint_type='publicURL', cacert=None,
timeout=None, extensions=nova.nova_extensions) timeout=None, extensions=nova.nova_extensions)
@mock.patch('novaclient.client.Client') @mock.patch('novaclient.client.Client')
def test_nova_client_admin_endpoint(self, p_client): @mock.patch('keystoneauth1.loading.get_plugin_loader')
@mock.patch('keystoneauth1.session.Session')
def test_nova_client_admin_endpoint(self, p_session, p_plugin_loader,
p_client):
nova.novaclient(self.ctx, admin_endpoint=True) nova.novaclient(self.ctx, admin_endpoint=True)
p_plugin_loader.return_value.load_from_options.assert_called_once_with(
auth_url='http://novaadmhost:4778/v2/e3f0833dc08b4cea',
password='token', project_name=None, username='regularuser'
)
p_client.assert_called_once_with( p_client.assert_called_once_with(
nova.NOVA_API_VERSION, nova.NOVA_API_VERSION,
'regularuser', 'token', None, region_name=None, session=p_session.return_value, region_name=None,
auth_url='http://novaadmhost:4778/v2/e3f0833dc08b4cea',
insecure=False, endpoint_type='adminURL', cacert=None, insecure=False, endpoint_type='adminURL', cacert=None,
timeout=None, extensions=nova.nova_extensions) timeout=None, extensions=nova.nova_extensions)
@mock.patch('novaclient.client.Client') @mock.patch('novaclient.client.Client')
def test_nova_client_privileged_user(self, p_client): @mock.patch('keystoneauth1.loading.get_plugin_loader')
@mock.patch('keystoneauth1.session.Session')
def test_nova_client_privileged_user(self, p_session, p_plugin_loader,
p_client):
nova.novaclient(self.ctx, privileged_user=True) nova.novaclient(self.ctx, privileged_user=True)
p_plugin_loader.return_value.load_from_options.assert_called_once_with(
auth_url='http://keystonehost:5000/v2.0',
password='strongpassword', project_name=None, username='adminuser'
)
p_client.assert_called_once_with( p_client.assert_called_once_with(
nova.NOVA_API_VERSION, nova.NOVA_API_VERSION,
'adminuser', 'strongpassword', None, region_name=None, session=p_session.return_value, region_name=None,
auth_url='http://keystonehost:5000/v2.0',
insecure=False, endpoint_type='publicURL', cacert=None, insecure=False, endpoint_type='publicURL', cacert=None,
timeout=None, extensions=nova.nova_extensions) timeout=None, extensions=nova.nova_extensions)
@mock.patch('novaclient.client.Client') @mock.patch('novaclient.client.Client')
def test_nova_client_privileged_user_custom_auth_url(self, p_client): @mock.patch('keystoneauth1.loading.get_plugin_loader')
@mock.patch('keystoneauth1.session.Session')
def test_nova_client_privileged_user_custom_auth_url(self, p_session,
p_plugin_loader,
p_client):
self.override_config('os_privileged_user_auth_url', self.override_config('os_privileged_user_auth_url',
'http://privatekeystonehost:5000/v2.0') 'http://privatekeystonehost:5000/v2.0')
nova.novaclient(self.ctx, privileged_user=True) nova.novaclient(self.ctx, privileged_user=True)
p_plugin_loader.return_value.load_from_options.assert_called_once_with(
auth_url='http://privatekeystonehost:5000/v2.0',
password='strongpassword', project_name=None, username='adminuser'
)
p_client.assert_called_once_with( p_client.assert_called_once_with(
nova.NOVA_API_VERSION, nova.NOVA_API_VERSION,
'adminuser', 'strongpassword', None, region_name=None, session=p_session.return_value, region_name=None,
auth_url='http://privatekeystonehost:5000/v2.0',
insecure=False, endpoint_type='publicURL', cacert=None, insecure=False, endpoint_type='publicURL', cacert=None,
timeout=None, extensions=nova.nova_extensions) timeout=None, extensions=nova.nova_extensions)
@mock.patch('novaclient.client.Client') @mock.patch('novaclient.client.Client')
def test_nova_client_custom_region(self, p_client): @mock.patch('keystoneauth1.loading.get_plugin_loader')
@mock.patch('keystoneauth1.session.Session')
def test_nova_client_custom_region(self, p_session, p_plugin_loader,
p_client):
self.override_config('os_region_name', 'farfaraway') self.override_config('os_region_name', 'farfaraway')
nova.novaclient(self.ctx) nova.novaclient(self.ctx)
p_plugin_loader.return_value.load_from_options.assert_called_once_with(
auth_url='http://novahost:8774/v2/e3f0833dc08b4cea',
password='token', project_name=None, username='regularuser'
)
p_client.assert_called_once_with( p_client.assert_called_once_with(
nova.NOVA_API_VERSION, nova.NOVA_API_VERSION,
'regularuser', 'token', None, region_name='farfaraway', session=p_session.return_value, region_name='farfaraway',
auth_url='http://novahost:8774/v2/e3f0833dc08b4cea',
insecure=False, endpoint_type='publicURL', cacert=None, insecure=False, endpoint_type='publicURL', cacert=None,
timeout=None, extensions=nova.nova_extensions) timeout=None, extensions=nova.nova_extensions)

View File

@ -996,13 +996,11 @@ class InstanceLocalityFilterTestCase(HostFiltersTestCase):
filter_properties = {'context': self.context, 'size': 100} filter_properties = {'context': self.context, 'size': 100}
self.assertTrue(filt_cls.host_passes(host, filter_properties)) self.assertTrue(filt_cls.host_passes(host, filter_properties))
@mock.patch('novaclient.client.discover_extensions') @mock.patch('cinder.compute.nova.novaclient')
@mock.patch('requests.request') def test_nova_timeout(self, mock_novaclient):
def test_nova_timeout(self, _mock_request, fake_extensions):
# Simulate a HTTP timeout # Simulate a HTTP timeout
_mock_request.side_effect = request_exceptions.Timeout mock_show_all = mock_novaclient.return_value.list_extensions.show_all
fake_extensions.return_value = ( mock_show_all.side_effect = request_exceptions.Timeout
fakes.FakeNovaClient().list_extensions.show_all())
filt_cls = self.class_map['InstanceLocalityFilter']() filt_cls = self.class_map['InstanceLocalityFilter']()
host = fakes.FakeHostState('host1', {}) host = fakes.FakeHostState('host1', {})