Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added first version of a CI-focused testing pipeline #2459

Merged
merged 66 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
b144c0a
Update to latest
AlexanderSehr Mar 23, 2024
99f88df
Update to latest
AlexanderSehr Mar 23, 2024
754793b
Merge branch 'main' of https://github.com/Azure/bicep-registry-modules
AlexanderSehr Mar 25, 2024
8b1daef
Merge branch 'main' of https://github.com/Azure/bicep-registry-modules
AlexanderSehr Mar 30, 2024
9f35870
Merge branch 'main' of https://github.com/Azure/bicep-registry-modules
AlexanderSehr Apr 6, 2024
4a87461
Merge branch 'main' of https://github.com/Azure/bicep-registry-modules
AlexanderSehr Apr 10, 2024
3d4ce5d
Merge branch 'main' of https://github.com/Azure/bicep-registry-modules
AlexanderSehr Apr 10, 2024
246477c
Merge branch 'main' of https://github.com/Azure/bicep-registry-modules
AlexanderSehr Apr 10, 2024
068f7d3
Merge branch 'main' of https://github.com/Azure/bicep-registry-modules
AlexanderSehr Apr 12, 2024
d668fb9
Merge branch 'main' of https://github.com/Azure/bicep-registry-modules
AlexanderSehr Apr 15, 2024
9d7bc95
Merge branch 'main' of https://github.com/Azure/bicep-registry-modules
AlexanderSehr Apr 18, 2024
90ae3c5
Merge branch 'main' of https://github.com/Azure/bicep-registry-modules
AlexanderSehr Apr 18, 2024
93aa434
Merge branch 'main' of https://github.com/Azure/bicep-registry-modules
AlexanderSehr Apr 19, 2024
05c9378
Merge branch 'main' of https://github.com/Azure/bicep-registry-modules
AlexanderSehr Apr 20, 2024
d6503d6
Merge branches 'main' and 'main' of https://github.com/Azure/bicep-re…
AlexanderSehr Apr 29, 2024
43125dd
Merge branch 'main' of https://github.com/Azure/bicep-registry-modules
AlexanderSehr Apr 29, 2024
3264515
Merge branch 'main' of https://github.com/Azure/bicep-registry-modules
AlexanderSehr Apr 30, 2024
e175c3e
Merge branch 'main' of https://github.com/Azure/bicep-registry-modules
AlexanderSehr Apr 30, 2024
83c8dfb
Merge branch 'main' of https://github.com/Azure/bicep-registry-modules
AlexanderSehr Apr 30, 2024
b87c025
Merge branch 'main' of https://github.com/Azure/bicep-registry-modules
AlexanderSehr May 2, 2024
803bf65
Merge branch 'main' of https://github.com/Azure/bicep-registry-modules
AlexanderSehr May 3, 2024
58ba613
Merge branch 'main' of https://github.com/Azure/bicep-registry-modules
AlexanderSehr May 6, 2024
1a055b2
Merge branches 'main' and 'main' of https://github.com/Azure/bicep-re…
AlexanderSehr May 6, 2024
4dd07d6
Merge branch 'main' of https://github.com/Azure/bicep-registry-modules
AlexanderSehr May 7, 2024
9d3e6ce
Merge branch 'main' of https://github.com/Azure/bicep-registry-modules
AlexanderSehr May 7, 2024
f1012df
Validated change with test
AlexanderSehr May 8, 2024
b5a253a
Merge branch 'main' of https://github.com/Azure/bicep-registry-modules
AlexanderSehr May 15, 2024
a755a6e
Merge branch 'main' of https://github.com/Azure/bicep-registry-modules
AlexanderSehr May 22, 2024
6bd61c7
Merge branch 'main' of https://github.com/Azure/bicep-registry-modules
AlexanderSehr May 22, 2024
5f8a587
Merge branch 'main' of https://github.com/Azure/bicep-registry-modules
AlexanderSehr May 23, 2024
7089a7c
Merge branch 'main' of https://github.com/Azure/bicep-registry-modules
AlexanderSehr May 23, 2024
0b8a80d
Merge branch 'main' of https://github.com/Azure/bicep-registry-modules
AlexanderSehr May 25, 2024
11d7ce4
Merge branch 'main' of https://github.com/Azure/bicep-registry-modules
AlexanderSehr May 28, 2024
94b60f8
Merge branch 'main' of https://github.com/Azure/bicep-registry-modules
AlexanderSehr May 31, 2024
f3f5b23
Merge branch 'main' of https://github.com/Azure/bicep-registry-modules
AlexanderSehr Jun 3, 2024
68393fe
Merge branch 'main' of https://github.com/Azure/bicep-registry-modules
AlexanderSehr Jun 4, 2024
148d10e
Merge branch 'main' of https://github.com/Azure/bicep-registry-modules
AlexanderSehr Jun 7, 2024
3bbd671
Merge branches 'main' and 'main' of https://github.com/Azure/bicep-re…
AlexanderSehr Jun 10, 2024
3f64fd8
Merge branch 'main' of https://github.com/Azure/bicep-registry-modules
AlexanderSehr Jun 13, 2024
2355e78
Merge branch 'main' of https://github.com/Azure/bicep-registry-modules
AlexanderSehr Jun 13, 2024
a36536f
Merge branch 'main' of https://github.com/Azure/bicep-registry-modules
AlexanderSehr Jun 14, 2024
a264c36
Merge branch 'main' of https://github.com/Azure/bicep-registry-modules
AlexanderSehr Jun 14, 2024
fb8c37e
Merge branch 'main' of https://github.com/Azure/bicep-registry-modules
AlexanderSehr Jun 15, 2024
27786a3
Update to latest
AlexanderSehr Jun 15, 2024
14da135
Finished first draft of test workflow
AlexanderSehr Jun 16, 2024
dc78a82
Update to latest
AlexanderSehr Jun 16, 2024
9e5a7ef
Update to latest
AlexanderSehr Jun 16, 2024
b318634
Update to latest
AlexanderSehr Jun 16, 2024
a27fea0
Update to latest
AlexanderSehr Jun 16, 2024
7118a82
Update to latest
AlexanderSehr Jun 16, 2024
c0ec391
Removed CI trigger
AlexanderSehr Jun 16, 2024
3164345
Update to latest
AlexanderSehr Jun 16, 2024
d4798b7
Merge branch 'main' into users/alsehr/pesterCiTests
AlexanderSehr Jun 16, 2024
b089f0b
Update to latest
AlexanderSehr Jun 16, 2024
e176dbb
Update to latest
AlexanderSehr Jun 16, 2024
3552f24
Update to latest
AlexanderSehr Jun 16, 2024
f2b303c
Update to latest
AlexanderSehr Jun 16, 2024
ad1cf99
Update to latest
AlexanderSehr Jun 16, 2024
319299c
Update to latest
AlexanderSehr Jun 16, 2024
1d257b1
Update to latest
AlexanderSehr Jul 9, 2024
ccbec38
Update to latest
AlexanderSehr Jul 9, 2024
1017dac
Update to latest
AlexanderSehr Jul 9, 2024
360cd01
Update to latest
AlexanderSehr Jul 9, 2024
13c076a
Update to latest
AlexanderSehr Jul 9, 2024
745b786
Undo temp trigger
AlexanderSehr Jul 9, 2024
23bee9b
Merge branch 'main' into users/alsehr/pesterCiTests
ChrisSidebotham Jul 22, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions .github/workflows/platform.ci-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
name: ".Platform - Run CI tests"

