From 72fa94f72b361a6c097eaf071fe7f26b2ba4e924 Mon Sep 17 00:00:00 2001 From: "Kevin L. Mitchell" Date: Thu, 17 Nov 2011 13:47:56 -0600 Subject: Implement schedule_prep_resize() Implement schedule_prep_resize() in the distributed scheduler. Adds a request_spec argument to enable the current host of an instance to be excluded for resizes. Corrects bug 888236. Change-Id: Ia52415e79639275a06bef59f1e13ca64bf7243ee --- nova/tests/scheduler/test_chance_scheduler.py | 50 +++++++++++++ nova/tests/scheduler/test_distributed_scheduler.py | 85 ++++++++++++++++++++-- nova/tests/test_compute.py | 37 ++++++++++ 3 files changed, 164 insertions(+), 8 deletions(-) create mode 100644 nova/tests/scheduler/test_chance_scheduler.py (limited to 'nova/tests') diff --git a/nova/tests/scheduler/test_chance_scheduler.py b/nova/tests/scheduler/test_chance_scheduler.py new file mode 100644 index 000000000..8d4c73adb --- /dev/null +++ b/nova/tests/scheduler/test_chance_scheduler.py @@ -0,0 +1,50 @@ +# 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 Chance Scheduler. +""" + +from nova import test +from nova.scheduler import chance + + +class ChanceSchedulerTestCase(test.TestCase): + """Test case for Chance Scheduler.""" + + def test_filter_hosts_avoid(self): + """Test to make sure _filter_hosts() filters original hosts if + avoid_original_host is True.""" + + sched = chance.ChanceScheduler() + + hosts = ['host1', 'host2', 'host3'] + request_spec = dict(original_host='host2', + avoid_original_host=True) + + filtered = sched._filter_hosts(request_spec, hosts) + self.assertEqual(filtered, ['host1', 'host3']) + + def test_filter_hosts_no_avoid(self): + """Test to make sure _filter_hosts() does not filter original + hosts if avoid_original_host is False.""" + + sched = chance.ChanceScheduler() + + hosts = ['host1', 'host2', 'host3'] + request_spec = dict(original_host='host2', + avoid_original_host=False) + + filtered = sched._filter_hosts(request_spec, hosts) + self.assertEqual(filtered, hosts) diff --git a/nova/tests/scheduler/test_distributed_scheduler.py b/nova/tests/scheduler/test_distributed_scheduler.py index ad2d1b1b6..80781c049 100644 --- a/nova/tests/scheduler/test_distributed_scheduler.py +++ b/nova/tests/scheduler/test_distributed_scheduler.py @@ -82,6 +82,10 @@ def fake_zone_get_all(context): ] +def fake_filter_hosts(topic, request_info, unfiltered_hosts, options): + return unfiltered_hosts + + class DistributedSchedulerTestCase(test.TestCase): """Test case for Distributed Scheduler.""" @@ -205,16 +209,11 @@ class DistributedSchedulerTestCase(test.TestCase): "compute", {}) def test_schedule_happy_day(self): - """_schedule() has no branching logic beyond basic input parameter - checking. Just make sure there's nothing glaringly wrong by doing - a happy day pass through.""" + """Make sure there's nothing glaringly wrong with _schedule() + by doing a happy day pass through.""" self.next_weight = 1.0 - def _fake_filter_hosts(topic, request_info, unfiltered_hosts, - options): - return unfiltered_hosts - def _fake_weighted_sum(functions, hosts, options): self.next_weight += 2.0 host, hostinfo = hosts[0] @@ -224,7 +223,7 @@ class DistributedSchedulerTestCase(test.TestCase): sched = ds_fakes.FakeDistributedScheduler() fake_context = context.RequestContext('user', 'project') sched.zone_manager = ds_fakes.FakeZoneManager() - self.stubs.Set(sched, '_filter_hosts', _fake_filter_hosts) + self.stubs.Set(sched, '_filter_hosts', fake_filter_hosts) self.stubs.Set(least_cost, 'weighted_sum', _fake_weighted_sum) self.stubs.Set(nova.db, 'zone_get_all', fake_zone_get_all) self.stubs.Set(sched, '_call_zone_method', fake_call_zone_method) @@ -243,6 +242,37 @@ class DistributedSchedulerTestCase(test.TestCase): self.assertTrue(weighted_host.host != None) self.assertTrue(weighted_host.zone == None) + def test_schedule_local_zone(self): + """Test to make sure _schedule makes no call out to zones if + local_zone in the request spec is True.""" + + self.next_weight = 1.0 + + def _fake_weighted_sum(functions, hosts, options): + self.next_weight += 2.0 + host, hostinfo = hosts[0] + return least_cost.WeightedHost(self.next_weight, host=host, + hostinfo=hostinfo) + + sched = ds_fakes.FakeDistributedScheduler() + fake_context = context.RequestContext('user', 'project') + sched.zone_manager = ds_fakes.FakeZoneManager() + self.stubs.Set(sched, '_filter_hosts', fake_filter_hosts) + self.stubs.Set(least_cost, 'weighted_sum', _fake_weighted_sum) + self.stubs.Set(nova.db, 'zone_get_all', fake_zone_get_all) + self.stubs.Set(sched, '_call_zone_method', fake_call_zone_method) + + instance_type = dict(memory_mb=512, local_gb=512) + request_spec = dict(num_instances=10, instance_type=instance_type, + local_zone=True) + weighted_hosts = sched._schedule(fake_context, 'compute', + request_spec) + self.assertEquals(len(weighted_hosts), 10) + for weighted_host in weighted_hosts: + # There should be no remote hosts + self.assertTrue(weighted_host.host != None) + self.assertTrue(weighted_host.zone == None) + def test_decrypt_blob(self): """Test that the decrypt method works.""" @@ -269,3 +299,42 @@ class DistributedSchedulerTestCase(test.TestCase): self.assertEquals(weight, 1.0) hostinfo = zone_manager.HostInfo('host', free_ram_mb=1000) self.assertEquals(1000, fn(hostinfo)) + + def test_filter_hosts_avoid(self): + """Test to make sure _filter_hosts() filters original hosts if + avoid_original_host is True.""" + + def _fake_choose_host_filters(): + return [] + + sched = ds_fakes.FakeDistributedScheduler() + fake_context = context.RequestContext('user', 'project') + self.stubs.Set(sched, '_choose_host_filters', + _fake_choose_host_filters) + + hosts = [('host1', '1info'), ('host2', '2info'), ('host3', '3info')] + request_spec = dict(original_host='host2', + avoid_original_host=True) + + filtered = sched._filter_hosts('compute', request_spec, hosts, {}) + self.assertEqual(filtered, + [('host1', '1info'), ('host3', '3info')]) + + def test_filter_hosts_no_avoid(self): + """Test to make sure _filter_hosts() does not filter original + hosts if avoid_original_host is False.""" + + def _fake_choose_host_filters(): + return [] + + sched = ds_fakes.FakeDistributedScheduler() + fake_context = context.RequestContext('user', 'project') + self.stubs.Set(sched, '_choose_host_filters', + _fake_choose_host_filters) + + hosts = [('host1', '1info'), ('host2', '2info'), ('host3', '3info')] + request_spec = dict(original_host='host2', + avoid_original_host=False) + + filtered = sched._filter_hosts('compute', request_spec, hosts, {}) + self.assertEqual(filtered, hosts) diff --git a/nova/tests/test_compute.py b/nova/tests/test_compute.py index 397b5e268..358d43399 100644 --- a/nova/tests/test_compute.py +++ b/nova/tests/test_compute.py @@ -1443,6 +1443,43 @@ class ComputeAPITestCase(BaseTestCase): self.compute_api.resize(context, instance, None) self.compute.terminate_instance(context, instance_id) + def test_resize_request_spec(self): + def _fake_cast(context, args): + request_spec = args['args']['request_spec'] + self.assertEqual(request_spec['original_host'], 'host2') + self.assertEqual(request_spec['avoid_original_host'], True) + + self.stubs.Set(self.compute_api, '_cast_scheduler_message', + _fake_cast) + + context = self.context.elevated() + instance_id = self._create_instance(dict(host='host2')) + instance = db.instance_get(context, instance_id) + self.compute.run_instance(self.context, instance_id) + try: + self.compute_api.resize(context, instance, None) + finally: + self.compute.terminate_instance(context, instance_id) + + def test_resize_request_spec_noavoid(self): + def _fake_cast(context, args): + request_spec = args['args']['request_spec'] + self.assertEqual(request_spec['original_host'], 'host2') + self.assertEqual(request_spec['avoid_original_host'], False) + + self.stubs.Set(self.compute_api, '_cast_scheduler_message', + _fake_cast) + self.flags(allow_resize_to_same_host=True) + + context = self.context.elevated() + instance_id = self._create_instance(dict(host='host2')) + instance = db.instance_get(context, instance_id) + self.compute.run_instance(self.context, instance_id) + try: + self.compute_api.resize(context, instance, None) + finally: + self.compute.terminate_instance(context, instance_id) + def test_get_all_by_name_regexp(self): """Test searching instances by name (display_name)""" c = context.get_admin_context() -- cgit