DefaultLoadBalancerJobValidator implements LoadBalancerJobValidator { @Override public void validateJobId(String jobId) throws LoadBalancerException, JobManagerException { Job job = v3JobOperations.getJob(jobId).orElseThrow(() -> JobManagerException.v3JobNotFound(jobId)); JobState state = job.getStatus().getState(); if (state != JobState.Accepted) { throw JobManagerException.unexpectedJobState(job, JobState.Accepted); } JobDescriptor.JobDescriptorExt extensions = job.getJobDescriptor().getExtensions(); if (!(extensions instanceof ServiceJobExt)) { throw JobManagerException.notServiceJob(jobId); } int maxLoadBalancers = loadBalancerValidationConfiguration.getMaxLoadBalancersPerJob(); int numLoadBalancers = loadBalancerStore.getNumLoadBalancersForJob(jobId); if (numLoadBalancers > maxLoadBalancers) { throw LoadBalancerException.jobMaxLoadBalancers(jobId, numLoadBalancers, maxLoadBalancers); } } @Inject  DefaultLoadBalancerJobValidator(V3JobOperations v3JobOperations,
                                           LoadBalancerStore loadBalancerStore,
                                           LoadBalancerValidationConfiguration loadBalancerValidationConfiguration); @Override void validateJobId(String jobId);  }
@Test public void testValidateJobExists() throws Exception { when(jobOperations.getJob(JOB_ID)).thenReturn(Optional.empty()); Throwable thrown = catchThrowable(() -> loadBalancerValidator.validateJobId(JOB_ID)); assertThat(thrown).isInstanceOf(JobManagerException.class); assertThat(((JobManagerException) thrown).getErrorCode()).isEqualTo(JobManagerException.ErrorCode.JobNotFound); }
@Test public void testValidateJobAccepted() throws Exception { when(jobOperations.getJob(JOB_ID)).thenReturn(Optional.of(Job.newBuilder() .withId(JOB_ID) .withStatus(JobStatus.newBuilder() .withState(JobState.Finished) .build()) .build())); Throwable thrown = catchThrowable(() -> loadBalancerValidator.validateJobId(JOB_ID)); assertThat(thrown).isInstanceOf(JobManagerException.class); assertThat(((JobManagerException) thrown).getErrorCode()).isEqualTo(JobManagerException.ErrorCode.UnexpectedJobState); }
@Test public void testValidateJobIsService() throws Exception { when(jobOperations.getJob(JOB_ID)).thenReturn(Optional.of(Job.<BatchJobExt>newBuilder() .withId(JOB_ID) .withStatus(JobStatus.newBuilder() .withState(JobState.Accepted) .build()) .withJobDescriptor(JobDescriptor.<BatchJobExt>newBuilder() .build()) .build())); Throwable thrown = catchThrowable(() -> loadBalancerValidator.validateJobId(JOB_ID)); assertThat(thrown).isInstanceOf(JobManagerException.class); assertThat(((JobManagerException) thrown).getErrorCode()).isEqualTo(JobManagerException.ErrorCode.NotServiceJob); }
@Test public void testValidateMaxLoadBalancers() throws Exception { when(jobOperations.getJob(JOB_ID)).thenReturn(Optional.of(Job.<ServiceJobExt>newBuilder() .withId(JOB_ID) .withStatus(JobStatus.newBuilder() .withState(JobState.Accepted) .build()) .withJobDescriptor(JobDescriptor.<ServiceJobExt>newBuilder() .withExtensions(ServiceJobExt.newBuilder().build()) .withContainer(Container.newBuilder() .withImage(Image.newBuilder().build()) .withContainerResources(ContainerResources.newBuilder() .withAllocateIP(true) .build()) .build()) .build()) .build())); when(loadBalancerValidationConfiguration.getMaxLoadBalancersPerJob()).thenReturn(30); for (int i = 0; i < loadBalancerValidationConfiguration.getMaxLoadBalancersPerJob() + 1; i++) { loadBalancerStore.addOrUpdateLoadBalancer(new JobLoadBalancer(JOB_ID, "LoadBalancer-" + i), JobLoadBalancer.State.ASSOCIATED).await(TIMEOUT_MS, TimeUnit.MILLISECONDS); } Throwable thrown = catchThrowable(() -> loadBalancerValidator.validateJobId(JOB_ID)); assertThat(thrown).isInstanceOf(LoadBalancerException.class); assertThat(((LoadBalancerException) thrown).getErrorCode()).isEqualTo(LoadBalancerException.ErrorCode.JobMaxLoadBalancers); }
@Test public void testValidJob() throws Exception { when(jobOperations.getJob(JOB_ID)).thenReturn(Optional.of(Job.<ServiceJobExt>newBuilder() .withId(JOB_ID) .withStatus(JobStatus.newBuilder() .withState(JobState.Accepted) .build()) .withJobDescriptor(JobDescriptor.<ServiceJobExt>newBuilder() .withExtensions(ServiceJobExt.newBuilder().build()) .withContainer(Container.newBuilder() .withImage(Image.newBuilder().build()) .withContainerResources(ContainerResources.newBuilder() .withAllocateIP(true) .build()) .build()) .build()) .build())); assertThatCode(() -> loadBalancerValidator.validateJobId(JOB_ID)).doesNotThrowAnyException(); }