on:
workflow_dispatch:
inputs:
testFileRegex:
type: string
description: "The regex of the test file(s) to run"
required: false
default: '.*'
push:
branches:
- main
paths:
- ".github/workflows/platform.ci-tests.yml"
- "avm/utilities/**"
- "!*/**/README.md"
schedule:
- cron: "0 0 * * Sun" # Every Sunday

env:
workflowPath: ".github/workflows/platform.ci-tests.yml"


jobs:
###########################
# Initialize pipeline #
###########################
job_initialize_pipeline:
runs-on: ubuntu-latest
name: "Initialize pipeline"
steps:
- name: "Checkout"
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: "Set input parameters to output variables"
id: get-workflow-param
uses: ./.github/actions/templates/avm-getWorkflowInput
with:
workflowPath: "${{ env.workflowPath}}"
outputs:
workflowInput: ${{ steps.get-workflow-param.outputs.workflowInput }}

###############
# Removal #
###############
job_run_tests:
runs-on: ubuntu-20.04
name: "Run CI tests"
needs:
- job_initialize_pipeline
steps:
- name: "Checkout"
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set environment
uses: ./.github/actions/templates/avm-setEnvironment

- name: Run CI tests
id: pester_run_step
uses: azure/powershell@v2
with:
inlineScript: |
# Load used functions
. (Join-Path $env:GITHUB_WORKSPACE 'avm' 'utilities' 'tests' 'Test-CI.ps1')

