PowerShell/test/tools/Modules/HelpersCommon/HelpersCommon.psm1
James Truher [MSFT] d43b2cf958 Remove DCOM support from *-Computer cmdlets (#5277)
Since DCOM is not supported in corefx there was a great deal of dead code in the computer cmdlets.
This PR removes all vestiges of DCOM support from:

- Rename-Computer
- Restart-Computer
- Stop-Computer

removing about 4500 lines of dead code. Also, tests are updated to provide more complete coverage.
I also removed test-connection completely to make way for @iSazonov upcoming PR to improve coverage in tests, I created some test hook code which will test the cmdlet code without actually calling the WMI method to restart/rename/stop the system
2017-11-01 10:59:41 -07:00

140 lines
3.5 KiB
PowerShell

function Wait-UntilTrue
{
[CmdletBinding()]
param (
[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())) {
# If the timeout period has passed, return false
if (([DateTime]::Now - $startTime).TotalMilliseconds -gt $timeoutInMilliseconds) {
return $false
}
# Sleep for the specified interval
Start-Sleep -Milliseconds $intervalInMilliseconds
}
return $true
}
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
}
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
if ($windowsPrincipal.IsInRole("Administrators") -eq 1)
{
$IsElevated = $true
}
}
else {
# on Linux, tests run via sudo will generally report "root" for whoami
if ( (whoami) -match "root" ) {
$IsElevated = $true
}
}
return $IsElevated
}
#This function follows the pester naming convention
function ShouldBeErrorId
{
param([Parameter(ValueFromPipeline, Mandatory)]
[ScriptBlock]
$sb,
[Parameter(Mandatory, Position=0)]
[string]
$FullyQualifiedErrorId)
try
{
& $sb | Out-Null
Throw "No Exception!"
}
catch
{
$_.FullyQualifiedErrorId | Should Be $FullyQualifiedErrorId | Out-Null
# Write the exception to output that allow us to check later other properies of the exception
Write-Output $_
}
}
function Get-RandomFileName
{
[System.IO.Path]::GetFileNameWithoutExtension([IO.Path]::GetRandomFileName())
}
#
# 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)
}