Skip to content

Commit

Permalink
test: re-instate localstack for s3 testing (#2024)
Browse files Browse the repository at this point in the history
* test: re-instate localstack for s3 testing

* refactor: clean up

* refactor: clean up
  • Loading branch information
paulcwarren authored Jul 10, 2024
1 parent 8bb2b76 commit 024e634
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 286 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ jobs:
# bash <(curl -s https://codecov.io/bash)
env:
AWS_REGION: ${{ secrets.AWS_REGION }}
AWS_ACCESS_KEY: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_KEY: ${{ secrets.AWS_SECRET_KEY }}

- name: Publish JARs
run: |
Expand All @@ -62,8 +60,6 @@ jobs:
GPG_KEY_NAME: ${{ secrets.GPG_KEY_NAME }}
GPG_PASSPHASE: ${{ secrets.GPG_PASSPHRASE }}
AWS_REGION: ${{ secrets.AWS_REGION }}
AWS_ACCESS_KEY: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_KEY: ${{ secrets.AWS_SECRET_KEY }}

- name: Publish Docs
run: |
Expand Down
52 changes: 0 additions & 52 deletions .github/workflows/prs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ jobs:
popd
env:
AWS_REGION: ${{ secrets.AWS_REGION }}
AWS_ACCESS_KEY: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_KEY: ${{ secrets.AWS_SECRET_KEY }}

- name: Save Maven Cache
id: cache-maven-save
Expand All @@ -44,54 +42,6 @@ jobs:
path: ~/.m2
key: ${{ runner.os }}-maven-${{ github.run_id }}

validate-with-examples:
runs-on: ubuntu-latest
needs: build
steps:
- name: Set up JDK 17
uses: actions/setup-java@v1
with:
java-version: 17
server-id: ossrh
server-username: MAVEN_USERNAME
server-password: MAVEN_PASSWORD
gpg-private-key: ${{ secrets.MAVEN_GPG_PRIVATE_KEY }}
gpg-passphrase: MAVEN_GPG_PASSPHRASE
- name: Restore Maven packages
uses: actions/cache@v3
with:
path: ~/.m2
key: ${{ runner.os }}-maven-${{ github.run_id }}
- uses: actions/checkout@v2
with:
path: spring-content
- name: Re-build spring-content
run: |
pushd spring-content
mvn install -DskipTests=true
popd
- uses: actions/checkout@v2
with:
repository: paulcwarren/spring-content-examples
path: spring-content-examples
ref: refs/heads/main
- name: Validate against Examples
run: |
pushd spring-content-examples
git fetch --all
git checkout main
SPRING_CONTENT_VERSION=${{ needs.build.outputs.spring_content_version }} mvn -B clean install
popd
env:
AWS_REGION: ${{ secrets.AWS_REGION }}
AWS_ACCESS_KEY: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_KEY: ${{ secrets.AWS_SECRET_KEY }}

# - name: Setup tmate session if anything fails
# if: ${{ failure() }}
# uses: mxschmitt/action-tmate@v3
# timeout-minutes: 360

validate-with-gettingstarteds:
runs-on: ubuntu-latest
needs: build
Expand Down Expand Up @@ -122,5 +72,3 @@ jobs:
popd
env:
AWS_REGION: ${{ secrets.AWS_REGION }}
AWS_ACCESS_KEY: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_KEY: ${{ secrets.AWS_SECRET_KEY }}
14 changes: 14 additions & 0 deletions spring-content-s3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,20 @@
<version>${hibernate-orm.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>localstack</artifactId>
<version>${test-containers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.awspring.cloud</groupId>
<artifactId>spring-cloud-aws-context</artifactId>
<version>2.4.4</version>
<scope>test</scope>
</dependency>

</dependencies>

<repositories>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
import static org.mockito.Mockito.verify;

import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

import internal.org.springframework.content.s3.it.LocalStack;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
Expand All @@ -41,7 +40,6 @@
import com.github.paulcwarren.ginkgo4j.Ginkgo4jRunner;

import internal.org.springframework.content.s3.io.S3StoreResource;
import internal.org.springframework.content.s3.it.S3StoreIT;
import lombok.Data;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.AwsCredentials;
Expand Down Expand Up @@ -240,13 +238,7 @@ public static class InfrastructureConfig {

@Bean
public S3Client client() throws URISyntaxException {
AwsCredentials awsCredentials = AwsBasicCredentials.create(env.getProperty("AWS_ACCESS_KEY"), env.getProperty("AWS_SECRET_KEY"));
StaticCredentialsProvider credentialsProvider = StaticCredentialsProvider.create(awsCredentials);

return S3Client.builder()
.region(Region.US_WEST_1)
.credentialsProvider(credentialsProvider)
.build();
return LocalStack.getAmazonS3Client();
}
}

Expand Down
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());
}
}
}
Loading

0 comments on commit 024e634

Please sign in to comment.