Skip to content

Commit 149a42b

Browse files
committed
Add Tests for covergae.
Signed-off-by: Pranit Kumar <[email protected]>
1 parent 6813ba1 commit 149a42b

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

plugins/repository-s3/src/main/java/org/opensearch/repositories/s3/S3Repository.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,8 @@ private void validateRepositoryMetadata(RepositoryMetadata newRepositoryMetadata
620620
validateHttpClientType(S3_ASYNC_HTTP_CLIENT_TYPE.get(settings));
621621
}
622622

623-
private void validateHttpClientType(String httpClientType) {
623+
// package access for tests
624+
void validateHttpClientType(String httpClientType) {
624625
if (!(httpClientType.equalsIgnoreCase(NETTY_ASYNC_HTTP_CLIENT_TYPE)
625626
|| httpClientType.equalsIgnoreCase(CRT_ASYNC_HTTP_CLIENT_TYPE))) {
626627
throw new BlobStoreException("Invalid http client type. `" + httpClientType + "`");

plugins/repository-s3/src/test/java/org/opensearch/repositories/s3/S3AsyncServiceTests.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,24 @@ public void testCachedClientsWithCredentialsAreReleased() {
104104
}
105105

106106
public void testBuildHttpClientWithNetty() {
107+
final int port = randomIntBetween(10, 1080);
108+
final String userName = randomAlphaOfLength(10);
109+
final String password = randomAlphaOfLength(10);
110+
final String proxyType = randomFrom("http", "https", "socks");
107111
final S3AsyncService s3AsyncService = new S3AsyncService(configPath());
108-
final Settings settings = Settings.builder().put("endpoint", "http://first").put("region", "us-east-2").build();
112+
113+
final MockSecureSettings secureSettings = new MockSecureSettings();
114+
secureSettings.setString("s3.client.default.proxy.username", userName);
115+
secureSettings.setString("s3.client.default.proxy.password", password);
116+
117+
final Settings settings = Settings.builder()
118+
.put("endpoint", "http://first")
119+
.put("region", "us-east-2")
120+
.put("s3.client.default.proxy.type", proxyType)
121+
.put("s3.client.default.proxy.host", randomFrom("127.0.0.10"))
122+
.put("s3.client.default.proxy.port", randomFrom(port))
123+
.setSecureSettings(secureSettings)
124+
.build();
109125
final RepositoryMetadata metadata1 = new RepositoryMetadata("first", "s3", settings);
110126
final S3ClientSettings clientSettings = s3AsyncService.settings(metadata1);
111127

@@ -123,8 +139,25 @@ public void testBuildHttpClientWithNetty() {
123139
}
124140

125141
public void testBuildHttpClientWithCRT() {
142+
final int port = randomIntBetween(10, 1080);
143+
final String userName = randomAlphaOfLength(10);
144+
final String password = randomAlphaOfLength(10);
145+
final String proxyType = randomFrom("http", "https", "socks");
126146
final S3AsyncService s3AsyncService = new S3AsyncService(configPath());
127-
final Settings settings = Settings.builder().put("endpoint", "http://first").put("region", "us-east-2").build();
147+
148+
final MockSecureSettings secureSettings = new MockSecureSettings();
149+
secureSettings.setString("s3.client.default.proxy.username", userName);
150+
secureSettings.setString("s3.client.default.proxy.password", password);
151+
152+
final Settings settings = Settings.builder()
153+
.put("endpoint", "http://first")
154+
.put("region", "us-east-2")
155+
.put("s3.client.default.proxy.type", proxyType)
156+
.put("s3.client.default.proxy.host", randomFrom("127.0.0.10"))
157+
.put("s3.client.default.proxy.port", randomFrom(port))
158+
.setSecureSettings(secureSettings)
159+
.build();
160+
128161
final RepositoryMetadata metadata1 = new RepositoryMetadata("first", "s3", settings);
129162
final S3ClientSettings clientSettings = s3AsyncService.settings(metadata1);
130163

plugins/repository-s3/src/test/java/org/opensearch/repositories/s3/S3RepositoryTests.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import software.amazon.awssdk.services.s3.S3Client;
3636

3737
import org.opensearch.cluster.metadata.RepositoryMetadata;
38+
import org.opensearch.common.blobstore.BlobStoreException;
3839
import org.opensearch.common.settings.ClusterSettings;
3940
import org.opensearch.common.settings.Setting;
4041
import org.opensearch.common.settings.Settings;
@@ -157,6 +158,23 @@ public void testRestrictedSettingsDefault() {
157158
}
158159
}
159160

161+
public void testValidateHttpLClientType_Valid_Values() {
162+
final RepositoryMetadata metadata = new RepositoryMetadata("dummy-repo", "mock", Settings.EMPTY);
163+
try (S3Repository s3Repo = createS3Repo(metadata)) {
164+
// Don't expect any Exception
165+
s3Repo.validateHttpClientType(S3Repository.CRT_ASYNC_HTTP_CLIENT_TYPE);
166+
s3Repo.validateHttpClientType(S3Repository.NETTY_ASYNC_HTTP_CLIENT_TYPE);
167+
}
168+
}
169+
170+
public void testValidateHttpLClientType_Invalid_Values() {
171+
final RepositoryMetadata metadata = new RepositoryMetadata("dummy-repo", "mock", Settings.EMPTY);
172+
try (S3Repository s3Repo = createS3Repo(metadata)) {
173+
// Don't expect any Exception
174+
assertThrows(BlobStoreException.class, () -> s3Repo.validateHttpClientType(randomAlphaOfLength(4)));
175+
}
176+
}
177+
160178
private S3Repository createS3Repo(RepositoryMetadata metadata) {
161179
return new S3Repository(
162180
metadata,

0 commit comments

Comments
 (0)