PageRenderTime 44ms CodeModel.GetById 17ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/test/java/nl/bitbrains/nebu/deployer/policies/TestAggregatedLocalityPolicy.java

https://github.com/deltaforge/nebu-core
Java | 158 lines | 140 code | 14 blank | 4 comment | 1 complexity | 1a256d946907e73a1ff3de5878b96c3a MD5 | raw file
  1package nl.bitbrains.nebu.deployer.policies;
  2
  3import java.util.ArrayList;
  4import java.util.HashSet;
  5import java.util.List;
  6import java.util.Set;
  7
  8import nl.bitbrains.nebu.common.VirtualMachine;
  9import nl.bitbrains.nebu.common.VirtualMachineBuilder;
 10import nl.bitbrains.nebu.common.cache.CacheException;
 11import nl.bitbrains.nebu.common.topology.PhysicalTopology;
 12import nl.bitbrains.nebu.containers.Application;
 13import nl.bitbrains.nebu.containers.ApplicationBuilder;
 14import nl.bitbrains.nebu.containers.Deployment;
 15import nl.bitbrains.nebu.containers.DeploymentBuilder;
 16import nl.bitbrains.nebu.containers.VMDeploymentSpecification;
 17import nl.bitbrains.nebu.containers.VMTemplate;
 18import nl.bitbrains.nebu.deployer.DeployerException;
 19import nl.bitbrains.nebu.deployer.policies.AggregatedLocalityPolicy;
 20import nl.bitbrains.nebu.rest.client.RequestSender;
 21
 22import org.junit.Assert;
 23import org.junit.Before;
 24import org.junit.Test;
 25import org.junit.runner.RunWith;
 26import org.mockito.Mockito;
 27import org.powermock.api.mockito.PowerMockito;
 28import org.powermock.core.classloader.annotations.PowerMockIgnore;
 29import org.powermock.core.classloader.annotations.PrepareForTest;
 30import org.powermock.modules.junit4.PowerMockRunner;
 31
 32/**
 33 * @author Jesse Donkervliet, Tim Hegeman, and Stefan Hugtenburg
 34 * 
 35 */
 36@RunWith(PowerMockRunner.class)
 37@PrepareForTest({ RequestSender.class, Application.class, VMDeploymentSpecification.class })
 38@PowerMockIgnore("javax.management.*")
 39public class TestAggregatedLocalityPolicy extends PolicyTest {
 40
 41    private final int numDcs = 2;
 42    private final int numRacks = 3;
 43    private final int numCpus = 4;
 44    private final int numNetworkDisks = 2;
 45    private final int numLocalDisks = 3;
 46    private final String prefix = "prefix";
 47
 48    @Override
 49    @Before
 50    public void setUp() {
 51        super.setUp();
 52        this.setPolicy(new AggregatedLocalityPolicy());
 53    }
 54
 55    @Test
 56    public void getSetMax() {
 57        final AggregatedLocalityPolicy policy = (AggregatedLocalityPolicy) this.getPolicy();
 58        policy.setMaxVMsPerHost(1);
 59        Assert.assertEquals(1, policy.getMaxVMsPerHost());
 60    }
 61
 62    @Test
 63    public void testAllOnOneHostSimpleTopology() throws CacheException, DeployerException {
 64        this.generateDeployment(new String[] { "id" }, new int[] { 2 }, new int[] { 8 });
 65        Assert.assertEquals(this.getSpecs().get(0).getHost(), this.getSpecs().get(1).getHost());
 66    }
 67
 68    @Test
 69    public void testAllOnOneHostNonSimpleTopology() throws CacheException, DeployerException {
 70        final PhysicalTopology topology = this.createTopology(this.numDcs,
 71                                                              this.numRacks,
 72                                                              this.numCpus,
 73                                                              this.numNetworkDisks,
 74                                                              this.numLocalDisks,
 75                                                              this.prefix);
 76        this.generateDeployment(new String[] { "id" }, new int[] { 2 }, topology);
 77        Assert.assertEquals(this.getSpecs().get(0).getHost(), this.getSpecs().get(1).getHost());
 78    }
 79
 80    @Test
 81    public void testTwoHostsNonSimpleTopology() throws CacheException, DeployerException {
 82        final AggregatedLocalityPolicy policy = (AggregatedLocalityPolicy) this.getPolicy();
 83        policy.setMaxVMsPerHost(1);
 84        final PhysicalTopology topology = this.createTopology(this.numDcs,
 85                                                              this.numRacks,
 86                                                              this.numCpus,
 87                                                              this.numNetworkDisks,
 88                                                              this.numLocalDisks,
 89                                                              this.prefix);
 90        this.generateDeployment(new String[] { "id" }, new int[] { 2 }, topology);
 91        Assert.assertNotEquals(this.getSpecs().get(0).getHost(), this.getSpecs().get(1).getHost());
 92    }
 93
 94    @Test
 95    public void testThreeHostsNonSimpleTopology() throws CacheException, DeployerException {
 96        final AggregatedLocalityPolicy policy = (AggregatedLocalityPolicy) this.getPolicy();
 97        policy.setMaxVMsPerHost(1);
 98        final PhysicalTopology topology = this.createTopology(this.numDcs,
 99                                                              this.numRacks,
100                                                              this.numCpus,
101                                                              this.numNetworkDisks,
102                                                              this.numLocalDisks,
103                                                              this.prefix);
104        this.generateDeployment(new String[] { "id" }, new int[] { 3 }, topology);
105        final Set<String> set = new HashSet<String>();
106        for (final VMDeploymentSpecification spec : this.getSpecs()) {
107            set.add(spec.getHost());
108        }
109        Assert.assertEquals(3, set.size());
110    }
111
112    @Test
113    public void testNotEnoughHostsSimpleTopology() throws CacheException, DeployerException {
114        final AggregatedLocalityPolicy policy = (AggregatedLocalityPolicy) this.getPolicy();
115        policy.setMaxVMsPerHost(1);
116        boolean caught = false;
117        try {
118            this.generateDeployment(new String[] { "id" }, new int[] { 3 }, new int[] { 1 });
119        } catch (final DeployerException e) {
120            caught = true;
121        }
122        Assert.assertTrue(caught);
123    }
124
125    @Test
126    public void testExistingVMsMakeItNotFitSimpleTopology() throws CacheException,
127            DeployerException {
128        final AggregatedLocalityPolicy policy = (AggregatedLocalityPolicy) this.getPolicy();
129        policy.setMaxVMsPerHost(1);
130        final PhysicalTopology topology = this.createTopology(1, 1, 1, 1, 1, this.prefix);
131        final String hostname = topology.getCPUs().get(0).getUniqueIdentifier();
132        final Application app = new ApplicationBuilder().withUuid("uuid").build();
133        final List<Deployment> deps = new ArrayList<Deployment>();
134        final List<VirtualMachine> vms = new ArrayList<VirtualMachine>();
135        final List<String> vmIds = new ArrayList<String>();
136        final Deployment dep = new DeploymentBuilder().withUuid("id").build();
137        final VirtualMachine vm = new VirtualMachineBuilder().withUuid("vmId").withHost(hostname)
138                .build();
139        final VMDeploymentSpecification spec = PowerMockito.mock(VMDeploymentSpecification.class);
140        final VMTemplate template = this.mockTemplate("id", 1, topology);
141        vmIds.add(vm.getUniqueIdentifier());
142        Mockito.when(spec.getTemplate()).thenReturn(template);
143        Mockito.when(this.reqSender.getVirtualMachines()).thenReturn(vmIds);
144        Mockito.when(this.reqSender.getVirtualMachine(vm.getUniqueIdentifier())).thenReturn(vm);
145        vms.add(vm);
146        dep.addVirtualMachines(vm, spec);
147        deps.add(dep);
148        app.putDeployment(dep);
149
150        boolean caught = false;
151        try {
152            this.generateDeployment(this.mockRequest(app));
153        } catch (final DeployerException e) {
154            caught = true;
155        }
156        Assert.assertTrue(caught);
157    }
158}