### Install Local Provider Binary Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/scripts/mock-test/README.md Install the locally built binary into the dev_overrides path for the harness to use. ```bash GOOS=$(go env GOOS); GOARCH=$(go env GOARCH) LOCAL_PLUGIN_DIR=~/.terraform.d/plugins/aztfmod.com/arnaudlh/azurecaf/1.0.0/${GOOS}_${GOARCH} mkdir -p "$LOCAL_PLUGIN_DIR" && cp ./terraform-provider-azurecaf "$LOCAL_PLUGIN_DIR/" ``` -------------------------------- ### Install Act using Homebrew or Script Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/ACT_TESTING_GUIDE.md Install Act on macOS using Homebrew or download and run the installation script for other systems. Ensure Docker Desktop is installed and running. ```bash # macOS with Homebrew brew install act ``` ```bash # Or download from GitHub releases curl -sL https://raw.githubusercontent.com/nektos/act/master/install.sh | bash ``` -------------------------------- ### Setup Terraform and Go in CI Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/CI_E2E_INTEGRATION.md Configures Terraform and Go environments within the CI pipeline using GitHub Actions. Ensures the correct versions are installed for provider development and testing. ```yaml - name: Setup Terraform uses: hashicorp/setup-terraform@v3 with: terraform_version: "~> 1.0" - name: Set up Go uses: actions/setup-go@v5 with: go-version-file: './go.mod' ``` -------------------------------- ### Good Naming Practice Example Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/docs/resources/azurecaf_name.md Demonstrates a recommended approach for generating Azure resource names that stay within length limits. This example uses short, descriptive components for the base name, prefixes, suffixes, and random length. ```hcl # Good: Short, descriptive components resource "azurecaf_name" "example" { name = "api" resource_type = "azurerm_storage_account" prefixes = ["prod"] suffixes = ["001"] random_length = 3 } # Result: "stprodapi001abc" (15 chars - well within 24 char limit) ``` -------------------------------- ### Act Workflow Testing Strategy Examples Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/ACT_TESTING_GUIDE.md Examples demonstrating different stages of testing: quick validation during development, full workflow validation before commit, and debugging failed CI jobs locally. ```bash # Quick validation during development act pull_request --job e2e-tests -n ``` ```bash # Test specific changes act pull_request --job e2e-tests ``` ```bash # Full workflow validation before commit ./scripts/quick-ci-test.sh ``` ```bash # Comprehensive testing before PR ./scripts/test-ci-with-act.sh ``` ```bash # Debug failed CI jobs locally act pull_request --job e2e-tests -v ``` ```bash # Test environment setup act pull_request --job e2e-tests --env DEBUG=1 ``` -------------------------------- ### Best Practice: Short Components Example Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/docs/data-sources/azurecaf_name.md An example following best practices for avoiding truncation by using short, descriptive base names, essential prefixes/suffixes, and appropriate random length. ```hcl # Good: Short, descriptive components data "azurecaf_name" "example" { name = "api" resource_type = "azurerm_storage_account" prefixes = ["prod"] suffixes = ["001"] random_length = 3 } # Result: "stprodapi001abc" (15 chars - well within 24 char limit) ``` -------------------------------- ### Mock AzureRM PR Gate Setup Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/CHANGELOG.md Setup target for mock-azurerm tests. Ensures the necessary environment is prepared for validating azurerm resource schemas. ```makefile make test_mock_azurerm_setup ``` -------------------------------- ### Example Name Composition Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/docs/resources/azurecaf_name.md Demonstrates the step-by-step process of composing a resource name using various parameters like name, resource type, prefixes, suffixes, and random length. ```hcl resource "azurecaf_name" "example" { name = "myapp" resource_type = "azurerm_storage_account" prefixes = ["corp", "prod"] suffixes = ["web", "001"] random_length = 3 separator = "-" } ``` -------------------------------- ### Example Terraform Import Command Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/e2e/README.md Demonstrates the format for importing an existing Azure resource into Terraform state using the provider. This is tested in the import functionality tests. ```bash terraform import azurecaf_name.imported_storage azurerm_storage_account:stmyexistingapp ``` -------------------------------- ### Build and Test Provider Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/examples/README.md Commands to build the Terraform provider locally and run all example configurations. ```bash make build ``` ```bash make test ``` -------------------------------- ### Passthrough Mode Example Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/docs/data-sources/azurecaf_name.md Shows how to use the passthrough mode, where only the 'name' parameter is used for validation, and other composition elements like prefixes and suffixes are ignored. ```hcl data "azurecaf_name" "example" { name = "mycustomstorageaccount" resource_type = "azurerm_storage_account" passthrough = true # prefixes, suffixes, etc. are ignored } # Result: "mycustomstorageaccount" ``` -------------------------------- ### Run All Tests (Bash) Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/TESTING.md Execute all unit tests, or all tests including integration, or CI-friendly tests without integration. Ensure Go and Make are installed. ```bash # Unit tests only (fast) make unittest # All tests including integration make test_all # CI-friendly tests (unit + coverage, no integration) make test_ci ``` -------------------------------- ### Module Integration Example for App Service Resources Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/examples/README.md This example shows how to integrate the azurecaf_name data source within a Terraform module to generate names for a set of related App Service resources, including resource group, app service plan, app service, and application insights. ```hcl variable "application_name" { description = "Name of the application" type = string } variable "environment" { description = "Environment (dev/test/prod)" type = string } variable "instance" { description = "Instance number" type = string default = "001" } # Generate names for all app service resources data "azurecaf_name" "app_service_resources" { for_each = toset([ "azurerm_resource_group", "azurerm_app_service_plan", "azurerm_app_service", "azurerm_application_insights" ]) name = var.application_name resource_type = each.key prefixes = [var.environment] suffixes = [var.instance] } # Create resources with generated names resource "azurerm_resource_group" "main" { name = data.azurecaf_name.app_service_resources["azurerm_resource_group"].result location = "East US" } resource "azurerm_app_service_plan" "main" { name = data.azurecaf_name.app_service_resources["azurerm_app_service_plan"].result location = azurerm_resource_group.main.location resource_group_name = azurerm_resource_group.main.name sku { tier = "Standard" size = "S1" } } # Output generated names for reference output "resource_names" { description = "Generated resource names" value = { for key, name_data in data.azurecaf_name.app_service_resources : key => name_data.result } } ``` -------------------------------- ### Run Specific Unit Tests (Go) Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/TESTING.md Execute specific unit tests using Go commands, with options for verbose output or filtering by test name patterns. Ensure Go is installed. ```go # Standard unit tests go test ./azurecaf/... # With verbose output go test -v ./azurecaf/... # Specific test patterns go test ./azurecaf/... -run="TestResourceName" go test ./azurecaf/... -run="TestNamingConvention" ``` -------------------------------- ### Name Composition Example Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/docs/data-sources/azurecaf_name.md Demonstrates how the Azure CAF name data source composes a resource name by combining prefixes, slug, base name, suffixes, and random characters. ```hcl data "azurecaf_name" "example" { name = "myapp" resource_type = "azurerm_storage_account" prefixes = ["corp", "prod"] suffixes = ["web", "001"] random_length = 3 separator = "-" } ``` -------------------------------- ### Coverage Analysis (Go) Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/TESTING.md Analyze code coverage using Go commands. Generate a coverage profile, view coverage in the terminal, or generate an HTML report. Ensure Go is installed. ```go # Generate coverage profile go test -coverprofile=coverage.out ./azurecaf/... # View coverage in terminal go tool cover -func=coverage.out # Generate HTML coverage report go tool cover -html=coverage.out -o coverage.html ``` -------------------------------- ### Case Conversion Example Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/docs/data-sources/azurecaf_name.md Illustrates how the data source converts resource names to lowercase when the resource type requires it, ensuring compliance with Azure naming conventions. ```hcl # Input with mixed case data "azurecaf_name" "example" { name = "MyApp" resource_type = "azurerm_storage_account" # Requires lowercase } # Result: "stmyapp" (converted to lowercase) ``` -------------------------------- ### Basic Unit Test Structure in Go Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/TESTING.md Example of a table-driven unit test in Go. It defines multiple test cases with inputs and expected outputs to verify basic functionality. ```go func TestResourceName_BasicFunctionality(t *testing.T) { tests := []struct { name string input map[string]interface{}, expected string, wantErr bool }{ { name: "basic storage account name", input: map[string]interface{}{ "name": "myapp", "resource_type": "azurerm_storage_account", "prefixes": []string{"prod"}, }, expected: "stprodmyapp", wantErr: false, }, // More test cases... } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { // Test implementation }) } } ``` -------------------------------- ### Test Result Indicators Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/COMPLETE_TESTING_GUIDE.md Example output indicating successful name generation for different configurations of an Azure Storage Account. Shows generated names meeting constraints. ```bash # Running tests shows: ✓ Config 1 for azurerm_storage_account: dev-st-testname-xj8kl-001 ✓ Config 2 for azurerm_storage_account: prod-web-st-testname-x8k-001-east ✓ Config 3 for azurerm_storage_account: test-st-testname ``` -------------------------------- ### Suffix Truncation Example Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/docs/data-sources/azurecaf_name.md Demonstrates how suffixes are handled when the total name length exceeds the available space. It shows which suffixes are included and which are skipped based on length. ```hcl data "azurecaf_name" "example" { name = "myapp" # 5 chars resource_type = "azurerm_storage_account" suffixes = ["production", "web", "001"] # Multiple suffixes random_length = 8 # 8 chars use_slug = true # "st" = 2 chars } ``` -------------------------------- ### Debug Test by Logging Intermediate Values Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/TESTING.md This example demonstrates how to use 't.Logf' within a test function to log intermediate values of configuration, results, and errors. This is helpful for inspecting test state during failures. ```go func TestDebugExample(t *testing.T) { config := map[string]interface{}{ "name": "debug", "resource_type": "azurerm_storage_account", } result, err := generateName(config) // Debug output t.Logf("Config: %+v", config) t.Logf("Result: %s", result) t.Logf("Error: %v", err) // Assertions assert.NoError(t, err) assert.NotEmpty(t, result) } ``` -------------------------------- ### Act Performance Optimization Examples Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/ACT_TESTING_GUIDE.md Optimize Act performance by using specific container images, managing resources, and selectively running tests. This includes limiting resources and skipping jobs. ```bash # Limit resource usage act --container-cap-add SYS_ADMIN --container-cap-drop ALL ``` ```bash # Use specific container options act --container-options "-m 2g --cpus 2" ``` ```bash # Test only changed workflows act --detect-event ``` ```bash # Skip time-consuming jobs act --skip job-name ``` -------------------------------- ### Integration Test Structure for Data Sources in Go Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/TESTING.md Example of an integration test structure for a data source using the Terraform testing framework. It defines providers, test steps, and assertion checks. ```go func TestAccDataSourceAzureCAFName_StorageAccount(t *testing.T) { resource.Test(t, resource.TestCase{ Providers: testAccProviders, Steps: []resource.TestStep{ { Config: testAccDataSourceAzureCAFName_StorageAccount(), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("data.azurecaf_name.test", "result", "stmyapp123"), resource.TestCheckResourceAttr("data.azurecaf_name.test", "id", "stmyapp123"), ), }, }, }) } ``` -------------------------------- ### Environment-Based Naming Configuration Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/docs/index.md This example demonstrates how to configure resource naming dynamically based on the environment (e.g., dev, prod) using Terraform locals and the azurecaf_name data source. It allows for different prefix and random string lengths per environment. ```hcl locals { environment_config = { dev = { prefix = "dev" random_length = 3 } prod = { prefix = "prod" random_length = 5 } } current_env = local.environment_config[var.environment] } data "azurecaf_name" "app_service" { name = var.application_name resource_type = "azurerm_app_service" prefixes = [local.current_env.prefix] random_length = local.current_env.random_length } ``` -------------------------------- ### Run Quick E2E Tests via Makefile Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/e2e/README.md Execute a subset of quick end-to-end tests using the make command. Useful for faster feedback cycles. ```bash make test_e2e_quick ``` -------------------------------- ### Run All E2E Tests via Makefile Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/e2e/README.md Execute all end-to-end tests for the provider using the make command. This is the recommended way to run the full test suite. ```bash make test_e2e ``` -------------------------------- ### Example Breakdown of Name Pattern Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/docs/resources/azurecaf_naming_convention.md Illustrates the input parameters and the resulting output for the 'cafrandom' naming convention, including random padding. ```plaintext [prefix]-[name]-[resource_type_abbreviation]-[postfix]-[padding] Input: prefix="dev", name="myapp", resource_type="rg", postfix="001", convention="cafrandom" Output: "dev-myapp-rg-001-wxyz" (where "wxyz" is random padding) ``` -------------------------------- ### Act Configuration (.actrc) Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/ACT_TESTING_GUIDE.md Configure Act for optimal performance by specifying container architecture and using optimized Ubuntu images. This reduces resource usage and speeds up startup. ```bash -P ubuntu-latest=catthehacker/ubuntu:act-latest --container-architecture linux/amd64 ``` -------------------------------- ### Install Azure CAF Terraform Provider Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/docs/index.md Add the azurecaf provider to your Terraform configuration to use its features. Specify the source and desired version. ```hcl terraform { required_providers { azurecaf = { source = "aztfmod/azurecaf" version = "~> 1.2.32" } } } provider "azurecaf" { # Configuration options } ``` -------------------------------- ### Find Incomplete Resource Definitions Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/COMPLETE_TESTING_GUIDE.md Run this make target to identify resource definitions that are missing required fields. ```bash # Run this to find incomplete definitions: make test_resource_definitions ``` -------------------------------- ### Build the Provider Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/scripts/mock-test/README.md Build the provider locally so the harness can use it via dev_overrides. ```bash make build ``` -------------------------------- ### Post-Import Configuration for Storage Account Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/docs/resources/azurecaf_name.md After importing a resource, update your Terraform configuration to reflect the imported state. This example shows the configuration for an imported storage account. ```hcl resource "azurecaf_name" "storage" { name = "mystorageaccount123" resource_type = "azurerm_storage_account" passthrough = true } ``` -------------------------------- ### Run All Tests Directly with Go Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/e2e/README.md Execute all tests directly using the 'go test' command after navigating to the 'e2e' directory. Includes verbose output. ```bash cd e2e && go test -v ``` -------------------------------- ### Simulate CI Locally with Make Commands Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/TESTING.md These make commands simulate the CI environment locally. 'make test_ci' runs the same tests as CI, while 'make test_all' executes the full CI pipeline including cleaning and building. ```bash # Run the same tests as CI make test_ci # Or run everything like the full CI pipeline make clean make build make test_all ``` -------------------------------- ### Run All Tests (Unit and Integration) (Makefile Target) Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/README.md Execute all tests, including both unit and integration tests, using the Makefile. This provides a comprehensive test run. ```bash make test_all ``` -------------------------------- ### Prefix Truncation Example Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/docs/data-sources/azurecaf_name.md Illustrates how the Azure CAF name data source truncates components, prioritizing the base name and slug over prefixes when the maximum length is exceeded. ```hcl # Storage account max length: 24 characters data "azurecaf_name" "example" { name = "verylongapplicationname" # 23 chars resource_type = "azurerm_storage_account" prefixes = ["corporate"] # 9 chars + separator use_slug = true # "st" = 2 chars } ``` -------------------------------- ### Run Tests with Coverage Reporting (Makefile Target) Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/README.md Execute tests and generate coverage reporting using the Makefile. This command is equivalent to `go test -cover` but is invoked via the Makefile. ```bash make test_coverage ``` -------------------------------- ### Make Test All Resources Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/COMPLETE_TESTING_GUIDE.md Run all resource type tests. This is a comprehensive test that takes a long time to complete. ```bash # Test all resource types (comprehensive - takes time) make test_all_resources ``` -------------------------------- ### Pattern Validation Failure Example Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/docs/data-sources/azurecaf_name.md Demonstrates a scenario where name composition and truncation might fail pattern validation due to invalid characters for the specified resource type when 'clean_input' is false. ```hcl # This might fail validation if the pattern doesn't allow certain characters data "azurecaf_name" "example" { name = "my-app_name" resource_type = "azurerm_storage_account" # Only allows alphanumeric clean_input = false # Won't clean invalid characters } # Error: Pattern validation failed ``` -------------------------------- ### Run Fast CI Tests Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/COMPLETE_TESTING_GUIDE.md Use this make target for quick CI tests, including unit tests and coverage analysis, but excluding comprehensive integration tests. ```bash # Fast CI tests (unit + coverage, no comprehensive integration) make test_ci ``` -------------------------------- ### Run Data Source Integration Tests (Makefile Target) Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/README.md Execute data source integration tests using the Makefile. This target focuses on testing the provider's data sources. ```bash make test_data_sources ``` -------------------------------- ### Run Complete Test Suite Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/COMPLETE_TESTING_GUIDE.md Execute the entire test suite for all resources. This is a comprehensive test that may take a significant amount of time. ```bash # Run the complete test suite ./test_all_resources.sh ``` -------------------------------- ### Benchmark Name Generation Performance Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/TESTING.md Use this benchmark function to measure the performance of the name generation logic. It resets the timer and runs the generation multiple times to get an accurate performance reading. ```go func BenchmarkNameGeneration(b *testing.B) { config := map[string]interface{}{ "name": "benchmark", "resource_type": "azurerm_storage_account", "random_length": 5, } b.ResetTimer() for i := 0; i < b.N; i++ { _, err := generateName(config) if err != nil { b.Fatal(err) } } } ``` -------------------------------- ### Generate Storage Account Name Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/README.md Generate a compliant name for an Azure Storage Account using the `azurecaf_name` data source. This example demonstrates automatic resource type prefixes and random character generation. ```hcl data "azurecaf_name" "storage_account" { name = "mydata" resource_type = "azurerm_storage_account" random_length = 3 } resource "azurerm_storage_account" "example" { name = data.azurecaf_name.storage_account.result resource_group_name = azurerm_resource_group.example.name location = azurerm_resource_group.example.location account_tier = "Standard" account_replication_type = "LRS" } # Result: "stmydata123" (st = storage account slug, mydata = name, 123 = random) ``` -------------------------------- ### Run Integration Tests (Makefile Target) Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/README.md Execute integration tests using the Makefile. This target is a convenient way to run all integration tests. ```bash make test_integration ``` -------------------------------- ### Run Comprehensive Tests for All Resource Types Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/README.md Execute a comprehensive testing suite that covers all 405 Azure resource types supported by the provider. This ensures broad compatibility and correctness. ```bash make test_all_resources ``` -------------------------------- ### Terraform Test with Mock Provider Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/CHANGELOG.md Example of using `terraform test` with `mock_provider "azurerm" {}`. This validates generated CAF names against the live azurerm resource schema without requiring Azure credentials. ```hcl mock_provider "azurerm" {} ``` -------------------------------- ### Pattern Validation Failure Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/docs/resources/azurecaf_name.md Shows a scenario where name composition and truncation might lead to a validation error if the resulting name does not match the resource type's expected pattern. This example uses `clean_input = false` to highlight the issue. ```hcl # This might fail validation if the pattern doesn't allow certain characters resource "azurecaf_name" "example" { name = "my-app_name" resource_type = "azurerm_storage_account" # Only allows alphanumeric clean_input = false # Won't clean invalid characters } # Error: Pattern validation failed ``` -------------------------------- ### Run Specific Integration Tests (Go) Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/TESTING.md Execute specific integration tests using Go commands, requiring the TF_ACC=1 environment variable. Options for verbose output and filtering by test name patterns are available. ```go # All integration tests (requires TF_ACC=1) TF_ACC=1 go test -v ./azurecaf/... -run="TestAcc" # Specific integration test categories TF_ACC=1 go test -v ./azurecaf/... -run="TestAccDataSourcesIntegration" TF_ACC=1 go test -v ./azurecaf/... -run="TestAccErrorHandling" ``` -------------------------------- ### Generate Names for Multiple Related Resources Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/README.md Generate consistent names for multiple related Azure resources like App Service Plan, App Service, and Key Vault. This example uses local variables for project and environment names, applying prefixes and suffixes. ```hcl locals { project = "webapp" env = "prod" } data "azurecaf_name" "app_service_plan" { name = local.project resource_type = "azurerm_app_service_plan" prefixes = [local.env] suffixes = ["001"] } data "azurecaf_name" "app_service" { name = local.project resource_type = "azurerm_app_service" prefixes = [local.env] suffixes = ["001"] } data "azurecaf_name" "key_vault" { name = local.project resource_type = "azurerm_key_vault" prefixes = [local.env] suffixes = ["001"] } # Results: # App Service Plan: "plan-prod-webapp-001" # App Service: "app-prod-webapp-001" # Key Vault: "kv-prod-webapp-001" ``` -------------------------------- ### Make Test Complete Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/COMPLETE_TESTING_GUIDE.md Execute the complete test suite, covering all aspects of the provider's functionality. This is an exhaustive test. ```bash # Complete test suite (everything) make test_complete ``` -------------------------------- ### Go Unit Test for Naming Conventions Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/examples/README.md This Go test case demonstrates how to write unit tests for your naming conventions using the azurecaf_name data source. It includes a sample test for a production storage account with specific prefixes and random length. ```go package main import "testing" func TestNamingConventions(t *testing.T) { tests := []struct { name string config map[string]interface{} expectedMatch string }{ { name: "production storage account", config: map[string]interface{}{ "name": "myapp", "resource_type": "azurerm_storage_account", "prefixes": []string{"prod"}, "random_length": 3, }, expectedMatch: `^stprodmyapp[a-z0-9]{3}$`, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { // Test implementation }) } } ``` -------------------------------- ### Execute Quick CI Environment Test Script Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/ACT_TESTING_GUIDE.md Run a local script designed for quick CI environment testing. This is often used for initial validation before committing changes. ```bash ./scripts/quick-ci-test.sh ``` -------------------------------- ### Troubleshooting Docker and Permissions Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/ACT_TESTING_GUIDE.md Commands and steps to troubleshoot common issues with Act, including Docker not being found, container architecture errors on M-series Macs, and Docker permission errors. ```bash # Check Docker status docker info ``` ```bash # Start Docker Desktop if needed ``` ```bash # Use explicit architecture act --container-architecture linux/amd64 ``` ```bash # Check Docker permissions docker run hello-world ``` ```bash # Fix Docker permissions if needed sudo chmod 666 /var/run/docker.sock ``` ```bash # Use host network act --container-options "--network host" ``` -------------------------------- ### Storage and Databases Resources Added Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/CHANGELOG.md Definitions for new storage and database resources including SQL databases, MSSQL virtual machines, Cosmos DB configurations, and storage encryption scopes. ```go azurerm_sql_database ``` ```go azurerm_mssql_virtual_machine ``` ```go azurerm_cosmosdb_cassandra_keyspace ``` ```go azurerm_cosmosdb_gremlin_database ``` ```go azurerm_cosmosdb_gremlin_graph ``` ```go azurerm_cosmosdb_mongo_collection ``` ```go azurerm_cosmosdb_mongo_database ``` ```go azurerm_cosmosdb_sql_container ``` ```go azurerm_cosmosdb_sql_database ``` ```go azurerm_cosmosdb_sql_stored_procedure ``` ```go azurerm_cosmosdb_table ``` ```go azurerm_storage_encryption_scope ``` ```go azurerm_storage_data_lake_gen2_path ``` ```go azurerm_shared_image_version ``` -------------------------------- ### Run Resource Naming Convention Tests (Makefile Target) Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/README.md Execute tests that validate resource naming conventions using the Makefile. This target ensures adherence to Azure's naming rules. ```bash make test_resource_naming ``` -------------------------------- ### Available Test Commands (Bash) Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/TESTING.md Utilize Makefile targets for running unit tests with or without coverage, integration tests, comprehensive testing, or specialized naming convention tests. Build and clean commands are also available. ```bash # Unit Tests (Fast - No Terraform Required) make unittest # Run all unit tests without coverage make test_coverage # Run unit tests with coverage reporting make test_coverage_html # Generate HTML coverage report # Integration Tests (Slower - Requires Terraform) make test_integration # Run all integration tests make test_data_sources # Run data source integration tests make test_error_handling # Run error handling integration tests # Comprehensive Testing make test_all # Run both unit and integration tests make test_ci # Run CI tests (unit + coverage, no integration) # Specialized Tests make test_resource_naming # Run naming convention tests specifically # Build and Test make build # Build project and run unit tests make clean # Clean up build artifacts and test results ``` -------------------------------- ### Run Unit Tests (Makefile Target) Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/README.md Execute unit tests without coverage reporting using the Makefile. This is a quick way to run basic tests during development. ```bash make unittest ``` -------------------------------- ### Terraform Plan Command Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/examples/README.md Execute this command to preview the changes Terraform will make, including the generated resource names, without actually creating or modifying any resources. ```bash terraform plan ``` -------------------------------- ### Run All Tests (Unit and Integration) for Azure CAF Terraform Provider Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/README.md Execute all available tests, including both unit and integration tests, using the make test_all command. This provides a comprehensive test suite. ```bash # All tests (unit + integration) make test_all ``` -------------------------------- ### Run Standard Unit Tests Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/README.md Execute the standard unit tests for the Azure CAF Terraform provider using Go's testing framework. This command is essential for verifying individual components and functions. ```bash go test ./azurecaf/... ``` -------------------------------- ### List Available Workflows with Act Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/ACT_TESTING_GUIDE.md Use the `--list` flag to see all available workflows and their corresponding filenames within the repository. This helps in identifying which workflows can be tested. ```bash act --list ``` -------------------------------- ### Run Tests with Verbose Output Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/TESTING.md Use the '-v' flag with 'go test' to enable verbose output, showing individual test cases being run. This is useful for identifying which specific tests are executing. ```bash go test -v ./azurecaf/... -run="TestSpecificFunction" ``` -------------------------------- ### Before JSON Structure Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/history/JSON_REFACTOR_SUMMARY.md Illustrates the previous flat structure of the resource definition JSON. ```json { "name": "azurerm_api_management", "slug": "apim", "resource": "Azure Api Management", "resource_provider_namespace": "Unknown", "out_of_doc": false, ...other attributes... } ``` -------------------------------- ### Make Test Resource Definitions Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/COMPLETE_TESTING_GUIDE.md Validate that all resource definitions are complete. This ensures the integrity and completeness of resource configurations. ```bash # Validate all resource definitions are complete make test_resource_definitions ``` -------------------------------- ### Make Test Coverage Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/COMPLETE_TESTING_GUIDE.md Run the standard test suite along with code coverage analysis. This provides insights into both functionality and code quality. ```bash # Run standard test suite with coverage make test_coverage ``` -------------------------------- ### Detailed Coverage Analysis (Bash) Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/TESTING.md These bash commands facilitate in-depth analysis of test coverage. The first command generates a coverage profile, which is then used by `go tool cover` to display coverage by function or to generate an HTML report for visual inspection. ```bash # Coverage by function go test -coverprofile=coverage.out ./azurecaf/... go tool cover -func=coverage.out # Coverage by file go tool cover -html=coverage.out ``` -------------------------------- ### Test Specific Resource Types (Storage) Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/COMPLETE_TESTING_GUIDE.md Use this command to run tests exclusively for storage-related resources. ```bash # Test only storage resources go test ./azurecaf/... -run="TestResourceMatrix.*Storage" ``` -------------------------------- ### Multiple Resources with Consistent Naming Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/docs/resources/azurecaf_naming_convention.md Demonstrates how to generate consistent names for multiple Azure resources, such as a storage account and a virtual network, using the same prefix and base name with the 'cafrandom' convention. This ensures uniformity across related resources. ```hcl # Storage Account resource "azurecaf_naming_convention" "storage" { name = "myapp" prefix = "prod" resource_type = "st" convention = "cafrandom" } # Virtual Network resource "azurecaf_naming_convention" "vnet" { name = "myapp" prefix = "prod" resource_type = "vnet" convention = "cafrandom" } # Use in resources resource "azurerm_storage_account" "example" { name = azurecaf_naming_convention.storage.result resource_group_name = azurerm_resource_group.example.name location = azurerm_resource_group.example.location # ... other configuration } ``` -------------------------------- ### Data Source vs. Resource for Naming Source: https://github.com/aztfmod/terraform-provider-azurecaf/blob/main/examples/README.md Demonstrates using the `azurecaf_name` data source for plan-time evaluation and the `azurecaf_name` resource for generating multiple related names. ```hcl # Evaluated during plan phase - shows name before creation data "azurecaf_name" "storage" { name = "mydata" resource_type = "azurerm_storage_account" prefixes = ["prod"] random_length = 3 } resource "azurerm_storage_account" "example" { name = data.azurecaf_name.storage.result # ... other configuration } ``` ```hcl # Useful for generating multiple related names resource "azurecaf_name" "multi_res" { name = "myapp" resource_type = "azurerm_app_service" resource_types = [ "azurerm_app_service_plan", "azurerm_application_insights" ] prefixes = ["prod"] random_length = 3 } # Access names: # Primary: azurecaf_name.multi_res.result # All: azurecaf_name.multi_res.results ```