# Terraform Testing Workflow ## Complete CI/CD Pipeline with Testing ``` ┌─────────────────────────────────────────────────────────────────┐ │ Code Push / Pull Request │ └────────────────────────────┬────────────────────────────────────┘ │ ▼ ┌────────────────┐ │ TFLint │ │ (Code Style) │ └────────┬───────┘ │ ▼ ┌────────────────┐ │ Tfsec │ │ (Security) │ └────────┬───────┘ │ ▼ ┌────────────────┐ │ Checkov │ │ (Compliance) │ └────────┬───────┘ │ ▼ ┌────────────────────────────────────────┐ │ Terraform Test (NEW!) │ │ ┌──────────────────────────────────┐ │ │ │ 1. Format Check │ │ │ │ terraform fmt -check │ │ │ └──────────────────────────────────┘ │ │ ┌──────────────────────────────────┐ │ │ │ 2. Run All Test Suites │ │ │ │ - resource_groups.tftest │ │ │ │ - custom_configuration │ │ │ │ - variable_validation │ │ │ │ (21 test cases total) │ │ │ └──────────────────────────────────┘ │ │ ┌──────────────────────────────────┐ │ │ │ 3. Generate Test Report │ │ │ └──────────────────────────────────┘ │ │ ┌──────────────────────────────────┐ │ │ │ 4. Upload Artifacts │ │ │ │ (30-day retention) │ │ │ └──────────────────────────────────┘ │ └────────────────┬───────────────────────┘ │ ▼ ┌────────────────┐ │ SonarQube │ │ (Code Quality) │ └────────┬───────┘ │ ▼ ┌────────────────┐ │ Terraform Init │ └────────┬───────┘ │ ▼ ┌────────────────┐ │ Terraform Plan │ └────────┬───────┘ │ ▼ ┌───────────────────────────────┐ │ Master Branch Only │ │ ┌────────────────────┐ │ │ │ Terraform Apply │ │ │ │ (Production) │ │ │ └────────────────────┘ │ └───────────────────────────────┘ ``` ## Test Execution Flow ``` ┌──────────────────────────────────────────────────────────────────┐ │ Terraform Test Stage │ └──────────────────────────────────────────────────────────────────┘ Step 1: Format Check ┌─────────────────────────────────────┐ │ terraform fmt -check -recursive │ │ │ │ ✓ Validates code formatting │ │ ✗ Fails if files need formatting │ └─────────────────────────────────────┘ │ ▼ Step 2: Execute Tests ┌─────────────────────────────────────────────────────────┐ │ terraform test -verbose │ │ │ │ Test Suite 1: resource_groups.tftest.hcl (7 tests) │ │ ├─ verify_default_resource_groups │ │ ├─ validate_shares_mapping │ │ ├─ verify_tag_categories │ │ ├─ verify_resource_group_tags │ │ ├─ verify_default_resource_pool_config │ │ ├─ verify_outputs │ │ └─ verify_resource_pool_names │ │ │ │ Test Suite 2: custom_configuration.tftest.hcl (6 tests)│ │ ├─ custom_resource_group_config │ │ ├─ low_priority_resource_group │ │ ├─ non_expandable_resource_group │ │ ├─ multiple_custom_resource_groups │ │ ├─ environment_specific_config │ │ └─ single_resource_group │ │ │ │ Test Suite 3: variable_validation.tftest.hcl (8 tests) │ │ ├─ valid_environment_values │ │ ├─ datacenter_variable │ │ ├─ cluster_name_variable │ │ ├─ resource_groups_structure │ │ ├─ optional_parameters_defaults │ │ ├─ shares_value_mapping │ │ ├─ empty_resource_groups │ │ └─ resource_limits_validation │ └─────────────────────────────────────────────────────────┘ │ ▼ Step 3: Generate Report ┌─────────────────────────────────────┐ │ Create test-report.md │ │ - Execution timestamp │ │ - Test results summary │ └─────────────────────────────────────┘ │ ▼ Step 4: Upload Artifacts ┌─────────────────────────────────────┐ │ Upload test-report.md │ │ Retention: 30 days │ └─────────────────────────────────────┘ ``` ## Local Development Workflow ``` ┌──────────────────────┐ │ Make Code Changes │ └──────────┬───────────┘ │ ▼ ┌──────────────────────┐ │ terraform fmt │ │ (Auto-format) │ └──────────┬───────────┘ │ ▼ ┌──────────────────────┐ │ terraform validate │ │ (Syntax check) │ └──────────┬───────────┘ │ ▼ ┌──────────────────────┐ │ terraform test │ │ (Run all tests) │ └──────────┬───────────┘ │ ▼ ┌─────────┐ │ Success?│ └────┬────┘ │ ┌─────┴─────┐ │ │ Yes No │ │ │ ▼ │ ┌──────────────┐ │ │ Fix Issues │ │ └──────┬───────┘ │ │ │ └──────┐ │ │ ▼ ▼ ┌────────────────────────┐ │ git commit & push │ └────────────────────────┘ ``` ## Test Failure Handling ``` ┌──────────────────────────────────────┐ │ Test Failure Detected │ └──────────────┬───────────────────────┘ │ ▼ ┌──────────────┐ │ Review Error │ │ Message │ └──────┬───────┘ │ ▼ ┌─────────────────────┐ │ What type of error? │ └─────────┬───────────┘ │ ┌─────────┴─────────────┬──────────────┐ │ │ │ ▼ ▼ ▼ ┌──────────┐ ┌─────────────┐ ┌────────────┐ │Formatting│ │Test Logic │ │Code Bug │ │ Error │ │ Error │ │ │ └────┬─────┘ └──────┬──────┘ └─────┬──────┘ │ │ │ ▼ ▼ ▼ ┌──────────┐ ┌─────────────┐ ┌────────────┐ │terraform │ │Fix Test │ │Fix Code │ │ fmt │ │Assertions │ │Logic │ └────┬─────┘ └──────┬──────┘ └─────┬──────┘ │ │ │ └─────────────────────┴───────────────┘ │ ▼ ┌─────────────────┐ │ Re-run Tests │ └─────────────────┘ ``` ## Quality Gates ``` Quality Gates │ ┌────────────────┼────────────────┐ │ │ │ ▼ ▼ ▼ ┌─────────┐ ┌──────────┐ ┌──────────┐ │ TFLint │ │ Tfsec │ │ Checkov │ │ ✓ │ │ ✓ │ │ ✓ │ └─────────┘ └──────────┘ └──────────┘ │ ▼ ┌────────────────┐ │ Terraform Test │ ◄─── NEW! │ ✓ │ └────────┬───────┘ │ ▼ ┌────────────┐ │ SonarQube │ │ ✓ │ └──────┬─────┘ │ ▼ ┌────────────┐ │ Deploy │ │ Ready │ └────────────┘ ``` ## Test Types Coverage ``` ┌─────────────────────────────────────────────────────────────┐ │ Test Coverage Matrix │ ├─────────────────────────────────────────────────────────────┤ │ │ │ Unit Tests (Terraform Test) │ │ ├─ Variable validation ✓ (8 tests) │ │ ├─ Resource creation ✓ (7 tests) │ │ ├─ Configuration logic ✓ (6 tests) │ │ └─ Output generation ✓ (4 assertions) │ │ │ │ Integration Tests │ │ ├─ Tag category creation ✓ │ │ ├─ Tag application ✓ │ │ └─ Resource dependencies ✓ │ │ │ │ Security Tests │ │ ├─ Tfsec security scan ✓ │ │ └─ Checkov compliance ✓ │ │ │ │ Code Quality Tests │ │ ├─ TFLint style check ✓ │ │ ├─ Format validation ✓ │ │ └─ SonarQube analysis ✓ │ │ │ └─────────────────────────────────────────────────────────────┘ ``` ## Quick Reference Commands ### Local Testing ```bash # Complete test workflow terraform fmt -recursive && \ terraform validate && \ terraform test -verbose # Individual steps terraform fmt -check # Check formatting terraform validate # Validate syntax terraform test # Run all tests terraform test -verbose # Verbose output ``` ### CI/CD Monitoring ```bash # Check workflow status git push origin master # Triggers full pipeline # View test results # Check Actions tab in repository # Download test-report.md artifact ``` ## Success Indicators ✅ All 21 tests passing ✅ No formatting issues ✅ No linting warnings ✅ No security vulnerabilities ✅ Code quality metrics met ✅ Deployment successful ## Resources - **Full Documentation**: [TESTING.md](../TESTING.md) - **Quick Start**: [TESTING_QUICK_START.md](TESTING_QUICK_START.md) - **Summary**: [TEST_SUMMARY.md](../TEST_SUMMARY.md) - **Main README**: [README.md](../README.md)