Search in sources :

Example 1 with Resource

use of org.apache.mesos.Protos.Resource 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"));
}
Also used : TaskInfo(org.apache.mesos.Protos.TaskInfo) DiscoveryInfo(org.apache.mesos.Protos.DiscoveryInfo) Port(org.apache.mesos.Protos.Port) Resource(org.apache.mesos.Protos.Resource) Label(org.apache.mesos.Protos.Label) Operation(org.apache.mesos.Protos.Offer.Operation) OfferRecommendation(com.mesosphere.sdk.offer.OfferRecommendation) Test(org.junit.Test)

Example 2 with Resource

use of org.apache.mesos.Protos.Resource 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"));
}
Also used : TaskInfo(org.apache.mesos.Protos.TaskInfo) Resource(org.apache.mesos.Protos.Resource) Operation(org.apache.mesos.Protos.Offer.Operation) OfferRecommendation(com.mesosphere.sdk.offer.OfferRecommendation) Test(org.junit.Test)

Example 3 with Resource

use of org.apache.mesos.Protos.Resource in project dcos-commons by mesosphere.

the class OfferEvaluatorPortsTest method testLaunchExpectedMultiplePortsCustomExecutor.

@Test
public void testLaunchExpectedMultiplePortsCustomExecutor() throws Exception {
    useCustomExecutor();
    // Launch for the first time: get ports 10000,10001
    PodInstanceRequirement podInstanceRequirement = PodInstanceRequirementTestUtils.getPortRequirement(10000, 10001);
    List<Resource> reserveResources = recordLaunchWithOfferedResources(podInstanceRequirement, ResourceTestUtils.getUnreservedPorts(10000, 10001));
    Assert.assertEquals(reserveResources.toString(), 2, reserveResources.size());
    String resourceId0 = getResourceId(reserveResources.get(0));
    String resourceId1 = getResourceId(reserveResources.get(1));
    // Now try relaunch:
    List<OfferRecommendation> recommendations = evaluator.evaluate(PodInstanceRequirementTestUtils.getPortRequirement(10000, 10001), OfferTestUtils.getOffers(Arrays.asList(ResourceTestUtils.getReservedPorts(10000, 10000, resourceId0), ResourceTestUtils.getReservedPorts(10001, 10001, resourceId1))));
    Assert.assertEquals(1, recommendations.size());
    // Validate LAUNCH Operation
    Operation launchOperation = recommendations.get(0).getOperation();
    Assert.assertEquals(Operation.Type.LAUNCH, launchOperation.getType());
    List<Resource> launchResources = launchOperation.getLaunch().getTaskInfos(0).getResourcesList();
    Assert.assertEquals(launchResources.toString(), 2, launchResources.size());
    Assert.assertEquals(resourceId0, getResourceId(launchResources.get(0)));
    Assert.assertEquals(resourceId1, getResourceId(launchResources.get(1)));
}
Also used : Resource(org.apache.mesos.Protos.Resource) Operation(org.apache.mesos.Protos.Offer.Operation) PodInstanceRequirement(com.mesosphere.sdk.scheduler.plan.PodInstanceRequirement) OfferRecommendation(com.mesosphere.sdk.offer.OfferRecommendation) Test(org.junit.Test)

Example 4 with Resource

use of org.apache.mesos.Protos.Resource in project dcos-commons by mesosphere.

the class OfferEvaluatorPortsTest method testReserveTaskMultipleDynamicPorts.

@Test
public void testReserveTaskMultipleDynamicPorts() throws Exception {
    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.getCompleteOffers(ResourceTestUtils.getUnreservedPorts(10000, 10001)));
    Assert.assertEquals(recommendations.toString(), 6, 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(5).getOperation();
    TaskInfo taskInfo = launchOperation.getLaunchGroup().getTaskGroup().getTasks(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());
}
Also used : TaskInfo(org.apache.mesos.Protos.TaskInfo) HashMap(java.util.HashMap) Resource(org.apache.mesos.Protos.Resource) Operation(org.apache.mesos.Protos.Offer.Operation) OfferRecommendation(com.mesosphere.sdk.offer.OfferRecommendation) Test(org.junit.Test)

Example 5 with Resource

use of org.apache.mesos.Protos.Resource in project dcos-commons by mesosphere.

the class OfferEvaluatorPortsTest method testLaunchExpectedStaticPortCustomExecutor.

@Test
public void testLaunchExpectedStaticPortCustomExecutor() throws Exception {
    useCustomExecutor();
    // Launch for the first time: get port 555
    PodInstanceRequirement podInstanceRequirement = PodInstanceRequirementTestUtils.getPortRequirement(555);
    Resource reserveResource = recordLaunchWithOfferedResources(podInstanceRequirement, ResourceTestUtils.getUnreservedPorts(555, 555)).get(0);
    String resourceId = getResourceId(reserveResource);
    // Launch on previously reserved resources
    List<OfferRecommendation> recommendations = evaluator.evaluate(podInstanceRequirement, OfferTestUtils.getOffers(ResourceTestUtils.getReservedPorts(555, 555, resourceId)));
    Assert.assertEquals(1, recommendations.size());
    // Validate LAUNCH Operation
    Protos.Offer.Operation launchOperation = recommendations.get(0).getOperation();
    Assert.assertEquals(Protos.Offer.Operation.Type.LAUNCH, launchOperation.getType());
    Protos.Resource launchResource = launchOperation.getLaunch().getTaskInfos(0).getResources(0);
    Assert.assertEquals(resourceId, getResourceId(launchResource));
}
Also used : Operation(org.apache.mesos.Protos.Offer.Operation) Protos(org.apache.mesos.Protos) Resource(org.apache.mesos.Protos.Resource) Resource(org.apache.mesos.Protos.Resource) PodInstanceRequirement(com.mesosphere.sdk.scheduler.plan.PodInstanceRequirement) OfferRecommendation(com.mesosphere.sdk.offer.OfferRecommendation) Test(org.junit.Test)

Aggregations

Resource (org.apache.mesos.Protos.Resource)40 Test (org.junit.Test)35 OfferRecommendation (com.mesosphere.sdk.offer.OfferRecommendation)26 Operation (org.apache.mesos.Protos.Offer.Operation)25 PodInstanceRequirement (com.mesosphere.sdk.scheduler.plan.PodInstanceRequirement)13 Protos (org.apache.mesos.Protos)13 TaskInfo (org.apache.mesos.Protos.TaskInfo)12 Offer (org.apache.mesos.Protos.Offer)9 DefaultResourceSet (com.mesosphere.sdk.specification.DefaultResourceSet)4 DefaultVolumeSpec (com.mesosphere.sdk.specification.DefaultVolumeSpec)4 ResourceSet (com.mesosphere.sdk.specification.ResourceSet)4 DiscoveryInfo (org.apache.mesos.Protos.DiscoveryInfo)4 Label (org.apache.mesos.Protos.Label)4 Port (org.apache.mesos.Protos.Port)4 ResourceUtils (com.mesosphere.sdk.offer.ResourceUtils)3 PodInstanceRequirementTestUtils (com.mesosphere.sdk.scheduler.plan.PodInstanceRequirementTestUtils)3 OfferTestUtils (com.mesosphere.sdk.testutils.OfferTestUtils)3 ResourceTestUtils (com.mesosphere.sdk.testutils.ResourceTestUtils)3 TestConstants (com.mesosphere.sdk.testutils.TestConstants)3 Arrays (java.util.Arrays)3