82
82
super(LeastCostSchedulerTestCase, self).tearDown()
84
84
def assertWeights(self, expected, num, request_spec, hosts):
85
weighted = self.sched.weigh_hosts("compute", request_spec, hosts)
85
weighted = self.sched.weigh_hosts(request_spec, hosts)
86
86
self.assertDictListMatch(weighted, expected, approx_equal=True)
88
88
def test_no_hosts(self):
97
97
self.flags(least_cost_scheduler_cost_functions=[
98
98
'nova.scheduler.least_cost.noop_cost_fn'],
99
99
noop_cost_fn_weight=1)
102
101
request_spec = {}
103
102
hosts = self.sched.filter_hosts(num, request_spec)
105
expected = [dict(weight=1, hostname=hostname)
106
for hostname, caps in hosts]
103
expected = [{"hostname": hostname, "weight": 1, "capabilities": caps}
104
for hostname, caps in hosts]
107
105
self.assertWeights(expected, num, request_spec, hosts)
109
107
def test_cost_fn_weights(self):
110
108
self.flags(least_cost_scheduler_cost_functions=[
111
109
'nova.scheduler.least_cost.noop_cost_fn'],
112
110
noop_cost_fn_weight=2)
115
112
request_spec = {}
116
113
hosts = self.sched.filter_hosts(num, request_spec)
118
expected = [dict(weight=2, hostname=hostname)
119
for hostname, caps in hosts]
120
self.assertWeights(expected, num, request_spec, hosts)
122
def test_compute_fill_first_cost_fn(self):
123
self.flags(least_cost_scheduler_cost_functions=[
124
'nova.scheduler.least_cost.compute_fill_first_cost_fn'],
125
compute_fill_first_cost_fn_weight=1)
127
instance_type = {'memory_mb': 1024}
128
request_spec = {'instance_type': instance_type}
129
svc_states = self.sched.zone_manager.service_states.iteritems()
130
all_hosts = [(host, services["compute"])
131
for host, services in svc_states
132
if "compute" in services]
133
hosts = self.sched.filter_hosts('compute', request_spec, all_hosts)
136
for idx, (hostname, services) in enumerate(hosts):
137
caps = copy.deepcopy(services["compute"])
138
# Costs are normalized so over 10 hosts, each host with increasing
139
# free ram will cost 1/N more. Since the lowest cost host has some
140
# free ram, we add in the 1/N for the base_cost
141
weight = 0.1 + (0.1 * idx)
142
wtd_dict = dict(hostname=hostname, weight=weight,
144
expected.append(wtd_dict)
114
expected = [{"hostname": hostname, "weight": 2, "capabilities": caps}
115
for hostname, caps in hosts]
146
116
self.assertWeights(expected, num, request_spec, hosts)