use of org.apache.mesos.Protos.Resource in project dcos-commons by mesosphere.
the class OfferEvaluatorVolumesTest method testExpectedMountVolume.
@Test
public void testExpectedMountVolume() throws Exception {
// Launch for the first time.
Resource offeredCpuResource = ResourceTestUtils.getUnreservedCpus(1.0);
Resource offeredDiskResource = ResourceTestUtils.getUnreservedMountVolume(2000);
PodInstanceRequirement podInstanceRequirement = PodInstanceRequirementTestUtils.getMountVolumeRequirement(1.0, 1500);
List<OfferRecommendation> recommendations = evaluator.evaluate(podInstanceRequirement, Arrays.asList(OfferTestUtils.getCompleteOffer(Arrays.asList(offeredDiskResource, offeredCpuResource))));
String cpuResourceId = ResourceTestUtils.getResourceId(recommendations.get(0).getOperation().getReserve().getResources(0));
Resource createResource = recommendations.get(2).getOperation().getCreate().getVolumes(0);
String executorCpuResourceId = ResourceTestUtils.getResourceId(recommendations.get(3).getOperation().getReserve().getResources(0));
String executorMemResourceId = ResourceTestUtils.getResourceId(recommendations.get(4).getOperation().getReserve().getResources(0));
String executorDiskResourceId = ResourceTestUtils.getResourceId(recommendations.get(5).getOperation().getReserve().getResources(0));
String diskResourceId = ResourceTestUtils.getResourceId(createResource);
String persistenceId = ResourceTestUtils.getPersistenceId(createResource);
Operation launchOperation = recommendations.get(recommendations.size() - 1).getOperation();
Protos.ExecutorInfo executorInfo = launchOperation.getLaunchGroup().getExecutor();
Collection<Protos.TaskInfo> taskInfos = launchOperation.getLaunchGroup().getTaskGroup().getTasksList().stream().map(t -> t.toBuilder().setExecutor(executorInfo).build()).collect(Collectors.toList());
stateStore.storeTasks(taskInfos);
// Launch again on expected resources.
Resource expectedCpu = ResourceTestUtils.getReservedCpus(1.0, cpuResourceId);
Resource expectedDisk = ResourceTestUtils.getReservedMountVolume(2000, diskResourceId, persistenceId);
Resource expectedExecutorCpu = ResourceTestUtils.getReservedCpus(0.1, executorCpuResourceId);
Resource expectedExecutorMem = ResourceTestUtils.getReservedMem(32, executorMemResourceId);
Resource expectedExecutorDisk = ResourceTestUtils.getReservedDisk(256, executorDiskResourceId);
recommendations = evaluator.evaluate(podInstanceRequirement, Arrays.asList(OfferTestUtils.getCompleteOffer(Arrays.asList(expectedCpu, expectedDisk, expectedExecutorCpu, expectedExecutorMem, expectedExecutorDisk))));
Assert.assertEquals(1, recommendations.size());
launchOperation = recommendations.get(0).getOperation();
Protos.TaskInfo launchTask = launchOperation.getLaunchGroup().getTaskGroup().getTasks(0);
Assert.assertEquals(recommendations.toString(), 2, launchTask.getResourcesCount());
Resource launchResource = launchTask.getResources(1);
Assert.assertEquals(Operation.Type.LAUNCH_GROUP, launchOperation.getType());
Assert.assertEquals(2000, launchResource.getScalar().getValue(), 0.0);
OfferEvaluatorTest.validateRole(launchResource);
Assert.assertEquals(TestConstants.ROLE, ResourceUtils.getRole(launchResource));
Assert.assertEquals(TestConstants.MOUNT_ROOT, launchResource.getDisk().getSource().getMount().getRoot());
Assert.assertEquals(persistenceId, launchResource.getDisk().getPersistence().getId());
Assert.assertEquals(TestConstants.PRINCIPAL, launchResource.getDisk().getPersistence().getPrincipal());
validatePrincipal(launchResource);
Assert.assertEquals(diskResourceId, getResourceId(launchResource));
}
use of org.apache.mesos.Protos.Resource in project dcos-commons by mesosphere.
the class OfferEvaluatorVolumesTest method testFailToCreateVolumeWithWrongResource.
@Test
public void testFailToCreateVolumeWithWrongResource() throws Exception {
Resource wrongOfferedResource = ResourceTestUtils.getUnreservedMountVolume(2000);
Resource offeredCpu = ResourceTestUtils.getUnreservedCpus(1.0);
List<OfferRecommendation> recommendations = evaluator.evaluate(PodInstanceRequirementTestUtils.getRootVolumeRequirement(1.0, 1500), Arrays.asList(OfferTestUtils.getCompleteOffer(Arrays.asList(offeredCpu, wrongOfferedResource))));
Assert.assertEquals(0, recommendations.size());
Assert.assertEquals(0, recommendations.size());
}
use of org.apache.mesos.Protos.Resource in project dcos-commons by mesosphere.
the class OfferEvaluatorVolumesTest method testReserveCreateExecutorVolume.
@Test
public void testReserveCreateExecutorVolume() throws Exception {
List<Resource> offeredResources = Arrays.asList(ResourceTestUtils.getUnreservedMountVolume(2000), ResourceTestUtils.getUnreservedCpus(1.0));
Protos.Offer offer = OfferTestUtils.getCompleteOffer(offeredResources);
PodInstanceRequirement podInstanceRequirement = PodInstanceRequirementTestUtils.getExecutorRequirement(PodInstanceRequirementTestUtils.getCpuResourceSet(1.0), Arrays.asList(new DefaultVolumeSpec(1000, VolumeSpec.Type.MOUNT, TestConstants.CONTAINER_PATH, TestConstants.ROLE, Constants.ANY_ROLE, TestConstants.PRINCIPAL)), TestConstants.POD_TYPE, 0);
List<OfferRecommendation> recommendations = evaluator.evaluate(podInstanceRequirement, Arrays.asList(offer));
Assert.assertEquals(7, recommendations.size());
// Validate just the operations pertaining to the executor
// Validate RESERVE Operation
Operation reserveOperation = recommendations.get(0).getOperation();
Resource reserveResource = reserveOperation.getReserve().getResources(0);
Assert.assertEquals(Operation.Type.RESERVE, reserveOperation.getType());
Assert.assertEquals(2000, reserveResource.getScalar().getValue(), 0.0);
OfferEvaluatorTest.validateRole(reserveResource);
Assert.assertEquals(TestConstants.ROLE, ResourceUtils.getRole(reserveResource));
Assert.assertEquals(TestConstants.MOUNT_ROOT, reserveResource.getDisk().getSource().getMount().getRoot());
validatePrincipal(reserveResource);
Assert.assertEquals(36, getResourceId(reserveResource).length());
// Validate CREATE Operation
String resourceId = getResourceId(reserveResource);
Operation createOperation = recommendations.get(1).getOperation();
Resource createResource = createOperation.getCreate().getVolumes(0);
Assert.assertEquals(resourceId, getResourceId(createResource));
Assert.assertEquals(36, createResource.getDisk().getPersistence().getId().length());
Assert.assertEquals(TestConstants.MOUNT_ROOT, createResource.getDisk().getSource().getMount().getRoot());
Assert.assertEquals(TestConstants.PRINCIPAL, createResource.getDisk().getPersistence().getPrincipal());
Assert.assertTrue(createResource.getDisk().hasVolume());
}
use of org.apache.mesos.Protos.Resource in project dcos-commons by mesosphere.
the class OfferEvaluatorPortsTest method testUpdateDynamicToStaticPort.
@Test
public void testUpdateDynamicToStaticPort() throws Exception {
// Launch for the first time: get port 555 from dynamic port
Resource reserveResource = recordLaunchWithCompleteOfferedResources(PodInstanceRequirementTestUtils.getPortRequirement(0), ResourceTestUtils.getUnreservedPorts(555, 555)).get(0);
String resourceId = getResourceId(reserveResource);
Collection<Resource> expectedResources = getExpectedExecutorResources(stateStore.fetchTasks().iterator().next().getExecutor());
expectedResources.addAll(Arrays.asList(ResourceTestUtils.getReservedPorts(555, 555, resourceId), ResourceTestUtils.getUnreservedPorts(666, 666)));
// Now lets move to port 666:
List<OfferRecommendation> recommendations = evaluator.evaluate(PodInstanceRequirementTestUtils.getPortRequirement(666), Arrays.asList(OfferTestUtils.getOffer(expectedResources)));
// RESERVE, UNRESERVE, LAUNCH
Assert.assertEquals(recommendations.toString(), 3, recommendations.size());
Assert.assertEquals(Operation.Type.UNRESERVE, recommendations.get(0).getOperation().getType());
Assert.assertEquals(Operation.Type.RESERVE, recommendations.get(1).getOperation().getType());
Assert.assertEquals(Operation.Type.LAUNCH_GROUP, recommendations.get(2).getOperation().getType());
Operation launchOperation = recommendations.get(2).getOperation();
TaskInfo taskInfo = launchOperation.getLaunchGroup().getTaskGroup().getTasks(0);
Map<String, String> envvars = EnvUtils.toMap(TaskPackingUtils.unpack(taskInfo).getCommand().getEnvironment());
Assert.assertEquals(String.valueOf(666), envvars.get(TestConstants.PORT_ENV_NAME + "_666"));
}
use of org.apache.mesos.Protos.Resource in project dcos-commons by mesosphere.
the class OfferEvaluatorPortsTest method getExpectedExecutorResources.
private Collection<Resource> getExpectedExecutorResources(Protos.ExecutorInfo executorInfo) {
String executorCpuId = executorInfo.getResourcesList().stream().filter(r -> r.getName().equals("cpus")).map(ResourceUtils::getResourceId).filter(o -> o.isPresent()).map(o -> o.get()).findFirst().get();
String executorMemId = executorInfo.getResourcesList().stream().filter(r -> r.getName().equals("mem")).map(ResourceUtils::getResourceId).filter(o -> o.isPresent()).map(o -> o.get()).findFirst().get();
String executorDiskId = executorInfo.getResourcesList().stream().filter(r -> r.getName().equals("disk")).map(ResourceUtils::getResourceId).filter(o -> o.isPresent()).map(o -> o.get()).findFirst().get();
Resource expectedExecutorCpu = ResourceTestUtils.getReservedCpus(0.1, executorCpuId);
Resource expectedExecutorMem = ResourceTestUtils.getReservedMem(32, executorMemId);
Resource expectedExecutorDisk = ResourceTestUtils.getReservedDisk(256, executorDiskId);
return new ArrayList<>(Arrays.asList(expectedExecutorCpu, expectedExecutorMem, expectedExecutorDisk));
}
Aggregations