#what-to-test-in-terraform
What is there to test in Terraform code at all?
Что отвечать
Several levels. (1) Static analysis: HCL validation, style, common mistakes with `terraform validate`, `fmt -check`, `tflint`. (2) Security and compliance, where the HCL matches policy: `checkov`, `trivy`, `tfsec`. (3) Unit tests with `terraform test` and mock providers, which checks HCL logic without a real apply. (4) Integration tests with terratest or `terraform test` against real providers in a sandbox account, which stands the infrastructure up, checks it, and tears it down. Not every project needs all of it; the more the HCL touches production, the deeper the pyramid.
Что хотят услышать
A senior should: - lay out the pyramid: many cheap static checks at the bottom, a few expensive integration tests at the top - say that a library module (shared across the org) needs unit and integration tests, while a root config for a single application is often fine with static plus security - separate "testing HCL" (expression logic, count, for_each) from "testing the infrastructure" (that AWS actually brought up what was expected) - mention that AWS tests are slow (minutes per apply), so isolate them in a separate scheduled CI job rather than every PR
Подводные камни
- ✗ Running integration tests on every PR without a sandbox account. In a week you get a bill and rate limits
- ✗ Thinking `terraform validate` is a 'test.' It is a syntax linter, not a test
- ✗ Covering 100% with static checks and skipping integration. You miss the bugs in how you interact with the provider
Follow-up
- ? What tests does a shared module used by 10 teams need?
- ? How does 'testing HCL' differ from 'testing real AWS'?
- ? Where do you draw the line: what runs in CI on every PR, what runs nightly?
Глубина в базе знаний