Mock providers in tests in Terraform - Time & Space Complexity
When testing Terraform code, we often use mock providers to simulate real cloud services.
We want to understand how the time to run tests changes as we add more mock resources.
Analyze the time complexity of creating multiple mock resources in tests.
provider "mock" {
# Mock provider configuration
}
resource "mock_resource" "example" {
count = var.resource_count
name = "test-${count.index}"
}
This code creates a number of mock resources equal to resource_count for testing.
Each mock resource creation triggers similar operations.
- Primary operation: Creating a mock resource via the mock provider API call.
- How many times: Once per resource, so
resource_counttimes.
As you increase the number of mock resources, the number of API calls grows proportionally.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The operations increase directly with the number of resources.
Time Complexity: O(n)
This means the time to run tests grows linearly with the number of mock resources created.
[X] Wrong: "Adding more mock resources won't affect test run time much because they are mocks."
[OK] Correct: Even mocks require processing and API calls, so more resources mean more work and longer test times.
Understanding how test complexity grows helps you write efficient tests and manage resources wisely in real projects.
"What if we batch multiple mock resources into a single resource block? How would the time complexity change?"