use of com.mesosphere.sdk.offer.OfferRecommendation in project dcos-commons by mesosphere.
the class OfferEvaluatorPortsTest method testReserveTaskNamedVIPPort.
@SuppressWarnings("PMD.AvoidUsingHardCodedIP")
@Test
public void testReserveTaskNamedVIPPort() throws Exception {
List<OfferRecommendation> recommendations = evaluator.evaluate(PodInstanceRequirementTestUtils.getVIPRequirement(80, 10000), OfferTestUtils.getCompleteOffers(ResourceTestUtils.getUnreservedPorts(10000, 10000)));
Assert.assertEquals(5, recommendations.size());
Operation launchOperation = recommendations.get(4).getOperation();
TaskInfo taskInfo = launchOperation.getLaunchGroup().getTaskGroup().getTasks(0);
Resource fulfilledPortResource = taskInfo.getResources(0);
Assert.assertEquals(10000, fulfilledPortResource.getRanges().getRange(0).getBegin());
Assert.assertFalse(getResourceId(fulfilledPortResource).isEmpty());
DiscoveryInfo discoveryInfo = taskInfo.getDiscovery();
Assert.assertEquals(discoveryInfo.getName(), taskInfo.getName());
Assert.assertEquals(discoveryInfo.getVisibility(), DiscoveryInfo.Visibility.CLUSTER);
Port discoveryPort = discoveryInfo.getPorts().getPorts(0);
Assert.assertEquals(discoveryPort.getProtocol(), "tcp");
Assert.assertEquals(discoveryPort.getVisibility(), DiscoveryInfo.Visibility.EXTERNAL);
Assert.assertEquals(discoveryPort.getNumber(), 10000);
Label vipLabel = discoveryPort.getLabels().getLabels(0);
Assert.assertTrue(vipLabel.getKey().startsWith("VIP_"));
Assert.assertEquals(vipLabel.getValue(), TestConstants.VIP_NAME + "-10000:80");
Map<String, String> envvars = EnvUtils.toMap(TaskPackingUtils.unpack(taskInfo).getCommand().getEnvironment());
Assert.assertEquals(String.valueOf(10000), envvars.get(TestConstants.PORT_ENV_NAME + "_VIP_10000"));
}
use of com.mesosphere.sdk.offer.OfferRecommendation in project dcos-commons by mesosphere.
the class OfferEvaluationUtilsTest method testResourceSufficient.
private void testResourceSufficient(Optional<String> resourceId, Optional<String> namespace) {
Protos.Value desired = getValue(5);
ResourceSpec resourceSpec = getResourceSpec(desired);
if (resourceId.isPresent()) {
when(mockPool.consumeReserved(RESOURCE_NAME, desired, resourceId.get())).thenReturn(Optional.of(getMesosResource(resourceSpec, resourceId.get(), namespace)));
} else {
when(mockPool.consumeReservableMerged(RESOURCE_NAME, desired, Constants.ANY_ROLE)).thenReturn(Optional.of(getMesosResource(desired)));
}
ReserveEvaluationOutcome outcome = OfferEvaluationUtils.evaluateSimpleResource(mockStage, resourceSpec, resourceId, namespace, mockPool);
Assert.assertTrue(outcome.getEvaluationOutcome().isPassing());
if (resourceId.isPresent()) {
Assert.assertTrue(outcome.getEvaluationOutcome().getOfferRecommendations().isEmpty());
Assert.assertEquals(resourceId.get(), outcome.getResourceId().get());
} else {
OfferRecommendation recommendation = outcome.getEvaluationOutcome().getOfferRecommendations().get(0);
Assert.assertTrue(recommendation instanceof ReserveOfferRecommendation);
Assert.assertTrue(outcome.getResourceId().isPresent());
Protos.Resource resource = recommendation.getOperation().getReserve().getResources(0);
Assert.assertEquals(desired.getScalar(), resource.getScalar());
if (namespace.isPresent()) {
Assert.assertEquals(namespace.get(), ResourceUtils.getNamespace(resource).get());
} else {
Assert.assertFalse(ResourceUtils.getNamespace(resource).isPresent());
}
}
}
use of com.mesosphere.sdk.offer.OfferRecommendation in project dcos-commons by mesosphere.
the class OfferEvaluatorPlacementTest method testColocateAgents.
@Test
public void testColocateAgents() throws Exception {
Protos.Resource offeredCpu = ResourceTestUtils.getUnreservedCpus(2.0);
// Don't launch
PlacementRule placementRule = PlacementUtils.getAgentPlacementRule(Collections.emptyList(), Arrays.asList("some-random-agent")).get();
PodSpec podSpec = PodInstanceRequirementTestUtils.getCpuRequirement(1.0).getPodInstance().getPod();
podSpec = DefaultPodSpec.newBuilder(podSpec).placementRule(placementRule).build();
PodInstance podInstance = new DefaultPodInstance(podSpec, 0);
PodInstanceRequirement podInstanceRequirement = PodInstanceRequirement.newBuilder(podInstance, Arrays.asList(TestConstants.TASK_NAME)).build();
List<OfferRecommendation> recommendations = evaluator.evaluate(podInstanceRequirement, Arrays.asList(OfferTestUtils.getCompleteOffer(offeredCpu)));
Assert.assertEquals(0, recommendations.size());
// Launch
placementRule = PlacementUtils.getAgentPlacementRule(Collections.emptyList(), Arrays.asList(TestConstants.AGENT_ID.getValue())).get();
podSpec = DefaultPodSpec.newBuilder(podSpec).placementRule(placementRule).build();
podInstance = new DefaultPodInstance(podSpec, 0);
podInstanceRequirement = PodInstanceRequirement.newBuilder(podInstance, Arrays.asList(TestConstants.TASK_NAME)).build();
recommendations = evaluator.evaluate(podInstanceRequirement, Arrays.asList(OfferTestUtils.getCompleteOffer(offeredCpu)));
Assert.assertEquals(5, recommendations.size());
}
use of com.mesosphere.sdk.offer.OfferRecommendation in project dcos-commons by mesosphere.
the class OfferEvaluatorPlacementTest method testAvoidAgents.
@Test
public void testAvoidAgents() throws Exception {
Protos.Resource offeredCpu = ResourceTestUtils.getUnreservedCpus(2.0);
// Don't launch
PlacementRule placementRule = PlacementUtils.getAgentPlacementRule(Arrays.asList(TestConstants.AGENT_ID.getValue()), Collections.emptyList()).get();
PodSpec podSpec = PodInstanceRequirementTestUtils.getCpuRequirement(1.0).getPodInstance().getPod();
podSpec = DefaultPodSpec.newBuilder(podSpec).placementRule(placementRule).build();
PodInstance podInstance = new DefaultPodInstance(podSpec, 0);
PodInstanceRequirement podInstanceRequirement = PodInstanceRequirement.newBuilder(podInstance, Arrays.asList(TestConstants.TASK_NAME)).build();
List<OfferRecommendation> recommendations = evaluator.evaluate(podInstanceRequirement, Arrays.asList(OfferTestUtils.getCompleteOffer(offeredCpu)));
Assert.assertEquals(0, recommendations.size());
// Launch
placementRule = PlacementUtils.getAgentPlacementRule(Arrays.asList("some-random-agent"), Collections.emptyList()).get();
podSpec = DefaultPodSpec.newBuilder(podSpec).placementRule(placementRule).build();
podInstance = new DefaultPodInstance(podSpec, 0);
podInstanceRequirement = PodInstanceRequirement.newBuilder(podInstance, Arrays.asList(TestConstants.TASK_NAME)).build();
recommendations = evaluator.evaluate(podInstanceRequirement, Arrays.asList(OfferTestUtils.getCompleteOffer(offeredCpu)));
Assert.assertEquals(5, recommendations.size());
}
use of com.mesosphere.sdk.offer.OfferRecommendation in project dcos-commons by mesosphere.
the class OfferEvaluatorPortsTest method testUpdateStaticToStaticPortCustomExecutor.
@Test
public void testUpdateStaticToStaticPortCustomExecutor() throws Exception {
useCustomExecutor();
// Launch for the first time: get port 555
Resource reserveResource = recordLaunchWithOfferedResources(PodInstanceRequirementTestUtils.getPortRequirement(555), ResourceTestUtils.getUnreservedPorts(555, 555)).get(0);
String resourceId = getResourceId(reserveResource);
// Now lets move to port 666:
List<OfferRecommendation> recommendations = evaluator.evaluate(PodInstanceRequirementTestUtils.getPortRequirement(666), OfferTestUtils.getOffers(Arrays.asList(ResourceTestUtils.getReservedPorts(555, 555, resourceId), ResourceTestUtils.getUnreservedPorts(666, 666))));
// UNRESERVE, RESERVE, 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, recommendations.get(2).getOperation().getType());
Operation launchOperation = recommendations.get(2).getOperation();
TaskInfo taskInfo = launchOperation.getLaunch().getTaskInfos(0);
Map<String, String> envvars = EnvUtils.toMap(TaskPackingUtils.unpack(taskInfo).getCommand().getEnvironment());
Assert.assertEquals(String.valueOf(666), envvars.get(TestConstants.PORT_ENV_NAME + "_666"));
}
Aggregations