Skip to content

Commit 5606137

Browse files
committed
fix ut
Signed-off-by: Yaliang Wu <[email protected]>
1 parent a965eb5 commit 5606137

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

ml-algorithms/src/test/java/org/opensearch/ml/engine/httpclient/MLHttpClientFactoryTests.java

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import static org.junit.Assert.assertNotNull;
99

1010
import java.time.Duration;
11+
import java.util.concurrent.atomic.AtomicBoolean;
1112

1213
import org.junit.Rule;
1314
import org.junit.Test;
@@ -28,70 +29,84 @@ public void test_getSdkAsyncHttpClient_success() {
2829

2930
@Test
3031
public void test_validateIp_validIp_noException() throws Exception {
31-
MLHttpClientFactory.validate("http", "api.openai.com", 80);
32+
AtomicBoolean privateIpEnabled = new AtomicBoolean(false);
33+
MLHttpClientFactory.validate("http", "api.openai.com", 80, privateIpEnabled);
3234
}
3335

3436
@Test
3537
public void test_validateIp_rarePrivateIp_throwException() throws Exception {
38+
AtomicBoolean privateIpEnabled = new AtomicBoolean(false);
3639
try {
37-
MLHttpClientFactory.validate("http", "0254.020.00.01", 80);
40+
MLHttpClientFactory.validate("http", "0254.020.00.01", 80, privateIpEnabled);
3841
} catch (IllegalArgumentException e) {
3942
assertNotNull(e);
4043
}
4144

4245
try {
43-
MLHttpClientFactory.validate("http", "172.1048577", 80);
46+
MLHttpClientFactory.validate("http", "172.1048577", 80, privateIpEnabled);
4447
} catch (Exception e) {
4548
assertNotNull(e);
4649
}
4750

4851
try {
49-
MLHttpClientFactory.validate("http", "2886729729", 80);
52+
MLHttpClientFactory.validate("http", "2886729729", 80, privateIpEnabled);
5053
} catch (IllegalArgumentException e) {
5154
assertNotNull(e);
5255
}
5356

5457
try {
55-
MLHttpClientFactory.validate("http", "192.11010049", 80);
58+
MLHttpClientFactory.validate("http", "192.11010049", 80, privateIpEnabled);
5659
} catch (IllegalArgumentException e) {
5760
assertNotNull(e);
5861
}
5962

6063
try {
61-
MLHttpClientFactory.validate("http", "3232300545", 80);
64+
MLHttpClientFactory.validate("http", "3232300545", 80, privateIpEnabled);
6265
} catch (IllegalArgumentException e) {
6366
assertNotNull(e);
6467
}
6568

6669
try {
67-
MLHttpClientFactory.validate("http", "0:0:0:0:0:ffff:127.0.0.1", 80);
70+
MLHttpClientFactory.validate("http", "0:0:0:0:0:ffff:127.0.0.1", 80, privateIpEnabled);
6871
} catch (IllegalArgumentException e) {
6972
assertNotNull(e);
7073
}
7174

7275
try {
73-
MLHttpClientFactory.validate("http", "153.24.76.232", 80);
76+
MLHttpClientFactory.validate("http", "153.24.76.232", 80, privateIpEnabled);
7477
} catch (IllegalArgumentException e) {
7578
assertNotNull(e);
7679
}
7780
}
7881

82+
@Test
83+
public void test_validateIp_rarePrivateIp_NotThrowException() throws Exception {
84+
AtomicBoolean privateIpEnabled = new AtomicBoolean(true);
85+
MLHttpClientFactory.validate("http", "0254.020.00.01", 80, privateIpEnabled);
86+
MLHttpClientFactory.validate("http", "172.1048577", 80, privateIpEnabled);
87+
MLHttpClientFactory.validate("http", "2886729729", 80, privateIpEnabled);
88+
MLHttpClientFactory.validate("http", "192.11010049", 80, privateIpEnabled);
89+
MLHttpClientFactory.validate("http", "3232300545", 80, privateIpEnabled);
90+
MLHttpClientFactory.validate("http", "0:0:0:0:0:ffff:127.0.0.1", 80, privateIpEnabled);
91+
MLHttpClientFactory.validate("http", "153.24.76.232", 80, privateIpEnabled);
92+
}
93+
7994
@Test
8095
public void test_validateSchemaAndPort_success() throws Exception {
81-
MLHttpClientFactory.validate("http", "api.openai.com", 80);
96+
MLHttpClientFactory.validate("http", "api.openai.com", 80, new AtomicBoolean(false));
8297
}
8398

8499
@Test
85100
public void test_validateSchemaAndPort_notAllowedSchema_throwException() throws Exception {
86101
expectedException.expect(IllegalArgumentException.class);
87-
MLHttpClientFactory.validate("ftp", "api.openai.com", 80);
102+
MLHttpClientFactory.validate("ftp", "api.openai.com", 80, new AtomicBoolean(false));
88103
}
89104

90105
@Test
91106
public void test_validateSchemaAndPort_portNotInRange_throwException() throws Exception {
92107
expectedException.expect(IllegalArgumentException.class);
93108
expectedException.expectMessage("Port out of range: 65537");
94-
MLHttpClientFactory.validate("https", "api.openai.com", 65537);
109+
MLHttpClientFactory.validate("https", "api.openai.com", 65537, new AtomicBoolean(false));
95110
}
96111

97112
}

plugin/src/test/java/org/opensearch/ml/model/MLModelManagerTests.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import java.util.Set;
6262
import java.util.concurrent.ConcurrentHashMap;
6363
import java.util.concurrent.ExecutorService;
64+
import java.util.concurrent.atomic.AtomicBoolean;
6465
import java.util.function.Supplier;
6566

6667
import org.junit.Before;
@@ -109,6 +110,7 @@
109110
import org.opensearch.ml.engine.encryptor.Encryptor;
110111
import org.opensearch.ml.engine.encryptor.EncryptorImpl;
111112
import org.opensearch.ml.engine.indices.MLIndicesHandler;
113+
import org.opensearch.ml.settings.MLFeatureEnabledSetting;
112114
import org.opensearch.ml.stats.ActionName;
113115
import org.opensearch.ml.stats.MLActionLevelStat;
114116
import org.opensearch.ml.stats.MLNodeLevelStat;
@@ -179,6 +181,8 @@ public class MLModelManagerTests extends OpenSearchTestCase {
179181

180182
@Mock
181183
ClusterApplierService clusterApplierService;
184+
@Mock
185+
MLFeatureEnabledSetting mlFeatureEnabledSetting;
182186

183187
@Before
184188
public void setup() throws URISyntaxException {
@@ -253,6 +257,8 @@ public void setup() throws URISyntaxException {
253257
when(client.threadPool()).thenReturn(threadPool);
254258
when(threadPool.getThreadContext()).thenReturn(threadContext);
255259

260+
when(mlFeatureEnabledSetting.isConnectorPrivateIpEnabled()).thenReturn(new AtomicBoolean(false));
261+
256262
modelManager = spy(
257263
new MLModelManager(
258264
clusterService,
@@ -268,7 +274,8 @@ public void setup() throws URISyntaxException {
268274
mlTaskManager,
269275
modelCacheHelper,
270276
mlEngine,
271-
nodeHelper
277+
nodeHelper,
278+
mlFeatureEnabledSetting
272279
)
273280
);
274281

0 commit comments

Comments
 (0)