Architecture as Code · Module 4 · Delivery & Operations

Testing Strategies for Infrastructure as Code

The lesson builds a testing strategy for codified infrastructure. It covers the test pyramid, unit testing with mocks and built-in test frameworks, a shared pipeline scaffold for Pulumi and Terraform, testing of configuration generators and validators with Vitest, integration and environment parity testing, chaos experiments with guardrails, security and compliance testing with preventive and detective policy evaluation, performance testing, and traceability through Requirements as Code.

Lesson 17 of 31Chapter 1390 minutes

Read the chapterThe whitepaperThe slides

All courses / Architecture as Code / Module 4 / Chapter 13

Learning objectives

After the lesson the participant can:

Key concepts

Architecture as Code test pyramid
A structured progression from rapid developer tests to comprehensive integration and end-to-end validation, each layer serving a distinct quality purpose. The multilayered approach finds problems early, when they are cheaper and simpler to fix.
Unit testing without deployment
Validation of individual modules and resources without deploying infrastructure. Tests check resource configurations, variable validations, output consistency and module interface contracts, using mocks so that assertions run before any cloud cost is incurred.
Shared pipeline scaffold
Four complementary stages, lint and format, contract and unit tests, policy enforcement and integration rehearsal, implemented for both Pulumi and Terraform paths so that both pass the same assurance gates and platform parity is maintained.
Preventive and detective policy evaluation
Open Policy Agent evaluates policies preventively in the pipeline, before deployment. AWS Config Rules are detective by default, evaluating resources after they have been deployed.
Chaos experiments as code
Failure scenarios such as instance loss or degraded dependencies described alongside infrastructure definitions. Failure injection policies, scheduling rules and opt-in environments live in version control, which keeps the blast radius controlled and every change reviewable.

Interactive exercises

Do them here and get feedback at once. Choose a card and then where it belongs, or drag it.

Interactive exercise 1 · Sort

Commands in the shared pipeline scaffold

Sort each command or check into the stage of the shared Pulumi and Terraform scaffold where the chapter's table places it.

Interactive exercise 2 · Scenario

A proposal for a chaos experiment

Read the situation and choose what the chapter's reasoning leads to. Each choice gives feedback.

Teaching plan

15 minTeacher presents the test pyramid and the dimensions of infrastructure testing, then unit testing with mocks on programmatic and declarative tools.
15 minWalk through the four-stage scaffold table and the Vitest practices table, including the 80 per cent coverage guideline and the under 100 milliseconds rule for unit tests.
15 minTeacher covers integration and parity testing, chaos experiments with their four guardrails, security, compliance and performance testing, and requirements traceability.
35 minExercise in groups: write a test strategy for one infrastructure module and present the order of investment.
10 minPlenary on the testing strategy quadrant: where to start and where end-to-end tests are worth their cost.

Discussion

  1. The chapter says each pipeline stage should block deployment on critical failures while allowing warnings to proceed. How would you decide which findings are hard failures in your organisation?
  2. Why does the chapter warn that coverage goals should focus on meaningful tests and not on the metric?
  3. What must be communicated, and to whom, before a fault is injected deliberately into an environment?

Written exercise

Exercise · 35 minutes

A test strategy for one infrastructure module

Choose one reusable infrastructure module in your own organisation, for example a network, a storage bucket or a database. Write its test strategy across the pyramid. List five unit tests that need no deployment, naming the property each asserts, such as an approved region, mandatory tags or encryption. Define the four pipeline stages for the module and what blocks at each. Add two policy tests, one positive and one negative. Describe one integration test in a temporary environment with automated clean-up. Finally describe one chaos experiment with the chapter's guardrails: lower environment first, limited concurrent faults, announced window and how the learning is recorded. This is a design exercise on your own systems.

Deliverable: A two-page test strategy with five unit tests, a four-stage pipeline table, two policy tests, one integration test and one guarded chaos experiment.

Self-test

Choose an answer to see whether it is right, or open the answer below each question.

  1. According to the chapter, at what point does Open Policy Agent evaluate policies, in contrast to AWS Config Rules?

    Show the answer

    B. Preventively in the pipeline, before deployment
    The chapter notes that the two act at different points: Open Policy Agent evaluates preventively in the pipeline before deployment, while AWS Config Rules are detective by default.

  2. What should unit tests for codified infrastructure validate, according to the chapter?

    Show the answer

    D. Resource configurations, variable validations, outputs and interface contracts
    Unit tests validate resource configurations, variable validations, output consistency and module interface contracts without deploying infrastructure. Load, failover and end-to-end checks belong to higher layers.

  3. Which order of investment does the chapter's summary suggest, based on the testing strategy quadrant?

    Show the answer

    A. Start with security scanning and policy testing, then add end-to-end tests where risk justifies it
    Security scanning and policy testing offer high coverage with moderate complexity, so the chapter suggests starting there and adding end-to-end tests where the risk justifies their cost.