-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: re-instate localstack for s3 testing (#2024)
* test: re-instate localstack for s3 testing * refactor: clean up * refactor: clean up
- Loading branch information
1 parent
8bb2b76
commit 024e634
Showing
7 changed files
with
196 additions
and
286 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
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
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
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
68 changes: 68 additions & 0 deletions
68
spring-content-s3/src/test/java/internal/org/springframework/content/s3/it/LocalStack.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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package internal.org.springframework.content.s3.it; | ||
|
||
import com.amazonaws.auth.AWSCredentials; | ||
import com.amazonaws.auth.AWSCredentialsProvider; | ||
import com.github.dockerjava.zerodep.shaded.org.apache.hc.core5.net.URIBuilder; | ||
import org.testcontainers.containers.localstack.LocalStackContainer; | ||
import org.testcontainers.utility.DockerImageName; | ||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; | ||
import software.amazon.awssdk.auth.credentials.AwsCredentials; | ||
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; | ||
import software.amazon.awssdk.services.s3.S3Client; | ||
|
||
import java.io.Serializable; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
|
||
public class LocalStack extends LocalStackContainer implements Serializable { | ||
|
||
private static final DockerImageName IMAGE_NAME = DockerImageName.parse("localstack/localstack"); | ||
|
||
private LocalStack() { | ||
super(IMAGE_NAME); | ||
withServices(Service.S3); | ||
start(); | ||
} | ||
|
||
private static class Singleton { | ||
private static final LocalStack INSTANCE = new LocalStack(); | ||
} | ||
|
||
public static S3Client getAmazonS3Client() throws URISyntaxException { | ||
return S3Client.builder() | ||
.endpointOverride(new URI(Singleton.INSTANCE.getEndpointConfiguration(LocalStackContainer.Service.S3).getServiceEndpoint())) | ||
.credentialsProvider(new CrossAwsCredentialsProvider(Singleton.INSTANCE.getDefaultCredentialsProvider())) | ||
.serviceConfiguration((bldr) -> bldr.pathStyleAccessEnabled(true).build()) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public URI getEndpointOverride(EnabledService service) { | ||
try { | ||
// super method converts localhost to 127.0.0.1 which fails on macos | ||
// need to revert it back to whatever getContainerIpAddress() returns | ||
return new URIBuilder(super.getEndpointOverride(service)).setHost(getContainerIpAddress()).build(); | ||
} catch (URISyntaxException e) { | ||
throw new IllegalStateException("Cannot obtain endpoint URL", e); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unused") // Serializable safe singleton usage | ||
protected LocalStack readResolve() { | ||
return Singleton.INSTANCE; | ||
} | ||
|
||
|
||
private static class CrossAwsCredentialsProvider implements AwsCredentialsProvider { | ||
private final AWSCredentials credentials; | ||
|
||
public CrossAwsCredentialsProvider(AWSCredentialsProvider provider) { | ||
this.credentials = provider.getCredentials(); | ||
} | ||
|
||
@Override | ||
public AwsCredentials resolveCredentials() { | ||
return AwsBasicCredentials.create(credentials.getAWSAccessKeyId(), credentials.getAWSSecretKey()); | ||
} | ||
} | ||
} |
Oops, something went wrong.