terraform-vsphere-resourceg.../tests/custom_configuration.tftest.hcl
Patrick de Ruiter cfbe6cbdc4
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
Add comprehensive Terraform testing framework
- 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.
2025-11-09 00:37:45 +01:00

224 lines
6.1 KiB
HCL

# Test suite for custom resource group configurations
# Tests override functionality and custom values
# Test 1: Custom resource group with specific CPU/Memory settings
run "custom_resource_group_config" {
command = plan
variables {
resource_groups = {
high_priority = {
name = "High Priority"
cpu_reservation = 2000
cpu_limit = 4000
cpu_shares = "high"
memory_reservation = 4096
memory_limit = 8192
memory_shares = "high"
}
}
}
# Verify custom CPU configuration
assert {
condition = vsphere_resource_pool.resource_groups["high_priority"].cpu_reservation == 2000
error_message = "CPU reservation should be 2000 MHz"
}
assert {
condition = vsphere_resource_pool.resource_groups["high_priority"].cpu_limit == 4000
error_message = "CPU limit should be 4000 MHz"
}
assert {
condition = vsphere_resource_pool.resource_groups["high_priority"].cpu_shares == 2000
error_message = "CPU shares should be 2000 (high priority)"
}
# Verify custom memory configuration
assert {
condition = vsphere_resource_pool.resource_groups["high_priority"].memory_reservation == 4096
error_message = "Memory reservation should be 4096 MB"
}
assert {
condition = vsphere_resource_pool.resource_groups["high_priority"].memory_limit == 8192
error_message = "Memory limit should be 8192 MB"
}
assert {
condition = vsphere_resource_pool.resource_groups["high_priority"].memory_shares == 2000
error_message = "Memory shares should be 2000 (high priority)"
}
}
# Test 2: Low priority resource group
run "low_priority_resource_group" {
command = plan
variables {
resource_groups = {
low_priority = {
name = "Low Priority"
cpu_shares = "low"
memory_shares = "low"
}
}
}
# Verify low priority shares
assert {
condition = vsphere_resource_pool.resource_groups["low_priority"].cpu_shares == 500
error_message = "CPU shares should be 500 (low priority)"
}
assert {
condition = vsphere_resource_pool.resource_groups["low_priority"].memory_shares == 500
error_message = "Memory shares should be 500 (low priority)"
}
}
# Test 3: Non-expandable resource group (fixed resources)
run "non_expandable_resource_group" {
command = plan
variables {
resource_groups = {
fixed_resources = {
name = "Fixed Resources"
cpu_reservation = 1000
cpu_expandable = false
cpu_limit = 1000
memory_reservation = 2048
memory_expandable = false
memory_limit = 2048
}
}
}
# Verify non-expandable configuration
assert {
condition = vsphere_resource_pool.resource_groups["fixed_resources"].cpu_expandable == false
error_message = "CPU should not be expandable"
}
assert {
condition = vsphere_resource_pool.resource_groups["fixed_resources"].memory_expandable == false
error_message = "Memory should not be expandable"
}
# Verify reservation matches limit (fixed allocation)
assert {
condition = (
vsphere_resource_pool.resource_groups["fixed_resources"].cpu_reservation ==
vsphere_resource_pool.resource_groups["fixed_resources"].cpu_limit
)
error_message = "For fixed resources, CPU reservation should equal CPU limit"
}
assert {
condition = (
vsphere_resource_pool.resource_groups["fixed_resources"].memory_reservation ==
vsphere_resource_pool.resource_groups["fixed_resources"].memory_limit
)
error_message = "For fixed resources, memory reservation should equal memory limit"
}
}
# Test 4: Multiple custom resource groups
run "multiple_custom_resource_groups" {
command = plan
variables {
resource_groups = {
web_tier = {
name = "Web Tier"
cpu_shares = "high"
}
app_tier = {
name = "Application Tier"
cpu_shares = "normal"
}
db_tier = {
name = "Database Tier"
cpu_shares = "high"
}
}
}
# Verify correct number of resource groups
assert {
condition = length(vsphere_resource_pool.resource_groups) == 3
error_message = "Should create exactly 3 resource pools"
}
# Verify each resource group is created
assert {
condition = alltrue([
contains(keys(vsphere_resource_pool.resource_groups), "web_tier"),
contains(keys(vsphere_resource_pool.resource_groups), "app_tier"),
contains(keys(vsphere_resource_pool.resource_groups), "db_tier")
])
error_message = "All three custom resource groups should be created"
}
# Verify correct share levels
assert {
condition = (
vsphere_resource_pool.resource_groups["web_tier"].cpu_shares == 2000 &&
vsphere_resource_pool.resource_groups["app_tier"].cpu_shares == 1000 &&
vsphere_resource_pool.resource_groups["db_tier"].cpu_shares == 2000
)
error_message = "Share levels should be correctly mapped (high=2000, normal=1000)"
}
}
# Test 5: Environment-specific configuration
run "environment_specific_config" {
command = plan
variables {
environment = "dev"
resource_groups = {
development = {
name = "Development Resources"
}
}
}
# Verify environment tag
assert {
condition = vsphere_tag.environment.name == "dev"
error_message = "Environment tag should be 'dev'"
}
# Verify environment tag description
assert {
condition = vsphere_tag.environment.description == "Environment tag for dev"
error_message = "Environment tag description should reference 'dev'"
}
}
# Test 6: Edge case - single resource group
run "single_resource_group" {
command = plan
variables {
resource_groups = {
production = {
name = "Production Only"
}
}
}
assert {
condition = length(vsphere_resource_pool.resource_groups) == 1
error_message = "Should create exactly 1 resource pool"
}
assert {
condition = length(vsphere_tag.resource_group) == 1
error_message = "Should create exactly 1 resource group tag"
}
}