一键导入
mock-testing
Execute and debug Terraform mock tests for Azure CAF modules. Use this skill when user asks to test a module, run tests, or debug test failures.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Execute and debug Terraform mock tests for Azure CAF modules. Use this skill when user asks to test a module, run tests, or debug test failures.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Validate Azure Cloud Adoption Framework (CAF) naming conventions in Terraform modules and examples. Use this skill to ensure resource names follow Microsoft CAF standards and prevent naming conflicts.
Add Azure Monitor diagnostic settings to existing Terraform modules. Use this skill when adding monitoring, logging, or diagnostics capabilities to a module that supports Azure diagnostic settings.
Add Azure Private Endpoint integration to Terraform modules for secure, private network connectivity. Use this skill when adding private networking capabilities to modules that support Azure Private Link.
Complete 8-step workflow for integrating a new module into the Azure CAF root framework. Use this skill after creating a module to wire it into the root aggregator, set up combined_objects, create examples, and integrate with CI/CD.
Validate Azure Terraform resource schemas using MCP Terraform tools. Use this skill BEFORE implementing or modifying any Azure resource to ensure all attributes are correctly implemented according to the official provider documentation.
Complete workflow for creating a new Azure CAF Terraform module from scratch. Use this skill when the user explicitly asks to create a new module for an Azure resource type.
| name | mock-testing |
| description | Execute and debug Terraform mock tests for Azure CAF modules. Use this skill when user asks to test a module, run tests, or debug test failures. |
Mock tests validate that your Terraform module can successfully generate a plan without requiring actual Azure resources.
When test failures indicate possible schema drift (missing/renamed attributes), confirm provider schema with:
mcp_terraform_get_provider_detailsWhen comparing expected module interfaces, use:
mcp_terraform_search_modulesmcp_terraform_get_module_detailsMock tests ensure:
Mock tests use the same configuration files as deployment examples:
examples/
├── <category>/
│ └── <service>/
│ └── 100-simple-service/
│ ├── configuration.tfvars # Used for both deployment and mock tests
│ └── README.md
└── tests/
└── mock/
└── e2e_plan.tftest.hcl # Test framework (validates syntax)
Examples (examples/<category>/<service>/):
resource_group = { key = "rg1" }Mock tests validate syntax without creating resources: The test framework uses mock providers to validate that Terraform can generate a valid plan without actually connecting to Azure.
Same configuration for both: Since mock tests only validate syntax and planning, the same key-based references work perfectly for both deployment and testing.
MANDATORY scenarios:
Mock tests validate syntax only - they don't deploy to Azure.
For real deployments, use terraform plan/apply in the examples directory:
# ✅ CORRECT - For real deployment
cd examples
terraform_with_var_files \
--dir /chaos_studio/100-simple-chaos-target/ \
--action plan
For mock tests, use terraform test command:
# ✅ CORRECT - For syntax validation (no Azure connection)
terraform -chdir=examples \
test \
-test-directory=./tests/mock \
-var-file=./chaos_studio/100-simple-chaos-target/configuration.tfvars \
-verbose
Mock examples contain fake Azure resource IDs that don't exist in your subscription.
Standard Mock Test Command:
cd examples
terraform test \
-test-directory=./tests/mock \
-var-file=./<category>/<service>/100-example/configuration.tfvars \
-verbose
Example for chaos_studio module:
cd examples
terraform test \
-test-directory=./tests/mock \
-var-file=./chaos_studio/100-simple-chaos-target/configuration.tfvars \
-verbose
Alternative (single command): Use terraform -chdir=examples test ... if you don't want to change directories.
Mock tests do NOT require Azure subscription (they use mock providers).
For REAL deployments (terraform plan/apply), ALWAYS verify subscription first:
# 1. Check current Azure subscription
az account show --query "{subscriptionId:id, name:name, state:state}" -o table
# 2. Confirm with user this is correct
# MUST get explicit confirmation before plan/apply
# 3. Export for Terraform
export ARM_SUBSCRIPTION_ID=$(az account show --query id -o tsv)
# Navigate to examples directory
cd /path/to/terraform-azurerm-caf/examples
# Initialize Terraform (required first time or after module changes)
terraform init -upgrade
# Run mock test for a specific example (NO subscription needed)
terraform test \
-test-directory=./tests/mock \
-var-file=./<category>/<service>/<example_number>/configuration.tfvars \
-verbose
# Test chaos_studio module
cd examples
terraform init -upgrade
terraform test \
-test-directory=./tests/mock \
-var-file=./tests/chaos_studio/100-simple-chaos-target-mock/configuration.tfvars \
-verbose
# Note: Use mock examples from tests/ directory, not deployment examples
# Mock examples have direct IDs, deployment examples have key-based references
# If testing deployment example (for actual deployment planning):
terraform test \
-test-directory=./tests/mock \
-var-file=./chaos_studio/100-simple-chaos-target/configuration.tfvars \
-verbose
# Note: Deployment examples may fail in mock tests if they depend on
# remote_objects that aren't populated during mock testing
tests/mock/e2e_plan.tftest.hcl... in progress
run "test_plan"... pass
tests/mock/e2e_plan.tftest.hcl... tearing down
tests/mock/e2e_plan.tftest.hcl... pass
Success! 1 passed, 0 failed.
tests/mock/e2e_plan.tftest.hcl... in progress
run "test_plan"... fail
╷
│ Error: Reference to undeclared input variable
│
│ on ../modules/category/service/variables.tf line 10:
│ 10: name = var.wrong_variable_name
│
│ An input variable with the name "wrong_variable_name" has not been declared.
╵
Error message:
Error: Reference to undeclared input variable
An input variable with the name "managed_redis" has not been declared.
Cause: Using resource-specific variable names instead of generic var.settings
Solution: Update all variable references to use var.settings
Example fix:
# ❌ WRONG
name = var.managed_redis.name
# ✅ CORRECT
name = var.settings.name
Files to check:
modules/<category>/<module>/<module>.tf - Main resource filemodules/<category>/<module>/locals.tf - Local variable definitionsmodules/<category>/<module>/diagnostics.tf - Diagnostics configurationError message:
Error: Reference to undeclared local value
A local value with the name "tags" has not been declared.
Cause: Missing or incorrect local variable definitions
Solution: Ensure standard locals pattern is implemented in locals.tf:
locals {
module_tag = {
"category/module_name" = basename(abspath(path.module))
}
tags = merge(var.base_tags, local.module_tag, try(var.settings.tags, null))
location = coalesce(
try(var.settings.location, null),
try(var.location, null),
try(var.resource_group.location, null),
try(var.resource_groups[try(var.settings.resource_group.lz_key, var.client_config.landingzone_key)][try(var.settings.resource_group.key, var.settings.resource_group_key)].location, null)
)
resource_group_name = coalesce(
try(var.settings.resource_group_name, null),
try(var.resource_group.name, null),
try(var.resource_groups[try(var.settings.resource_group.lz_key, var.client_config.landingzone_key)][try(var.settings.resource_group.key, var.settings.resource_group_key)].name, null)
)
}
Error message:
Error: Invalid reference
A reference to a resource type must be followed by at least one attribute access
Cause: Using reserved keywords (e.g., module) as iterator names in dynamic blocks
Solution: Rename iterator to avoid reserved keywords
Example fix:
# ❌ WRONG - 'module' is a reserved keyword
dynamic "origins" {
for_each = try(var.settings.origins, {})
iterator = module
content {
name = module.value.name
}
}
# ✅ CORRECT - Use descriptive name
dynamic "origins" {
for_each = try(var.settings.origins, {})
iterator = origin
content {
name = origin.value.name
}
}
Error message:
Error: Module not installed
This module is not yet installed. Run "terraform init" to install all modules required by this configuration.
Cause: Terraform not initialized or module changes not recognized
Solution:
cd examples
terraform init -upgrade
When to run:
Error message:
Error: Invalid for_each argument
The given "for_each" argument value is unsuitable
Cause: for_each receiving null or non-map/set value
Solution: Ensure proper default values with try()
Example fix:
# ❌ WRONG - Can receive null
for_each = var.settings.optional_blocks
# ✅ CORRECT - Defaults to empty map
for_each = try(var.settings.optional_blocks, {})
Error message:
Error: Cycle: module.resource1, module.resource2
Cause: Circular dependency between modules
Solution:
depends_on if neededExample fix:
# Add explicit dependency order
resource "azurerm_resource_a" "example" {
# ...
depends_on = [azurerm_resource_b.example]
}
Read the full error message
Check the module file
# Open the file mentioned in error
code modules/<category>/<module>/<file>.tf
Verify variable names
var.settings.*var.managed_redis.* or other resource-specific namesCheck locals.tf exists and is complete
Validate example configuration
# Open the example tfvars
code examples/<category>/<module>/100-*/configuration.tfvars
Check variable definitions
# Verify variable is defined in module
code modules/<category>/<module>/variables.tf
Re-run test after fix
cd examples
terraform test -test-directory=./tests/mock -var-file=./<path>/configuration.tfvars -verbose
┌─────────────────────────────────────┐
│ 1. Make Changes to Module │
└─────────────────┬───────────────────┘
│
▼
┌─────────────────────────────────────┐
│ 2. Navigate to examples/ │
│ cd examples │
└─────────────────┬───────────────────┘
│
▼
┌─────────────────────────────────────┐
│ 3. Initialize Terraform │
│ terraform init -upgrade │
└─────────────────┬───────────────────┘
│
▼
┌─────────────────────────────────────┐
│ 4. Run Mock Test │
│ terraform test ... │
└─────────────────┬───────────────────┘
│
┌───────┴───────┐
│ │
▼ ▼
┌─────────┐ ┌─────────┐
│ PASS │ │ FAIL │
└────┬────┘ └────┬────┘
│ │
│ ▼
│ ┌──────────────────┐
│ │ 5. Debug Error │
│ │ - Read message │
│ │ - Check file │
│ │ - Fix issue │
│ └────┬─────────────┘
│ │
│ ▼
│ ┌──────────────────┐
│ │ 6. Re-test │
│ └────┬─────────────┘
│ │
└──────────┴──────────────┐
│
▼
┌─────────────────┐
│ 7. Commit │
└─────────────────┘
If a module has multiple examples, test each one:
cd examples
# Test all examples for a module
for config in ./<category>/<module>/*/configuration.tfvars; do
echo "Testing: $config"
terraform test -test-directory=./tests/mock -var-file="$config" -verbose
if [ $? -ne 0 ]; then
echo "FAILED: $config"
break
fi
done
Mock tests are also run in GitHub Actions workflows. Ensure your example is added to the appropriate workflow file:
.github/workflows/standalone-scenarios.json.github/workflows/standalone-scenarios-additional.json.github/workflows/standalone-compute.json.github/workflows/standalone-networking.json.github/workflows/standalone-dataplat.json# Initialize
cd examples && terraform init -upgrade
# Test single example
terraform test -test-directory=./tests/mock -var-file=./path/to/configuration.tfvars -verbose
# Test with detailed output
TF_LOG=DEBUG terraform test -test-directory=./tests/mock -var-file=./path/to/configuration.tfvars -verbose
# Clean state between tests
rm -rf .terraform/ .terraform.lock.hcl
terraform init -upgrade
Before marking testing complete:
examples/ directoryterraform init -upgradeDO:
var.settings)DON'T: