AvailabilityZoneConstraint implements ConstraintEvaluator { @Override public Result evaluate(TaskRequest taskRequest, VirtualMachineCurrentState targetVM, TaskTrackerState taskTrackerState) { Optional<AgentInstance> instanceOpt = SchedulerUtils.findInstance(agentManagementService, configuration.getInstanceAttributeName(), targetVM); if (!instanceOpt.isPresent()) { return MACHINE_DOES_NOT_EXIST; } AgentInstance agentInstance = instanceOpt.get(); String instanceAvailabilityZone = agentInstance.getAttributes().getOrDefault(configuration.getAvailabilityZoneAttributeName(), ""); return instanceAvailabilityZone.equalsIgnoreCase(availabilityZone) ? VALID : AVAILABILITY_ZONE_DOES_NOT_MATCH; } AvailabilityZoneConstraint(SchedulerConfiguration configuration, AgentManagementService agentManagementService, String availabilityZone); @Override String getName(); @Override Result evaluate(TaskRequest taskRequest, VirtualMachineCurrentState targetVM, TaskTrackerState taskTrackerState); static final String NAME; }
@Test public void machineDoesNotExist() { when(agentManagementService.findAgentInstance(MACHINE_ID)).thenReturn(Optional.empty()); Result result = constraint.evaluate(createTaskRequest(TASK_ID), createVirtualMachineCurrentStateMock(MACHINE_ID), createTaskTrackerState()); assertThat(result.isSuccessful()).isFalse(); assertThat(result.getFailureReason()).isEqualToIgnoringCase("The machine does not exist"); }
@Test public void availabilityZoneDoesNotMatch() { AgentInstance instance = AgentGenerator.agentInstances().getValue().toBuilder() .withId(MACHINE_ID) .withAttributes(Collections.singletonMap(AVAILABILITY_ZONE_ATTRIBUTE_NAME, "noMatch")) .build(); when(agentManagementService.findAgentInstance(MACHINE_ID)).thenReturn(Optional.of(instance)); Result result = constraint.evaluate(createTaskRequest(TASK_ID), createVirtualMachineCurrentStateMock(MACHINE_ID), createTaskTrackerState()); assertThat(result.isSuccessful()).isFalse(); assertThat(result.getFailureReason()).isEqualToIgnoringCase("The availability zone does not match the specified availability zone"); }
@Test public void availabilityZoneDoesMatch() { AgentInstance instance = AgentGenerator.agentInstances().getValue().toBuilder() .withId(MACHINE_ID) .withAttributes(Collections.singletonMap(AVAILABILITY_ZONE_ATTRIBUTE_NAME, AVAILABILITY_ZONE)) .build(); when(agentManagementService.findAgentInstance(MACHINE_ID)).thenReturn(Optional.of(instance)); Result result = constraint.evaluate(createTaskRequest(TASK_ID), createVirtualMachineCurrentStateMock(MACHINE_ID), createTaskTrackerState()); assertThat(result.isSuccessful()).isTrue(); }