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.
257 lines
6.8 KiB
HCL
257 lines
6.8 KiB
HCL
# Test suite for input variable validation
|
|
# Tests that variables are properly validated and constrained
|
|
|
|
# Test 1: Verify environment variable accepts valid values
|
|
run "valid_environment_values" {
|
|
command = plan
|
|
|
|
variables {
|
|
environment = "prd"
|
|
}
|
|
|
|
assert {
|
|
condition = var.environment == "prd"
|
|
error_message = "Environment should accept 'prd' as valid value"
|
|
}
|
|
}
|
|
|
|
# Test 2: Verify datacenter variable
|
|
run "datacenter_variable" {
|
|
command = plan
|
|
|
|
variables {
|
|
datacenter = "WBYC-DC01"
|
|
}
|
|
|
|
assert {
|
|
condition = var.datacenter == "WBYC-DC01"
|
|
error_message = "Datacenter variable should accept string values"
|
|
}
|
|
}
|
|
|
|
# Test 3: Verify cluster_name variable
|
|
run "cluster_name_variable" {
|
|
command = plan
|
|
|
|
variables {
|
|
cluster_name = "wbyc-cluster01"
|
|
}
|
|
|
|
assert {
|
|
condition = var.cluster_name == "wbyc-cluster01"
|
|
error_message = "Cluster name variable should accept string values"
|
|
}
|
|
}
|
|
|
|
# Test 4: Verify resource_groups map accepts proper structure
|
|
run "resource_groups_structure" {
|
|
command = plan
|
|
|
|
variables {
|
|
resource_groups = {
|
|
test_group = {
|
|
name = "Test Group"
|
|
cpu_reservation = 1000
|
|
cpu_expandable = true
|
|
cpu_limit = 2000
|
|
cpu_shares = "normal"
|
|
memory_reservation = 2048
|
|
memory_expandable = true
|
|
memory_limit = 4096
|
|
memory_shares = "high"
|
|
}
|
|
}
|
|
}
|
|
|
|
assert {
|
|
condition = var.resource_groups["test_group"].name == "Test Group"
|
|
error_message = "Resource groups should accept properly structured objects"
|
|
}
|
|
|
|
assert {
|
|
condition = var.resource_groups["test_group"].cpu_reservation == 1000
|
|
error_message = "CPU reservation should be a number"
|
|
}
|
|
|
|
assert {
|
|
condition = var.resource_groups["test_group"].cpu_expandable == true
|
|
error_message = "CPU expandable should be a boolean"
|
|
}
|
|
|
|
assert {
|
|
condition = contains(["low", "normal", "high"], var.resource_groups["test_group"].cpu_shares)
|
|
error_message = "CPU shares should be one of: low, normal, high"
|
|
}
|
|
}
|
|
|
|
# Test 5: Verify optional parameters with defaults
|
|
run "optional_parameters_defaults" {
|
|
command = plan
|
|
|
|
variables {
|
|
resource_groups = {
|
|
minimal_config = {
|
|
name = "Minimal Config"
|
|
# All other parameters should use defaults
|
|
}
|
|
}
|
|
}
|
|
|
|
# Verify defaults are applied
|
|
assert {
|
|
condition = vsphere_resource_pool.resource_groups["minimal_config"].cpu_reservation == 0
|
|
error_message = "Default CPU reservation should be 0"
|
|
}
|
|
|
|
assert {
|
|
condition = vsphere_resource_pool.resource_groups["minimal_config"].cpu_expandable == true
|
|
error_message = "Default CPU expandable should be true"
|
|
}
|
|
|
|
assert {
|
|
condition = vsphere_resource_pool.resource_groups["minimal_config"].cpu_limit == -1
|
|
error_message = "Default CPU limit should be -1 (unlimited)"
|
|
}
|
|
|
|
assert {
|
|
condition = vsphere_resource_pool.resource_groups["minimal_config"].cpu_shares == 1000
|
|
error_message = "Default CPU shares should be 1000 (normal)"
|
|
}
|
|
|
|
assert {
|
|
condition = vsphere_resource_pool.resource_groups["minimal_config"].memory_reservation == 0
|
|
error_message = "Default memory reservation should be 0"
|
|
}
|
|
|
|
assert {
|
|
condition = vsphere_resource_pool.resource_groups["minimal_config"].memory_expandable == true
|
|
error_message = "Default memory expandable should be true"
|
|
}
|
|
|
|
assert {
|
|
condition = vsphere_resource_pool.resource_groups["minimal_config"].memory_limit == -1
|
|
error_message = "Default memory limit should be -1 (unlimited)"
|
|
}
|
|
|
|
assert {
|
|
condition = vsphere_resource_pool.resource_groups["minimal_config"].memory_shares == 1000
|
|
error_message = "Default memory shares should be 1000 (normal)"
|
|
}
|
|
}
|
|
|
|
# Test 6: Verify shares value mapping for all levels
|
|
run "shares_value_mapping" {
|
|
command = plan
|
|
|
|
variables {
|
|
resource_groups = {
|
|
low_shares = {
|
|
name = "Low Shares"
|
|
cpu_shares = "low"
|
|
memory_shares = "low"
|
|
}
|
|
normal_shares = {
|
|
name = "Normal Shares"
|
|
cpu_shares = "normal"
|
|
memory_shares = "normal"
|
|
}
|
|
high_shares = {
|
|
name = "High Shares"
|
|
cpu_shares = "high"
|
|
memory_shares = "high"
|
|
}
|
|
}
|
|
}
|
|
|
|
# Verify low shares mapping
|
|
assert {
|
|
condition = (
|
|
vsphere_resource_pool.resource_groups["low_shares"].cpu_shares == 500 &&
|
|
vsphere_resource_pool.resource_groups["low_shares"].memory_shares == 500
|
|
)
|
|
error_message = "Low shares should map to 500"
|
|
}
|
|
|
|
# Verify normal shares mapping
|
|
assert {
|
|
condition = (
|
|
vsphere_resource_pool.resource_groups["normal_shares"].cpu_shares == 1000 &&
|
|
vsphere_resource_pool.resource_groups["normal_shares"].memory_shares == 1000
|
|
)
|
|
error_message = "Normal shares should map to 1000"
|
|
}
|
|
|
|
# Verify high shares mapping
|
|
assert {
|
|
condition = (
|
|
vsphere_resource_pool.resource_groups["high_shares"].cpu_shares == 2000 &&
|
|
vsphere_resource_pool.resource_groups["high_shares"].memory_shares == 2000
|
|
)
|
|
error_message = "High shares should map to 2000"
|
|
}
|
|
}
|
|
|
|
# Test 7: Verify empty resource_groups map handling
|
|
run "empty_resource_groups" {
|
|
command = plan
|
|
|
|
variables {
|
|
resource_groups = {}
|
|
}
|
|
|
|
assert {
|
|
condition = length(vsphere_resource_pool.resource_groups) == 0
|
|
error_message = "Should handle empty resource_groups map"
|
|
}
|
|
|
|
assert {
|
|
condition = length(vsphere_tag.resource_group) == 0
|
|
error_message = "Should not create resource group tags when map is empty"
|
|
}
|
|
|
|
# Environment resources should still be created
|
|
assert {
|
|
condition = vsphere_tag_category.environment.name == "Environment"
|
|
error_message = "Environment tag category should still be created"
|
|
}
|
|
}
|
|
|
|
# Test 8: Verify resource limits are properly set
|
|
run "resource_limits_validation" {
|
|
command = plan
|
|
|
|
variables {
|
|
resource_groups = {
|
|
limited = {
|
|
name = "Limited Resources"
|
|
cpu_limit = 1000
|
|
memory_limit = 2048
|
|
}
|
|
unlimited = {
|
|
name = "Unlimited Resources"
|
|
# Using defaults for limits (-1)
|
|
}
|
|
}
|
|
}
|
|
|
|
assert {
|
|
condition = vsphere_resource_pool.resource_groups["limited"].cpu_limit == 1000
|
|
error_message = "CPU limit should be set to 1000"
|
|
}
|
|
|
|
assert {
|
|
condition = vsphere_resource_pool.resource_groups["limited"].memory_limit == 2048
|
|
error_message = "Memory limit should be set to 2048"
|
|
}
|
|
|
|
assert {
|
|
condition = vsphere_resource_pool.resource_groups["unlimited"].cpu_limit == -1
|
|
error_message = "Default CPU limit should be -1 (unlimited)"
|
|
}
|
|
|
|
assert {
|
|
condition = vsphere_resource_pool.resource_groups["unlimited"].memory_limit == -1
|
|
error_message = "Default memory limit should be -1 (unlimited)"
|
|
}
|
|
}
|