Trying out localstack with JUnit5
I was trying to fake Amazon Web Services
using localstack
by running it with Junit5
and this is what I did.
You can find the code for this example here: localstack-example.
For this you will need docker. I installed docker
on my mac with brew
using this post as a guide: How to install Docker on MacOS by Robin Wieruch which basically consists on running the following:
That is all the stuff we need to install, then we need to create the virtual machine with virtualbox
which will be running docker
.
And we start the virtual machine like this:
Now we need to run this:
Which will output something like this:
export DOCKER_TLS_VERIFY="1" export DOCKER_HOST="tcp://192.168.99.100:2376" export DOCKER_CERT_PATH="/Users/jsedano/.docker/machine/machines/default" export DOCKER_MACHINE_NAME="default" # Run this command to configure your shell: # eval $(docker-machine env)
We then run that last sentence to prepare our shell with the environment variables needed:
That is it for the docker
installation.
Now for localstack
we will use localstack-java-utils which already has a guide and some code we will use, but I had to add other stuff.
These are the dependencies needed:
And this is our test class:
I was trying to run the test with mvn clean verify
without setting up the hostNameResolver
but it was failing with the following error:
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 34.445 s <<< FAILURE! - in dev.jsedano.example.AppTest [ERROR] creatingBucketAndPuttingObject Time elapsed: 1.645 s <<< ERROR! com.amazonaws.SdkClientException: Unable to execute HTTP request: Connect to localhost.localstack.cloud:4566 [localhost.localstack.cloud/127.0.0.1] failed: Connection refused (Connection refused) at dev.jsedano.example.AppTest.creatingBucketAndPuttingObject(AppTest.java:29) Caused by: org.apache.http.conn.HttpHostConnectException: Connect to localhost.localstack.cloud:4566 [localhost.localstack.cloud/127.0.0.1] failed: Connection refused (Connection refused) at dev.jsedano.example.AppTest.creatingBucketAndPuttingObject(AppTest.java:29) Caused by: java.net.ConnectException: Connection refused (Connection refused) at dev.jsedano.example.AppTest.creatingBucketAndPuttingObject(AppTest.java:29)
So I did this, which admittedly seems a little hackish…
If you remember the output of docker-machine env default
there is an environment variable called DOCKER_HOST
which contains the ip address of the underlying docker running on the virtual machine, and we only need the ip part, that’s why we are using that substring.
There should be other ways to solve this, for example modifying the hosts
file, or mapping the address to localhost with something like nginx
.
After that this is the output of running
[INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
One of the things I couldn’t do was to run this test using IntelliJ
, there should be a way to set the environment variables (by telling IntelliJ to run eval $(docker-machine env)
).
Download the complete code from this example here localstack-example.