Skip to content

Commit

Permalink
Refactored flaky test. (#1464)
Browse files Browse the repository at this point in the history
* Refactored flaky test.

Signed-off-by: AWSHurneyt <hurneyt@amazon.com>

* Refactored test for flakiness.

Signed-off-by: AWSHurneyt <hurneyt@amazon.com>

* Updated tests.

Signed-off-by: AWSHurneyt <hurneyt@amazon.com>

---------

Signed-off-by: AWSHurneyt <hurneyt@amazon.com>
  • Loading branch information
AWSHurneyt authored Feb 3, 2025
1 parent d4f44ec commit 783167a
Showing 1 changed file with 39 additions and 18 deletions.
57 changes: 39 additions & 18 deletions src/test/java/org/opensearch/securityanalytics/alerts/AlertsIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.opensearch.client.Request;
import org.opensearch.client.Response;
import org.opensearch.client.ResponseException;
import org.opensearch.commons.alerting.model.Monitor;
import org.opensearch.commons.alerting.model.action.Action;
import org.opensearch.core.rest.RestStatus;
import org.opensearch.search.SearchHit;
Expand All @@ -44,9 +43,7 @@
import static org.opensearch.securityanalytics.TestHelpers.netFlowMappings;
import static org.opensearch.securityanalytics.TestHelpers.randomAction;
import static org.opensearch.securityanalytics.TestHelpers.randomAggregationRule;
import static org.opensearch.securityanalytics.TestHelpers.randomDetector;
import static org.opensearch.securityanalytics.TestHelpers.randomDetectorType;
import static org.opensearch.securityanalytics.TestHelpers.randomDetectorWithInputs;
import static org.opensearch.securityanalytics.TestHelpers.randomDetectorWithInputsAndTriggers;
import static org.opensearch.securityanalytics.TestHelpers.randomDetectorWithTriggers;
import static org.opensearch.securityanalytics.TestHelpers.randomDoc;
Expand Down Expand Up @@ -794,7 +791,7 @@ public void testAlertHistoryRollover_maxAge() throws IOException, InterruptedExc
*
* @throws IOException
*/
public void testMultipleAggregationAndDocRules_alertSuccess() throws IOException {
public void testMultipleAggregationAndDocRules_alertSuccess() throws IOException, InterruptedException {
String index = createTestIndex(randomIndex(), windowsIndexMapping());

Request createMappingRequest = new Request("POST", SecurityAnalyticsPlugin.MAPPER_BASE_URI);
Expand Down Expand Up @@ -948,17 +945,28 @@ public void testMultipleAggregationAndDocRules_alertSuccess() throws IOException
}
}

assertTrue(Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8").containsAll(docLevelFinding));

params1 = new HashMap<>();
params1.put("detector_id", detectorId);
getAlertsResponse = makeRequest(client(), "GET", SecurityAnalyticsPlugin.ALERTS_BASE_URI, params1, null);
getAlertsBody = asMap(getAlertsResponse);
// TODO enable asserts here when able
Assert.assertEquals(2, getAlertsBody.get("total_alerts"));
AtomicBoolean alertRespStatus = new AtomicBoolean(false);
OpenSearchRestTestCase.waitUntil(
() -> {
Map<String, String> queryParams = new HashMap<>();
queryParams.put("detector_id", detectorId);
try {
Response alertsResponse = makeRequest(client(), "GET", SecurityAnalyticsPlugin.ALERTS_BASE_URI, queryParams, null);
Map<String, Object> alertsBody = asMap(alertsResponse);
// TODO enable asserts here when able
if (Integer.parseInt(alertsBody.get("total_alerts").toString()) == 2) {
alertRespStatus.set(true);
return true;
}
return false;
} catch (IOException e) {
return false;
}
}, 2, TimeUnit.MINUTES);
Assert.assertTrue(alertRespStatus.get());
}

public void test_detectorWith1AggRuleAndTriggeronRule_updateWithSecondAggRule() throws IOException {
public void test_detectorWith1AggRuleAndTriggeronRule_updateWithSecondAggRule() throws IOException, InterruptedException {
String index = createTestIndex(randomIndex(), windowsIndexMapping());

Request createMappingRequest = new Request("POST", SecurityAnalyticsPlugin.MAPPER_BASE_URI);
Expand Down Expand Up @@ -1070,11 +1078,24 @@ public void test_detectorWith1AggRuleAndTriggeronRule_updateWithSecondAggRule()
assertNotNull(getFindingsBody);
assertEquals(3, getFindingsBody.get("total_findings"));

params1 = new HashMap<>();
params1.put("detector_id", detectorId);
getAlertsResponse = makeRequest(client(), "GET", SecurityAnalyticsPlugin.ALERTS_BASE_URI, params1, null);
getAlertsBody = asMap(getAlertsResponse);
Assert.assertEquals(3, getAlertsBody.get("total_alerts"));
AtomicBoolean alertsCondSatisfy = new AtomicBoolean(false);
OpenSearchRestTestCase.waitUntil(
() -> {
try {
Map<String, String> queryParams = new HashMap<>();
queryParams.put("detector_id", detectorId);
Response alertsResponse = makeRequest(client(), "GET", SecurityAnalyticsPlugin.ALERTS_BASE_URI, queryParams, null);
Map<String, Object> alertsBody = asMap(alertsResponse);
if (Integer.parseInt(alertsBody.get("total_alerts").toString()) == 3) {
alertsCondSatisfy.set(true);
}
return 3 == Integer.parseInt(alertsBody.get("total_alerts").toString());
} catch (Exception e) {
return false;
}
}, 2, TimeUnit.MINUTES
);
Assert.assertTrue(alertsCondSatisfy.get());
}

@Ignore
Expand Down

0 comments on commit 783167a

Please sign in to comment.