Simplify random host choice.

The random Python module provides a handy funcion for choosing a random
element from a sequence.  Use that instead of a more manual method of
accomplishing the same thing.

Change-Id: I1908bd5e79b6c21d0dcf7e540143e9b8de478d24
This commit is contained in:
Russell Bryant 2013-04-10 01:39:16 +02:00
parent a993b2b969
commit 2afb205d51
2 changed files with 14 additions and 15 deletions

View File

@ -56,7 +56,7 @@ class ChanceScheduler(driver.Scheduler):
msg = _("Could not find another compute")
raise exception.NoValidHost(reason=msg)
return hosts[int(random.random() * len(hosts))]
return random.choice(hosts)
def select_hosts(self, context, request_spec, filter_properties):
"""Selects a set of random hosts."""

View File

@ -82,15 +82,15 @@ class ChanceSchedulerTestCase(test_scheduler.SchedulerTestCase):
self.mox.StubOutWithMock(ctxt, 'elevated')
self.mox.StubOutWithMock(self.driver, 'hosts_up')
self.mox.StubOutWithMock(random, 'random')
self.mox.StubOutWithMock(random, 'choice')
self.mox.StubOutWithMock(driver, 'instance_update_db')
self.mox.StubOutWithMock(compute_rpcapi.ComputeAPI, 'run_instance')
ctxt.elevated().AndReturn(ctxt_elevated)
# instance 1
self.driver.hosts_up(ctxt_elevated, 'compute').AndReturn(
['host1', 'host2', 'host3', 'host4'])
random.random().AndReturn(.5)
hosts_full = ['host1', 'host2', 'host3', 'host4']
self.driver.hosts_up(ctxt_elevated, 'compute').AndReturn(hosts_full)
random.choice(hosts_full).AndReturn('host3')
driver.instance_update_db(ctxt, instance1['uuid']).WithSideEffects(
inc_launch_index).AndReturn(instance1)
compute_rpcapi.ComputeAPI.run_instance(ctxt, host='host3',
@ -100,9 +100,8 @@ class ChanceSchedulerTestCase(test_scheduler.SchedulerTestCase):
# instance 2
ctxt.elevated().AndReturn(ctxt_elevated)
self.driver.hosts_up(ctxt_elevated, 'compute').AndReturn(
['host1', 'host2', 'host3', 'host4'])
random.random().AndReturn(.2)
self.driver.hosts_up(ctxt_elevated, 'compute').AndReturn(hosts_full)
random.choice(hosts_full).AndReturn('host1')
driver.instance_update_db(ctxt, instance2['uuid']).WithSideEffects(
inc_launch_index).AndReturn(instance2)
compute_rpcapi.ComputeAPI.run_instance(ctxt, host='host1',
@ -178,19 +177,19 @@ class ChanceSchedulerTestCase(test_scheduler.SchedulerTestCase):
self.mox.StubOutWithMock(ctxt, 'elevated')
self.mox.StubOutWithMock(self.driver, 'hosts_up')
self.mox.StubOutWithMock(random, 'random')
self.mox.StubOutWithMock(random, 'choice')
ctxt.elevated().AndReturn(ctxt_elevated)
# instance 1
self.driver.hosts_up(ctxt_elevated, 'compute').AndReturn(
['host1', 'host2', 'host3', 'host4'])
random.random().AndReturn(.5)
hosts_full = ['host1', 'host2', 'host3', 'host4']
self.driver.hosts_up(ctxt_elevated, 'compute').AndReturn(hosts_full)
random.choice(hosts_full).AndReturn('host3')
# instance 2
ctxt.elevated().AndReturn(ctxt_elevated)
self.driver.hosts_up(ctxt_elevated, 'compute').AndReturn(
['host1', 'host2', 'host3', 'host4'])
random.random().AndReturn(.2)
self.driver.hosts_up(ctxt_elevated, 'compute').AndReturn(hosts_full)
random.choice(hosts_full).AndReturn('host1')
self.mox.ReplayAll()
hosts = self.driver.select_hosts(ctxt, request_spec, {})