Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xSQLServerAlwaysOnService: IsHadrEnabled is cast to [System.Boolean] #768

Merged
merged 1 commit into from
Aug 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
- 1-EnableAlwaysOn.ps1
- 2-DisableAlwaysOn.ps1
- Fixed PS Script Analyzer errors ([issue #724](https://github.com/PowerShell/xSQLServer/issues/724))
- Casting the result of the property IsHadrEnabled to [System.Boolean] so that
$null is never returned, which resulted in an exception ([issue #763](https://github.com/PowerShell/xFailOverCluster/issues/763)).
- Changes to xSQLServerDatabasePermission
- Fixed PS Script Analyzer errors ([issue #725](https://github.com/PowerShell/xSQLServer/issues/725))
- Changes to xSQLServerScript
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function Get-TargetResource

$sqlServerObject = Connect-SQL -SQLServer $SQLServer -SQLInstanceName $SQLInstanceName

$isAlwaysOnEnabled = $sqlServerObject.IsHadrEnabled
$isAlwaysOnEnabled = [System.Boolean] $sqlServerObject.IsHadrEnabled
if ($isAlwaysOnEnabled -eq $true)
{
$statusString = 'enabled'
Expand All @@ -51,27 +51,6 @@ function Get-TargetResource
{
$statusString = 'disabled'
}
else
{
# This is a validation test for issue #519.
try
{
if ($null -eq $isAlwaysOnEnabled)
{
throw 'Server.IsHadrEnabled was set to $null.'
}
else
{
$statusString = $isAlwaysOnEnabled

throw 'Server.IsHadrEnabled was set to unexpected value.'
}
}
catch
{
throw New-TerminatingError -ErrorType UnexpectedAlwaysOnStatus -FormatArgs $statusString -ErrorCategory InvalidResult -InnerException $_.Exception
}
}

New-VerboseMessage -Message ( 'SQL Always On is {0} on "{1}\{2}".' -f $statusString, $SQLServer, $SQLInstanceName )

Expand Down
43 changes: 5 additions & 38 deletions Tests/Unit/MSFT_xSQLServerAlwaysOnService.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -43,59 +43,48 @@ $enableHadrNamedInstance = @{
try
{
Describe "$($script:DSCResourceName)\Get-TargetResource" {

Context 'When HADR is disabled' {

Mock -CommandName Connect-SQL -MockWith {
return New-Object PSObject -Property @{
IsHadrEnabled = $false
}
} -ModuleName $script:DSCResourceName -Verifiable

It 'Should return that HADR is disabled' {

# Get the current state
$result = Get-TargetResource @enableHadr

$result.IsHadrEnabled | Should Be $false

Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Connect-SQL -Scope It -Times 1 -Exactly
}

It 'Should return that HADR is disabled' {

# Get the current state
$result = Get-TargetResource @enableHadrNamedInstance

$result.IsHadrEnabled | Should Be $false

Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Connect-SQL -Scope It -Times 1 -Exactly
}
}

Context 'When HADR is enabled' {

Mock -CommandName Connect-SQL -MockWith {
return New-Object PSObject -Property @{
IsHadrEnabled = $true
}
} -ModuleName $script:DSCResourceName -Verifiable

It 'Should return that HADR is enabled' {

# Get the current state
$result = Get-TargetResource @enableHadr

$result.IsHadrEnabled | Should Be $true

Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Connect-SQL -Scope It -Times 1 -Exactly
}

It 'Should return that HADR is enabled' {

# Get the current state
$result = Get-TargetResource @enableHadrNamedInstance

$result.IsHadrEnabled | Should Be $true

Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Connect-SQL -Scope It -Times 1 -Exactly
Expand All @@ -111,47 +100,30 @@ try
} -ModuleName $script:DSCResourceName -Verifiable

It 'Should fail with the correct error message' {
# Regression test for issue #519
{ Get-TargetResource @enableHadr } | Should Not Throw 'Index operation failed; the array index evaluated to null'
{ Get-TargetResource @enableHadr } | Should Throw 'The status of property Server.IsHadrEnabled was neither $true or $false. Status is ''''. InnerException: Server.IsHadrEnabled was set to $null.'
Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Connect-SQL -Scope It -Times 2 -Exactly
}
}

Context 'When Server.IsHadrEnabled returns unexpected value' {
Mock -CommandName Connect-SQL -MockWith {
return New-Object PSObject -Property @{
IsHadrEnabled = 'UnknownStatus'
}
} -ModuleName $script:DSCResourceName -Verifiable
$result = Get-TargetResource @enableHadr
$result.IsHadrEnabled | Should Be $false

It 'Should fail with the correct error message' {
{ Get-TargetResource @enableHadr } | Should Throw 'The status of property Server.IsHadrEnabled was neither $true or $false. Status is ''UnknownStatus''. InnerException: Server.IsHadrEnabled was set to unexpected value.'
Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Connect-SQL -Scope It -Times 1 -Exactly
Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Connect-SQL -Scope It -Times 2 -Exactly
}
}
}

Describe "$($script:DSCResourceName)\Set-TargetResource" {

# Loading stub cmdlets
# Loading stub cmdlets
Import-Module -Name ( Join-Path -Path ( Join-Path -Path $PSScriptRoot -ChildPath Stubs ) -ChildPath SQLPSStub.psm1 ) -Force

Mock -CommandName Disable-SqlAlwaysOn -MockWith {} -ModuleName $script:DSCResourceName

Mock -CommandName Enable-SqlAlwaysOn -MockWith {} -ModuleName $script:DSCResourceName

Mock -CommandName Import-SQLPSModule -MockWith {} -ModuleName $script:DSCResourceName

Mock -CommandName New-TerminatingError { $ErrorType } -ModuleName $script:DSCResourceName

Mock -CommandName New-VerboseMessage -MockWith {} -ModuleName $script:DSCResourceName

Mock -CommandName Restart-SqlService -MockWith {} -ModuleName $script:DSCResourceName -Verifiable

Context 'When HADR is not in the desired state' {

It 'Should enable SQL Always On when Ensure is set to Present' {

Mock -CommandName Connect-SQL -MockWith {
return New-Object PSObject -Property @{
IsHadrEnabled = $true
Expand All @@ -166,7 +138,6 @@ try
}

It 'Should disable SQL Always On when Ensure is set to Absent' {

Mock -CommandName Connect-SQL -MockWith {
return New-Object PSObject -Property @{
IsHadrEnabled = $false
Expand All @@ -181,7 +152,6 @@ try
}

It 'Should enable SQL Always On on a named instance when Ensure is set to Present' {

Mock -CommandName Connect-SQL -MockWith {
return New-Object PSObject -Property @{
IsHadrEnabled = $true
Expand All @@ -196,7 +166,6 @@ try
}

It 'Should disable SQL Always On on a named instance when Ensure is set to Absent' {

Mock -CommandName Connect-SQL -MockWith {
return New-Object PSObject -Property @{
IsHadrEnabled = $false
Expand All @@ -211,7 +180,6 @@ try
}

It 'Should throw the correct error message when Ensure is set to Present, but IsHadrEnabled is $false' {

Mock -CommandName Connect-SQL -MockWith {
return New-Object PSObject -Property @{
IsHadrEnabled = $false
Expand All @@ -227,7 +195,6 @@ try
}

It 'Should throw the correct error message when Ensure is set to Absent, but IsHadrEnabled is $true' {

Mock -CommandName Connect-SQL -MockWith {
return New-Object PSObject -Property @{
IsHadrEnabled = $true
Expand Down