use of com.mesosphere.sdk.offer.OfferRecommendation 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 com.mesosphere.sdk.offer.OfferRecommendation in project dcos-commons by mesosphere.
the class OfferEvaluatorPortsTest method testReserveTaskDynamicVIPPortCustomExecutor.
@SuppressWarnings("PMD.AvoidUsingHardCodedIP")
@Test
public void testReserveTaskDynamicVIPPortCustomExecutor() throws Exception {
useCustomExecutor();
List<OfferRecommendation> recommendations = evaluator.evaluate(PodInstanceRequirementTestUtils.getVIPRequirement(80, 0), OfferTestUtils.getOffers(ResourceTestUtils.getUnreservedPorts(10000, 10000)));
Assert.assertEquals(2, recommendations.size());
Operation launchOperation = recommendations.get(1).getOperation();
TaskInfo taskInfo = launchOperation.getLaunch().getTaskInfos(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 + "-0:80");
Map<String, String> envvars = EnvUtils.toMap(TaskPackingUtils.unpack(taskInfo).getCommand().getEnvironment());
Assert.assertEquals(String.valueOf(10000), envvars.get(TestConstants.PORT_ENV_NAME + "_VIP_0"));
}
use of com.mesosphere.sdk.offer.OfferRecommendation in project dcos-commons by mesosphere.
the class OfferEvaluatorPortsTest method testUpdateDynamicToStaticPortCustomExecutor.
@Test
public void testUpdateDynamicToStaticPortCustomExecutor() throws Exception {
useCustomExecutor();
// Launch for the first time: get port 555 from dynamic port
Resource reserveResource = recordLaunchWithOfferedResources(PodInstanceRequirementTestUtils.getPortRequirement(0), 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))));
// 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, 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"));
}
use of com.mesosphere.sdk.offer.OfferRecommendation in project dcos-commons by mesosphere.
the class OfferEvaluatorPortsTest method testReserveTaskMultipleDynamicPortsCustomExecutor.
@Test
public void testReserveTaskMultipleDynamicPortsCustomExecutor() throws Exception {
useCustomExecutor();
String portenv0 = TestConstants.PORT_ENV_NAME + "_DYN_ZERO";
String portenv1 = TestConstants.PORT_ENV_NAME + "_DYN_ONE";
Map<String, Integer> ports = new HashMap<>();
ports.put(portenv0, 0);
ports.put(portenv1, 0);
List<OfferRecommendation> recommendations = evaluator.evaluate(PodInstanceRequirementTestUtils.getPortRequirement(ports), OfferTestUtils.getOffers(ResourceTestUtils.getUnreservedPorts(10000, 10001)));
Assert.assertEquals(recommendations.toString(), 3, recommendations.size());
Operation reserveOperation = recommendations.get(0).getOperation();
Resource fulfilledPortResource1 = reserveOperation.getReserve().getResources(0);
Assert.assertEquals(10000, fulfilledPortResource1.getRanges().getRange(0).getBegin());
Assert.assertEquals(10000, fulfilledPortResource1.getRanges().getRange(0).getEnd());
reserveOperation = recommendations.get(1).getOperation();
Resource fulfilledPortResource2 = reserveOperation.getReserve().getResources(0);
Assert.assertEquals(10001, fulfilledPortResource2.getRanges().getRange(0).getBegin());
Assert.assertEquals(10001, fulfilledPortResource2.getRanges().getRange(0).getEnd());
Operation launchOperation = recommendations.get(2).getOperation();
TaskInfo taskInfo = launchOperation.getLaunch().getTaskInfos(0);
Assert.assertEquals(getResourceId(taskInfo.getResources(0)), getResourceId(fulfilledPortResource1));
Assert.assertEquals(getResourceId(taskInfo.getResources(1)), getResourceId(fulfilledPortResource2));
Map<String, String> envvars = EnvUtils.toMap(TaskPackingUtils.unpack(taskInfo).getCommand().getEnvironment());
Assert.assertEquals(String.valueOf(10000), envvars.get(portenv0));
Assert.assertEquals(String.valueOf(10001), envvars.get(portenv1));
Assert.assertEquals(10000, taskInfo.getResources(0).getRanges().getRange(0).getBegin());
Assert.assertEquals(10000, taskInfo.getResources(0).getRanges().getRange(0).getEnd());
Assert.assertEquals(10001, taskInfo.getResources(1).getRanges().getRange(0).getBegin());
Assert.assertEquals(10001, taskInfo.getResources(1).getRanges().getRange(0).getEnd());
}
use of com.mesosphere.sdk.offer.OfferRecommendation in project dcos-commons by mesosphere.
the class OfferEvaluatorPortsTest method testReserveTaskDynamicVIPPort.
@SuppressWarnings("PMD.AvoidUsingHardCodedIP")
@Test
public void testReserveTaskDynamicVIPPort() throws Exception {
List<OfferRecommendation> recommendations = evaluator.evaluate(PodInstanceRequirementTestUtils.getVIPRequirement(80, 0), 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 + "-0:80");
Map<String, String> envvars = EnvUtils.toMap(TaskPackingUtils.unpack(taskInfo).getCommand().getEnvironment());
Assert.assertEquals(String.valueOf(10000), envvars.get(TestConstants.PORT_ENV_NAME + "_VIP_0"));
}
Aggregations