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

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-08-16 14:04:11 UTC
  • mto: This revision was merged to the branch mainline in revision 84.
  • Revision ID: package-import@ubuntu.com-20120816140411-0mr4n241wmk30t9l
Tags: upstream-2012.2~f3
ImportĀ upstreamĀ versionĀ 2012.2~f3

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
#    License for the specific language governing permissions and limitations
14
14
#    under the License.
15
15
"""
16
 
Tests For Distributed Scheduler.
 
16
Tests For Filter Scheduler.
17
17
"""
18
18
 
19
19
import mox
20
20
 
21
21
from nova import context
22
22
from nova import exception
 
23
from nova.scheduler import driver
23
24
from nova.scheduler import filter_scheduler
24
25
from nova.scheduler import host_manager
25
26
from nova.scheduler import least_cost
32
33
 
33
34
 
34
35
class FilterSchedulerTestCase(test_scheduler.SchedulerTestCase):
35
 
    """Test case for Distributed Scheduler."""
 
36
    """Test case for Filter Scheduler."""
36
37
 
37
38
    driver_cls = filter_scheduler.FilterScheduler
38
39
 
50
51
                                          'ephemeral_gb': 0},
51
52
                        'instance_properties': {'project_id': 1}}
52
53
        self.assertRaises(exception.NoValidHost, sched.schedule_run_instance,
53
 
                          fake_context, request_spec, None)
 
54
                          fake_context, request_spec, None, None, None,
 
55
                          None, {}, None)
54
56
 
55
57
    def test_run_instance_non_admin(self):
56
58
        """Test creating an instance locally using run_instance, passing
71
73
        request_spec = {'instance_type': {'memory_mb': 1, 'local_gb': 1},
72
74
                        'instance_properties': {'project_id': 1}}
73
75
        self.assertRaises(exception.NoValidHost, sched.schedule_run_instance,
74
 
                          fake_context, request_spec, None)
 
76
                          fake_context, request_spec, None, None, None,
 
77
                          None, {}, None)
75
78
        self.assertTrue(self.was_admin)
76
79
 
77
80
    def test_schedule_bad_topic(self):
79
82
        sched = fakes.FakeFilterScheduler()
80
83
        fake_context = context.RequestContext('user', 'project')
81
84
        self.assertRaises(NotImplementedError, sched._schedule, fake_context,
82
 
                          "foo", {})
 
85
                          "foo", {}, {})
83
86
 
84
87
    def test_scheduler_includes_launch_index(self):
85
88
        ctxt = "fake-context"
111
114
        self.mox.StubOutWithMock(self.driver, '_provision_resource')
112
115
 
113
116
        self.driver._schedule(context_fake, 'compute',
114
 
                              request_spec, **fake_kwargs
 
117
                              request_spec, {}
115
118
                              ).AndReturn(['host1', 'host2'])
116
119
        # instance 1
117
120
        self.driver._provision_resource(
118
121
            ctxt, 'host1',
119
122
            mox.Func(_has_launch_index(0)), None,
120
 
            fake_kwargs).AndReturn(instance1)
 
123
            {}, None, None, None, None).AndReturn(instance1)
121
124
        # instance 2
122
125
        self.driver._provision_resource(
123
126
            ctxt, 'host2',
124
127
            mox.Func(_has_launch_index(1)), None,
125
 
            fake_kwargs).AndReturn(instance2)
 
128
            {}, None, None, None, None).AndReturn(instance2)
126
129
        self.mox.ReplayAll()
127
130
 
128
 
        self.driver.schedule_run_instance(context_fake, request_spec, None,
129
 
                                          **fake_kwargs)
 
131
        self.driver.schedule_run_instance(context_fake, request_spec,
 
132
                None, None, None, None, {}, None)
130
133
 
131
134
    def test_schedule_happy_day(self):
132
135
        """Make sure there's nothing glaringly wrong with _schedule()
160
163
                                                'vcpus': 1}}
161
164
        self.mox.ReplayAll()
162
165
        weighted_hosts = sched._schedule(fake_context, 'compute',
163
 
                request_spec)
 
166
                request_spec, {})
164
167
        self.assertEquals(len(weighted_hosts), 10)
165
168
        for weighted_host in weighted_hosts:
166
169
            self.assertTrue(weighted_host.host_state is not None)
167
170
 
 
171
    def test_schedule_prep_resize_doesnt_update_host(self):
 
172
        fake_context = context.RequestContext('user', 'project',
 
173
                is_admin=True)
 
174
 
 
175
        sched = fakes.FakeFilterScheduler()
 
176
 
 
177
        def _return_hosts(*args, **kwargs):
 
178
            host_state = host_manager.HostState('host2', 'compute')
 
179
            return [least_cost.WeightedHost(1.0, host_state=host_state)]
 
180
 
 
181
        self.stubs.Set(sched, '_schedule', _return_hosts)
 
