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

Testcontainers and API mocking with WireMock for C/C++ projects

Testcontainers and API mocking with WireMock for C/C++ projects

Testcontainers has become one of the most popular tools for software integration testing. If you can put your system-under-test into a container, Docker compose cluster or a pod, this is what you're likely to use. If your target isn't ready, not containerizable or just too heavy for testing as is, you can always use API mocking like WireMock to mock your interfaces including REST API, gRPC, etc. But are these tools available to C/C++ developers? And the answer is YES!

In September 2023 I created Testcontainers for C/C++. This is not a standalone Testcontainers engine, but a C-style shared library adapter for native languages like C/C++, and also D, Lua, Swift, etc. It is a MVP SDK that can be later extended for the languages. The project is based on Testcontainers for Go which is one of the most powerful Testcontainers implementations. And yes happy to chat about how it does [and doesn’t] work under the hood.

The objective of this talk is to present the existing solution, find early adopters and get feedback from professional C/C++ developers who use modern C/C++. For me it has been 10 years since I used it professionally for Embedded and SCADA projects, so it would be awesome to get the community feedback on use-cases and priorities!

Oleg Nenashev

November 29, 2023
Tweet

More Decks by Oleg Nenashev

Other Decks in Technology

Transcript

  1. 2

  2. Integration testing? 4 • Multiple technology stacks and toolchains •

    Many teams involved • Sacred knowledge • [Almost] always too late
  3. 7

  4. > whoami @oleg_nenashev oleg-nenashev Dr. Nenashev / Mr. Jenkins Developer

    tools hacker Community builder & DevRel consultant #RussiansAgainstPutin #StandWithUkraine
  5. 13

  6. 14 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
  7. 17 WireMock 101 WireMock is a FOSS tool for building

    mock APIs Created in 2011 as a Java test library and CLI Available beyond Java, inc Rust or Golang You can can: • Create stable development environments • Isolate yourself from flakey 3rd parties • Simulate APIs that don't exist yet wiremock.org
  8. WireMock Cloud by WireMock Inc. 20 • WireMock creator is

    a co-founder of the Inc. • WireMock Cloud - SaaS for end-to-end API mocking • Private beta: K8s Edition for managed / on-premises • API co-development wiremock.io
  9. 26

  10. 27

  11. 30

  12. Ways to do API integration testing 1. Testing against Production/Staging

    servers 2. Testing against a simplified/containerized instance (e.g. Testcontainers) 3. Mocking at the API provider level (e.g. WireMock) 4. Mocking at the code level 32 Slow Fast Accu- rate Nope
  13. Ways to do API integration testing 1. Testing against Production/Staging

    servers 2. Testing against a simplified/containerized instance (e.g. Testcontainers) 3. Mocking at the API provider level (e.g. WireMock) 4. Mocking at the code level 33 Slow Fast Nope Accu- rate
  14. 34 Build Unit tests Publish Reports Integration tests Publish Reports

    NOW “Shift Left” Fast integrations tests are critical
  15. Docker • Popular container engine • Developer-friendly • Huge ecosystem

    • DockerHub and Container Registries • Docker Compose – multi-container apps • Universal image format (OCI) https://www.docker.com/
  16. 41

  17. 43

  18. Containers are not always slow! • On-demand image build •

    Caching Docker image builds • Suspending containers between tests • Graceful termination ◦ github.com/testcontainers/moby-ryuk 44 testcontainers.org
  19. Testcontainers for C/C++ 48 Started by the WireMock Team in

    September 2023 We have Embedded/Automotive customers and users who need it Based on Testcontainers or Go WireMock module gh:testcontainers/testcontainers-c
  20. Example - WireMock with C code 49 github.com/testcontainers/testcontain ers-c/tree/main/examples/wiremock •

    Testcontainers C is an experimental project • Feedback is welcome! • See more examples in the repository: CppUnit, OpenSCADA, etc.
  21. Under the Hood 52 Testcontainers for Go C Adapter in

    Golang go build -buildmode=c-shared https://pkg.go.dev/cmd/cgo cgo C Header add-ons Header Files Test Code Your Project Test Executable
  22. Using Testcontainers in C/C++ projects • Built with CMake •

    Any CMake-compatible package manager • FetchContent • CPM.make Coming soon: Conan, vcpkg 53 github.com/cpm-cmake/CPM.cmake
  23. APIs are essential for Integration Testing 55 GROWTH IN WEB

    APIS SINCE 2005 Over 90% of developers use APIs. Skyrocket growth of APIs JANUARY 2006 JANUARY 2008 JANUARY 2010 JANUARY 2012 JANUARY 2014 JANUARY 2016 JANUARY 2018 MONTH The growth over time of the Programmable Web API API directory to more than 22,000 entries 22000 20000 18000 16000 14000 12000 10000 8000 6000 4000 2000 0 TOTAL API COUNT Programmable Web * Gartner Hype Cycle for APIs, 2022
  24. Ways to do API integration testing 1. Testing against Production/Staging

    servers 2. Testing against a simplified/containerized instance (e.g. Testcontainers) 3. Mocking at the API provider level (e.g. WireMock) 4. Mocking at the code level 59 Slow Fast Accu- rate Nope
  25. 60

  26. 62

  27. WireMock for Go Testcontainers Module REST API Client Go test

    integrations go.wiremock.org 63 github.com/wiremock/wiremock-testcontainers-go
  28. Testcontainers for C/C++ • Support for more features from Testcontainers

    for Go • C++ adapter with classes • Conan and Vcpkg distributions • Doxygen & docs 72 gh:testcontainers/testcontainers-c
  29. 74

  30. Takeaways 75 • Shift left your integration testing • Mocking

    APIs and services, reducing external dependencies, containers • There are tools for that, including WireMock and Testcontainers • C/C++ is no exception from the trends
  31. 76

  32. 79

  33. Credits to • All WireMock contributors • WireMock Inc. Team

    • All Testcontainers contributors and AtomicJar folks • All FOSS contributors 81