From a9c2e9e148097f4996dc7a96f85fced1ee857ddc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez=20Gonzales?= Date: Wed, 22 Jun 2022 13:30:09 -0500 Subject: [PATCH] [LocalStack] Remove usage of deprecated methods (#5491) Improve documentation and tests avoiding usage of deprecated methods. --- docs/modules/localstack.md | 35 +++------ .../localstack/LocalStackContainer.java | 5 +- .../containers/localstack/LegacyModeTest.java | 38 +++++++--- .../localstack/LocalstackContainerTest.java | 75 ++++++++++++++++--- 4 files changed, 102 insertions(+), 51 deletions(-) diff --git a/docs/modules/localstack.md b/docs/modules/localstack.md index b551536af74..dcc474e484e 100644 --- a/docs/modules/localstack.md +++ b/docs/modules/localstack.md @@ -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 + + +[AWS SDK V1](../../modules/localstack/src/test/java/org/testcontainers/containers/localstack/LocalstackContainerTest.java) inside_block:with_aws_sdk_v1 + + + +[AWS SDK V2](../../modules/localstack/src/test/java/org/testcontainers/containers/localstack/LocalstackContainerTest.java) inside_block:with_aws_sdk_v2 + + 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. diff --git a/modules/localstack/src/main/java/org/testcontainers/containers/localstack/LocalStackContainer.java b/modules/localstack/src/main/java/org/testcontainers/containers/localstack/LocalStackContainer.java index f3f905b84f6..146cb3200f1 100644 --- a/modules/localstack/src/main/java/org/testcontainers/containers/localstack/LocalStackContainer.java +++ b/modules/localstack/src/main/java/org/testcontainers/containers/localstack/LocalStackContainer.java @@ -27,10 +27,7 @@ /** *

Container for LocalStack, 'A fully functional local AWS cloud stack'.

*

{@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.

+ * are to be launched. See {@link Service} for available choices. */ @Slf4j public class LocalStackContainer extends GenericContainer { diff --git a/modules/localstack/src/test/java/org/testcontainers/containers/localstack/LegacyModeTest.java b/modules/localstack/src/test/java/org/testcontainers/containers/localstack/LegacyModeTest.java index a757c11e928..c075d2805d6 100644 --- a/modules/localstack/src/test/java/org/testcontainers/containers/localstack/LegacyModeTest.java +++ b/modules/localstack/src/test/java/org/testcontainers/containers/localstack/LegacyModeTest.java @@ -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; @@ -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; @@ -46,20 +48,28 @@ public static Iterable 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(); @@ -103,20 +113,28 @@ public static Iterable 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(); diff --git a/modules/localstack/src/test/java/org/testcontainers/containers/localstack/LocalstackContainerTest.java b/modules/localstack/src/test/java/org/testcontainers/containers/localstack/LocalstackContainerTest.java index 3b85c69ca34..950f29866ba 100644 --- a/modules/localstack/src/test/java/org/testcontainers/containers/localstack/LocalstackContainerTest.java +++ b/modules/localstack/src/test/java/org/testcontainers/containers/localstack/LocalstackContainerTest.java @@ -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; @@ -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); @@ -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)) @@ -113,6 +127,7 @@ public void s3TestUsingAwsSdkV2() { ) .region(Region.of(localstack.getRegion())) .build(); + // } final String bucketName = "foov2"; s3.createBucket(b -> b.bucket(bucketName)); @@ -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"); @@ -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")); @@ -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"); @@ -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() ); } } @@ -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",