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

View File

@ -1,9 +1,9 @@
=====================================
=======================================
OpenStack Manila Testing Infrastructure
=====================================
=======================================
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;
- 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
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
------------------
TBD
Using Fakes
~~~~~~~~~~~
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.
Usage of mox has been deprecated for writing Manila unit tests.
- use addCleanup in favor of tearDown
test.TestCase
-------------
The TestCase class from manila.test (generally imported as test) will
automatically manage self.stubs using the stubout module and self.mox
using the mox module during the setUp step. They will automatically
verify and clean up during the tearDown step.
automatically manage self.stubs using the stubout module.
They will automatically verify and clean up during the tearDown step.
If using test.TestCase, calling the super class setUp is required and
calling the super class tearDown is required to be last if tearDown
is overridden.
Writing Functional Tests
------------------------
Running Tests
-------------
TBD
Writing Integration 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.
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
the -h parameter to get options `./run_tests.sh -h`
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
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
@ -42,7 +40,9 @@ setattr(__builtin__, '_', lambda x: x)
import os
import shutil
from manila.db import migration
from manila.db.sqlalchemy.session import get_engine
from manila.tests import conf_fixture
CONF = cfg.CONF
@ -62,13 +62,7 @@ def reset_db():
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)
if CONF.sql_connection == "sqlite://":
if migration.db_version() > 1:
return

View File

@ -16,28 +16,11 @@
Fakes For Scheduler tests.
"""
import mox
from manila import db
from manila.openstack.common import timeutils
from manila.scheduler import filter_scheduler
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 = [
dict(id=1, host='host1', topic='share', disabled=False,
availability_zone='zone1', updated_at=timeutils.utcnow()),
@ -90,15 +73,22 @@ class FakeHostState(host_manager.HostState):
setattr(self, key, val)
def mox_host_manager_db_calls(mock, context):
mock.StubOutWithMock(db, 'service_get_all_by_topic')
db.service_get_all_by_topic(mox.IgnoreArg(),
mox.IgnoreArg()).AndReturn(VOLUME_SERVICES)
def mox_host_manager_db_calls_share(mock, context):
mock.StubOutWithMock(db, 'service_get_all_by_topic')
db.service_get_all_by_topic(mox.IgnoreArg(),
mox.IgnoreArg()).AndReturn(SHARE_SERVICES)
def mock_host_manager_db_calls(mock_obj, disabled=None):
services = [
dict(id=1, host='host1', topic='share', disabled=False,
availability_zone='zone1', updated_at=timeutils.utcnow()),
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,
availability_zone='zone2', updated_at=timeutils.utcnow()),
dict(id=4, host='host4', topic='share', disabled=False,
availability_zone='zone3', updated_at=timeutils.utcnow()),
# service on host5 is disabled
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.
"""
import mock
from oslo.config import cfg
import testtools
from manila import context
from manila.openstack.common.scheduler.weights import HostWeightHandler
from manila.scheduler.weights.capacity import CapacityWeigher
from manila import test
from manila.tests.scheduler import fakes
from manila.tests import utils as test_utils
CONF = cfg.CONF
class CapacityWeigherTestCase(test.TestCase):
@ -30,26 +34,25 @@ class CapacityWeigherTestCase(test.TestCase):
super(CapacityWeigherTestCase, self).setUp()
self.host_manager = fakes.FakeHostManager()
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):
if weight_properties is None:
weight_properties = {}
return self.weight_handler.get_weighed_objects(self.weight_classes,
return self.weight_handler.get_weighed_objects([CapacityWeigher],
hosts,
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()
fakes.mox_host_manager_db_calls(self.mox, ctxt)
self.mox.ReplayAll()
host_states = self.host_manager.get_all_host_states(ctxt)
self.mox.VerifyAll()
self.mox.ResetAll()
fakes.mock_host_manager_db_calls(_mock_service_get_all_by_topic,
disabled=disabled)
host_states = self.host_manager.get_all_host_states_share(ctxt)
_mock_service_get_all_by_topic.assert_called_once_with(
ctxt, CONF.share_topic)
return host_states
@testtools.skipIf(not test_utils.is_manila_installed(),
'Test requires Manila installed')
@testtools.skip("LP bug #1329718")
def test_default_of_spreading_first(self):
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.obj.host, 'host1')
@testtools.skipIf(not test_utils.is_manila_installed(),
'Test requires Manila installed')
@testtools.skip("LP bug #1329718")
def test_capacity_weight_multiplier1(self):
self.flags(capacity_weight_multiplier=-1.0)
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.obj.host, 'host4')
@testtools.skipIf(not test_utils.is_manila_installed(),
'Test requires Manila installed')
@testtools.skip("LP bug #1329718")
def test_capacity_weight_multiplier2(self):
self.flags(capacity_weight_multiplier=2.0)
hostinfo_list = self._get_all_hosts()