182
 
 
183
        info = {'called': 0}
 
184
 
 
185
        def _fake_instance_update_db(*args, **kwargs):
 
186
            # This should not be called
 
187
            info['called'] = 1
 
188
 
 
189
        self.stubs.Set(driver, 'instance_update_db',
 
190
                _fake_instance_update_db)
 
191
 
 
192
        instance = {'uuid': 'fake-uuid', 'host': 'host1'}
 
193
 
 
194
        sched.schedule_prep_resize(fake_context, {}, {}, {}, instance, {})
 
195
        self.assertEqual(info['called'], 0)
 
196
 
168
197
    def test_get_cost_functions(self):
169
198
        self.flags(reserved_host_memory_mb=128)
170
199
        fixture = fakes.FakeFilterScheduler()
176
205
        hostinfo.update_from_compute_node(dict(memory_mb=1000,
177
206
                local_gb=0, vcpus=1))
178
207
        self.assertEquals(1000 - 128, fn(hostinfo, {}))
 
208
 
 
209
    def test_max_attempts(self):
 
210
        self.flags(scheduler_max_attempts=4)
 
211
 
 
212
        sched = fakes.FakeFilterScheduler()
 
213
        self.assertEqual(4, sched._max_attempts())
 
214
 
 
215
    def test_invalid_max_attempts(self):
 
216
        self.flags(scheduler_max_attempts=0)
 
217
 
 
218
        sched = fakes.FakeFilterScheduler()
 
219
        self.assertRaises(exception.NovaException, sched._max_attempts)
 
220
 
 
221
    def test_retry_disabled(self):
 
222
        """Retry info should not get populated when re-scheduling is off"""
 
223
        self.flags(scheduler_max_attempts=1)
 
224
        sched = fakes.FakeFilterScheduler()
 
225
 
 
226
        instance_properties = {}
 
227
        request_spec = dict(instance_properties=instance_properties)
 
228
        filter_properties = {}
 
229
 
 
230
        sched._schedule(self.context, 'compute', request_spec,
 
231
                filter_properties=filter_properties)
 
232
 
 
233
        # should not have retry info in the populated filter properties:
 
234
        self.assertFalse("retry" in filter_properties)
 
235
 
 
236
    def test_retry_attempt_one(self):
 
237
        """Test retry logic on initial scheduling attempt"""
 
238
        self.flags(scheduler_max_attempts=2)
 
239
        sched = fakes.FakeFilterScheduler()
 
240
 
 
241
        instance_properties = {}
 
242
        request_spec = dict(instance_properties=instance_properties)
 
243
        filter_properties = {}
 
244
 
 
245
        sched._schedule(self.context, 'compute', request_spec,
 
246
                filter_properties=filter_properties)
 
247
 
 
248
        num_attempts = filter_properties['retry']['num_attempts']
 
249
        self.assertEqual(1, num_attempts)
 
250
 
 
251
    def test_retry_attempt_two(self):
 
252
        """Test retry logic when re-scheduling"""
 
253
        self.flags(scheduler_max_attempts=2)
 
254
        sched = fakes.FakeFilterScheduler()
 
255
 
 
256
        instance_properties = {}
 
257
        request_spec = dict(instance_properties=instance_properties)
 
258
 
 
259
        retry = dict(num_attempts=1)
 
260
        filter_properties = dict(retry=retry)
 
261
 
 
262
        sched._schedule(self.context, 'compute', request_spec,
 
263
                filter_properties=filter_properties)
 
264
 
 
265
        num_attempts = filter_properties['retry']['num_attempts']
 
266
        self.assertEqual(2, num_attempts)
 
267
 
 
268
    def test_retry_exceeded_max_attempts(self):
 
269
        """Test for necessary explosion when max retries is exceeded"""
 
270
        self.flags(scheduler_max_attempts=2)
 
271
        sched = fakes.FakeFilterScheduler()
 
272
 
 
273
        instance_properties = {}
 
274
        request_spec = dict(instance_properties=instance_properties)
 
275
 
 
276
        retry = dict(num_attempts=2)
 
277
        filter_properties = dict(retry=retry)
 
278
 
 
279
        self.assertRaises(exception.NoValidHost, sched._schedule, self.context,
 
280
                'compute', request_spec, filter_properties=filter_properties)
 
281
 
 
282
    def test_add_retry_host(self):
 
283
        retry = dict(num_attempts=1, hosts=[])
 
284
        filter_properties = dict(retry=retry)
 
285
        host = "fakehost"
 
286
 
 
287
        sched = fakes.FakeFilterScheduler()
 
288
        sched._add_retry_host(filter_properties, host)
 
289
 
 
290
        hosts = filter_properties['retry']['hosts']
 
291
        self.assertEqual(1, len(hosts))
 
292
        self.assertEqual(host, hosts[0])