use of com.mesosphere.sdk.scheduler.plan.DefaultPodInstance in project dcos-commons by mesosphere.
the class PortEvaluationStageTest method testHealthCheckPortEnvvarIsCorrectOnOverlay.
@Test
public void testHealthCheckPortEnvvarIsCorrectOnOverlay() throws Exception {
DefaultPodInstance podInstance = getPodInstance("valid-port-healthcheck-overlay.yml");
PodInstanceRequirement podInstanceRequirement = PodInstanceRequirement.newBuilder(podInstance, TaskUtils.getTaskNames(podInstance)).build();
PodInfoBuilder podInfoBuilder = getPodInfoBuilder(podInstanceRequirement, true);
Protos.Resource offeredPorts = ResourceTestUtils.getUnreservedPorts(10000, 10000);
Protos.Offer offer = OfferTestUtils.getOffer(offeredPorts);
PortEvaluationStage portEvaluationStage = new PortEvaluationStage(getPortSpec(podInstance), TestConstants.TASK_NAME, Optional.empty(), Optional.empty());
EvaluationOutcome outcome = portEvaluationStage.evaluate(new MesosResourcePool(offer, Optional.of(Constants.ANY_ROLE)), podInfoBuilder);
Assert.assertTrue(outcome.isPassing());
Assert.assertEquals(0, outcome.getOfferRecommendations().size());
Protos.TaskInfo.Builder taskBuilder = podInfoBuilder.getTaskBuilders().stream().findFirst().get();
Assert.assertTrue(taskBuilder.getCommand().getEnvironment().getVariablesList().stream().filter(variable -> variable.getName().equals("PORT_TEST_PORT") && variable.getValue().equals("10000")).count() == 1);
Assert.assertTrue(taskBuilder.getHealthCheck().getCommand().getEnvironment().getVariablesList().stream().filter(variable -> variable.getName().equals("PORT_TEST_PORT") && variable.getValue().equals("10000")).count() == 1);
}
use of com.mesosphere.sdk.scheduler.plan.DefaultPodInstance in project dcos-commons by mesosphere.
the class PortEvaluationStageTest method getPodInstance.
private DefaultPodInstance getPodInstance(String serviceSpecFileName) throws Exception {
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource(serviceSpecFileName).getFile());
DefaultServiceSpec serviceSpec = DefaultServiceSpec.newGenerator(file, SCHEDULER_CONFIG).build();
PodSpec podSpec = DefaultPodSpec.newBuilder(serviceSpec.getPods().get(0)).placementRule(TestPlacementUtils.PASS).build();
serviceSpec = DefaultServiceSpec.newBuilder(serviceSpec).pods(Arrays.asList(podSpec)).build();
return new DefaultPodInstance(serviceSpec.getPods().get(0), 0);
}
use of com.mesosphere.sdk.scheduler.plan.DefaultPodInstance in project dcos-commons by mesosphere.
the class PortEvaluationStageTest method testPortEnvvarOnReadinessCheck.
@Test
public void testPortEnvvarOnReadinessCheck() throws Exception {
DefaultPodInstance podInstance = getPodInstance("valid-port-readinesscheck.yml");
PodInstanceRequirement podInstanceRequirement = PodInstanceRequirement.newBuilder(podInstance, TaskUtils.getTaskNames(podInstance)).build();
PodInfoBuilder podInfoBuilder = getPodInfoBuilder(podInstanceRequirement, true);
Protos.Resource offeredPorts = ResourceTestUtils.getUnreservedPorts(10000, 10000);
Protos.Offer offer = OfferTestUtils.getOffer(offeredPorts);
PortEvaluationStage portEvaluationStage = new PortEvaluationStage(getPortSpec(podInstance), TestConstants.TASK_NAME, Optional.empty(), Optional.empty());
EvaluationOutcome outcome = portEvaluationStage.evaluate(new MesosResourcePool(offer, Optional.of(Constants.ANY_ROLE)), podInfoBuilder);
Assert.assertTrue(outcome.isPassing());
Assert.assertEquals(1, outcome.getOfferRecommendations().size());
OfferRecommendation recommendation = outcome.getOfferRecommendations().iterator().next();
Assert.assertEquals(Protos.Offer.Operation.Type.RESERVE, recommendation.getOperation().getType());
Protos.Resource resource = recommendation.getOperation().getReserve().getResources(0);
Assert.assertEquals(10000, resource.getRanges().getRange(0).getBegin(), resource.getRanges().getRange(0).getEnd());
Protos.TaskInfo.Builder taskBuilder = podInfoBuilder.getTaskBuilders().stream().findFirst().get();
boolean portInTaskEnv = false;
for (int i = 0; i < taskBuilder.getCommand().getEnvironment().getVariablesCount(); i++) {
Protos.Environment.Variable variable = taskBuilder.getCommand().getEnvironment().getVariables(i);
if (Objects.equals(variable.getName(), "PORT_TEST_PORT")) {
Assert.assertEquals(variable.getValue(), "10000");
portInTaskEnv = true;
}
}
Assert.assertTrue(portInTaskEnv);
boolean portInHealthEnv = false;
Protos.CheckInfo readinessCheck = taskBuilder.getCheck();
for (int i = 0; i < readinessCheck.getCommand().getCommand().getEnvironment().getVariablesCount(); i++) {
Protos.Environment.Variable variable = readinessCheck.getCommand().getCommand().getEnvironment().getVariables(i);
if (Objects.equals(variable.getName(), "PORT_TEST_PORT")) {
Assert.assertEquals(variable.getValue(), "10000");
portInHealthEnv = true;
}
}
Assert.assertTrue(portInHealthEnv);
}
use of com.mesosphere.sdk.scheduler.plan.DefaultPodInstance in project dcos-commons by mesosphere.
the class PlacementRuleEvaluationStageTest method testOfferPassesPlacementRule.
@Test
public void testOfferPassesPlacementRule() throws Exception {
String agent = "test-agent";
Protos.Resource offered = ResourceTestUtils.getUnreservedCpus(1.0);
PlacementRule rule = AgentRule.require(agent);
Protos.Offer offer = offerWithAgent(agent, offered);
MesosResourcePool mesosResourcePool = new MesosResourcePool(offer, Optional.of(Constants.ANY_ROLE));
PodSpec podSpec = PodInstanceRequirementTestUtils.getCpuRequirement(1.0).getPodInstance().getPod();
DefaultPodSpec.newBuilder(podSpec).placementRule(rule);
PodInstance podInstance = new DefaultPodInstance(podSpec, 0);
List<String> taskNames = TaskUtils.getTaskNames(podInstance);
PodInstanceRequirement podInstanceRequirement = PodInstanceRequirement.newBuilder(podInstance, taskNames).build();
PlacementRuleEvaluationStage placementRuleEvaluationStage = new PlacementRuleEvaluationStage(Collections.emptyList(), rule);
EvaluationOutcome outcome = placementRuleEvaluationStage.evaluate(mesosResourcePool, new PodInfoBuilder(podInstanceRequirement, TestConstants.SERVICE_NAME, UUID.randomUUID(), ArtifactResource.getUrlFactory(TestConstants.SERVICE_NAME), SchedulerConfigTestUtils.getTestSchedulerConfig(), Collections.emptyList(), TestConstants.FRAMEWORK_ID, true, Collections.emptyMap()));
Assert.assertTrue(outcome.isPassing());
Assert.assertEquals(3, mesosResourcePool.getUnreservedMergedPool().size());
Assert.assertTrue(Math.abs(mesosResourcePool.getUnreservedMergedPool().get("cpus").getScalar().getValue() - 1.1) < 0.01);
}
use of com.mesosphere.sdk.scheduler.plan.DefaultPodInstance in project dcos-commons by mesosphere.
the class OfferEvaluatorPlacementTest method testAvoidAgentsCustomExecutor.
@Test
public void testAvoidAgentsCustomExecutor() throws Exception {
useCustomExecutor();
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.getOffer(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(2, recommendations.size());
}
Aggregations