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

Workshop - Mocks as Code. Modeling services in ...

Workshop - Mocks as Code. Modeling services in Config Management integration tests

When managing service configurations at scale, we often need to automate testing of these configurations. In software integration testing we can use Testcontainers - a popular framework for Docker and other container engines. For public cloud environments, there is LocalStack. And for API service providers, we have WireMock. Can we use the same toolchain for configuration management? If you use modern Infrastructure-as-Code tools like Pulumi, yes!

In this edition, we will use Java or Kotlin for EVERYTHING, from the application itself and all the way up to Infrastructure as Code definitions and tests. The workshop also leverages capabilities offered by Gradle, especially for build automation and troubleshooting.

In this workshop, we will go through the following topics:
1. Containerizing Spring app development
2. Integration testing with Testcontainers and LocalStack
3. Mocking APIs with WireMock
4. Infrastructure as Code (IaC) with Pulumi and Pulumi for Java
5. Testing IaC locally, with all tools above combined
6. Advanced troubleshooting with Gradle and Gradle Build Scan

See https://github.com/oleg-nenashev/mocks-as-code-demo for the demo repository we will be using in this workshop. It also contains all the links to the used tools and additional learning materials.

Oleg Nenashev

May 14, 2024
Tweet

Resources

Demo Repo

https://github.com/oleg-nenashev/mocks-as-code-demo

Repository with all the demos

More Decks by Oleg Nenashev

Other Decks in Programming

Transcript

  1. > whoami @oleg_nenashev oleg-nenashev Dr. Nenashev / Mr. Jenkins Community

    builder Developer Tools Hacker #RussiansAgainstPutin #StandWithUkraine
  2. Staging & QA Tests Build Integration tests Build Tests Gaining

    confidence in you software “Shift Left” with modern tools
  3. 10 Java All the Way Down Up Application Unit Tests

    Build Definition and Pipelines Integration Tests Infrastructure-as-Code Deployment Pipelines Deployment Tests Production Health Checks Java the stack up all the way
  4. Today 11 1. Containerizing Spring App Development 2. Integration testing

    with Testcontainers and LocalStack 3. Mocking APIs with WireMock 4. Infrastructure as Code (IaC) with Pulumi 5. Testing IaC locally 6. Advanced Troubleshooting Localstack
  5. Workshop Prerequisites • Internet :( • Java 17 & your

    favorite IDE • Testcontainers-compatible Docker environment ◦ Tested on: Docker Desktop ◦ You can use: Podman, Colima, Rancher Desktop (list) ◦ OR: Testcontainers Cloud • Pulumi CLI + registration ◦ Guide: www.pulumi.com/docs/install/ • Nice2Have: Gradle Build Scan account ◦ scans.gradle.com/ 13
  6. Credits 16 • Siva Prasad Reddy and Oleg Shelajev, Docker

    • The demos are based on: ◦ github.com/testcontainers/tc-guide-testing-aws-service-integrations-using-loca lstack ◦ github.com/testcontainers-community/testcontainers-wiremock-demo
  7. Ways to do API integration testing 1. Testing against Production/Staging

    servers 2. Testing against a simplified/containerized instance (e.g. Testcontainers) 3. Modeling (aka Mocking) at the API provider level (e.g. WireMock or Microcks) 4. Mocking at the code level Slow Fast Nope Accu- rate * For Testcontainers, see the slides
  8. LocalStack • Local AWS Services emulation OR mocking • “awslocal”

    CLI utility • SDKs for languages and test frameworks 29 docs.localstack.cloud/
  9. Some of Them 31 • Emulated: S3, SQS, … •

    Mocked: EC2 (in the FOSS lib) • Thoughts & Prayers: AWS GreenShift ;) • …
  10. Ways to do API integration testing 1. Testing against Production/Staging

    servers 2. Testing against a simplified/containerized instance (e.g. Testcontainers) 3. Modeling (aka Mocking) at the API provider level (e.g. WireMock or Microcks) 4. Mocking at the code level Slow Fast Nope Accu- rate * For Testcontainers, see the slides
  11. wiremock.org/docs/stubbing IF (request_url) THEN (response) * * it gets MUCH

    more complex WireMock Config JSON: Client library response request Client App Mock API Server HTTP/2 API Mocking 101
  12. API Mocking Tools Main implementation in Java Native lib, Docker

    images and Testcontainers modules Support for GraalVM Rich Ecosystem Sandbox CNCF Project Golang / Microservice arch Docker image, Testcontainers module, All-in K8s Charts Less Rich Ecosystem
  13. WireMock Ecosystem / Java • Java • Kotlin • Scala

    • Groovy • Clojure • (?)Android • JUnit 4/5 • Testcontainers • Sprint Boot • Spring Cloud Contract • Quarkus • Maven/Gradle • Spock • Pact
  14. 46

  15. Enter -as-Code Tools! • Infrastructure as Real Code • SDKs

    for programming languages • Standard IDEs • Standard Developer Tools • Extensibility via libraries 49
  16. 55 Java All the Way Down Up Application Unit Tests

    Build Definition and Pipelines Integration Tests Infrastructure-as-Code Deployment Pipelines Deployment Tests Production Health Checks Java the stack up all the way
  17. 67 OpenTelemetry plugin for Gradle • Injecting traces and spans

    to the Gradle execution • Helps to find the slowest parts of your build with the Gradle OpenTelemetry build plugin. • Created by Craig Atkinson • craigatk/opentelemetry-gradle-plugin
  18. 70

  19. • For many open source projects: Gradle sponsors Develocity licenses/hosting,

    helps with build optimization and developer productivity • Maven, Bazel and sbt projects welcome gradle/develocity-oss-projects
  20. Beware Pulumi for AWS Native • AWS Native configuration is

    not applied for endpoints • APIs are slightly different in the package 78
  21. 79

  22. 80

  23. 82

  24. 83 Java All the Way Down Up Application Unit Tests

    Build Definition and Pipelines Integration Tests Infrastructure-as-Code Deployment Pipelines Deployment Tests Production Health Checks Java the stack up all the way
  25. What I’m looking forward to? • Feedback from Java developers

    • Is Infra-as-Code with Java interesting to you? • Do you want Pulumi-LocalStack for Java productization? • Any other feedback on Gradle :)
  26. References • Main Demo: github.com/oleg-nenashev/mocks-as-code-demo • Gradle Build Scan: scans.gradle.com

    • Credits: github.com/testcontainers/tc-guide-testing-rest-api-integrations-u sing-wiremock