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

xSQLServerAlwaysOnAvailabilityGroup\Replica: Utilize the Get-PrimaryReplicaServerObject helper function #842

Merged
merged 4 commits into from
Sep 22, 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 @@ -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