Commit 883f5aec authored by Eduardo Falcão's avatar Eduardo Falcão
Browse files

fixing unit test for shared provisioner

parent c387e015
......@@ -73,16 +73,6 @@ public class ResourceProvisionerShared extends ResourceProvisionerAbstract {
deallocateResourceForVm(vm);
}
/*
Pe resources are not stored in the VM resource List.
Only the provisioner keeps track of Pe allocation for VM.
This way, if the resource is not found inside the VM
and it is a Pe, it's OK (as it is expected)
*/
if(!getResource().isObjectSubClassOf(Pe.class) && !vm.getResource(getResourceClass()).setCapacity(newTotalVmResourceCapacity)){
return false;
}
//Allocates the requested resource from the physical resource
getResource().allocateResource(newTotalVmResourceCapacity);
getResourceAllocationMap().put(vm, newTotalVmResourceCapacity);
......
......@@ -235,14 +235,14 @@ final class VmNull implements Vm {
return null;
}
@Override
public double updateProcessing(double currentTime, List<Double> mipsShare) {
// TODO Auto-generated method stub
return 0;
}
@Override
public SlaContract getContract() {
return null;
}
@Override
public void setContract(SlaContract contract) { }
public void setContract(SlaContract contract) { }
@Override
public double updateProcessing(double currentTime, List<Double> mipsShare,
double iops) {
return 0;
}
}
......@@ -8,7 +8,13 @@
package org.cloudbus.cloudsim.provisioners;
import java.util.ArrayList;
import java.util.List;
import org.cloudbus.cloudsim.resources.Bandwidth;
import org.cloudbus.cloudsim.resources.Iops;
import org.cloudbus.cloudsim.resources.Ram;
import org.cloudbus.cloudsim.resources.Storage;
import static org.cloudbus.cloudsim.vms.VmSimpleTest.createVm;
import static org.junit.Assert.assertEquals;
......@@ -16,7 +22,6 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.cloudbus.cloudsim.vms.VmSimple;
import org.junit.Before;
import org.junit.Test;
import org.cloudbus.cloudsim.resources.ResourceManageable;
......@@ -31,20 +36,25 @@ public class ResourceProvisionerSharedTest {
/** @see #createSharedProvisioner() */
private ResourceProvisionerShared provisioner;
private List<ResourceManageable> resources;
@Before
public void setUp() throws Exception {
provisioner = createSharedProvisioner();
/**
* It does not have an @before annotation because of the argument
* that may change.
* @param capacity
*/
public void setUp(long capacity){
resources = new ArrayList<ResourceManageable>();
resources.add(new Ram(capacity));
resources.add(new Bandwidth(capacity));
resources.add(new Iops(capacity));
resources.add(new Storage(capacity));
}
/**
* Creates a shared provisioner for any generic resource. It in fact doesn't matter
* if it is for HOST_RAM, CPU, HOST_BW or any other possible resource.
*/
private ResourceProvisionerShared createSharedProvisioner() {
return new ResourceProvisionerShared(new Ram(CAPACITY));
}
private ResourceProvisionerShared createSharedProvisioner(ResourceManageable resource) {
return new ResourceProvisionerShared(resource);
}
......@@ -56,146 +66,176 @@ public class ResourceProvisionerSharedTest {
@Test
public void testGetCapacity() {
assertEquals(CAPACITY, provisioner.getCapacity());
setUp(CAPACITY);
for(ResourceManageable res: resources){
provisioner = createSharedProvisioner(res);
assertEquals(CAPACITY, provisioner.getCapacity());
}
}
@Test
public void testGetAvailableResource() {
assertEquals(CAPACITY, provisioner.getAvailableResource());
setUp(CAPACITY);
for(ResourceManageable res: resources){
provisioner = createSharedProvisioner(res);
assertEquals(CAPACITY, provisioner.getAvailableResource());
}
}
@Test
public void testGetTotalAllocatedResource() {
final ResourceProvisioner p = createSharedProvisioner();
assertEquals(p.getCapacity(), p.getAvailableResource());
assertEquals(0, p.getTotalAllocatedResource());
final VmSimple vm = createVm(1, CAPACITY);
final long allocatedResource = HALF_CAPACITY;
p.allocateResourceForVm(vm, allocatedResource);
assertEquals(allocatedResource, p.getTotalAllocatedResource());
setUp(CAPACITY);
for(ResourceManageable res: resources){
provisioner = createSharedProvisioner(res);
assertEquals(provisioner.getCapacity(), provisioner.getAvailableResource());
assertEquals(0, provisioner.getTotalAllocatedResource());
final VmSimple vm = createVm(1, CAPACITY);
final long allocatedResource = HALF_CAPACITY;
provisioner.allocateResourceForVm(vm, allocatedResource);
assertEquals(allocatedResource, provisioner.getTotalAllocatedResource());
}
}
@Test
public void testIsSuitableForVm() {
final VmSimple vm0 = createVm(0, HALF_CAPACITY);
assertTrue(provisioner.isSuitableForVm(vm0, QUARTER_OF_CAPACITY));
assertTrue(provisioner.isSuitableForVm(vm0, HALF_CAPACITY));
assertTrue(provisioner.isSuitableForVm(vm0, CAPACITY));
assertTrue(provisioner.isSuitableForVm(vm0, CAPACITY*2));
provisioner.allocateResourceForVm(vm0, HALF_CAPACITY);
assertTrue(provisioner.isSuitableForVm(vm0, QUARTER_OF_CAPACITY));
for(int i = 1; i <= CAPACITY-1; i++){
VmSimple vm = createVm(i, HALF_CAPACITY);
assertTrue(provisioner.isSuitableForVm(vm, HALF_CAPACITY));
provisioner.allocateResourceForVm(vm, HALF_CAPACITY);
setUp(CAPACITY);
for(ResourceManageable res: resources){
provisioner = createSharedProvisioner(res);
final VmSimple vm0 = createVm(0, HALF_CAPACITY);
assertTrue(provisioner.isSuitableForVm(vm0, QUARTER_OF_CAPACITY));
assertTrue(provisioner.isSuitableForVm(vm0, HALF_CAPACITY));
assertTrue(provisioner.isSuitableForVm(vm0, CAPACITY));
assertTrue(provisioner.isSuitableForVm(vm0, CAPACITY*2));
provisioner.allocateResourceForVm(vm0, HALF_CAPACITY);
assertTrue(provisioner.isSuitableForVm(vm0, QUARTER_OF_CAPACITY));
for(int i = 1; i <= CAPACITY-1; i++){
VmSimple vm = createVm(i, HALF_CAPACITY);
assertTrue(provisioner.isSuitableForVm(vm, HALF_CAPACITY));
provisioner.allocateResourceForVm(vm, HALF_CAPACITY);
}
VmSimple vm = createVm(1001, HALF_CAPACITY);
assertFalse(provisioner.isSuitableForVm(vm, HALF_CAPACITY));
}
VmSimple vm = createVm(1001, HALF_CAPACITY);
assertFalse(provisioner.isSuitableForVm(vm, HALF_CAPACITY));
}
@Test
public void testAllocateResourceForVmScenario2() {
provisioner = new ResourceProvisionerShared(new Ram(2*CAPACITY));
final VmSimple vm0 = createVm(0, 200);
final VmSimple vm1 = createVm(1, 700);
final VmSimple vm2 = createVm(2, 701);
final VmSimple vm3 = createVm(3, 550);
assertTrue(provisioner.isSuitableForVm(vm0, 200));
assertTrue(provisioner.allocateResourceForVm(vm0, 200));
assertEquals(1800, provisioner.getAvailableResource());
assertTrue(provisioner.isSuitableForVm(vm1, 700));
assertTrue(provisioner.allocateResourceForVm(vm1, 700));
assertEquals(1100, provisioner.getAvailableResource());
assertTrue(provisioner.isSuitableForVm(vm2, 701));
assertTrue(provisioner.allocateResourceForVm(vm2, 701));
assertEquals(399, provisioner.getAvailableResource());
assertTrue(provisioner.isSuitableForVm(vm3, 550));
assertTrue(provisioner.allocateResourceForVm(vm3,550));
assertEquals(0, provisioner.getAvailableResource());
assertEquals(200, provisioner.getAllocatedResourceForVm(vm0));
assertEquals(625, provisioner.getAllocatedResourceForVm(vm1));
assertEquals(625, provisioner.getAllocatedResourceForVm(vm2));
assertEquals(550, provisioner.getAllocatedResourceForVm(vm3));
setUp(2*CAPACITY);
for(ResourceManageable res: resources){
provisioner = createSharedProvisioner(res);
final VmSimple vm0 = createVm(0, 200);
final VmSimple vm1 = createVm(1, 700);
final VmSimple vm2 = createVm(2, 701);
final VmSimple vm3 = createVm(3, 550);
assertTrue(provisioner.isSuitableForVm(vm0, 200));
assertTrue(provisioner.allocateResourceForVm(vm0, 200));
assertEquals(1800, provisioner.getAvailableResource());
assertTrue(provisioner.isSuitableForVm(vm1, 700));
assertTrue(provisioner.allocateResourceForVm(vm1, 700));
assertEquals(1100, provisioner.getAvailableResource());
assertTrue(provisioner.isSuitableForVm(vm2, 701));
assertTrue(provisioner.allocateResourceForVm(vm2, 701));
assertEquals(399, provisioner.getAvailableResource());
assertTrue(provisioner.isSuitableForVm(vm3, 550));
assertTrue(provisioner.allocateResourceForVm(vm3,550));
assertEquals(0, provisioner.getAvailableResource());
assertEquals(200, provisioner.getAllocatedResourceForVm(vm0));
assertEquals(625, provisioner.getAllocatedResourceForVm(vm1));
assertEquals(625, provisioner.getAllocatedResourceForVm(vm2));
assertEquals(550, provisioner.getAllocatedResourceForVm(vm3));
}
}
@Test
public void testAllocateResourceForVm() {
final VmSimple vm0 = createVm(0, HALF_CAPACITY);
final VmSimple vm1 = createVm(1, CAPACITY);
assertTrue(provisioner.isSuitableForVm(vm0, HALF_CAPACITY));
assertTrue(provisioner.allocateResourceForVm(vm0, HALF_CAPACITY));
assertEquals(HALF_CAPACITY, provisioner.getAvailableResource());
assertTrue(provisioner.isSuitableForVm(vm1, CAPACITY));
assertTrue(provisioner.allocateResourceForVm(vm1, CAPACITY));
assertEquals(0, provisioner.getAvailableResource());
assertTrue(provisioner.isSuitableForVm(vm1, QUARTER_OF_CAPACITY));
assertTrue(provisioner.allocateResourceForVm(vm1, QUARTER_OF_CAPACITY));
assertEquals(QUARTER_OF_CAPACITY, provisioner.getAvailableResource());
assertTrue(provisioner.isSuitableForVm(vm1, HALF_CAPACITY));
assertTrue(provisioner.allocateResourceForVm(vm1, HALF_CAPACITY));
assertEquals(0, provisioner.getAvailableResource());
setUp(CAPACITY);
for(ResourceManageable res: resources){
provisioner = createSharedProvisioner(res);
final VmSimple vm0 = createVm(0, HALF_CAPACITY);
final VmSimple vm1 = createVm(1, CAPACITY);
assertTrue(provisioner.isSuitableForVm(vm0, HALF_CAPACITY));
assertTrue(provisioner.allocateResourceForVm(vm0, HALF_CAPACITY));
assertEquals(HALF_CAPACITY, provisioner.getAvailableResource());
assertTrue(provisioner.isSuitableForVm(vm1, CAPACITY));
assertTrue(provisioner.allocateResourceForVm(vm1, CAPACITY));
assertEquals(0, provisioner.getAvailableResource());
assertTrue(provisioner.isSuitableForVm(vm1, QUARTER_OF_CAPACITY));
assertTrue(provisioner.allocateResourceForVm(vm1, QUARTER_OF_CAPACITY));
assertEquals(QUARTER_OF_CAPACITY, provisioner.getAvailableResource());
assertTrue(provisioner.isSuitableForVm(vm1, HALF_CAPACITY));
assertTrue(provisioner.allocateResourceForVm(vm1, HALF_CAPACITY));
assertEquals(0, provisioner.getAvailableResource());
}
}
@Test
public void testGetAllocatedResourceForVm() {
final VmSimple vm1 = createVm(0, HALF_CAPACITY);
final VmSimple vm2 = createVm(1, CAPACITY);
assertTrue(provisioner.isSuitableForVm(vm1, HALF_CAPACITY));
assertTrue(provisioner.allocateResourceForVm(vm1, HALF_CAPACITY));
assertEquals(HALF_CAPACITY, provisioner.getAllocatedResourceForVm(vm1));
assertTrue(provisioner.isSuitableForVm(vm2, CAPACITY));
assertTrue(provisioner.allocateResourceForVm(vm2, CAPACITY));
assertEquals(HALF_CAPACITY, provisioner.getAllocatedResourceForVm(vm2));
assertTrue(provisioner.isSuitableForVm(vm2, QUARTER_OF_CAPACITY));
assertTrue(provisioner.allocateResourceForVm(vm2, QUARTER_OF_CAPACITY));
assertEquals(QUARTER_OF_CAPACITY, provisioner.getAllocatedResourceForVm(vm2));
assertTrue(provisioner.isSuitableForVm(vm2, HALF_CAPACITY));
assertTrue(provisioner.allocateResourceForVm(vm2, HALF_CAPACITY));
assertEquals(HALF_CAPACITY, provisioner.getAllocatedResourceForVm(vm2));
setUp(CAPACITY);
for(ResourceManageable res: resources){
provisioner = createSharedProvisioner(res);
final VmSimple vm1 = createVm(0, HALF_CAPACITY);
final VmSimple vm2 = createVm(1, CAPACITY);
assertTrue(provisioner.isSuitableForVm(vm1, HALF_CAPACITY));
assertTrue(provisioner.allocateResourceForVm(vm1, HALF_CAPACITY));
assertEquals(HALF_CAPACITY, provisioner.getAllocatedResourceForVm(vm1));
assertTrue(provisioner.isSuitableForVm(vm2, CAPACITY));
assertTrue(provisioner.allocateResourceForVm(vm2, CAPACITY));
assertEquals(HALF_CAPACITY, provisioner.getAllocatedResourceForVm(vm2));
assertTrue(provisioner.isSuitableForVm(vm2, QUARTER_OF_CAPACITY));
assertTrue(provisioner.allocateResourceForVm(vm2, QUARTER_OF_CAPACITY));
assertEquals(QUARTER_OF_CAPACITY, provisioner.getAllocatedResourceForVm(vm2));
assertTrue(provisioner.isSuitableForVm(vm2, HALF_CAPACITY));
assertTrue(provisioner.allocateResourceForVm(vm2, HALF_CAPACITY));
assertEquals(HALF_CAPACITY, provisioner.getAllocatedResourceForVm(vm2));
}
}
@Test
public void testDeallocateResourceForVm() {
final VmSimple vm1 = createVm(0, HALF_CAPACITY);
final VmSimple vm2 = createVm(1, HALF_CAPACITY);
assertEquals(0, vm1.getCurrentAllocatedRam());
assertEquals(0, vm2.getCurrentAllocatedRam());
assertTrue(provisioner.isSuitableForVm(vm1, HALF_CAPACITY));
assertTrue(provisioner.allocateResourceForVm(vm1, HALF_CAPACITY));
assertEquals(HALF_CAPACITY, provisioner.getAvailableResource());
provisioner.deallocateResourceForVm(vm1);
assertEquals(CAPACITY, provisioner.getAvailableResource());
assertTrue(provisioner.isSuitableForVm(vm1, HALF_CAPACITY));
assertTrue(provisioner.allocateResourceForVm(vm1, HALF_CAPACITY));
assertTrue(provisioner.isSuitableForVm(vm2, HALF_CAPACITY));
assertTrue(provisioner.allocateResourceForVm(vm2, HALF_CAPACITY));
assertEquals(0, provisioner.getAvailableResource());
provisioner.deallocateResourceForVm(vm1);
provisioner.deallocateResourceForVm(vm2);
assertEquals(CAPACITY, provisioner.getAvailableResource());
assertEquals(0, vm1.getCurrentAllocatedRam());
assertEquals(0, vm2.getCurrentAllocatedRam());
setUp(CAPACITY);
for(ResourceManageable res: resources){
provisioner = createSharedProvisioner(res);
final VmSimple vm1 = createVm(0, HALF_CAPACITY);
final VmSimple vm2 = createVm(1, HALF_CAPACITY);
assertEquals(0, vm1.getCurrentAllocatedRam());
assertEquals(0, vm2.getCurrentAllocatedRam());
assertTrue(provisioner.isSuitableForVm(vm1, HALF_CAPACITY));
assertTrue(provisioner.allocateResourceForVm(vm1, HALF_CAPACITY));
assertEquals(HALF_CAPACITY, provisioner.getAvailableResource());
provisioner.deallocateResourceForVm(vm1);
assertEquals(CAPACITY, provisioner.getAvailableResource());
assertTrue(provisioner.isSuitableForVm(vm1, HALF_CAPACITY));
assertTrue(provisioner.allocateResourceForVm(vm1, HALF_CAPACITY));
assertTrue(provisioner.isSuitableForVm(vm2, HALF_CAPACITY));
assertTrue(provisioner.allocateResourceForVm(vm2, HALF_CAPACITY));
assertEquals(0, provisioner.getAvailableResource());
provisioner.deallocateResourceForVm(vm1);
provisioner.deallocateResourceForVm(vm2);
assertEquals(CAPACITY, provisioner.getAvailableResource());
assertEquals(0, vm1.getCurrentAllocatedRam());
assertEquals(0, vm2.getCurrentAllocatedRam());
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment