From 431a7988fb1df1f9333a4a5dbffefb8924c4928b Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Fri, 9 Dec 2016 15:45:25 +0100 Subject: [PATCH] added more tests Original commit: elastic/x-pack-elasticsearch@871c13307ef56c852c5eb6c09f44dee0015c6a75 --- .../prelert/job/manager/JobManagerTests.java | 48 ----------- .../job/metadata/PrelertMetadataTests.java | 85 +++++++++++++++++++ 2 files changed, 85 insertions(+), 48 deletions(-) diff --git a/elasticsearch/src/test/java/org/elasticsearch/xpack/prelert/job/manager/JobManagerTests.java b/elasticsearch/src/test/java/org/elasticsearch/xpack/prelert/job/manager/JobManagerTests.java index 93533ba7ad3..1ebb9b3308b 100644 --- a/elasticsearch/src/test/java/org/elasticsearch/xpack/prelert/job/manager/JobManagerTests.java +++ b/elasticsearch/src/test/java/org/elasticsearch/xpack/prelert/job/manager/JobManagerTests.java @@ -5,7 +5,6 @@ */ package org.elasticsearch.xpack.prelert.job.manager; -import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.ResourceNotFoundException; import org.elasticsearch.cluster.ClusterName; import org.elasticsearch.cluster.ClusterState; @@ -13,12 +12,9 @@ import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; -import org.elasticsearch.rest.RestStatus; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.prelert.job.Job; -import org.elasticsearch.xpack.prelert.job.JobStatus; import org.elasticsearch.xpack.prelert.job.audit.Auditor; -import org.elasticsearch.xpack.prelert.job.metadata.Allocation; import org.elasticsearch.xpack.prelert.job.metadata.PrelertMetadata; import org.elasticsearch.xpack.prelert.job.persistence.JobProvider; import org.elasticsearch.xpack.prelert.job.persistence.JobResultsPersister; @@ -32,7 +28,6 @@ import java.util.stream.Collectors; import static org.elasticsearch.xpack.prelert.job.JobTests.buildJobBuilder; import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.is; import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -70,49 +65,6 @@ public class JobManagerTests extends ESTestCase { assertTrue(diff.contains("tom")); } - public void testRemoveJobFromClusterState_GivenExistingMetadata() { - JobManager jobManager = createJobManager(); - ClusterState clusterState = createClusterState(); - Job job = buildJobBuilder("foo").build(); - clusterState = jobManager.innerPutJob(job, false, clusterState); - - clusterState = jobManager.removeJobFromClusterState("foo", clusterState); - - PrelertMetadata prelertMetadata = clusterState.metaData().custom(PrelertMetadata.TYPE); - assertThat(prelertMetadata.getJobs().containsKey("foo"), is(false)); - } - - public void testRemoveJobFromClusterState_GivenJobIsOpened() { - JobManager jobManager = createJobManager(); - ClusterState clusterState = createClusterState(); - Job job = buildJobBuilder("foo").build(); - clusterState = jobManager.innerPutJob(job, false, clusterState); - Allocation.Builder allocation = new Allocation.Builder(); - allocation.setNodeId("myNode"); - allocation.setJobId(job.getId()); - allocation.setStatus(JobStatus.OPENING); - PrelertMetadata.Builder newMetadata = new PrelertMetadata.Builder(clusterState.metaData().custom(PrelertMetadata.TYPE)); - newMetadata.assignToNode(job.getId(), "myNode"); - newMetadata.updateAllocation(job.getId(), allocation.build()); - - ClusterState jobRunningClusterState = new ClusterState.Builder(clusterState) - .metaData(MetaData.builder().putCustom(PrelertMetadata.TYPE, newMetadata.build())).build(); - - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> jobManager.removeJobFromClusterState("foo", jobRunningClusterState)); - assertThat(e.status(), equalTo(RestStatus.CONFLICT)); - assertThat(e.getMessage(), equalTo("Cannot delete job 'foo' while it is OPENING")); - } - - public void testRemoveJobFromClusterState_jobMissing() { - JobManager jobManager = createJobManager(); - ClusterState clusterState = createClusterState(); - Job job = buildJobBuilder("foo").build(); - ClusterState clusterState2 = jobManager.innerPutJob(job, false, clusterState); - Exception e = expectThrows(ResourceNotFoundException.class, () -> jobManager.removeJobFromClusterState("bar", clusterState2)); - assertThat(e.getMessage(), equalTo("job [bar] does not exist")); - } - public void testGetJobOrThrowIfUnknown_GivenUnknownJob() { JobManager jobManager = createJobManager(); ClusterState cs = createClusterState(); diff --git a/elasticsearch/src/test/java/org/elasticsearch/xpack/prelert/job/metadata/PrelertMetadataTests.java b/elasticsearch/src/test/java/org/elasticsearch/xpack/prelert/job/metadata/PrelertMetadataTests.java index 2d6b28cef2c..047d7334371 100644 --- a/elasticsearch/src/test/java/org/elasticsearch/xpack/prelert/job/metadata/PrelertMetadataTests.java +++ b/elasticsearch/src/test/java/org/elasticsearch/xpack/prelert/job/metadata/PrelertMetadataTests.java @@ -5,7 +5,9 @@ */ package org.elasticsearch.xpack.prelert.job.metadata; +import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.ResourceAlreadyExistsException; +import org.elasticsearch.ResourceNotFoundException; import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.common.io.stream.InputStreamStreamInput; import org.elasticsearch.common.io.stream.OutputStreamStreamOutput; @@ -14,16 +16,19 @@ import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.rest.RestStatus; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.prelert.job.Job; import org.elasticsearch.xpack.prelert.job.JobStatus; import org.elasticsearch.xpack.prelert.job.JobTests; +import org.elasticsearch.xpack.prelert.job.SchedulerStatus; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import static org.elasticsearch.xpack.prelert.job.JobTests.buildJobBuilder; +import static org.elasticsearch.xpack.prelert.job.scheduler.ScheduledJobRunnerTests.createScheduledJob; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; @@ -100,7 +105,11 @@ public class PrelertMetadataTests extends ESTestCase { PrelertMetadata result = builder.build(); assertThat(result.getJobs().get("1"), sameInstance(job1)); + assertThat(result.getAllocations().get("1").getStatus(), equalTo(JobStatus.CLOSED)); + assertThat(result.getSchedulerStatuses().get("1"), nullValue()); assertThat(result.getJobs().get("2"), sameInstance(job2)); + assertThat(result.getAllocations().get("2").getStatus(), equalTo(JobStatus.CLOSED)); + assertThat(result.getSchedulerStatuses().get("2"), nullValue()); builder = new PrelertMetadata.Builder(result); @@ -116,6 +125,82 @@ public class PrelertMetadataTests extends ESTestCase { assertThat(result.getJobs().get("2"), sameInstance(job2Attempt2)); } + public void testRemoveJob() { + Job job1 = buildJobBuilder("1").build(); + PrelertMetadata.Builder builder = new PrelertMetadata.Builder(); + builder.putJob(job1, false); + + PrelertMetadata result = builder.build(); + assertThat(result.getJobs().get("1"), sameInstance(job1)); + assertThat(result.getAllocations().get("1").getStatus(), equalTo(JobStatus.CLOSED)); + assertThat(result.getSchedulerStatuses().get("1"), nullValue()); + + builder = new PrelertMetadata.Builder(result); + builder.removeJob("1"); + result = builder.build(); + assertThat(result.getJobs().get("1"), nullValue()); + assertThat(result.getAllocations().get("1"), nullValue()); + assertThat(result.getSchedulerStatuses().get("1"), nullValue()); + } + + public void testRemoveJob_failBecauseJobIsOpen() { + Job job1 = buildJobBuilder("1").build(); + PrelertMetadata.Builder builder1 = new PrelertMetadata.Builder(); + builder1.putJob(job1, false); + builder1.updateStatus("1", JobStatus.OPENING, null); + builder1.updateStatus("1", JobStatus.OPENED, null); + + PrelertMetadata result = builder1.build(); + assertThat(result.getJobs().get("1"), sameInstance(job1)); + assertThat(result.getAllocations().get("1").getStatus(), equalTo(JobStatus.OPENED)); + assertThat(result.getSchedulerStatuses().get("1"), nullValue()); + + PrelertMetadata.Builder builder2 = new PrelertMetadata.Builder(result); + ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, () -> builder2.removeJob("1")); + assertThat(e.status(), equalTo(RestStatus.CONFLICT)); + } + + public void testRemoveJob_failBecauseJobDoesNotExist() { + PrelertMetadata.Builder builder1 = new PrelertMetadata.Builder(); + expectThrows(ResourceNotFoundException.class, () -> builder1.removeJob("1")); + } + + public void testCrudScheduledJob() { + Job job1 = createScheduledJob().build(); + PrelertMetadata.Builder builder = new PrelertMetadata.Builder(); + builder.putJob(job1, false); + + PrelertMetadata result = builder.build(); + assertThat(result.getJobs().get("foo"), sameInstance(job1)); + assertThat(result.getAllocations().get("foo").getStatus(), equalTo(JobStatus.CLOSED)); + assertThat(result.getSchedulerStatuses().get("foo"), equalTo(SchedulerStatus.STOPPED)); + + builder = new PrelertMetadata.Builder(result); + builder.removeJob("foo"); + result = builder.build(); + assertThat(result.getJobs().get("foo"), nullValue()); + assertThat(result.getAllocations().get("foo"), nullValue()); + assertThat(result.getSchedulerStatuses().get("foo"), nullValue()); + } + + public void testDeletedScheduledJob_failBecauseSchedulerStarted() { + Job job1 = createScheduledJob().build(); + PrelertMetadata.Builder builder = new PrelertMetadata.Builder(); + builder.putJob(job1, false); + builder.updateStatus("foo", JobStatus.OPENING, null); + builder.updateStatus("foo", JobStatus.OPENED, null); + builder.updateSchedulerStatus("foo", SchedulerStatus.STARTED); + + PrelertMetadata result = builder.build(); + assertThat(result.getJobs().get("foo"), sameInstance(job1)); + assertThat(result.getAllocations().get("foo").getStatus(), equalTo(JobStatus.OPENED)); + assertThat(result.getSchedulerStatuses().get("foo"), equalTo(SchedulerStatus.STARTED)); + + PrelertMetadata.Builder builder2 = new PrelertMetadata.Builder(result); + ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, () -> builder2.removeJob("foo")); + assertThat(e.status(), equalTo(RestStatus.CONFLICT)); + } + public void testUpdateAllocation_setFinishedTime() { PrelertMetadata.Builder builder = new PrelertMetadata.Builder(); builder.putJob(buildJobBuilder("_job_id").build(), false);