diff --git a/actions_bootstrap.ps1 b/actions_bootstrap.ps1 index 71665a4..0218488 100644 --- a/actions_bootstrap.ps1 +++ b/actions_bootstrap.ps1 @@ -29,6 +29,11 @@ $null = $modulesToInstall.Add(([PSCustomObject]@{ ModuleName = 'platyPS' ModuleVersion = '0.12.0' })) +# https://github.com/cloudbase/powershell-yaml +$null = $modulesToInstall.Add(([PSCustomObject]@{ + ModuleName = 'powershell-yaml' + ModuleVersion = '0.4.12' + })) 'Installing PowerShell Modules' foreach ($module in $modulesToInstall) { diff --git a/src/ALZ/Private/Deploy-Accelerator-Helpers/New-Bootstrap.ps1 b/src/ALZ/Private/Deploy-Accelerator-Helpers/New-Bootstrap.ps1 index 54cd438..3bb37d3 100644 --- a/src/ALZ/Private/Deploy-Accelerator-Helpers/New-Bootstrap.ps1 +++ b/src/ALZ/Private/Deploy-Accelerator-Helpers/New-Bootstrap.ps1 @@ -181,6 +181,36 @@ function New-Bootstrap { } } + $architectureFileToRemove = $null + $architectureFilePathInputName = "terraform_architecture_file_path" + if ($iac -eq "terraform" -and $hasStarter -and + $bootstrapParameters.PSObject.Properties.Name -contains $architectureFilePathInputName -and + $inputConfig.PSObject.Properties.Name -notcontains $architectureFilePathInputName -and + $null -eq [Environment]::GetEnvironmentVariable("TF_VAR_$architectureFilePathInputName") -and + $null -eq [Environment]::GetEnvironmentVariable("TF_VAR_management_group_settings")) { + + $architectureName = "" + if ($inputConfig.PSObject.Properties.Name -contains "management_group_settings" -and $null -ne $inputConfig.management_group_settings.Value.architecture_name) { + $architectureName = $inputConfig.management_group_settings.Value.architecture_name + } + + $architectureFile = Resolve-TerraformArchitectureFile ` + -starterModulePath $starterRootModuleFolderPath ` + -starterAdditionalFiles $starterAdditionalFiles ` + -defaultArchitectureFilePath $bootstrapParameters.$architectureFilePathInputName.DefaultValue ` + -architectureName $architectureName ` + -bootstrapStatePath (Join-Path -Path $bootstrapModulePath -ChildPath "terraform.tfstate") + + if ($null -ne $architectureFile.ArchitectureFilePath) { + $inputConfig | Add-Member -NotePropertyName $architectureFilePathInputName -NotePropertyValue @{ + Value = $architectureFile.ArchitectureFilePath + Source = "calculated" + Sensitive = $false + } + $architectureFileToRemove = $architectureFile.DefaultFileToRemove + } + } + Write-Verbose "Final Input config: $(ConvertTo-Json $inputConfig -Depth 100)" # Getting the input for the bootstrap module @@ -283,6 +313,11 @@ function New-Bootstrap { } } + if ($null -ne $architectureFileToRemove -and (Test-Path -LiteralPath $architectureFileToRemove)) { + Write-ToConsoleLog "Removing the starter architecture file $architectureFileToRemove because your file '$($inputConfig.terraform_architecture_file_path.Value)' defines the same architecture." + Remove-Item -LiteralPath $architectureFileToRemove -Force + } + # Running terraform init and apply Write-ToConsoleLog "Thank you for providing those inputs, we are now initializing and applying Terraform to bootstrap your environment..." -IsSuccess diff --git a/src/ALZ/Private/Deploy-Accelerator-Helpers/Resolve-TerraformArchitectureFile.ps1 b/src/ALZ/Private/Deploy-Accelerator-Helpers/Resolve-TerraformArchitectureFile.ps1 new file mode 100644 index 0000000..9f9ac03 --- /dev/null +++ b/src/ALZ/Private/Deploy-Accelerator-Helpers/Resolve-TerraformArchitectureFile.ps1 @@ -0,0 +1,197 @@ +function Resolve-TerraformArchitectureFile { + [CmdletBinding()] + param( + [Parameter(Mandatory = $true)] + [string] $starterModulePath, + + [Parameter(Mandatory = $false)] + [string[]] $starterAdditionalFiles = @(), + + [Parameter(Mandatory = $true)] + [string] $defaultArchitectureFilePath, + + [Parameter(Mandatory = $false)] + [string] $architectureName = "", + + [Parameter(Mandatory = $false)] + [string] $bootstrapStatePath = "" + ) + + $result = [PSCustomObject]@{ + ArchitectureFilePath = $null + DefaultFileToRemove = $null + } + + $pathComparison = [System.StringComparison]::OrdinalIgnoreCase + $defaultArchitectureFilePath = $defaultArchitectureFilePath.Replace('\', '/') + $libraryFolderName = $defaultArchitectureFilePath.Split('/')[0] + $defaultFileFullPath = Join-Path -Path $starterModulePath -ChildPath $defaultArchitectureFilePath + $defaultFileExists = Test-Path -LiteralPath $defaultFileFullPath -PathType Leaf + + function Get-ArchitectureDefinition { + param([string] $path) + try { + $content = Get-Content -LiteralPath $path -Raw + $definition = if ($path.EndsWith('.json')) { $content | ConvertFrom-Json } else { $content | ConvertFrom-Yaml } + } catch { + Write-Verbose "Could not parse architecture definition file ${path}: $_" + return $null + } + if ($null -eq $definition -or $definition.name -isnot [string] -or [string]::IsNullOrWhiteSpace($definition.name)) { + return $null + } + $rootManagementGroupId = @($definition.management_groups | Where-Object { $null -ne $_ -and $null -eq $_.parent_id } | ForEach-Object { $_.id }) + return [PSCustomObject]@{ + Name = $definition.name + RootManagementGroupId = if ($rootManagementGroupId.Count -eq 1) { $rootManagementGroupId[0] } else { $null } + } + } + + function Complete-WithoutChange { + if (-not $defaultFileExists) { + Write-ToConsoleLog "The bootstrap will read '$defaultArchitectureFilePath', but it is missing from the starter module at $starterModulePath. If an earlier run removed it, move your architecture file to '$($defaultArchitectureFilePath.Substring($defaultArchitectureFilePath.IndexOf('/') + 1))' inside your $libraryFolderName folder, so that only one file declares the architecture, or set 'terraform_architecture_file_path' in your inputs." -IsWarning + } + return $result + } + + $userArchitectureFiles = @() + foreach ($additionalFile in $starterAdditionalFiles) { + if (-not (Test-Path -LiteralPath $additionalFile -PathType Container)) { + continue + } + $folderName = ([System.IO.DirectoryInfo]::new($additionalFile)).Name + if ($folderName -cne $libraryFolderName) { + continue + } + $folderPath = (Resolve-Path -LiteralPath $additionalFile).Path + $files = Get-ChildItem -LiteralPath $folderPath -File -Recurse -Force | Where-Object { $_.Name -cmatch '\.alz_architecture_definition\.(json|yaml|yml)$' } + foreach ($file in $files) { + $relativePath = [System.IO.Path]::GetRelativePath($folderPath, $file.FullName).Replace('\', '/') + $userArchitectureFiles += [PSCustomObject]@{ + SourcePath = $file.FullName + DestinationPath = "$folderName/$relativePath" + } + } + } + + if ($userArchitectureFiles.Count -eq 0) { + return Complete-WithoutChange + } + + $copiedArchitectureFiles = [System.Collections.Specialized.OrderedDictionary]::new([System.StringComparer]::Ordinal) + foreach ($userArchitectureFile in $userArchitectureFiles) { + $copiedArchitectureFiles[$userArchitectureFile.DestinationPath] = $userArchitectureFile + } + $userArchitectureFiles = @($copiedArchitectureFiles.Values) + + if (@($userArchitectureFiles | Where-Object { [string]::Equals($_.DestinationPath, $defaultArchitectureFilePath, $pathComparison) }).Count -gt 0) { + return $result + } + + $needsYaml = @($userArchitectureFiles | Where-Object { -not $_.SourcePath.EndsWith('.json') }).Count -gt 0 -or ($defaultFileExists -and -not $defaultFileFullPath.EndsWith('.json')) + if ($needsYaml -and $null -eq (Get-Command -Name ConvertFrom-Yaml -ErrorAction SilentlyContinue)) { + Write-ToConsoleLog "Found architecture definition files ($($userArchitectureFiles.DestinationPath -join ', ')) but the powershell-yaml module is not available to read them, so the bootstrap will use '$defaultArchitectureFilePath'. Install powershell-yaml or set 'terraform_architecture_file_path' in your inputs." -IsWarning + return Complete-WithoutChange + } + + $defaultDefinition = if ($defaultFileExists) { Get-ArchitectureDefinition -path $defaultFileFullPath } else { $null } + + $starterTerraform = @(Get-ChildItem -LiteralPath $starterModulePath -Filter '*.tf' -File -ErrorAction SilentlyContinue | ForEach-Object { + $terraform = Get-Content -LiteralPath $_.FullName -Raw + $terraform = [regex]::Replace($terraform, '(?ms)<<-?\s*([A-Za-z_][A-Za-z0-9_]*)\s*$.*?^\s*\1\s*$', '') + [regex]::Replace($terraform, '(?s)/\*.*?\*/', '') + }) + $fixedNames = @($starterTerraform | ForEach-Object { [regex]::Matches($_, '(?m)^\s*architecture_name\s*=\s*"([^"$]+)"\s*(#.*|//.*)?$') } | ForEach-Object { $_.Groups[1].Value } | Select-Object -Unique) + $optionalDefaultNames = @($starterTerraform | ForEach-Object { [regex]::Matches($_, '(?m)^\s*architecture_name\s*=\s*optional\(\s*string\s*,\s*"([^"$]+)"\s*\)') } | ForEach-Object { $_.Groups[1].Value } | Select-Object -Unique) + + $targetArchitectureName = $null + if ($fixedNames.Count -eq 1) { + $targetArchitectureName = $fixedNames[0] + } elseif (-not [string]::IsNullOrWhiteSpace($architectureName)) { + $targetArchitectureName = $architectureName + } elseif ($optionalDefaultNames.Count -eq 1) { + $targetArchitectureName = $optionalDefaultNames[0] + } elseif ($null -ne $defaultDefinition) { + $targetArchitectureName = $defaultDefinition.Name + } + + $validFiles = @() + foreach ($userArchitectureFile in $userArchitectureFiles) { + $definition = Get-ArchitectureDefinition -path $userArchitectureFile.SourcePath + if ($null -eq $definition) { + Write-ToConsoleLog "Skipping architecture definition file '$($userArchitectureFile.DestinationPath)': it could not be read or has no name." -IsWarning + continue + } + $validFiles += [PSCustomObject]@{ + DestinationPath = $userArchitectureFile.DestinationPath + Name = $definition.Name + RootManagementGroupId = $definition.RootManagementGroupId + } + } + + $candidates = @($validFiles | Where-Object { $null -eq $targetArchitectureName -or $_.Name -ceq $targetArchitectureName }) + + if ($candidates.Count -eq 0) { + Write-ToConsoleLog "Found architecture definition files ($($userArchitectureFiles.DestinationPath -join ', ')) but none has the architecture name '$targetArchitectureName', so the bootstrap will use '$defaultArchitectureFilePath'. Set 'terraform_architecture_file_path' in your inputs to choose a file." -IsWarning + return Complete-WithoutChange + } + + if ($candidates.Count -gt 1) { + if ($null -eq $targetArchitectureName) { + Write-ToConsoleLog "Found more than one architecture definition file ($($candidates.DestinationPath -join ', ')) and could not tell which one the starter module uses, so the bootstrap will use '$defaultArchitectureFilePath'. Set 'terraform_architecture_file_path' in your inputs to choose a file." -IsWarning + return Complete-WithoutChange + } + $message = "Found more than one architecture definition file for architecture '$targetArchitectureName': $($candidates.DestinationPath -join ', '). Remove the duplicates or set 'terraform_architecture_file_path' in your inputs to choose a file." + Write-ToConsoleLog $message -IsError + throw $message + } + + $selected = $candidates[0] + + $existingRootManagementGroupIds = @() + if (-not [string]::IsNullOrWhiteSpace($bootstrapStatePath) -and (Test-Path -LiteralPath $bootstrapStatePath)) { + try { + $existingRootManagementGroupIds = & { + $ErrorActionPreference = 'Stop' + $bootstrapState = Get-Content -LiteralPath $bootstrapStatePath -Raw | ConvertFrom-Json -AsHashtable + if ($bootstrapState -isnot [hashtable]) { + throw "The bootstrap state is empty or not a JSON object." + } + , @($bootstrapState.resources | + Where-Object { $_.mode -eq "managed" -and $_.type -eq "azapi_resource" -and $_.name -eq "intermediate_root_management_group" } | + ForEach-Object { $_.instances } | + Where-Object { -not $_.ContainsKey("deposed") } | + ForEach-Object { $_.attributes.name } | + Select-Object -Unique) + } + } catch { + Write-Verbose "Could not read the bootstrap state at ${bootstrapStatePath}: $_" + $existingRootManagementGroupIds = $null + } + } + + if ($null -eq $existingRootManagementGroupIds -or $existingRootManagementGroupIds.Count -gt 1) { + Write-ToConsoleLog "Could not tell which intermediate root management group the bootstrap state at $bootstrapStatePath manages, so the bootstrap will use '$defaultArchitectureFilePath'. Set 'terraform_architecture_file_path' in your inputs to choose an architecture file." -IsWarning + return Complete-WithoutChange + } + $existingRootManagementGroupId = $existingRootManagementGroupIds | Select-Object -First 1 + + if (-not [string]::IsNullOrWhiteSpace($existingRootManagementGroupId) -and $selected.RootManagementGroupId -cne $existingRootManagementGroupId) { + if ($null -eq $defaultDefinition -or $defaultDefinition.RootManagementGroupId -cne $existingRootManagementGroupId) { + $message = "Your bootstrap manages the intermediate root management group '$existingRootManagementGroupId', but '$($selected.DestinationPath)' uses '$($selected.RootManagementGroupId)' and '$defaultArchitectureFilePath' does not use it either. Set 'terraform_architecture_file_path' in your inputs to choose the architecture file for the bootstrap." + Write-ToConsoleLog $message -IsError + throw $message + } + Write-ToConsoleLog "Your bootstrap manages the intermediate root management group '$existingRootManagementGroupId'. Using '$($selected.DestinationPath)' would change it to '$($selected.RootManagementGroupId)', so the bootstrap will keep using '$defaultArchitectureFilePath'. Set 'terraform_architecture_file_path' in your inputs to switch." -IsWarning + return Complete-WithoutChange + } + + Write-Verbose "Using architecture definition file '$($selected.DestinationPath)' for the bootstrap." + $result.ArchitectureFilePath = $selected.DestinationPath + + if ($null -ne $defaultDefinition -and $defaultDefinition.Name -ceq $selected.Name) { + $result.DefaultFileToRemove = $defaultFileFullPath + } + + return $result +} diff --git a/src/Tests/Unit/Private/New-Bootstrap.Tests.ps1 b/src/Tests/Unit/Private/New-Bootstrap.Tests.ps1 new file mode 100644 index 0000000..c9a15f5 --- /dev/null +++ b/src/Tests/Unit/Private/New-Bootstrap.Tests.ps1 @@ -0,0 +1,259 @@ +#------------------------------------------------------------------------- +Set-Location -Path $PSScriptRoot +#------------------------------------------------------------------------- +$ModuleName = 'ALZ' +$PathToManifest = [System.IO.Path]::Combine('..', '..', '..', $ModuleName, "$ModuleName.psd1") +#------------------------------------------------------------------------- +if (Get-Module -Name $ModuleName -ErrorAction 'SilentlyContinue') { + Remove-Module -Name $ModuleName -Force +} +Import-Module $PathToManifest -Force +#------------------------------------------------------------------------- + +InModuleScope 'ALZ' { + Describe 'New-Bootstrap architecture file handling' -Tag Unit { + BeforeAll { + Import-Module powershell-yaml -ErrorAction Stop + + function Add-TestFile { + param([string] $Path, [string] $Content) + New-Item -Path (Split-Path $Path -Parent) -ItemType Directory -Force | Out-Null + Set-Content -Path $Path -Value $Content + } + + function Get-TestInputConfig { + [PSCustomObject]@{ + starter_module_name = @{ Value = 'platform_landing_zone'; Source = 'input'; Sensitive = $false } + } + } + + function Invoke-TestBootstrap { + param([PSCustomObject] $InputConfig) + New-Bootstrap ` + -iac 'terraform' ` + -bootstrapDetails ([PSCustomObject]@{ Value = [PSCustomObject]@{ location = 'alz/local' } }) ` + -inputConfig $InputConfig ` + -bootstrapTargetPath $bootstrapTargetPath ` + -bootstrapRelease 'v1.0.0' ` + -hasStarter ` + -starterTargetPath $starterTargetPath ` + -starterRelease 'v2.0.0' ` + -starterConfig ([PSCustomObject]@{ starter_modules = [PSCustomObject]@{ Value = [PSCustomObject]@{ platform_landing_zone = [PSCustomObject]@{ location = 'platform_landing_zone' } } } }) ` + -hclParserToolPath 'hcl2json' ` + -starterAdditionalFiles @($userLibPath) ` + -autoApprove + } + } + + BeforeEach { + $root = Join-Path $TestDrive ([guid]::NewGuid().ToString()) + $bootstrapTargetPath = Join-Path $root 'boot-module' + $starterTargetPath = Join-Path $root 'starter-module' + $userLibPath = Join-Path $root 'config/lib' + $bootstrapTfvarsPath = Join-Path $bootstrapTargetPath 'v1.0.0/alz/local/terraform.tfvars.json' + $starterModulePath = Join-Path $starterTargetPath 'v2.0.0/platform_landing_zone' + $starterDefaultFile = Join-Path $starterModulePath 'lib/architecture_definitions/alz_custom.alz_architecture_definition.yaml' + + Add-TestFile -Path (Join-Path $bootstrapTargetPath 'v1.0.0/alz/local/variables.tf') -Content '' + Add-TestFile -Path (Join-Path $starterModulePath 'variables.tf') -Content '' + Add-TestFile -Path $starterDefaultFile -Content "name: alz_custom`nmanagement_groups:`n - id: alz`n parent_id: null" + New-Item -Path $userLibPath -ItemType Directory -Force | Out-Null + + Mock -CommandName Convert-HCLVariablesToInputConfig -ParameterFilter { $targetVariableFile -like '*boot-module*' } -MockWith { + [PSCustomObject]@{ + terraform_architecture_file_path = [PSCustomObject]@{ + Value = '' + Source = 'input' + DefaultValue = 'lib/architecture_definitions/alz_custom.alz_architecture_definition.yaml' + Description = '' + Sensitive = $false + } + } + } + Mock -CommandName Convert-HCLVariablesToInputConfig -ParameterFilter { $targetVariableFile -like '*starter-module*' } -MockWith { [PSCustomObject]@{} } + Mock -CommandName Invoke-Terraform -MockWith { } + Mock -CommandName Write-ToConsoleLog -MockWith { } + } + + It 'passes a renamed architecture file to the bootstrap and removes the duplicate default' { + Add-TestFile -Path (Join-Path $userLibPath 'architecture_definitions/contoso.alz_architecture_definition.yaml') -Content 'name: alz_custom' + + Invoke-TestBootstrap -InputConfig (Get-TestInputConfig) + + (Get-Content $bootstrapTfvarsPath -Raw | ConvertFrom-Json).terraform_architecture_file_path | Should -Be 'lib/architecture_definitions/contoso.alz_architecture_definition.yaml' + Test-Path (Join-Path $starterModulePath 'lib/architecture_definitions/contoso.alz_architecture_definition.yaml') | Should -BeTrue + Test-Path $starterDefaultFile | Should -BeFalse + } + + It 'keeps the default architecture file path when the user lib has no architecture file' { + Add-TestFile -Path (Join-Path $userLibPath 'archetype_definitions/root_custom.alz_archetype_override.yaml') -Content 'name: root_custom' + + Invoke-TestBootstrap -InputConfig (Get-TestInputConfig) + + (Get-Content $bootstrapTfvarsPath -Raw | ConvertFrom-Json).terraform_architecture_file_path | Should -Be 'lib/architecture_definitions/alz_custom.alz_architecture_definition.yaml' + Test-Path $starterDefaultFile | Should -BeTrue + } + + It 'keeps an explicit architecture file path input and the default file' { + Add-TestFile -Path (Join-Path $userLibPath 'architecture_definitions/contoso.alz_architecture_definition.yaml') -Content 'name: alz_custom' + $inputConfig = Get-TestInputConfig + $inputConfig | Add-Member -NotePropertyName 'terraform_architecture_file_path' -NotePropertyValue @{ Value = 'lib/architecture_definitions/explicit.alz_architecture_definition.yaml'; Source = '.yaml'; Sensitive = $false } + + Invoke-TestBootstrap -InputConfig $inputConfig + + (Get-Content $bootstrapTfvarsPath -Raw | ConvertFrom-Json).terraform_architecture_file_path | Should -Be 'lib/architecture_definitions/explicit.alz_architecture_definition.yaml' + Test-Path $starterDefaultFile | Should -BeTrue + } + + It 'uses the architecture name from the platform landing zone inputs' { + Add-TestFile -Path (Join-Path $userLibPath 'architecture_definitions/contoso.alz_architecture_definition.json') -Content '{ "name": "contoso" }' + $inputConfig = Get-TestInputConfig + $inputConfig | Add-Member -NotePropertyName 'management_group_settings' -NotePropertyValue @{ Value = [PSCustomObject]@{ architecture_name = 'contoso' }; Source = '.tfvars'; Sensitive = $false } + + Invoke-TestBootstrap -InputConfig $inputConfig + + (Get-Content $bootstrapTfvarsPath -Raw | ConvertFrom-Json).terraform_architecture_file_path | Should -Be 'lib/architecture_definitions/contoso.alz_architecture_definition.json' + Test-Path $starterDefaultFile | Should -BeTrue + } + + It 'gives the same result when run again on the same output folder' { + Add-TestFile -Path (Join-Path $userLibPath 'architecture_definitions/contoso.alz_architecture_definition.yaml') -Content 'name: alz_custom' + + Invoke-TestBootstrap -InputConfig (Get-TestInputConfig) + Invoke-TestBootstrap -InputConfig (Get-TestInputConfig) + + (Get-Content $bootstrapTfvarsPath -Raw | ConvertFrom-Json).terraform_architecture_file_path | Should -Be 'lib/architecture_definitions/contoso.alz_architecture_definition.yaml' + Test-Path $starterDefaultFile | Should -BeFalse + } + + It 'keeps the default when the architecture file path is set as a TF_VAR environment variable' { + Add-TestFile -Path (Join-Path $userLibPath 'architecture_definitions/contoso.alz_architecture_definition.yaml') -Content 'name: alz_custom' + $env:TF_VAR_terraform_architecture_file_path = 'lib/architecture_definitions/explicit.alz_architecture_definition.yaml' + try { + Invoke-TestBootstrap -InputConfig (Get-TestInputConfig) + } finally { + Remove-Item -Path Env:\TF_VAR_terraform_architecture_file_path + } + + (Get-Content $bootstrapTfvarsPath -Raw | ConvertFrom-Json).PSObject.Properties.Name | Should -Not -Contain 'terraform_architecture_file_path' + Test-Path $starterDefaultFile | Should -BeTrue + } + + It 'uses the architecture name in the starter default file when the inputs omit it' { + Add-TestFile -Path (Join-Path $userLibPath 'architecture_definitions/contoso.alz_architecture_definition.yaml') -Content 'name: alz_custom' + $inputConfig = Get-TestInputConfig + $inputConfig | Add-Member -NotePropertyName 'management_group_settings' -NotePropertyValue @{ Value = [PSCustomObject]@{ location = 'uksouth' }; Source = '.tfvars'; Sensitive = $false } + + Invoke-TestBootstrap -InputConfig $inputConfig + + (Get-Content $bootstrapTfvarsPath -Raw | ConvertFrom-Json).terraform_architecture_file_path | Should -Be 'lib/architecture_definitions/contoso.alz_architecture_definition.yaml' + } + + It 'stops before deleting anything when two user files declare the same architecture' { + Add-TestFile -Path (Join-Path $userLibPath 'architecture_definitions/contoso.alz_architecture_definition.yaml') -Content 'name: alz_custom' + Add-TestFile -Path (Join-Path $userLibPath 'architecture_definitions/fabrikam.alz_architecture_definition.yaml') -Content 'name: alz_custom' + + { Invoke-TestBootstrap -InputConfig (Get-TestInputConfig) } | Should -Throw '*more than one architecture definition file*' + + Test-Path $starterDefaultFile | Should -BeTrue + Should -Invoke -CommandName Invoke-Terraform -Times 0 + } + + It 'keeps the default when the bootstrap state manages a different root management group' { + Add-TestFile -Path (Join-Path $userLibPath 'architecture_definitions/contoso.alz_architecture_definition.yaml') -Content "name: alz_custom`nmanagement_groups:`n - id: contoso`n parent_id: null" + Add-TestFile -Path (Join-Path $bootstrapTargetPath 'v1.0.0/alz/local/terraform.tfstate') -Content '{ "version": 4, "resources": [ { "module": "module.azure[0]", "mode": "managed", "type": "azapi_resource", "name": "intermediate_root_management_group", "instances": [ { "index_key": 0, "attributes": { "name": "alz" } } ] } ] }' + + Invoke-TestBootstrap -InputConfig (Get-TestInputConfig) + + (Get-Content $bootstrapTfvarsPath -Raw | ConvertFrom-Json).terraform_architecture_file_path | Should -Be 'lib/architecture_definitions/alz_custom.alz_architecture_definition.yaml' + Test-Path $starterDefaultFile | Should -BeTrue + } + + It 'ignores deposed instances in the bootstrap state' { + Add-TestFile -Path (Join-Path $userLibPath 'architecture_definitions/contoso.alz_architecture_definition.yaml') -Content "name: alz_custom`nmanagement_groups:`n - id: alz`n parent_id: null" + Add-TestFile -Path (Join-Path $bootstrapTargetPath 'v1.0.0/alz/local/terraform.tfstate') -Content '{ "version": 4, "resources": [ { "module": "module.azure[0]", "mode": "managed", "type": "azapi_resource", "name": "intermediate_root_management_group", "instances": [ { "index_key": 0, "deposed": "00000001", "attributes": { "name": "old" } }, { "index_key": 0, "attributes": { "name": "alz" } } ] } ] }' + + Invoke-TestBootstrap -InputConfig (Get-TestInputConfig) + + (Get-Content $bootstrapTfvarsPath -Raw | ConvertFrom-Json).terraform_architecture_file_path | Should -Be 'lib/architecture_definitions/contoso.alz_architecture_definition.yaml' + } + + It 'keeps the default when the bootstrap state names more than one root management group' { + Add-TestFile -Path (Join-Path $userLibPath 'architecture_definitions/contoso.alz_architecture_definition.yaml') -Content "name: alz_custom`nmanagement_groups:`n - id: alz`n parent_id: null" + Add-TestFile -Path (Join-Path $bootstrapTargetPath 'v1.0.0/alz/local/terraform.tfstate') -Content '{ "version": 4, "resources": [ { "module": "module.azure[0]", "mode": "managed", "type": "azapi_resource", "name": "intermediate_root_management_group", "instances": [ { "index_key": 0, "attributes": { "name": "alz" } } ] }, { "module": "module.other", "mode": "managed", "type": "azapi_resource", "name": "intermediate_root_management_group", "instances": [ { "index_key": 0, "attributes": { "name": "contoso" } } ] } ] }' + + Invoke-TestBootstrap -InputConfig (Get-TestInputConfig) + + (Get-Content $bootstrapTfvarsPath -Raw | ConvertFrom-Json).terraform_architecture_file_path | Should -Be 'lib/architecture_definitions/alz_custom.alz_architecture_definition.yaml' + Should -Invoke -CommandName Write-ToConsoleLog -ParameterFilter { $IsWarning -and "$Messages" -like '*Could not tell which intermediate root management group*' } + } + + It 'keeps the default and warns when the bootstrap state cannot be read' { + Add-TestFile -Path (Join-Path $userLibPath 'architecture_definitions/contoso.alz_architecture_definition.yaml') -Content "name: alz_custom`nmanagement_groups:`n - id: contoso`n parent_id: null" + Add-TestFile -Path (Join-Path $bootstrapTargetPath 'v1.0.0/alz/local/terraform.tfstate') -Content '{ "version": 4, "resources": [' + + Invoke-TestBootstrap -InputConfig (Get-TestInputConfig) + + (Get-Content $bootstrapTfvarsPath -Raw | ConvertFrom-Json).terraform_architecture_file_path | Should -Be 'lib/architecture_definitions/alz_custom.alz_architecture_definition.yaml' + Test-Path $starterDefaultFile | Should -BeTrue + Should -Invoke -CommandName Write-ToConsoleLog -ParameterFilter { $IsWarning -and "$Messages" -like '*Could not tell which intermediate root management group*' } + Should -Invoke -CommandName Invoke-Terraform -Times 1 + } + + It 'keeps the default and warns when the bootstrap state is empty' { + Add-TestFile -Path (Join-Path $userLibPath 'architecture_definitions/contoso.alz_architecture_definition.yaml') -Content "name: alz_custom`nmanagement_groups:`n - id: contoso`n parent_id: null" + New-Item -Path (Join-Path $bootstrapTargetPath 'v1.0.0/alz/local/terraform.tfstate') -ItemType File -Force | Out-Null + + Invoke-TestBootstrap -InputConfig (Get-TestInputConfig) + + (Get-Content $bootstrapTfvarsPath -Raw | ConvertFrom-Json).terraform_architecture_file_path | Should -Be 'lib/architecture_definitions/alz_custom.alz_architecture_definition.yaml' + Test-Path $starterDefaultFile | Should -BeTrue + Should -Invoke -CommandName Write-ToConsoleLog -ParameterFilter { $IsWarning -and "$Messages" -like '*Could not tell which intermediate root management group*' } + } + + It 'keeps the default and warns when the bootstrap state is not a JSON object' { + Add-TestFile -Path (Join-Path $userLibPath 'architecture_definitions/contoso.alz_architecture_definition.yaml') -Content "name: alz_custom`nmanagement_groups:`n - id: contoso`n parent_id: null" + Add-TestFile -Path (Join-Path $bootstrapTargetPath 'v1.0.0/alz/local/terraform.tfstate') -Content '[ ]' + + Invoke-TestBootstrap -InputConfig (Get-TestInputConfig) + + (Get-Content $bootstrapTfvarsPath -Raw | ConvertFrom-Json).terraform_architecture_file_path | Should -Be 'lib/architecture_definitions/alz_custom.alz_architecture_definition.yaml' + Test-Path $starterDefaultFile | Should -BeTrue + Should -Invoke -CommandName Write-ToConsoleLog -ParameterFilter { $IsWarning -and "$Messages" -like '*Could not tell which intermediate root management group*' } + } + + It 'keeps the default and warns when the bootstrap state path is a folder' { + Add-TestFile -Path (Join-Path $userLibPath 'architecture_definitions/contoso.alz_architecture_definition.yaml') -Content "name: alz_custom`nmanagement_groups:`n - id: contoso`n parent_id: null" + New-Item -Path (Join-Path $bootstrapTargetPath 'v1.0.0/alz/local/terraform.tfstate') -ItemType Directory -Force | Out-Null + + Invoke-TestBootstrap -InputConfig (Get-TestInputConfig) + + (Get-Content $bootstrapTfvarsPath -Raw | ConvertFrom-Json).terraform_architecture_file_path | Should -Be 'lib/architecture_definitions/alz_custom.alz_architecture_definition.yaml' + Should -Invoke -CommandName Write-ToConsoleLog -ParameterFilter { $IsWarning -and "$Messages" -like '*Could not tell which intermediate root management group*' } + } + + It 'does not read the bootstrap state when the user lib has no architecture file' { + Add-TestFile -Path (Join-Path $userLibPath 'archetype_definitions/root_custom.alz_archetype_override.yaml') -Content 'name: root_custom' + Add-TestFile -Path (Join-Path $bootstrapTargetPath 'v1.0.0/alz/local/terraform.tfstate') -Content '{ "version": 4, "resources": [' + + Invoke-TestBootstrap -InputConfig (Get-TestInputConfig) + + (Get-Content $bootstrapTfvarsPath -Raw | ConvertFrom-Json).terraform_architecture_file_path | Should -Be 'lib/architecture_definitions/alz_custom.alz_architecture_definition.yaml' + Test-Path $starterDefaultFile | Should -BeTrue + Should -Invoke -CommandName Write-ToConsoleLog -Times 0 -ParameterFilter { $IsWarning } + } + + It 'keeps the default when management_group_settings is set as a TF_VAR environment variable' { + Add-TestFile -Path (Join-Path $userLibPath 'architecture_definitions/contoso.alz_architecture_definition.yaml') -Content 'name: alz_custom' + $env:TF_VAR_management_group_settings = '{ "architecture_name": "contoso" }' + try { + Invoke-TestBootstrap -InputConfig (Get-TestInputConfig) + } finally { + Remove-Item -Path Env:\TF_VAR_management_group_settings + } + + (Get-Content $bootstrapTfvarsPath -Raw | ConvertFrom-Json).terraform_architecture_file_path | Should -Be 'lib/architecture_definitions/alz_custom.alz_architecture_definition.yaml' + Test-Path $starterDefaultFile | Should -BeTrue + } + } +} diff --git a/src/Tests/Unit/Private/Resolve-TerraformArchitectureFile.Tests.ps1 b/src/Tests/Unit/Private/Resolve-TerraformArchitectureFile.Tests.ps1 new file mode 100644 index 0000000..0b8f457 --- /dev/null +++ b/src/Tests/Unit/Private/Resolve-TerraformArchitectureFile.Tests.ps1 @@ -0,0 +1,264 @@ +#------------------------------------------------------------------------- +Set-Location -Path $PSScriptRoot +#------------------------------------------------------------------------- +$ModuleName = 'ALZ' +$PathToManifest = [System.IO.Path]::Combine('..', '..', '..', $ModuleName, "$ModuleName.psd1") +#------------------------------------------------------------------------- +if (Get-Module -Name $ModuleName -ErrorAction 'SilentlyContinue') { + Remove-Module -Name $ModuleName -Force +} +Import-Module $PathToManifest -Force +#------------------------------------------------------------------------- + +InModuleScope 'ALZ' { + Describe 'Resolve-TerraformArchitectureFile Function Tests' -Tag Unit { + BeforeAll { + Import-Module powershell-yaml -ErrorAction Stop + $defaultPath = 'lib/architecture_definitions/alz_custom.alz_architecture_definition.yaml' + + function Add-TestFile { + param([string] $Path, [string] $Content) + New-Item -Path (Split-Path $Path -Parent) -ItemType Directory -Force | Out-Null + Set-Content -Path $Path -Value $Content + } + + function Add-TestBootstrapState { + param([string] $Path, [string] $RootManagementGroupId) + Add-TestFile -Path $Path -Content "{ `"version`": 4, `"resources`": [ { `"mode`": `"managed`", `"type`": `"azapi_resource`", `"name`": `"intermediate_root_management_group`", `"instances`": [ { `"attributes`": { `"name`": `"$RootManagementGroupId`" } } ] } ] }" + } + } + + BeforeEach { + Mock -CommandName Write-ToConsoleLog -MockWith { } + $root = Join-Path $TestDrive ([guid]::NewGuid().ToString()) + $starterPath = Join-Path $root 'starter' + $userLibPath = Join-Path $root 'config/lib' + $starterDefaultFile = Join-Path $starterPath $defaultPath + $statePath = Join-Path $root 'bootstrap/terraform.tfstate' + Add-TestFile -Path $starterDefaultFile -Content "name: alz_custom`nmanagement_groups:`n - id: alz`n parent_id: null" + Add-TestFile -Path (Join-Path $starterPath 'variables.management.tf') -Content "variable `"management_group_settings`" {`n type = object({`n architecture_name = optional(string, `"alz_custom`")`n })`n}" + New-Item -Path $userLibPath -ItemType Directory -Force | Out-Null + } + + It 'makes no change when the user lib has no architecture file' { + Add-TestFile -Path (Join-Path $userLibPath 'archetype_definitions/root_custom.alz_archetype_override.yaml') -Content 'name: root_custom' + + $result = Resolve-TerraformArchitectureFile -starterModulePath $starterPath -starterAdditionalFiles @($userLibPath) -defaultArchitectureFilePath $defaultPath + + $result.ArchitectureFilePath | Should -BeNullOrEmpty + $result.DefaultFileToRemove | Should -BeNullOrEmpty + } + + It 'makes no change when a user file replaces the default path, even with another file for the same architecture' { + Add-TestFile -Path (Join-Path $userLibPath 'architecture_definitions/alz_custom.alz_architecture_definition.yaml') -Content 'name: foo' + Add-TestFile -Path (Join-Path $userLibPath 'architecture_definitions/contoso.alz_architecture_definition.yaml') -Content 'name: alz_custom' + + $result = Resolve-TerraformArchitectureFile -starterModulePath $starterPath -starterAdditionalFiles @($userLibPath) -defaultArchitectureFilePath $defaultPath + + $result.ArchitectureFilePath | Should -BeNullOrEmpty + $result.DefaultFileToRemove | Should -BeNullOrEmpty + } + + It 'selects a renamed yaml file with the default architecture name and removes the duplicate default' { + Add-TestFile -Path (Join-Path $userLibPath 'architecture_definitions/contoso.alz_architecture_definition.yaml') -Content 'name: alz_custom' + + $result = Resolve-TerraformArchitectureFile -starterModulePath $starterPath -starterAdditionalFiles @($userLibPath) -defaultArchitectureFilePath $defaultPath + + $result.ArchitectureFilePath | Should -Be 'lib/architecture_definitions/contoso.alz_architecture_definition.yaml' + $result.DefaultFileToRemove | Should -Be $starterDefaultFile + } + + It 'selects a renamed json file by the architecture name input and keeps the differently named default' { + Add-TestFile -Path (Join-Path $userLibPath 'architecture_definitions/contoso.alz_architecture_definition.json') -Content '{ "name": "contoso", "management_groups": [] }' + + $result = Resolve-TerraformArchitectureFile -starterModulePath $starterPath -starterAdditionalFiles @($userLibPath) -defaultArchitectureFilePath $defaultPath -architectureName 'contoso' + + $result.ArchitectureFilePath | Should -Be 'lib/architecture_definitions/contoso.alz_architecture_definition.json' + $result.DefaultFileToRemove | Should -BeNullOrEmpty + } + + It 'keeps the subfolder of a nested yml file' { + Add-TestFile -Path (Join-Path $userLibPath 'architecture_definitions/custom/contoso.alz_architecture_definition.yml') -Content 'name: alz_custom' + + $result = Resolve-TerraformArchitectureFile -starterModulePath $starterPath -starterAdditionalFiles @($userLibPath) -defaultArchitectureFilePath $defaultPath + + $result.ArchitectureFilePath | Should -Be 'lib/architecture_definitions/custom/contoso.alz_architecture_definition.yml' + } + + It 'throws when more than one user file declares the target architecture name' { + Add-TestFile -Path (Join-Path $userLibPath 'architecture_definitions/contoso.alz_architecture_definition.yaml') -Content 'name: alz_custom' + Add-TestFile -Path (Join-Path $userLibPath 'architecture_definitions/fabrikam.alz_architecture_definition.json') -Content '{ "name": "alz_custom" }' + + { Resolve-TerraformArchitectureFile -starterModulePath $starterPath -starterAdditionalFiles @($userLibPath) -defaultArchitectureFilePath $defaultPath } | + Should -Throw "*lib/architecture_definitions/contoso.alz_architecture_definition.yaml*lib/architecture_definitions/fabrikam.alz_architecture_definition.json*terraform_architecture_file_path*" + } + + It 'makes no change and warns when no user file declares the target architecture name' { + Add-TestFile -Path (Join-Path $userLibPath 'architecture_definitions/contoso.alz_architecture_definition.yaml') -Content 'name: contoso' + + $result = Resolve-TerraformArchitectureFile -starterModulePath $starterPath -starterAdditionalFiles @($userLibPath) -defaultArchitectureFilePath $defaultPath + + $result.ArchitectureFilePath | Should -BeNullOrEmpty + $result.DefaultFileToRemove | Should -BeNullOrEmpty + Should -Invoke -CommandName Write-ToConsoleLog -Times 1 -ParameterFilter { $IsWarning -and "$Messages" -like "*none has the architecture name 'alz_custom'*" } + } + + It 'selects the same file on a rerun after the default was removed, using the starter variable default' { + Remove-Item -Path $starterDefaultFile -Force + Add-TestFile -Path (Join-Path $userLibPath 'architecture_definitions/contoso.alz_architecture_definition.yaml') -Content 'name: alz_custom' + Add-TestFile -Path (Join-Path $userLibPath 'architecture_definitions/sandbox.alz_architecture_definition.yaml') -Content 'name: sandbox' + + $result = Resolve-TerraformArchitectureFile -starterModulePath $starterPath -starterAdditionalFiles @($userLibPath) -defaultArchitectureFilePath $defaultPath + + $result.ArchitectureFilePath | Should -Be 'lib/architecture_definitions/contoso.alz_architecture_definition.yaml' + $result.DefaultFileToRemove | Should -BeNullOrEmpty + } + + It 'makes no change without a starter architecture name when user files have different names' { + Remove-Item -Path $starterDefaultFile, (Join-Path $starterPath 'variables.management.tf') -Force + Add-TestFile -Path (Join-Path $userLibPath 'architecture_definitions/contoso.alz_architecture_definition.yaml') -Content 'name: contoso' + Add-TestFile -Path (Join-Path $userLibPath 'architecture_definitions/sandbox.alz_architecture_definition.yaml') -Content 'name: sandbox' + + $result = Resolve-TerraformArchitectureFile -starterModulePath $starterPath -starterAdditionalFiles @($userLibPath) -defaultArchitectureFilePath $defaultPath + + $result.ArchitectureFilePath | Should -BeNullOrEmpty + Should -Invoke -CommandName Write-ToConsoleLog -ParameterFilter { $IsWarning -and "$Messages" -like '*could not tell which one*' } + } + + It 'follows an architecture name the starter hardcodes over the input' { + Remove-Item -Path (Join-Path $starterPath 'variables.management.tf') -Force + Add-TestFile -Path (Join-Path $starterPath 'main.tf') -Content "module `"alz`" {`n architecture_name = `"alz_custom`"`n}" + Add-TestFile -Path (Join-Path $userLibPath 'architecture_definitions/contoso.alz_architecture_definition.yaml') -Content 'name: contoso' + + $result = Resolve-TerraformArchitectureFile -starterModulePath $starterPath -starterAdditionalFiles @($userLibPath) -defaultArchitectureFilePath $defaultPath -architectureName 'contoso' + + $result.ArchitectureFilePath | Should -BeNullOrEmpty + } + + It 'ignores architecture files outside the library folder' { + $file = Join-Path $root 'config/contoso.alz_architecture_definition.yaml' + Add-TestFile -Path $file -Content 'name: alz_custom' + $otherFolder = Join-Path $root 'config/templates' + Add-TestFile -Path (Join-Path $otherFolder 'fabrikam.alz_architecture_definition.yaml') -Content 'name: alz_custom' + + $result = Resolve-TerraformArchitectureFile -starterModulePath $starterPath -starterAdditionalFiles @($file, $otherFolder) -defaultArchitectureFilePath $defaultPath + + $result.ArchitectureFilePath | Should -BeNullOrEmpty + $result.DefaultFileToRemove | Should -BeNullOrEmpty + } + + It 'ignores extensions in another case than the bootstrap reads' { + Add-TestFile -Path (Join-Path $userLibPath 'architecture_definitions/contoso.alz_architecture_definition.YAML') -Content 'name: alz_custom' + + $result = Resolve-TerraformArchitectureFile -starterModulePath $starterPath -starterAdditionalFiles @($userLibPath) -defaultArchitectureFilePath $defaultPath + + $result.ArchitectureFilePath | Should -BeNullOrEmpty + } + + It 'treats a default path in another letter case as the default path' { + Add-TestFile -Path (Join-Path $userLibPath 'architecture_definitions/ALZ_custom.alz_architecture_definition.yaml') -Content 'name: alz_custom' + + $result = Resolve-TerraformArchitectureFile -starterModulePath $starterPath -starterAdditionalFiles @($userLibPath) -defaultArchitectureFilePath $defaultPath + + $result.ArchitectureFilePath | Should -BeNullOrEmpty + $result.DefaultFileToRemove | Should -BeNullOrEmpty + } + + It 'skips files that cannot be read or whose name is not text' { + Add-TestFile -Path (Join-Path $userLibPath 'architecture_definitions/broken.alz_architecture_definition.yaml') -Content "name: [alz_custom`n : :" + Add-TestFile -Path (Join-Path $userLibPath 'architecture_definitions/flag.alz_architecture_definition.yaml') -Content 'name: true' + Add-TestFile -Path (Join-Path $userLibPath 'architecture_definitions/contoso.alz_architecture_definition.yaml') -Content 'name: alz_custom' + + $result = Resolve-TerraformArchitectureFile -starterModulePath $starterPath -starterAdditionalFiles @($userLibPath) -defaultArchitectureFilePath $defaultPath + + $result.ArchitectureFilePath | Should -Be 'lib/architecture_definitions/contoso.alz_architecture_definition.yaml' + Should -Invoke -CommandName Write-ToConsoleLog -Times 2 -ParameterFilter { $IsWarning -and "$Messages" -like 'Skipping architecture definition file*' } + } + + It 'makes no change and warns when the powershell-yaml module is not available' { + Mock -CommandName Get-Command -ParameterFilter { $Name -eq 'ConvertFrom-Yaml' } -MockWith { $null } + Add-TestFile -Path (Join-Path $userLibPath 'architecture_definitions/contoso.alz_architecture_definition.yaml') -Content 'name: alz_custom' + + $result = Resolve-TerraformArchitectureFile -starterModulePath $starterPath -starterAdditionalFiles @($userLibPath) -defaultArchitectureFilePath $defaultPath + + $result.ArchitectureFilePath | Should -BeNullOrEmpty + Should -Invoke -CommandName Write-ToConsoleLog -Times 1 -ParameterFilter { $IsWarning -and "$Messages" -like '*powershell-yaml*' } + } + + It 'keeps the default when the existing bootstrap manages a different root management group' { + Add-TestFile -Path (Join-Path $userLibPath 'architecture_definitions/contoso.alz_architecture_definition.yaml') -Content "name: alz_custom`nmanagement_groups:`n - id: contoso`n parent_id: null" + Add-TestBootstrapState -Path $statePath -RootManagementGroupId 'alz' + + $result = Resolve-TerraformArchitectureFile -starterModulePath $starterPath -starterAdditionalFiles @($userLibPath) -defaultArchitectureFilePath $defaultPath -bootstrapStatePath $statePath + + $result.ArchitectureFilePath | Should -BeNullOrEmpty + $result.DefaultFileToRemove | Should -BeNullOrEmpty + Should -Invoke -CommandName Write-ToConsoleLog -Times 1 -ParameterFilter { $IsWarning -and "$Messages" -like "*manages the intermediate root management group 'alz'*'contoso'*" } + } + + It 'switches when the existing bootstrap manages the same root management group' { + Add-TestFile -Path (Join-Path $userLibPath 'architecture_definitions/contoso.alz_architecture_definition.yaml') -Content "name: alz_custom`nmanagement_groups:`n - id: alz`n parent_id: null" + Add-TestBootstrapState -Path $statePath -RootManagementGroupId 'alz' + + $result = Resolve-TerraformArchitectureFile -starterModulePath $starterPath -starterAdditionalFiles @($userLibPath) -defaultArchitectureFilePath $defaultPath -bootstrapStatePath $statePath + + $result.ArchitectureFilePath | Should -Be 'lib/architecture_definitions/contoso.alz_architecture_definition.yaml' + $result.DefaultFileToRemove | Should -Be $starterDefaultFile + } + + It 'stops when neither the selected file nor the default uses the root management group in the bootstrap state' { + Add-TestFile -Path (Join-Path $userLibPath 'architecture_definitions/contoso.alz_architecture_definition.yaml') -Content "name: alz_custom`nmanagement_groups:`n - id: fabrikam`n parent_id: null" + Add-TestBootstrapState -Path $statePath -RootManagementGroupId 'contoso' + + { Resolve-TerraformArchitectureFile -starterModulePath $starterPath -starterAdditionalFiles @($userLibPath) -defaultArchitectureFilePath $defaultPath -bootstrapStatePath $statePath } | + Should -Throw "*manages the intermediate root management group 'contoso'*'fabrikam'*terraform_architecture_file_path*" + } + + It 'ignores architecture names in comments and expressions in the starter code' { + Add-TestFile -Path (Join-Path $starterPath 'main.tf') -Content "/*`n architecture_name = `"old`"`n*/`nlocals {`n architecture_name = `"old`" == var.variant ? `"contoso`" : `"alz_custom`"`n}" + Add-TestFile -Path (Join-Path $userLibPath 'architecture_definitions/contoso.alz_architecture_definition.yaml') -Content 'name: alz_custom' + + $result = Resolve-TerraformArchitectureFile -starterModulePath $starterPath -starterAdditionalFiles @($userLibPath) -defaultArchitectureFilePath $defaultPath + + $result.ArchitectureFilePath | Should -Be 'lib/architecture_definitions/contoso.alz_architecture_definition.yaml' + } + + It 'counts a file once when two lib folders copy it to the same destination' { + $commonLibPath = Join-Path $root 'common/lib' + Add-TestFile -Path (Join-Path $commonLibPath 'architecture_definitions/contoso.alz_architecture_definition.yaml') -Content 'name: alz_custom' + Add-TestFile -Path (Join-Path $userLibPath 'architecture_definitions/contoso.alz_architecture_definition.yaml') -Content 'name: alz_custom' + + $result = Resolve-TerraformArchitectureFile -starterModulePath $starterPath -starterAdditionalFiles @($commonLibPath, $userLibPath) -defaultArchitectureFilePath $defaultPath + + $result.ArchitectureFilePath | Should -Be 'lib/architecture_definitions/contoso.alz_architecture_definition.yaml' + } + + It 'ignores architecture names inside heredoc strings in the starter code' { + Add-TestFile -Path (Join-Path $starterPath 'variables.docs.tf') -Content "variable `"example`" {`n description = <