cinder/cinder/tests/test_xenapi_sm.py
Michael Basnight 6c708d12f5 Update cinder-volume to enable multi volume support
* Added the service wrapper for multi binscripts
* Added a service_name to manager
* Added the configuration wrapper so the impls do not need
  to know if they are accessing config values from a option
  group or from the DEFAULT group
* Updated drivers to use the new configuration wrapper
* Added an example config set for multi backend
* Modified service to comply w/ common
* Added get_volume_stats to iscsi
* Added config for volume_backend_name

implements blueprint multi-volume-backends

Change-Id: Idb86ac5f1c06b9cd5061bef6a24f17727e9a58c1
2013-02-18 11:19:20 -06:00

326 lines
11 KiB
Python

# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2010 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# 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.
from cinder.db import api as db_api
from cinder import exception
from cinder.openstack.common import cfg
from cinder.volume import configuration as conf
from cinder.volume import driver as parent_driver
from cinder.volume.drivers.xenapi import lib
from cinder.volume.drivers.xenapi import sm as driver
import mox
import unittest
class MockContext(object):
def __init__(ctxt, auth_token):
ctxt.auth_token = auth_token
class DriverTestCase(unittest.TestCase):
def assert_flag(self, flagname):
self.assertTrue(hasattr(driver.FLAGS, flagname))
def test_config_options(self):
self.assert_flag('xenapi_connection_url')
self.assert_flag('xenapi_connection_username')
self.assert_flag('xenapi_connection_password')
self.assert_flag('xenapi_nfs_server')
self.assert_flag('xenapi_nfs_serverpath')
self.assert_flag('xenapi_sr_base_path')
def test_do_setup(self):
mock = mox.Mox()
mock.StubOutWithMock(driver, 'xenapi_lib')
mock.StubOutWithMock(driver, 'xenapi_opts')
configuration = mox.MockObject(conf.Configuration)
configuration.xenapi_connection_url = 'url'
configuration.xenapi_connection_username = 'user'
configuration.xenapi_connection_password = 'pass'
configuration.append_config_values(mox.IgnoreArg())
session_factory = object()
nfsops = object()
driver.xenapi_lib.SessionFactory('url', 'user', 'pass').AndReturn(
session_factory)
driver.xenapi_lib.NFSBasedVolumeOperations(
session_factory).AndReturn(nfsops)
drv = driver.XenAPINFSDriver(configuration=configuration)
mock.ReplayAll()
drv.do_setup('context')
mock.VerifyAll()
self.assertEquals(nfsops, drv.nfs_ops)
def test_create_volume(self):
mock = mox.Mox()
configuration = mox.MockObject(conf.Configuration)
configuration.xenapi_nfs_server = 'server'
configuration.xenapi_nfs_serverpath = 'path'
configuration.append_config_values(mox.IgnoreArg())
ops = mock.CreateMock(lib.NFSBasedVolumeOperations)
drv = driver.XenAPINFSDriver(configuration=configuration)
drv.nfs_ops = ops
volume_details = dict(
sr_uuid='sr_uuid',
vdi_uuid='vdi_uuid'
)
ops.create_volume(
'server', 'path', 1, 'name', 'desc').AndReturn(volume_details)
mock.ReplayAll()
result = drv.create_volume(dict(
size=1, display_name='name', display_description='desc'))
mock.VerifyAll()
self.assertEquals(dict(provider_location='sr_uuid/vdi_uuid'), result)
def test_delete_volume(self):
mock = mox.Mox()
configuration = mox.MockObject(conf.Configuration)
configuration.xenapi_nfs_server = 'server'
configuration.xenapi_nfs_serverpath = 'path'
configuration.append_config_values(mox.IgnoreArg())
ops = mock.CreateMock(lib.NFSBasedVolumeOperations)
drv = driver.XenAPINFSDriver(configuration=configuration)
drv.nfs_ops = ops
ops.delete_volume('server', 'path', 'sr_uuid', 'vdi_uuid')
mock.ReplayAll()
result = drv.delete_volume(dict(
provider_location='sr_uuid/vdi_uuid'))
mock.VerifyAll()
def test_create_export_does_not_raise_exception(self):
configuration = conf.Configuration([])
drv = driver.XenAPINFSDriver(configuration=configuration)
drv.create_export('context', 'volume')
def test_remove_export_does_not_raise_exception(self):
configuration = conf.Configuration([])
drv = driver.XenAPINFSDriver(configuration=configuration)
drv.remove_export('context', 'volume')
def test_initialize_connection(self):
mock = mox.Mox()
configuration = mox.MockObject(conf.Configuration)
configuration.xenapi_nfs_server = 'server'
configuration.xenapi_nfs_serverpath = 'path'
configuration.append_config_values(mox.IgnoreArg())
drv = driver.XenAPINFSDriver(configuration=configuration)
mock.ReplayAll()
result = drv.initialize_connection(
dict(
display_name='name',
display_description='desc',
provider_location='sr_uuid/vdi_uuid'),
'connector'
)
mock.VerifyAll()
self.assertEquals(
dict(
driver_volume_type='xensm',
data=dict(
name_label='name',
name_description='desc',
sr_uuid='sr_uuid',
vdi_uuid='vdi_uuid',
sr_type='nfs',
server='server',
serverpath='path',
introduce_sr_keys=['sr_type', 'server', 'serverpath']
)
),
result
)
def test_initialize_connection_null_values(self):
mock = mox.Mox()
configuration = mox.MockObject(conf.Configuration)
configuration.xenapi_nfs_server = 'server'
configuration.xenapi_nfs_serverpath = 'path'
configuration.append_config_values(mox.IgnoreArg())
drv = driver.XenAPINFSDriver(configuration=configuration)
mock.ReplayAll()
result = drv.initialize_connection(
dict(
display_name=None,
display_description=None,
provider_location='sr_uuid/vdi_uuid'),
'connector'
)
mock.VerifyAll()
self.assertEquals(
dict(
driver_volume_type='xensm',
data=dict(
name_label='',
name_description='',
sr_uuid='sr_uuid',
vdi_uuid='vdi_uuid',
sr_type='nfs',
server='server',
serverpath='path',
introduce_sr_keys=['sr_type', 'server', 'serverpath']
)
),
result
)
def _setup_mock_driver(self, server, serverpath, sr_base_path="_srbp"):
mock = mox.Mox()
configuration = mox.MockObject(conf.Configuration)
configuration.xenapi_nfs_server = server
configuration.xenapi_nfs_serverpath = serverpath
configuration.append_config_values(mox.IgnoreArg())
drv = driver.XenAPINFSDriver(configuration=configuration)
ops = mock.CreateMock(lib.NFSBasedVolumeOperations)
db = mock.CreateMock(db_api)
drv.nfs_ops = ops
drv.db = db
mock.StubOutWithMock(driver, 'FLAGS')
driver.FLAGS.xenapi_nfs_server = server
driver.FLAGS.xenapi_nfs_serverpath = serverpath
driver.FLAGS.xenapi_sr_base_path = sr_base_path
return mock, drv
def test_create_snapshot(self):
mock, drv = self._setup_mock_driver('server', 'serverpath')
snapshot = dict(
volume_id="volume-id",
display_name="snapshot-name",
display_description="snapshot-desc",
volume=dict(provider_location="sr-uuid/vdi-uuid"))
drv.nfs_ops.copy_volume(
"server", "serverpath", "sr-uuid", "vdi-uuid",
"snapshot-name", "snapshot-desc"
).AndReturn(dict(sr_uuid="copied-sr", vdi_uuid="copied-vdi"))
mock.ReplayAll()
result = drv.create_snapshot(snapshot)
mock.VerifyAll()
self.assertEquals(
dict(provider_location="copied-sr/copied-vdi"),
result)
def test_create_volume_from_snapshot(self):
mock, drv = self._setup_mock_driver('server', 'serverpath')
snapshot = dict(
provider_location='src-sr-uuid/src-vdi-uuid')
volume = dict(
display_name='tgt-name', name_description='tgt-desc')
drv.nfs_ops.copy_volume(
"server", "serverpath", "src-sr-uuid", "src-vdi-uuid",
"tgt-name", "tgt-desc"
).AndReturn(dict(sr_uuid="copied-sr", vdi_uuid="copied-vdi"))
mock.ReplayAll()
result = drv.create_volume_from_snapshot(volume, snapshot)
mock.VerifyAll()
self.assertEquals(
dict(provider_location='copied-sr/copied-vdi'), result)
def test_delete_snapshot(self):
mock, drv = self._setup_mock_driver('server', 'serverpath')
snapshot = dict(
provider_location='src-sr-uuid/src-vdi-uuid')
drv.nfs_ops.delete_volume(
"server", "serverpath", "src-sr-uuid", "src-vdi-uuid")
mock.ReplayAll()
drv.delete_snapshot(snapshot)
mock.VerifyAll()
def test_copy_image_to_volume_success(self):
mock, drv = self._setup_mock_driver(
'server', 'serverpath', '/var/run/sr-mount')
volume = dict(
provider_location='sr-uuid/vdi-uuid',
size=2)
mock.StubOutWithMock(driver.glance, 'get_api_servers')
driver.glance.get_api_servers().AndReturn((x for x in ['glancesrv']))
drv.nfs_ops.use_glance_plugin_to_overwrite_volume(
'server', 'serverpath', 'sr-uuid', 'vdi-uuid', 'glancesrv',
'image_id', 'token', '/var/run/sr-mount').AndReturn(True)
drv.nfs_ops.resize_volume(
'server', 'serverpath', 'sr-uuid', 'vdi-uuid', 2)
mock.ReplayAll()
drv.copy_image_to_volume(
MockContext('token'), volume, "ignore", "image_id")
mock.VerifyAll()
def test_copy_image_to_volume_fail(self):
mock, drv = self._setup_mock_driver(
'server', 'serverpath', '/var/run/sr-mount')
volume = dict(
provider_location='sr-uuid/vdi-uuid',
size=2)
mock.StubOutWithMock(driver.glance, 'get_api_servers')
driver.glance.get_api_servers().AndReturn((x for x in ['glancesrv']))
drv.nfs_ops.use_glance_plugin_to_overwrite_volume(
'server', 'serverpath', 'sr-uuid', 'vdi-uuid', 'glancesrv',
'image_id', 'token', '/var/run/sr-mount').AndReturn(False)
mock.ReplayAll()
self.assertRaises(
exception.ImageCopyFailure,
lambda: drv.copy_image_to_volume(
MockContext('token'), volume, "ignore", "image_id"))
mock.VerifyAll()