View File

@ -15,21 +15,14 @@
"""
Tests For Filter Scheduler.
"""
import testtools
import mock
from manila import context
from manila import exception
from manila.openstack.common.scheduler import weights
from manila.scheduler import filter_scheduler
from manila.scheduler import host_manager
from manila import test
from manila.tests.scheduler import fakes
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):
@ -37,104 +30,132 @@ class FilterSchedulerTestCase(test_scheduler.SchedulerTestCase):
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):
"""
Ensure empty hosts & child_zones result in NoValidHosts exception.
"""
def _fake_empty_call_zone_method(*args, **kwargs):
return []
# Ensure empty hosts/child_zones result in NoValidHosts exception.
sched = fakes.FakeFilterScheduler()
fake_context = context.RequestContext('user', 'project')
request_spec = {'share_properties': {'project_id': 1,
'size': 1},
'share_type': {'name': 'LVM_NFS'},
'share_id': ['fake-id1']}
request_spec = {
'share_properties': {'project_id': 1, 'size': 1},
'share_type': {'name': 'LVM_NFS'},
'share_id': ['fake-id1'],
}
self.assertRaises(exception.NoValidHost, sched.schedule_create_share,
fake_context, request_spec, {})
@testtools.skipIf(not test_utils.is_manila_installed(),
'Test requires Manila installed (try setup.py develop')
def test_create_share_non_admin(self):
"""Test creating share passing a non-admin context.
DB actions should work."""
@mock.patch('manila.scheduler.host_manager.HostManager.'
'get_all_host_states_share')
def test_create_share_non_admin(self, _mock_get_all_host_states):
# Test creating a volume locally using create_volume, passing
# a non-admin context. DB actions should work.
self.was_admin = False
def fake_get(context, *args, **kwargs):
# make sure this is called with admin context, even though
# we're using user context below
# Make sure this is called with admin context, even though
# we're using user context below.
self.was_admin = context.is_admin
return {}
sched = fakes.FakeFilterScheduler()
self.stubs.Set(sched.host_manager,
'get_all_host_states_share',
fake_get)
_mock_get_all_host_states.side_effect = fake_get
fake_context = context.RequestContext('user', 'project')
request_spec = {'share_properties': {'project_id': 1,
'size': 1},
'share_type': {'name': 'LVM_NFS'},
'share_id': ['fake-id1']}
request_spec = {
'share_properties': {'project_id': 1, 'size': 1},
'share_type': {'name': 'LVM_NFS'},
'share_id': ['fake-id1'],
}
self.assertRaises(exception.NoValidHost, sched.schedule_create_share,
fake_context, request_spec, {})
self.assertTrue(self.was_admin)
@testtools.skipIf(not test_utils.is_manila_installed(),
'Test requires Manila installed (try setup.py develop')
def test_schedule_happy_day_share(self):
"""Make sure there's nothing glaringly wrong with _schedule_share()
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)]
@mock.patch('manila.db.service_get_all_by_topic')
def test_schedule_happy_day_share(self, _mock_service_get_all_by_topic):
# Make sure there's nothing glaringly wrong with _schedule()
# by doing a happy day pass through.
sched = fakes.FakeFilterScheduler()
sched.host_manager = fakes.FakeHostManager()
fake_context = context.RequestContext('user', 'project',
is_admin=True)
self.stubs.Set(sched.host_manager, 'get_filtered_hosts',
fake_get_filtered_hosts)
self.stubs.Set(weights.HostWeightHandler,
'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()
fakes.mock_host_manager_db_calls(_mock_service_get_all_by_topic)
request_spec = {
'share_type': {'name': 'LVM_NFS'},
'share_properties': {'project_id': 1, 'size': 1},
}
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):
self.flags(scheduler_max_attempts=4)
sched = fakes.FakeFilterScheduler()
self.assertEqual(4, sched._max_attempts())
def test_invalid_max_attempts(self):
self.flags(scheduler_max_attempts=0)
self.assertRaises(exception.InvalidParameterValue,
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):
retry = dict(num_attempts=1, hosts=[])
filter_properties = dict(retry=retry)
host = "fakehost"
sched = fakes.FakeFilterScheduler()
sched._add_retry_host(filter_properties, host)
hosts = filter_properties['retry']['hosts']
self.assertEqual(1, len(hosts))
self.assertEqual(host, hosts[0])
@ -144,13 +165,10 @@ class FilterSchedulerTestCase(test_scheduler.SchedulerTestCase):
retry = {'hosts': [], 'num_attempts': 1}
filter_properties = {'retry': retry}
sched = fakes.FakeFilterScheduler()
host_state = host_manager.HostState('host')
host_state.total_capacity_gb = 1024
sched._post_select_populate_filter_properties(filter_properties,
host_state)
self.assertEqual('host',
filter_properties['retry']['hosts'][0])
self.assertEqual(1024, host_state.total_capacity_gb)

View File

@ -16,8 +16,6 @@ Tests For Scheduler Host Filters.
"""
import httplib
import stubout
import testtools
from manila import context
from manila import db
@ -30,36 +28,11 @@ from manila.tests import utils as test_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):
"""Test case for host filters."""
def setUp(self):
super(HostFiltersTestCase, self).setUp()
self.stubs = stubout.StubOutForTesting()
stub_out_https_backend(self.stubs)
self.context = context.RequestContext('fake', 'fake')
self.json_query = jsonutils.dumps(
['and', ['>=', '$free_capacity_gb', 1024],
@ -77,8 +50,6 @@ class HostFiltersTestCase(test.TestCase):
return ret_value
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):
self._stub_service_is_up(True)
filt_cls = self.class_map['CapacityFilter']()
@ -90,8 +61,6 @@ class HostFiltersTestCase(test.TestCase):
'service': service})
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):
self._stub_service_is_up(True)
filt_cls = self.class_map['CapacityFilter']()
@ -104,8 +73,6 @@ class HostFiltersTestCase(test.TestCase):
'service': service})
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):
self._stub_service_is_up(True)
filt_cls = self.class_map['CapacityFilter']()
@ -117,8 +84,6 @@ class HostFiltersTestCase(test.TestCase):
'service': service})
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):
self._stub_service_is_up(True)
filt_cls = self.class_map['CapacityFilter']()
@ -130,8 +95,6 @@ class HostFiltersTestCase(test.TestCase):
'service': service})
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):
# Test case where retry/re-scheduling is disabled.
filt_cls = self.class_map['RetryFilter']()
@ -139,8 +102,6 @@ class HostFiltersTestCase(test.TestCase):
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):
# Node not previously tried.
filt_cls = self.class_map['RetryFilter']()
@ -149,8 +110,6 @@ class HostFiltersTestCase(test.TestCase):
filter_properties = dict(retry=retry)
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):
# Node was already tried.
filt_cls = self.class_map['RetryFilter']()

