Skip to content

Commit 6852e0d

Browse files
rbhavnaHailong-am
andauthored
[Backport 2.15] fix flaky test of PredictionITTests and RestConnectorToolIT (#2437) (#2548)
* fix flaky test of PredictionITTests and RestConnectorToolIT (#2437) Signed-off-by: Bhavana Ramaram <[email protected]> Co-authored-by: Hailong Cui <[email protected]>
1 parent 1c90e38 commit 6852e0d

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

Lines changed: 10 additions & 2 deletions
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

Lines changed: 10 additions & 0 deletions
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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -954,10 +954,18 @@ public void waitForTask(String taskId, MLTaskState targetState) throws Interrupt
954954
}
955955

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

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

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

Lines changed: 6 additions & 0 deletions
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)