terraform-vsphere-resourceg.../tests/resource_groups.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

173 lines
5.2 KiB
HCL

# Test suite for vSphere Resource Groups module
# Tests resource pool creation, tagging, and configuration validation
# Test 1: Verify default resource groups are created correctly
run "verify_default_resource_groups" {
command = plan
# Verify that all default resource groups are present
assert {
condition = length(var.resource_groups) == 5
error_message = "Expected 5 default resource groups (kubernetes, docker, infra, databases, app-servers)"
}
# Verify resource pools are created for each resource group
assert {
condition = length(vsphere_resource_pool.resource_groups) == 5
error_message = "Should create 5 resource pools"
}
}
# Test 2: Validate shares mapping logic
run "validate_shares_mapping" {
command = plan
# Verify shares mapping is correctly defined
assert {
condition = alltrue([
local.shares_mapping["low"] == 500,
local.shares_mapping["normal"] == 1000,
local.shares_mapping["high"] == 2000
])
error_message = "Shares mapping values are incorrect"
}
}
# Test 3: Verify tag categories are created
run "verify_tag_categories" {
command = plan
# Environment tag category
assert {
condition = vsphere_tag_category.environment.name == "Environment"
error_message = "Environment tag category name should be 'Environment'"
}
assert {
condition = vsphere_tag_category.environment.cardinality == "SINGLE"
error_message = "Environment tag category should have SINGLE cardinality"
}
# Resource group type tag category
assert {
condition = vsphere_tag_category.resource_group_type.name == "ResourceGroupType"
error_message = "Resource group type tag category name should be 'ResourceGroupType'"
}
assert {
condition = vsphere_tag_category.resource_group_type.cardinality == "SINGLE"
error_message = "Resource group type tag category should have SINGLE cardinality"
}
}
# Test 4: Verify tags are created for each resource group
run "verify_resource_group_tags" {
command = plan
assert {
condition = length(vsphere_tag.resource_group) == 5
error_message = "Should create 5 resource group tags"
}
# Verify environment tag is created
assert {
condition = vsphere_tag.environment.name == var.environment
error_message = "Environment tag name should match environment variable"
}
}
# Test 5: Verify resource pool default configurations
run "verify_default_resource_pool_config" {
command = plan
# Check kubernetes resource group defaults
assert {
condition = vsphere_resource_pool.resource_groups["kubernetes"].cpu_reservation == 0
error_message = "Default CPU reservation should be 0"
}
assert {
condition = vsphere_resource_pool.resource_groups["kubernetes"].cpu_expandable == true
error_message = "CPU should be expandable by default"
}
assert {
condition = vsphere_resource_pool.resource_groups["kubernetes"].cpu_limit == -1
error_message = "Default CPU limit should be -1 (unlimited)"
}
assert {
condition = vsphere_resource_pool.resource_groups["kubernetes"].memory_reservation == 0
error_message = "Default memory reservation should be 0"
}
assert {
condition = vsphere_resource_pool.resource_groups["kubernetes"].memory_expandable == true
error_message = "Memory should be expandable by default"
}
assert {
condition = vsphere_resource_pool.resource_groups["kubernetes"].memory_limit == -1
error_message = "Default memory limit should be -1 (unlimited)"
}
}
# Test 6: Verify outputs are generated correctly
run "verify_outputs" {
command = plan
# Resource pool IDs output
assert {
condition = length(keys(output.resource_pool_ids)) == 5
error_message = "Should output 5 resource pool IDs"
}
# Resource pool names output
assert {
condition = length(keys(output.resource_pool_names)) == 5
error_message = "Should output 5 resource pool names"
}
# Environment tag ID output
assert {
condition = output.environment_tag_id != null
error_message = "Environment tag ID should not be null"
}
# Resource group tag IDs output
assert {
condition = length(keys(output.resource_group_tag_ids)) == 5
error_message = "Should output 5 resource group tag IDs"
}
}
# Test 7: Verify resource pool naming
run "verify_resource_pool_names" {
command = plan
assert {
condition = vsphere_resource_pool.resource_groups["kubernetes"].name == "Kubernetes"
error_message = "Kubernetes resource pool should be named 'Kubernetes'"
}
assert {
condition = vsphere_resource_pool.resource_groups["docker"].name == "Docker"
error_message = "Docker resource pool should be named 'Docker'"
}
assert {
condition = vsphere_resource_pool.resource_groups["infra"].name == "Infra"
error_message = "Infra resource pool should be named 'Infra'"
}
assert {
condition = vsphere_resource_pool.resource_groups["databases"].name == "Databases"
error_message = "Databases resource pool should be named 'Databases'"
}
assert {
condition = vsphere_resource_pool.resource_groups["app-servers"].name == "Application Servers"
error_message = "App Servers resource pool should be named 'Application Servers'"
}
}