Refactor test_scheduler into unit tests

Also adds a service_is_up test for utils/service_is_up, which used to be
in the scheduler.

Minor bug spotted when doing tests, also, with live migration.  Was
checking 'launched_on' host when should be checking current host.

Change-Id: I964abb767d619afbd90da549b267d8a9d7c31fc1
This commit is contained in:
Chris Behrens
2012-01-20 20:19:15 -08:00
parent ab180bc47c
commit 895f692a33
2 changed files with 170 additions and 72 deletions

View File

@@ -0,0 +1,107 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2010 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# Copyright 2011 OpenStack LLC
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""
Tests For Multi Scheduler
"""
from nova.scheduler import driver
from nova.scheduler import multi
from nova.tests.scheduler import test_scheduler
class FakeComputeScheduler(driver.Scheduler):
is_fake_compute = True
def schedule_run_instance(self, *args, **kwargs):
pass
def schedule_start_instance(self, *args, **kwargs):
pass
def schedule(self, *args, **kwargs):
pass
class FakeVolumeScheduler(driver.Scheduler):
is_fake_volume = True
def schedule_create_volume(self, *args, **kwargs):
pass
def schedule_create_volumes(self, *args, **kwargs):
pass
def schedule(self, *args, **kwargs):
pass
class MultiDriverTestCase(test_scheduler.SchedulerTestCase):
"""Test case for multi driver"""
driver_cls = multi.MultiScheduler
def setUp(self):
super(MultiDriverTestCase, self).setUp()
base_name = 'nova.tests.scheduler.test_multi_scheduler.%s'
compute_cls_name = base_name % 'FakeComputeScheduler'
volume_cls_name = base_name % 'FakeVolumeScheduler'
self.flags(compute_scheduler_driver=compute_cls_name,
volume_scheduler_driver=volume_cls_name)
self._manager = multi.MultiScheduler()
def test_drivers_inited(self):
mgr = self._manager
self.assertEqual(len(mgr.drivers), 2)
self.assertTrue(mgr.drivers['compute'].is_fake_compute)
self.assertTrue(mgr.drivers['volume'].is_fake_volume)
def test_proxy_calls(self):
mgr = self._manager
compute_driver = mgr.drivers['compute']
volume_driver = mgr.drivers['volume']
test_methods = {compute_driver: ['run_instance', 'start_instance'],
volume_driver: ['create_volume', 'create_volumes']}
for driver, methods in test_methods.iteritems():
for method in methods:
mgr_func = getattr(mgr, 'schedule_' + method)
driver_func = getattr(driver, 'schedule_' + method)
self.assertEqual(mgr_func, driver_func)
def test_schedule_fallback_proxy(self):
mgr = self._manager
self.mox.StubOutWithMock(mgr.drivers['compute'], 'schedule')
self.mox.StubOutWithMock(mgr.drivers['volume'], 'schedule')
ctxt = 'fake_context'
method = 'fake_method'
fake_args = (1, 2, 3)
fake_kwargs = {'fake_kwarg1': 'fake_value1',
'fake_kwarg2': 'fake_value2'}
mgr.drivers['compute'].schedule(ctxt, 'compute', method,
*fake_args, **fake_kwargs)
mgr.drivers['volume'].schedule(ctxt, 'volume', method,
*fake_args, **fake_kwargs)
self.mox.ReplayAll()
mgr.schedule(ctxt, 'compute', method, *fake_args, **fake_kwargs)
mgr.schedule(ctxt, 'volume', method, *fake_args, **fake_kwargs)

View File

@@ -13,18 +13,18 @@
# License for the specific language governing permissions and limitations
# under the License.
import nova
from nova import context
from nova import db
from nova import exception
from nova import flags
from nova import log as logging
from nova import rpc
from nova.scheduler import vsa as vsa_sched
from nova import test
from nova.tests.scheduler import test_scheduler
from nova import utils
from nova.volume import volume_types
from nova.scheduler import vsa as vsa_sched
FLAGS = flags.FLAGS
LOG = logging.getLogger('nova.tests.scheduler.vsa')
@@ -50,7 +50,32 @@ class FakeVsaMostAvailCapacityScheduler(
pass
class VsaSchedulerTestCase(test.TestCase):
class VsaSchedulerTestCase(test_scheduler.SchedulerTestCase):
driver_cls = FakeVsaLeastUsedScheduler
def setUp(self):
super(VsaSchedulerTestCase, self).setUp()
self.host_num = 10
self.drive_type_num = 5
self.stubs.Set(rpc, 'cast', fake_rpc_cast)
self.stubs.Set(self.driver,
'_get_service_states', self._fake_get_service_states)
self.stubs.Set(self.driver,
'_provision_volume', self._fake_provision_volume)
self.stubs.Set(db, 'vsa_update', self._fake_vsa_update)
self.stubs.Set(db, 'volume_get', self._fake_volume_get)
self.stubs.Set(db, 'volume_update', self._fake_volume_update)
self.created_types_lst = []
def tearDown(self):
for name in self.created_types_lst:
volume_types.purge(self.context.elevated(), name)
super(VsaSchedulerTestCase, self).tearDown()
def _get_vol_creation_request(self, num_vols, drive_ix, size=0):
volume_params = []
@@ -58,7 +83,7 @@ class VsaSchedulerTestCase(test.TestCase):
name = 'name_' + str(i)
try:
volume_types.create(self.context, name,
volume_types.create(self.context.elevated(), name,
extra_specs={'type': 'vsa_drive',
'drive_name': name,
'drive_type': 'type_' + str(drive_ix),
@@ -205,35 +230,6 @@ class VsaSchedulerTestCase(test.TestCase):
def _fake_service_is_up_False(self, service):
return False
def setUp(self, sched_class=None):
super(VsaSchedulerTestCase, self).setUp()
self.context = context.get_admin_context()
if sched_class is None:
self.sched = FakeVsaLeastUsedScheduler()
else:
self.sched = sched_class
self.host_num = 10
self.drive_type_num = 5
self.stubs.Set(rpc, 'cast', fake_rpc_cast)
self.stubs.Set(self.sched,
'_get_service_states', self._fake_get_service_states)
self.stubs.Set(self.sched,
'_provision_volume', self._fake_provision_volume)
self.stubs.Set(nova.db, 'vsa_update', self._fake_vsa_update)
self.stubs.Set(nova.db, 'volume_get', self._fake_volume_get)
self.stubs.Set(nova.db, 'volume_update', self._fake_volume_update)
self.created_types_lst = []
def tearDown(self):
for name in self.created_types_lst:
volume_types.purge(self.context, name)
super(VsaSchedulerTestCase, self).tearDown()
def test_vsa_sched_create_volumes_simple(self):
global scheduled_volumes
scheduled_volumes = []
@@ -245,7 +241,7 @@ class VsaSchedulerTestCase(test.TestCase):
prev = self._generate_default_service_states()
request_spec = self._get_vol_creation_request(num_vols=3, drive_ix=2)
self.sched.schedule_create_volumes(self.context,
self.driver.schedule_create_volumes(self.context,
request_spec,
availability_zone=None)
@@ -271,7 +267,7 @@ class VsaSchedulerTestCase(test.TestCase):
init_num_drives=1)
request_spec = self._get_vol_creation_request(num_vols=1, drive_ix=6)
self.assertRaises(exception.NoValidHost,
self.sched.schedule_create_volumes,
self.driver.schedule_create_volumes,
self.context,
request_spec,
availability_zone=None)
@@ -288,7 +284,7 @@ class VsaSchedulerTestCase(test.TestCase):
request_spec = self._get_vol_creation_request(num_vols=3, drive_ix=0)
self.assertRaises(exception.NoValidHost,
self.sched.schedule_create_volumes,
self.driver.schedule_create_volumes,
self.context,
request_spec,
availability_zone=None)
@@ -311,7 +307,7 @@ class VsaSchedulerTestCase(test.TestCase):
request_spec = self._get_vol_creation_request(num_vols=1, drive_ix=0)
self.assertRaises(exception.NoValidHost,
self.sched.schedule_create_volumes,
self.driver.schedule_create_volumes,
self.context,
request_spec,
availability_zone=None)
@@ -326,13 +322,13 @@ class VsaSchedulerTestCase(test.TestCase):
request_spec = self._get_vol_creation_request(num_vols=1, drive_ix=0)
self.stubs.UnsetAll()
self.stubs.Set(self.sched,
self.stubs.Set(self.driver,
'_get_service_states', self._fake_get_service_states)
self.stubs.Set(nova.db, 'volume_create', self._fake_volume_create)
self.stubs.Set(nova.db, 'volume_update', self._fake_volume_update)
self.stubs.Set(db, 'volume_create', self._fake_volume_create)
self.stubs.Set(db, 'volume_update', self._fake_volume_update)
self.stubs.Set(rpc, 'cast', fake_rpc_cast)
self.sched.schedule_create_volumes(self.context,
self.driver.schedule_create_volumes(self.context,
request_spec,
availability_zone=None)
@@ -346,7 +342,7 @@ class VsaSchedulerTestCase(test.TestCase):
init_num_drives=1)
request_spec = self._get_vol_creation_request(num_vols=1, drive_ix=0)
self.sched.schedule_create_volumes(self.context,
self.driver.schedule_create_volumes(self.context,
request_spec,
availability_zone=None)
@@ -356,13 +352,13 @@ class VsaSchedulerTestCase(test.TestCase):
new_request = self._get_vol_creation_request(num_vols=1, drive_ix=0)
self.sched.schedule_create_volumes(self.context,
self.driver.schedule_create_volumes(self.context,
request_spec,
availability_zone=None)
self._print_service_states()
self.assertRaises(exception.NoValidHost,
self.sched.schedule_create_volumes,
self.driver.schedule_create_volumes,
self.context,
new_request,
availability_zone=None)
@@ -379,26 +375,26 @@ class VsaSchedulerTestCase(test.TestCase):
request_spec = self._get_vol_creation_request(num_vols=3, drive_ix=2)
self.assertRaises(exception.HostBinaryNotFound,
self.sched.schedule_create_volumes,
self.context,
self.driver.schedule_create_volumes,
self.context.elevated(),
request_spec,
availability_zone="nova:host_5")
self.stubs.Set(nova.db,
self.stubs.Set(db,
'service_get_by_args', self._fake_service_get_by_args)
self.stubs.Set(utils,
'service_is_up', self._fake_service_is_up_False)
self.assertRaises(exception.WillNotSchedule,
self.sched.schedule_create_volumes,
self.context,
self.driver.schedule_create_volumes,
self.context.elevated(),
request_spec,
availability_zone="nova:host_5")
self.stubs.Set(utils,
'service_is_up', self._fake_service_is_up_True)
self.sched.schedule_create_volumes(self.context,
self.driver.schedule_create_volumes(self.context.elevated(),
request_spec,
availability_zone="nova:host_5")
@@ -419,7 +415,7 @@ class VsaSchedulerTestCase(test.TestCase):
request_spec = self._get_vol_creation_request(num_vols=3,
drive_ix=3,
size=50)
self.sched.schedule_create_volumes(self.context,
self.driver.schedule_create_volumes(self.context,
request_spec,
availability_zone=None)
@@ -459,13 +455,13 @@ class VsaSchedulerTestCase(test.TestCase):
LOG.debug(_("Test: Volume get: id=%(volume_id)s"), locals())
return {'id': volume_id, 'availability_zone': 'nova:host_3'}
self.stubs.Set(nova.db, 'volume_get', _fake_volume_get_az)
self.stubs.Set(nova.db,
'service_get_by_args', self._fake_service_get_by_args)
self.stubs.Set(db, 'volume_get', _fake_volume_get_az)
self.stubs.Set(db, 'service_get_by_args',
self._fake_service_get_by_args)
self.stubs.Set(utils,
'service_is_up', self._fake_service_is_up_True)
self.sched.schedule_create_volume(self.context,
self.driver.schedule_create_volume(self.context.elevated(),
123, availability_zone=None)
self.assertEqual(scheduled_volume['id'], 123)
@@ -480,10 +476,10 @@ class VsaSchedulerTestCase(test.TestCase):
global_volume['volume_type_id'] = None
self.assertRaises(exception.NoValidHost,
self.sched.schedule_create_volume,
self.context,
123,
availability_zone=None)
self.driver.schedule_create_volume,
self.context,
123,
availability_zone=None)
def test_vsa_sched_create_single_volume(self):
global scheduled_volume
@@ -500,7 +496,7 @@ class VsaSchedulerTestCase(test.TestCase):
drive_ix = 2
name = 'name_' + str(drive_ix)
volume_types.create(self.context, name,
volume_types.create(self.context.elevated(), name,
extra_specs={'type': 'vsa_drive',
'drive_name': name,
'drive_type': 'type_' + str(drive_ix),
@@ -511,7 +507,7 @@ class VsaSchedulerTestCase(test.TestCase):
global_volume['volume_type_id'] = volume_type['id']
global_volume['size'] = 0
self.sched.schedule_create_volume(self.context,
self.driver.schedule_create_volume(self.context,
123, availability_zone=None)
self.assertEqual(scheduled_volume['id'], 123)
@@ -520,12 +516,7 @@ class VsaSchedulerTestCase(test.TestCase):
class VsaSchedulerTestCaseMostAvail(VsaSchedulerTestCase):
def setUp(self):
super(VsaSchedulerTestCaseMostAvail, self).setUp(
FakeVsaMostAvailCapacityScheduler())
def tearDown(self):
super(VsaSchedulerTestCaseMostAvail, self).tearDown()
driver_cls = FakeVsaMostAvailCapacityScheduler
def test_vsa_sched_create_single_volume(self):
global scheduled_volume
@@ -542,7 +533,7 @@ class VsaSchedulerTestCaseMostAvail(VsaSchedulerTestCase):
drive_ix = 2
name = 'name_' + str(drive_ix)
volume_types.create(self.context, name,
volume_types.create(self.context.elevated(), name,
extra_specs={'type': 'vsa_drive',
'drive_name': name,
'drive_type': 'type_' + str(drive_ix),
@@ -553,7 +544,7 @@ class VsaSchedulerTestCaseMostAvail(VsaSchedulerTestCase):
global_volume['volume_type_id'] = volume_type['id']
global_volume['size'] = 0
self.sched.schedule_create_volume(self.context,
self.driver.schedule_create_volume(self.context,
123, availability_zone=None)
self.assertEqual(scheduled_volume['id'], 123)
@@ -572,7 +563,7 @@ class VsaSchedulerTestCaseMostAvail(VsaSchedulerTestCase):
self._print_service_states()
self.sched.schedule_create_volumes(self.context,
self.driver.schedule_create_volumes(self.context,
request_spec,
availability_zone=None)
@@ -603,7 +594,7 @@ class VsaSchedulerTestCaseMostAvail(VsaSchedulerTestCase):
request_spec = self._get_vol_creation_request(num_vols=3,
drive_ix=3,
size=50)
self.sched.schedule_create_volumes(self.context,
self.driver.schedule_create_volumes(self.context,
request_spec,
availability_zone=None)