Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
fix integ test
Browse files Browse the repository at this point in the history
  • Loading branch information
ylwu-amzn committed Jan 12, 2021
1 parent d3a92ff commit eb8f2c0
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> historicalDetectorRunningStats = ImmutableSet
.of(ADTaskState.CREATED.name(), ADTaskState.INIT.name(), ADTaskState.RUNNING.name());

@Before
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,16 @@
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 {

public static final String AD_BASE_DETECTORS_URI = "/_opendistro/_anomaly_detection/detectors";
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<String> 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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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];
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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());
Expand Down

0 comments on commit eb8f2c0

Please sign in to comment.