Skip to content

Commit cbcc559

Browse files
authored
implementing error status (#1115)
* implementing error status fixing #1100 * reverting testing error * adding copilot suggestions
1 parent c8939bd commit cbcc559

17 files changed

Lines changed: 89 additions & 55 deletions

src/powershell/assets/ReportTemplate.html

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

src/powershell/private/core/Add-ZtTestResultDetail.ps1

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ function Add-ZtTestResultDetail {
8989
[string[]] $Tag,
9090

9191
# Optional. Custom status to return instead of the default status.
92-
[Parameter(Mandatory = $false)]
93-
[ValidateSet('Investigate')]
92+
[Parameter()]
93+
[ValidateSet('Investigate','Error')]
9494
[string] $CustomStatus,
9595

9696
[ValidateSet('Graph', 'Azure', 'AipService', 'ExchangeOnline', 'SecurityCompliance', 'SharePointOnline')]
@@ -133,6 +133,12 @@ function Add-ZtTestResultDetail {
133133
}
134134
}
135135

136+
if ($CustomStatus -eq 'Error') {
137+
if ([string]::IsNullOrEmpty($Result)) {
138+
$Result = 'An error occurred while running this test.'
139+
}
140+
}
141+
136142
if (-not $Description) {
137143
# Check if a markdown file exists for the cmdlet and parse the content
138144
$markdownPath = Join-Path -Path $script:ModuleRoot -ChildPath "tests/Test-Assessment.$actualTestId.md"
@@ -183,10 +189,19 @@ function Add-ZtTestResultDetail {
183189
$UserImpact = $testMeta.UserImpact
184190
}
185191

192+
$getZtTestStatusParams = @{
193+
Status = $Status
194+
SkippedBecause = $SkippedBecause
195+
}
196+
197+
if ($CustomStatus) {
198+
$getZtTestStatusParams.CustomStatus = $CustomStatus
199+
}
200+
186201
$testInfo = @{
187202
TestId = $actualTestId
188203
TestTitle = $docsTitle
189-
TestStatus = Get-ZtTestStatus -Status $Status -SkippedBecause $SkippedBecause -CustomStatus $CustomStatus
204+
TestStatus = Get-ZtTestStatus @getZtTestStatusParams
190205
TestCategory = $category
191206
TestTags = $Tag
192207
TestAppliesTo = $AppliesTo

src/powershell/private/core/Get-ZtTestStatus.ps1

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
Returns the status of the test as a string.
33
#>
44
function Get-ZtTestStatus {
5+
[OutputType([string])]
56
[CmdletBinding()]
6-
param(
7+
param (
78
# The status of the test.
89
[Parameter(Mandatory = $true)]
910
[bool] $Status,
@@ -12,14 +13,14 @@ function Get-ZtTestStatus {
1213
[string] $SkippedBecause,
1314

1415
# Optional. Custom status to return instead of the default status.
16+
[ValidateSet('Investigate','Error')]
1517
[string] $CustomStatus
1618
)
1719

1820
if ($CustomStatus) {
1921
return $CustomStatus
2022
}
21-
22-
if ($Status) {
23+
elseif ($Status) {
2324
return "Passed"
2425
}
2526
else {
@@ -30,9 +31,9 @@ function Get-ZtTestStatus {
3031
if ($SkippedBecause -eq "UnderConstruction") {
3132
return "Planned"
3233
}
33-
return "Skipped"
34+
else {
35+
return "Skipped"
36+
}
3437
}
3538
}
36-
37-
return $Status
3839
}

src/powershell/private/core/Test-ZtContext.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ function Test-ZtContext {
4242
$message += " Add the missing 'Application' permissions in the Microsoft Entra portal and grant consent. You will also need to Disconnect-Graph to refresh the permissions."
4343
$message += " Click here to open the 'API Permissions' blade for this app: $urlTemplate"
4444
}
45+
4546
$validContext = $false
4647
}
4748

src/powershell/private/export/Invoke-ZtTenantDataExport.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@
131131
return
132132
}
133133

134-
Start-Sleep -Seconds 5
134+
# Wait a bit before checking again
135+
Start-Sleep -Seconds 1
135136
}
136137
}
137138
#endregion Wait for Dependencies

src/powershell/private/export/Write-ZtExportLog.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function Write-ZtExportLog {
3838
[void][System.IO.Directory]::CreateDirectory($LogsPath)
3939

4040
$name = $Result.Name
41-
$status = if ($Result.Success) { 'Done' } else { 'Failed' }
41+
$status = if ($Result.Success) { 'Done' } elseif ($Result.Error) { 'Error' } else { 'Failed' }
4242
$duration = if ($null -ne $Result.Duration) { $Result.Duration.ToString('hh\:mm\:ss\.fff') } else { 'N/A' }
4343
$startTime = if ($Result.Start) { $Result.Start.ToString('yyyy-MM-dd HH:mm:ss.fff') } else { 'N/A' }
4444
$endTime = if ($Result.End) { $Result.End.ToString('yyyy-MM-dd HH:mm:ss.fff') } else { 'N/A' }

src/powershell/private/export/Write-ZtExportProgress.ps1

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function Write-ZtExportProgress {
5656
$LogsPath,
5757

5858
[Parameter(Mandatory = $true)]
59-
[ValidateSet('Started', 'Waiting', 'InProgress', 'Completed', 'Failed', 'Status')]
59+
[ValidateSet('Started', 'Waiting', 'InProgress', 'Completed', 'Failed', 'Status', 'Error')]
6060
[string]
6161
$Action,
6262

@@ -81,22 +81,26 @@ function Write-ZtExportProgress {
8181
if ($null -ne $Duration) {
8282
$line += " $($Duration.ToString('hh\:mm\:ss\.fff'))"
8383
}
84+
8485
if ($Action -eq 'Waiting' -and $StatusMessage) {
8586
$line += " $StatusMessage"
8687
}
88+
8789
if ($Action -eq 'Status' -and $StatusMessage) {
8890
$line += " $StatusMessage"
8991
}
90-
if ($Action -eq 'Failed' -and $ErrorMessage) {
92+
93+
if ($Action -in @('Failed', 'Error') -and $ErrorMessage) {
9194
$errorText = "$ErrorMessage"
9295
$errorText = $errorText -replace '[\r\n\t]+', ' '
9396
if ($errorText.Length -gt 1000) {
9497
$errorText = $errorText.Substring(0, 1000) + '...'
9598
}
99+
96100
$line += " $errorText"
97101
}
98-
$line += [System.Environment]::NewLine
99102

103+
$line += [System.Environment]::NewLine
100104
$progressFilePath = Join-Path $LogsPath '1-export_progress.log'
101105
$fullPath = [System.IO.Path]::GetFullPath($progressFilePath)
102106
$normalizedPath = if ($IsWindows) { $fullPath.ToLowerInvariant() } else { $fullPath }
@@ -131,6 +135,7 @@ function Write-ZtExportProgress {
131135
if ($lockAcquired -and $null -ne $mutex) {
132136
$null = $mutex.ReleaseMutex()
133137
}
138+
134139
if ($null -ne $mutex) {
135140
$mutex.Dispose()
136141
}

src/powershell/private/tests/Invoke-ZtTest.ps1

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@
174174
Set-ZtTimedOutResult -Result $result -Test $Test -Timeout $TestTimeout
175175
}
176176
else {
177+
Write-PSFMessage -Level Warning -Message "PipelineStoppedException in test '{0}' was not caused by timeout" -StringValues $Test.TestID -Target $Test -ErrorRecord $_
177178
throw
178179
}
179180
}
@@ -184,10 +185,18 @@
184185
}
185186
}
186187
catch {
187-
Write-PSFMessage -Level Warning -Message "Error executing test '{0}'" -StringValues $Test.TestID -Target $Test -ErrorRecord $_
188+
Write-PSFMessage -Level Warning -Message "Error executing test '{0}': {1}" -StringValues $Test.TestID, $_.Exception.Message -Target $Test -ErrorRecord $_
188189
$result.Success = $false
189190
$result.Error = $_
190-
Update-ZtProgressState -WorkerId $Test.TestID -WorkerName $testDisplayName -WorkerStatus 'Failed' -WorkerDetail "$_"
191+
$message = @(
192+
'❌ Test {0} failed due to an unexpected error.' -f $Test.TestID
193+
' - **Error Message**: {0}.' -f $_.Exception.Message
194+
'```'
195+
'{0}' -f ($_ | Get-Error | Out-String)
196+
'```'
197+
) -join "`r`n"
198+
Add-ZtTestResultDetail -TestId $Test.TestID -Title $Test.Title -Status $false -Result $message -CustomStatus 'Error'
199+
Update-ZtProgressState -WorkerId $Test.TestID -WorkerName $testDisplayName -WorkerStatus 'Error' -WorkerDetail "Error: $($_.Exception.Message)"
191200
}
192201
finally {
193202
$result.End = Get-Date
@@ -196,8 +205,10 @@
196205
# Reset marker in an assured way, to prevent confusion about the current test being executed
197206
$script:__ztCurrentTest = $null
198207
}
208+
199209
Write-PSFMessage -Message "Processing test '{0}' - Concluded" -StringValues $Test.TestID -Target $Test -Tag end
200210
}
211+
201212
end {
202213
$result.Messages = Get-PSFMessage -Runspace ([runspace]::DefaultRunspace.InstanceId) | Where-Object { $_ -notin $previousMessages }
203214
Write-ZtTestStatistics -Result $result
@@ -220,8 +231,11 @@
220231
elseif ($result.Success) {
221232
Write-ZtTestProgress -TestID $result.TestID -LogsPath $LogsPath -Action Completed -Duration $result.Duration
222233
}
234+
elseif ($result.Error) {
235+
Write-ZtTestProgress -TestID $result.TestID -LogsPath $LogsPath -Action Error -Duration $result.Duration -ErrorMessage "Error: $($result.Error.Exception.Message)"
236+
}
223237
else {
224-
$progressError = if ($result.Error) { "$($result.Error)" } else { $null }
238+
$progressError = if ($result.Error) { "Error: $($result.Error.Exception.Message)" } else { $null }
225239
Write-ZtTestProgress -TestID $result.TestID -LogsPath $LogsPath -Action Failed -Duration $result.Duration -ErrorMessage $progressError
226240
}
227241
}

src/powershell/private/tests/Invoke-ZtTests.ps1

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@
9595
$testsToRun = $testsToRun.Where{ $_.Pillar -in $stablePillars }
9696
}
9797

98+
# Filter based on Compatible licenses
99+
$skippedTestsForLicense = $testsToRun.Where{$_.CompatibleLicense.Count -gt 0 -and (-not (Test-ZtLicense -CompatibleLicense $_.CompatibleLicense)) }
100+
$skippedTestsForLicense.ForEach{
101+
Write-PSFMessage -Message ('Test {0} is skipped because no compatible license was found' -f $_.TestId) -Level Verbose
102+
Add-ZtTestResultDetail -SkippedBecause NoCompatibleLicenseFound -TestId $_.TestId
103+
}
104+
105+
$testsToRun = $testsToRun.Where{ $_.TestId -notin $skippedTestsForLicense.TestId }
106+
98107
# Filter based on service connection. If no service is specified in the test metadata, it will be run.
99108
$skippedTestsForService = $testsToRun.Where{ $_.Service.count -gt 0 -and $_.Service.Count -notin $_.Service.Where{ $_ -in $ConnectedService}.count }
100109
$skippedTestsForService.ForEach{
@@ -105,15 +114,6 @@
105114

106115
$testsToRun = $testsToRun.Where{ $_.TestId -notin $skippedTestsForService.TestId }
107116

108-
# Filter based on Compatible licenses
109-
$skippedTestsForLicense = $testsToRun.Where{$_.CompatibleLicense.Count -gt 0 -and (-not (Test-ZtLicense -CompatibleLicense $_.CompatibleLicense)) }
110-
$skippedTestsForLicense.ForEach{
111-
Write-PSFMessage -Message ('Test {0} is skipped because no compatible license was found' -f $_.TestId) -Level Verbose
112-
Add-ZtTestResultDetail -SkippedBecause NoCompatibleLicenseFound -TestId $_.TestId
113-
}
114-
115-
$testsToRun = $testsToRun.Where{ $_.TestId -notin $skippedTestsForLicense.TestId }
116-
117117
# Separate Sync Tests (Compliance/ExchangeOnline/SharePointOnline) from Parallel Tests (because of DLL order to manage in runspaces & remoting into WPS)
118118
[int[]]$syncTestIds = $testsToRun.Where{ $_.Pillar -eq 'Data'}.TestId
119119
$syncTests = $testsToRun.Where{ $_.TestId -in $syncTestIds }

src/powershell/private/tests/Set-ZtTimedOutResult.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function Set-ZtTimedOutResult {
2121
$Result.Error = New-ZtTimeoutErrorRecord -Test $Test -Timeout $Timeout
2222

2323
try {
24-
Add-ZtTestResultDetail -TestId $Test.TestID -Status $false -Result "The test did not complete within the configured timeout of $($Timeout.ToString('hh\:mm\:ss')). Partial results, if any, were discarded."
24+
Add-ZtTestResultDetail -TestId $Test.TestID -Status $false -CustomStatus 'Error' -Result "The test did not complete within the configured timeout of $($Timeout.ToString('hh\:mm\:ss')). Partial results, if any, were discarded."
2525
}
2626
catch {
2727
Write-PSFMessage -Level Warning -Message "Failed to overwrite timed-out test result detail for test '{0}': {1}" -StringValues $Test.TestID, $_ -Target $Test -Tag timeout

0 commit comments

Comments
 (0)