Rewrited mox tests to mock (part 2)

This is second and last part of removing mox
module from manila's dependencies, that became deprecated.
Rewrote unittests for scheduler, fixed and enabled them.

Partially implements: blueprint replace-mox-with-mock
Change-Id: Ida60abaa287e42a42c59f900bb168992575e311e
This commit is contained in:
vponomaryov
2014-06-13 13:55:33 +03:00
parent 0997dcf8d0
commit f8e4ff56b1
10 changed files with 391 additions and 490 deletions

View File

@@ -26,10 +26,8 @@ inline callbacks.
import functools import functools
import uuid import uuid
import mox import mock
import nose.plugins.skip
from oslo.config import cfg from oslo.config import cfg
import stubout
import testtools import testtools
from manila.openstack.common import importutils from manila.openstack.common import importutils
@@ -53,6 +51,16 @@ CONF.register_opts(test_opts)
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
class StubOutForTesting(object):
def __init__(self, parent):
self.parent = parent
def Set(self, obj, attr_name, new_attr):
stub = mock.patch.object(obj, attr_name, new_attr)
stub.start()
self.parent.addCleanup(stub.stop)
class TestCase(testtools.TestCase): class TestCase(testtools.TestCase):
"""Test case base class for all unit tests.""" """Test case base class for all unit tests."""
@@ -69,45 +77,36 @@ class TestCase(testtools.TestCase):
self.start = timeutils.utcnow() self.start = timeutils.utcnow()
tests.reset_db() tests.reset_db()
# emulate some of the mox stuff, we can't use the metaclass self.stubs = StubOutForTesting(self)
# because it screws with our generators
self.mox = mox.Mox()
self.stubs = stubout.StubOutForTesting()
self.injected = [] self.injected = []
self._services = [] self._services = []
CONF.set_override('fatal_exception_format_errors', True) CONF.set_override('fatal_exception_format_errors', True)
def tearDown(self): def tearDown(self):
"""Runs after each test method to tear down test environment.""" """Runs after each test method to tear down test environment."""
try: super(TestCase, self).tearDown()
self.mox.UnsetStubs() # Reset any overridden flags
self.stubs.UnsetAll() CONF.reset()
self.stubs.SmartUnsetAll()
self.mox.VerifyAll()
super(TestCase, self).tearDown()
finally:
# Reset any overridden flags
CONF.reset()
# Stop any timers # Stop any timers
for x in self.injected: for x in self.injected:
try: try:
x.stop() x.stop()
except AssertionError: except AssertionError:
pass pass
# Kill any services # Kill any services
for x in self._services: for x in self._services:
try: try:
x.kill() x.kill()
except Exception: except Exception:
pass pass
# Delete attributes that don't start with _ so they don't pin # Delete attributes that don't start with _ so they don't pin
# memory around unnecessarily for the duration of the test # memory around unnecessarily for the duration of the test
# suite # suite
for key in [k for k in self.__dict__.keys() if k[0] != '_']: for key in [k for k in self.__dict__.keys() if k[0] != '_']:
del self.__dict__[key] del self.__dict__[key]
def flags(self, **kw): def flags(self, **kw):
"""Override flag variables for a test.""" """Override flag variables for a test."""

View File

@@ -1,9 +1,9 @@
===================================== =======================================
OpenStack Manila Testing Infrastructure OpenStack Manila Testing Infrastructure
===================================== =======================================
A note of clarification is in order, to help those who are new to testing in A note of clarification is in order, to help those who are new to testing in
OpenStack manila: OpenStack Manila:
- actual unit tests are created in the "tests" directory; - actual unit tests are created in the "tests" directory;
- the "testing" directory is used to house the infrastructure needed to support - the "testing" directory is used to house the infrastructure needed to support
@@ -13,46 +13,33 @@ This README file attempts to provide current and prospective contributors with
everything they need to know in order to start creating unit tests and everything they need to know in order to start creating unit tests and
utilizing the convenience code provided in manila.testing. utilizing the convenience code provided in manila.testing.
Note: the content for the rest of this file will be added as the work items in
the following blueprint are completed:
https://blueprints.launchpad.net/manila/+spec/consolidate-testing-infrastructure
Test Types: Unit vs. Functional vs. Integration
-----------------------------------------------
TBD
Writing Unit Tests Writing Unit Tests
------------------ ------------------
TBD - All new unit tests are to be written in python-mock.
- Old tests that are still written in mox should be updated to use python-mock.
Using Fakes Usage of mox has been deprecated for writing Manila unit tests.
~~~~~~~~~~~ - use addCleanup in favor of tearDown
TBD
test.TestCase test.TestCase
------------- -------------
The TestCase class from manila.test (generally imported as test) will The TestCase class from manila.test (generally imported as test) will
automatically manage self.stubs using the stubout module and self.mox automatically manage self.stubs using the stubout module.
using the mox module during the setUp step. They will automatically They will automatically verify and clean up during the tearDown step.
verify and clean up during the tearDown step.
If using test.TestCase, calling the super class setUp is required and If using test.TestCase, calling the super class setUp is required and
calling the super class tearDown is required to be last if tearDown calling the super class tearDown is required to be last if tearDown
is overridden. is overridden.
Writing Functional Tests Running Tests
------------------------ -------------
TBD In the root of the Manila source code run the run_tests.sh script. This will
offer to create a virtual environment and populate it with dependencies.
Writing Integration Tests If you don't have dependencies installed that are needed for compiling Manila's
------------------------- direct dependencies, you'll have to use your operating system's method of
installing extra dependencies. To get help using this script execute it with
TBD the -h parameter to get options `./run_tests.sh -h`
Tests and assertRaises Tests and assertRaises
---------------------- ----------------------

View File

@@ -1,5 +1,3 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2010 United States Government as represented by the # Copyright 2010 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration. # Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved. # All Rights Reserved.
@@ -42,7 +40,9 @@ setattr(__builtin__, '_', lambda x: x)
import os import os
import shutil import shutil
from manila.db import migration
from manila.db.sqlalchemy.session import get_engine from manila.db.sqlalchemy.session import get_engine
from manila.tests import conf_fixture
CONF = cfg.CONF CONF = cfg.CONF
@@ -62,13 +62,7 @@ def reset_db():
def setup(): def setup():
import mox # Fail fast if you don't have mox. Workaround for bug 810424
from manila.db import migration
from manila.tests import conf_fixture
conf_fixture.set_defaults(CONF) conf_fixture.set_defaults(CONF)
if CONF.sql_connection == "sqlite://": if CONF.sql_connection == "sqlite://":
if migration.db_version() > 1: if migration.db_version() > 1:
return return

View File

