Slide 1

Slide 1 text

Cloud Integration Testing Made Easy for Your Java Apps Atlanta March 5th 2025 localstack.cloud

Slide 2

Slide 2 text

Anca Ghenade - Developer Advocate - [email protected] @tinyg210

Slide 3

Slide 3 text

Testing…

Slide 4

Slide 4 text

Some things cannot just easily be tested…

Slide 5

Slide 5 text

I was gonna show you my research on landing gear sensors…

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

So what’s the first thing we take away from this? Integration testing is important.

Slide 10

Slide 10 text

And second: it must also be realistic.

Slide 11

Slide 11 text

But how realistic?

Slide 12

Slide 12 text

The truth is we’ve gotten very used to being “fake”. ● Test data → a clean database on every test run. We get green tests with migration problems. ● Selenium → a fake user, using a mouse and keyboard, over and over again. We assume it’s realistic. ● Stamina → the runtime is always fresh for tests. What about subtle memory leaks? ● Scaling → tests run against a single process web server. Reality: a multi-thread environment.

Slide 13

Slide 13 text

Sometimes you have no choice.

Slide 14

Slide 14 text

WE INTERRUPT THE REGULAR SCHEDULED PRESENTATION TO BRING YOU THIS IMPORTANT STORY TIME

Slide 15

Slide 15 text

So my inspirations for this talk were our Code Cop and Justin Searls and his SCNA 2012 talk: To Mock or Not To Mock.

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

How do we test applications with dependencies in the cloud?

Slide 18

Slide 18 text

We want to keep it as real as possible when we’re testing our AWS-powered applications and the interaction between various components.

Slide 19

Slide 19 text

The reality: (of using a real cloud environment)

Slide 20

Slide 20 text

Your painful checklist for everything to consider: ● AWS Account: Ensure access to a test AWS account. ● Separate Test Env: Use a dedicated AWS environment. ● Data Isolation: Keep test data separate from production. ● Service Isolation: Use separate accounts or tagging to avoid conflicts. ● Cleanup: Ensure resources are cleaned up post-testing. ● CI Integration: Run tests automatically in CI/CD pipelines. ● IAM Permissions: Ensure test users/roles have the necessary permissions. ● Logging & Monitoring: Enable CloudWatch logs and metrics for debugging test failures. ● Secrets Management: Store and manage test credentials securely (e.g., AWS Secrets Manager, Parameter Store). ● Cost Management: Track and limit test resource usage to avoid unexpected costs. ● Test Parallelization: Ensure tests can run concurrently without conflicts. ● State Management: Handle persistent vs. ephemeral resources carefully.

Slide 21

Slide 21 text

So things are kind of… complicated. 󰤆

Slide 22

Slide 22 text

When it comes to testing, here are some common practices that we’re all guilty of. (At one point in our lives)

Slide 23

Slide 23 text

The “let’s prove it works”

Slide 24

Slide 24 text

Focus: ● You do some quick end-to-end and integration testing. ● Mocking only the necessities.

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

The “mocking the boundaries”

Slide 27

Slide 27 text

Focus: ● Mocking remote systems. ● Mostly unit and end-to-end testing. ● Reuse mocks and get tangled when anything changes.

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

Circling back to reality… reality is expensive, so we also want to test on a budget.

Slide 30

Slide 30 text

Sure mocks are cheap, but you might end up paying a little differently.

Slide 31

Slide 31 text

Mocking carries the risk of not getting updated and if we mock everything, aren’t we just making sure that the mocks work as expected?

Slide 32

Slide 32 text

How can we bring the ease and speed of unit tests into our integration tests?

Slide 33

Slide 33 text

What is LocalStack and how can it help you? A fully functional local cloud stack that was born out of these exact concerns.

Slide 34

Slide 34 text

