2018-02-13 18:23:53 +01:00
|
|
|
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
# Licensed under the MIT License.
|
2017-01-10 05:51:19 +01:00
|
|
|
function Wait-UntilTrue
|
2016-08-25 00:58:05 +02:00
|
|
|
{
|
|
|
|
[CmdletBinding()]
|
2017-01-16 22:31:14 +01:00
|
|
|
param (
|
2016-08-25 00:58:05 +02:00
|
|
|
[ScriptBlock]$sb,
|
|
|
|
[int]$TimeoutInMilliseconds = 10000,
|
|
|
|
[int]$IntervalInMilliseconds = 1000
|
|
|
|
)
|
|
|
|
# Get the current time
|
|
|
|
$startTime = [DateTime]::Now
|
|
|
|
|
|
|
|
# Loop until the script block evaluates to true
|
|
|
|
while (-not ($sb.Invoke())) {
|
2017-09-12 22:05:47 +02:00
|
|
|
# If the timeout period has passed, return false
|
|
|
|
if (([DateTime]::Now - $startTime).TotalMilliseconds -gt $timeoutInMilliseconds) {
|
2016-08-25 00:58:05 +02:00
|
|
|
return $false
|
|
|
|
}
|
2017-09-12 22:05:47 +02:00
|
|
|
# Sleep for the specified interval
|
|
|
|
Start-Sleep -Milliseconds $intervalInMilliseconds
|
2016-08-25 00:58:05 +02:00
|
|
|
}
|
|
|
|
return $true
|
|
|
|
}
|
2016-08-30 00:00:26 +02:00
|
|
|
|
2017-09-12 22:05:47 +02:00
|
|
|
function Wait-FileToBePresent
|
|
|
|
{
|
|
|
|
[CmdletBinding()]
|
|
|
|
param (
|
|
|
|
[string]$File,
|
|
|
|
[int]$TimeoutInSeconds = 10,
|
|
|
|
[int]$IntervalInMilliseconds = 100
|
|
|
|
)
|
|
|
|
|
|
|
|
Wait-UntilTrue -sb { Test-Path $File } -TimeoutInMilliseconds ($TimeoutInSeconds*1000) -IntervalInMilliseconds $IntervalInMilliseconds > $null
|
|
|
|
}
|
|
|
|
|
2016-08-30 00:00:26 +02:00
|
|
|
function Test-IsElevated
|
|
|
|
{
|
|
|
|
$IsElevated = $False
|
|
|
|
if ( $IsWindows ) {
|
|
|
|
# on Windows we can determine whether we're executing in an
|
|
|
|
# elevated context
|
|
|
|
$identity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
|
|
|
|
$windowsPrincipal = new-object 'Security.Principal.WindowsPrincipal' $identity
|
2017-01-16 22:31:14 +01:00
|
|
|
if ($windowsPrincipal.IsInRole("Administrators") -eq 1)
|
|
|
|
{
|
|
|
|
$IsElevated = $true
|
|
|
|
}
|
2016-08-30 00:00:26 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
# on Linux, tests run via sudo will generally report "root" for whoami
|
|
|
|
if ( (whoami) -match "root" ) {
|
|
|
|
$IsElevated = $true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $IsElevated
|
|
|
|
}
|
2016-09-17 01:54:48 +02:00
|
|
|
#This function follows the pester naming convention
|
|
|
|
function ShouldBeErrorId
|
|
|
|
{
|
|
|
|
param([Parameter(ValueFromPipeline, Mandatory)]
|
|
|
|
[ScriptBlock]
|
|
|
|
$sb,
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2016-09-17 01:54:48 +02:00
|
|
|
[Parameter(Mandatory, Position=0)]
|
|
|
|
[string]
|
|
|
|
$FullyQualifiedErrorId)
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2017-05-17 20:09:27 +02:00
|
|
|
& $sb | Out-Null
|
|
|
|
Throw "No Exception!"
|
2016-09-17 01:54:48 +02:00
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
2017-05-17 20:09:27 +02:00
|
|
|
$_.FullyQualifiedErrorId | Should Be $FullyQualifiedErrorId | Out-Null
|
|
|
|
# Write the exception to output that allow us to check later other properies of the exception
|
|
|
|
Write-Output $_
|
2016-09-17 01:54:48 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-30 00:00:26 +02:00
|
|
|
|
2017-09-30 01:28:15 +02:00
|
|
|
function Get-RandomFileName
|
|
|
|
{
|
|
|
|
[System.IO.Path]::GetFileNameWithoutExtension([IO.Path]::GetRandomFileName())
|
|
|
|
}
|
2017-11-01 18:59:41 +01:00
|
|
|
|
|
|
|
#
|
|
|
|
# Testhook setting functions
|
|
|
|
# note these manipulate private data in the PowerShell engine which will
|
|
|
|
# enable us to not actually alter the system or mock returned data
|
|
|
|
#
|
|
|
|
$SCRIPT:TesthookType = [system.management.automation.internal.internaltesthooks]
|
|
|
|
function Test-TesthookIsSet
|
|
|
|
{
|
|
|
|
param (
|
|
|
|
[ValidateNotNullOrEmpty()]
|
|
|
|
[Parameter(Mandatory=$true)]
|
|
|
|
$testhookName
|
|
|
|
)
|
|
|
|
try {
|
|
|
|
return ${Script:TesthookType}.GetField($testhookName, "NonPublic,Static").GetValue($null)
|
|
|
|
}
|
|
|
|
catch {
|
|
|
|
# fall through
|
|
|
|
}
|
|
|
|
return $false
|
|
|
|
}
|
|
|
|
|
|
|
|
function Enable-Testhook
|
|
|
|
{
|
|
|
|
param (
|
|
|
|
[ValidateNotNullOrEmpty()]
|
|
|
|
[Parameter(Mandatory=$true)]
|
|
|
|
$testhookName
|
|
|
|
)
|
|
|
|
${Script:TesthookType}::SetTestHook($testhookName, $true)
|
|
|
|
}
|
|
|
|
|
|
|
|
function Disable-Testhook
|
|
|
|
{
|
|
|
|
param (
|
|
|
|
[ValidateNotNullOrEmpty()]
|
|
|
|
[Parameter(Mandatory=$true)]
|
|
|
|
$testhookName
|
|
|
|
)
|
|
|
|
${Script:TesthookType}::SetTestHook($testhookName, $false)
|
|
|
|
}
|
|
|
|
|
|
|
|
function Set-TesthookResult
|
|
|
|
{
|
|
|
|
param (
|
|
|
|
[ValidateNotNullOrEmpty()]
|
|
|
|
[Parameter(Mandatory=$true)]
|
|
|
|
$testhookName,
|
|
|
|
[ValidateNotNullOrEmpty()]
|
|
|
|
[Parameter(Mandatory=$true)]
|
|
|
|
$value
|
|
|
|
)
|
|
|
|
${Script:TesthookType}::SetTestHook($testhookName, $value)
|
|
|
|
}
|