Skip to content

Commit

Permalink
Run tests (#3238)
Browse files Browse the repository at this point in the history
Add a new workflow to be able to run selected tests on a given REF,
like:


![image](https://github.com/microsoft/navcontainerhelper/assets/10775043/f89a0ff8-fd5d-40fd-b74f-0eedbcc3dc6c)

---------

Co-authored-by: freddydk <freddydk@users.noreply.github.com>
  • Loading branch information
freddydk and freddydk committed Dec 2, 2023
1 parent 8f56e2c commit 0bc7859
Show file tree
Hide file tree
Showing 39 changed files with 208 additions and 83 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/CI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ defaults:

jobs:
AnalyzeTests:
if: github.repository == 'Microsoft/NavContainerHelper'
runs-on: [ windows-latest ]
outputs:
tests: ${{ steps.Analyze.outputs.tests }}
Expand All @@ -37,6 +38,7 @@ jobs:
- name: Analyze
id: Analyze
run: |
$errorActionPreference = "stop"
$tests = @(Get-ChildItem -Path (Join-Path $ENV:GITHUB_WORKSPACE 'Tests\*.Tests.ps1') | ForEach-Object { $_.BaseName }) | ConvertTo-Json -compress
Add-Content -Path $env:GITHUB_OUTPUT -Value "tests=$tests"
Write-Host "tests=$tests"
Expand All @@ -62,7 +64,7 @@ jobs:
try {
$errorActionPreference = "stop"
Set-StrictMode -version 2.0
$pesterContainer = New-PesterContainer -Path (Join-Path $ENV:GITHUB_WORKSPACE 'Tests\${{ matrix.test }}.ps1') -Data @{ "licenseFile" = '${{ secrets.licensefile }}'; "buildLicenseFile" = '${{ secrets.buildLicenseFile }}'; "insiderSasToken" = '${{ secrets.insiderSasToken }}' }
$pesterContainer = New-PesterContainer -Path (Join-Path $ENV:GITHUB_WORKSPACE 'Tests\${{ matrix.test }}.ps1') -Data @{ "licenseFile" = '${{ secrets.licensefile }}'; "buildLicenseFile" = '${{ secrets.buildLicenseFile }}' }
$result = Invoke-Pester -Container $pesterContainer -passthru
if ($result.FailedCount -gt 0) {
Write-Host "::Error::$($result.FailedCount) tests are failing"
Expand Down Expand Up @@ -95,7 +97,7 @@ jobs:
try {
$errorActionPreference = "stop"
Set-StrictMode -version 2.0
$pesterContainer = New-PesterContainer -Path (Join-Path $ENV:GITHUB_WORKSPACE 'Tests\${{ matrix.test }}.ps1') -Data @{ "licenseFile" = '${{ secrets.licensefile }}'; "buildLicenseFile" = '${{ secrets.buildLicenseFile }}'; "insiderSasToken" = '${{ secrets.insiderSasToken }}' }
$pesterContainer = New-PesterContainer -Path (Join-Path $ENV:GITHUB_WORKSPACE 'Tests\${{ matrix.test }}.ps1') -Data @{ "licenseFile" = '${{ secrets.licensefile }}'; "buildLicenseFile" = '${{ secrets.buildLicenseFile }}' }
$result = Invoke-Pester -Container $pesterContainer -passthru
if ($result.FailedCount -gt 0) {
Write-Host "::Error::$($result.FailedCount) tests are failing"
Expand Down Expand Up @@ -128,7 +130,7 @@ jobs:
try {
$errorActionPreference = "stop"
Set-StrictMode -version 2.0
$pesterContainer = New-PesterContainer -Path (Join-Path $ENV:GITHUB_WORKSPACE 'LinuxTests\${{ matrix.test }}.ps1') -Data @{ "licenseFile" = '${{ secrets.licensefile }}'; "buildLicenseFile" = '${{ secrets.buildLicenseFile }}'; "insiderSasToken" = '${{ secrets.insiderSasToken }}' }
$pesterContainer = New-PesterContainer -Path (Join-Path $ENV:GITHUB_WORKSPACE 'LinuxTests\${{ matrix.test }}.ps1') -Data @{ "licenseFile" = '${{ secrets.licensefile }}'; "buildLicenseFile" = '${{ secrets.buildLicenseFile }}' }
$result = Invoke-Pester -Container $pesterContainer -passthru
if ($result.FailedCount -gt 0) {
Write-Host "::Error::$($result.FailedCount) tests are failing"
Expand Down
17 changes: 9 additions & 8 deletions .github/workflows/Release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ defaults:

jobs:
Deploy:
if: github.repository == 'Microsoft/NavContainerHelper'
runs-on: [ windows-latest ]
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Deploy
run: |
$errorActionPreference = "stop"
try {
$path = Join-Path ([System.IO.Path]::GetTempPath()) "BcContainerHelper"
New-Item -path $path -itemType Directory | Out-Null
Expand Down Expand Up @@ -56,7 +58,7 @@ jobs:
Write-Host "Release Notes:"
Write-Host $VersionReleaseNotes
Write-Host "Update Module Manifest"
Update-ModuleManifest -Path (Join-Path $path "BcContainerHelper.psd1") `
-RootModule "BcContainerHelper.psm1" `
Expand All @@ -67,28 +69,27 @@ jobs:
-CompanyName "Microsoft" `
-ReleaseNotes $versionReleaseNotes
$certFileName = Join-Path ([System.IO.Path]::GetTempPath()) "$([GUID]::NewGuid().ToString()).pfx"
Download-File -sourceUrl '${{ secrets.CodeSignCertificateUrl }}' -destinationFile $certFileName
Remove-Module BcContainerHelper
Write-Host $path
Write-Host "Signing scripts"
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($certFileName, '${{ secrets.CodeSignCertificatePassword }}')
$filesToSign = @((Get-ChildItem $path -Filter "*.ps1" -Recurse -Depth 99).FullName)+
@((Get-ChildItem $path -Filter "*.psm1" -Recurse -Depth 99).FullName)
Set-AuthenticodeSignature -Certificate $cert -HashAlgorithm SHA256 -TimestampServer "http://timestamp.digicert.com" -FilePath $filesToSign
Write-Host "Upload to storage (preview)"
$storageContext = New-AzureStorageContext -ConnectionString '${{ secrets.BchStorageConnectionString }}'
New-AzureStorageContainer -Name 'public' -Context $storageContext -Permission 'Container' -ErrorAction Ignore | Out-Null
Compress-Archive -path $path -DestinationPath "$($path).zip"
Set-AzureStorageBlobContent -File "$($path).zip" -Context $storageContext -Container 'public' -Blob "$version.zip" -Force | Out-Null
Set-AzureStorageBlobContent -File "$($path).zip" -Context $storageContext -Container 'public' -Blob "latest.zip" -Force | Out-Null
Write-Host "Publishing Module"
Publish-Module -Path $path -NuGetApiKey '${{ secrets.NugetKey }}' -SkipAutomaticTags
}
Expand Down
158 changes: 158 additions & 0 deletions .github/workflows/RunTests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
name: Run Tests

on:
workflow_dispatch:
inputs:
ref:
description: ref on which to run the end-2-end tests (default is head_sha on the current branch)
required: false
default: ''
testPatterns:
description: Commaseparated list of patterns to match against test names (default is * for all tests)
required: false
default: ''

permissions:
contents: read
actions: read
pull-requests: write
checks: write

concurrency:
group: 'runTests-${{ github.ref }}'
cancel-in-progress: true

defaults:
run:
shell: powershell

jobs:
AnalyzeTests:
runs-on: [ windows-latest ]
outputs:
tests: ${{ steps.Analyze.outputs.tests }}
linuxtests: ${{ steps.Analyze.outputs.linuxtests }}
steps:
- uses: actions/checkout@v3
with:
ref: ${{ github.event.inputs.ref }}
lfs: true

- name: Analyze
id: Analyze
env:
testPatterns: ${{ github.event.inputs.testPatterns }}
run: |
$errorActionPreference = "stop"
$testPatterns = $ENV:TESTPATTERNS
if (!$testPatterns) { $testPatterns = '*' }
$testPatternArr = $testPatterns.Split(',')
Write-Host "Running test matching patterns:"
$testPatternArr | ForEach-Object { Write-Host "- $_" }
$tests = ConvertTo-Json -compress -InputObject @(Get-ChildItem -Path (Join-Path $ENV:GITHUB_WORKSPACE 'Tests\*.Tests.ps1') | Where-Object { $name = $_.Basename; $testPatternArr | Where-Object { $name -like "$($_).Tests" } } | ForEach-Object { $_.BaseName })
Add-Content -Path $env:GITHUB_OUTPUT -Value "tests=$tests"
Write-Host "tests=$tests"
$linuxtests = ConvertTo-Json -compress -InputObject @(Get-ChildItem -Path (Join-Path $ENV:GITHUB_WORKSPACE 'LinuxTests\*.Tests.ps1') | Where-Object { $name = $_.Basename; $testPatternArr | Where-Object { $name -like "$($_).Tests" } } | ForEach-Object { $_.BaseName })
Add-Content -Path $env:GITHUB_OUTPUT -Value "linuxtests=$linuxtests"
Write-Host "linuxtests=$linuxtests"
PS5:
runs-on: [ windows-latest ]
needs: [ AnalyzeTests ]
if: needs.AnalyzeTests.outputs.tests != '[]'
strategy:
matrix:
test: ${{fromJson(needs.AnalyzeTests.outputs.tests)}}
fail-fast: false
steps:
- uses: actions/checkout@v3
with:
ref: ${{ github.event.inputs.ref }}
lfs: true

- name: Run Tests
run: |
try {
$errorActionPreference = "stop"
Set-StrictMode -version 2.0
$pesterContainer = New-PesterContainer -Path (Join-Path $ENV:GITHUB_WORKSPACE 'Tests\${{ matrix.test }}.ps1') -Data @{ "licenseFile" = '${{ secrets.licensefile }}'; "buildLicenseFile" = '${{ secrets.buildLicenseFile }}' }
$result = Invoke-Pester -Container $pesterContainer -passthru
if ($result.FailedCount -gt 0) {
Write-Host "::Error::$($result.FailedCount) tests are failing"
$host.SetShouldExit(1)
}
}
catch {
Write-Host "::Error::Exception when running tests. The Error was $($_.Exception.Message)"
$host.SetShouldExit(1)
}
PS7:
runs-on: [ windows-latest ]
needs: [ AnalyzeTests ]
if: needs.AnalyzeTests.outputs.tests != '[]'
strategy:
matrix:
test: ${{fromJson(needs.AnalyzeTests.outputs.tests)}}
fail-fast: false
defaults:
run:
shell: pwsh
steps:
- name: Checkout
uses: actions/checkout@v3
with:
ref: ${{ github.event.inputs.ref }}
lfs: true

- name: Run Tests
run: |
try {
$errorActionPreference = "stop"
Set-StrictMode -version 2.0
$pesterContainer = New-PesterContainer -Path (Join-Path $ENV:GITHUB_WORKSPACE 'Tests\${{ matrix.test }}.ps1') -Data @{ "licenseFile" = '${{ secrets.licensefile }}'; "buildLicenseFile" = '${{ secrets.buildLicenseFile }}' }
$result = Invoke-Pester -Container $pesterContainer -passthru
if ($result.FailedCount -gt 0) {
Write-Host "::Error::$($result.FailedCount) tests are failing"
$host.SetShouldExit(1)
}
}
catch {
Write-Host "::Error::Exception when running tests. The Error was $($_.Exception.Message)"
$host.SetShouldExit(1)
}
Linux:
runs-on: [ ubuntu-latest ]
needs: [ AnalyzeTests ]
if: needs.AnalyzeTests.outputs.linuxtests != '[]'
strategy:
matrix:
test: ${{fromJson(needs.AnalyzeTests.outputs.linuxtests)}}
fail-fast: false
defaults:
run:
shell: pwsh
steps:
- name: Checkout
uses: actions/checkout@v3
with:
ref: ${{ github.event.inputs.ref }}
lfs: true

- name: Run Tests
run: |
try {
$errorActionPreference = "stop"
Set-StrictMode -version 2.0
$pesterContainer = New-PesterContainer -Path (Join-Path $ENV:GITHUB_WORKSPACE 'LinuxTests\${{ matrix.test }}.ps1') -Data @{ "licenseFile" = '${{ secrets.licensefile }}'; "buildLicenseFile" = '${{ secrets.buildLicenseFile }}' }
$result = Invoke-Pester -Container $pesterContainer -passthru
if ($result.FailedCount -gt 0) {
Write-Host "::Error::$($result.FailedCount) tests are failing"
$host.SetShouldExit(1)
}
}
catch {
Write-Host "::Error::Exception when running tests. The Error was $($_.Exception.Message)"
$host.SetShouldExit(1)
}
3 changes: 1 addition & 2 deletions LinuxTests/Auth.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
Param(
[string] $licenseFile,
[string] $buildlicenseFile,
[string] $insiderSasToken
[string] $buildlicenseFile
)

BeforeAll {
Expand Down
3 changes: 1 addition & 2 deletions LinuxTests/Saas.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
Param(
[string] $licenseFile,
[string] $buildlicenseFile,
[string] $insiderSasToken
[string] $buildlicenseFile
)

BeforeAll {
Expand Down
3 changes: 1 addition & 2 deletions Tests/Api.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
Param(
[string] $licenseFile,
[string] $buildlicenseFile,
[string] $insiderSasToken
[string] $buildlicenseFile
)

BeforeAll {
Expand Down
3 changes: 1 addition & 2 deletions Tests/AppHandling (NAV).Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
Param(
[string] $licenseFile,
[string] $buildlicenseFile,
[string] $insiderSasToken
[string] $buildlicenseFile
)

BeforeAll {
Expand Down
3 changes: 1 addition & 2 deletions Tests/AppHandling.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
Param(
[string] $licenseFile,
[string] $buildlicenseFile,
[string] $insiderSasToken
[string] $buildlicenseFile
)

BeforeAll {
Expand Down
3 changes: 1 addition & 2 deletions Tests/AzureAD.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
Param(
[string] $licenseFile,
[string] $buildlicenseFile,
[string] $insiderSasToken
[string] $buildlicenseFile
)

BeforeAll {
Expand Down
3 changes: 1 addition & 2 deletions Tests/AzureVM.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
Param(
[string] $licenseFile,
[string] $buildlicenseFile,
[string] $insiderSasToken
[string] $buildlicenseFile
)

BeforeAll {
Expand Down
3 changes: 1 addition & 2 deletions Tests/Bacpac (MT).Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
Param(
[string] $licenseFile,
[string] $buildlicenseFile,
[string] $insiderSasToken
[string] $buildlicenseFile
)

BeforeAll {
Expand Down
3 changes: 1 addition & 2 deletions Tests/Bacpac (NAV).Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
Param(
[string] $licenseFile,
[string] $buildlicenseFile,
[string] $insiderSasToken
[string] $buildlicenseFile
)

BeforeAll {
Expand Down
3 changes: 1 addition & 2 deletions Tests/Bacpac.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
Param(
[string] $licenseFile,
[string] $buildlicenseFile,
[string] $insiderSasToken
[string] $buildlicenseFile
)

BeforeAll {
Expand Down
3 changes: 1 addition & 2 deletions Tests/CompanyHandling.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
Param(
[string] $licenseFile,
[string] $buildlicenseFile,
[string] $insiderSasToken
[string] $buildlicenseFile
)

BeforeAll {
Expand Down
3 changes: 1 addition & 2 deletions Tests/ConfigPackageHandling.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
Param(
[string] $licenseFile,
[string] $buildlicenseFile,
[string] $insiderSasToken
[string] $buildlicenseFile
)

BeforeAll {
Expand Down
3 changes: 1 addition & 2 deletions Tests/ContainerHandling.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
Param(
[string] $licenseFile,
[string] $buildlicenseFile,
[string] $insiderSasToken
[string] $buildlicenseFile
)

BeforeAll {
Expand Down
3 changes: 1 addition & 2 deletions Tests/ContainerInfo.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
Param(
[string] $licenseFile,
[string] $buildlicenseFile,
[string] $insiderSasToken
[string] $buildlicenseFile
)

BeforeAll {
Expand Down
3 changes: 1 addition & 2 deletions Tests/GetAndRunTests (Windows Auth).Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
Param(
[string] $licenseFile,
[string] $buildlicenseFile,
[string] $insiderSasToken
[string] $buildlicenseFile
)

BeforeAll {
Expand Down

0 comments on commit 0bc7859

Please sign in to comment.