$functionInput = @{
RepoRootPath = $env:GITHUB_WORKSPACE
BranchName = $env:GITHUB_REF
GitHubRepository = $env:GITHUB_REPOSITORY
TestFileRegex = '${{ (fromJson(needs.job_initialize_pipeline.outputs.workflowInput)).testFileRegex }}'
}

Write-Verbose "Invoke task with" -Verbose
Write-Verbose ($functionInput | ConvertTo-Json | Out-String) -Verbose

$outputsFilePath = Test-CI @functionInput

Write-Output ('{0}={1}' -f 'formattedPesterResultsPath', $outputsFilePath) >> $env:GITHUB_OUTPUT
azPSVersion: "latest"

- name: "Output to GitHub job summaries"
if: always()
shell: pwsh
run: |
# Grouping task logs
Write-Output '::group::Output to GitHub job summaries'

$mdPesterOutputFilePath = '${{ steps.pester_run_step.outputs.formattedPesterResultsPath }}'

if (-not (Test-Path $mdPesterOutputFilePath)) {
Write-Warning ('Input file [{0}] not found. Please check if the previous task threw an error and try again.' -f $mdPesterOutputFilePath)
} else {
Get-Content $mdPesterOutputFilePath >> $env:GITHUB_STEP_SUMMARY
Write-Verbose ('Successfully printed out file [{0}] to Job Summaries' -f $mdPesterOutputFilePath) -Verbose
}
Write-Output '::endgroup::'
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Parse a Pester output containing checks & results and generate formatted markdow
.DESCRIPTION
Parse a Pester output containing checks & results and generate formatted markdown file out of it.

.PARAMETER RepoRootPath
Optional. The path to the root of the repository

.PARAMETER PesterTestResults
Mandatory. The Pester tests results to parse. Can be fetched by running Pester with the `-PassThru` parameter. For example:

