diff --git a/.github/workflows/platform.convertToArmTemplate.tests.yml b/.github/workflows/platform.convertToArmTemplate.tests.yml index a29c9bb85a..abbce2b938 100644 --- a/.github/workflows/platform.convertToArmTemplate.tests.yml +++ b/.github/workflows/platform.convertToArmTemplate.tests.yml @@ -9,68 +9,42 @@ on: - 'utilities/tools/tests/ConvertTo-ARMTemplate**' jobs: - job_test_Default: - runs-on: windows-latest - name: 'Run test for default parameter' + job_test_convert: + runs-on: ubuntu-latest + name: 'Test with parameter' + strategy: + fail-fast: false + matrix: + tags: ['Default', 'ConvertChildren', 'Skip'] steps: - name: 'Checkout' uses: actions/checkout@v2 with: fetch-depth: 0 - token: '${{ secrets.PLATFORM_REPO_UPDATE_PAT }}' # Sets general GIT credentials up - name: 'Run Pester tests' shell: pwsh run: | - Invoke-Pester -Path ./utilities/tools/tests/ConvertTo-ARMTemplate-Default.Tests.ps1 -Verbose - job_test_ConvertChildren: - runs-on: windows-latest - name: 'Run test for -ConvertChildren parameter' - steps: - - name: 'Checkout' - uses: actions/checkout@v2 - with: - fetch-depth: 0 - token: '${{ secrets.PLATFORM_REPO_UPDATE_PAT }}' # Sets general GIT credentials up - - name: 'Run Pester tests' - shell: pwsh - run: | - Invoke-Pester -Path ./utilities/tools/tests/ConvertTo-ARMTemplate-ConvertChildren.Tests.ps1 -Verbose - job_test_SkipMetadataCleanup: - runs-on: windows-latest - name: 'Run test for -SkipMetadataCleanup parameter' - steps: - - name: 'Checkout' - uses: actions/checkout@v2 + Invoke-Pester -Configuration @{ + Run = @{ + Path = @( + Join-Path "$env:GITHUB_WORKSPACE" 'utilities' 'tools' 'tests' 'ConvertTo-ARMTemplate.Tests.ps1' + ) + } + Filter = @{ + Tag = '${{ matrix.tags }}' + } + TestResult = @{ + TestSuiteName = 'Conversion Tests' + OutputPath = Join-Path "$env:GITHUB_WORKSPACE" 'utilities' 'tools' 'tests' 'conversion-testResults-${{ matrix.tags }}.xml' + OutputFormat = 'JUnitXml' + Enabled = $true + } + Output = @{ + Verbosity = 'Detailed' + } + } + - name: Publish Test Results + uses: EnricoMi/publish-unit-test-result-action@v1 + if: always() with: - fetch-depth: 0 - token: '${{ secrets.PLATFORM_REPO_UPDATE_PAT }}' # Sets general GIT credentials up - - name: 'Run Pester tests' - shell: pwsh - run: | - Invoke-Pester -Path ./utilities/tools/tests/ConvertTo-ARMTemplate-SkipMetadataCleanup.Tests.ps1 -Verbose - job_test_SkipBicepCleanUp: - runs-on: windows-latest - name: 'Run test for -SkipBicepCleanUp parameter' - steps: - - name: 'Checkout' - uses: actions/checkout@v2 - with: - fetch-depth: 0 - token: '${{ secrets.PLATFORM_REPO_UPDATE_PAT }}' # Sets general GIT credentials up - - name: 'Run Pester tests' - shell: pwsh - run: | - Invoke-Pester -Path ./utilities/tools/tests/ConvertTo-ARMTemplate-SkipBicepCleanUp.Tests.ps1 -Verbose - job_test_SkipWorkflowUpdate: - runs-on: windows-latest - name: 'Run test for -SkipWorkflowUpdate parameter' - steps: - - name: 'Checkout' - uses: actions/checkout@v2 - with: - fetch-depth: 0 - token: '${{ secrets.PLATFORM_REPO_UPDATE_PAT }}' # Sets general GIT credentials up - - name: 'Run Pester tests' - shell: pwsh - run: | - Invoke-Pester -Path ./utilities/tools/tests/ConvertTo-ARMTemplate-SkipWorkflowUpdate.Tests.ps1 -Verbose + files: 'utilities/tools/tests/conversion-testResults-${{ matrix.tags }}.xml' diff --git a/utilities/tools/ConvertTo-ARMTemplate.ps1 b/utilities/tools/ConvertTo-ARMTemplate.ps1 index b2e937d93a..bfc5ee75a3 100644 --- a/utilities/tools/ConvertTo-ARMTemplate.ps1 +++ b/utilities/tools/ConvertTo-ARMTemplate.ps1 @@ -23,7 +23,7 @@ Optional. Skip Cleanup of Bicep metadata from json files .PARAMETER SkipBicepCleanUp Optional. Skip removal of bicep files and folders -.PARAMETER SkipWorkflowUpdate +.PARAMETER SkipPipelineUpdate Optional. Skip replacing .bicep with .json in workflow files .EXAMPLE @@ -32,7 +32,7 @@ Optional. Skip replacing .bicep with .json in workflow files Converts top level bicep modules to json-based ARM template, cleaning up all bicep files and folders and updating the workflow files to use the json files. .EXAMPLE -. .\utilities\tools\ConvertTo-ARMTemplate.ps1 -ConvertChildren -SkipMetadataCleanup -SkipBicepCleanUp -SkipWorkflowUpdate +. .\utilities\tools\ConvertTo-ARMTemplate.ps1 -ConvertChildren -SkipMetadataCleanup -SkipBicepCleanUp -SkipPipelineUpdate Only converts top level bicep modules to json based ARM template, keeping metadata in json, keeping all bicep files and folders, and not updating workflows. @@ -52,7 +52,7 @@ param ( [switch] $SkipBicepCleanUp, [Parameter(Mandatory = $false)] - [switch] $SkipWorkflowUpdate + [switch] $SkipPipelineUpdate ) $rootPath = Get-Item -Path $Path | Select-Object -ExpandProperty 'FullName' @@ -83,7 +83,7 @@ if ($PSCmdlet.ShouldProcess("[$($BicepFilesToConvert.count)] deploy.bicep file(s # parallelism is not supported on GitHub runners #$BicepFilesToConvert | ForEach-Object -ThrottleLimit $env:NUMBER_OF_PROCESSORS -Parallel { $BicepFilesToConvert | ForEach-Object { - Invoke-Expression -Command "az bicep build --file '$_'" + az bicep build --file $_ } } @@ -167,23 +167,38 @@ if (-not $SkipBicepCleanUp) { } #endregion -#region Update workflow files - Replace .bicep with .json in workflow files -if (-not $SkipWorkflowUpdate) { - Write-Verbose 'Update workflow files' +#region Update pipeline files - Replace .bicep with .json in workflow files +if (-not $SkipPipelineUpdate) { + Write-Verbose 'Update pipeline files' - $workflowFolderPath = Join-Path -Path $rootPath -ChildPath '.github\workflows' - $workflowFilesToUpdate = Get-ChildItem -Path $workflowFolderPath -Filter 'ms.*.yml' -File -Force - Write-Verbose "Update workflow files - Processing [$($workflowFilesToUpdate.count)] file(s)" - if ($PSCmdlet.ShouldProcess("[$($workflowFilesToUpdate.count)] ms.*.yml file(s) in path [$armFolderPath]", 'Set-Content')) { + # GitHub workflow files + $ghWorkflowFolderPath = Join-Path -Path $rootPath -ChildPath '.github\workflows' + $ghWorkflowFilesToUpdate = Get-ChildItem -Path $ghWorkflowFolderPath -Filter 'ms.*.yml' -File -Force + Write-Verbose ('Update workflow files - Processing [{0}] file(s)' -f $ghWorkflowFilesToUpdate.count) + if ($PSCmdlet.ShouldProcess(('[{0}] ms.*.yml file(s) in path [{1}]' -f $ghWorkflowFilesToUpdate.Count, $ghWorkflowFolderPath), 'Set-Content')) { # parallelism is not supported on GitHub runners - #$workflowFilesToUpdate | ForEach-Object -ThrottleLimit $env:NUMBER_OF_PROCESSORS -Parallel { - $workflowFilesToUpdate | ForEach-Object { + #$ghWorkflowFilesToUpdate | ForEach-Object -ThrottleLimit $env:NUMBER_OF_PROCESSORS -Parallel { + $ghWorkflowFilesToUpdate | ForEach-Object { $content = $_ | Get-Content - $content = $content.Replace('deploy.bicep', 'deploy.json') + $content = $content -replace 'templateFilePath:(.*).bicep', 'templateFilePath:$1.json' $_ | Set-Content -Value $content } } - Write-Verbose 'Update workflow files - Done' + # Azure DevOps Pipelines + $adoPipelineFolderPath = Join-Path -Path $rootPath -ChildPath '.azuredevops\modulePipelines' + $adoPipelineFilesToUpdate = Get-ChildItem -Path $adoPipelineFolderPath -Filter 'ms.*.yml' -File -Force + Write-Verbose ('Update Azure DevOps pipeline files - Processing [{0}] file(s)' -f $adoPipelineFilesToUpdate.count) + if ($PSCmdlet.ShouldProcess(('[{0}] ms.*.yml file(s) in path [{1}]' -f $adoPipelineFilesToUpdate.Count, $adoPipelineFolderPath), 'Set-Content')) { + # parallelism is not supported on GitHub runners + #$adoPipelineFilesToUpdate | ForEach-Object -ThrottleLimit $env:NUMBER_OF_PROCESSORS -Parallel { + $adoPipelineFilesToUpdate | ForEach-Object { + $content = $_ | Get-Content + $content = $content -replace 'templateFilePath:(.*).bicep', 'templateFilePath:$1.json' + $_ | Set-Content -Value $content + } + } + + Write-Verbose 'Update pipeline files - Done' } #endregion diff --git a/utilities/tools/tests/ConvertTo-ARMTemplate-ConvertChildren.Tests.ps1 b/utilities/tools/tests/ConvertTo-ARMTemplate-ConvertChildren.Tests.ps1 deleted file mode 100644 index 04c9a22146..0000000000 --- a/utilities/tools/tests/ConvertTo-ARMTemplate-ConvertChildren.Tests.ps1 +++ /dev/null @@ -1,80 +0,0 @@ -Describe 'Convert bicep files to ARM' { - BeforeAll { - $rootPath = Get-Location - $armFolderPath = Join-Path $rootPath 'arm' - $toolsPath = Join-Path $rootPath 'utilities' 'tools' - - $deployBicepFilesCount = (Get-ChildItem -Recurse $armFolderPath | Where-Object { $_.Name -match 'deploy.bicep' }).Count - $nestedBicepFilesCount = (Get-ChildItem -Recurse $armFolderPath | Where-Object { $_.Name -like 'nested_*bicep' }).Count - - Write-Verbose "$deployBicepFilesCount deploy.bicep file(s) found" - Write-Verbose "$nestedBicepFilesCount nested bicep file(s) found" - - $workflowFolderPath = Join-Path $rootPath '.github' 'workflows' - $workflowFiles = Get-ChildItem -Path $workflowFolderPath -Filter 'ms.*.yml' -File -Force - $workflowFilesToChange = 0 - - foreach ($workFlowFile in $workflowFiles) { - $content = Get-Content -Path $workFlowFile.FullName -Raw - - foreach ($line in $content) { - if ($line.Contains('deploy.bicep')) { - $workflowFilesToChange = $workflowFilesToChange + 1 - break - } - } - } - - Write-Verbose "$workflowFilesToChange workflow files need to change" - - Write-Verbose 'run ConvertTo-ARMTemplate script' - . "$toolsPath\ConvertTo-ARMTemplate.ps1" -Path $rootPath -ConvertChildren - } - - It 'all deploy.bicep files are converted to deploy.json' { - $deployJsonFilesCount = (Get-ChildItem -Recurse $armFolderPath | Where-Object { $_.FullName -match 'deploy.json' }).Count - Write-Verbose "$deployJsonFilesCount deploy.json file(s) found" - $deployJsonFilesCount | Should -Be $deployBicepFilesCount - } - - It 'all bicep files are removed' { - $bicepFilesCount = (Get-ChildItem -Recurse $armFolderPath | Where-Object { $_.FullName -match '.*.bicep' }).Count - Write-Verbose "$bicepFilesCount bicep file(s) found" - $bicepFilesCount | Should -Be 0 - } - - It 'all json files have metadata removed' { - $deployJsonFiles = (Get-ChildItem -Recurse $armFolderPath | Where-Object { $_.FullName -match 'deploy.json' }) - $metadataFound = $false - - foreach ($deployJsonFile in $deployJsonFiles) { - $content = Get-Content -Path $deployJsonFile.FullName -Raw - $TemplateObject = $content | ConvertFrom-Json - - if ([bool]($TemplateObject.PSobject.Properties.name -match 'metadata')) { - $metadataFound = $true; - break; - } - } - - $metadataFound | Should -Be $false - } - - It 'all workflow files are updated' { - $workflowFilesUpdated = 0 - - foreach ($workFlowFile in $workflowFiles) { - $content = Get-Content -Path $workFlowFile.FullName - - foreach ($line in $content) { - if ($line.Contains('deploy.json')) { - $workflowFilesUpdated = $workflowFilesUpdated + 1 - break - } - } - } - - Write-Verbose "$workflowFilesUpdated workflow file(s) updated" - $workflowFilesUpdated | Should -Be $workflowFilesToChange - } -} diff --git a/utilities/tools/tests/ConvertTo-ARMTemplate-Default.Tests.ps1 b/utilities/tools/tests/ConvertTo-ARMTemplate-Default.Tests.ps1 deleted file mode 100644 index f0b464efa7..0000000000 --- a/utilities/tools/tests/ConvertTo-ARMTemplate-Default.Tests.ps1 +++ /dev/null @@ -1,80 +0,0 @@ -Describe 'Convert bicep files to ARM' { - BeforeAll { - $rootPath = Get-Location - $armFolderPath = Join-Path $rootPath 'arm' - $toolsPath = Join-Path $rootPath 'utilities' 'tools' - - $deployParentBicepFilesCount = (Get-ChildItem -Recurse $armFolderPath -Depth 2 | Where-Object { $_.Name -match 'deploy.bicep' }).Count - $nestedBicepFilesCount = (Get-ChildItem -Recurse $armFolderPath | Where-Object { $_.Name -like 'nested_*bicep' }).Count - - Write-Host "$deployParentBicepFilesCount deploy.bicep file(s) found" - Write-Host "$nestedBicepFilesCount nested bicep file(s) found" - - $workflowFolderPath = Join-Path $rootPath '.github' 'workflows' - $workflowFiles = Get-ChildItem -Path $workflowFolderPath -Filter 'ms.*.yml' -File -Force - $workflowFilesToChange = 0 - - foreach ($workFlowFile in $workflowFiles) { - $content = Get-Content -Path $workFlowFile.FullName -Raw - - foreach ($line in $content) { - if ($line.Contains('deploy.bicep')) { - $workflowFilesToChange = $workflowFilesToChange + 1 - break - } - } - } - - Write-Host "$workflowFilesToChange workflow files need to change" - - Write-Host 'run ConvertTo-ARMTemplate script' - . "$toolsPath\ConvertTo-ARMTemplate.ps1" -Path $rootPath - } - - It 'all deploy.bicep files are converted to deploy.json' { - $deployJsonFilesCount = (Get-ChildItem -Recurse $armFolderPath | Where-Object { $_.FullName -match 'deploy.json' }).Count - Write-Host "$deployJsonFilesCount deploy.json file(s) found" - $deployJsonFilesCount | Should -Be $deployParentBicepFilesCount - } - - It 'all bicep files are removed' { - $bicepFilesCount = (Get-ChildItem -Recurse $armFolderPath | Where-Object { $_.FullName -match '.*.bicep' }).Count - Write-Host "$bicepFilesCount bicep file(s) found" - $bicepFilesCount | Should -Be 0 - } - - It 'all json files have metadata removed' { - $deployJsonFiles = (Get-ChildItem -Recurse $armFolderPath | Where-Object { $_.FullName -match 'deploy.json' }) - $metadataFound = $false - - foreach ($deployJsonFile in $deployJsonFiles) { - $content = Get-Content -Path $deployJsonFile.FullName -Raw - $TemplateObject = $content | ConvertFrom-Json - - if ([bool]($TemplateObject.PSobject.Properties.name -match 'metadata')) { - $metadataFound = $true; - break; - } - } - - $metadataFound | Should -Be $false - } - - It 'all workflow files are updated' { - $workflowFilesUpdated = 0 - - foreach ($workFlowFile in $workflowFiles) { - $content = Get-Content -Path $workFlowFile.FullName - - foreach ($line in $content) { - if ($line.Contains('deploy.json')) { - $workflowFilesUpdated = $workflowFilesUpdated + 1 - break - } - } - } - - Write-Host "$workflowFilesUpdated workflow file(s) updated" - $workflowFilesUpdated | Should -Be $workflowFilesToChange - } -} diff --git a/utilities/tools/tests/ConvertTo-ARMTemplate-SkipBicepCleanUp.Tests.ps1 b/utilities/tools/tests/ConvertTo-ARMTemplate-SkipBicepCleanUp.Tests.ps1 deleted file mode 100644 index e39998d9bd..0000000000 --- a/utilities/tools/tests/ConvertTo-ARMTemplate-SkipBicepCleanUp.Tests.ps1 +++ /dev/null @@ -1,81 +0,0 @@ -Describe 'Convert bicep files to ARM' { - BeforeAll { - $rootPath = Get-Location - $armFolderPath = Join-Path $rootPath 'arm' - $toolsPath = Join-Path $rootPath 'utilities' 'tools' - - $deployParentBicepFilesCount = (Get-ChildItem -Recurse $armFolderPath -Depth 2 | Where-Object { $_.Name -match 'deploy.bicep' }).Count - $deployBicepFilesCount = (Get-ChildItem -Recurse $armFolderPath | Where-Object { $_.Name -like '*.bicep' }).Count - $nestedBicepFilesCount = (Get-ChildItem -Recurse $armFolderPath | Where-Object { $_.Name -like 'nested_*bicep' }).Count - - Write-Verbose "$deployParentBicepFilesCount deploy.bicep file(s) found" - Write-Verbose "$nestedBicepFilesCount nested bicep file(s) found" - - $workflowFolderPath = Join-Path $rootPath '.github' 'workflows' - $workflowFiles = Get-ChildItem -Path $workflowFolderPath -Filter 'ms.*.yml' -File -Force - $workflowFilesToChange = 0 - - foreach ($workFlowFile in $workflowFiles) { - $content = Get-Content -Path $workFlowFile.FullName -Raw - - foreach ($line in $content) { - if ($line.Contains('deploy.bicep')) { - $workflowFilesToChange = $workflowFilesToChange + 1 - break - } - } - } - - Write-Verbose "$workflowFilesToChange workflow files need to change" - - Write-Verbose 'run ConvertTo-ARMTemplate script' - . "$toolsPath\ConvertTo-ARMTemplate.ps1" -Path $rootPath -SkipBicepCleanUp - } - - It 'all deploy.bicep files are converted to deploy.json' { - $deployJsonFilesCount = (Get-ChildItem -Recurse $armFolderPath | Where-Object { $_.FullName -match 'deploy.json' }).Count - Write-Verbose "$deployJsonFilesCount deploy.json file(s) found" - $deployJsonFilesCount | Should -Be $deployParentBicepFilesCount - } - - It 'all bicep files are still there' { - $bicepFilesCount = (Get-ChildItem -Recurse $armFolderPath | Where-Object { $_.FullName -match '.*.bicep' }).Count - Write-Verbose "$bicepFilesCount bicep file(s) found" - $bicepFilesCount | Should -Be $deployBicepFilesCount - } - - It 'all json files have metadata removed' { - $deployJsonFiles = (Get-ChildItem -Recurse $armFolderPath | Where-Object { $_.FullName -match 'deploy.json' }) - $metadataFound = $false - - foreach ($deployJsonFile in $deployJsonFiles) { - $content = Get-Content -Path $deployJsonFile.FullName -Raw - $TemplateObject = $content | ConvertFrom-Json - - if ([bool]($TemplateObject.PSobject.Properties.name -match 'metadata')) { - $metadataFound = $true; - break; - } - } - - $metadataFound | Should -Be $false - } - - It 'all workflow files are updated' { - $workflowFilesUpdated = 0 - - foreach ($workFlowFile in $workflowFiles) { - $content = Get-Content -Path $workFlowFile.FullName - - foreach ($line in $content) { - if ($line.Contains('deploy.json')) { - $workflowFilesUpdated = $workflowFilesUpdated + 1 - break - } - } - } - - Write-Verbose "$workflowFilesUpdated workflow file(s) updated" - $workflowFilesUpdated | Should -Be $workflowFilesToChange - } -} diff --git a/utilities/tools/tests/ConvertTo-ARMTemplate-SkipMetadataCleanup.Tests.ps1 b/utilities/tools/tests/ConvertTo-ARMTemplate-SkipMetadataCleanup.Tests.ps1 deleted file mode 100644 index 5950a9fb28..0000000000 --- a/utilities/tools/tests/ConvertTo-ARMTemplate-SkipMetadataCleanup.Tests.ps1 +++ /dev/null @@ -1,80 +0,0 @@ -Describe 'Convert bicep files to ARM' { - BeforeAll { - $rootPath = Get-Location - $armFolderPath = Join-Path $rootPath 'arm' - $toolsPath = Join-Path $rootPath 'utilities' 'tools' - - $deployParentBicepFilesCount = (Get-ChildItem -Recurse $armFolderPath -Depth 2 | Where-Object { $_.Name -match 'deploy.bicep' }).Count - $nestedBicepFilesCount = (Get-ChildItem -Recurse $armFolderPath | Where-Object { $_.Name -like 'nested_*bicep' }).Count - - Write-Verbose "$deployParentBicepFilesCount deploy.bicep file(s) found" - Write-Verbose "$nestedBicepFilesCount nested bicep file(s) found" - - $workflowFolderPath = Join-Path $rootPath '.github' 'workflows' - $workflowFiles = Get-ChildItem -Path $workflowFolderPath -Filter 'ms.*.yml' -File -Force - $workflowFilesToChange = 0 - - foreach ($workFlowFile in $workflowFiles) { - $content = Get-Content -Path $workFlowFile.FullName -Raw - - foreach ($line in $content) { - if ($line.Contains('deploy.bicep')) { - $workflowFilesToChange = $workflowFilesToChange + 1 - break - } - } - } - - Write-Verbose "$workflowFilesToChange workflow files need to change" - - Write-Verbose 'run ConvertTo-ARMTemplate script' - . "$toolsPath\ConvertTo-ARMTemplate.ps1" -Path $rootPath -SkipMetadataCleanup - } - - It 'all deploy.bicep files are converted to deploy.json' { - $deployJsonFilesCount = (Get-ChildItem -Recurse $armFolderPath | Where-Object { $_.FullName -match 'deploy.json' }).Count - Write-Verbose "$deployJsonFilesCount deploy.json file(s) found" - $deployJsonFilesCount | Should -Be $deployParentBicepFilesCount - } - - It 'all bicep files are removed' { - $bicepFilesCount = (Get-ChildItem -Recurse $armFolderPath | Where-Object { $_.FullName -match '.*.bicep' }).Count - Write-Verbose "$bicepFilesCount bicep file(s) found" - $bicepFilesCount | Should -Be 0 - } - - It 'all json files still have metadata' { - $deployJsonFiles = (Get-ChildItem -Recurse $armFolderPath | Where-Object { $_.FullName -match 'deploy.json' }) - $metadataFound = $false - - foreach ($deployJsonFile in $deployJsonFiles) { - $content = Get-Content -Path $deployJsonFile.FullName -Raw - $TemplateObject = $content | ConvertFrom-Json - - if ([bool]($TemplateObject.PSobject.Properties.name -match 'metadata')) { - $metadataFound = $true; - break; - } - } - - $metadataFound | Should -Be $true - } - - It 'all workflow files are updated' { - $workflowFilesUpdated = 0 - - foreach ($workFlowFile in $workflowFiles) { - $content = Get-Content -Path $workFlowFile.FullName - - foreach ($line in $content) { - if ($line.Contains('deploy.json')) { - $workflowFilesUpdated = $workflowFilesUpdated + 1 - break - } - } - } - - Write-Verbose "$workflowFilesUpdated workflow file(s) updated" - $workflowFilesUpdated | Should -Be $workflowFilesToChange - } -} diff --git a/utilities/tools/tests/ConvertTo-ARMTemplate-SkipWorkflowUpdate.Tests.ps1 b/utilities/tools/tests/ConvertTo-ARMTemplate-SkipWorkflowUpdate.Tests.ps1 deleted file mode 100644 index 2762bb07fe..0000000000 --- a/utilities/tools/tests/ConvertTo-ARMTemplate-SkipWorkflowUpdate.Tests.ps1 +++ /dev/null @@ -1,80 +0,0 @@ -Describe 'Convert bicep files to ARM' { - BeforeAll { - $rootPath = Get-Location - $armFolderPath = Join-Path $rootPath 'arm' - $toolsPath = Join-Path $rootPath 'utilities' 'tools' - - $deployParentBicepFilesCount = (Get-ChildItem -Recurse $armFolderPath -Depth 2 | Where-Object { $_.Name -match 'deploy.bicep' }).Count - $nestedBicepFilesCount = (Get-ChildItem -Recurse $armFolderPath | Where-Object { $_.Name -like 'nested_*bicep' }).Count - - Write-Verbose "$deployParentBicepFilesCount deploy.bicep file(s) found" - Write-Verbose "$nestedBicepFilesCount nested bicep file(s) found" - - $workflowFolderPath = Join-Path $rootPath '.github' 'workflows' - $workflowFiles = Get-ChildItem -Path $workflowFolderPath -Filter 'ms.*.yml' -File -Force - $workflowFilesToChange = 0 - - foreach ($workFlowFile in $workflowFiles) { - $content = Get-Content -Path $workFlowFile.FullName -Raw - - foreach ($line in $content) { - if ($line.Contains('deploy.bicep')) { - $workflowFilesToChange = $workflowFilesToChange + 1 - break - } - } - } - - Write-Verbose "$workflowFilesToChange workflow files need to change" - - Write-Verbose 'run ConvertTo-ARMTemplate script' - . "$toolsPath\ConvertTo-ARMTemplate.ps1" -Path $rootPath -SkipWorkflowUpdate - } - - It 'all deploy.bicep files are converted to deploy.json' { - $deployJsonFilesCount = (Get-ChildItem -Recurse $armFolderPath | Where-Object { $_.FullName -match 'deploy.json' }).Count - Write-Verbose "$deployJsonFilesCount deploy.json file(s) found" - $deployJsonFilesCount | Should -Be $deployParentBicepFilesCount - } - - It 'all bicep files are removed' { - $bicepFilesCount = (Get-ChildItem -Recurse $armFolderPath | Where-Object { $_.FullName -match '.*.bicep' }).Count - Write-Verbose "$bicepFilesCount bicep file(s) found" - $bicepFilesCount | Should -Be 0 - } - - It 'all json files have metadata removed' { - $deployJsonFiles = (Get-ChildItem -Recurse $armFolderPath | Where-Object { $_.FullName -match 'deploy.json' }) - $metadataFound = $false - - foreach ($deployJsonFile in $deployJsonFiles) { - $content = Get-Content -Path $deployJsonFile.FullName -Raw - $TemplateObject = $content | ConvertFrom-Json - - if ([bool]($TemplateObject.PSobject.Properties.name -match 'metadata')) { - $metadataFound = $true; - break; - } - } - - $metadataFound | Should -Be $false - } - - It 'all workflow files are not' { - $workflowFilesUpdated = 0 - - foreach ($workFlowFile in $workflowFiles) { - $content = Get-Content -Path $workFlowFile.FullName - - foreach ($line in $content) { - if ($line.Contains('deploy.json')) { - $workflowFilesUpdated = $workflowFilesUpdated + 1 - break - } - } - } - - Write-Verbose "$workflowFilesUpdated workflow file(s) updated" - $workflowFilesUpdated | Should -Be 0 - } -} diff --git a/utilities/tools/tests/ConvertTo-ARMTemplate.Tests.ps1 b/utilities/tools/tests/ConvertTo-ARMTemplate.Tests.ps1 new file mode 100644 index 0000000000..422bba1b51 --- /dev/null +++ b/utilities/tools/tests/ConvertTo-ARMTemplate.Tests.ps1 @@ -0,0 +1,225 @@ +# TEST FILE for script [ConvertTo-ARMTemplate.ps1] + +# Variables are used but not detected as they are not inside the same block +[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')] +param () + +BeforeAll { + # Define paths + $rootPath = (Get-Item $PSScriptRoot).Parent.Parent.Parent.FullName + $armFolderPath = Join-Path $rootPath 'arm' + $toolsPath = Join-Path $rootPath 'utilities' 'tools' + + # Collect original files + $bicepFilesCount = (Get-ChildItem -Recurse $armFolderPath | Where-Object { $_.Name -like '*.bicep' }).Count + $nestedBicepFilesCount = (Get-ChildItem -Recurse $armFolderPath | Where-Object { $_.Name -like 'nested_*bicep' }).Count + $deployBicepFilesCount = (Get-ChildItem -Recurse $armFolderPath | Where-Object { $_.Name -match 'deploy.bicep' }).Count + $deployParentBicepFilesCount = (Get-ChildItem -Recurse $armFolderPath -Depth 2 | Where-Object { $_.Name -match 'deploy.bicep' }).Count + + # GitHub Workflows + $moduleWorkflowFiles = Get-ChildItem -Path (Join-Path $rootPath '.github' 'workflows') -Filter 'ms.*.yml' -File + $originalModuleWorkflowWithBicep = 0 + foreach ($workFlowFile in $moduleWorkflowFiles) { + foreach ($line in (Get-Content -Path $workFlowFile.FullName)) { + if ($line -like '*.bicep*') { + $originalModuleWorkflowWithBicep += 1 + break + } + } + } + + # Azure DevOps pipelines + $adoModulePipelineFiles = Get-ChildItem -Path (Join-Path $rootPath '.azuredevops' 'modulePipelines') -Filter 'ms.*.yml' -File + $originalModulePipelinesWithBicep = 0 + foreach ($adoModulePipelineFile in $adoModulePipelineFiles) { + foreach ($line in (Get-Content -Path $adoModulePipelineFile.FullName)) { + if ($line -like '*.bicep*') { + $originalModulePipelinesWithBicep += 1 + break + } + } + } +} + +Describe 'Test default behavior' -Tag 'Default' { + + BeforeAll { + . (Join-Path $toolsPath 'ConvertTo-ARMTemplate.ps1') -Path $rootPath + } + + It 'All top-level deploy.bicep files are converted to deploy.json' { + $deployJsonFilesCount = (Get-ChildItem -Recurse $armFolderPath | Where-Object { $_.FullName -match 'deploy.json' }).Count + $deployJsonFilesCount | Should -Be $deployParentBicepFilesCount + } + + It 'All bicep files are removed' { + $bicepFilesCount = (Get-ChildItem -Recurse $armFolderPath | Where-Object { $_.FullName -match '.*.bicep' }).Count + $bicepFilesCount | Should -Be 0 + } + + It 'All json files have metadata removed' { + $deployJsonFiles = (Get-ChildItem -Recurse $armFolderPath | Where-Object { $_.FullName -match 'deploy.json' }) + $metadataFound = $false + + foreach ($deployJsonFile in $deployJsonFiles) { + $TemplateObject = Get-Content -Path $deployJsonFile.FullName -Raw | ConvertFrom-Json + + if ([bool]($TemplateObject.PSobject.Properties.name -match 'metadata')) { + $metadataFound = $true; + break; + } + } + + $metadataFound | Should -Be $false + } + + It 'All GitHub workflow files are updated' { + $moduleWorkflowFilesUpdated = 0 + + foreach ($workFlowFile in $moduleWorkflowFiles) { + foreach ($line in (Get-Content -Path $workFlowFile.FullName)) { + if ($line -like '*templateFilePath:*.json*') { + $moduleWorkflowFilesUpdated += 1 + break + } + } + } + $moduleWorkflowFilesUpdated | Should -Be $originalModuleWorkflowWithBicep + } + + It 'All Azure DevOps pipeline files are changed' { + $modulePipelineFileUpdated = 0 + + foreach ($pipelineFile in $adoModulePipelineFiles) { + foreach ($line in (Get-Content -Path $pipelineFile.FullName)) { + if ($line -like '*templateFilePath:*.json*') { + $modulePipelineFileUpdated += 1 + break + } + } + } + $modulePipelineFileUpdated | Should -Be $originalModulePipelinesWithBicep + } +} + +Describe 'Test flag to including children' -Tag 'ConvertChildren' { + + BeforeAll { + . (Join-Path $toolsPath 'ConvertTo-ARMTemplate.ps1') -Path $rootPath -ConvertChildren + } + + It 'All deploy.bicep files are converted to deploy.json' { + $deployJsonFilesCount = (Get-ChildItem -Recurse $armFolderPath | Where-Object { $_.FullName -match 'deploy.json' }).Count + $deployJsonFilesCount | Should -Be $deployBicepFilesCount + } + + It 'All bicep files are removed' { + $bicepFilesCount = (Get-ChildItem -Recurse $armFolderPath | Where-Object { $_.FullName -match '.*.bicep' }).Count + $bicepFilesCount | Should -Be 0 + } + + It 'All json files have metadata removed' { + $deployJsonFiles = (Get-ChildItem -Recurse $armFolderPath | Where-Object { $_.FullName -match 'deploy.json' }) + $metadataFound = $false + + foreach ($deployJsonFile in $deployJsonFiles) { + $TemplateObject = Get-Content -Path $deployJsonFile.FullName -Raw | ConvertFrom-Json + + if ([bool]($TemplateObject.PSobject.Properties.name -match 'metadata')) { + $metadataFound = $true; + break; + } + } + + $metadataFound | Should -Be $false + } + + It 'All GitHub workflow files are updated' { + $moduleWorkflowFilesUpdated = 0 + + foreach ($workFlowFile in $moduleWorkflowFiles) { + foreach ($line in (Get-Content -Path $workFlowFile.FullName)) { + if ($line -like '*templateFilePath:*.json*') { + $moduleWorkflowFilesUpdated += 1 + break + } + } + } + $moduleWorkflowFilesUpdated | Should -Be $originalModuleWorkflowWithBicep + } + + It 'All Azure DevOps pipeline files are changed' { + $modulePipelineFileUpdated = 0 + + foreach ($pipelineFile in $adoModulePipelineFiles) { + foreach ($line in (Get-Content -Path $pipelineFile.FullName)) { + if ($line -like '*templateFilePath:*.json*') { + $modulePipelineFileUpdated += 1 + break + } + } + } + $modulePipelineFileUpdated | Should -Be $originalModulePipelinesWithBicep + } +} + +Describe 'Test flags that skip logic' -Tag 'Skip' { + + BeforeAll { + . (Join-Path $toolsPath 'ConvertTo-ARMTemplate.ps1') -Path $rootPath -SkipBicepCleanUp -SkipMetadataCleanup -SkipPipelineUpdate + } + + It 'All deploy.bicep files are converted to deploy.json' { + $deployJsonFilesCount = (Get-ChildItem -Recurse $armFolderPath | Where-Object { $_.FullName -match 'deploy.json' }).Count + $deployJsonFilesCount | Should -Be $deployParentBicepFilesCount + } + + It 'All bicep files are still there' { + $bicepFilesCount = (Get-ChildItem -Recurse $armFolderPath | Where-Object { $_.FullName -match '.*.bicep' }).Count + $bicepFilesCount | Should -Be $bicepFilesCount + } + + It 'All json files still have metadata' { + $deployJsonFiles = (Get-ChildItem -Recurse $armFolderPath | Where-Object { $_.FullName -match 'deploy.json' }) + $metadataFound = $false + + foreach ($deployJsonFile in $deployJsonFiles) { + $content = Get-Content -Path $deployJsonFile.FullName -Raw + $TemplateObject = $content | ConvertFrom-Json + + if ([bool]($TemplateObject.PSobject.Properties.name -match 'metadata')) { + $metadataFound = $true; + break; + } + } + $metadataFound | Should -Be $true + } + + It 'No GH workflow files are changed' { + $moduleWorkflowFilesUpdated = 0 + + foreach ($workFlowFile in $moduleWorkflowFiles) { + foreach ($line in (Get-Content -Path $workFlowFile.FullName)) { + if ($line -like '*templateFilePath:*.json*') { + $moduleWorkflowFilesUpdated += 1 + break + } + } + } + $moduleWorkflowFilesUpdated | Should -Be 0 + } + + It 'No Azure DevOps pipeline files are changed' { + $modulePipelineFileUpdated = 0 + + foreach ($pipelineFile in $adoModulePipelineFiles) { + foreach ($line in (Get-Content -Path $pipelineFile.FullName)) { + if ($line -like '*templateFilePath:*.json*') { + $modulePipelineFileUpdated += 1 + break + } + } + } + $modulePipelineFileUpdated | Should -Be 0 + } +}