@@ -16,28 +16,11 @@
Fakes For Scheduler tests. Fakes For Scheduler tests.
""" """
import mox
from manila import db
from manila.openstack.common import timeutils from manila.openstack.common import timeutils
from manila.scheduler import filter_scheduler from manila.scheduler import filter_scheduler
from manila.scheduler import host_manager from manila.scheduler import host_manager
VOLUME_SERVICES = [
dict(id=1, host='host1', topic='volume', disabled=False,
availability_zone='zone1', updated_at=timeutils.utcnow()),
dict(id=2, host='host2', topic='volume', disabled=False,
availability_zone='zone1', updated_at=timeutils.utcnow()),
dict(id=3, host='host3', topic='volume', disabled=False,
availability_zone='zone2', updated_at=timeutils.utcnow()),
dict(id=4, host='host4', topic='volume', disabled=False,
availability_zone='zone3', updated_at=timeutils.utcnow()),
# service on host5 is disabled
dict(id=5, host='host5', topic='volume', disabled=True,
availability_zone='zone4', updated_at=timeutils.utcnow()),
]
SHARE_SERVICES = [ SHARE_SERVICES = [
dict(id=1, host='host1', topic='share', disabled=False, dict(id=1, host='host1', topic='share', disabled=False,
availability_zone='zone1', updated_at=timeutils.utcnow()), availability_zone='zone1', updated_at=timeutils.utcnow()),
@@ -90,15 +73,22 @@ class FakeHostState(host_manager.HostState):
setattr(self, key, val) setattr(self, key, val)
def mox_host_manager_db_calls(mock, context): def mock_host_manager_db_calls(mock_obj, disabled=None):
mock.StubOutWithMock(db, 'service_get_all_by_topic') services = [
dict(id=1, host='host1', topic='share', disabled=False,
db.service_get_all_by_topic(mox.IgnoreArg(), availability_zone='zone1', updated_at=timeutils.utcnow()),
mox.IgnoreArg()).AndReturn(VOLUME_SERVICES) dict(id=2, host='host2', topic='share', disabled=False,
availability_zone='zone1', updated_at=timeutils.utcnow()),
dict(id=3, host='host3', topic='share', disabled=False,
def mox_host_manager_db_calls_share(mock, context): availability_zone='zone2', updated_at=timeutils.utcnow()),
mock.StubOutWithMock(db, 'service_get_all_by_topic') dict(id=4, host='host4', topic='share', disabled=False,
availability_zone='zone3', updated_at=timeutils.utcnow()),
db.service_get_all_by_topic(mox.IgnoreArg(), # service on host5 is disabled
mox.IgnoreArg()).AndReturn(SHARE_SERVICES) dict(id=5, host='host5', topic='share', disabled=True,
availability_zone='zone4', updated_at=timeutils.utcnow()),
]
if disabled is None:
mock_obj.return_value = services
else:
mock_obj.return_value = [service for service in services
if service['disabled'] == disabled]

View File

@@ -16,13 +16,17 @@
Tests For Capacity Weigher. Tests For Capacity Weigher.
""" """
import mock
from oslo.config import cfg
import testtools import testtools
from manila import context from manila import context
from manila.openstack.common.scheduler.weights import HostWeightHandler from manila.openstack.common.scheduler.weights import HostWeightHandler
from manila.scheduler.weights.capacity import CapacityWeigher
from manila import test from manila import test
from manila.tests.scheduler import fakes from manila.tests.scheduler import fakes
from manila.tests import utils as test_utils
CONF = cfg.CONF
class CapacityWeigherTestCase(test.TestCase): class CapacityWeigherTestCase(test.TestCase):
@@ -30,26 +34,25 @@ class CapacityWeigherTestCase(test.TestCase):
super(CapacityWeigherTestCase, self).setUp() super(CapacityWeigherTestCase, self).setUp()
self.host_manager = fakes.FakeHostManager() self.host_manager = fakes.FakeHostManager()
self.weight_handler = HostWeightHandler('manila.scheduler.weights') self.weight_handler = HostWeightHandler('manila.scheduler.weights')
self.weight_classes = self.weight_handler.get_all_classes()
def _get_weighed_host(self, hosts, weight_properties=None): def _get_weighed_host(self, hosts, weight_properties=None):
if weight_properties is None: if weight_properties is None:
weight_properties = {} weight_properties = {}
return self.weight_handler.get_weighed_objects(self.weight_classes, return self.weight_handler.get_weighed_objects([CapacityWeigher],
hosts, hosts,
weight_properties)[0] weight_properties)[0]
def _get_all_hosts(self): @mock.patch('manila.db.sqlalchemy.api.service_get_all_by_topic')
def _get_all_hosts(self, _mock_service_get_all_by_topic, disabled=False):
ctxt = context.get_admin_context() ctxt = context.get_admin_context()
fakes.mox_host_manager_db_calls(self.mox, ctxt) fakes.mock_host_manager_db_calls(_mock_service_get_all_by_topic,
self.mox.ReplayAll() disabled=disabled)
host_states = self.host_manager.get_all_host_states(ctxt) host_states = self.host_manager.get_all_host_states_share(ctxt)
self.mox.VerifyAll() _mock_service_get_all_by_topic.assert_called_once_with(
self.mox.ResetAll() ctxt, CONF.share_topic)
return host_states return host_states
@testtools.skipIf(not test_utils.is_manila_installed(), @testtools.skip("LP bug #1329718")
'Test requires Manila installed')
def test_default_of_spreading_first(self): def test_default_of_spreading_first(self):
hostinfo_list = self._get_all_hosts() hostinfo_list = self._get_all_hosts()
@@ -63,8 +66,7 @@ class CapacityWeigherTestCase(test.TestCase):
self.assertEqual(weighed_host.weight, 921.0) self.assertEqual(weighed_host.weight, 921.0)
self.assertEqual(weighed_host.obj.host, 'host1') self.assertEqual(weighed_host.obj.host, 'host1')
@testtools.skipIf(not test_utils.is_manila_installed(), @testtools.skip("LP bug #1329718")
'Test requires Manila installed')
def test_capacity_weight_multiplier1(self): def test_capacity_weight_multiplier1(self):
self.flags(capacity_weight_multiplier=-1.0) self.flags(capacity_weight_multiplier=-1.0)
hostinfo_list = self._get_all_hosts() hostinfo_list = self._get_all_hosts()
@@ -79,8 +81,7 @@ class CapacityWeigherTestCase(test.TestCase):
self.assertEqual(weighed_host.weight, -190.0) self.assertEqual(weighed_host.weight, -190.0)
self.assertEqual(weighed_host.obj.host, 'host4') self.assertEqual(weighed_host.obj.host, 'host4')
@testtools.skipIf(not test_utils.is_manila_installed(), @testtools.skip("LP bug #1329718")
'Test requires Manila installed')
def test_capacity_weight_multiplier2(self): def test_capacity_weight_multiplier2(self):
self.flags(capacity_weight_multiplier=2.0) self.flags(capacity_weight_multiplier=2.0)
hostinfo_list = self._get_all_hosts() hostinfo_list = self._get_all_hosts()

View File

@@ -15,21 +15,14 @@
""" """
Tests For Filter Scheduler. Tests For Filter Scheduler.
""" """
import testtools import mock
from manila import context from manila import context
from manila import exception from manila import exception
from manila.openstack.common.scheduler import weights
from manila.scheduler import filter_scheduler from manila.scheduler import filter_scheduler
from manila.scheduler import host_manager from manila.scheduler import host_manager
from manila import test
from manila.tests.scheduler import fakes from manila.tests.scheduler import fakes
from manila.tests.scheduler import test_scheduler from manila.tests.scheduler import test_scheduler
from manila.tests import utils as test_utils
def fake_get_filtered_hosts(hosts, filter_properties):
return list(hosts)
class FilterSchedulerTestCase(test_scheduler.SchedulerTestCase): class FilterSchedulerTestCase(test_scheduler.SchedulerTestCase):
@@ -37,104 +30,132 @@ class FilterSchedulerTestCase(test_scheduler.SchedulerTestCase):
driver_cls = filter_scheduler.FilterScheduler driver_cls = filter_scheduler.FilterScheduler
@testtools.skipIf(not test_utils.is_manila_installed(),
'Test requires Manila installed (try setup.py develop')
def test_create_share_no_hosts(self): def test_create_share_no_hosts(self):
""" # Ensure empty hosts/child_zones result in NoValidHosts exception.
Ensure empty hosts & child_zones result in NoValidHosts exception.
"""
def _fake_empty_call_zone_method(*args, **kwargs):
return []
sched = fakes.FakeFilterScheduler() sched = fakes.FakeFilterScheduler()
fake_context = context.RequestContext('user', 'project') fake_context = context.RequestContext('user', 'project')
request_spec = {'share_properties': {'project_id': 1, request_spec = {
'size': 1}, 'share_properties': {'project_id': 1, 'size': 1},
'share_type': {'name': 'LVM_NFS'}, 'share_type': {'name': 'LVM_NFS'},
'share_id': ['fake-id1']} 'share_id': ['fake-id1'],
}
self.assertRaises(exception.NoValidHost, sched.schedule_create_share, self.assertRaises(exception.NoValidHost, sched.schedule_create_share,
fake_context, request_spec, {}) fake_context, request_spec, {})
@testtools.skipIf(not test_utils.is_manila_installed(), @mock.patch('manila.scheduler.host_manager.HostManager.'
'Test requires Manila installed (try setup.py develop') 'get_all_host_states_share')
def test_create_share_non_admin(self): def test_create_share_non_admin(self, _mock_get_all_host_states):
"""Test creating share passing a non-admin context. # Test creating a volume locally using create_volume, passing
# a non-admin context. DB actions should work.
DB actions should work."""
self.was_admin = False self.was_admin = False
def fake_get(context, *args, **kwargs): def fake_get(context, *args, **kwargs):
# make sure this is called with admin context, even though # Make sure this is called with admin context, even though
# we're using user context below # we're using user context below.
self.was_admin = context.is_admin self.was_admin = context.is_admin
return {} return {}
sched = fakes.FakeFilterScheduler() sched = fakes.FakeFilterScheduler()
self.stubs.Set(sched.host_manager, _mock_get_all_host_states.side_effect = fake_get
'get_all_host_states_share',
fake_get)
fake_context = context.RequestContext('user', 'project') fake_context = context.RequestContext('user', 'project')
request_spec = {
request_spec = {'share_properties': {'project_id': 1, 'share_properties': {'project_id': 1, 'size': 1},
'size': 1}, 'share_type': {'name': 'LVM_NFS'},
'share_type': {'name': 'LVM_NFS'}, 'share_id': ['fake-id1'],
'share_id': ['fake-id1']} }
self.assertRaises(exception.NoValidHost, sched.schedule_create_share, self.assertRaises(exception.NoValidHost, sched.schedule_create_share,
fake_context, request_spec, {}) fake_context, request_spec, {})
self.assertTrue(self.was_admin) self.assertTrue(self.was_admin)
@testtools.skipIf(not test_utils.is_manila_installed(), @mock.patch('manila.db.service_get_all_by_topic')
'Test requires Manila installed (try setup.py develop') def test_schedule_happy_day_share(self, _mock_service_get_all_by_topic):
def test_schedule_happy_day_share(self): # Make sure there's nothing glaringly wrong with _schedule()
"""Make sure there's nothing glaringly wrong with _schedule_share() # by doing a happy day pass through.
by doing a happy day pass through."""
self.next_weight = 1.0
def _fake_weigh_objects(_self, functions, hosts, options):
self.next_weight += 2.0
host_state = hosts[0]
return [weights.WeighedHost(host_state, self.next_weight)]
sched = fakes.FakeFilterScheduler() sched = fakes.FakeFilterScheduler()
sched.host_manager = fakes.FakeHostManager()
fake_context = context.RequestContext('user', 'project', fake_context = context.RequestContext('user', 'project',
is_admin=True) is_admin=True)
fakes.mock_host_manager_db_calls(_mock_service_get_all_by_topic)
self.stubs.Set(sched.host_manager, 'get_filtered_hosts', request_spec = {
fake_get_filtered_hosts) 'share_type': {'name': 'LVM_NFS'},
self.stubs.Set(weights.HostWeightHandler, 'share_properties': {'project_id': 1, 'size': 1},
'get_weighed_objects', _fake_weigh_objects) }
fakes.mox_host_manager_db_calls_share(self.mox, fake_context)
request_spec = {'share_type': {'name': 'LVM_NFS'},
'sharee_properties': {'project_id': 1,
'size': 1}}
self.mox.ReplayAll()
weighed_host = sched._schedule_share(fake_context, request_spec, {}) weighed_host = sched._schedule_share(fake_context, request_spec, {})
self.assertTrue(weighed_host.obj is not None) self.assertIsNotNone(weighed_host.obj)
self.assertTrue(_mock_service_get_all_by_topic.called)
def test_max_attempts(self): def test_max_attempts(self):
self.flags(scheduler_max_attempts=4) self.flags(scheduler_max_attempts=4)
sched = fakes.FakeFilterScheduler() sched = fakes.FakeFilterScheduler()
self.assertEqual(4, sched._max_attempts()) self.assertEqual(4, sched._max_attempts())
def test_invalid_max_attempts(self): def test_invalid_max_attempts(self):
self.flags(scheduler_max_attempts=0) self.flags(scheduler_max_attempts=0)
self.assertRaises(exception.InvalidParameterValue, self.assertRaises(exception.InvalidParameterValue,
fakes.FakeFilterScheduler) fakes.FakeFilterScheduler)
def test_retry_disabled(self):
# Retry info should not get populated when re-scheduling is off.
self.flags(scheduler_max_attempts=1)
sched = fakes.FakeFilterScheduler()
request_spec = {
'volume_type': {'name': 'LVM_iSCSI'},
'share_properties': {'project_id': 1, 'size': 1},
}
filter_properties = {}
sched._schedule_share(self.context, request_spec,
filter_properties=filter_properties)
# Should not have retry info in the populated filter properties.
self.assertNotIn("retry", filter_properties)
def test_retry_attempt_one(self):
# Test retry logic on initial scheduling attempt.
self.flags(scheduler_max_attempts=2)
sched = fakes.FakeFilterScheduler()
request_spec = {
'volume_type': {'name': 'LVM_iSCSI'},
'share_properties': {'project_id': 1, 'size': 1},
}
filter_properties = {}
sched._schedule_share(self.context, request_spec,
filter_properties=filter_properties)
num_attempts = filter_properties['retry']['num_attempts']
self.assertEqual(1, num_attempts)
def test_retry_attempt_two(self):
# Test retry logic when re-scheduling.
self.flags(scheduler_max_attempts=2)
sched = fakes.FakeFilterScheduler()
request_spec = {
'volume_type': {'name': 'LVM_iSCSI'},
'share_properties': {'project_id': 1, 'size': 1},
}
retry = dict(num_attempts=1)
filter_properties = dict(retry=retry)
sched._schedule_share(self.context, request_spec,
filter_properties=filter_properties)
num_attempts = filter_properties['retry']['num_attempts']
self.assertEqual(2, num_attempts)
def test_retry_exceeded_max_attempts(self):
# Test for necessary explosion when max retries is exceeded.
self.flags(scheduler_max_attempts=2)
sched = fakes.FakeFilterScheduler()
request_spec = {
'volume_type': {'name': 'LVM_iSCSI'},
'share_properties': {'project_id': 1, 'size': 1},
}
retry = dict(num_attempts=2)
filter_properties = dict(retry=retry)
self.assertRaises(exception.NoValidHost, sched._schedule_share,
self.context, request_spec, filter_properties=filter_properties)
def test_add_retry_host(self): def test_add_retry_host(self):
retry = dict(num_attempts=1, hosts=[]) retry = dict(num_attempts=1, hosts=[])
filter_properties = dict(retry=retry) filter_properties = dict(retry=retry)
host = "fakehost" host = "fakehost"
sched = fakes.FakeFilterScheduler() sched = fakes.FakeFilterScheduler()
sched._add_retry_host(filter_properties, host) sched._add_retry_host(filter_properties, host)
hosts = filter_properties['retry']['hosts'] hosts = filter_properties['retry']['hosts']
self.assertEqual(1, len(hosts)) self.assertEqual(1, len(hosts))
self.assertEqual(host, hosts[0]) self.assertEqual(host, hosts[0])
@@ -144,13 +165,10 @@ class FilterSchedulerTestCase(test_scheduler.SchedulerTestCase):
retry = {'hosts': [], 'num_attempts': 1} retry = {'hosts': [], 'num_attempts': 1}
filter_properties = {'retry': retry} filter_properties = {'retry': retry}
sched = fakes.FakeFilterScheduler() sched = fakes.FakeFilterScheduler()
host_state = host_manager.HostState('host') host_state = host_manager.HostState('host')
host_state.total_capacity_gb = 1024 host_state.total_capacity_gb = 1024
sched._post_select_populate_filter_properties(filter_properties, sched._post_select_populate_filter_properties(filter_properties,
host_state) host_state)
self.assertEqual('host', self.assertEqual('host',
filter_properties['retry']['hosts'][0]) filter_properties['retry']['hosts'][0])
self.assertEqual(1024, host_state.total_capacity_gb) self.assertEqual(1024, host_state.total_capacity_gb)

View File

@@ -16,8 +16,6 @@ Tests For Scheduler Host Filters.
""" """
import httplib import httplib
import stubout
import testtools
from manila import context from manila import context
from manila import db from manila import db
@@ -30,36 +28,11 @@ from manila.tests import utils as test_utils
from manila import utils from manila import utils
DATA = ''
def stub_out_https_backend(stubs):
"""
Stubs out the httplib.HTTPRequest.getresponse to return
faked-out data instead of grabbing actual contents of a resource
The stubbed getresponse() returns an iterator over
the data "I am a teapot, short and stout\n"
:param stubs: Set of stubout stubs
"""
class FakeHTTPResponse(object):
def read(self):
return DATA
def fake_do_request(self, *args, **kwargs):
return httplib.OK, FakeHTTPResponse()
class HostFiltersTestCase(test.TestCase): class HostFiltersTestCase(test.TestCase):
"""Test case for host filters.""" """Test case for host filters."""
def setUp(self): def setUp(self):
super(HostFiltersTestCase, self).setUp() super(HostFiltersTestCase, self).setUp()
self.stubs = stubout.StubOutForTesting()
stub_out_https_backend(self.stubs)
self.context = context.RequestContext('fake', 'fake') self.context = context.RequestContext('fake', 'fake')
self.json_query = jsonutils.dumps( self.json_query = jsonutils.dumps(
['and', ['>=', '$free_capacity_gb', 1024], ['and', ['>=', '$free_capacity_gb', 1024],
@@ -77,8 +50,6 @@ class HostFiltersTestCase(test.TestCase):
return ret_value return ret_value
self.stubs.Set(utils, 'service_is_up', fake_service_is_up) self.stubs.Set(utils, 'service_is_up', fake_service_is_up)
@testtools.skipIf(not test_utils.is_manila_installed(),
'Test requires Manila installed')
def test_capacity_filter_passes(self): def test_capacity_filter_passes(self):
self._stub_service_is_up(True) self._stub_service_is_up(True)
filt_cls = self.class_map['CapacityFilter']() filt_cls = self.class_map['CapacityFilter']()
@@ -90,8 +61,6 @@ class HostFiltersTestCase(test.TestCase):
'service': service}) 'service': service})
self.assertTrue(filt_cls.host_passes(host, filter_properties)) self.assertTrue(filt_cls.host_passes(host, filter_properties))
@testtools.skipIf(not test_utils.is_manila_installed(),
'Test requires Manila installed')
def test_capacity_filter_fails(self): def test_capacity_filter_fails(self):
self._stub_service_is_up(True) self._stub_service_is_up(True)
filt_cls = self.class_map['CapacityFilter']() filt_cls = self.class_map['CapacityFilter']()
@@ -104,8 +73,6 @@ class HostFiltersTestCase(test.TestCase):
'service': service}) 'service': service})
self.assertFalse(filt_cls.host_passes(host, filter_properties)) self.assertFalse(filt_cls.host_passes(host, filter_properties))
@testtools.skipIf(not test_utils.is_manila_installed(),
'Test requires Manila installed')
def test_capacity_filter_passes_infinite(self): def test_capacity_filter_passes_infinite(self):
self._stub_service_is_up(True) self._stub_service_is_up(True)
filt_cls = self.class_map['CapacityFilter']() filt_cls = self.class_map['CapacityFilter']()
@@ -117,8 +84,6 @@ class HostFiltersTestCase(test.TestCase):
'service': service}) 'service': service})
self.assertTrue(filt_cls.host_passes(host, filter_properties)) self.assertTrue(filt_cls.host_passes(host, filter_properties))
@testtools.skipIf(not test_utils.is_manila_installed(),
'Test requires Manila installed')
def test_capacity_filter_passes_unknown(self): def test_capacity_filter_passes_unknown(self):
self._stub_service_is_up(True) self._stub_service_is_up(True)
filt_cls = self.class_map['CapacityFilter']() filt_cls = self.class_map['CapacityFilter']()
@@ -130,8 +95,6 @@ class HostFiltersTestCase(test.TestCase):
'service': service}) 'service': service})
self.assertTrue(filt_cls.host_passes(host, filter_properties)) self.assertTrue(filt_cls.host_passes(host, filter_properties))
@testtools.skipIf(not test_utils.is_manila_installed(),
'Test requires Manila installed')
def test_retry_filter_disabled(self): def test_retry_filter_disabled(self):
# Test case where retry/re-scheduling is disabled. # Test case where retry/re-scheduling is disabled.
filt_cls = self.class_map['RetryFilter']() filt_cls = self.class_map['RetryFilter']()
@@ -139,8 +102,6 @@ class HostFiltersTestCase(test.TestCase):
filter_properties = {} filter_properties = {}
self.assertTrue(filt_cls.host_passes(host, filter_properties)) self.assertTrue(filt_cls.host_passes(host, filter_properties))
@testtools.skipIf(not test_utils.is_manila_installed(),
'Test requires Manila installed')
def test_retry_filter_pass(self): def test_retry_filter_pass(self):
# Node not previously tried. # Node not previously tried.
filt_cls = self.class_map['RetryFilter']() filt_cls = self.class_map['RetryFilter']()
@@ -149,8 +110,6 @@ class HostFiltersTestCase(test.TestCase):
filter_properties = dict(retry=retry) filter_properties = dict(retry=retry)
self.assertTrue(filt_cls.host_passes(host, filter_properties)) self.assertTrue(filt_cls.host_passes(host, filter_properties))
@testtools.skipIf(not test_utils.is_manila_installed(),
'Test requires Manila installed')
def test_retry_filter_fail(self): def test_retry_filter_fail(self):
# Node was already tried. # Node was already tried.
filt_cls = self.class_map['RetryFilter']() filt_cls = self.class_map['RetryFilter']()

