forked from opensearch-project/data-prepper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Srikanth Govindarajan <[email protected]>
- Loading branch information
1 parent
cd72a82
commit 0eca813
Showing
3 changed files
with
104 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
267 changes: 104 additions & 163 deletions
267
...java/org/opensearch/dataprepper/plugins/lambda/common/client/LambdaClientFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,185 +1,126 @@ | ||
package org.opensearch.dataprepper.plugins.lambda.common.client; | ||
|
||
import java.time.Duration; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.opensearch.dataprepper.aws.api.AwsCredentialsOptions; | ||
import org.opensearch.dataprepper.aws.api.AwsCredentialsSupplier; | ||
import org.opensearch.dataprepper.plugins.lambda.common.config.AwsAuthenticationOptions; | ||
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; | ||
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration; | ||
import software.amazon.awssdk.http.nio.netty.NettyNioAsyncHttpClient; | ||
import software.amazon.awssdk.regions.Region; | ||
import software.amazon.awssdk.services.lambda.LambdaAsyncClient; | ||
|
||
import java.time.Duration; | ||
import java.util.HashMap; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class LambdaClientFactoryTest { | ||
|
||
@Mock | ||
private AwsCredentialsSupplier awsCredentialsSupplier; | ||
private AwsAuthenticationOptions awsAuthenticationOptions; | ||
|
||
@Mock | ||
private AwsAuthenticationOptions awsAuthenticationOptions; | ||
private AwsCredentialsSupplier awsCredentialsSupplier; | ||
|
||
@Mock | ||
private AwsCredentialsProvider awsCredentialsProvider; | ||
|
||
private Duration sdkTimeout = Duration.ofSeconds(60); | ||
|
||
/*@Test | ||
void createLambdaAsyncClient_with_real_LambdaAsyncClient() { | ||
try (MockedStatic<LambdaAsyncClient> mockedStaticLambdaAsyncClient = mockStatic( | ||
LambdaAsyncClient.class); | ||
MockedStatic<PluginMetrics> mockedPluginMetrics = mockStatic(PluginMetrics.class)) { | ||
PluginMetrics pluginMetricsMock = mock(PluginMetrics.class); | ||
mockedPluginMetrics.when(() -> PluginMetrics.fromNames("sdk", "aws")) | ||
.thenReturn(pluginMetricsMock); | ||
LambdaAsyncClientBuilder lambdaAsyncClientBuilder = mock(LambdaAsyncClientBuilder.class); | ||
LambdaAsyncClient lambdaAsyncClientMock = mock(LambdaAsyncClient.class); | ||
mockedStaticLambdaAsyncClient.when(LambdaAsyncClient::builder) | ||
.thenReturn(lambdaAsyncClientBuilder); | ||
when(lambdaAsyncClientBuilder.region(any(Region.class))).thenReturn(lambdaAsyncClientBuilder); | ||
when(lambdaAsyncClientBuilder.credentialsProvider( | ||
any(AwsCredentialsProvider.class))).thenReturn(lambdaAsyncClientBuilder); | ||
when(lambdaAsyncClientBuilder.overrideConfiguration( | ||
any(ClientOverrideConfiguration.class))).thenReturn(lambdaAsyncClientBuilder); | ||
when(lambdaAsyncClientBuilder.build()).thenReturn(lambdaAsyncClientMock); | ||
Region region = Region.US_WEST_2; | ||
when(awsAuthenticationOptions.getAwsRegion()).thenReturn(region); | ||
String stsRoleArn = UUID.randomUUID().toString(); | ||
String stsExternalId = UUID.randomUUID().toString(); | ||
Map<String, String> stsHeaderOverrides = Map.of(UUID.randomUUID().toString(), | ||
UUID.randomUUID().toString()); | ||
when(awsAuthenticationOptions.getAwsStsRoleArn()).thenReturn(stsRoleArn); | ||
when(awsAuthenticationOptions.getAwsStsExternalId()).thenReturn(stsExternalId); | ||
when(awsAuthenticationOptions.getAwsStsHeaderOverrides()).thenReturn(stsHeaderOverrides); | ||
when(awsCredentialsSupplier.getProvider(any(AwsCredentialsOptions.class))).thenReturn( | ||
awsCredentialsProvider); | ||
// Act | ||
int maxConnectionRetries = 3; | ||
LambdaAsyncClient lambdaAsyncClient = LambdaClientFactory.createAsyncLambdaClient( | ||
awsAuthenticationOptions, | ||
maxConnectionRetries, | ||
awsCredentialsSupplier, | ||
sdkTimeout | ||
); | ||
// Verify | ||
assertThat(lambdaAsyncClient, notNullValue()); | ||
verify(lambdaAsyncClientBuilder).region(region); | ||
verify(lambdaAsyncClientBuilder).credentialsProvider(awsCredentialsProvider); | ||
// Capture and verify ClientOverrideConfiguration | ||
ArgumentCaptor<ClientOverrideConfiguration> configCaptor = ArgumentCaptor.forClass( | ||
ClientOverrideConfiguration.class); | ||
verify(lambdaAsyncClientBuilder).overrideConfiguration(configCaptor.capture()); | ||
ClientOverrideConfiguration config = configCaptor.getValue(); | ||
assertThat(config.apiCallTimeout(), equalTo(Optional.of(sdkTimeout))); | ||
// Verify RetryPolicy | ||
assertThat(config.retryPolicy().isPresent(), equalTo(true)); | ||
RetryPolicy retryPolicy = config.retryPolicy().get(); | ||
assertThat(retryPolicy.numRetries(), equalTo(maxConnectionRetries)); | ||
// Verify MetricPublisher | ||
assertThat(config.metricPublishers(), notNullValue()); | ||
assertThat(config.metricPublishers().size(), equalTo(1)); | ||
MetricPublisher metricPublisher = config.metricPublishers().get(0); | ||
assertThat(metricPublisher, instanceOf(MicrometerMetricPublisher.class)); | ||
// Verify that awsCredentialsSupplier.getProvider was called with correct AwsCredentialsOptions | ||
ArgumentCaptor<AwsCredentialsOptions> optionsCaptor = ArgumentCaptor.forClass( | ||
AwsCredentialsOptions.class); | ||
verify(awsCredentialsSupplier).getProvider(optionsCaptor.capture()); | ||
AwsCredentialsOptions credentialsOptions = optionsCaptor.getValue(); | ||
assertThat(credentialsOptions.getRegion(), equalTo(region)); | ||
assertThat(credentialsOptions.getStsRoleArn(), equalTo(stsRoleArn)); | ||
assertThat(credentialsOptions.getStsExternalId(), equalTo(stsExternalId)); | ||
assertThat(credentialsOptions.getStsHeaderOverrides(), equalTo(stsHeaderOverrides)); | ||
} | ||
@BeforeEach | ||
void setUp() { | ||
when(awsAuthenticationOptions.getAwsRegion()).thenReturn(Region.US_WEST_2); | ||
when(awsAuthenticationOptions.getAwsStsRoleArn()).thenReturn("arn:aws:iam::123456789012:role/example-role"); | ||
when(awsAuthenticationOptions.getAwsStsExternalId()).thenReturn("externalId"); | ||
when(awsAuthenticationOptions.getAwsStsHeaderOverrides()).thenReturn(new HashMap<>()); | ||
|
||
when(awsCredentialsSupplier.getProvider(any(AwsCredentialsOptions.class))).thenReturn(awsCredentialsProvider); | ||
} | ||
|
||
@Test | ||
void testCreateAsyncLambdaClient() { | ||
int maxConnectionRetries = 3; | ||
Duration sdkTimeout = Duration.ofSeconds(120); | ||
|
||
LambdaAsyncClient client = LambdaClientFactory.createAsyncLambdaClient( | ||
awsAuthenticationOptions, | ||
maxConnectionRetries, | ||
awsCredentialsSupplier, | ||
sdkTimeout | ||
); | ||
|
||
assertNotNull(client); | ||
assertEquals(Region.US_WEST_2, client.serviceClientConfiguration().region()); | ||
} | ||
|
||
@Test | ||
void testCreateAsyncLambdaClientWithDifferentRegion() { | ||
when(awsAuthenticationOptions.getAwsRegion()).thenReturn(Region.EU_CENTRAL_1); | ||
|
||
LambdaAsyncClient client = LambdaClientFactory.createAsyncLambdaClient( | ||
awsAuthenticationOptions, | ||
3, | ||
awsCredentialsSupplier, | ||
Duration.ofSeconds(60) | ||
); | ||
|
||
assertNotNull(client); | ||
assertEquals(Region.EU_CENTRAL_1, client.serviceClientConfiguration().region()); | ||
} | ||
|
||
@Test | ||
void testCreateAsyncLambdaClientWithCustomSdkTimeout() { | ||
Duration customTimeout = Duration.ofMinutes(5); | ||
|
||
LambdaAsyncClient client = LambdaClientFactory.createAsyncLambdaClient( | ||
awsAuthenticationOptions, | ||
3, | ||
awsCredentialsSupplier, | ||
customTimeout | ||
); | ||
|
||
assertNotNull(client); | ||
assertEquals(customTimeout, client.serviceClientConfiguration().overrideConfiguration().apiCallTimeout().get()); | ||
} | ||
|
||
@Test | ||
void testCreateAsyncLambdaClientWithMaxRetries() { | ||
int maxRetries = 5; | ||
|
||
LambdaAsyncClient client = LambdaClientFactory.createAsyncLambdaClient( | ||
awsAuthenticationOptions, | ||
maxRetries, | ||
awsCredentialsSupplier, | ||
Duration.ofSeconds(60) | ||
); | ||
|
||
assertNotNull(client); | ||
} | ||
|
||
@Test | ||
void createAsyncLambdaClient_with_correct_configuration() { | ||
try (MockedStatic<LambdaAsyncClient> mockedStaticLambdaAsyncClient = mockStatic( | ||
LambdaAsyncClient.class); | ||
MockedStatic<PluginMetrics> mockedPluginMetrics = mockStatic(PluginMetrics.class)) { | ||
PluginMetrics pluginMetricsMock = mock(PluginMetrics.class); | ||
mockedPluginMetrics.when(() -> PluginMetrics.fromNames("sdk", "aws")) | ||
.thenReturn(pluginMetricsMock); | ||
LambdaAsyncClientBuilder lambdaAsyncClientBuilder = mock(LambdaAsyncClientBuilder.class); | ||
LambdaAsyncClient lambdaAsyncClientMock = mock(LambdaAsyncClient.class); | ||
mockedStaticLambdaAsyncClient.when(LambdaAsyncClient::builder) | ||
.thenReturn(lambdaAsyncClientBuilder); | ||
when(lambdaAsyncClientBuilder.region(any(Region.class))).thenReturn(lambdaAsyncClientBuilder); | ||
when(lambdaAsyncClientBuilder.credentialsProvider( | ||
any(AwsCredentialsProvider.class))).thenReturn(lambdaAsyncClientBuilder); | ||
when(lambdaAsyncClientBuilder.overrideConfiguration( | ||
any(ClientOverrideConfiguration.class))).thenReturn(lambdaAsyncClientBuilder); | ||
when(lambdaAsyncClientBuilder.build()).thenReturn(lambdaAsyncClientMock); | ||
Region region = Region.US_WEST_2; | ||
when(awsAuthenticationOptions.getAwsRegion()).thenReturn(region); | ||
String stsRoleArn = UUID.randomUUID().toString(); | ||
String stsExternalId = UUID.randomUUID().toString(); | ||
Map<String, String> stsHeaderOverrides = Map.of(UUID.randomUUID().toString(), | ||
UUID.randomUUID().toString()); | ||
when(awsAuthenticationOptions.getAwsStsRoleArn()).thenReturn(stsRoleArn); | ||
when(awsAuthenticationOptions.getAwsStsExternalId()).thenReturn(stsExternalId); | ||
when(awsAuthenticationOptions.getAwsStsHeaderOverrides()).thenReturn(stsHeaderOverrides); | ||
// when(awsCredentialsSupplier.getProvider(any(AwsCredentialsOptions.class))).thenReturn(awsCredentialsProvider); | ||
// Act | ||
int maxConnectionRetries = 3; | ||
LambdaAsyncClient lambdaAsyncClient = LambdaClientFactory.createAsyncLambdaClient( | ||
awsAuthenticationOptions, | ||
maxConnectionRetries, | ||
awsCredentialsSupplier, | ||
sdkTimeout | ||
); | ||
// Verify | ||
assertThat(lambdaAsyncClient, notNullValue()); | ||
// Verify builder methods | ||
verify(lambdaAsyncClientBuilder).region(region); | ||
verify(lambdaAsyncClientBuilder).credentialsProvider(awsCredentialsProvider); | ||
// Capture and verify ClientOverrideConfiguration | ||
ArgumentCaptor<ClientOverrideConfiguration> configCaptor = ArgumentCaptor.forClass( | ||
ClientOverrideConfiguration.class); | ||
verify(lambdaAsyncClientBuilder).overrideConfiguration(configCaptor.capture()); | ||
ClientOverrideConfiguration config = configCaptor.getValue(); | ||
// Verify apiCallTimeout | ||
assertThat(config.apiCallTimeout(), equalTo(Optional.of(sdkTimeout))); | ||
// Verify RetryPolicy | ||
assertThat(config.retryPolicy().isPresent(), equalTo(true)); | ||
RetryPolicy retryPolicy = config.retryPolicy().get(); | ||
assertThat(retryPolicy.numRetries(), equalTo(maxConnectionRetries)); | ||
// Verify MetricPublisher | ||
assertThat(config.metricPublishers(), notNullValue()); | ||
assertThat(config.metricPublishers().size(), equalTo(1)); | ||
MetricPublisher metricPublisher = config.metricPublishers().get(0); | ||
assertThat(metricPublisher, instanceOf(MicrometerMetricPublisher.class)); | ||
// Verify that awsCredentialsSupplier.getProvider was called with correct AwsCredentialsOptions | ||
ArgumentCaptor<AwsCredentialsOptions> optionsCaptor = ArgumentCaptor.forClass( | ||
AwsCredentialsOptions.class); | ||
verify(awsCredentialsSupplier).getProvider(optionsCaptor.capture()); | ||
AwsCredentialsOptions credentialsOptions = optionsCaptor.getValue(); | ||
assertThat(credentialsOptions.getRegion(), equalTo(region)); | ||
assertThat(credentialsOptions.getStsRoleArn(), equalTo(stsRoleArn)); | ||
assertThat(credentialsOptions.getStsExternalId(), equalTo(stsExternalId)); | ||
assertThat(credentialsOptions.getStsHeaderOverrides(), equalTo(stsHeaderOverrides)); | ||
} | ||
}*/ | ||
} | ||
void testCreateAsyncLambdaClientOverrideConfiguration() { | ||
Duration sdkTimeout = Duration.ofSeconds(90); | ||
int maxRetries = 4; | ||
|
||
LambdaAsyncClient client = LambdaClientFactory.createAsyncLambdaClient( | ||
awsAuthenticationOptions, | ||
maxRetries, | ||
awsCredentialsSupplier, | ||
sdkTimeout | ||
); | ||
|
||
assertNotNull(client); | ||
ClientOverrideConfiguration overrideConfig = client.serviceClientConfiguration().overrideConfiguration(); | ||
|
||
assertEquals(sdkTimeout, overrideConfig.apiCallTimeout().get()); | ||
assertEquals(maxRetries, overrideConfig.retryPolicy().get().numRetries()); | ||
assertNotNull(overrideConfig.metricPublishers()); | ||
assertFalse(overrideConfig.metricPublishers().isEmpty()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters