Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored flaky test. #1464

Merged
merged 3 commits into from
Feb 3, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading