Files
PowerShell/test/powershell/Modules/Microsoft.PowerShell.Management/Restart-Computer.Tests.ps1
T
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

100 lines
4.1 KiB
PowerShell

# the testhook for restart-computer is the same as for stop-computer
$restartTesthookName = "TestStopComputer"
$restartTesthookResultName = "TestStopComputerResults"
$DefaultResultValue = 0
try
{
# set up for testing
$PSDefaultParameterValues["it:skip"] = ! $IsWindows
Enable-Testhook -testhookName $restartTesthookName
Describe "Restart-Computer" -Tag Feature {
# if we throw in BeforeEach, the test will fail and the restart will not be called
BeforeEach {
if ( ! (Test-TesthookIsSet -testhookName $restartTesthookName) ) {
throw "Testhook '${restartTesthookName}' is not set"
}
}
AfterEach {
Set-TesthookResult -testhookName $restartTesthookResultName -value $defaultResultValue
}
It "Should restart the local computer" {
Set-TesthookResult -testhookName $restartTesthookResultName -value $defaultResultValue
Restart-Computer -ErrorAction Stop| Should BeNullOrEmpty
}
It "Should support -computer parameter" {
Set-TesthookResult -testhookName $restartTesthookResultName -value $defaultResultValue
$computerNames = "localhost","${env:COMPUTERNAME}"
Restart-Computer -Computer $computerNames -ErrorAction Stop| Should BeNullOrEmpty
}
It "Should support WsmanAuthentication types" {
$authChoices = "Default","Basic","Negotiate","CredSSP","Digest","Kerberos"
foreach ( $auth in $authChoices ) {
Restart-Computer -WsmanAuthentication $auth | Should BeNullOrEmpty
}
}
# this requires setting a test hook, so we wrap the execution with try/finally of the
# set operation. Internally, we want to suppress the progress, so
# that is also wrapped in try/finally
It "Should wait for a remote system" {
try
{
Enable-Testhook -testhookname TestWaitStopComputer
$timeout = 3
try
{
$pPref = $ProgressPreference
$ProgressPreference="SilentlyContinue"
$duration = Measure-Command {
Restart-Computer -computer localhost -Wait -Timeout $timeout -ErrorAction stop | Should BeNullOrEmpty
}
}
finally
{
$ProgressPreference=$pPref
}
$duration.TotalSeconds | Should BeGreaterThan $timeout
}
finally
{
Disable-Testhook -testhookname TestWaitStopComputer
}
}
Context "Restart-Computer Error Conditions" {
It "Should return the proper error when it occurs" {
Set-TesthookResult -testhookName $restartTesthookResultName -value 0x300000
Restart-Computer -ErrorVariable RestartError 2>$null
$RestartError.Exception.Message | Should match 0x300000
}
It "Should produce an error when 'Delay' is specified" {
{ Restart-Computer -Delay 30 } | ShouldBeErrorId "RestartComputerInvalidParameter,Microsoft.PowerShell.Commands.RestartComputerCommand"
}
It "Should not support timeout on localhost" {
Set-TesthookResult -testhookName $restartTesthookResultName -value $defaultResultValue
{ Restart-Computer -timeout 3 -ErrorAction Stop } | ShouldBeErrorId "RestartComputerInvalidParameter,Microsoft.PowerShell.Commands.RestartComputerCommand"
}
It "Should not support timeout on localhost" {
Set-TesthookResult -testhookName $restartTesthookResultName -value $defaultResultValue
{ Restart-Computer -timeout 3 -ErrorAction Stop } | ShouldBeErrorId "RestartComputerInvalidParameter,Microsoft.PowerShell.Commands.RestartComputerCommand"
}
}
}
}
finally
{
$PSDefaultParameterValues.Remove("it:skip")
Disable-Testhook -testhookName $restartTesthookName
Set-TesthookResult -testhookName $restartTesthookResultName -value 0
}