# saif-utilities / naming / tests

This directory contains comprehensive tests for the Terraform naming modules, organized by responsibility and functionality.

## Test Architecture

### Core String Processing Tests
- **`../string_utilities/casing/casing_module.tftest.hcl`** (10 tests)
  - Tests core string transformation logic
  - Covers Pascal, Camel, Kebab, and Snake case conversions
  - Handles edge cases like mixed inputs, dots, uppercase normalization
  - **All string casing logic should be tested here**

### Integration Tests
- **`base_module.tftest.hcl`** (10 tests)
  - Tests base naming module integration
  - Focuses on configuration handling, prefix/suffix logic
  - Resource-agnostic naming patterns
  
- **`servicebus_module.tftest.hcl`** (15 tests)
  - Tests ServiceBus-specific naming rules
  - Azure ServiceBus length/character constraints
  - Organization/Owner name integration
  - Unique name generation with ServiceBus context

- **`integration.tftest.hcl`** (6 tests)
  - End-to-end cross-module testing
  - Multi-tenant scenarios
  - Complex real-world configurations

## Test Organization Principles

1. **Single Responsibility**: Each test file focuses on one aspect
2. **No Redundancy**: Core logic tested once in casing module
3. **Clear Separation**: 
   - String transformations → `casing_module.tftest.hcl`
   - Base functionality → `base_module.tftest.hcl` 
   - ServiceBus specifics → `servicebus_module.tftest.hcl`
   - Integration scenarios → `integration.tftest.hcl`

## Running Tests

### Run All Tests
```powershell
# From the tests directory
terraform test

# Or from the parent directory
terraform test -test-directory=tests
```

### Run Specific Test Files
```powershell
# Run only base module tests
terraform test base_module.tftest.hcl

# Run only ServiceBus module tests
terraform test servicebus_module.tftest.hcl

# Run only integration tests
terraform test integration.tftest.hcl
```

### Run Specific Test Cases
```powershell
# Run a specific test case
terraform test -run=basic_naming

# Run tests matching a pattern
terraform test -run=".*environment.*"
```

### Verbose Output
```powershell
# Show detailed output
terraform test -verbose

# Show planning output
terraform test -verbose -json
```

## Test Coverage

### Base Module Tests (`base_module.tftest.hcl`)
- ✅ **basic_naming**: Tests basic single resource naming with VM slug
- ✅ **multiple_resources**: Tests multiple resources with storage account configuration
- ✅ **custom_prefix_suffix**: Tests custom prefix/suffix functionality
- ✅ **environment_tenant_variations**: Tests different environment and tenant combinations
- ✅ **empty_slug**: Tests edge case with empty slug
- ✅ **regex_compliance**: Tests strict regex pattern compliance

### ServiceBus Module Tests (`servicebus_module.tftest.hcl`)
- ✅ **default_servicebus_naming**: Tests default ServiceBus namespace naming
- ✅ **custom_prefix_suffix**: Tests custom prefix/suffix for ServiceBus
- ✅ **environment_variations**: Tests different environments
- ✅ **short_resource_names**: Tests edge cases with short input names
- ✅ **multi_tenant**: Tests multi-tenant scenarios
- ✅ **performance_uniqueness**: Tests performance with 10 resources and uniqueness
- ✅ **strict_regex_compliance**: Tests strict Azure ServiceBus naming requirements

### Integration Tests (`integration.tftest.hcl`)
- ✅ **mixed_environment_deployment**: Tests complex staging environment deployment
- ✅ **cross_tenant_consistency**: Tests naming consistency across different tenants
- ✅ **performance_at_scale**: Tests performance with 25 resources
- ✅ **multi_region_deployment**: Tests production multi-region scenario
- ✅ **minimal_configuration**: Tests minimal configuration edge case
- ✅ **name_collision_prevention**: Tests duplicate name handling
- ✅ **long_name_handling**: Tests truncation of long input values

## Test Validation Points

### Azure ServiceBus Namespace Requirements
- ✅ Length: 6-50 characters
- ✅ Pattern: `^[a-zA-Z][a-zA-Z0-9-]{4,48}[a-zA-Z0-9]$`
- ✅ Start: Must begin with a letter
- ✅ End: Must end with letter or number
- ✅ Characters: Letters, numbers, and hyphens only
- ✅ No consecutive hyphens
- ✅ No underscores (converted to hyphens)

### Base Module Requirements
- ✅ Configurable regex patterns
- ✅ Custom slug support
- ✅ Dash configuration (enabled/disabled)
- ✅ Prefix/suffix support
- ✅ Environment/tenant/project_id integration
- ✅ Unique name generation

## Expected Test Results

All tests should pass with the current implementation. If any tests fail, check:

1. **Module paths**: Ensure relative paths to modules are correct
2. **Variable requirements**: Verify all required variables are provided
3. **Regex patterns**: Check that generated names match expected patterns
4. **Length constraints**: Verify names meet Azure requirements
5. **Uniqueness**: Confirm duplicate inputs generate unique outputs

## Debugging Failed Tests

### View Test Plan
```powershell
# See what Terraform would create
terraform test -verbose

# Show JSON output for parsing
terraform test -verbose -json
```

### Test Individual Components
```powershell
# Initialize and validate modules
terraform -chdir="../base" init
terraform -chdir="../base" validate

terraform -chdir="../azure/servicebus/namespace" init
terraform -chdir="../azure/servicebus/namespace" validate
```

### Common Issues
1. **Module not found**: Check relative paths in test files
2. **Variable validation errors**: Ensure all required variables are provided
3. **Regex match failures**: Verify generated names meet Azure requirements
4. **Length violations**: Check that names are within 6-50 character range

## Performance Expectations

- **Base module tests**: ~5-10 seconds
- **ServiceBus module tests**: ~10-15 seconds  
- **Integration tests**: ~15-20 seconds
- **Total test suite**: ~30-45 seconds

Tests run planning operations only (no actual resource creation) for speed and safety.

## Continuous Integration

These tests can be integrated into CI/CD pipelines:

```yaml
# GitHub Actions example
- name: Run Terraform Tests
  run: |
    cd terraform-saif-utilities/modules/naming
    terraform test -test-directory=tests
```

```yaml
# Azure DevOps example
- script: |
    cd $(Build.SourcesDirectory)/terraform-saif-utilities/modules/naming
    terraform test -test-directory=tests
  displayName: 'Run Naming Module Tests'
```

---

[View source on GitHub](https://github.com/saif-corp/forge/blob/main/src/terraform/saif-utilities/modules/naming/tests/README.md)
