MachineGroupConstraint 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(); return agentInstance.getInstanceGroupId().equalsIgnoreCase(machineGroup) ? VALID : MACHINE_GROUP_DOES_NOT_MATCH; } MachineGroupConstraint(SchedulerConfiguration configuration, AgentManagementService agentManagementService, String machineGroup); @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 machineGroupDoesNotMatch() { AgentInstance instance = AgentGenerator.agentInstances().getValue().toBuilder().withId(MACHINE_ID).withInstanceGroupId("").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 machine group does not match the specified name"); }
@Test public void machineGroupDoesMatch() { AgentInstance instance = AgentGenerator.agentInstances().getValue().toBuilder().withId(MACHINE_ID).withInstanceGroupId(MACHINE_GROUP).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(); }