Expand Down Expand Up @@ -68,6 +71,9 @@ function Set-PesterGitHubOutput {

[CmdletBinding(SupportsShouldProcess)]
param (
[Parameter(Mandatory = $false)]
[string] $RepoRootPath = (Get-Item $PSScriptRoot).Parent.Parent.Parent.Parent.Parent.FullName,

[Parameter(Mandatory = $true)]
[PSCustomObject] $PesterTestResults,

Expand All @@ -94,6 +100,8 @@ function Set-PesterGitHubOutput {
Write-Verbose ('Formatting [{0}] skipped tests' -f $skippedTests.Count)
Write-Verbose ('Formatting [{0}] tests with explicit warnings' -f $warnings.Count)

$moduleSplitRegex = '[\/|\\]avm[\/|\\](res|ptn)[\/|\\]'

######################
# Set output content #
######################
Expand All @@ -108,7 +116,7 @@ function Set-PesterGitHubOutput {
$fileContent += [System.Collections.ArrayList]@(
'| Total No. of Processed Tests| Passed Tests :white_check_mark: | Failed Tests :x: | Skipped Tests :paperclip: | Tests with warnings :warning: |',
'| :-- | :-- | :-- | :-- | :-- |',
('| {0} | {1} | {2} | {3} | {4} |' -f $PesterTestResults.TotalCount, $passedTests.count , $failedTests.count, $skippedTests.count, $testsWithWarnings.count),
('| {0} | {1} | {2} | {3} | {4} |' -f $PesterTestResults.TotalCount, $passedTests.count , $failedTests.count, $skippedTests.count, $testsWithWarnings.count),
''
)

Expand All @@ -134,9 +142,16 @@ function Set-PesterGitHubOutput {
$intermediateNameElements[-1] = '**{0}**' -f $failedTest.ExpandedName
$testName = ((($intermediateNameElements -join ' / ' | Out-String) -replace '\|', '\|') -replace '_', '\_').Trim()

if ($failedTest.ScriptBlock.File -match $moduleSplitRegex) {
# Module test
$errorFileIdentifier = $failedTest.ErrorRecord.TargetObject.File -split $moduleSplitRegex
$errorTestFile = ('avm/{0}/{1}' -f $errorFileIdentifier[1], $errorFileIdentifier[2]) -replace '\\', '/' # e.g., [avm\res\cognitive-services\account\tests\unit\custom.tests.ps1]
} else {
# None-module test
$testFile = $failedTest.ScriptBlock.File -replace ('{0}[\\|\/]*' -f [regex]::Escape($RepoRootPath))
}

$errorTestLine = $failedTest.ErrorRecord.TargetObject.Line
$errorFileIdentifier = $failedTest.ErrorRecord.TargetObject.File -split '[\/|\\]avm[\/|\\](res|ptn)[\/|\\]'
$errorTestFile = ('avm/{0}/{1}' -f $errorFileIdentifier[1], $errorFileIdentifier[2]) -replace '\\', '/' # e.g., [avm\res\cognitive-services\account\tests\unit\custom.tests.ps1]
$errorMessage = ($failedTest.ErrorRecord.TargetObject.Message.Trim() -replace '_', '\_') -replace '\n', '<br>' # Replace new lines with <br> to enable line breaks in markdown

$testReference = '{0}:{1}' -f (Split-Path $errorTestFile -Leaf), $errorTestLine
Expand Down Expand Up @@ -181,10 +196,16 @@ function Set-PesterGitHubOutput {
$intermediateNameElements[-1] = '**{0}**' -f $passedTest.ExpandedName
$testName = ((($intermediateNameElements -join ' / ' | Out-String) -replace '\|', '\|') -replace '_', '\_').Trim()

$testLine = $passedTest.ScriptBlock.StartPosition.StartLine
$testFileIdentifier = $passedTest.ScriptBlock.File -split '[\/|\\]avm[\/|\\](res|ptn)[\/|\\]'
$testFile = ('avm/{0}/{1}' -f $testFileIdentifier[1], $testFileIdentifier[2]) -replace '\\', '/' # e.g., [avm\res\cognitive-services\account\tests\unit\custom.tests.ps1]
if ($passedTest.ScriptBlock.File -match $moduleSplitRegex) {
# Module test
$testFileIdentifier = $passedTest.ScriptBlock.File -split $moduleSplitRegex
$testFile = ('avm/{0}/{1}' -f $testFileIdentifier[1], $testFileIdentifier[2]) -replace '\\', '/' # e.g., [avm\res\cognitive-services\account\tests\unit\custom.tests.ps1]
} else {
# None-module test
$testFile = $passedTest.ScriptBlock.File -replace ('{0}[\\|\/]*' -f [regex]::Escape($RepoRootPath))
}

$testLine = $passedTest.ScriptBlock.StartPosition.StartLine
$testReference = '{0}:{1}' -f (Split-Path $testFile -Leaf), $testLine
if (-not [String]::IsNullOrEmpty($GitHubRepository) -and -not [String]::IsNullOrEmpty($BranchName)) {
# Creating URL to test file to enable users to 'click' on it
Expand Down Expand Up @@ -229,10 +250,16 @@ function Set-PesterGitHubOutput {

$reason = ('Test {0}' -f $skippedTest.ErrorRecord.Exception.Message -replace '\|', '\|').Trim()

$testLine = $skippedTest.ScriptBlock.StartPosition.StartLine
$testFileIdentifier = $skippedTest.ScriptBlock.File -split '[\/|\\]avm[\/|\\](res|ptn)[\/|\\]'
$testFile = ('avm/{0}/{1}' -f $testFileIdentifier[1], $testFileIdentifier[2]) -replace '\\', '/' # e.g., [avm\res\cognitive-services\account\tests\unit\custom.tests.ps1]
if ($skippedTest.ScriptBlock.File -match $moduleSplitRegex) {
# Module test
$testFileIdentifier = $skippedTest.ScriptBlock.File -split $moduleSplitRegex
$testFile = ('avm/{0}/{1}' -f $testFileIdentifier[1], $testFileIdentifier[2]) -replace '\\', '/' # e.g., [avm\res\cognitive-services\account\tests\unit\custom.tests.ps1]
} else {
# None-module test
$testFile = $skippedTest.ScriptBlock.File -replace ('{0}[\\|\/]*' -f [regex]::Escape($RepoRootPath))
}

$testLine = $skippedTest.ScriptBlock.StartPosition.StartLine
$testReference = '{0}:{1}' -f (Split-Path $testFile -Leaf), $testLine
if (-not [String]::IsNullOrEmpty($GitHubRepository) -and -not [String]::IsNullOrEmpty($BranchName)) {
# Creating URL to test file to enable users to 'click' on it
Expand Down Expand Up @@ -275,8 +302,15 @@ function Set-PesterGitHubOutput {
$intermediateNameElements[-1] = '**{0}**' -f $test.ExpandedName
$testName = ((($intermediateNameElements -join ' / ' | Out-String) -replace '\|', '\|') -replace '_', '\_').Trim()

$testLine = $test.ScriptBlock.StartPosition.StartLine
$testFileIdentifier = $test.ScriptBlock.File -split '[\/|\\]avm[\/|\\](res|ptn)[\/|\\]'
if ($test.ScriptBlock.File -match $moduleSplitRegex) {
# Module test
$testLine = $test.ScriptBlock.StartPosition.StartLine
$testFileIdentifier = $test.ScriptBlock.File -split $moduleSplitRegex
} else {
# None-module test
$testFile = $test.ScriptBlock.File -replace ('{0}[\\|\/]*' -f [regex]::Escape($RepoRootPath))
}

$testFile = ('avm/{0}/{1}' -f $testFileIdentifier[1], $testFileIdentifier[2]) -replace '\\', '/' # e.g., [avm\res\cognitive-services\account\tests\unit\custom.tests.ps1]

$testReference = '{0}:{1}' -f (Split-Path $testFile -Leaf), $testLine
Expand Down
111 changes: 111 additions & 0 deletions avm/utilities/tests/Test-CI.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<#
.SYNOPSIS
This script executes either all CI-focused Pester tests, or a specific test file and generates a markdown with the test results.

.DESCRIPTION
This script executes either all CI-focused Pester tests, or a specific test file and generates a markdown with the test results.
It only considers test files in its current or sub-folders

.PARAMETER RepoRootPath
Optional. The root of the repository. Used to correctly resolve paths.

.PARAMETER BranchName
Optional. The branch to test for.

.PARAMETER GitHubRepository
Optional. The repository containing the test file. If provided it will be used to generate a URL to the exact line of the test.
For example: 'Azure/ResourceModules'

.PARAMETER TestFileRegex
Optional. The regex to use when searching for test files

.PARAMETER SkipOutput
Optional. Set if you don't want the script to generate the results markdown

.EXAMPLE
Test-CI

Invoke all CI-focused Pester tests and generate a markdown file with the test results.

.EXAMPLE
Test-CI -TestFileRegex '.*Ordered.*' -SkipOutput

Invoke all CI-focused Pester tests that match the regex '.*Ordered.*' skip the generation of the markdown file containing the test results.
#>
function Test-CI {

[CmdletBinding()]
param (
[Parameter()]
[string] $RepoRootPath = (Get-Item $PSScriptRoot).Parent.Parent.Parent.FullName,

[Parameter()]
[string] $BranchName = (git branch --show-current),

[Parameter(Mandatory )]
[string] $GitHubRepository,

[Parameter()]
[string] $TestFileRegex = '.*',

[Parameter()]
[switch] $SkipOutput
)

# Load used functions
. (Join-Path $RepoRootPath 'avm' 'utilities' 'pipelines' 'staticValidation' 'compliance' 'Set-PesterGitHubOutput.ps1')

$testFiles = (Get-ChildItem -Path (Join-Path $RepoRootPath 'avm' 'utilities' 'tests') -Recurse -File -Filter '*.tests.ps1').FullName | Where-Object {
$_ -match $TestFileRegex
}

if (-not $testFiles) {
Write-Warning "Skipping test execution as no test files matching the regex [$TestFileRegex] were found."
return
}

# ------------------- #
# Invoke Pester tests #
# ------------------- #
$pesterConfiguration = @{
Run = @{
Container = New-PesterContainer -Path $testFiles -Data @{
RepoRootPath = $RepoRootPath
}
PassThru = $true
}
Output = @{
Verbosity = 'Detailed'
}
}

Write-Verbose 'Invoke test with' -Verbose
Write-Verbose ($pesterConfiguration | ConvertTo-Json -Depth 4 | Out-String) -Verbose

$testResults = Invoke-Pester -Configuration $pesterConfiguration

if (-not $SkipOutput) {
# ----------------------------------------- #
# Create formatted Pester Test Results File #
# ----------------------------------------- #

$functionInput = @{
RepoRootPath = $RepoRootPath
PesterTestResults = $testResults
OutputFilePath = Join-Path $RepoRootPath 'avm' 'utilities' 'tests' 'Pester-output.md'
GitHubRepository = $GitHubRepository
BranchName = $BranchName
}

Write-Verbose 'Invoke Pester formatting function with' -Verbose
Write-Verbose ($functionInput | ConvertTo-Json -Depth 0 | Out-String) -Verbose

Set-PesterGitHubOutput @functionInput -Verbose

return $functionInput.OutputFilePath
}
}




Loading