1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2010 United States Government as represented by the
4
# Administrator of the National Aeronautics and Space Administration.
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
11
# http://www.apache.org/licenses/LICENSE-2.0
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
22
from nova import context
24
from nova import flags
25
from nova import service
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
35
flags.DECLARE('max_cores', 'nova.scheduler.simple')
38
class TestDriver(driver.Scheduler):
39
"""Scheduler Driver for Tests"""
40
def schedule(context, topic, *args, **kwargs):
41
return 'fallback_host'
43
def schedule_named_method(context, topic, num):
47
class SchedulerTestCase(test.TrialTestCase):
48
"""Test case for scheduler"""
50
super(SchedulerTestCase, self).setUp()
51
self.flags(scheduler_driver='nova.tests.scheduler_unittest.TestDriver')
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()
58
'topic.fallback_host',
62
scheduler.noexist(ctxt, 'topic', num=7)
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()
70
{'method': 'named_method',
73
scheduler.named_method(ctxt, 'topic', num=7)
76
class SimpleDriverTestCase(test.TrialTestCase):
77
"""Test case for simple driver"""
79
super(SimpleDriverTestCase, self).setUp()
80
self.flags(connection_type='fake',
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()
93
self.manager.delete_user(self.user)
94
self.manager.delete_project(self.project)
96
def _create_instance(self):
97
"""Create a test instance"""
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
107
return db.instance_create(self.context, inst)['id']
109
def _create_volume(self):
110
"""Create a test volume"""
112
vol['image_id'] = 'ami-test'
113
vol['reservation_id'] = 'r-fakeres'
115
return db.volume_create(self.context, vol)['id']
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',
124
FLAGS.compute_manager)
125
compute1.startService()
126
compute2 = service.Service('host2',
129
FLAGS.compute_manager)
130
compute2.startService()
131
hosts = self.scheduler.driver.hosts_up(self.context, 'compute')
132
self.assertEqual(len(hosts), 2)
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',
141
FLAGS.compute_manager)
142
compute1.startService()
143
compute2 = service.Service('host2',
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,
153
self.assertEqual(host, 'host2')
154
compute1.terminate_instance(self.context, instance_id1)
155
db.instance_destroy(self.context, instance_id2)
159
def test_too_many_cores(self):
160
"""Ensures we don't go over max cores"""
161
compute1 = service.Service('host1',
164
FLAGS.compute_manager)
165
compute1.startService()
166
compute2 = service.Service('host2',
169
FLAGS.compute_manager)
170
compute2.startService()
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,
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)
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',
197
FLAGS.volume_manager)
198
volume1.startService()
199
volume2 = service.Service('host2',
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,
209
self.assertEqual(host, 'host2')
210
volume1.delete_volume(self.context, volume_id1)
211
db.volume_destroy(self.context, volume_id2)
215
def test_too_many_gigabytes(self):
216
"""Ensures we don't go over max gigabytes"""
217
volume1 = service.Service('host1',
220
FLAGS.volume_manager)
221
volume1.startService()
222
volume2 = service.Service('host2',
225
FLAGS.volume_manager)
226
volume2.startService()
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,
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)