Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
DCOS-47365: Ensure port-range is an inclusive range. (#2891)
Browse files Browse the repository at this point in the history
* DCOS-47365: Ensure port-range is an inclusive range.
Related to #2888 and #2890

* DCOS-47365: Added tests to ensure both ports on the inclusive range are correctly covered.
  • Loading branch information
kaiwalyajoshi authored Jan 18, 2019
1 parent 3923bf0 commit da93d0a
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ private static Optional<Integer> selectDynamicPort(
List<Integer> constrainedPorts = new ArrayList<>();
for (RangeSpec range : spec.getRanges()) {
constrainedPorts.addAll(
IntStream.range(range.getBegin(), range.getEnd())
IntStream.rangeClosed(range.getBegin(), range.getEnd())
.boxed()
.collect(Collectors.toList()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -782,4 +782,92 @@ public void testDynamicPortWithUnboundedRanges() throws Exception {
EvaluationOutcome outcome = portEvaluationStage.evaluate(mesosResourcePool, podInfoBuilder);
Assert.assertEquals(true, outcome.isPassing());
}

@Test
public void testDynamicPortWithRangeInclusiveUpperbound() throws Exception {
Protos.Resource offeredPorts = ResourceTestUtils.getPrereservedPort(3000, 5050, "slave_public");
Protos.Offer offer = OfferTestUtils.getCompleteOffer(Arrays.asList(offeredPorts), "slave_public");

//only the upperbound port of 3000 should match
RangeSpec spec1 = new RangeSpec(2000, 3000);

PortSpec.Builder builder = PortSpec.newBuilder()
.envKey("PORT_TEST")
.portName("TEST")
.ranges(Arrays.asList(spec1))
.visibility(TestConstants.PORT_VISIBILITY)
.networkNames(Collections.emptyList());
builder
.value(getPort(0))
.role(TestConstants.ROLE)
.preReservedRole("slave_public")
.principal(TestConstants.PRINCIPAL);
PortSpec portSpec = builder.build();

PodInstanceRequirement podInstanceRequirement = getPodInstanceWithPrereservedRole(portSpec);

PodInfoBuilder podInfoBuilder = new PodInfoBuilder(
podInstanceRequirement,
TestConstants.SERVICE_NAME,
UUID.randomUUID(),
PodTestUtils.getTemplateUrlFactory(),
SchedulerConfigTestUtils.getTestSchedulerConfig(),
Collections.emptyList(),
TestConstants.FRAMEWORK_ID,
Collections.emptyMap());

PortEvaluationStage portEvaluationStage = new PortEvaluationStage(
portSpec,
Collections.singleton(TestConstants.TASK_NAME),
Optional.empty(),
Optional.empty());

MesosResourcePool mesosResourcePool = new MesosResourcePool(offer, Optional.of("slave_public"));
EvaluationOutcome outcome = portEvaluationStage.evaluate(mesosResourcePool, podInfoBuilder);
Assert.assertEquals(true, outcome.isPassing());
}

@Test
public void testDynamicPortWithRangeInclusiveLowerbound() throws Exception {
Protos.Resource offeredPorts = ResourceTestUtils.getPrereservedPort(3000, 5050, "slave_public");
Protos.Offer offer = OfferTestUtils.getCompleteOffer(Arrays.asList(offeredPorts), "slave_public");

//only the lowerbound port of 5050 should match
RangeSpec spec1 = new RangeSpec(5050, 6000);

PortSpec.Builder builder = PortSpec.newBuilder()
.envKey("PORT_TEST")
.portName("TEST")
.ranges(Arrays.asList(spec1))
.visibility(TestConstants.PORT_VISIBILITY)
.networkNames(Collections.emptyList());
builder
.value(getPort(0))
.role(TestConstants.ROLE)
.preReservedRole("slave_public")
.principal(TestConstants.PRINCIPAL);
PortSpec portSpec = builder.build();

PodInstanceRequirement podInstanceRequirement = getPodInstanceWithPrereservedRole(portSpec);

PodInfoBuilder podInfoBuilder = new PodInfoBuilder(
podInstanceRequirement,
TestConstants.SERVICE_NAME,
UUID.randomUUID(),
PodTestUtils.getTemplateUrlFactory(),
SchedulerConfigTestUtils.getTestSchedulerConfig(),
Collections.emptyList(),
TestConstants.FRAMEWORK_ID,
Collections.emptyMap());

PortEvaluationStage portEvaluationStage = new PortEvaluationStage(
portSpec,
Collections.singleton(TestConstants.TASK_NAME),
Optional.empty(),
Optional.empty());

MesosResourcePool mesosResourcePool = new MesosResourcePool(offer, Optional.of("slave_public"));
EvaluationOutcome outcome = portEvaluationStage.evaluate(mesosResourcePool, podInfoBuilder);
Assert.assertEquals(true, outcome.isPassing());
}
}

0 comments on commit da93d0a

Please sign in to comment.