~ubuntu-branches/ubuntu/raring/cinder/raring-updates

« back to all changes in this revision

Viewing changes to cinder/tests/scheduler/test_filter_scheduler.py

Tags: upstream-2013.1~g2
ImportĀ upstreamĀ versionĀ 2013.1~g2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2011 OpenStack LLC.
 
2
# All Rights Reserved.
 
3
#
 
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
 
7
#
 
8
#         http://www.apache.org/licenses/LICENSE-2.0
 
9
#
 
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
 
14
#    under the License.
 
15
"""
 
16
Tests For Filter Scheduler.
 
17
"""
 
18
 
 
19
import mox
 
20
 
 
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
 
29
 
 
30
 
 
31
def fake_get_filtered_hosts(hosts, filter_properties):
 
32
    return list(hosts)
 
33
 
 
34
 
 
35
class FilterSchedulerTestCase(test_scheduler.SchedulerTestCase):
 
36
    """Test case for Filter Scheduler."""
 
37
 
 
38
    driver_cls = filter_scheduler.FilterScheduler
 
39
 
 
40
    def test_create_volume_no_hosts(self):
 
41
        """
 
42
        Ensure empty hosts & child_zones result in NoValidHosts exception.
 
43
        """
 
44
        def _fake_empty_call_zone_method(*args, **kwargs):
 
45
            return []
 
46
 
 
47
        sched = fakes.FakeFilterScheduler()
 
48
 
 
49
        fake_context = context.RequestContext('user', 'project')
 
50
        request_spec = {'volume_properties': {'project_id': 1,
 
51
                                              'size': 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)
 
56
 
 
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
 
61
 
 
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
 
66
            return {}
 
67
 
 
68
        sched = fakes.FakeFilterScheduler()
 
69
        self.stubs.Set(sched.host_manager, 'get_all_host_states', fake_get)
 
70
 
 
71
        fake_context = context.RequestContext('user', 'project')
 
72
 
 
73
        request_spec = {'volume_properties': {'project_id': 1,
 
74
                                              'size': 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)
 
80
 
 
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."""
 
84
 
 
85
        self.next_weight = 1.0
 
86
 
 
87
        def _fake_weigh_objects(_self, functions, hosts, options):
 
88
            self.next_weight += 2.0
 
89
            host_state = hosts[0]
 
90
            return [weights.WeighedHost(host_state, self.next_weight)]
 
91
 
 
92
        sched = fakes.FakeFilterScheduler()
 
93
        fake_context = context.RequestContext('user', 'project',
 
94
                                              is_admin=True)
 
95
 
 
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)
 
101
 
 
102
        request_spec = {'volume_type': {'name': 'LVM_iSCSI'},
 
103
                        'volume_properties': {'project_id': 1,
 
104
                                              'size': 1}}
 
105
        self.mox.ReplayAll()
 
106
        weighed_host = sched._schedule(fake_context, request_spec, {})
 
107
        self.assertTrue(weighed_host.obj is not None)