import datetime
import json
import uuid
+
+import mock
import webob
from cinder.api.contrib import volume_actions
self.assertEqual(res.status_int, 202)
def test_initialize_connection(self):
- def fake_initialize_connection(*args, **kwargs):
- return {}
- self.stubs.Set(volume.API, 'initialize_connection',
- fake_initialize_connection)
-
- body = {'os-initialize_connection': {'connector': 'fake'}}
- req = webob.Request.blank('/v2/fake/volumes/1/action')
- req.method = "POST"
- req.body = jsonutils.dumps(body)
- req.headers["content-type"] = "application/json"
+ with mock.patch.object(volume_api.API,
+ 'initialize_connection') as init_conn:
+ init_conn.return_value = {}
+ body = {'os-initialize_connection': {'connector': 'fake'}}
+ req = webob.Request.blank('/v2/fake/volumes/1/action')
+ req.method = "POST"
+ req.body = jsonutils.dumps(body)
+ req.headers["content-type"] = "application/json"
- res = req.get_response(fakes.wsgi_app())
- self.assertEqual(res.status_int, 200)
+ res = req.get_response(fakes.wsgi_app())
+ self.assertEqual(res.status_int, 200)
def test_initialize_connection_without_connector(self):
- def fake_initialize_connection(*args, **kwargs):
- return {}
- self.stubs.Set(volume.API, 'initialize_connection',
- fake_initialize_connection)
+ with mock.patch.object(volume_api.API,
+ 'initialize_connection') as init_conn:
+ init_conn.return_value = {}
+ body = {'os-initialize_connection': {}}
+ req = webob.Request.blank('/v2/fake/volumes/1/action')
+ req.method = "POST"
+ req.body = jsonutils.dumps(body)
+ req.headers["content-type"] = "application/json"
- body = {'os-initialize_connection': {}}
- req = webob.Request.blank('/v2/fake/volumes/1/action')
- req.method = "POST"
- req.body = jsonutils.dumps(body)
- req.headers["content-type"] = "application/json"
+ res = req.get_response(fakes.wsgi_app())
+ self.assertEqual(res.status_int, 400)
+
+ def test_initialize_connection_exception(self):
+ with mock.patch.object(volume_api.API,
+ 'initialize_connection') as init_conn:
+ init_conn.side_effect = \
+ exception.VolumeBackendAPIException(data=None)
+ body = {'os-initialize_connection': {'connector': 'fake'}}
+ req = webob.Request.blank('/v2/fake/volumes/1/action')
+ req.method = "POST"
+ req.body = jsonutils.dumps(body)
+ req.headers["content-type"] = "application/json"
- res = req.get_response(fakes.wsgi_app())
- self.assertEqual(res.status_int, 400)
+ res = req.get_response(fakes.wsgi_app())
+ self.assertEqual(res.status_int, 500)
def test_terminate_connection(self):
def fake_terminate_connection(*args, **kwargs):
"""
volume = self.db.volume_get(context, volume_id)
self.driver.validate_connector(connector)
- conn_info = self.driver.initialize_connection(volume, connector)
+ try:
+ conn_info = self.driver.initialize_connection(volume, connector)
+ except Exception as err:
+ err_msg = (_('Unable to fetch connection information from '
+ 'backend: %(err)s') % {'err': str(err)})
+ LOG.error(err_msg)
+ raise exception.VolumeBackendAPIException(data=err_msg)
# Add qos_specs to connection info
typeid = volume['volume_type_id']