Giter VIP home page Giter VIP logo

spring-localstack's Introduction

A Simple Localstack and Spring Integration

Build Status PyPI License

About Localstack

LocalStack is a fully functional local AWS cloud stack. It provides an easy-to-use test/mocking framework for developing Cloud applications. Currently, the focus is primarily on supporting the AWS cloud stack.

You can checkout more about here.

Warning: Project Discontinued

This project was discontinued, and won't be receiving any updates. As per its license, you are free to fork it, and take ownership.

Distribution

This library is distributed under Apache 2.0 license and it is available as an artifact in Maven Central. You can just simply add the following dependency to your project.

Maven:

<dependency>
	<groupId>xyz.fabiano</groupId>  
	<artifactId>spring-localstack</artifactId>  
	<version>0.0.9</version>
</dependency>

Gradle:

compile 'xyz.fabiano:spring-localstack:0.0.9'

Requirements

Before you get up and running, you must meet the following requirements:

  • Java (1.8 or greater)
  • Spring (Starting from 4.x)
  • Docker (service running)

Spring Boot Configuration

To activate the auto-configuration with Spring Boot, just add the following property:

spring.localstack.enabled=true  

When you get your application running, this property will get up a docker image with Localstack and instantiate beans for each AWS client already with the needed configuration for use them with Localstack. Then, you can just inject them like this:

@Component
public class MyComponent {
	@Autowired  
	private AmazonS3 amazonS3;

	...
	
	public uploadFileToS3(File file) {
		PutObjectRequest request = new PutObjectRequest("my-bucket", "my-data", file);  
		amazonS3.putObject(request);
	}
}

Below, you can check it out all available configuration options for this library:

##
#   Enables Localstack auto-configuration and runs it when getting Spring up.
#   The auto-configuration already creates all the AWS clients with the endpoint configuration,
#   so you do not need to instantiate or configure then, just inject it.
##
spring.localstack.enabled=true

##
#   Enables Async clients and creates it in the Spring Context
##
spring.localstack.async-clients.enabled=true

##
#   This tells the application for always look for a new Docker image if available.
##
spring.localstack.pull-new-image=true

##
#   This tells the application to pull a particular version of Docker image only. Default latest.
##
spring.localstack.version=0.9.2

##
#   With this option you can specify which services you want to run with Localstack.
#   This makes the Localstack container more lightweight.
##
spring.localstack.services=sqs,sns

##
#   Enable randoms ports for the Localstack container. It's a nice option to avoid port conflicts.
#   If you use the client beans, you do not need to worry about the endpoints and ports,
#   it will already configured with the random ports.
##
spring.localstack.random-ports=true

##
#   You can specify which is the Docker host for the container to use.
##
spring.localstack.external-host=localhost

##
#   You can specify is container will be removed after stop. Default: true
##
spring.localstack.auto-remove=false

##
#   You can specify the extra-options for the container.
##
spring.localstack.extra-options=

##
#   You can specify which is the region. Default: us-east-1
##
spring.localstack.region=sa-east-1

Testing with JUnit Runner

Warning: This option is for testing and you should use it when you do NOT have Localstack integrated with Springboot. if you are using Springboot integrated with Localstack you should use the SpringRunner.class.

This option will fit you if you wish to get Spring Context and the Localstack container at the same time for this particular test. The class SpringLocalstackDockerRunner is a JUnit runner with the purpose of testing integrated with Localstack and Spring. With this runner, we can compound with @SpringLocalstackProperties for more extensible configuration. Check it out:

...
import xyz.fabiano.spring.localstack.LocalstackService;
import xyz.fabiano.spring.localstack.annotation.SpringLocalstackProperties;
import xyz.fabiano.spring.localstack.junit.SpringLocalstackDockerRunner;

@RunWith(SpringLocalstackDockerRunner.class)
@SpringLocalstackProperties(services = { LocalstackService.S3 })
@ContextConfiguration(classes = SpringTestContext.class)
public class SpringWithLocalstackExampleTest {
	@Autowired
	private AmazonS3 amazonS3;

	@Test
	public void testIfHas10Files() {
	    ...
	    
	    ObjectListing listing = amazonS3.listObjects("my-bucket", "a-preffix");
	    assertThat(listing.getObjectSummaries().size(), is(10));
	}
}

And the context configuration:

...
import xyz.fabiano.spring.localstack.legacy.LocalstackDocker;
import xyz.fabiano.spring.localstack.support.AmazonDockerClientsHolder;

@Configuration
public class SpringTestContext {

    @Bean
    public AmazonDockerClientsHolder amazonDockerClientsHolder() {
        return new AmazonDockerClientsHolder(LocalstackDocker.getLocalstackDocker());
    }

    @Bean
    public AmazonS3 amazonS3(AmazonDockerClientsHolder amazonDockerClientsHolder) {
        return amazonDockerClientsHolder.amazonS3();
    }
}

spring-localstack's People

Contributors

cadorfo avatar dependabot[bot] avatar fabianoxyz avatar galhardi avatar soelvar avatar vitor-moraes-ifood avatar vitornp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

spring-localstack's Issues

Feature to be able to attach to an already running container with default port numbers

It would be very useful to have an auto conf feature to be able to attach to an already running container with localstack default port numbers when testing integration between applications locally. I guess there are a couple of different options e.g.

  • specificy configuration attach only

  • allow fallback to attach when other instance already running.

  • no attach.

