Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Testing Serverless Applications - AWS Summit Santa Clara

Testing Serverless Applications - AWS Summit Santa Clara

Testing serverless applications brings reduced risk, speeds up delivery and improved design of your serverless apps.

Key Takeaways

1. Many think testing serverless applications is unnecessary.
2. Testing is very important on all levels both unit, integrated and end-to-end-testing.
3. Testing speeds up software delivery and reduces risk.
4. Testing gives improved design of your serverless apps.
5. By testing and properly designing using Hexagonal architecture you’re even out of vendor lock-in.

Aleksandar Simovic

March 27, 2019
Tweet

More Decks by Aleksandar Simovic

Other Decks in Programming

Transcript

  1. simalexan AWS Summit 
 Santa Clara 2019 Business is 


    more profitable Focus on 
 business logic
  2. AWS Summit 
 Santa Clara 2019 simalexan It is serverless,

    
 the same way WIFI is wireless. Gojko Adzic
  3. AWS Summit 
 Santa Clara 2019 simalexan Aleksandar Simovic Senior

    Software Engineer @ ScienceExchange AWS Serverless Hero Co-author of “Serverless Applications with Node.js” book Core team member @ Claudia.js AWS SAM & Lambda Builders Contributor
  4. AWS Summit 
 Santa Clara 2019 simalexan • Jest •

    Jasmine • Mocha • any other popular tool For example, for Node.js you can use:
  5. AWS Summit 
 Santa Clara 2019 simalexan Integration tests are

    cheaper, but also more important, because a common serverless app is split into many small pieces
  6. AWS Summit 
 Santa Clara 2019 simalexan const AWS =

    require('aws-sdk'), stripe = require('stripe'), qs = require('querystring'), processResponse = require('./process-response'), stripe = require('stripe')(process.env.STRIPE_SECRET_KEY); exports.handler = (event) => { if (event.httpMethod === 'OPTIONS') { return Promise.resolve(processResponse(true)); } if (!event.body) { return Promise.resolve(processResponse(true, 'invalid', 400)); } const newCharge = qs.parse(event.body); if (!newCharge.amount || !newCharge.currency) { return Promise.resolve(processResponse(true, 'invalid arguments, please provide amount and currency fields as mentioned in the app README', 400)); } return stripe.charges.create({ source: newCharge.stripeToken, amount: newCharge.amount, currency: newCharge.currency, description: 'Charge Description' }) .then(charge => processResponse(true, {charge})) .catch((err) => { console.log(err); return processResponse(true, 'stripe-error', 500); }); };
  7. AWS Summit 
 Santa Clara 2019 simalexan One of the

    purposes of testing is to Reduce Risk
  8. AWS Summit 
 Santa Clara 2019 simalexan • configuration risks

    • technical workflow risks • business logic risks • integration risks
  9. AWS Summit 
 Santa Clara 2019 simalexan How do we

    write testable functions
 to address those risks
  10. AWS Summit 
 Santa Clara 2019 simalexan Alistair Cockburn Allow

    an application to equally be driven by users, programs, automated test or batch scripts, and to be developed and tested in isolation from its eventual run-time devices and databases.
  11. AWS Summit 
 Santa Clara 2019 simalexan const pubsubRepository =

    require('./pubsub-repository'), paymentProcessorRepository = require('./payment-processor'), TOPIC_ARN = process.env.TOPIC_ARN; module.exports = function chargeCustomer(secretKey, token, email, amount, currency, description = 'Charge Description', paymentProcessor = paymentProcessorRepository, pubsub = pubsubRepository) { let createdCharge; return paymentProcessor.createCharge(secretKey, token, amount, currency, description) .then(chargeResponse => { createdCharge = chargeResponse; createdCharge.email = email; return pubsub.publish(createdCharge, TOPIC_ARN); }) .then(() => createdCharge) .catch((err) => { console.log(err); throw err; }); }
  12. AWS Summit 
 Santa Clara 2019 simalexan const AWS =

    require('aws-sdk'), sns = new AWS.SNS(), NO_DATA_REPLY = 'You must provide data to the your PubSub'; module.exports = { publish: function publish(data, topic, pubSub = sns) { if (!data) { return Promise.reject(NO_DATA_REPLY); } return pubSub.publish({ Message: JSON.stringify(data), TopicArn: topic }) .promise() .catch(err => { console.log(err); throw err; }); } };
  13. AWS Summit 
 Santa Clara 2019 simalexan const convertFromSentry =

    require('../src/convert-from-sentry'), lambdaProxyEvent = require('./test-events/lambda-proxy-event'), wrapEvent = (event) => { const result = JSON.parse(JSON.stringify(lambdaProxyEvent)); result.body = JSON.stringify(event); return result; }; describe('Convert from Sentry', () => { describe('Error type', () => { test('should pickup the error type if defined', () => { const event = wrapEvent(require('./test-events/raven-exception')); expect(convertFromSentry(event).type).toBe('TypeError'); }); test('should use runtime error type if undefined', () => { const event = wrapEvent(require('./test-events/raven-manually-tracked')); expect(convertFromSentry(event).type).toBe('RuntimeError'); }); }); describe('Error message', () => { test('should pickup the message if exception value defined', () => { const event = wrapEvent(require('./test-events/raven-exception')); expect(convertFromSentry(event).message).toBe(`Cannot read property 'captureException' of undefined`); }); test('should use body message if exception value undefined', () => { const event = wrapEvent(require('./test-events/raven-manually-tracked')); expect(convertFromSentry(event).message).toBe('error'); }); }); });
  14. AWS Summit 
 Santa Clara 2019 simalexan If you test

    and properly design using Hexagonal architecture you’re even out of “vendor lock-in”.
  15. AWS Summit 
 Santa Clara 2019 simalexan Testing is very

    important on all levels both unit, integrated and end-to-end-testing.
  16. AWS Summit 
 Santa Clara 2019 simalexan • hexagonal architecture

    • be smart • test integrations that you own • or test your code with a different adapter
  17. AWS Summit 
 Santa Clara 2019 simalexan Benefits of serverless

    are cheap infrastructure and easy fast parallelization
  18. AWS Summit 
 Santa Clara 2019 simalexan Use your favorite

    deployment tool from CI to deploy your serverless app
  19. AWS Summit 
 Santa Clara 2019 simalexan • Jenkins •

    Travis CI • AWS CodePipeline • Semaphore CI • and many more
  20. AWS Summit 
 Santa Clara 2019 simalexan Automate deployment from

    git branches to different stages (ie. production, dev, staging, qa)
  21. AWS Summit 
 Santa Clara 2019 simalexan Testing your serverless

    apps quickens your software delivery even more.
  22. AWS Summit 
 Santa Clara 2019 simalexan Hexagonal Architecture 3

    months > 2 weeks Unit / Integration testing + =
  23. AWS Summit 
 Santa Clara 2019 simalexan • Testing your

    serverless applications is important • One of the goals of testing is to reduce risk • use hexagonal architecture to stay flexible • Hexagonal Architecture also significantly removes the “vendor lock-in” • use CI/CD for tests and deployment to different stages • Testing serverless applications speeds up delivery even more • use mature and tested deployment tools, and track and analyze errors • serverless helps testers to do their job faster and cheaper Key takeaways