Test work
Add Terraform Module Development Standards & Enhanced Azure Tests
Summary of Changes
This update introduces a comprehensive set of Terraform module development standards and enhances the testing framework for Azure Terraform modules by introducing new testing scripts, configurations, and extensive documentation. Additionally, environment variables and test workflows have been modified to introduce better defaults and improved validation.
Key Modifications and Their Purpose
1. Documentation and Standards for Terraform Development
-
File Added:
infra/modules/.cursor/rules/terraform-module.mdc- Introduces Terraform module development guidelines, including:
- Directory structure standards (
main.tf,variables.tf,outputs.tf, etc.). - Coding conventions for variables, outputs, and dynamic blocks.
- Testing standards with a focus on both
unitandintegrationtests. - Best practices for security, naming, resource management, and CI/CD pipelines.
- Directory structure standards (
- Introduces Terraform module development guidelines, including:
2. Enhancements to magefile.go
- Added a default setup for
RESOURCE_GROUP_NAMEin theFindAndRunTestsfunction if not already provided. - Introduced a
Test(module string)function for executing module-specifictest.shscripts.- Ensures scripts are executable and validated.
- Sets default environment variables like
RESOURCE_GROUP_NAMEandLOCATIONif not set.
3. Addition of Module Testing Scripts
-
New Files Added: Testing scripts for Azure modules, including:
infra/modules/providers/azure/app-insights/test.shinfra/modules/providers/azure/log-analytics/test.shinfra/modules/providers/azure/resource-group/test.shinfra/modules/providers/azure/storage-account/test.sh- Purpose: Standardizes tests for core modules, including validation, variable setup, Terraform execution, and cleanup processes.
- All scripts initialize environment variables, validate configurations, and utilize a shared testing framework for consistency.
4. Updates to Individual Module Testing Configurations
- Introduced tailored
testing/main.tfandunit_test.gofor the following modules:-
Azure App Insights:
- Adds Log Analytics workspace integration with workspace ID validation.
-
Azure Resource Group:
- Modularized
main.tfwith variable-driven configurations forname,location, and tagging. - Enhanced
unit_test.goto utilize dynamic generation of resource group names and better support environment variable overrides.
- Modularized
-
Azure Storage Account:
- Modularized Terraform configuration with support for additional resource types (containers, queues, and shares).
- Enhanced tests to validate storage account configurations, including TLS enforcement and HTTPS-only access.
-
Azure App Insights:
5. Shared Common Testing Functions
-
New File Added:
infra/modules/providers/azure/test-functions.sh- Acts as a base testing library with reusable functions:
-
setup_configuration: Handles common Terraform setup for tests. -
generate_unique_name: Provides unique name generation for resources. -
terraform_init_and_apply: Standardizes Terraform initialization and deployment. -
run_standard_test_sequence: Provides a structured flow for unit and integration tests. - Includes environment validation (
validate_azure_credentials), cleanup, logging, and testing utilities.
-
- Acts as a base testing library with reusable functions:
6. Updates to Integration and Unit Tests
- Adjustments to
unit_test.goandintegrationtests of specific modules:-
Azure Storage Account:
-
ExpectedResourceAttributeValuesimproved to validate additional outputs likemin_tls_versionandenable_https_traffic_only. - Added dynamic name setups from environment variables for better test control.
-
-
Azure Resource Group:
-
unit_test.goupdated to dynamically pullLOCATIONandRESOURCE_GROUP_NAMEfrom the environment or generate them as needed.
-
-
Integration Test Updates:
- Refactored options for both unit and integration testing to include explicit parameterization of variables like
location,replication_type, etc.
- Refactored options for both unit and integration testing to include explicit parameterization of variables like
-
Azure Storage Account:
7. Licensing Header Fixes
- Resolved inconsistencies in license headers of Go files (
unit_test.go).- Reformatted headers for compliance with Apache 2.0 licensing.
8. Improved Azure Module Outputs
- Enhanced
outputs.tfforresource-groupandstorage-accountmodules:- Added structured outputs for resource IDs, properties, and additional sensitive attributes.
- Enabled detailed output structures for downstream module dependencies.
9. Adjustments to Terraform Providers
- Updated
main.tffiles for modules (storage-account,resource-group, etc.) to explicitly pin theazurermprovider version (3.90.0).
10. Minor Enhancements to Unit Test Configurations
- Updated
tf_options.goto support more flexible integration testing:- Added
ContainerName,replication_type, and improved handling of environment overrides.
- Added
Notable Technical Details
-
Dynamic Variable Validation:
- All module-specific tests employ dynamic resource generation, ensuring tests remain idempotent.
- Introduced safeguards for environment variable validation and resource naming conventions.
-
Integrated Resource Cleanup:
-
test.shscripts handle thorough cleanup via Azure CLI (az group delete), ensuring no residual artifacts post-tests.
-
-
Terraform Configuration Validation:
- Introduces
terraform planas part of the test lifecycle to pre-validate configurations before applying changes.
- Introduces
-
Enhanced Testing Framework:
- Shifts to a unified testing framework (
test-functions.sh) to eliminate redundancies across module configurations. - Modular composition of tests improves scalability and testing consistency across the infrastructure codebase.
- Shifts to a unified testing framework (
Edited by Daniel Scholl (MS]