Skip to content

Commit 2775942

Browse files
rbhavnaHailong-am
andauthored
[Backport 2.x] fix flaky test of PredictionITTests and RestConnectorToolIT (opensearch-project#2437) (opensearch-project#2547)
* fix flaky test of PredictionITTests and RestConnectorToolIT (opensearch-project#2437) Signed-off-by: Hailong Cui <ihailong@amazon.com> Signed-off-by: Bhavana Ramaram <rbhavna@amazon.com> Co-authored-by: Hailong Cui <ihailong@amazon.com>
1 parent 1bb4ec4 commit 2775942

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

plugin/src/test/java/org/opensearch/ml/action/MLCommonsIntegTestCase.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,23 @@
9393
import org.opensearch.ml.utils.TestData;
9494
import org.opensearch.plugins.Plugin;
9595
import org.opensearch.search.builder.SearchSourceBuilder;
96-
import org.opensearch.test.OpenSearchIntegTestCase;
96+
import org.opensearch.test.ParameterizedStaticSettingsOpenSearchIntegTestCase;
9797

9898
import com.google.common.collect.ImmutableList;
9999
import com.google.common.collect.ImmutableSet;
100100
import com.google.gson.Gson;
101101

102-
public class MLCommonsIntegTestCase extends OpenSearchIntegTestCase {
102+
public class MLCommonsIntegTestCase extends ParameterizedStaticSettingsOpenSearchIntegTestCase {
103103
private Gson gson = new Gson();
104104

105+
public MLCommonsIntegTestCase() {
106+
super(Settings.EMPTY);
107+
}
108+
109+
public MLCommonsIntegTestCase(Settings nodeSettings) {
110+
super(nodeSettings);
111+
}
112+
105113
@Override
106114
protected Collection<Class<? extends Plugin>> nodePlugins() {
107115
return Collections.singletonList(MachineLearningPlugin.class);

plugin/src/test/java/org/opensearch/ml/action/prediction/PredictionITTests.java

+10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package org.opensearch.ml.action.prediction;
77

8+
import static org.opensearch.ml.settings.MLCommonsSettings.ML_COMMONS_SYNC_UP_JOB_INTERVAL_IN_SECONDS;
89
import static org.opensearch.ml.utils.TestData.IRIS_DATA_SIZE;
910
import static org.opensearch.ml.utils.TestData.TIME_FIELD;
1011

@@ -17,6 +18,7 @@
1718
import org.junit.rules.ExpectedException;
1819
import org.opensearch.action.ActionRequestValidationException;
1920
import org.opensearch.common.action.ActionFuture;
21+
import org.opensearch.common.settings.Settings;
2022
import org.opensearch.ml.action.MLCommonsIntegTestCase;
2123
import org.opensearch.ml.common.FunctionName;
2224
import org.opensearch.ml.common.MLModel;
@@ -50,6 +52,14 @@ public class PredictionITTests extends MLCommonsIntegTestCase {
5052
private String logisticRegressionModelId;
5153
private int batchRcfDataSize = 100;
5254

55+
/**
56+
* set ML_COMMONS_SYNC_UP_JOB_INTERVAL_IN_SECONDS to 0 to disable ML_COMMONS_SYNC_UP_JOB
57+
* the cluster will be pre-created with the settings at startup
58+
*/
59+
public PredictionITTests() {
60+
super(Settings.builder().put(ML_COMMONS_SYNC_UP_JOB_INTERVAL_IN_SECONDS.getKey(), 0).build());
61+
}
62+
5363
@Rule
5464
public ExpectedException exceptionRule = ExpectedException.none();
5565

plugin/src/test/java/org/opensearch/ml/rest/MLCommonsRestTestCase.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -955,10 +955,18 @@ public void waitForTask(String taskId, MLTaskState targetState) throws Interrupt
955955
}
956956

957957
public String registerConnector(String createConnectorInput) throws IOException, InterruptedException {
958-
Response response = RestMLRemoteInferenceIT.createConnector(createConnectorInput);
958+
Response response;
959+
try {
960+
response = RestMLRemoteInferenceIT.createConnector(createConnectorInput);
961+
} catch (Throwable throwable) {
962+
// Add retry for `The ML encryption master key has not been initialized yet. Please retry after waiting for 10 seconds.`
963+
TimeUnit.SECONDS.sleep(10);
964+
response = RestMLRemoteInferenceIT.createConnector(createConnectorInput);
965+
}
959966
Map responseMap = parseResponseToMap(response);
960967
String connectorId = (String) responseMap.get("connector_id");
961968
return connectorId;
969+
962970
}
963971

964972
public String registerRemoteModel(String createConnectorInput, String modelName, boolean deploy) throws IOException,

plugin/src/test/java/org/opensearch/ml/rest/RestConnectorToolIT.java

+6
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ public void tearDown() throws Exception {
8585
}
8686

8787
public void testConnectorToolInFlowAgent_WrongAction() throws IOException {
88+
if (AWS_ACCESS_KEY_ID == null || AWS_SECRET_ACCESS_KEY == null || AWS_SESSION_TOKEN == null) {
89+
return;
90+
}
8891
String registerAgentRequestBody = "{\n"
8992
+ " \"name\": \"Test agent with connector tool\",\n"
9093
+ " \"type\": \"flow\",\n"
@@ -110,6 +113,9 @@ public void testConnectorToolInFlowAgent_WrongAction() throws IOException {
110113
}
111114

112115
public void testConnectorToolInFlowAgent() throws IOException {
116+
if (AWS_ACCESS_KEY_ID == null || AWS_SECRET_ACCESS_KEY == null || AWS_SESSION_TOKEN == null) {
117+
return;
118+
}
113119
String registerAgentRequestBody = "{\n"
114120
+ " \"name\": \"Test agent with connector tool\",\n"
115121
+ " \"type\": \"flow\",\n"

0 commit comments

Comments
 (0)