1
# Copyright 2011 OpenStack LLC.
4
# Licensed under the Apache License, Version 2.0 (the "License"); you may
5
# not use this file except in compliance with the License. You may obtain
6
# a copy of the License at
8
# http://www.apache.org/licenses/LICENSE-2.0
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
# License for the specific language governing permissions and limitations
16
Tests For Filter Scheduler.
21
from cinder import context
22
from cinder import exception
23
from cinder.openstack.common.scheduler import weights
24
from cinder.scheduler import driver
25
from cinder.scheduler import filter_scheduler
26
from cinder.scheduler import host_manager
27
from cinder.tests.scheduler import fakes
28
from cinder.tests.scheduler import test_scheduler
31
def fake_get_filtered_hosts(hosts, filter_properties):
35
class FilterSchedulerTestCase(test_scheduler.SchedulerTestCase):
36
"""Test case for Filter Scheduler."""
38
driver_cls = filter_scheduler.FilterScheduler
40
def test_create_volume_no_hosts(self):
42
Ensure empty hosts & child_zones result in NoValidHosts exception.
44
def _fake_empty_call_zone_method(*args, **kwargs):
47
sched = fakes.FakeFilterScheduler()
49
fake_context = context.RequestContext('user', 'project')
50
request_spec = {'volume_properties': {'project_id': 1,
52
'volume_type': {'name': 'LVM_iSCSI'},
53
'volume_id': ['fake-id1']}
54
self.assertRaises(exception.NoValidHost, sched.schedule_create_volume,
55
fake_context, request_spec, None)
57
def test_create_volume_non_admin(self):
58
"""Test creating an instance locally using run_instance, passing
59
a non-admin context. DB actions should work."""
60
self.was_admin = False
62
def fake_get(context, *args, **kwargs):
63
# make sure this is called with admin context, even though
64
# we're using user context below
65
self.was_admin = context.is_admin
68
sched = fakes.FakeFilterScheduler()
69
self.stubs.Set(sched.host_manager, 'get_all_host_states', fake_get)
71
fake_context = context.RequestContext('user', 'project')
73
request_spec = {'volume_properties': {'project_id': 1,
75
'volume_type': {'name': 'LVM_iSCSI'},
76
'volume_id': ['fake-id1']}
77
self.assertRaises(exception.NoValidHost, sched.schedule_create_volume,
78
fake_context, request_spec, None)
79
self.assertTrue(self.was_admin)
81
def test_schedule_happy_day(self):
82
"""Make sure there's nothing glaringly wrong with _schedule()
83
by doing a happy day pass through."""
85
self.next_weight = 1.0
87
def _fake_weigh_objects(_self, functions, hosts, options):
88
self.next_weight += 2.0
90
return [weights.WeighedHost(host_state, self.next_weight)]
92
sched = fakes.FakeFilterScheduler()
93
fake_context = context.RequestContext('user', 'project',
96
self.stubs.Set(sched.host_manager, 'get_filtered_hosts',
97
fake_get_filtered_hosts)
98
self.stubs.Set(weights.HostWeightHandler,
99
'get_weighed_objects', _fake_weigh_objects)
100
fakes.mox_host_manager_db_calls(self.mox, fake_context)
102
request_spec = {'volume_type': {'name': 'LVM_iSCSI'},
103
'volume_properties': {'project_id': 1,
106
weighed_host = sched._schedule(fake_context, request_spec, {})
107
self.assertTrue(weighed_host.obj is not None)