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 8e6f18b
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 22 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
91 changes: 91 additions & 0 deletions Untitled-1.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
$integrationTests = @()

$getChildItemParameters = @{
Path = '.'
Recurse = $true
}

$getChildItemParameters['Filter'] = '*.Tests.ps1'
$testFiles = Get-ChildItem @getChildItemParameters

foreach ($testFile in $testFiles)
{
$integrationTests += @(
[PSCustomObject] @{
TestPath = $testFile.FullName
OrderNumber = $null
}
)
}

($integrationTests | Where-Object -FilterScript {
$_.TestPath -match "DSCResource.Tests"
}).OrderNumber = 0

function Get-DscIntegrationTestOrderNumber
{
[CmdletBinding()]
[OutputType([System.UInt32])]
param
(
[Parameter()]
[ValidateNotNullOrEmpty()]
[String]
$Path
)

$scriptBlockAst = [System.Management.Automation.Language.Parser]::ParseFile($Path, [ref] $null, [ref] $null)

$findAllHashTablesFilter = {
$args[0] -is [System.Management.Automation.Language.HashtableAst]
}

[System.Management.Automation.Language.Ast[]] $hashTablesAst = $scriptBlockAst.FindAll( $findAllHashTablesFilter, $true )

$integrationTestOrderNumber = $null

foreach ($hashTableAst in $hashTablesAst)
{
foreach ($keyValuePair in $hashTableAst.KeyValuePairs.GetEnumerator())
{
if ($keyValuePair.Item1.Extent.Text -eq 'DscIntegrationTestOrder')
{
return [System.UInt32] $keyValuePair.Item2.Extent.Text
}
}
}

return $integrationTestOrderNumber
}

$getChildItemParameters['Filter'] = '*.config.ps1'
$integrationTestConfigurationFiles = Get-ChildItem @getChildItemParameters

foreach ($integrationTestConfigurationFile in $integrationTestConfigurationFiles)
{
$orderNumber = Get-DscIntegrationTestOrderNumber -Path $integrationTestConfigurationFile.FullName
if ($orderNumber)
{
$testFileName = $integrationTestConfigurationFile.FullName -replace '.config.ps1','.Integration.Tests.ps1'

# If the test must run in order, add the order number to the test in the array.
($integrationTests | Where-Object -FilterScript {
$_.TestPath -eq $testFileName
}).OrderNumber = $orderNumber
}
}

$integrationTestOrder = @()

# First add all integration tests which must run in order.
$integrationTestOrder += $integrationTests | Where-Object -FilterScript {
$null -ne $_.OrderNumber
} | Sort-Object -Property 'OrderNumber'

# Then add the rest of the integration tests.
$integrationTestOrder += $integrationTests | Where-Object -FilterScript {
$null -eq $_.OrderNumber
}

$integrationTestOrder
#$integrationTestOrder.TestPath

0 comments on commit 8e6f18b

Please sign in to comment.