Some checks failed
Code Quality & Security Scan / TFLint (push) Successful in 24s
Code Quality & Security Scan / Terraform Destroy (push) Has been skipped
Code Quality & Security Scan / Tfsec Security Scan (push) Successful in 29s
Code Quality & Security Scan / Checkov Security Scan (push) Successful in 44s
Code Quality & Security Scan / Terraform Tests (push) Failing after 35s
Code Quality & Security Scan / SonarQube Trigger (push) Has been skipped
Code Quality & Security Scan / Terraform Init (push) Has been skipped
Code Quality & Security Scan / Terraform Apply (push) Has been skipped
- Implemented 21 test cases across 3 test suites: * resource_groups.tftest.hcl (7 tests): Default behavior and validation * custom_configuration.tftest.hcl (6 tests): Custom configurations * variable_validation.tftest.hcl (8 tests): Input validation and edge cases - Updated CI/CD pipeline (.gitea/workflows/sonarqube.yaml): * Added terraform-test job with format check and test execution * Generates and uploads test reports (30-day retention) * Runs after security scanning, before deployment - Added comprehensive documentation: * TESTING.md: Complete testing guide with best practices * TEST_SUMMARY.md: Implementation summary and statistics * TESTING_QUICK_START.md: Quick reference for developers * TESTING_WORKFLOW.md: Visual workflow diagrams - Updated existing documentation: * README.md: Added testing section with examples * CLAUDE.md: Added test commands to workflow - Test coverage includes: * Resource creation and configuration validation * Tag category and tag management * Variable validation and defaults * Custom configurations and overrides * Edge cases and error handling * Output generation verification Tests use mock credentials for infrastructure-independent execution. Requires Terraform >= 1.6.0 for native testing framework.
2.9 KiB
2.9 KiB
Terraform Testing Quick Start
Quick Commands
# Run all tests
terraform test
# Run tests with verbose output
terraform test -verbose
# Check formatting
terraform fmt -check -recursive
# Auto-format all files
terraform fmt -recursive
# Validate configuration
terraform validate
Pre-Commit Checklist
Before committing changes, ensure:
- Code is formatted:
terraform fmt -recursive - Configuration is valid:
terraform validate - All tests pass:
terraform test - No sensitive data is hardcoded
Test File Locations
tests/resource_groups.tftest.hcl- Default resource group teststests/custom_configuration.tftest.hcl- Custom configuration teststests/variable_validation.tftest.hcl- Variable validation tests
Common Test Scenarios
Adding a New Test
- Choose the appropriate test file based on what you're testing
- Add a new
runblock with a descriptive name - Use
command = planfor most tests - Add assertions with clear error messages
- Run the specific test to verify it works
Example:
run "my_new_test" {
command = plan
variables {
resource_groups = {
test = { name = "Test" }
}
}
assert {
condition = <your_condition>
error_message = "Clear description of what failed"
}
}
Testing a Specific Feature
# Run specific test file
terraform test -filter=tests/custom_configuration.tftest.hcl
# Run with verbose output for debugging
terraform test -verbose -filter=tests/variable_validation.tftest.hcl
CI/CD Pipeline
Tests run automatically on:
- Every push to master
- Every pull request
Pipeline order:
- TFLint
- Tfsec
- Checkov
- Terraform Test ⬅ Your tests
- SonarQube
- Terraform Init/Plan/Apply
Troubleshooting
Test Fails Locally But Not in CI
- Check Terraform version:
terraform version(need >= 1.6.0) - Ensure environment variables are not interfering
- Check for local
terraform.tfvarsoverriding test values
Test Fails in CI But Not Locally
- Review CI job output for environment-specific issues
- Verify mock credentials are properly set in CI
- Check for provider version mismatches
Format Check Fails
# Fix automatically
terraform fmt -recursive
# Verify
terraform fmt -check -recursive
Best Practices
- Write tests for new features: Every new feature should have corresponding tests
- Test edge cases: Include tests for empty values, minimum/maximum values, etc.
- Use descriptive names: Test names should clearly indicate what they test
- Clear error messages: Help future developers understand failures quickly
- Keep tests focused: One test should verify one specific behavior
Need Help?
- Full documentation: TESTING.md
- Terraform test docs: https://developer.hashicorp.com/terraform/language/tests
- Module README: README.md