- 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.
7.0 KiB
Terraform Testing Implementation Summary
Overview
This document summarizes the Terraform testing implementation for the vSphere Resource Groups module.
Implementation Details
Test Statistics
- Total Test Files: 3
- Total Test Cases: 21
resource_groups.tftest.hcl: 7 testscustom_configuration.tftest.hcl: 6 testsvariable_validation.tftest.hcl: 8 tests
Test Coverage
1. Default Behavior Tests (resource_groups.tftest.hcl)
| Test Case | Purpose |
|---|---|
| verify_default_resource_groups | Validates all 5 default resource groups are created |
| validate_shares_mapping | Ensures shares mapping logic (low=500, normal=1000, high=2000) |
| verify_tag_categories | Confirms Environment and ResourceGroupType tag categories |
| verify_resource_group_tags | Validates tags created for each resource group |
| verify_default_resource_pool_config | Checks default CPU/memory configurations |
| verify_outputs | Ensures all outputs are generated correctly |
| verify_resource_pool_names | Validates resource pool naming conventions |
2. Custom Configuration Tests (custom_configuration.tftest.hcl)
| Test Case | Purpose |
|---|---|
| custom_resource_group_config | Tests high-priority custom configurations |
| low_priority_resource_group | Validates low-priority share allocation |
| non_expandable_resource_group | Tests fixed resource allocation |
| multiple_custom_resource_groups | Validates multiple resource groups with different priorities |
| environment_specific_config | Tests environment variable integration |
| single_resource_group | Edge case: single resource group scenario |
3. Variable Validation Tests (variable_validation.tftest.hcl)
| Test Case | Purpose |
|---|---|
| valid_environment_values | Validates accepted environment values |
| datacenter_variable | Tests datacenter variable acceptance |
| cluster_name_variable | Tests cluster name variable |
| resource_groups_structure | Validates resource_groups map structure |
| optional_parameters_defaults | Verifies default values are applied |
| shares_value_mapping | Tests all three share levels |
| empty_resource_groups | Handles empty resource_groups map |
| resource_limits_validation | Validates CPU/memory limits configuration |
CI/CD Integration
Pipeline Workflow
TFLint → Tfsec → Checkov → Terraform Test → SonarQube → Terraform Init → Terraform Plan → Terraform Apply
↑
New Test Step
Test Job Details
The terraform-test job in .gitea/workflows/sonarqube.yaml:
- Checkout Code: Fetches repository with full history
- Setup Terraform: Installs latest Terraform version
- Format Check: Validates code formatting (
terraform fmt -check -recursive) - Run Tests: Executes all tests with verbose output
- Generate Report: Creates test execution report
- Upload Artifacts: Stores test report for 30 days
Environment Variables
Tests use mock credentials to avoid requiring actual infrastructure:
TF_VAR_role_id: "test-role-id"TF_VAR_secret_id: "test-secret-id"
Files Created
Test Files
tests/resource_groups.tftest.hcl- Default resource group teststests/custom_configuration.tftest.hcl- Custom configuration teststests/variable_validation.tftest.hcl- Variable validation teststests/setup/main.tf- Mock provider setup
Documentation
TESTING.md- Comprehensive testing guideTEST_SUMMARY.md- This summary document.github/TESTING_QUICK_START.md- Quick reference guide
Configuration Updates
.gitea/workflows/sonarqube.yaml- Added terraform-test jobREADME.md- Added testing sectionCLAUDE.md- Updated with testing commands
Running Tests
Local Execution
# Run all tests
terraform test
# Run with verbose output
terraform test -verbose
# Run specific test file
terraform test -filter=tests/resource_groups.tftest.hcl
CI/CD Execution
Tests automatically run on:
- Push to master branch
- Pull request (opened, synchronized, reopened)
Test Quality Metrics
Assertions by Category
- Resource Creation: 8 assertions
- Configuration Validation: 25+ assertions
- Tag Management: 6 assertions
- Output Validation: 4 assertions
- Edge Cases: 4 assertions
- Variable Validation: 10+ assertions
Coverage Areas
✅ Resource pool creation and naming ✅ CPU/Memory reservation, limits, and shares ✅ Shares value mapping (low/normal/high) ✅ Tag category creation ✅ Tag application to resources ✅ Output generation ✅ Variable validation ✅ Default value application ✅ Custom configuration override ✅ Edge cases (empty maps, single items) ✅ Environment-specific configuration
Benefits
- Early Error Detection: Catch configuration errors before deployment
- Regression Prevention: Ensures changes don't break existing functionality
- Documentation: Tests serve as executable documentation
- Confidence: Validates module behavior across scenarios
- Quality Gates: Automated quality checks in CI/CD pipeline
Future Enhancements
Recommended Additions
- Mock Providers: Complete isolation from infrastructure
- Coverage Reporting: Metrics on test coverage percentage
- Performance Tests: Validate behavior with large numbers of resource groups
- Integration Tests: Tests against actual vSphere environment (staging)
- Contract Tests: Ensure output structure stability
- Property-Based Testing: Generate random valid configurations
Potential Test Scenarios
- Test with maximum number of resource groups (scalability)
- Validate behavior with special characters in names
- Test resource pool hierarchy and inheritance
- Validate concurrent resource group creation
- Test failure scenarios (invalid configurations)
- Validate resource pool updates (state migration)
Maintenance
When to Update Tests
- Adding new features or configuration options
- Changing default values
- Modifying resource creation logic
- Updating provider versions
- Fixing bugs (add regression tests)
Test Review Checklist
- Tests pass locally (
terraform test) - Tests pass in CI/CD pipeline
- Test names are descriptive
- Error messages are clear and actionable
- Edge cases are covered
- Documentation is updated
Success Criteria
✅ All 21 tests successfully implemented ✅ CI/CD pipeline updated and tested ✅ Comprehensive documentation created ✅ Test execution automated ✅ Code formatting validated ✅ Quick start guide provided
References
Implementation Date: 2025-11-09 Terraform Version Required: >= 1.6.0 Test Framework: Native Terraform Testing