~ubuntu-branches/ubuntu/quantal/nova/quantal-proposed

« back to all changes in this revision

Viewing changes to nova/tests/scheduler_unittest.py

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2011-01-21 11:48:06 UTC
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: james.westby@ubuntu.com-20110121114806-v8fvnnl6az4m4ohv
Tags: upstream-2011.1~bzr597
ImportĀ upstreamĀ versionĀ 2011.1~bzr597

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
 
 
3
 
# Copyright 2010 United States Government as represented by the
4
 
# Administrator of the National Aeronautics and Space Administration.
5
 
# All Rights Reserved.
6
 
#
7
 
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
8
 
#    not use this file except in compliance with the License. You may obtain
9
 
#    a copy of the License at
10
 
#
11
 
#         http://www.apache.org/licenses/LICENSE-2.0
12
 
#
13
 
#    Unless required by applicable law or agreed to in writing, software
14
 
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15
 
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16
 
#    License for the specific language governing permissions and limitations
17
 
#    under the License.
18
 
"""
19
 
Tests For Scheduler
20
 
"""
21
 
 
22
 
from nova import context
23
 
from nova import db
24
 
from nova import flags
25
 
from nova import service
26
 
from nova import test
27
 
from nova import rpc
28
 
from nova import utils
29
 
from nova.auth import manager as auth_manager
30
 
from nova.scheduler import manager
31
 
from nova.scheduler import driver
32
 
 
33
 
 
34
 
FLAGS = flags.FLAGS
35
 
flags.DECLARE('max_cores', 'nova.scheduler.simple')
36
 
 
37
 
 
38
 
class TestDriver(driver.Scheduler):
39
 
    """Scheduler Driver for Tests"""
40
 
    def schedule(context, topic, *args, **kwargs):
41
 
        return 'fallback_host'
42
 
 
43
 
    def schedule_named_method(context, topic, num):
44
 
        return 'named_host'
45
 
 
46
 
 
47
 
class SchedulerTestCase(test.TrialTestCase):
48
 
    """Test case for scheduler"""
49
 
    def setUp(self):
50
 
        super(SchedulerTestCase, self).setUp()
51
 
        self.flags(scheduler_driver='nova.tests.scheduler_unittest.TestDriver')
52
 
 
53
 
    def test_fallback(self):
54
 
        scheduler = manager.SchedulerManager()
55
 
        self.mox.StubOutWithMock(rpc, 'cast', use_mock_anything=True)
56
 
        ctxt = context.get_admin_context()
57
 
        rpc.cast(ctxt,
58
 
                 'topic.fallback_host',
59
 
                 {'method': 'noexist',
60
 
                  'args': {'num': 7}})
61
 
        self.mox.ReplayAll()
62
 
        scheduler.noexist(ctxt, 'topic', num=7)
63
 
 
64
 
    def test_named_method(self):
65
 
        scheduler = manager.SchedulerManager()
66
 
        self.mox.StubOutWithMock(rpc, 'cast', use_mock_anything=True)
67
 
        ctxt = context.get_admin_context()
68
 
        rpc.cast(ctxt,
69
 
                 'topic.named_host',
70
 
                 {'method': 'named_method',
71
 
                  'args': {'num': 7}})
72
 
        self.mox.ReplayAll()
73
 
        scheduler.named_method(ctxt, 'topic', num=7)
74
 
 
75
 
 
76
 
class SimpleDriverTestCase(test.TrialTestCase):
77
 
    """Test case for simple driver"""
78
 
    def setUp(self):
79
 
        super(SimpleDriverTestCase, self).setUp()
80
 
        self.flags(connection_type='fake',
81
 
                   max_cores=4,
82
 
                   max_gigabytes=4,
83
 
                   network_manager='nova.network.manager.FlatManager',
84
 
                   volume_driver='nova.volume.driver.FakeISCSIDriver',
85
 
                   scheduler_driver='nova.scheduler.simple.SimpleScheduler')
86
 
        self.scheduler = manager.SchedulerManager()
87
 
        self.manager = auth_manager.AuthManager()
88
 
        self.user = self.manager.create_user('fake', 'fake', 'fake')
89
 
        self.project = self.manager.create_project('fake', 'fake', 'fake')
90
 
        self.context = context.get_admin_context()
91
 
 
92
 
    def tearDown(self):
93
 
        self.manager.delete_user(self.user)
94
 
        self.manager.delete_project(self.project)
95
 
 
96
 
    def _create_instance(self):
97
 
        """Create a test instance"""
98
 
        inst = {}
99
 
        inst['image_id'] = 'ami-test'
100
 
        inst['reservation_id'] = 'r-fakeres'
101
 
        inst['user_id'] = self.user.id
102
 
        inst['project_id'] = self.project.id
103
 
        inst['instance_type'] = 'm1.tiny'
104
 
        inst['mac_address'] = utils.generate_mac()
105
 
        inst['ami_launch_index'] = 0
106
 
        inst['vcpus'] = 1
107
 
        return db.instance_create(self.context, inst)['id']
108
 
 
109
 
    def _create_volume(self):
110
 
        """Create a test volume"""
111
 
        vol = {}
112
 
        vol['image_id'] = 'ami-test'
113
 
        vol['reservation_id'] = 'r-fakeres'
114
 
        vol['size'] = 1
115
 
        return db.volume_create(self.context, vol)['id']
116
 
 
117
 
    def test_hosts_are_up(self):
118
 
        """Ensures driver can find the hosts that are up"""
119
 
        # NOTE(vish): constructing service without create method
120
 
        #             because we are going to use it without queue
