Course contentsShow
← Cloud and Infrastructure
Lesson 2881 of 2,98963. Infrastructure TestingFree lesson

Mocking and Stubbing Infrastructure

Creating mock providers and stubs to test IaC logic without provisioning real cloud resources during development.

Mocking and Stubbing Infrastructure

What you'll learn: How to test Infrastructure as Code logic without spinning up actual cloud resources by using mock providers and stubs.

Why Mock Infrastructure?

When developing IaC, you don't want every test run to provision real cloud resources—that's slow, costly, and risky. Mocking and stubbing let you simulate cloud provider responses and resource states, enabling you to validate your infrastructure logic quickly during development.

Think of it like testing a recipe: you don't need to cook the full meal every time to verify your instructions are correct. You can walk through the steps with imaginary ingredients to catch errors in your procedure.

Core Concepts

Mocking replaces actual cloud provider APIs with fake implementations that return predetermined responses. When your IaC code tries to create an S3 bucket, the mock provider pretends it succeeded without touching AWS.

Stubbing provides predefined data or state that your tests can use. For example, you might stub a "list of existing VPCs" response to test how your code handles conflicts or dependencies.

How It Works

With tools like Terratest, you can configure tests to use mock providers instead of real ones. Your Terraform or other IaC code runs normally, but calls to cloud APIs are intercepted and answered by your mocks. This allows you to:

  • Test error handling (simulate API failures)
  • Verify conditional logic (stub different resource states)
  • Validate configurations before provisioning
  • Run tests in CI/CD pipelines without cloud credentials

The mock returns what you tell it to return, letting you control every scenario without the overhead of real infrastructure.

Key Takeaway: Mocking and stubbing infrastructure testing separates your IaC logic validation from actual resource provisioning, enabling fast, safe, and cost-free development cycles while maintaining comprehensive test coverage.