● Enables a highly efficient dev & test loop for cloud apps ● Ships as a container image, easy to install and start up ● Support for 100+ services (and growing): ○ compute (Lambda, ECS, EKS) ○ various databases (DynamoDB, RDS) ○ messaging (SQS, Kinesis, MSK) ○ some sophisticated/exotic APIs (Athena, Glue) ● CI integrations & advanced collaboration features ● Branching out into other areas: Chaos Engineering, IAM Security Testing, Cloud Ephemeral Environments, 3rd Party Extensions, etc

Slide 35

Slide 35 text

How much of your system you can test Cloud emulation Staging environments Real AWS Mocking So how much reality can we have in cloud application testing? 3 4 Service emulation 2 1

Slide 36

Slide 36 text

I’ll show you some stuff

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

Using Spring Profiles…

Slide 39

Slide 39 text

Scenario 1 (Production) Scenario 2 (Development)

Slide 40

Slide 40 text

Scenario 1 Scenario 2

Slide 41

Slide 41 text

Scenario 1 Scenario 2

Slide 42

Slide 42 text

What about those integration tests?

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

What approach do we use for testing our apps?

Slide 46

Slide 46 text

1. The “my app changes frequently” example

Slide 47

Slide 47 text

Testing your application workflows

Slide 48

Slide 48 text

https://github.com/tinyg210/sf-java-meetup-demo

Slide 49

Slide 49 text

And your app runs against LocalStack.

Slide 50

Slide 50 text

2. The “my app is pretty much ready” example Source: https://github.com/tinyg210/stack-bytes-apigw-lambda-s3 Folder: github-actions-testcontainers

Slide 51

Slide 51 text

Test successful POST Test successful GET Test fails validation on incorrect values Test 404 on wrong param

Slide 52

Slide 52 text

Your tests will revolve around an HTTP client

Slide 53

Slide 53 text

And your app runs inside LocalStack.

Slide 54

Slide 54 text

Tips and Tricks Dump

Slide 55

Slide 55 text

There are several ways to provision your infrastructure for testing.

Slide 56

Slide 56 text

1. You do everything yourself. Programmatically with the AWS SDKs. (example: https://github.com/tinyg210/testcontainers-shipment-list-demo)

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

2. Use init hooks to create your AWS resources.

Slide 59

Slide 59 text

a. AWS CLI scripts via init hooks (example: https://github.com/tinyg210/stack-bytes-apigw-lambda-s3)

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

b. Terraform configuration files via init hooks.

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

3. Use LocalStack Cloud Pods

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

Locally…

Slide 66

Slide 66 text

There’s a catch: you need to use the same creation major version

Slide 67

Slide 67 text

Download on the fly… And see it in the webapp: https://app.localstack.cloud/pods

Slide 68

Slide 68 text

4. Use a mix of all of the above. 󰤆 ● spin up only desired services at startup ● manage other resources via SDK clients

Slide 69

Slide 69 text

Now how do you interact with the container?

Slide 70

Slide 70 text

Did I mention the SDKs?

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

Or you can use AWS CLI commands.

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

What about testing in CI?

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

Your local tests…

Slide 77

Slide 77 text

In CI…

Slide 78

Slide 78 text

Some More Tips and Tricks

Slide 79

Slide 79 text

Use a waiter to make sure your Lambdas are in state ACTIVE and not just created.

Slide 80

Slide 80 text

Source: https://aws.amazon.com/blogs/compute/coming-soon-expansion-of-aws-lambda-states-to-all-functions/

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

Wait for LocalStack logs to tell you when to start, so that your instance is in the right state before the tests are allowed to start.

Slide 83

Slide 83 text

No content

Slide 84

Slide 84 text

Streaming container output to an SLF4J logger.

Slide 85

Slide 85 text

No content

Slide 86

Slide 86 text

Lastly, shutting down your Lambda containers finally work!

Slide 87

Slide 87 text

Before

Slide 88

Slide 88 text

This is just me debugging btw.

Slide 89

Slide 89 text

Thank you, Eddu, for that workaround! 󰚦 󰚦 󰗨

Slide 90

Slide 90 text

After

Slide 91

Slide 91 text

Make use of all the configuration flags: https://docs.localstack.cloud/references/ configuration/

Slide 92

Slide 92 text

Thank You!