Testing serverless microservces


#1

Instead of being general I’ll ask the question using my specific examples.

So I have a few serverless projects, each representing a microservice. There are two in particular, the OrderService and RulesEngine. When a new order is create it needs to send it to the rules engine to have any rules applied.

Now there are a few different ways to do this, two of which I considered:

  1. OrderService says the order, publishes a NewOrder event via some message broker (queues, streams etc), RulesEngine picks its, applies the rules, publishes RuleApplied event, OrderService updates order.

  2. OrderService calls ‘applyRules’ on RulesEngine, gets response and saves it.

I know (1) scales better but we went with (2) because this is a MVP and we didn’t want the hassle of configuring SQS etc whatever, dealing with failures etc. It was just easier to have something that fails so we can debug it.

Anyways, how would I test this? I can run the OrderService in offline mode but I don’t have a great way to simulate the call to the RulesEngine. Is there a way to provide a mock RulesEngine lambda at run time?


#2

For my own unit testing I like to use serverless-mocha-plugin as well as aws-sdk-mock so that I can mock any AWS services. And this where using something like SNS would have been a boon because you could mock any number of responses and hard code some test event if your two services communicated via SNS instead of synchronously via http.

However, there is a node module I have used that allows me to capture outgoing http requests and “respond” with whatever I like called mitm (man in the middle). You should, ideally, have tests that have no dependancy on any other service be they external third party or your own so these modules are very useful. At least I have found they are:

https://www.npmjs.com/package/serverless-mocha-plugin
https://www.npmjs.com/package/aws-sdk-mock
https://www.npmjs.com/package/mitm

Hope that helps.


#3

That’s a great start, thanks!

I just noticed the SQS trigger support was added. We’re going to use those between services. Add in aws-sdk-mock and we should be off to the races


#4

So have you used mitm to do integration testing? II’m fiddling around with it right now. Is the general premise to write a mitm handler with a conditional looking for any calls writing to aws and then pipe those to the aws-mock library?


#5

@piersmacdonald mitm will capture all outgoing traffic you set it up to capture and then you can return whatever response you want. I only really use mitm when i cant use aws-sdk-mock; for example for other third party API’s. aws-sdk-mock does what mitm does essentially but is only for aws services and is a lot easier to setup which is why both are in my tool belt.