Please let me know what you think - I would be happy to help.

Localstack container still running at start of new test

I'm getting the following error:

Caused by: java.lang.IllegalStateException: A docker instance is starting or already started.

This is because I have 3 service classes, each having their own test class, and when I run all the tests together in my project Localstack is started for the first (I think by @SpringBootTest) but does not close down at the end.

Is there some way to limit the run scope? or perhaps I need to configure my application/tests differently?

Any suggestions you can give would be appreciated.

Unknown port mapping for service

Hi, I've looked at similar issues etc., and maybe my code isn't set up right, but after many different attempts I still cannot get this to work.

I have followed the example on the readme (added the library to my pom and spring.localstack.enabled=true) but whenever I run my test I get the "Unknown port mapping for service" error which from code appears to be trying to find a file somewhere under /opt/code/localstack/. I'm running on OSX and this folder does not exist, even if I have installed localstack on the machine using pip. Please explain how this will ever work.

My test looks like this:

`
@SpringBootTest
class ComSpreadsheetRetrieverTest {

@Autowired
private AmazonS3 amazonS3;

@Autowired
private ComSpreadsheetRetriever comSpreadsheetRetriever;

@BeforeEach
void setUp() {
    amazonS3.createBucket("comSpreadsheets");
    File spreadsheet = new File("src/test/resources/single_item_com_day_report.xlsx");
    amazonS3.putObject("comSpreadsheets", spreadsheet.getName(), spreadsheet);
}

@Test
void canRetrieveSpreadsheetFromS3() {
    File file = comSpreadsheetRetriever.getSpreadsheetFile();
    assertTrue(file.exists());
    assertEquals("single_item_com_day_report.xlsx", file.getName());
}

@TestConfiguration
public static class Sq3TestConfiguration {
    @Bean
    public LocalstackDocker localstackDocker() {
        return LocalstackDocker.getLocalstackDocker();
    }

    @Bean
    public AmazonDockerClientsHolder amazonDockerClientsHolder(LocalstackDocker localstackDocker) {
        return new AmazonDockerClientsHolder(localstackDocker);
    }
}

`

Latest version not found in maven repo

This is not working as it's not be available in maven central.

<dependency>
	<groupId>xyz.fabiano</groupId>  
	<artifactId>spring-localstack</artifactId>  
	<version>0.0.10</version>
</dependency>

Mapping port issue

at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.3.7.RELEASE.jar!/:4.3.7
.RELEASE]
... 26 common frames omitted
Caused by: java.lang.IllegalArgumentException: Port: 4585 does not exist
at xyz.fabiano.spring.localstack.legacy.Container.lambda$getExternalPortFor$1(Container.java:90) ~[spring-localstack-0.0.10.jar!/:na]
at java.util.Optional.orElseThrow(Optional.java:290) ~[na:1.8.0_201]
at xyz.fabiano.spring.localstack.legacy.Container.getExternalPortFor(Container.java:90) ~[spring-localstack-0.0.10.jar!/:na]
at xyz.fabiano.spring.localstack.legacy.LocalstackDocker.endpointForPort(LocalstackDocker.java:208) ~[spring-localstack-0.0.10.jar!/:na]
at xyz.fabiano.spring.localstack.legacy.LocalstackDocker.endpointForService(LocalstackDocker.java:202) ~[spring-localstack-0.0.10.jar!/:na]
at xyz.fabiano.spring.localstack.legacy.LocalstackDocker.getEndpointStepFunction(LocalstackDocker.java:189) ~[spring-localstack-0.0.10.jar!/:na]
at xyz.fabiano.spring.localstack.support.AbstractAmazonDockerClientsHolder.decorateWithConfigsAndBuild(AbstractAmazonDockerClientsHolder.java:35) ~[spring-localstack
-0.0.10.jar!/:na]
at xyz.fabiano.spring.localstack.support.AmazonDockerClientsHolder.awsStepFunctions(AmazonDockerClientsHolder.java:157) ~[spring-localstack-0.0.10.jar!/:na]
at xyz.fabiano.spring.localstack.autoconfigure.EveryAwsClientAutoConfiguration.awsStepFunctions(EveryAwsClientAutoConfiguration.java:111) ~[spring-localstack-0.0.10.
jar!/:na]
at xyz.fabiano.spring.localstack.autoconfigure.EveryAwsClientAutoConfiguration$$EnhancerBySpringCGLIB$$8e0061d3.CGLIB$awsStepFunctions$7() ~[spring-locals
tack-0.0.10.jar!/:na]
at xyz.fabiano.spring.localstack.autoconfigure.EveryAwsClientAutoConfiguration$$EnhancerBySpringCGLIB$$8e0061d3$$FastClassBySpringCGLIB$$cbc9ad80.invoke()
~[spring-localstack-0.0.10.jar!/:na]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-4.3.7.RELEASE.jar!/:4.3.7.RELEASE]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358) ~[spring-context-4.3.7.RELE
ASE.jar!/:4.3.7.RELEASE]
at xyz.fabiano.spring.localstack.autoconfigure.EveryAwsClientAutoConfiguration$$EnhancerBySpringCGLIB$$8e0061d3.awsStepFunctions() ~[spring-localstack-0.0
.10.jar!/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_201]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_201]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_201]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.