Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LocalStack] Remove usage of deprecated methods #5491

Merged
merged 7 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 10 additions & 25 deletions docs/modules/localstack.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,18 @@ DockerImageName localstackImage = DockerImageName.parse("localstack/localstack:0
@Rule
public LocalStackContainer localstack = new LocalStackContainer(localstackImage)
.withServices(S3);

@Test
public void someTestMethod() {
// AWS SDK v1
AmazonS3 s3 = AmazonS3ClientBuilder
.standard()
.withEndpointConfiguration(localstack.getEndpointConfiguration(S3))
.withCredentials(localstack.getDefaultCredentialsProvider())
.build();

s3.createBucket("foo");
s3.putObject("foo", "bar", "baz");

// AWS SDK v2
S3Client s3 = S3Client
.builder()
.endpointOverride(localstack.getEndpointOverride(LocalStackContainer.Service.S3))
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(
localstack.getAccessKey(), localstack.getSecretKey()
)))
.region(Region.of(localstack.getRegion()))
.build();

s3.createBucket(b -> b.bucket("foo"));
s3.putObject(b -> b.bucket("foo").key("bar"), RequestBody.fromBytes("baz".getBytes()));
```

## Creating a client using AWS SDK

<!--codeinclude-->
[AWS SDK V1](../../modules/localstack/src/test/java/org/testcontainers/containers/localstack/LocalstackContainerTest.java) inside_block:with_aws_sdk_v1
<!--/codeinclude-->

<!--codeinclude-->
[AWS SDK V2](../../modules/localstack/src/test/java/org/testcontainers/containers/localstack/LocalstackContainerTest.java) inside_block:with_aws_sdk_v2
<!--/codeinclude-->

Environment variables listed in [Localstack's README](https://github.com/localstack/localstack#configurations) may be used to customize Localstack's configuration.
Use the `.withEnv(key, value)` method on `LocalStackContainer` to apply configuration settings.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@
/**
* <p>Container for LocalStack, 'A fully functional local AWS cloud stack'.</p>
* <p>{@link LocalStackContainer#withServices(Service...)} should be used to select which services
* are to be launched. See {@link Service} for available choices. It is advised that
* {@link LocalStackContainer#getEndpointConfiguration(Service)} and
* {@link LocalStackContainer#getDefaultCredentialsProvider()}
* be used to obtain compatible endpoint configuration and credentials, respectively.</p>
* are to be launched. See {@link Service} for available choices.
*/
@Slf4j
public class LocalStackContainer extends GenericContainer<LocalStackContainer> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.testcontainers.containers.localstack;

import com.amazonaws.client.builder.AwsClientBuilder;
import com.github.dockerjava.api.DockerClient;
import lombok.AllArgsConstructor;
import org.junit.BeforeClass;
Expand All @@ -8,6 +9,7 @@
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.testcontainers.DockerClientFactory;
import org.testcontainers.containers.localstack.LocalStackContainer.Service;
import org.testcontainers.images.RemoteDockerImage;
import org.testcontainers.utility.DockerImageName;

Expand Down Expand Up @@ -46,20 +48,28 @@ public static Iterable<Object[]> constructors() {

@Test
public void samePortIsExposedForAllServices() {
localstack.withServices(LocalStackContainer.Service.S3, LocalStackContainer.Service.SQS);
localstack.withServices(Service.S3, Service.SQS);
localstack.start();

try {
assertTrue("A single port is exposed", localstack.getExposedPorts().size() == 1);
assertEquals(
"Endpoint overrides are different",
localstack.getEndpointOverride(LocalStackContainer.Service.S3).toString(),
localstack.getEndpointOverride(LocalStackContainer.Service.SQS).toString()
localstack.getEndpointOverride(Service.S3).toString(),
localstack.getEndpointOverride(Service.SQS).toString()
);
assertEquals(
"Endpoint configuration have different endpoints",
localstack.getEndpointConfiguration(LocalStackContainer.Service.S3).getServiceEndpoint(),
localstack.getEndpointConfiguration(LocalStackContainer.Service.SQS).getServiceEndpoint()
new AwsClientBuilder.EndpointConfiguration(
localstack.getEndpointOverride(Service.S3).toString(),
localstack.getRegion()
)
.getServiceEndpoint(),
new AwsClientBuilder.EndpointConfiguration(
localstack.getEndpointOverride(Service.SQS).toString(),
localstack.getRegion()
)
.getServiceEndpoint()
);
} finally {
localstack.stop();
Expand Down Expand Up @@ -103,20 +113,28 @@ public static Iterable<Object[]> constructors() {

@Test
public void differentPortsAreExposed() {
localstack.withServices(LocalStackContainer.Service.S3, LocalStackContainer.Service.SQS);
localstack.withServices(Service.S3, Service.SQS);
localstack.start();

try {
assertTrue("Multiple ports are exposed", localstack.getExposedPorts().size() > 1);
assertNotEquals(
"Endpoint overrides are different",
localstack.getEndpointOverride(LocalStackContainer.Service.S3).toString(),
localstack.getEndpointOverride(LocalStackContainer.Service.SQS).toString()
localstack.getEndpointOverride(Service.S3).toString(),
localstack.getEndpointOverride(Service.SQS).toString()
);
assertNotEquals(
"Endpoint configuration have different endpoints",
localstack.getEndpointConfiguration(LocalStackContainer.Service.S3).getServiceEndpoint(),
localstack.getEndpointConfiguration(LocalStackContainer.Service.SQS).getServiceEndpoint()
new AwsClientBuilder.EndpointConfiguration(
localstack.getEndpointOverride(Service.S3).toString(),
localstack.getRegion()
)
.getServiceEndpoint(),
new AwsClientBuilder.EndpointConfiguration(
localstack.getEndpointOverride(Service.SQS).toString(),
localstack.getRegion()
)
.getServiceEndpoint()
);
} finally {
localstack.stop();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.testcontainers.containers.localstack;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.kms.AWSKMS;
import com.amazonaws.services.kms.AWSKMSClientBuilder;
Expand Down Expand Up @@ -73,11 +75,22 @@ public static class WithoutNetwork {

@Test
public void s3TestOverBridgeNetwork() throws IOException {
// with_aws_sdk_v1 {
AmazonS3 s3 = AmazonS3ClientBuilder
.standard()
.withEndpointConfiguration(localstack.getEndpointConfiguration(Service.S3))
.withCredentials(localstack.getDefaultCredentialsProvider())
.withEndpointConfiguration(
new AwsClientBuilder.EndpointConfiguration(
localstack.getEndpointOverride(Service.S3).toString(),
localstack.getRegion()
)
)
.withCredentials(
new AWSStaticCredentialsProvider(
new BasicAWSCredentials(localstack.getAccessKey(), localstack.getSecretKey())
)
)
.build();
// }

final String bucketName = "foo";
s3.createBucket(bucketName);
Expand All @@ -103,6 +116,7 @@ public void s3TestOverBridgeNetwork() throws IOException {

@Test
public void s3TestUsingAwsSdkV2() {
// with_aws_sdk_v2 {
S3Client s3 = S3Client
.builder()
.endpointOverride(localstack.getEndpointOverride(Service.S3))
Expand All @@ -113,6 +127,7 @@ public void s3TestUsingAwsSdkV2() {
)
.region(Region.of(localstack.getRegion()))
.build();
// }

final String bucketName = "foov2";
s3.createBucket(b -> b.bucket(bucketName));
Expand All @@ -126,8 +141,17 @@ public void s3TestUsingAwsSdkV2() {
public void sqsTestOverBridgeNetwork() {
AmazonSQS sqs = AmazonSQSClientBuilder
.standard()
.withEndpointConfiguration(localstack.getEndpointConfiguration(Service.SQS))
.withCredentials(localstack.getDefaultCredentialsProvider())
.withEndpointConfiguration(
new AwsClientBuilder.EndpointConfiguration(
localstack.getEndpointOverride(Service.SQS).toString(),
localstack.getRegion()
)
)
.withCredentials(
new AWSStaticCredentialsProvider(
new BasicAWSCredentials(localstack.getAccessKey(), localstack.getSecretKey())
)
)
.build();

CreateQueueResult queueResult = sqs.createQueue("baz");
Expand Down Expand Up @@ -157,8 +181,17 @@ public void sqsTestOverBridgeNetwork() {
public void cloudWatchLogsTestOverBridgeNetwork() {
AWSLogs logs = AWSLogsClientBuilder
.standard()
.withEndpointConfiguration(localstack.getEndpointConfiguration(Service.CLOUDWATCHLOGS))
.withCredentials(localstack.getDefaultCredentialsProvider())
.withEndpointConfiguration(
new AwsClientBuilder.EndpointConfiguration(
localstack.getEndpointOverride(Service.CLOUDWATCHLOGS).toString(),
localstack.getRegion()
)
)
.withCredentials(
new AWSStaticCredentialsProvider(
new BasicAWSCredentials(localstack.getAccessKey(), localstack.getSecretKey())
)
)
.build();

logs.createLogGroup(new CreateLogGroupRequest("foo"));
Expand All @@ -172,8 +205,17 @@ public void cloudWatchLogsTestOverBridgeNetwork() {
public void kmsKeyCreationTest() {
AWSKMS awskms = AWSKMSClientBuilder
.standard()
.withEndpointConfiguration(localstack.getEndpointConfiguration(Service.KMS))
.withCredentials(localstack.getDefaultCredentialsProvider())
.withEndpointConfiguration(
new AwsClientBuilder.EndpointConfiguration(
localstack.getEndpointOverride(Service.KMS).toString(),
localstack.getRegion()
)
)
.withCredentials(
new AWSStaticCredentialsProvider(
new BasicAWSCredentials(localstack.getAccessKey(), localstack.getSecretKey())
)
)
.build();

String desc = String.format("AWS CMK Description");
Expand All @@ -198,8 +240,16 @@ public void samePortIsExposedForAllServices() {
);
assertEquals(
"Endpoint configuration have different endpoints",
localstack.getEndpointConfiguration(Service.S3).getServiceEndpoint(),
localstack.getEndpointConfiguration(Service.SQS).getServiceEndpoint()
new AwsClientBuilder.EndpointConfiguration(
localstack.getEndpointOverride(Service.S3).toString(),
localstack.getRegion()
)
.getServiceEndpoint(),
new AwsClientBuilder.EndpointConfiguration(
localstack.getEndpointOverride(Service.SQS).toString(),
localstack.getRegion()
)
.getServiceEndpoint()
);
}
}
Expand Down Expand Up @@ -302,8 +352,9 @@ public static class WithRegion {

@Test
public void s3EndpointHasProperRegion() {
final AwsClientBuilder.EndpointConfiguration endpointConfiguration = localstack.getEndpointConfiguration(
Service.S3
final AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(
localstack.getEndpointOverride(Service.S3).toString(),
localstack.getRegion()
);
assertEquals(
"The endpoint configuration has right region",
Expand Down