121
 
        compute1 = service.Service('host1',
122
 
                                   'nova-compute',
123
 
                                   'compute',
124
 
                                   FLAGS.compute_manager)
125
 
        compute1.startService()
126
 
        compute2 = service.Service('host2',
127
 
                                   'nova-compute',
128
 
                                   'compute',
129
 
                                   FLAGS.compute_manager)
130
 
        compute2.startService()
131
 
        hosts = self.scheduler.driver.hosts_up(self.context, 'compute')
132
 
        self.assertEqual(len(hosts), 2)
133
 
        compute1.kill()
134
 
        compute2.kill()
135
 
 
136
 
    def test_least_busy_host_gets_instance(self):
137
 
        """Ensures the host with less cores gets the next one"""
138
 
        compute1 = service.Service('host1',
139
 
                                   'nova-compute',
140
 
                                   'compute',
141
 
                                   FLAGS.compute_manager)
142
 
        compute1.startService()
143
 
        compute2 = service.Service('host2',
144
 
                                   'nova-compute',
145
 
                                   'compute',
146
 
                                   FLAGS.compute_manager)
147
 
        compute2.startService()
148
 
        instance_id1 = self._create_instance()
149
 
        compute1.run_instance(self.context, instance_id1)
150
 
        instance_id2 = self._create_instance()
151
 
        host = self.scheduler.driver.schedule_run_instance(self.context,
152
 
                                                           instance_id2)
153
 
        self.assertEqual(host, 'host2')
154
 
        compute1.terminate_instance(self.context, instance_id1)
155
 
        db.instance_destroy(self.context, instance_id2)
156
 
        compute1.kill()
157
 
        compute2.kill()
158
 
 
159
 
    def test_too_many_cores(self):
160
 
        """Ensures we don't go over max cores"""
161
 
        compute1 = service.Service('host1',
162
 
                                   'nova-compute',
163
 
                                   'compute',
164
 
                                   FLAGS.compute_manager)
165
 
        compute1.startService()
166
 
        compute2 = service.Service('host2',
167
 
                                   'nova-compute',
168
 
                                   'compute',
169
 
                                   FLAGS.compute_manager)
170
 
        compute2.startService()
171
 
        instance_ids1 = []
172
 
        instance_ids2 = []
173
 
        for index in xrange(FLAGS.max_cores):
174
 
            instance_id = self._create_instance()
175
 
            compute1.run_instance(self.context, instance_id)
176
 
            instance_ids1.append(instance_id)
177
 
            instance_id = self._create_instance()
178
 
            compute2.run_instance(self.context, instance_id)
179
 
            instance_ids2.append(instance_id)
180
 
        instance_id = self._create_instance()
181
 
        self.assertRaises(driver.NoValidHost,
182
 
                          self.scheduler.driver.schedule_run_instance,
183
 
                          self.context,
184
 
                          instance_id)
185
 
        for instance_id in instance_ids1:
186
 
            compute1.terminate_instance(self.context, instance_id)
187
 
        for instance_id in instance_ids2:
188
 
            compute2.terminate_instance(self.context, instance_id)
189
 
        compute1.kill()
190
 
        compute2.kill()
191
 
 
192
 
    def test_least_busy_host_gets_volume(self):
193
 
        """Ensures the host with less gigabytes gets the next one"""
194
 
        volume1 = service.Service('host1',
195
 
                                   'nova-volume',
196
 
                                   'volume',
197
 
                                   FLAGS.volume_manager)
198
 
        volume1.startService()
199
 
        volume2 = service.Service('host2',
200
 
                                   'nova-volume',
201
 
                                   'volume',
202
 
                                   FLAGS.volume_manager)
203
 
        volume2.startService()
204
 
        volume_id1 = self._create_volume()
205
 
        volume1.create_volume(self.context, volume_id1)
206
 
        volume_id2 = self._create_volume()
207
 
        host = self.scheduler.driver.schedule_create_volume(self.context,
208
 
                                                            volume_id2)
209
 
        self.assertEqual(host, 'host2')
210
 
        volume1.delete_volume(self.context, volume_id1)
211
 
        db.volume_destroy(self.context, volume_id2)
212
 
        volume1.kill()
213
 
        volume2.kill()
214
 
 
215
 
    def test_too_many_gigabytes(self):
216
 
        """Ensures we don't go over max gigabytes"""
217
 
        volume1 = service.Service('host1',
218
 
                                   'nova-volume',
219
 
                                   'volume',
220
 
                                   FLAGS.volume_manager)
221
 
        volume1.startService()
222
 
        volume2 = service.Service('host2',
223
 
                                   'nova-volume',
224
 
                                   'volume',
225
 
                                   FLAGS.volume_manager)
226
 
        volume2.startService()
227
 
        volume_ids1 = []
228
 
        volume_ids2 = []
229
 
        for index in xrange(FLAGS.max_gigabytes):
230
 
            volume_id = self._create_volume()
231
 
            volume1.create_volume(self.context, volume_id)
232
 
            volume_ids1.append(volume_id)
233
 
            volume_id = self._create_volume()
234
 
            volume2.create_volume(self.context, volume_id)
235
 
            volume_ids2.append(volume_id)
236
 
        volume_id = self._create_volume()
237
 
        self.assertRaises(driver.NoValidHost,
238
 
                          self.scheduler.driver.schedule_create_volume,
239
 
                          self.context,
240
 
                          volume_id)
241
 
        for volume_id in volume_ids1:
242
 
            volume1.delete_volume(self.context, volume_id)
243
 
        for volume_id in volume_ids2:
244
 
            volume2.delete_volume(self.context, volume_id)
245
 
        volume1.kill()
246
 
        volume2.kill()