Skip to content

Commit

Permalink
Changes to xSQLServerAlwaysOnService
Browse files Browse the repository at this point in the history
- Casting the result of the property IsHadrEnabled to [System.Boolean] so that
  $null is never returned, which resulted in an exception (issue dsccommunity#763).
  • Loading branch information
johlju committed Aug 16, 2017
1 parent e0d6ec1 commit b4b3f40
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 60 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
- Added examples (issue #633)
- 1-EnableAlwaysOn.ps1
- 2-DisableAlwaysOn.ps1
- 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 xSQLServerScript
- Fixed PS Script Analyzer errors ([issue #728](https://github.com/PowerShell/xSQLServer/issues/728))
- Changes to xSQLServerSetup
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 ($isAlwaysOnEnabled -eq $null)
{
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

0 comments on commit b4b3f40

Please sign in to comment.