Avoid dividing zero
Some flavors may set disk size 0 byte, so disk_capacity may be 0. Change-Id: I0f2cfd5ce9f64cd94e7b6ded7865384cb2b1e190 Closes-Bug: #1517033
This commit is contained in:
@@ -233,8 +233,12 @@ class BasicConsolidation(Strategy):
|
||||
score_cores = (1 - (float(cpu_capacity) - float(total_cores_used)) /
|
||||
float(cpu_capacity))
|
||||
|
||||
score_disk = (1 - (float(disk_capacity) - float(total_disk_used)) /
|
||||
float(disk_capacity))
|
||||
# It's possible that disk_capacity is 0, e.g. m1.nano.disk = 0
|
||||
if disk_capacity == 0:
|
||||
score_disk = 0
|
||||
else:
|
||||
score_disk = (1 - (float(disk_capacity) - float(total_disk_used)) /
|
||||
float(disk_capacity))
|
||||
|
||||
score_memory = (
|
||||
1 - (float(memory_capacity) - float(total_memory_used)) /
|
||||
|
||||
@@ -336,3 +336,49 @@ class FakerStateCollector(ClusterStateCollector):
|
||||
current_state_cluster.add_hypervisor(node)
|
||||
|
||||
return current_state_cluster
|
||||
|
||||
def generate_scenario_5_with_vm_disk_0(self):
|
||||
vms = []
|
||||
current_state_cluster = ModelRoot()
|
||||
# number of nodes
|
||||
count_node = 1
|
||||
# number of vms
|
||||
count_vm = 1
|
||||
|
||||
# define ressouce ( CPU, MEM disk, ... )
|
||||
mem = Resource(ResourceType.memory)
|
||||
# 2199.954 Mhz
|
||||
num_cores = Resource(ResourceType.cpu_cores)
|
||||
disk = Resource(ResourceType.disk)
|
||||
|
||||
current_state_cluster.create_resource(mem)
|
||||
current_state_cluster.create_resource(num_cores)
|
||||
current_state_cluster.create_resource(disk)
|
||||
|
||||
for i in range(0, count_node):
|
||||
node_uuid = "Node_" + str(i)
|
||||
node = Hypervisor()
|
||||
node.uuid = node_uuid
|
||||
|
||||
mem.set_capacity(node, 4)
|
||||
disk.set_capacity(node, 4)
|
||||
num_cores.set_capacity(node, 4)
|
||||
# print("create "+str(node))
|
||||
current_state_cluster.add_hypervisor(node)
|
||||
|
||||
for i in range(0, count_vm):
|
||||
vm_uuid = "VM_" + str(i)
|
||||
vm = VM()
|
||||
vm.uuid = vm_uuid
|
||||
# print("create "+str(vm))
|
||||
mem.set_capacity(vm, 2)
|
||||
disk.set_capacity(vm, 0)
|
||||
num_cores.set_capacity(vm, 4)
|
||||
vms.append(vm)
|
||||
current_state_cluster.add_vm(vm)
|
||||
|
||||
current_state_cluster.get_mapping().map(
|
||||
current_state_cluster.get_hypervisor_from_id("Node_0"),
|
||||
current_state_cluster.get_vm_from_id("VM_0"))
|
||||
|
||||
return current_state_cluster
|
||||
|
||||
@@ -92,6 +92,14 @@ class TestBasicConsolidation(base.BaseTestCase):
|
||||
vm_7_score = 0.0
|
||||
self.assertEqual(sercon.calculate_score_vm(vm_7, cluster), vm_7_score)
|
||||
|
||||
def test_basic_consolidation_score_vm_disk(self):
|
||||
cluster = self.fake_cluster.generate_scenario_5_with_vm_disk_0()
|
||||
sercon = BasicConsolidation()
|
||||
sercon.set_metrics_resource_collector(self.fake_metrics)
|
||||
vm_0 = cluster.get_vm_from_id("VM_0")
|
||||
vm_0_score = 0.0
|
||||
self.assertEqual(sercon.calculate_score_vm(vm_0, cluster), vm_0_score)
|
||||
|
||||
def test_basic_consolidation_weight(self):
|
||||
cluster = self.fake_cluster.generate_scenario_1()
|
||||
sercon = BasicConsolidation()
|
||||
|
||||
Reference in New Issue
Block a user