Skip to content

Commit

Permalink
Mitigate intermittent failures in access denied tests. (PowerShell#4788)
Browse files Browse the repository at this point in the history
This change mitigates intermittent timeout failures in the access denied tests as well as fixes unintended cross-test dependencies. If PowerShell takes longer that 10 seconds to complete a race can cause the subsequent test to pick up the output file of the previous test and fail (i.e., the expected output error file already exists from the previous test because it was created by the long running PowerShell process after the second test started.) Worse case, the failure cascades causing the race to propagate through the remaining tests in the set.

The fix is two fold:
1: Increase the timeout for waiting for the launched powershell process. (mitigation)
2: Ensure each uses a unique name for the error and done files to avoid polluting the next test.
  • Loading branch information
dantraMSFT authored and daxian-dbw committed Sep 12, 2017
1 parent fd3a003 commit 6a78e30
Showing 1 changed file with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,10 @@ Describe "Basic FileSystem Provider Tests" -Tags "CI" {
$protectedPath2 = Join-Path $protectedPath "Install"
$newItemPath = Join-Path $protectedPath "foo"
}
$errFile = "$testdrive\error.txt"
$doneFile = "$testdrive\done.txt"
}
AfterEach {
Remove-Item -Force $errFile -ErrorAction SilentlyContinue
Remove-Item -Force $doneFile -ErrorAction SilentlyContinue
}

It "Access-denied test for '<cmdline>" -Skip:(-not $IsWindows) -TestCases @(
# NOTE: ensure the fileNameBase parameter is unique for each test case; it is used to generate a unique error and done file name.
@{cmdline = "Get-Item $protectedPath2"; expectedError = "ItemExistsUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetItemCommand"}
@{cmdline = "Get-ChildItem $protectedPath"; expectedError = "DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand"}
@{cmdline = "New-Item -Type File -Path $newItemPath"; expectedError = "NewItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.NewItemCommand"}
Expand All @@ -228,14 +223,23 @@ Describe "Basic FileSystem Provider Tests" -Tags "CI" {
) {
param ($cmdline, $expectedError)

# generate a filename to use for the error and done text files to avoid test output collision
# when a timeout occurs waiting for powershell.
$fileNameBase = ([string] $cmdline).GetHashCode().ToString()
$errFile = Join-Path -Path $TestDrive -ChildPath "$fileNameBase.error.txt"
$doneFile = Join-Path -Path $TestDrive -Childpath "$fileNameBase.done.txt"

# Seed the error file with text indicating a timeout waiting for the command.
"Test timeout waiting for $cmdLine" | Set-Content -Path $errFile

runas.exe /trustlevel:0x20000 "$powershell -nop -c try { $cmdline -ErrorAction Stop } catch { `$_.FullyQualifiedErrorId | Out-File $errFile }; New-Item -Type File -Path $doneFile"

$startTime = Get-Date
while (((Get-Date) - $startTime).TotalSeconds -lt 10 -and -not (Test-Path $doneFile))
while (((Get-Date) - $startTime).TotalSeconds -lt 15 -and -not (Test-Path $doneFile))
{
Start-Sleep -Milliseconds 100
}

$errFile | Should Exist
$err = Get-Content $errFile
$err | Should Be $expectedError
}
Expand Down

0 comments on commit 6a78e30

Please sign in to comment.