diff --git a/src/test/java/com/amazon/opendistroforelasticsearch/ad/HistoricalDetectorRestTestCase.java b/src/test/java/com/amazon/opendistroforelasticsearch/ad/HistoricalDetectorRestTestCase.java index 7c568ceb..8d6cd5f7 100644 --- a/src/test/java/com/amazon/opendistroforelasticsearch/ad/HistoricalDetectorRestTestCase.java +++ b/src/test/java/com/amazon/opendistroforelasticsearch/ad/HistoricalDetectorRestTestCase.java @@ -38,20 +38,16 @@ import com.amazon.opendistroforelasticsearch.ad.mock.model.MockSimpleLog; import com.amazon.opendistroforelasticsearch.ad.model.ADTaskProfile; -import com.amazon.opendistroforelasticsearch.ad.model.ADTaskState; import com.amazon.opendistroforelasticsearch.ad.model.AnomalyDetector; import com.amazon.opendistroforelasticsearch.ad.model.DetectionDateRange; import com.amazon.opendistroforelasticsearch.ad.model.Feature; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; public abstract class HistoricalDetectorRestTestCase extends AnomalyDetectorRestTestCase { protected String historicalDetectorTestIndex = "test_historical_detector_data"; protected int detectionIntervalInMinutes = 1; - protected ImmutableSet historicalDetectorRunningStats = ImmutableSet - .of(ADTaskState.CREATED.name(), ADTaskState.INIT.name(), ADTaskState.RUNNING.name()); @Before @Override diff --git a/src/test/java/com/amazon/opendistroforelasticsearch/ad/TestHelpers.java b/src/test/java/com/amazon/opendistroforelasticsearch/ad/TestHelpers.java index 8053a0ae..2c7d57bd 100644 --- a/src/test/java/com/amazon/opendistroforelasticsearch/ad/TestHelpers.java +++ b/src/test/java/com/amazon/opendistroforelasticsearch/ad/TestHelpers.java @@ -136,6 +136,7 @@ import com.amazon.opendistroforelasticsearch.jobscheduler.spi.schedule.IntervalSchedule; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; public class TestHelpers { @@ -143,6 +144,8 @@ public class TestHelpers { public static final String AD_BASE_RESULT_URI = "/_opendistro/_anomaly_detection/detectors/results"; public static final String AD_BASE_PREVIEW_URI = "/_opendistro/_anomaly_detection/detectors/%s/_preview"; public static final String AD_BASE_STATS_URI = "/_opendistro/_anomaly_detection/stats"; + public static ImmutableSet historicalDetectorRunningStats = ImmutableSet + .of(ADTaskState.CREATED.name(), ADTaskState.INIT.name(), ADTaskState.RUNNING.name()); private static final Logger logger = LogManager.getLogger(TestHelpers.class); public static final Random random = new Random(42); diff --git a/src/test/java/com/amazon/opendistroforelasticsearch/ad/rest/HistoricalDetectorRestApiIT.java b/src/test/java/com/amazon/opendistroforelasticsearch/ad/rest/HistoricalDetectorRestApiIT.java index e1bc0694..062d2775 100644 --- a/src/test/java/com/amazon/opendistroforelasticsearch/ad/rest/HistoricalDetectorRestApiIT.java +++ b/src/test/java/com/amazon/opendistroforelasticsearch/ad/rest/HistoricalDetectorRestApiIT.java @@ -34,14 +34,7 @@ public class HistoricalDetectorRestApiIT extends HistoricalDetectorRestTestCase { - public void testCreateHistoricalDetector() throws Exception { - // create historical detector - AnomalyDetector detector = createHistoricalDetector(); - String detectorId = detector.getDetectorId(); - assertNotNull(detectorId); - } - - public void testStartHistoricalDetector() throws Exception { + public void testHistoricalDetectorWorkflow() throws Exception { // create historical detector AnomalyDetector detector = createHistoricalDetector(); String detectorId = detector.getDetectorId(); @@ -58,48 +51,8 @@ public void testStartHistoricalDetector() throws Exception { ADTaskProfile adTaskProfile = getADTaskProfile(detectorId); ADTask adTask = adTaskProfile.getAdTask(); assertEquals(taskId, adTask.getTaskId()); - assertTrue(historicalDetectorRunningStats.contains(adTask.getState())); - assertTrue(detector.equals(adTask.getDetector())); - } - - public void testStopHistoricalDetector() throws Exception { - updateClusterSettings(BATCH_TASK_PIECE_INTERVAL_SECONDS.getKey(), 5); - // create historical detector - AnomalyDetector detector = createHistoricalDetector(); - String detectorId = detector.getDetectorId(); - - // start historical detector - String taskId = startHistoricalDetector(detectorId); - - Thread.sleep(2000); - // stop historical detector - Response stopDetectorResponse = stopAnomalyDetector(detectorId, client()); - assertEquals(RestStatus.OK, restStatus(stopDetectorResponse)); - - // get task profile - ADTaskProfile adTaskProfile = getADTaskProfile(detectorId); - int i = 0; - while (historicalDetectorRunningStats.contains(adTaskProfile.getAdTask().getState()) && i < 10) { - adTaskProfile = getADTaskProfile(detectorId); - Thread.sleep(2000); - i++; - } - ADTask adTask = adTaskProfile.getAdTask(); - assertEquals(taskId, adTask.getTaskId()); - assertEquals(ADTaskState.STOPPED.name(), adTask.getState()); - assertTrue(detector.equals(adTask.getDetector())); - updateClusterSettings(BATCH_TASK_PIECE_INTERVAL_SECONDS.getKey(), 1); - } - - public void testGetHistoricalAnomalyDetectorWithTask() throws Exception { - // create historical detector - AnomalyDetector detector = createHistoricalDetector(); - String detectorId = detector.getDetectorId(); - - // start historical detector - String taskId = startHistoricalDetector(detectorId); + assertTrue(TestHelpers.historicalDetectorRunningStats.contains(adTask.getState())); - Thread.sleep(5000); // get historical detector with AD task ToXContentObject[] result = getHistoricalAnomalyDetector(detectorId, true, client()); AnomalyDetector parsedDetector = (AnomalyDetector) result[0]; @@ -111,6 +64,24 @@ public void testGetHistoricalAnomalyDetectorWithTask() throws Exception { assertTrue(detector.equals(parsedDetector)); assertTrue(detector.equals(parsedADTask.getDetector())); assertEquals(taskId, parsedADTask.getTaskId()); + + // stop historical detector + Response stopDetectorResponse = stopAnomalyDetector(detectorId, client()); + assertEquals(RestStatus.OK, restStatus(stopDetectorResponse)); + + // get task profile + ADTaskProfile stoppedAdTaskProfile = getADTaskProfile(detectorId); + int i = 0; + while (TestHelpers.historicalDetectorRunningStats.contains(stoppedAdTaskProfile.getAdTask().getState()) && i < 10) { + stoppedAdTaskProfile = getADTaskProfile(detectorId); + Thread.sleep(2000); + i++; + } + ADTask stoppedAdTask = stoppedAdTaskProfile.getAdTask(); + assertEquals(taskId, stoppedAdTask.getTaskId()); + assertEquals(ADTaskState.STOPPED.name(), stoppedAdTask.getState()); + assertTrue(detector.equals(stoppedAdTask.getDetector())); + updateClusterSettings(BATCH_TASK_PIECE_INTERVAL_SECONDS.getKey(), 1); } } diff --git a/src/test/java/com/amazon/opendistroforelasticsearch/ad/transport/AnomalyDetectorJobTransportActionTests.java b/src/test/java/com/amazon/opendistroforelasticsearch/ad/transport/AnomalyDetectorJobTransportActionTests.java index 3d4ee074..57f7d028 100644 --- a/src/test/java/com/amazon/opendistroforelasticsearch/ad/transport/AnomalyDetectorJobTransportActionTests.java +++ b/src/test/java/com/amazon/opendistroforelasticsearch/ad/transport/AnomalyDetectorJobTransportActionTests.java @@ -338,15 +338,19 @@ public void testStopHistoricalDetector() throws IOException, InterruptedExceptio public void testProfileHistoricalDetector() throws IOException, InterruptedException { ADTask adTask = startHistoricalDetector(); GetAnomalyDetectorRequest request = taskProfileRequest(adTask.getDetectorId()); - GetAnomalyDetectorResponse response = dataNodeClient().execute(GetAnomalyDetectorAction.INSTANCE, request).actionGet(10000); - assertNotNull(response.getDetectorProfile().getAdTaskProfile().getNodeId()); - - Thread.sleep(10000); + GetAnomalyDetectorResponse response = client().execute(GetAnomalyDetectorAction.INSTANCE, request).actionGet(10000); + assertNotNull(response.getDetectorProfile().getAdTaskProfile()); ADTask finishedTask = getADTask(adTask.getTaskId()); + int i = 0; + while (TestHelpers.historicalDetectorRunningStats.contains(finishedTask.getState()) && i < 10) { + finishedTask = getADTask(adTask.getTaskId()); + Thread.sleep(2000); + i++; + } assertEquals(ADTaskState.FINISHED.name(), finishedTask.getState()); - response = dataNodeClient().execute(GetAnomalyDetectorAction.INSTANCE, request).actionGet(10000); + response = client().execute(GetAnomalyDetectorAction.INSTANCE, request).actionGet(10000); assertNull(response.getDetectorProfile().getAdTaskProfile().getNodeId()); ADTask profileAdTask = response.getDetectorProfile().getAdTaskProfile().getAdTask(); assertEquals(finishedTask.getTaskId(), profileAdTask.getTaskId()); @@ -361,8 +365,8 @@ public void testProfileWithMultipleRunningTask() throws IOException { GetAnomalyDetectorRequest request1 = taskProfileRequest(adTask1.getDetectorId()); GetAnomalyDetectorRequest request2 = taskProfileRequest(adTask2.getDetectorId()); - GetAnomalyDetectorResponse response1 = dataNodeClient().execute(GetAnomalyDetectorAction.INSTANCE, request1).actionGet(10000); - GetAnomalyDetectorResponse response2 = dataNodeClient().execute(GetAnomalyDetectorAction.INSTANCE, request2).actionGet(10000); + GetAnomalyDetectorResponse response1 = client().execute(GetAnomalyDetectorAction.INSTANCE, request1).actionGet(10000); + GetAnomalyDetectorResponse response2 = client().execute(GetAnomalyDetectorAction.INSTANCE, request2).actionGet(10000); ADTaskProfile taskProfile1 = response1.getDetectorProfile().getAdTaskProfile(); ADTaskProfile taskProfile2 = response2.getDetectorProfile().getAdTaskProfile(); assertNotNull(taskProfile1.getNodeId());