View File

@@ -15,17 +15,16 @@
""" """
Tests For HostManager Tests For HostManager
""" """
import mock
from oslo.config import cfg
from manila import db from manila import db
from manila import exception from manila import exception
from manila.openstack.common.scheduler import filters from manila.openstack.common.scheduler import filters
from manila.openstack.common import timeutils from manila.openstack.common import timeutils
from manila.scheduler import host_manager from manila.scheduler import host_manager
from manila import test from manila import test
from manila.tests.scheduler import fakes from manila.tests.scheduler import fakes
from oslo.config import cfg
CONF = cfg.CONF CONF = cfg.CONF
@@ -67,21 +66,6 @@ class HostManagerTestCase(test.TestCase):
self.assertEqual(len(filter_classes), 1) self.assertEqual(len(filter_classes), 1)
self.assertEqual(filter_classes[0].__name__, 'FakeFilterClass2') self.assertEqual(filter_classes[0].__name__, 'FakeFilterClass2')
def _mock_get_filtered_hosts(self, info, specified_filters=None):
self.mox.StubOutWithMock(self.host_manager, '_choose_host_filters')
info['got_objs'] = []
info['got_fprops'] = []
def fake_filter_one(_self, obj, filter_props):
info['got_objs'].append(obj)
info['got_fprops'].append(filter_props)
return True
self.stubs.Set(FakeFilterClass1, '_filter_one', fake_filter_one)
self.host_manager._choose_host_filters(specified_filters).AndReturn(
[FakeFilterClass1])
def _verify_result(self, info, result): def _verify_result(self, info, result):
for x in info['got_fprops']: for x in info['got_fprops']:
self.assertEqual(x, info['expected_fprops']) self.assertEqual(x, info['expected_fprops'])
@@ -90,37 +74,49 @@ class HostManagerTestCase(test.TestCase):
def test_get_filtered_hosts(self): def test_get_filtered_hosts(self):
fake_properties = {'moo': 1, 'cow': 2} fake_properties = {'moo': 1, 'cow': 2}
info = {
'expected_objs': self.fake_hosts,
'expected_fprops': fake_properties,
}
with mock.patch.object(self.host_manager, '_choose_host_filters',
mock.Mock(return_value=[FakeFilterClass1])):
info['got_objs'] = []
info['got_fprops'] = []
info = {'expected_objs': self.fake_hosts, def fake_filter_one(_self, obj, filter_props):
'expected_fprops': fake_properties} info['got_objs'].append(obj)
info['got_fprops'].append(filter_props)
return True
self._mock_get_filtered_hosts(info) self.stubs.Set(FakeFilterClass1, '_filter_one', fake_filter_one)
result = self.host_manager.get_filtered_hosts(self.fake_hosts,
self.mox.ReplayAll() fake_properties)
result = self.host_manager.get_filtered_hosts(self.fake_hosts, self._verify_result(info, result)
fake_properties) self.host_manager._choose_host_filters.assert_called_once_with(
self._verify_result(info, result) mock.ANY)
def test_update_service_capabilities_for_shares(self): def test_update_service_capabilities_for_shares(self):
service_states = self.host_manager.service_states service_states = self.host_manager.service_states
self.assertDictMatch(service_states, {}) self.assertDictMatch(service_states, {})
self.mox.StubOutWithMock(timeutils, 'utcnow')
timeutils.utcnow().AndReturn(31337)
timeutils.utcnow().AndReturn(31338)
timeutils.utcnow().AndReturn(31339)
host1_share_capabs = dict(free_capacity_gb=4321, timestamp=1) host1_share_capabs = dict(free_capacity_gb=4321, timestamp=1)
host2_share_capabs = dict(free_capacity_gb=5432, timestamp=1) host2_share_capabs = dict(free_capacity_gb=5432, timestamp=1)
host3_share_capabs = dict(free_capacity_gb=6543, timestamp=1) host3_share_capabs = dict(free_capacity_gb=6543, timestamp=1)
self.mox.ReplayAll()
service_name = 'share' service_name = 'share'
self.host_manager.update_service_capabilities(service_name, 'host1', with mock.patch.object(timeutils, 'utcnow',
host1_share_capabs) mock.Mock(return_value=31337)):
self.host_manager.update_service_capabilities(service_name, 'host2', self.host_manager.update_service_capabilities(
host2_share_capabs) service_name, 'host1', host1_share_capabs)
self.host_manager.update_service_capabilities(service_name, 'host3', timeutils.utcnow.assert_called_once_with()
host3_share_capabs) with mock.patch.object(timeutils, 'utcnow',
mock.Mock(return_value=31338)):
self.host_manager.update_service_capabilities(
service_name, 'host2', host2_share_capabs)
timeutils.utcnow.assert_called_once_with()
with mock.patch.object(timeutils, 'utcnow',
mock.Mock(return_value=31339)):
self.host_manager.update_service_capabilities(
service_name, 'host3', host3_share_capabs)
timeutils.utcnow.assert_called_once_with()
# Make sure dictionary isn't re-assigned # Make sure dictionary isn't re-assigned
self.assertEqual(self.host_manager.service_states, service_states) self.assertEqual(self.host_manager.service_states, service_states)
@@ -131,34 +127,30 @@ class HostManagerTestCase(test.TestCase):
host2_share_capabs['timestamp'] = 31338 host2_share_capabs['timestamp'] = 31338
host3_share_capabs['timestamp'] = 31339 host3_share_capabs['timestamp'] = 31339
expected = {'host1': host1_share_capabs, expected = {
'host2': host2_share_capabs, 'host1': host1_share_capabs,
'host3': host3_share_capabs} 'host2': host2_share_capabs,
'host3': host3_share_capabs,
}
self.assertDictMatch(service_states, expected) self.assertDictMatch(service_states, expected)
def test_get_all_host_states_share(self): def test_get_all_host_states_share(self):
context = 'fake_context' context = 'fake_context'
topic = CONF.share_topic topic = CONF.share_topic
self.mox.StubOutWithMock(db, 'service_get_all_by_topic')
self.mox.StubOutWithMock(host_manager.LOG, 'warn')
ret_services = fakes.SHARE_SERVICES ret_services = fakes.SHARE_SERVICES
db.service_get_all_by_topic(context, topic).AndReturn(ret_services) with mock.patch.object(db, 'service_get_all_by_topic',
# Disabled service mock.Mock(return_value=ret_services)):
host_manager.LOG.warn("service is down or disabled.") # Disabled service
self.host_manager.get_all_host_states_share(context)
host_state_map = self.host_manager.host_state_map
self.mox.ReplayAll() self.assertEqual(len(host_state_map), 4)
self.host_manager.get_all_host_states_share(context) # Check that service is up
host_state_map = self.host_manager.host_state_map for i in xrange(4):
share_node = fakes.SHARE_SERVICES[i]
self.assertEqual(len(host_state_map), 4) host = share_node['host']
# Check that service is up self.assertEqual(host_state_map[host].service, share_node)
for i in xrange(4): db.service_get_all_by_topic.assert_called_once_with(context, topic)
share_node = fakes.SHARE_SERVICES[i]
host = share_node['host']
self.assertEqual(host_state_map[host].service,
share_node)
class HostStateTestCase(test.TestCase): class HostStateTestCase(test.TestCase):

View File

@@ -1,5 +1,3 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2010 United States Government as represented by the # Copyright 2010 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration. # Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved. # All Rights Reserved.
@@ -19,19 +17,18 @@
Tests For Scheduler Tests For Scheduler
""" """
from mox import IsA import mock
from oslo.config import cfg
from manila import context from manila import context
from manila import db from manila import db
from manila import exception from manila import exception
from manila.openstack.common import timeutils from manila.openstack.common import timeutils
from manila.scheduler import driver from manila.scheduler import driver
from manila.scheduler import manager from manila.scheduler import manager
from manila.scheduler import simple from manila.scheduler import simple
from manila import test from manila import test
from manila import utils from manila import utils
from oslo.config import cfg
CONF = cfg.CONF CONF = cfg.CONF
@@ -44,9 +41,6 @@ class SchedulerManagerTestCase(test.TestCase):
driver_cls = driver.Scheduler driver_cls = driver.Scheduler
driver_cls_name = 'manila.scheduler.driver.Scheduler' driver_cls_name = 'manila.scheduler.driver.Scheduler'
class AnException(Exception):
pass
def setUp(self): def setUp(self):
super(SchedulerManagerTestCase, self).setUp() super(SchedulerManagerTestCase, self).setUp()
self.flags(scheduler_driver=self.driver_cls_name) self.flags(scheduler_driver=self.driver_cls_name)
@@ -64,64 +58,44 @@ class SchedulerManagerTestCase(test.TestCase):
def test_update_service_capabilities(self): def test_update_service_capabilities(self):
service_name = 'fake_service' service_name = 'fake_service'
host = 'fake_host' host = 'fake_host'
with mock.patch.object(self.manager.driver,
'update_service_capabilities', mock.Mock()):
result = self.manager.update_service_capabilities(
self.context, service_name=service_name, host=host)
self.manager.driver.update_service_capabilities.\
assert_called_once_with(service_name, host, {})
with mock.patch.object(self.manager.driver,
'update_service_capabilities', mock.Mock()):
capabilities = {'fake_capability': 'fake_value'}
result = self.manager.update_service_capabilities(
self.context, service_name=service_name, host=host,
capabilities=capabilities)
self.manager.driver.update_service_capabilities.\
assert_called_once_with(service_name, host, capabilities)
self.mox.StubOutWithMock(self.manager.driver, @mock.patch.object(db, 'share_update', mock.Mock())
'update_service_capabilities')
# Test no capabilities passes empty dictionary
self.manager.driver.update_service_capabilities(service_name,
host, {})
self.mox.ReplayAll()
result = self.manager.update_service_capabilities(
self.context,
service_name=service_name,
host=host)
self.mox.VerifyAll()
self.mox.ResetAll()
# Test capabilities passes correctly
capabilities = {'fake_capability': 'fake_value'}
self.manager.driver.update_service_capabilities(service_name,
host,
capabilities)
self.mox.ReplayAll()
result = self.manager.update_service_capabilities(
self.context,
service_name=service_name, host=host,
capabilities=capabilities)
def test_create_share_exception_puts_share_in_error_state(self): def test_create_share_exception_puts_share_in_error_state(self):
"""Test that a NoValideHost exception for create_share. """Test that a NoValideHost exception for create_share.
Puts the share in 'error' state and eats the exception. Puts the share in 'error' state and eats the exception.
""" """
fake_share_id = 1 def raise_no_valid_host(*args, **kwargs):
self._mox_schedule_method_helper('schedule_create_share') raise exception.NoValidHost(reason="")
self.mox.StubOutWithMock(db, 'share_update')
fake_share_id = 1
topic = 'fake_topic' topic = 'fake_topic'
share_id = fake_share_id share_id = fake_share_id
request_spec = {'share_id': fake_share_id} request_spec = {'share_id': fake_share_id}
with mock.patch.object(self.manager.driver,
self.manager.driver.schedule_create_share( 'schedule_create_share',
self.context, mock.Mock(side_effect=raise_no_valid_host)):
request_spec, {}).AndRaise(exception.NoValidHost(reason="")) self.manager.create_share(self.context, topic, share_id,
db.share_update(self.context, fake_share_id, {'status': 'error'}) request_spec=request_spec,
filter_properties={})
self.mox.ReplayAll() db.share_update.assert_called_once_with(
self.manager.create_share(self.context, topic, share_id, self.context, fake_share_id, {'status': 'error'})
request_spec=request_spec, self.manager.driver.schedule_create_share.assert_called_once_with(
filter_properties={}) self.context, request_spec, {})
def _mox_schedule_method_helper(self, method_name):
# Make sure the method exists that we're going to test call
def stub_method(*args, **kwargs):
pass
setattr(self.manager.driver, method_name, stub_method)
self.mox.StubOutWithMock(self.manager.driver,
method_name)
class SchedulerTestCase(test.TestCase): class SchedulerTestCase(test.TestCase):
@@ -139,35 +113,32 @@ class SchedulerTestCase(test.TestCase):
def test_update_service_capabilities(self): def test_update_service_capabilities(self):
service_name = 'fake_service' service_name = 'fake_service'
host = 'fake_host' host = 'fake_host'
self.mox.StubOutWithMock(self.driver.host_manager,
'update_service_capabilities')
capabilities = {'fake_capability': 'fake_value'} capabilities = {'fake_capability': 'fake_value'}
self.driver.host_manager.update_service_capabilities(service_name, with mock.patch.object(self.driver.host_manager,
host, 'update_service_capabilities', mock.Mock()):
capabilities) result = self.driver.update_service_capabilities(
self.mox.ReplayAll() service_name, host, capabilities)
result = self.driver.update_service_capabilities(service_name, self.driver.host_manager.update_service_capabilities.\
host, assert_called_once_with(service_name, host, capabilities)
capabilities)
def test_hosts_up(self): def test_hosts_up(self):
service1 = {'host': 'host1'} service1 = {'host': 'host1'}
service2 = {'host': 'host2'} service2 = {'host': 'host2'}
services = [service1, service2] services = [service1, service2]
self.mox.StubOutWithMock(db, 'service_get_all_by_topic') def fake_service_is_up(*args, **kwargs):
self.mox.StubOutWithMock(utils, 'service_is_up') if args[0]['host'] == 'host1':
return False
return True
db.service_get_all_by_topic(self.context, with mock.patch.object(db, 'service_get_all_by_topic',
self.topic).AndReturn(services) mock.Mock(return_value=services)):
utils.service_is_up(service1).AndReturn(False) with mock.patch.object(utils, 'service_is_up',
utils.service_is_up(service2).AndReturn(True) mock.Mock(side_effect=fake_service_is_up)):
result = self.driver.hosts_up(self.context, self.topic)
self.mox.ReplayAll() self.assertEqual(result, ['host2'])
result = self.driver.hosts_up(self.context, self.topic) db.service_get_all_by_topic.assert_called_once_with(
self.assertEqual(result, ['host2']) self.context, self.topic)
class SchedulerDriverBaseTestCase(SchedulerTestCase): class SchedulerDriverBaseTestCase(SchedulerTestCase):
@@ -190,17 +161,13 @@ class SchedulerDriverModuleTestCase(test.TestCase):
super(SchedulerDriverModuleTestCase, self).setUp() super(SchedulerDriverModuleTestCase, self).setUp()
self.context = context.RequestContext('fake_user', 'fake_project') self.context = context.RequestContext('fake_user', 'fake_project')
@mock.patch.object(db, 'share_update', mock.Mock())
def test_share_host_update_db(self): def test_share_host_update_db(self):
self.mox.StubOutWithMock(timeutils, 'utcnow') with mock.patch.object(timeutils, 'utcnow',
self.mox.StubOutWithMock(db, 'share_update') mock.Mock(return_value='fake-now')):
driver.share_update_db(self.context, 31337, 'fake_host')
timeutils.utcnow().AndReturn('fake-now') db.share_update.assert_called_once_with(self.context, 31337,
db.share_update(self.context, 31337, {'host': 'fake_host', 'scheduled_at': 'fake-now'})
{'host': 'fake_host',
'scheduled_at': 'fake-now'})
self.mox.ReplayAll()
driver.share_update_db(self.context, 31337, 'fake_host')
class SimpleSchedulerSharesTestCase(test.TestCase): class SimpleSchedulerSharesTestCase(test.TestCase):
@@ -214,147 +181,142 @@ class SimpleSchedulerSharesTestCase(test.TestCase):
'fake_project') 'fake_project')
self.admin_context.is_admin = True self.admin_context.is_admin = True
@mock.patch.object(utils, 'service_is_up', mock.Mock(return_value=True))
def test_create_share_if_two_services_up(self): def test_create_share_if_two_services_up(self):
share_id = 'fake' share_id = 'fake'
fake_share = {'id': share_id, 'size': 1} fake_share = {'id': share_id, 'size': 1}
fake_service_1 = {'disabled': False, 'host': 'fake_host1'} fake_service_1 = {'disabled': False, 'host': 'fake_host1'}
fake_service_2 = {'disabled': False, 'host': 'fake_host2'} fake_service_2 = {'disabled': False, 'host': 'fake_host2'}
fake_result = [(fake_service_1, 2), (fake_service_2, 1)] fake_result = [(fake_service_1, 2), (fake_service_2, 1)]
fake_request_spec = {
self.mox.StubOutWithMock(db, 'service_get_all_share_sorted') 'share_id': share_id,
self.mox.StubOutWithMock(utils, 'service_is_up') 'share_properties': fake_share,
self.mox.StubOutWithMock(driver, 'share_update_db') }
with mock.patch.object(db, 'service_get_all_share_sorted',
fake_request_spec = {'share_id': share_id, mock.Mock(return_value=fake_result)):
'share_properties': fake_share} with mock.patch.object(driver, 'share_update_db',
mock.Mock(return_value=fake_share)):
db.service_get_all_share_sorted(IsA(context.RequestContext))\ self.driver.schedule_create_share(self.context,
.AndReturn(fake_result) fake_request_spec, {})
utils.service_is_up(IsA(dict)).AndReturn(True) utils.service_is_up.assert_called_once_with(
driver.share_update_db(IsA(context.RequestContext), share_id, utils.IsAMatcher(dict))
'fake_host1').AndReturn(fake_share) db.service_get_all_share_sorted.assert_called_once_with(
self.mox.ReplayAll() utils.IsAMatcher(context.RequestContext))
driver.share_update_db.assert_called_once_with(
self.driver.schedule_create_share(self.context, fake_request_spec, {}) utils.IsAMatcher(context.RequestContext),
share_id, 'fake_host1')
def test_create_share_if_services_not_available(self): def test_create_share_if_services_not_available(self):
share_id = 'fake' share_id = 'fake'
fake_share = {'id': share_id, 'size': 1} fake_share = {'id': share_id, 'size': 1}
fake_result = [] fake_result = []
fake_request_spec = {
fake_request_spec = {'share_id': share_id, 'share_id': share_id,
'share_properties': fake_share} 'share_properties': fake_share,
}
self.mox.StubOutWithMock(db, 'service_get_all_share_sorted') with mock.patch.object(db, 'service_get_all_share_sorted',
mock.Mock(return_value=fake_result)):
db.service_get_all_share_sorted(IsA(context.RequestContext))\ self.assertRaises(exception.NoValidHost,
.AndReturn(fake_result) self.driver.schedule_create_share,
self.context, fake_request_spec, {})
self.mox.ReplayAll() db.service_get_all_share_sorted.assert_called_once_with(
utils.IsAMatcher(context.RequestContext))
self.assertRaises(exception.NoValidHost,
self.driver.schedule_create_share,
self.context, fake_request_spec, {})
def test_create_share_if_max_gigabytes_exceeded(self): def test_create_share_if_max_gigabytes_exceeded(self):
share_id = 'fake' share_id = 'fake'
fake_share = {'id': share_id, 'size': 10001} fake_share = {'id': share_id, 'size': 10001}
fake_service_1 = {'disabled': False, 'host': 'fake_host1'} fake_service_1 = {'disabled': False, 'host': 'fake_host1'}
fake_service_2 = {'disabled': False, 'host': 'fake_host2'} fake_service_2 = {'disabled': False, 'host': 'fake_host2'}
fake_result = [(fake_service_1, 5), (fake_service_2, 7)] fake_result = [(fake_service_1, 5), (fake_service_2, 7)]
fake_request_spec = {
'share_id': share_id,
'share_properties': fake_share,
}
with mock.patch.object(db, 'service_get_all_share_sorted',
mock.Mock(return_value=fake_result)):
self.assertRaises(exception.NoValidHost,
self.driver.schedule_create_share,
self.context, fake_request_spec, {})
db.service_get_all_share_sorted.assert_called_once_with(
utils.IsAMatcher(context.RequestContext))
fake_request_spec = {'share_id': share_id, @mock.patch.object(utils, 'service_is_up', mock.Mock(return_value=True))
'share_properties': fake_share}
self.mox.StubOutWithMock(db, 'service_get_all_share_sorted')
db.service_get_all_share_sorted(IsA(context.RequestContext))\
.AndReturn(fake_result)
self.mox.ReplayAll()
self.assertRaises(exception.NoValidHost,
self.driver.schedule_create_share,
self.context, fake_request_spec, {})
def test_create_share_availability_zone(self): def test_create_share_availability_zone(self):
share_id = 'fake' share_id = 'fake'
fake_share = {'id': share_id, fake_share = {
'availability_zone': 'fake:fake', 'id': share_id,
'size': 1} 'availability_zone': 'fake:fake',
'size': 1,
fake_service_1 = {'disabled': False, 'host': 'fake_host1', }
'availability_zone': 'fake'} fake_service_1 = {
'disabled': False, 'host': 'fake_host1',
fake_service_2 = {'disabled': False, 'host': 'fake_host2', 'availability_zone': 'fake',
'availability_zone': 'super_fake'} }
fake_service_2 = {
'disabled': False, 'host': 'fake_host2',
'availability_zone': 'super_fake',
}
fake_result = [(fake_service_1, 0), (fake_service_2, 1)] fake_result = [(fake_service_1, 0), (fake_service_2, 1)]
fake_request_spec = {
'share_id': share_id,
'share_properties': fake_share,
}
with mock.patch.object(db, 'service_get_all_share_sorted',
mock.Mock(return_value=fake_result)):
with mock.patch.object(driver, 'share_update_db',
mock.Mock(return_value=fake_share)):
self.driver.schedule_create_share(self.context,
fake_request_spec, {})
utils.service_is_up.assert_called_once_with(fake_service_1)
driver.share_update_db.assert_called_once_with(
utils.IsAMatcher(context.RequestContext), share_id,
fake_service_1['host'])
db.service_get_all_share_sorted.assert_called_once_with(
utils.IsAMatcher(context.RequestContext))
fake_request_spec = {'share_id': share_id, @mock.patch.object(utils, 'service_is_up', mock.Mock(return_value=True))
'share_properties': fake_share}
self.mox.StubOutWithMock(utils, 'service_is_up')
self.mox.StubOutWithMock(driver, 'share_update_db')
self.mox.StubOutWithMock(db, 'service_get_all_share_sorted')
db.service_get_all_share_sorted(IsA(context.RequestContext))\
.AndReturn(fake_result)
utils.service_is_up(fake_service_1).AndReturn(True)
driver.share_update_db(IsA(context.RequestContext), share_id,
fake_service_1['host']).AndReturn(fake_share)
self.mox.ReplayAll()
self.driver.schedule_create_share(self.context, fake_request_spec, {})
def test_create_share_availability_zone_on_host(self): def test_create_share_availability_zone_on_host(self):
share_id = 'fake' share_id = 'fake'
fake_share = {'id': share_id, fake_share = {
'availability_zone': 'fake:fake', 'id': share_id,
'size': 1} 'availability_zone': 'fake:fake',
'size': 1,
fake_request_spec = {'share_id': share_id, }
'share_properties': fake_share} fake_request_spec = {
'share_id': share_id,
self.mox.StubOutWithMock(utils, 'service_is_up') 'share_properties': fake_share,
self.mox.StubOutWithMock(db, 'service_get_by_args') }
self.mox.StubOutWithMock(driver, 'share_update_db') with mock.patch.object(db, 'service_get_by_args',
mock.Mock(return_value='fake_service')):
db.service_get_by_args(IsA(context.RequestContext), 'fake', with mock.patch.object(driver, 'share_update_db',
'manila-share').AndReturn('fake_service') mock.Mock(return_value=fake_share)):
utils.service_is_up('fake_service').AndReturn(True) self.driver.schedule_create_share(self.admin_context,
driver.share_update_db(IsA(context.RequestContext), share_id, fake_request_spec, {})
'fake').AndReturn(fake_share) utils.service_is_up.assert_called_once_with('fake_service')
db.service_get_by_args.assert_called_once_with(
self.mox.ReplayAll() utils.IsAMatcher(context.RequestContext),
self.driver.schedule_create_share(self.admin_context, 'fake', 'manila-share')
fake_request_spec, {}) driver.share_update_db.assert_called_once_with(
utils.IsAMatcher(context.RequestContext), share_id, 'fake')
@mock.patch.object(utils, 'service_is_up', mock.Mock(return_value=False))
def test_create_share_availability_zone_if_service_down(self): def test_create_share_availability_zone_if_service_down(self):
share_id = 'fake' share_id = 'fake'
fake_share = {'id': share_id, fake_share = {
'availability_zone': 'fake:fake', 'id': share_id,
'size': 1} 'availability_zone': 'fake:fake',
'size': 1,
fake_request_spec = {'share_id': share_id, }
'share_properties': fake_share} fake_request_spec = {
'share_id': share_id,
self.mox.StubOutWithMock(utils, 'service_is_up') 'share_properties': fake_share,
self.mox.StubOutWithMock(db, 'service_get_by_args') }
with mock.patch.object(db, 'service_get_by_args',
db.service_get_by_args(IsA(context.RequestContext), 'fake', mock.Mock(return_value='fake_service')):
'manila-share').AndReturn('fake_service') self.assertRaises(exception.WillNotSchedule,
utils.service_is_up('fake_service').AndReturn(False) self.driver.schedule_create_share,
self.admin_context, fake_request_spec, {})
self.mox.ReplayAll() utils.service_is_up.assert_called_once_with('fake_service')
self.assertRaises(exception.WillNotSchedule, db.service_get_by_args.assert_called_once_with(
self.driver.schedule_create_share, utils.IsAMatcher(context.RequestContext),
self.admin_context, fake_request_spec, {}) 'fake', 'manila-share')

View File

@@ -2,7 +2,6 @@ hacking>=0.8.0,<0.9
coverage>=3.6 coverage>=3.6
mock>=1.0 mock>=1.0
mox>=0.5.3
MySQL-python MySQL-python
nose nose
nosehtmloutput>=0.0.3 nosehtmloutput>=0.0.3