View File

@ -15,17 +15,16 @@
"""
Tests For HostManager
"""
import mock
from oslo.config import cfg
from manila import db
from manila import exception
from manila.openstack.common.scheduler import filters
from manila.openstack.common import timeutils
from manila.scheduler import host_manager
from manila import test
from manila.tests.scheduler import fakes
from oslo.config import cfg
CONF = cfg.CONF
@ -67,21 +66,6 @@ class HostManagerTestCase(test.TestCase):
self.assertEqual(len(filter_classes), 1)
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):
for x in info['got_fprops']:
self.assertEqual(x, info['expected_fprops'])
@ -90,37 +74,49 @@ class HostManagerTestCase(test.TestCase):
def test_get_filtered_hosts(self):
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,
'expected_fprops': fake_properties}
def fake_filter_one(_self, obj, filter_props):
info['got_objs'].append(obj)
info['got_fprops'].append(filter_props)
return True
self._mock_get_filtered_hosts(info)
self.mox.ReplayAll()
result = self.host_manager.get_filtered_hosts(self.fake_hosts,
fake_properties)
self._verify_result(info, result)
self.stubs.Set(FakeFilterClass1, '_filter_one', fake_filter_one)
result = self.host_manager.get_filtered_hosts(self.fake_hosts,
fake_properties)
self._verify_result(info, result)
self.host_manager._choose_host_filters.assert_called_once_with(
mock.ANY)
def test_update_service_capabilities_for_shares(self):
service_states = self.host_manager.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)
host2_share_capabs = dict(free_capacity_gb=5432, timestamp=1)
host3_share_capabs = dict(free_capacity_gb=6543, timestamp=1)
self.mox.ReplayAll()
service_name = 'share'
self.host_manager.update_service_capabilities(service_name, 'host1',
host1_share_capabs)
self.host_manager.update_service_capabilities(service_name, 'host2',
host2_share_capabs)
self.host_manager.update_service_capabilities(service_name, 'host3',
host3_share_capabs)
with mock.patch.object(timeutils, 'utcnow',
mock.Mock(return_value=31337)):
self.host_manager.update_service_capabilities(
service_name, 'host1', host1_share_capabs)
timeutils.utcnow.assert_called_once_with()
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
self.assertEqual(self.host_manager.service_states, service_states)
@ -131,34 +127,30 @@ class HostManagerTestCase(test.TestCase):
host2_share_capabs['timestamp'] = 31338
host3_share_capabs['timestamp'] = 31339
expected = {'host1': host1_share_capabs,
'host2': host2_share_capabs,
'host3': host3_share_capabs}
expected = {
'host1': host1_share_capabs,
'host2': host2_share_capabs,
'host3': host3_share_capabs,
}
self.assertDictMatch(service_states, expected)
def test_get_all_host_states_share(self):
context = 'fake_context'
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
db.service_get_all_by_topic(context, topic).AndReturn(ret_services)
# Disabled service
host_manager.LOG.warn("service is down or disabled.")
with mock.patch.object(db, 'service_get_all_by_topic',
mock.Mock(return_value=ret_services)):
# Disabled service
self.host_manager.get_all_host_states_share(context)
host_state_map = self.host_manager.host_state_map
self.mox.ReplayAll()
self.host_manager.get_all_host_states_share(context)
host_state_map = self.host_manager.host_state_map
self.assertEqual(len(host_state_map), 4)
# Check that service is up
for i in xrange(4):
share_node = fakes.SHARE_SERVICES[i]
host = share_node['host']
self.assertEqual(host_state_map[host].service,
share_node)
self.assertEqual(len(host_state_map), 4)
# Check that service is up
for i in xrange(4):
share_node = fakes.SHARE_SERVICES[i]
host = share_node['host']
self.assertEqual(host_state_map[host].service, share_node)
db.service_get_all_by_topic.assert_called_once_with(context, topic)
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
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
@ -19,19 +17,18 @@
Tests For Scheduler
"""
from mox import IsA
import mock
from oslo.config import cfg
from manila import context
from manila import db
from manila import exception
from manila.openstack.common import timeutils
from manila.scheduler import driver
from manila.scheduler import manager
from manila.scheduler import simple
from manila import test
from manila import utils
from oslo.config import cfg
CONF = cfg.CONF
@ -44,9 +41,6 @@ class SchedulerManagerTestCase(test.TestCase):
driver_cls = driver.Scheduler
driver_cls_name = 'manila.scheduler.driver.Scheduler'
class AnException(Exception):
pass
def setUp(self):
super(SchedulerManagerTestCase, self).setUp()
self.flags(scheduler_driver=self.driver_cls_name)
@ -64,64 +58,44 @@ class SchedulerManagerTestCase(test.TestCase):
def test_update_service_capabilities(self):
service_name = 'fake_service'
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,
'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)
@mock.patch.object(db, 'share_update', mock.Mock())
def test_create_share_exception_puts_share_in_error_state(self):
"""Test that a NoValideHost exception for create_share.
Puts the share in 'error' state and eats the exception.
"""
fake_share_id = 1
self._mox_schedule_method_helper('schedule_create_share')
self.mox.StubOutWithMock(db, 'share_update')
def raise_no_valid_host(*args, **kwargs):
raise exception.NoValidHost(reason="")
fake_share_id = 1
topic = 'fake_topic'
share_id = fake_share_id
request_spec = {'share_id': fake_share_id}
self.manager.driver.schedule_create_share(
self.context,
request_spec, {}).AndRaise(exception.NoValidHost(reason=""))
db.share_update(self.context, fake_share_id, {'status': 'error'})
self.mox.ReplayAll()
self.manager.create_share(self.context, topic, share_id,
request_spec=request_spec,
filter_properties={})
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)
with mock.patch.object(self.manager.driver,
'schedule_create_share',
mock.Mock(side_effect=raise_no_valid_host)):
self.manager.create_share(self.context, topic, share_id,
request_spec=request_spec,
filter_properties={})
db.share_update.assert_called_once_with(
self.context, fake_share_id, {'status': 'error'})
self.manager.driver.schedule_create_share.assert_called_once_with(
self.context, request_spec, {})
class SchedulerTestCase(test.TestCase):
@ -139,35 +113,32 @@ class SchedulerTestCase(test.TestCase):
def test_update_service_capabilities(self):
service_name = 'fake_service'
host = 'fake_host'
self.mox.StubOutWithMock(self.driver.host_manager,
'update_service_capabilities')
capabilities = {'fake_capability': 'fake_value'}
self.driver.host_manager.update_service_capabilities(service_name,
host,
capabilities)
self.mox.ReplayAll()
result = self.driver.update_service_capabilities(service_name,
host,
capabilities)
with mock.patch.object(self.driver.host_manager,
'update_service_capabilities', mock.Mock()):
result = self.driver.update_service_capabilities(
service_name, host, capabilities)
self.driver.host_manager.update_service_capabilities.\
assert_called_once_with(service_name, host, capabilities)
def test_hosts_up(self):
service1 = {'host': 'host1'}
service2 = {'host': 'host2'}
services = [service1, service2]
self.mox.StubOutWithMock(db, 'service_get_all_by_topic')
self.mox.StubOutWithMock(utils, 'service_is_up')
def fake_service_is_up(*args, **kwargs):
if args[0]['host'] == 'host1':
return False
return True
db.service_get_all_by_topic(self.context,
self.topic).AndReturn(services)
utils.service_is_up(service1).AndReturn(False)
utils.service_is_up(service2).AndReturn(True)
self.mox.ReplayAll()
result = self.driver.hosts_up(self.context, self.topic)
self.assertEqual(result, ['host2'])
with mock.patch.object(db, 'service_get_all_by_topic',
mock.Mock(return_value=services)):
with mock.patch.object(utils, 'service_is_up',
mock.Mock(side_effect=fake_service_is_up)):
result = self.driver.hosts_up(self.context, self.topic)
self.assertEqual(result, ['host2'])
db.service_get_all_by_topic.assert_called_once_with(
self.context, self.topic)
class SchedulerDriverBaseTestCase(SchedulerTestCase):
@ -190,17 +161,13 @@ class SchedulerDriverModuleTestCase(test.TestCase):
super(SchedulerDriverModuleTestCase, self).setUp()
self.context = context.RequestContext('fake_user', 'fake_project')
@mock.patch.object(db, 'share_update', mock.Mock())
def test_share_host_update_db(self):
self.mox.StubOutWithMock(timeutils, 'utcnow')
self.mox.StubOutWithMock(db, 'share_update')
timeutils.utcnow().AndReturn('fake-now')
db.share_update(self.context, 31337,
{'host': 'fake_host',
'scheduled_at': 'fake-now'})
self.mox.ReplayAll()
driver.share_update_db(self.context, 31337, 'fake_host')
with mock.patch.object(timeutils, 'utcnow',
mock.Mock(return_value='fake-now')):
driver.share_update_db(self.context, 31337, 'fake_host')
db.share_update.assert_called_once_with(self.context, 31337,
{'host': 'fake_host', 'scheduled_at': 'fake-now'})
class SimpleSchedulerSharesTestCase(test.TestCase):
@ -214,147 +181,142 @@ class SimpleSchedulerSharesTestCase(test.TestCase):
'fake_project')
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):
share_id = 'fake'
fake_share = {'id': share_id, 'size': 1}
fake_service_1 = {'disabled': False, 'host': 'fake_host1'}
fake_service_2 = {'disabled': False, 'host': 'fake_host2'}
fake_result = [(fake_service_1, 2), (fake_service_2, 1)]
self.mox.StubOutWithMock(db, 'service_get_all_share_sorted')
self.mox.StubOutWithMock(utils, 'service_is_up')
self.mox.StubOutWithMock(driver, 'share_update_db')
fake_request_spec = {'share_id': share_id,
'share_properties': fake_share}
db.service_get_all_share_sorted(IsA(context.RequestContext))\
.AndReturn(fake_result)
utils.service_is_up(IsA(dict)).AndReturn(True)
driver.share_update_db(IsA(context.RequestContext), share_id,
'fake_host1').AndReturn(fake_share)
self.mox.ReplayAll()
self.driver.schedule_create_share(self.context, fake_request_spec, {})
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(
utils.IsAMatcher(dict))
db.service_get_all_share_sorted.assert_called_once_with(
utils.IsAMatcher(context.RequestContext))
driver.share_update_db.assert_called_once_with(
utils.IsAMatcher(context.RequestContext),
share_id, 'fake_host1')
def test_create_share_if_services_not_available(self):
share_id = 'fake'
fake_share = {'id': share_id, 'size': 1}
fake_result = []
fake_request_spec = {'share_id': share_id,
'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, {})
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))
def test_create_share_if_max_gigabytes_exceeded(self):
share_id = 'fake'
fake_share = {'id': share_id, 'size': 10001}
fake_service_1 = {'disabled': False, 'host': 'fake_host1'}
fake_service_2 = {'disabled': False, 'host': 'fake_host2'}
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,
'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, {})
@mock.patch.object(utils, 'service_is_up', mock.Mock(return_value=True))
def test_create_share_availability_zone(self):
share_id = 'fake'
fake_share = {'id': share_id,
'availability_zone': 'fake:fake',
'size': 1}
fake_service_1 = {'disabled': False, 'host': 'fake_host1',
'availability_zone': 'fake'}
fake_service_2 = {'disabled': False, 'host': 'fake_host2',
'availability_zone': 'super_fake'}
fake_share = {
'id': share_id,
'availability_zone': 'fake:fake',
'size': 1,
}
fake_service_1 = {
'disabled': False, 'host': 'fake_host1',
'availability_zone': 'fake',
}
fake_service_2 = {
'disabled': False, 'host': 'fake_host2',
'availability_zone': 'super_fake',
}
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,
'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, {})
@mock.patch.object(utils, 'service_is_up', mock.Mock(return_value=True))
def test_create_share_availability_zone_on_host(self):
share_id = 'fake'
fake_share = {'id': share_id,
'availability_zone': 'fake:fake',
'size': 1}
fake_request_spec = {'share_id': share_id,
'share_properties': fake_share}
self.mox.StubOutWithMock(utils, 'service_is_up')
self.mox.StubOutWithMock(db, 'service_get_by_args')
self.mox.StubOutWithMock(driver, 'share_update_db')
db.service_get_by_args(IsA(context.RequestContext), 'fake',
'manila-share').AndReturn('fake_service')
utils.service_is_up('fake_service').AndReturn(True)
driver.share_update_db(IsA(context.RequestContext), share_id,
'fake').AndReturn(fake_share)
self.mox.ReplayAll()
self.driver.schedule_create_share(self.admin_context,
fake_request_spec, {})
fake_share = {
'id': share_id,
'availability_zone': 'fake:fake',
'size': 1,
}
fake_request_spec = {
'share_id': share_id,
'share_properties': fake_share,
}
with mock.patch.object(db, 'service_get_by_args',
mock.Mock(return_value='fake_service')):
with mock.patch.object(driver, 'share_update_db',
mock.Mock(return_value=fake_share)):
self.driver.schedule_create_share(self.admin_context,
fake_request_spec, {})
utils.service_is_up.assert_called_once_with('fake_service')
db.service_get_by_args.assert_called_once_with(
utils.IsAMatcher(context.RequestContext),
'fake', 'manila-share')
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):
share_id = 'fake'
fake_share = {'id': share_id,
'availability_zone': 'fake:fake',
'size': 1}
fake_request_spec = {'share_id': share_id,
'share_properties': fake_share}
self.mox.StubOutWithMock(utils, 'service_is_up')
self.mox.StubOutWithMock(db, 'service_get_by_args')
db.service_get_by_args(IsA(context.RequestContext), 'fake',
'manila-share').AndReturn('fake_service')
utils.service_is_up('fake_service').AndReturn(False)
self.mox.ReplayAll()
self.assertRaises(exception.WillNotSchedule,
self.driver.schedule_create_share,
self.admin_context, fake_request_spec, {})
fake_share = {
'id': share_id,
'availability_zone': 'fake:fake',
'size': 1,
}
fake_request_spec = {
'share_id': share_id,
'share_properties': fake_share,
}
with mock.patch.object(db, 'service_get_by_args',
mock.Mock(return_value='fake_service')):
self.assertRaises(exception.WillNotSchedule,
self.driver.schedule_create_share,
self.admin_context, fake_request_spec, {})
utils.service_is_up.assert_called_once_with('fake_service')
db.service_get_by_args.assert_called_once_with(
utils.IsAMatcher(context.RequestContext),
'fake', 'manila-share')

View File

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