Skip to content

Commit

Permalink
xSQLServerAlwaysOnAvailabilityGroup\Replica: Utilize the Get-PrimaryR…
Browse files Browse the repository at this point in the history
…eplicaServerObject helper function (#842)

- Changes to xSQLServerAlwaysOnAvailabilityGroup
  - Use the Get-PrimaryReplicaServerObject helper function.
- Changes to xSQLServerAlwaysOnAvailabilityGroupReplica
  - Use the Get-PrimaryReplicaServerObject helper function.
  • Loading branch information
randomnote1 authored and johlju committed Sep 22, 2017
1 parent 9ce87a8 commit 5d99b62
Show file tree
Hide file tree
Showing 5 changed files with 680 additions and 305 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@
- EndpointURL
- SQLServerNetName
- Version
- Use the Get-PrimaryReplicaServerObject helper function
- Changes to xSQLServerAlwaysOnAvailabilityGroupReplica
- Fixed the formatting for the AvailabilityGroupNotFound error.
- Added the following read-only properties to the schema ([issue #477](https://github.com/PowerShell/xSQLServer/issues/477))
- EndpointPort
- EndpointURL
- Use the new helper function "Test-ClusterPermissions".
- Use the Get-PrimaryReplicaServerObject helper function
- Changes to xSQLServerHelper
- Fixed Connect-SQL by ensuring the Status property returns 'Online' prior to
returning the SQL Server object ([issue #333](https://github.com/PowerShell/xSQLServer/issues/333)).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,11 +368,8 @@ function Set-TargetResource
else
{
# Make sure we're communicating with the primary replica
if ( $availabilityGroup.LocalReplicaRole -ne 'Primary' )
{
$primaryServerObject = Connect-SQL -SQLServer $availabilityGroup.PrimaryReplicaServerName
$availabilityGroup = $primaryServerObject.AvailabilityGroups[$Name]
}
$primaryServerObject = Get-PrimaryReplicaServerObject -ServerObject $serverObject -AvailabilityGroup $availabilityGroup
$availabilityGroup = $primaryServerObject.AvailabilityGroups[$Name]

if ( $AutomatedBackupPreference -ne $availabilityGroup.AutomatedBackupPreference )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ function Set-TargetResource
{
while ( $availabilityGroup.LocalReplicaRole -ne 'Primary' )
{
$primaryServerObject = Connect-SQL -SQLServer $availabilityGroup.PrimaryReplicaServerName
$primaryServerObject = Get-PrimaryReplicaServerObject -ServerObject $serverObject -AvailabilityGroup $availabilityGroup
$availabilityGroup = $primaryServerObject.AvailabilityGroups[$AvailabilityGroupName]
}
}
Expand Down Expand Up @@ -378,13 +378,8 @@ function Set-TargetResource
if ( $primaryReplicaAvailabilityGroup )
{
# Make sure the instance defined as the primary replica in the parameters is actually the primary replica
if ( $primaryReplicaAvailabilityGroup.LocalReplicaRole -ne 'Primary' )
{
New-VerboseMessage -Message "The instance '$PrimaryReplicaSQLServer\$PrimaryReplicaSQLInstanceName' is not currently the primary replica. Connecting to '$($primaryReplicaAvailabilityGroup.PrimaryReplicaServerName)'."

$primaryReplicaServerObject = Connect-SQL -SQLServer $primaryReplicaAvailabilityGroup.PrimaryReplicaServerName
$primaryReplicaAvailabilityGroup = $primaryReplicaServerObject.AvailabilityGroups[$AvailabilityGroupName]
}
$primaryReplicaServerObject = Get-PrimaryReplicaServerObject -ServerObject $primaryReplicaServerObject -AvailabilityGroup $primaryReplicaAvailabilityGroup
$availabilityGroup = $primaryReplicaServerObject.AvailabilityGroups[$AvailabilityGroupName]

# Build the endpoint URL
$endpointUrl = "TCP://$($EndpointHostName):$($endpointPort)"
Expand Down
102 changes: 97 additions & 5 deletions Tests/Unit/MSFT_xSQLServerAlwaysOnAvailabilityGroup.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,16 @@ try

[Parameter()]
[string]
$SQLInstanceName
$SQLInstanceName,

# The following two parameters are used to mock Get-PrimaryReplicaServerObject
[Parameter()]
[Microsoft.SqlServer.Management.Smo.AvailabilityGroup]
$AvailabilityGroup,

[Parameter()]
[Microsoft.SqlServer.Management.Smo.Server]
$ServerObject
)

# Define the server object
Expand Down Expand Up @@ -188,7 +197,16 @@ try

[Parameter()]
[string]
$SQLInstanceName
$SQLInstanceName,

# The following two parameters are used to mock Get-PrimaryReplicaServerObject
[Parameter()]
[Microsoft.SqlServer.Management.Smo.AvailabilityGroup]
$AvailabilityGroup,

[Parameter()]
[Microsoft.SqlServer.Management.Smo.Server]
$ServerObject
)

# Define the server object
Expand Down Expand Up @@ -477,6 +495,12 @@ try
Mock -CommandName Connect-SQL -MockWith $mockConnectSqlServer2 -Verifiable -ParameterFilter {
$SQLServer -eq $mockServer2Name
}
Mock -CommandName Get-PrimaryReplicaServerObject -MockWith $mockConnectSqlServer1 -Verifiable -ParameterFilter {
$AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer1Name
}
Mock -CommandName Get-PrimaryReplicaServerObject -MockWith $mockConnectSqlServer2 -Verifiable -ParameterFilter {
$AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer2Name
}
Mock -CommandName Invoke-Query -MockWith {} -Verifiable
Mock -CommandName Import-SQLPSModule -MockWith {} -Verifiable
Mock -CommandName New-SqlAvailabilityGroup {} -Verifiable -ParameterFilter {
Expand Down Expand Up @@ -660,6 +684,12 @@ try
Assert-MockCalled -CommandName Connect-SQL -Scope It -Times 0 -Exactly -ParameterFilter {
$SQLServer -eq $mockServer2Name
}
Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter {
$AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer1Name
}
Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter {
$AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer2Name
}
Assert-MockCalled -CommandName Import-SQLPSModule -Scope It -Times 1 -Exactly
Assert-MockCalled -CommandName New-SqlAvailabilityReplica -Scope It -Times 0 -Exactly -ParameterFilter {
$Name -eq $mockAvailabilityGroupReplicaAbsentName
Expand Down Expand Up @@ -692,6 +722,12 @@ try
Assert-MockCalled -CommandName Connect-SQL -Scope It -Times 0 -Exactly -ParameterFilter {
$SQLServer -eq $mockServer2Name
}
Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter {
$AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer1Name
}
Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter {
$AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer2Name
}
Assert-MockCalled -CommandName Import-SQLPSModule -Scope It -Times 1 -Exactly
Assert-MockCalled -CommandName New-SqlAvailabilityReplica -Scope It -Times 0 -Exactly -ParameterFilter {
$Name -eq $mockAvailabilityGroupReplicaAbsentName
Expand Down Expand Up @@ -724,6 +760,12 @@ try
Assert-MockCalled -CommandName Connect-SQL -Scope It -Times 0 -Exactly -ParameterFilter {
$SQLServer -eq $mockServer2Name
}
Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter {
$AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer1Name
}
Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter {
$AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer2Name
}
Assert-MockCalled -CommandName Import-SQLPSModule -Scope It -Times 1 -Exactly
Assert-MockCalled -CommandName New-SqlAvailabilityReplica -Scope It -Times 0 -Exactly -ParameterFilter {
$Name -eq $mockAvailabilityGroupReplicaAbsentName
Expand Down Expand Up @@ -763,6 +805,12 @@ try
Assert-MockCalled -CommandName Connect-SQL -Scope It -Times 1 -Exactly -ParameterFilter {
$SQLServer -eq $mockServer2Name
}
Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter {
$AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer1Name
}
Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter {
$AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer2Name
}
Assert-MockCalled -CommandName Import-SQLPSModule -Scope It -Times 1 -Exactly
Assert-MockCalled -CommandName New-SqlAvailabilityReplica -Scope It -Times 1 -Exactly -ParameterFilter {
$Name -eq $mockAvailabilityGroupReplicaAbsentName
Expand Down Expand Up @@ -801,6 +849,12 @@ try
Assert-MockCalled -CommandName Connect-SQL -Scope It -Times 0 -Exactly -ParameterFilter {
$SQLServer -eq $mockServer2Name
}
Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter {
$AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer1Name
}
Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter {
$AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer2Name
}
Assert-MockCalled -CommandName Import-SQLPSModule -Scope It -Times 1 -Exactly
Assert-MockCalled -CommandName New-SqlAvailabilityReplica -Scope It -Times 0 -Exactly -ParameterFilter {
$Name -eq $mockAvailabilityGroupReplicaAbsentName
Expand Down Expand Up @@ -850,6 +904,12 @@ try
Assert-MockCalled -CommandName Connect-SQL -Scope It -Times 0 -Exactly -ParameterFilter {
$SQLServer -eq $mockServer2Name
}
Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter {
$AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer1Name
}
Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter {
$AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer2Name
}
Assert-MockCalled -CommandName Import-SQLPSModule -Scope It -Times 1 -Exactly
Assert-MockCalled -CommandName New-SqlAvailabilityReplica -Scope It -Times 0 -Exactly -ParameterFilter {
$Name -eq $mockAvailabilityGroupReplicaAbsentName
Expand Down Expand Up @@ -894,6 +954,12 @@ try
Assert-MockCalled -CommandName Connect-SQL -Scope It -Times 0 -Exactly -ParameterFilter {
$SQLServer -eq $mockServer2Name
}
Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter {
$AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer1Name
}
Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter {
$AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer2Name
}
Assert-MockCalled -CommandName Import-SQLPSModule -Scope It -Times 1 -Exactly
Assert-MockCalled -CommandName New-SqlAvailabilityReplica -Scope It -Times 0 -Exactly -ParameterFilter {
$Name -eq $mockAvailabilityGroupReplicaAbsentName
Expand Down Expand Up @@ -939,6 +1005,12 @@ try
Assert-MockCalled -CommandName Connect-SQL -Scope It -Times 0 -Exactly -ParameterFilter {
$SQLServer -eq $mockServer2Name
}
Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter {
$AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer1Name
}
Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter {
$AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer2Name
}
Assert-MockCalled -CommandName Import-SQLPSModule -Scope It -Times 1 -Exactly
Assert-MockCalled -CommandName New-SqlAvailabilityReplica -Scope It -Times 0 -Exactly -ParameterFilter {
$Name -eq $mockAvailabilityGroupReplicaAbsentName
Expand Down Expand Up @@ -975,12 +1047,18 @@ try

{ Set-TargetResource @currentTestParameters } | Should Not Throw

Assert-MockCalled -CommandName Connect-SQL -Scope It -Times 1 -Exactly -ParameterFilter {
Assert-MockCalled -CommandName Connect-SQL -Scope It -Times 0 -Exactly -ParameterFilter {
$SQLServer -eq $mockServer1Name
}
Assert-MockCalled -CommandName Connect-SQL -Scope It -Times 1 -Exactly -ParameterFilter {
$SQLServer -eq $mockServer2Name
}
Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 1 -Exactly -ParameterFilter {
$AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer1Name
}
Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter {
$AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer2Name
}
Assert-MockCalled -CommandName Import-SQLPSModule -Scope It -Times 1 -Exactly
Assert-MockCalled -CommandName New-SqlAvailabilityReplica -Scope It -Times 0 -Exactly -ParameterFilter {
$Name -eq $mockAvailabilityGroupReplicaAbsentName
Expand Down Expand Up @@ -1021,7 +1099,8 @@ try
# Define how many times each mock should be called
if ( $currentTestParameters.Ensure -eq 'Present' )
{
$assertConnectSql = 2
$assertConnectSql = 1
$assertGetPrimaryReplicaServerObject = 1
$assertRemoveSqlAvailabilityGroup = 0
$assertTestClusterPermissions = 1

Expand All @@ -1047,6 +1126,7 @@ try
else
{
$assertConnectSql = 1
$assertGetPrimaryReplicaServerObject = 0
$assertRemoveSqlAvailabilityGroup = 1
$assertTestClusterPermissions = 0
$assertUpdateAvailabilityGroup = 0
Expand All @@ -1061,6 +1141,12 @@ try
Assert-MockCalled -CommandName Connect-SQL -Scope It -Times 0 -Exactly -ParameterFilter {
$SQLServer -eq $mockServer2Name
}
Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time $assertGetPrimaryReplicaServerObject -Exactly -ParameterFilter {
$AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer1Name
}
Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter {
$AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer2Name
}
Assert-MockCalled -CommandName Import-SQLPSModule -Scope It -Times 1 -Exactly
Assert-MockCalled -CommandName New-SqlAvailabilityReplica -Scope It -Times 0 -Exactly -ParameterFilter {
$Name -eq $mockAvailabilityGroupReplicaAbsentName
Expand Down Expand Up @@ -1099,12 +1185,18 @@ try

{ Set-TargetResource @mockResourceParameters } | Should Not Throw

Assert-MockCalled -CommandName Connect-SQL -Scope It -Times 2 -Exactly -ParameterFilter {
Assert-MockCalled -CommandName Connect-SQL -Scope It -Times 1 -Exactly -ParameterFilter {
$SQLServer -eq $mockServer1Name
}
Assert-MockCalled -CommandName Connect-SQL -Scope It -Times 0 -Exactly -ParameterFilter {
$SQLServer -eq $mockServer2Name
}
Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 1 -Exactly -ParameterFilter {
$AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer1Name
}
Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter {
$AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer2Name
}
Assert-MockCalled -CommandName Import-SQLPSModule -Scope It -Times 1 -Exactly
Assert-MockCalled -CommandName New-SqlAvailabilityReplica -Scope It -Times 0 -Exactly -ParameterFilter {
$Name -eq $mockAvailabilityGroupReplicaAbsentName
Expand Down
Loading

0 comments on commit 5d99b62

Please sign in to comment.