From 9b4cbd5ba375bb2a0ea337ca1f0a7403e048ae87 Mon Sep 17 00:00:00 2001 From: Jared Holgate Date: Wed, 23 Oct 2024 15:47:20 +0100 Subject: [PATCH] Update e2e tests --- .github/tests/scripts/generate-matrix.ps1 | 141 ++++++++++++++++++++++ .github/workflows/end-to-end-test.yml | 130 ++++++++------------ 2 files changed, 189 insertions(+), 82 deletions(-) create mode 100644 .github/tests/scripts/generate-matrix.ps1 diff --git a/.github/tests/scripts/generate-matrix.ps1 b/.github/tests/scripts/generate-matrix.ps1 new file mode 100644 index 0000000..7263080 --- /dev/null +++ b/.github/tests/scripts/generate-matrix.ps1 @@ -0,0 +1,141 @@ +$combinations = @{ + azuredevops_bicep = @{ + versionControlSystem = @("azuredevops") + agentType = @("public", "private", "none") + operatingSystem = @("ubuntu") + terraformVersion = @("latest") + infrastructureAsCode = @("bicep") + regions = @("multi") + staterModule = @("test") + } + azuredevops_terraform = @{ + versionControlSystem = @("azuredevops") + agentType = @("public", "private", "none") + operatingSystem = @("ubuntu") + terraformVersion = @("latest") + infrastructureAsCode = @("terraform") + regions = @("multi") + staterModule = @("test_nested") + } + github_bicep = @{ + versionControlSystem = @("github") + agentType = @("public", "private", "none") + operatingSystem = @("ubuntu") + terraformVersion = @("latest") + infrastructureAsCode = @("bicep") + regions = @("multi") + staterModule = @("test") + } + github_terraform = @{ + versionControlSystem = @("github") + agentType = @("public", "private", "none") + operatingSystem = @("ubuntu") + terraformVersion = @("latest") + infrastructureAsCode = @("terraform") + regions = @("multi") + staterModule = @("test_nested") + } + local_cross_os_tests = @{ + versionControlSystem = @("local") + agentType = @("none") + operatingSystem = @("ubuntu", "windows", "macos") + terraformVersion = @("latest", "1.5.0") + infrastructureAsCode = @("terraform") + regions = @("multi") + staterModule = @("test") + } + local_single_region_tests = @{ + versionControlSystem = @("local") + agentType = @("none") + operatingSystem = @("ubuntu") + terraformVersion = @("latest") + infrastructureAsCode = @("terraform") + regions = @("single") + staterModule = @("test") + } + local_starter_module_terraform_tests = @{ + versionControlSystem = @("local") + agentType = @("none") + operatingSystem = @("ubuntu") + terraformVersion = @("latest") + infrastructureAsCode = @("terraform") + regions = @("single") + staterModule = @("complete", "complete_multi_region", "sovereign_landing_zone", "basic", "hubnetworking") + } + local_starter_module_bicep_tests = @{ + versionControlSystem = @("local") + agentType = @("none") + operatingSystem = @("ubuntu") + terraformVersion = @("latest") + infrastructureAsCode = @("terraform") + regions = @("single") + staterModule = @("complete") + } +} + +function Get-Hash([string]$textToHash) { + $hasher = new-object System.Security.Cryptography.MD5CryptoServiceProvider + $toHash = [System.Text.Encoding]::UTF8.GetBytes($textToHash) + $hashByteArray = $hasher.ComputeHash($toHash) + foreach($byte in $hashByteArray) + { + $result += "{0:X2}" -f $byte + } + return $result; +} + +function Get-MatrixRecursively { + param( + $calculatedCombinations = @(), + [hashtable]$indexes = @{}, + [hashtable]$definition + ) + + if($indexes.Count -eq 0) { + foreach($key in $definition.Keys) { + $indexes[$key] = @{ + current = 0 + max = $definition[$key].Length - 1 + } + } + } + + $combination = @{} + + $name = "" + + foreach($key in $indexes.Keys) { + $combinationValue = $definition[$key][$indexes[$key].current] + $combination[$key] = $combinationValue + $name = "$name-$combinationValue" + } + + $combination.Name = $name.Trim("-") + $combination.Hash = Get-Hash $name + $combination.ShortName = $combination.Hash.Substring(0,5) + + $calculatedCombinations += $combination + + $hasMore = $false + foreach($key in $indexes.Keys) { + if($indexes[$key].current -lt $indexes[$key].max) { + $indexes[$key].current++ + $hasMore = $true + break + } + } + + if($hasMore) { + $calculatedCombinations = Get-MatrixRecursively -calculatedCombinations $calculatedCombinations -indexes $indexes -definition $definition + } + + return $calculatedCombinations +} + +$finalMatrix = @() + +foreach($key in $combinations.Keys) { + $finalMatrix += Get-MatrixRecursively -definition $combinations[$key] +} + +return $finalMatrix \ No newline at end of file diff --git a/.github/workflows/end-to-end-test.yml b/.github/workflows/end-to-end-test.yml index 6814f14..e5356eb 100644 --- a/.github/workflows/end-to-end-test.yml +++ b/.github/workflows/end-to-end-test.yml @@ -44,51 +44,35 @@ env: ALZ_BICEP_BRANCH: ${{ inputs.alz_bicep_branch != '' && inputs.alz_bicep_branch || 'main' }} ALZ_TERRAFORM_BRANCH: ${{ inputs.alz_terraform_branch != '' && inputs.alz_terraform_branch || 'main' }} ALZ_ON_DEMAND_FOLDER_RELEASE_TAG: ${{ inputs.alz_on_demand_folder_release_tag != '' && inputs.alz_on_demand_folder_release_tag || 'latest' }} + jobs: + define-matrix: + name: Define Matrix + runs-on: ubuntu-latest + + outputs: + colors: ${{ steps.matrix.outputs.matrix }} + + steps: + - name: Generate Matrix + id: matrix + run: | + $matrix = ${{ env.BOOTSTRAP_MODULE_FOLDER }}/.github/tests/scripts/generate-matrix.ps1 + $matrixJson = ConvertFrom-Json $matrix -Depth 10 + Write-Output "matrix=$matrixJson" >> $env:GITHUB_OUTPUT + shell: pwsh + e2e-test: - name: "${{ matrix.vcs }}-${{ matrix.iac }}-${{ matrix.ag }}-${{ matrix.os }}-${{ matrix.tf }}-${{ matrix.rg }}" + needs: define-matrix + name: "${{ matrix.name }} (${{ matrix.ShortName }})" environment: ${{ github.event_name == 'schedule' && 'CSUTFAUTO' || 'CSUTF' }} if: "${{ github.repository == 'Azure/accelerator-bootstrap-modules' && (contains(github.event.pull_request.labels.*.name, 'PR: Safe to test 🧪') || github.event_name == 'workflow_dispatch' || github.event_name == 'schedule') }}" strategy: fail-fast: false - matrix: - vcs: ['github', 'azuredevops', 'local'] # Version Control System - iac: ['terraform', 'bicep'] - ag: ['public', 'private', 'none'] # Self Hosted Agents - os: ['ubuntu', 'windows', 'macos'] # Operating System - tf: ['latest', '1.5.0'] # Terraform Version - rg: ['single', 'multi'] # Regions - exclude: - - iac: bicep - tf: 1.5.0 - - vcs: local - ag: public - - vcs: local - ag: private - - vcs: azuredevops - tf: 1.5.0 - - vcs: github - tf: 1.5.0 - - os: windows - vcs: azuredevops - - os: macos - vcs: azuredevops - - os: windows - vcs: github - - os: macos - vcs: github - - rg: single - vcs: github - - rg: single - vcs: azuredevops - - rg: single - tf: 1.5.0 - - rg: single - os: windows - - rg: single - os: macos - - runs-on: ${{ matrix.os }}-latest + matrix: + include: ${{ fromJSON(needs.define-matrix.outputs.matrix) }} + + runs-on: ${{ matrix.operatingSystem }}-latest steps: - name: Show env run: env | sort @@ -107,7 +91,7 @@ jobs: - name: Checkout Starter Modules for Bicep uses: actions/checkout@v4 - if: ${{ matrix.iac == 'bicep' }} + if: ${{ matrix.infrastructureAsCode == 'bicep' }} with: repository: ${{ env.BICEP_STARTER_MODULE_REPOSITORY }} ref: ${{ env.ALZ_BICEP_BRANCH }} @@ -115,7 +99,7 @@ jobs: - name: Checkout Starter Modules for Terraform uses: actions/checkout@v4 - if: ${{ matrix.iac == 'terraform' }} + if: ${{ matrix.infrastructureAsCode == 'terraform' }} with: repository: ${{ env.TERRAFORM_STARTER_MODULE_REPOSITORY }} ref: ${{ env.ALZ_TERRAFORM_BRANCH }} @@ -124,20 +108,22 @@ jobs: - name: Setup Terraform uses: hashicorp/setup-terraform@v3 with: - terraform_version: ${{ matrix.tf }} + terraform_version: ${{ matrix.terraformVersion }} terraform_wrapper: false - if: ${{ matrix.tf != 'latest' }} + if: ${{ matrix.terraformVersion != 'latest' }} - name: Setup ALZ Module Inputs run: | # Get Inputs - $infrastructureAsCode = "${{ matrix.iac }}" - $versionControlSystem = "${{ matrix.vcs }}" - $operatingSystem = "${{ matrix.os }}" - $terraformVersion = "${{ matrix.tf }}" - $selfHostedAgents = "${{ matrix.ag }}" - $regions = "${{ matrix.rg }}" + $infrastructureAsCode = "${{ matrix.infrastructureAsCode }}" + $versionControlSystem = "${{ matrix.versionControlSystem }}" + $operatingSystem = "${{ matrix.operatingSystem }}" + $terraformVersion = "${{ matrix.terraformVersion }}" + $selfHostedAgents = "${{ matrix.agentType }}" + $regions = "${{ matrix.regions }}" + $starterModule = "${{ matrix.starterModule }}" + $shortName = "${{ matrix.ShortName }}" $locations = @( "uksouth", @@ -170,36 +156,16 @@ jobs: if($selfHostedAgents -eq "private") { $enablePrivateNetworking = "true" } - - # Get Unique ID - $infrastructureAsCodeShort = $infrastructureAsCode.Substring(0, 1) - - $versionControlSystemShort = $versionControlSystem.Substring(0, 1) - if($versionControlSystem.Contains("-")) { - $versionControlSystemSplit = $versionControlSystem.Split("-") - $versionControlSystemShort = $versionControlSystemSplit[0].Substring(0, 1) + $versionControlSystemSplit[1].Substring(0, 1) - } - $operatingSystemShort = $operatingSystem.Substring(0, 1) - $terraformVersionShort = if ($terraformVersion -eq "latest") { "l" } else { "m" } - $selfhostedAgentsShort = "n" - if($selfHostedAgents -eq "public") { - $selfhostedAgentsShort = "p" - } - if($selfHostedAgents -eq "private") { - $selfhostedAgentsShort = "r" - } - + $localDeployAzureResources = if($terraformVersion -eq "latest") { "true" } else { "false" } $runNumber = "${{ github.run_number }}" - $runNumberEven = $runNumber % 2 -eq 0 - $starterModule = $runNumberEven ? "test" : "test_nested" - - Write-Host "Infrastructure As Code: $infrastructureAsCode ($infrastructureAsCodeShort)" - Write-Host "Version Control System: $versionControlSystem ($versionControlSystemShort)" - Write-Host "Operating System: $operatingSystem ($operatingSystemShort)" - Write-Host "Terraform Version: $terraformVersion ($terraformVersionShort)" - Write-Host "Self Hosted Agents: $selfHostedAgents ($selfhostedAgentsShort)" + + Write-Host "Infrastructure As Code: $infrastructureAsCode" + Write-Host "Version Control System: $versionControlSystem" + Write-Host "Operating System: $operatingSystem" + Write-Host "Terraform Version: $terraformVersion" + Write-Host "Self Hosted Agents: $selfHostedAgents" Write-Host "Local Deploy Azure Resources: $localDeployAzureResources" Write-Host "Run Number: $runNumber" Write-Host "Starter Module: $starterModule" @@ -207,7 +173,7 @@ jobs: Write-Host "Location: $location" Write-Host "Location 2: $location2" - $uniqueId = "$versionControlSystemShort$infrastructureAsCodeShort$selfhostedAgentsShort$operatingSystemShort$terraformVersionShort$runNumber".ToLower() + $uniqueId = "$shortName$runNumber".ToLower() echo "UNIQUE_ID=$uniqueId" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf8 -Append Write-Host "Unique ID: $uniqueId" @@ -291,8 +257,8 @@ jobs: Write-Host "Runner IP Address: $myIp" # Get Inputs - $versionControlSystem = "${{ matrix.vcs }}" - $infrastructureAsCode = "${{ matrix.iac }}" + $versionControlSystem = "${{ matrix.versionControlSystem }}" + $infrastructureAsCode = "${{ matrix.infrastructureAsCode }}" # Install the Module Write-Host "Installing the Accelerator PowerShell Module" @@ -354,10 +320,10 @@ jobs: - name: Run Pipelines or Actions run: | - $infrastructureAsCode = "${{ matrix.iac }}" + $infrastructureAsCode = "${{ matrix.infrastructureAsCode }}" # Get Inputs - $versionControlSystem = "${{ matrix.vcs }}" + $versionControlSystem = "${{ matrix.versionControlSystem }}" $versionControlSystemOrganisationName = "${{ vars.VCS_ORGANIZATION }}" $uniqueId = $ENV:UNIQUE_ID @@ -420,7 +386,7 @@ jobs: Write-Host "Runner IP Address: $myIp" # Get Inputs - $versionControlSystem = "${{ matrix.vcs }}" + $versionControlSystem = "${{ matrix.versionControlSystem }}" Write-Host "Installing the Accelerator PowerShell Module" ${{ env.POWERSHELL_MODULE_FOLDER }}/actions_bootstrap_for_e2e_tests.ps1 | Out-String | Write-Verbose