From 5d99b628e5fb8d4e0e3ebddf22f59a4ee67c136c Mon Sep 17 00:00:00 2001 From: Dan Reist Date: Fri, 22 Sep 2017 14:22:54 -0400 Subject: [PATCH] xSQLServerAlwaysOnAvailabilityGroup\Replica: Utilize the Get-PrimaryReplicaServerObject helper function (#842) - Changes to xSQLServerAlwaysOnAvailabilityGroup - Use the Get-PrimaryReplicaServerObject helper function. - Changes to xSQLServerAlwaysOnAvailabilityGroupReplica - Use the Get-PrimaryReplicaServerObject helper function. --- CHANGELOG.md | 2 + ...T_xSQLServerAlwaysOnAvailabilityGroup.psm1 | 7 +- ...erverAlwaysOnAvailabilityGroupReplica.psm1 | 11 +- ...LServerAlwaysOnAvailabilityGroup.Tests.ps1 | 102 ++- ...AlwaysOnAvailabilityGroupReplica.Tests.ps1 | 863 ++++++++++++------ 5 files changed, 680 insertions(+), 305 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 13eba0ad4..28cdc9da1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)). diff --git a/DSCResources/MSFT_xSQLServerAlwaysOnAvailabilityGroup/MSFT_xSQLServerAlwaysOnAvailabilityGroup.psm1 b/DSCResources/MSFT_xSQLServerAlwaysOnAvailabilityGroup/MSFT_xSQLServerAlwaysOnAvailabilityGroup.psm1 index b7433cb40..c5ef58f91 100644 --- a/DSCResources/MSFT_xSQLServerAlwaysOnAvailabilityGroup/MSFT_xSQLServerAlwaysOnAvailabilityGroup.psm1 +++ b/DSCResources/MSFT_xSQLServerAlwaysOnAvailabilityGroup/MSFT_xSQLServerAlwaysOnAvailabilityGroup.psm1 @@ -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 ) { diff --git a/DSCResources/MSFT_xSQLServerAlwaysOnAvailabilityGroupReplica/MSFT_xSQLServerAlwaysOnAvailabilityGroupReplica.psm1 b/DSCResources/MSFT_xSQLServerAlwaysOnAvailabilityGroupReplica/MSFT_xSQLServerAlwaysOnAvailabilityGroupReplica.psm1 index cbf687f23..f0e6fb388 100644 --- a/DSCResources/MSFT_xSQLServerAlwaysOnAvailabilityGroupReplica/MSFT_xSQLServerAlwaysOnAvailabilityGroupReplica.psm1 +++ b/DSCResources/MSFT_xSQLServerAlwaysOnAvailabilityGroupReplica/MSFT_xSQLServerAlwaysOnAvailabilityGroupReplica.psm1 @@ -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] } } @@ -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)" diff --git a/Tests/Unit/MSFT_xSQLServerAlwaysOnAvailabilityGroup.Tests.ps1 b/Tests/Unit/MSFT_xSQLServerAlwaysOnAvailabilityGroup.Tests.ps1 index e9b4aecb7..8ce8a2cb9 100644 --- a/Tests/Unit/MSFT_xSQLServerAlwaysOnAvailabilityGroup.Tests.ps1 +++ b/Tests/Unit/MSFT_xSQLServerAlwaysOnAvailabilityGroup.Tests.ps1 @@ -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 @@ -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 @@ -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 { @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -1047,6 +1126,7 @@ try else { $assertConnectSql = 1 + $assertGetPrimaryReplicaServerObject = 0 $assertRemoveSqlAvailabilityGroup = 1 $assertTestClusterPermissions = 0 $assertUpdateAvailabilityGroup = 0 @@ -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 @@ -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 diff --git a/Tests/Unit/MSFT_xSQLServerAlwaysOnAvailabilityGroupReplica.Tests.ps1 b/Tests/Unit/MSFT_xSQLServerAlwaysOnAvailabilityGroupReplica.Tests.ps1 index 484704015..f16bc2bb8 100644 --- a/Tests/Unit/MSFT_xSQLServerAlwaysOnAvailabilityGroupReplica.Tests.ps1 +++ b/Tests/Unit/MSFT_xSQLServerAlwaysOnAvailabilityGroupReplica.Tests.ps1 @@ -3,9 +3,9 @@ # Unit Test Template Version: 1.2.0 $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or ` - (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) ) + (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) ) { - & git @('clone','https://github.com/PowerShell/DscResource.Tests.git',(Join-Path -Path $script:moduleRoot -ChildPath '\DSCResource.Tests\')) + & git @('clone', 'https://github.com/PowerShell/DscResource.Tests.git', (Join-Path -Path $script:moduleRoot -ChildPath '\DSCResource.Tests\')) } Import-Module -Name ( Join-Path -Path ( Join-Path -Path $PSScriptRoot -ChildPath Stubs ) -ChildPath SQLPSStub.psm1 ) -Force -Global @@ -19,9 +19,12 @@ $TestEnvironment = Initialize-TestEnvironment ` #endregion HEADER -function Invoke-TestSetup {} +function Invoke-TestSetup +{ +} -function Invoke-TestCleanup { +function Invoke-TestCleanup +{ Restore-TestEnvironment -TestEnvironment $TestEnvironment } @@ -34,126 +37,126 @@ try #region parameter mocks - $mockSqlServer = 'Server1' - $mockSqlInstanceName = 'MSSQLSERVER' - $mockPrimaryReplicaSQLServer = 'Server2' - $mockPrimaryReplicaSQLInstanceName = 'MSSQLSERVER' - $mockAvailabilityGroupName = 'AG_AllServers' - $mockAvailabilityGroupReplicaName = $mockSqlServer - $mockEnsure = 'Present' - $mockAvailabilityMode = 'AsynchronousCommit' - $mockBackupPriority = 50 - $mockConnectionModeInPrimaryRole = 'AllowAllConnections' - $mockConnectionModeInSecondaryRole = 'AllowNoConnections' - $mockEndpointHostName = $mockSqlServer - $mockFailoverMode = 'Manual' - $mockReadOnlyRoutingConnectionUrl = "TCP://$($mockSqlServer).domain.com:1433" - $mockReadOnlyRoutingList = @($mockSqlServer) + $mockSqlServer = 'Server1' + $mockSqlInstanceName = 'MSSQLSERVER' + $mockPrimaryReplicaSQLServer = 'Server2' + $mockPrimaryReplicaSQLInstanceName = 'MSSQLSERVER' + $mockAvailabilityGroupName = 'AG_AllServers' + $mockAvailabilityGroupReplicaName = $mockSqlServer + $mockEnsure = 'Present' + $mockAvailabilityMode = 'AsynchronousCommit' + $mockBackupPriority = 50 + $mockConnectionModeInPrimaryRole = 'AllowAllConnections' + $mockConnectionModeInSecondaryRole = 'AllowNoConnections' + $mockEndpointHostName = $mockSqlServer + $mockFailoverMode = 'Manual' + $mockReadOnlyRoutingConnectionUrl = "TCP://$($mockSqlServer).domain.com:1433" + $mockReadOnlyRoutingList = @($mockSqlServer) #endregion #region server mock variables - $mockServer1Name = 'Server1' - $mockServer1NetName = $mockServer1Name - $mockServer1IsHadrEnabled = $true - $mockServer1ServiceName = 'MSSQLSERVER' + $mockServer1Name = 'Server1' + $mockServer1NetName = $mockServer1Name + $mockServer1IsHadrEnabled = $true + $mockServer1ServiceName = 'MSSQLSERVER' - $mockServer2Name = 'Server2' - $mockServer2NetName = $mockServer1Name - $mockServer2IsHadrEnabled = $true - $mockServer2ServiceName = $mockServer1ServiceName + $mockServer2Name = 'Server2' + $mockServer2NetName = $mockServer1Name + $mockServer2IsHadrEnabled = $true + $mockServer2ServiceName = $mockServer1ServiceName - $mockServer3Name = 'Server3' - $mockServer3NetName = $mockServer3Name - $mockServer3IsHadrEnabled = $true - $mockServer3ServiceName = $mockServer1ServiceName + $mockServer3Name = 'Server3' + $mockServer3NetName = $mockServer3Name + $mockServer3IsHadrEnabled = $true + $mockServer3ServiceName = $mockServer1ServiceName #endregion #region Login mocks - $mockLogins = @{} # Will be dynamically set during tests + $mockLogins = @{} # Will be dynamically set during tests - $mockNtServiceClusSvcName = 'NT SERVICE\ClusSvc' - $mockNtAuthoritySystemName = 'NT AUTHORITY\SYSTEM' + $mockNtServiceClusSvcName = 'NT SERVICE\ClusSvc' + $mockNtAuthoritySystemName = 'NT AUTHORITY\SYSTEM' - $mockAllLoginsAbsent = @{} + $mockAllLoginsAbsent = @{} - $mockNtServiceClusSvcPresent = @{ - $mockNtServiceClusSvcName = ( New-Object -TypeName Microsoft.SqlServer.Management.Smo.Login($mockSqlServer,$mockNtServiceClusSvcName) ) - } + $mockNtServiceClusSvcPresent = @{ + $mockNtServiceClusSvcName = ( New-Object -TypeName Microsoft.SqlServer.Management.Smo.Login($mockSqlServer, $mockNtServiceClusSvcName) ) + } - $mockNtAuthoritySystemPresent = @{ - $mockNtAuthoritySystemName = ( New-Object -TypeName Microsoft.SqlServer.Management.Smo.Login($mockSqlServer,$mockNtAuthoritySystemName) ) - } + $mockNtAuthoritySystemPresent = @{ + $mockNtAuthoritySystemName = ( New-Object -TypeName Microsoft.SqlServer.Management.Smo.Login($mockSqlServer, $mockNtAuthoritySystemName) ) + } - $mockAllLoginsPresent = @{ - $mockNtServiceClusSvcName = ( New-Object -TypeName Microsoft.SqlServer.Management.Smo.Login($mockSqlServer,$mockNtServiceClusSvcName) ) - $mockNtAuthoritySystemName = ( New-Object -TypeName Microsoft.SqlServer.Management.Smo.Login($mockSqlServer,$mockNtAuthoritySystemName) ) - } + $mockAllLoginsPresent = @{ + $mockNtServiceClusSvcName = ( New-Object -TypeName Microsoft.SqlServer.Management.Smo.Login($mockSqlServer, $mockNtServiceClusSvcName) ) + $mockNtAuthoritySystemName = ( New-Object -TypeName Microsoft.SqlServer.Management.Smo.Login($mockSqlServer, $mockNtAuthoritySystemName) ) + } #endregion #region Endpoint mock variables - $mockEndpointPort = 5022 + $mockEndpointPort = 5022 #endregion Endpoint mock variables #region Availability Group mock variables - $mockAvailabilityGroup1Name = 'AG_AllServers' - $mockAvailabilityGroup1PrimaryReplicaServer = $mockServer2Name + $mockAvailabilityGroup1Name = 'AG_AllServers' + $mockAvailabilityGroup1PrimaryReplicaServer = $mockServer2Name - $mockAvailabilityGroup2Name = 'AG_PrimaryOnServer2' - $mockAvailabilityGroup2PrimaryReplicaServer = $mockServer2Name + $mockAvailabilityGroup2Name = 'AG_PrimaryOnServer2' + $mockAvailabilityGroup2PrimaryReplicaServer = $mockServer2Name - $mockAvailabilityGroup3Name = 'AG_PrimaryOnServer3' - $mockAvailabilityGroup3PrimaryReplicaServer = $mockServer3Name + $mockAvailabilityGroup3Name = 'AG_PrimaryOnServer3' + $mockAvailabilityGroup3PrimaryReplicaServer = $mockServer3Name #endregion #region Availability Group Replica mock variables - $mockAlternateEndpointPort = $false - $mockAlternateEndpointProtocol = $false - - $mockAvailabilityGroupReplica1Name = $mockServer1Name - $mockAvailabilityGroupReplica1AvailabilityMode = 'AsynchronousCommit' - $mockAvailabilityGroupReplica1BackupPriority = 50 - $mockAvailabilityGroupReplica1ConnectionModeInPrimaryRole = 'AllowAllConnections' - $mockAvailabilityGroupReplica1ConnectionModeInSecondaryRole = 'AllowNoConnections' - $mockAvailabilityGroupReplica1EndpointProtocol = 'TCP' - $mockAvailabilityGroupReplica1EndpointPort = $mockEndpointPort - $mockAvailabilityGroupReplica1EndpointUrl = "$($mockAvailabilityGroupReplica1EndpointProtocol)://$($mockServer1Name):$($mockAvailabilityGroupReplica1EndpointPort)" - $mockAvailabilityGroupReplica1FailoverMode = 'Manual' - $mockAvailabilityGroupReplica1ReadOnlyRoutingConnectionUrl = "TCP://$($mockServer1Name).domain.com:1433" - $mockAvailabilityGroupReplica1ReadOnlyRoutingList = @($mockServer1Name) - - $mockAvailabilityGroupReplica2Name = $mockServer2Name - $mockAvailabilityGroupReplica2AvailabilityMode = 'AsynchronousCommit' - $mockAvailabilityGroupReplica2BackupPriority = 50 - $mockAvailabilityGroupReplica2ConnectionModeInPrimaryRole = 'AllowAllConnections' - $mockAvailabilityGroupReplica2ConnectionModeInSecondaryRole = 'AllowNoConnections' - $mockAvailabilityGroupReplica2EndpointProtocol = 'TCP' - $mockAvailabilityGroupReplica2EndpointPort = $mockEndpointPort - $mockAvailabilityGroupReplica2EndpointUrl = "$($mockAvailabilityGroupReplica2EndpointProtocol)://$($mockServer2Name):$($mockAvailabilityGroupReplica2EndpointPort)" - $mockAvailabilityGroupReplica2FailoverMode = 'Manual' - $mockAvailabilityGroupReplica2ReadOnlyRoutingConnectionUrl = "TCP://$($mockServer2Name).domain.com:1433" - $mockAvailabilityGroupReplica2ReadOnlyRoutingList = @($mockServer2Name) - - $mockAvailabilityGroupReplica3Name = $mockServer3Name - $mockAvailabilityGroupReplica3AvailabilityMode = 'AsynchronousCommit' - $mockAvailabilityGroupReplica3BackupPriority = 50 - $mockAvailabilityGroupReplica3ConnectionModeInPrimaryRole = 'AllowAllConnections' - $mockAvailabilityGroupReplica3ConnectionModeInSecondaryRole = 'AllowNoConnections' - $mockAvailabilityGroupReplica3EndpointProtocol = 'TCP' - $mockAvailabilityGroupReplica3EndpointPort = $mockEndpointPort - $mockAvailabilityGroupReplica3EndpointUrl = "$($mockAvailabilityGroupReplica3EndpointProtocol)://$($mockServer3Name):$($mockAvailabilityGroupReplica3EndpointPort)" - $mockAvailabilityGroupReplica3FailoverMode = 'Manual' - $mockAvailabilityGroupReplica3ReadOnlyRoutingConnectionUrl = "TCP://$($mockServer3Name).domain.com:1433" - $mockAvailabilityGroupReplica3ReadOnlyRoutingList = @($mockServer3Name) + $mockAlternateEndpointPort = $false + $mockAlternateEndpointProtocol = $false + + $mockAvailabilityGroupReplica1Name = $mockServer1Name + $mockAvailabilityGroupReplica1AvailabilityMode = 'AsynchronousCommit' + $mockAvailabilityGroupReplica1BackupPriority = 50 + $mockAvailabilityGroupReplica1ConnectionModeInPrimaryRole = 'AllowAllConnections' + $mockAvailabilityGroupReplica1ConnectionModeInSecondaryRole = 'AllowNoConnections' + $mockAvailabilityGroupReplica1EndpointProtocol = 'TCP' + $mockAvailabilityGroupReplica1EndpointPort = $mockEndpointPort + $mockAvailabilityGroupReplica1EndpointUrl = "$($mockAvailabilityGroupReplica1EndpointProtocol)://$($mockServer1Name):$($mockAvailabilityGroupReplica1EndpointPort)" + $mockAvailabilityGroupReplica1FailoverMode = 'Manual' + $mockAvailabilityGroupReplica1ReadOnlyRoutingConnectionUrl = "TCP://$($mockServer1Name).domain.com:1433" + $mockAvailabilityGroupReplica1ReadOnlyRoutingList = @($mockServer1Name) + + $mockAvailabilityGroupReplica2Name = $mockServer2Name + $mockAvailabilityGroupReplica2AvailabilityMode = 'AsynchronousCommit' + $mockAvailabilityGroupReplica2BackupPriority = 50 + $mockAvailabilityGroupReplica2ConnectionModeInPrimaryRole = 'AllowAllConnections' + $mockAvailabilityGroupReplica2ConnectionModeInSecondaryRole = 'AllowNoConnections' + $mockAvailabilityGroupReplica2EndpointProtocol = 'TCP' + $mockAvailabilityGroupReplica2EndpointPort = $mockEndpointPort + $mockAvailabilityGroupReplica2EndpointUrl = "$($mockAvailabilityGroupReplica2EndpointProtocol)://$($mockServer2Name):$($mockAvailabilityGroupReplica2EndpointPort)" + $mockAvailabilityGroupReplica2FailoverMode = 'Manual' + $mockAvailabilityGroupReplica2ReadOnlyRoutingConnectionUrl = "TCP://$($mockServer2Name).domain.com:1433" + $mockAvailabilityGroupReplica2ReadOnlyRoutingList = @($mockServer2Name) + + $mockAvailabilityGroupReplica3Name = $mockServer3Name + $mockAvailabilityGroupReplica3AvailabilityMode = 'AsynchronousCommit' + $mockAvailabilityGroupReplica3BackupPriority = 50 + $mockAvailabilityGroupReplica3ConnectionModeInPrimaryRole = 'AllowAllConnections' + $mockAvailabilityGroupReplica3ConnectionModeInSecondaryRole = 'AllowNoConnections' + $mockAvailabilityGroupReplica3EndpointProtocol = 'TCP' + $mockAvailabilityGroupReplica3EndpointPort = $mockEndpointPort + $mockAvailabilityGroupReplica3EndpointUrl = "$($mockAvailabilityGroupReplica3EndpointProtocol)://$($mockServer3Name):$($mockAvailabilityGroupReplica3EndpointPort)" + $mockAvailabilityGroupReplica3FailoverMode = 'Manual' + $mockAvailabilityGroupReplica3ReadOnlyRoutingConnectionUrl = "TCP://$($mockServer3Name).domain.com:1433" + $mockAvailabilityGroupReplica3ReadOnlyRoutingList = @($mockServer3Name) #endregion @@ -168,7 +171,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 ) # Mock the server object @@ -215,16 +227,16 @@ try if ( $mockAlternateEndpointPort ) { - $mockAvailabilityGroupReplica1.EndpointUrl = $mockAvailabilityGroupReplica1EndpointUrl.Replace($mockAvailabilityGroupReplica1EndpointPort,'1234') - $mockAvailabilityGroupReplica2.EndpointUrl = $mockAvailabilityGroupReplica2EndpointUrl.Replace($mockAvailabilityGroupReplica2EndpointPort,'1234') - $mockAvailabilityGroupReplica3.EndpointUrl = $mockAvailabilityGroupReplica3EndpointUrl.Replace($mockAvailabilityGroupReplica3EndpointPort,'1234') + $mockAvailabilityGroupReplica1.EndpointUrl = $mockAvailabilityGroupReplica1EndpointUrl.Replace($mockAvailabilityGroupReplica1EndpointPort, '1234') + $mockAvailabilityGroupReplica2.EndpointUrl = $mockAvailabilityGroupReplica2EndpointUrl.Replace($mockAvailabilityGroupReplica2EndpointPort, '1234') + $mockAvailabilityGroupReplica3.EndpointUrl = $mockAvailabilityGroupReplica3EndpointUrl.Replace($mockAvailabilityGroupReplica3EndpointPort, '1234') } if ( $mockAlternateEndpointProtocol ) { - $mockAvailabilityGroupReplica1.EndpointUrl = $mockAvailabilityGroupReplica1EndpointUrl.Replace($mockAvailabilityGroupReplica1EndpointProtocol,'UDP') - $mockAvailabilityGroupReplica2.EndpointUrl = $mockAvailabilityGroupReplica2EndpointUrl.Replace($mockAvailabilityGroupReplica2EndpointProtocol,'UDP') - $mockAvailabilityGroupReplica3.EndpointUrl = $mockAvailabilityGroupReplica3EndpointUrl.Replace($mockAvailabilityGroupReplica3EndpointProtocol,'UDP') + $mockAvailabilityGroupReplica1.EndpointUrl = $mockAvailabilityGroupReplica1EndpointUrl.Replace($mockAvailabilityGroupReplica1EndpointProtocol, 'UDP') + $mockAvailabilityGroupReplica2.EndpointUrl = $mockAvailabilityGroupReplica2EndpointUrl.Replace($mockAvailabilityGroupReplica2EndpointProtocol, 'UDP') + $mockAvailabilityGroupReplica3.EndpointUrl = $mockAvailabilityGroupReplica3EndpointUrl.Replace($mockAvailabilityGroupReplica3EndpointProtocol, 'UDP') } # Mock the availability groups @@ -262,7 +274,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 ) # Mock the server object @@ -274,52 +295,52 @@ try $mockServerObject.ServiceName = $mockServer2ServiceName #region Mock the availability group replicas - $mockAvailabilityGroupReplica1 = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityReplica - $mockAvailabilityGroupReplica1.AvailabilityMode = $mockAvailabilityGroupReplica1AvailabilityMode - $mockAvailabilityGroupReplica1.BackupPriority = $mockAvailabilityGroupReplica1BackupPriority - $mockAvailabilityGroupReplica1.ConnectionModeInPrimaryRole = $mockAvailabilityGroupReplica1ConnectionModeInPrimaryRole - $mockAvailabilityGroupReplica1.ConnectionModeInSecondaryRole = $mockAvailabilityGroupReplica1ConnectionModeInSecondaryRole - $mockAvailabilityGroupReplica1.EndpointUrl = $mockAvailabilityGroupReplica1EndpointUrl - $mockAvailabilityGroupReplica1.FailoverMode = $mockAvailabilityGroupReplica1FailoverMode - $mockAvailabilityGroupReplica1.Name = $mockAvailabilityGroupReplica1Name - $mockAvailabilityGroupReplica1.ReadOnlyRoutingConnectionUrl = $mockAvailabilityGroupReplica1ReadOnlyRoutingConnectionUrl - $mockAvailabilityGroupReplica1.ReadOnlyRoutingList = $mockAvailabilityGroupReplica1ReadOnlyRoutingList - - $mockAvailabilityGroupReplica2 = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityReplica - $mockAvailabilityGroupReplica2.AvailabilityMode = $mockAvailabilityGroupReplica2AvailabilityMode - $mockAvailabilityGroupReplica2.BackupPriority = $mockAvailabilityGroupReplica2BackupPriority - $mockAvailabilityGroupReplica2.ConnectionModeInPrimaryRole = $mockAvailabilityGroupReplica2ConnectionModeInPrimaryRole - $mockAvailabilityGroupReplica2.ConnectionModeInSecondaryRole = $mockAvailabilityGroupReplica2ConnectionModeInSecondaryRole - $mockAvailabilityGroupReplica2.EndpointUrl = $mockAvailabilityGroupReplica2EndpointUrl - $mockAvailabilityGroupReplica2.FailoverMode = $mockAvailabilityGroupReplica2FailoverMode - $mockAvailabilityGroupReplica2.Name = $mockAvailabilityGroupReplica2Name - $mockAvailabilityGroupReplica2.ReadOnlyRoutingConnectionUrl = $mockAvailabilityGroupReplica2ReadOnlyRoutingConnectionUrl - $mockAvailabilityGroupReplica2.ReadOnlyRoutingList = $mockAvailabilityGroupReplica2ReadOnlyRoutingList - - $mockAvailabilityGroupReplica3 = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityReplica - $mockAvailabilityGroupReplica3.AvailabilityMode = $mockAvailabilityGroupReplica3AvailabilityMode - $mockAvailabilityGroupReplica3.BackupPriority = $mockAvailabilityGroupReplica3BackupPriority - $mockAvailabilityGroupReplica3.ConnectionModeInPrimaryRole = $mockAvailabilityGroupReplica3ConnectionModeInPrimaryRole - $mockAvailabilityGroupReplica3.ConnectionModeInSecondaryRole = $mockAvailabilityGroupReplica3ConnectionModeInSecondaryRole - $mockAvailabilityGroupReplica3.EndpointUrl = $mockAvailabilityGroupReplica3EndpointUrl - $mockAvailabilityGroupReplica3.FailoverMode = $mockAvailabilityGroupReplica3FailoverMode - $mockAvailabilityGroupReplica3.Name = $mockAvailabilityGroupReplica3Name - $mockAvailabilityGroupReplica3.ReadOnlyRoutingConnectionUrl = $mockAvailabilityGroupReplica3ReadOnlyRoutingConnectionUrl - $mockAvailabilityGroupReplica3.ReadOnlyRoutingList = $mockAvailabilityGroupReplica3ReadOnlyRoutingList + $mockAvailabilityGroupReplica1 = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityReplica + $mockAvailabilityGroupReplica1.AvailabilityMode = $mockAvailabilityGroupReplica1AvailabilityMode + $mockAvailabilityGroupReplica1.BackupPriority = $mockAvailabilityGroupReplica1BackupPriority + $mockAvailabilityGroupReplica1.ConnectionModeInPrimaryRole = $mockAvailabilityGroupReplica1ConnectionModeInPrimaryRole + $mockAvailabilityGroupReplica1.ConnectionModeInSecondaryRole = $mockAvailabilityGroupReplica1ConnectionModeInSecondaryRole + $mockAvailabilityGroupReplica1.EndpointUrl = $mockAvailabilityGroupReplica1EndpointUrl + $mockAvailabilityGroupReplica1.FailoverMode = $mockAvailabilityGroupReplica1FailoverMode + $mockAvailabilityGroupReplica1.Name = $mockAvailabilityGroupReplica1Name + $mockAvailabilityGroupReplica1.ReadOnlyRoutingConnectionUrl = $mockAvailabilityGroupReplica1ReadOnlyRoutingConnectionUrl + $mockAvailabilityGroupReplica1.ReadOnlyRoutingList = $mockAvailabilityGroupReplica1ReadOnlyRoutingList + + $mockAvailabilityGroupReplica2 = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityReplica + $mockAvailabilityGroupReplica2.AvailabilityMode = $mockAvailabilityGroupReplica2AvailabilityMode + $mockAvailabilityGroupReplica2.BackupPriority = $mockAvailabilityGroupReplica2BackupPriority + $mockAvailabilityGroupReplica2.ConnectionModeInPrimaryRole = $mockAvailabilityGroupReplica2ConnectionModeInPrimaryRole + $mockAvailabilityGroupReplica2.ConnectionModeInSecondaryRole = $mockAvailabilityGroupReplica2ConnectionModeInSecondaryRole + $mockAvailabilityGroupReplica2.EndpointUrl = $mockAvailabilityGroupReplica2EndpointUrl + $mockAvailabilityGroupReplica2.FailoverMode = $mockAvailabilityGroupReplica2FailoverMode + $mockAvailabilityGroupReplica2.Name = $mockAvailabilityGroupReplica2Name + $mockAvailabilityGroupReplica2.ReadOnlyRoutingConnectionUrl = $mockAvailabilityGroupReplica2ReadOnlyRoutingConnectionUrl + $mockAvailabilityGroupReplica2.ReadOnlyRoutingList = $mockAvailabilityGroupReplica2ReadOnlyRoutingList + + $mockAvailabilityGroupReplica3 = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityReplica + $mockAvailabilityGroupReplica3.AvailabilityMode = $mockAvailabilityGroupReplica3AvailabilityMode + $mockAvailabilityGroupReplica3.BackupPriority = $mockAvailabilityGroupReplica3BackupPriority + $mockAvailabilityGroupReplica3.ConnectionModeInPrimaryRole = $mockAvailabilityGroupReplica3ConnectionModeInPrimaryRole + $mockAvailabilityGroupReplica3.ConnectionModeInSecondaryRole = $mockAvailabilityGroupReplica3ConnectionModeInSecondaryRole + $mockAvailabilityGroupReplica3.EndpointUrl = $mockAvailabilityGroupReplica3EndpointUrl + $mockAvailabilityGroupReplica3.FailoverMode = $mockAvailabilityGroupReplica3FailoverMode + $mockAvailabilityGroupReplica3.Name = $mockAvailabilityGroupReplica3Name + $mockAvailabilityGroupReplica3.ReadOnlyRoutingConnectionUrl = $mockAvailabilityGroupReplica3ReadOnlyRoutingConnectionUrl + $mockAvailabilityGroupReplica3.ReadOnlyRoutingList = $mockAvailabilityGroupReplica3ReadOnlyRoutingList #endregion Mock the availability group replicas if ( $mockAlternateEndpointPort ) { - $mockAvailabilityGroupReplica1.EndpointUrl = $mockAvailabilityGroupReplica1EndpointUrl.Replace($mockAvailabilityGroupReplica1EndpointPort,'1234') - $mockAvailabilityGroupReplica2.EndpointUrl = $mockAvailabilityGroupReplica2EndpointUrl.Replace($mockAvailabilityGroupReplica2EndpointPort,'1234') - $mockAvailabilityGroupReplica3.EndpointUrl = $mockAvailabilityGroupReplica3EndpointUrl.Replace($mockAvailabilityGroupReplica3EndpointPort,'1234') + $mockAvailabilityGroupReplica1.EndpointUrl = $mockAvailabilityGroupReplica1EndpointUrl.Replace($mockAvailabilityGroupReplica1EndpointPort, '1234') + $mockAvailabilityGroupReplica2.EndpointUrl = $mockAvailabilityGroupReplica2EndpointUrl.Replace($mockAvailabilityGroupReplica2EndpointPort, '1234') + $mockAvailabilityGroupReplica3.EndpointUrl = $mockAvailabilityGroupReplica3EndpointUrl.Replace($mockAvailabilityGroupReplica3EndpointPort, '1234') } if ( $mockAlternateEndpointProtocol ) { - $mockAvailabilityGroupReplica1.EndpointUrl = $mockAvailabilityGroupReplica1EndpointUrl.Replace($mockAvailabilityGroupReplica1EndpointProtocol,'UDP') - $mockAvailabilityGroupReplica2.EndpointUrl = $mockAvailabilityGroupReplica2EndpointUrl.Replace($mockAvailabilityGroupReplica2EndpointProtocol,'UDP') - $mockAvailabilityGroupReplica3.EndpointUrl = $mockAvailabilityGroupReplica3EndpointUrl.Replace($mockAvailabilityGroupReplica3EndpointProtocol,'UDP') + $mockAvailabilityGroupReplica1.EndpointUrl = $mockAvailabilityGroupReplica1EndpointUrl.Replace($mockAvailabilityGroupReplica1EndpointProtocol, 'UDP') + $mockAvailabilityGroupReplica2.EndpointUrl = $mockAvailabilityGroupReplica2EndpointUrl.Replace($mockAvailabilityGroupReplica2EndpointProtocol, 'UDP') + $mockAvailabilityGroupReplica3.EndpointUrl = $mockAvailabilityGroupReplica3EndpointUrl.Replace($mockAvailabilityGroupReplica3EndpointProtocol, 'UDP') } # Mock the availability groups @@ -373,7 +394,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 ) # Mock the server object @@ -385,52 +415,52 @@ try $mockServerObject.ServiceName = $mockServer3ServiceName #region Mock the availability group replicas - $mockAvailabilityGroupReplica1 = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityReplica - $mockAvailabilityGroupReplica1.AvailabilityMode = $mockAvailabilityGroupReplica1AvailabilityMode - $mockAvailabilityGroupReplica1.BackupPriority = $mockAvailabilityGroupReplica1BackupPriority - $mockAvailabilityGroupReplica1.ConnectionModeInPrimaryRole = $mockAvailabilityGroupReplica1ConnectionModeInPrimaryRole - $mockAvailabilityGroupReplica1.ConnectionModeInSecondaryRole = $mockAvailabilityGroupReplica1ConnectionModeInSecondaryRole - $mockAvailabilityGroupReplica1.EndpointUrl = $mockAvailabilityGroupReplica1EndpointUrl - $mockAvailabilityGroupReplica1.FailoverMode = $mockAvailabilityGroupReplica1FailoverMode - $mockAvailabilityGroupReplica1.Name = $mockAvailabilityGroupReplica1Name - $mockAvailabilityGroupReplica1.ReadOnlyRoutingConnectionUrl = $mockAvailabilityGroupReplica1ReadOnlyRoutingConnectionUrl - $mockAvailabilityGroupReplica1.ReadOnlyRoutingList = $mockAvailabilityGroupReplica1ReadOnlyRoutingList - - $mockAvailabilityGroupReplica2 = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityReplica - $mockAvailabilityGroupReplica2.AvailabilityMode = $mockAvailabilityGroupReplica2AvailabilityMode - $mockAvailabilityGroupReplica2.BackupPriority = $mockAvailabilityGroupReplica2BackupPriority - $mockAvailabilityGroupReplica2.ConnectionModeInPrimaryRole = $mockAvailabilityGroupReplica2ConnectionModeInPrimaryRole - $mockAvailabilityGroupReplica2.ConnectionModeInSecondaryRole = $mockAvailabilityGroupReplica2ConnectionModeInSecondaryRole - $mockAvailabilityGroupReplica2.EndpointUrl = $mockAvailabilityGroupReplica2EndpointUrl - $mockAvailabilityGroupReplica2.FailoverMode = $mockAvailabilityGroupReplica2FailoverMode - $mockAvailabilityGroupReplica2.Name = $mockAvailabilityGroupReplica2Name - $mockAvailabilityGroupReplica2.ReadOnlyRoutingConnectionUrl = $mockAvailabilityGroupReplica2ReadOnlyRoutingConnectionUrl - $mockAvailabilityGroupReplica2.ReadOnlyRoutingList = $mockAvailabilityGroupReplica2ReadOnlyRoutingList - - $mockAvailabilityGroupReplica3 = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityReplica - $mockAvailabilityGroupReplica3.AvailabilityMode = $mockAvailabilityGroupReplica3AvailabilityMode - $mockAvailabilityGroupReplica3.BackupPriority = $mockAvailabilityGroupReplica3BackupPriority - $mockAvailabilityGroupReplica3.ConnectionModeInPrimaryRole = $mockAvailabilityGroupReplica3ConnectionModeInPrimaryRole - $mockAvailabilityGroupReplica3.ConnectionModeInSecondaryRole = $mockAvailabilityGroupReplica3ConnectionModeInSecondaryRole - $mockAvailabilityGroupReplica3.EndpointUrl = $mockAvailabilityGroupReplica3EndpointUrl - $mockAvailabilityGroupReplica3.FailoverMode = $mockAvailabilityGroupReplica3FailoverMode - $mockAvailabilityGroupReplica3.Name = $mockAvailabilityGroupReplica3Name - $mockAvailabilityGroupReplica3.ReadOnlyRoutingConnectionUrl = $mockAvailabilityGroupReplica3ReadOnlyRoutingConnectionUrl - $mockAvailabilityGroupReplica3.ReadOnlyRoutingList = $mockAvailabilityGroupReplica3ReadOnlyRoutingList + $mockAvailabilityGroupReplica1 = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityReplica + $mockAvailabilityGroupReplica1.AvailabilityMode = $mockAvailabilityGroupReplica1AvailabilityMode + $mockAvailabilityGroupReplica1.BackupPriority = $mockAvailabilityGroupReplica1BackupPriority + $mockAvailabilityGroupReplica1.ConnectionModeInPrimaryRole = $mockAvailabilityGroupReplica1ConnectionModeInPrimaryRole + $mockAvailabilityGroupReplica1.ConnectionModeInSecondaryRole = $mockAvailabilityGroupReplica1ConnectionModeInSecondaryRole + $mockAvailabilityGroupReplica1.EndpointUrl = $mockAvailabilityGroupReplica1EndpointUrl + $mockAvailabilityGroupReplica1.FailoverMode = $mockAvailabilityGroupReplica1FailoverMode + $mockAvailabilityGroupReplica1.Name = $mockAvailabilityGroupReplica1Name + $mockAvailabilityGroupReplica1.ReadOnlyRoutingConnectionUrl = $mockAvailabilityGroupReplica1ReadOnlyRoutingConnectionUrl + $mockAvailabilityGroupReplica1.ReadOnlyRoutingList = $mockAvailabilityGroupReplica1ReadOnlyRoutingList + + $mockAvailabilityGroupReplica2 = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityReplica + $mockAvailabilityGroupReplica2.AvailabilityMode = $mockAvailabilityGroupReplica2AvailabilityMode + $mockAvailabilityGroupReplica2.BackupPriority = $mockAvailabilityGroupReplica2BackupPriority + $mockAvailabilityGroupReplica2.ConnectionModeInPrimaryRole = $mockAvailabilityGroupReplica2ConnectionModeInPrimaryRole + $mockAvailabilityGroupReplica2.ConnectionModeInSecondaryRole = $mockAvailabilityGroupReplica2ConnectionModeInSecondaryRole + $mockAvailabilityGroupReplica2.EndpointUrl = $mockAvailabilityGroupReplica2EndpointUrl + $mockAvailabilityGroupReplica2.FailoverMode = $mockAvailabilityGroupReplica2FailoverMode + $mockAvailabilityGroupReplica2.Name = $mockAvailabilityGroupReplica2Name + $mockAvailabilityGroupReplica2.ReadOnlyRoutingConnectionUrl = $mockAvailabilityGroupReplica2ReadOnlyRoutingConnectionUrl + $mockAvailabilityGroupReplica2.ReadOnlyRoutingList = $mockAvailabilityGroupReplica2ReadOnlyRoutingList + + $mockAvailabilityGroupReplica3 = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityReplica + $mockAvailabilityGroupReplica3.AvailabilityMode = $mockAvailabilityGroupReplica3AvailabilityMode + $mockAvailabilityGroupReplica3.BackupPriority = $mockAvailabilityGroupReplica3BackupPriority + $mockAvailabilityGroupReplica3.ConnectionModeInPrimaryRole = $mockAvailabilityGroupReplica3ConnectionModeInPrimaryRole + $mockAvailabilityGroupReplica3.ConnectionModeInSecondaryRole = $mockAvailabilityGroupReplica3ConnectionModeInSecondaryRole + $mockAvailabilityGroupReplica3.EndpointUrl = $mockAvailabilityGroupReplica3EndpointUrl + $mockAvailabilityGroupReplica3.FailoverMode = $mockAvailabilityGroupReplica3FailoverMode + $mockAvailabilityGroupReplica3.Name = $mockAvailabilityGroupReplica3Name + $mockAvailabilityGroupReplica3.ReadOnlyRoutingConnectionUrl = $mockAvailabilityGroupReplica3ReadOnlyRoutingConnectionUrl + $mockAvailabilityGroupReplica3.ReadOnlyRoutingList = $mockAvailabilityGroupReplica3ReadOnlyRoutingList #endregion Mock the availability group replicas if ( $mockAlternateEndpointPort ) { - $mockAvailabilityGroupReplica1.EndpointUrl = $mockAvailabilityGroupReplica1EndpointUrl.Replace($mockAvailabilityGroupReplica1EndpointPort,'1234') - $mockAvailabilityGroupReplica2.EndpointUrl = $mockAvailabilityGroupReplica2EndpointUrl.Replace($mockAvailabilityGroupReplica2EndpointPort,'1234') - $mockAvailabilityGroupReplica3.EndpointUrl = $mockAvailabilityGroupReplica3EndpointUrl.Replace($mockAvailabilityGroupReplica3EndpointPort,'1234') + $mockAvailabilityGroupReplica1.EndpointUrl = $mockAvailabilityGroupReplica1EndpointUrl.Replace($mockAvailabilityGroupReplica1EndpointPort, '1234') + $mockAvailabilityGroupReplica2.EndpointUrl = $mockAvailabilityGroupReplica2EndpointUrl.Replace($mockAvailabilityGroupReplica2EndpointPort, '1234') + $mockAvailabilityGroupReplica3.EndpointUrl = $mockAvailabilityGroupReplica3EndpointUrl.Replace($mockAvailabilityGroupReplica3EndpointPort, '1234') } if ( $mockAlternateEndpointProtocol ) { - $mockAvailabilityGroupReplica1.EndpointUrl = $mockAvailabilityGroupReplica1EndpointUrl.Replace($mockAvailabilityGroupReplica1EndpointProtocol,'UDP') - $mockAvailabilityGroupReplica2.EndpointUrl = $mockAvailabilityGroupReplica2EndpointUrl.Replace($mockAvailabilityGroupReplica2EndpointProtocol,'UDP') - $mockAvailabilityGroupReplica3.EndpointUrl = $mockAvailabilityGroupReplica3EndpointUrl.Replace($mockAvailabilityGroupReplica3EndpointProtocol,'UDP') + $mockAvailabilityGroupReplica1.EndpointUrl = $mockAvailabilityGroupReplica1EndpointUrl.Replace($mockAvailabilityGroupReplica1EndpointProtocol, 'UDP') + $mockAvailabilityGroupReplica2.EndpointUrl = $mockAvailabilityGroupReplica2EndpointUrl.Replace($mockAvailabilityGroupReplica2EndpointProtocol, 'UDP') + $mockAvailabilityGroupReplica3.EndpointUrl = $mockAvailabilityGroupReplica3EndpointUrl.Replace($mockAvailabilityGroupReplica3EndpointProtocol, 'UDP') } # Mock the availability groups @@ -502,10 +532,10 @@ try Describe 'xSQLServerAlwaysOnAvailabilityGroupReplica\Get-TargetResource' { BeforeEach { $getTargetResourceParameters = @{ - Name = $mockAvailabilityGroupReplicaName + Name = $mockAvailabilityGroupReplicaName AvailabilityGroupName = $mockAvailabilityGroupName - SQLServer = $mockSqlServer - SQLInstanceName = $mockSqlInstanceName + SQLServer = $mockSqlServer + SQLInstanceName = $mockSqlInstanceName } $mockDatabaseMirroringEndpoint = $true @@ -575,7 +605,9 @@ try BeforeAll { Mock -CommandName Import-SQLPSModule -MockWith {} -Verifiable - Mock -CommandName New-TerminatingError { $ErrorType } -Verifiable + Mock -CommandName New-TerminatingError { + $ErrorType + } -Verifiable } BeforeEach { @@ -583,12 +615,29 @@ try $mockLogins = $mockNtServiceClusSvcPresent $mockServer1IsHadrEnabled = $true - Mock -CommandName Connect-SQL -MockWith $mockConnectSqlServer1 -Verifiable -ParameterFilter { $SQLServer -eq $mockServer1Name } - Mock -CommandName Connect-SQL -MockWith $mockConnectSqlServer2 -Verifiable -ParameterFilter { $SQLServer -eq $mockServer2Name } - Mock -CommandName Connect-SQL -MockWith $mockConnectSqlServer3 -Verifiable -ParameterFilter { $SQLServer -eq $mockServer3Name } + Mock -CommandName Connect-SQL -MockWith $mockConnectSqlServer1 -Verifiable -ParameterFilter { + $SQLServer -eq $mockServer1Name + } + Mock -CommandName Connect-SQL -MockWith $mockConnectSqlServer2 -Verifiable -ParameterFilter { + $SQLServer -eq $mockServer2Name + } + Mock -CommandName Connect-SQL -MockWith $mockConnectSqlServer3 -Verifiable -ParameterFilter { + $SQLServer -eq $mockServer3Name + } + 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 Get-PrimaryReplicaServerObject -MockWith $mockConnectSqlServer3 -Verifiable -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer3Name + } Mock -CommandName Join-SqlAvailabilityGroup -MockWith {} -Verifiable Mock -CommandName New-SqlAvailabilityReplica {} -Verifiable - Mock -CommandName Test-ClusterPermissions -MockWith { $null } -Verifiable + Mock -CommandName Test-ClusterPermissions -MockWith { + $null + } -Verifiable } Context 'When the desired state is absent' { @@ -599,11 +648,11 @@ try BeforeEach { $setTargetResourceParameters = @{ - Name = $mockSqlServer + Name = $mockSqlServer AvailabilityGroupName = $mockAvailabilityGroupName - SQLServer = $mockSqlServer - SQLInstanceName = $mockSqlInstanceName - Ensure = 'Absent' + SQLServer = $mockSqlServer + SQLInstanceName = $mockSqlInstanceName + Ensure = 'Absent' } } @@ -613,9 +662,24 @@ try { Set-TargetResource @setTargetResourceParameters } | Should Not Throw - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer1Name } -Times 1 -Exactly - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer2Name } -Times 1 -Exactly - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer3Name } -Times 0 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer1Name + } -Times 1 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer2Name + } -Times 0 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer3Name + } -Times 0 -Exactly + Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer1Name + } + Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 1 -Exactly -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer2Name + } + Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer3Name + } Assert-MockCalled -CommandName Import-SQLPSModule -Scope It -Times 1 -Exactly Assert-MockCalled -CommandName Join-SqlAvailabilityGroup -Scope It -Times 0 -Exactly Assert-MockCalled -CommandName New-SqlAvailabilityReplica -Scope It -Times 0 -Exactly @@ -631,9 +695,24 @@ try { Set-TargetResource @setTargetResourceParameters } | Should Throw 'RemoveAvailabilityGroupReplicaFailed' - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer1Name } -Times 1 -Exactly - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer2Name } -Times 1 -Exactly - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer3Name } -Times 0 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer1Name + } -Times 1 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer2Name + } -Times 0 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer3Name + } -Times 0 -Exactly + Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer1Name + } + Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 1 -Exactly -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer2Name + } + Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer3Name + } Assert-MockCalled -CommandName Import-SQLPSModule -Scope It -Times 1 -Exactly Assert-MockCalled -CommandName Join-SqlAvailabilityGroup -Scope It -Times 0 -Exactly Assert-MockCalled -CommandName New-SqlAvailabilityReplica -Scope It -Times 0 -Exactly @@ -653,21 +732,21 @@ try BeforeEach { $setTargetResourceParameters = @{ - Name = $mockSqlServer - AvailabilityGroupName = $mockAvailabilityGroup2Name - SQLServer = $mockSqlServer - SQLInstanceName = $mockSqlInstanceName - PrimaryReplicaSQLServer = $mockPrimaryReplicaSQLServer + Name = $mockSqlServer + AvailabilityGroupName = $mockAvailabilityGroup2Name + SQLServer = $mockSqlServer + SQLInstanceName = $mockSqlInstanceName + PrimaryReplicaSQLServer = $mockPrimaryReplicaSQLServer PrimaryReplicaSQLInstanceName = $mockPrimaryReplicaSQLInstanceName - Ensure = $mockEnsure - AvailabilityMode = $mockAvailabilityMode - BackupPriority = $mockBackupPriority - ConnectionModeInPrimaryRole = $mockConnectionModeInPrimaryRole + Ensure = $mockEnsure + AvailabilityMode = $mockAvailabilityMode + BackupPriority = $mockBackupPriority + ConnectionModeInPrimaryRole = $mockConnectionModeInPrimaryRole ConnectionModeInSecondaryRole = $mockConnectionModeInSecondaryRole - EndpointHostName = $mockEndpointHostName - FailoverMode = $mockFailoverMode - ReadOnlyRoutingConnectionUrl = $mockReadOnlyRoutingConnectionUrl - ReadOnlyRoutingList = $mockReadOnlyRoutingList + EndpointHostName = $mockEndpointHostName + FailoverMode = $mockFailoverMode + ReadOnlyRoutingConnectionUrl = $mockReadOnlyRoutingConnectionUrl + ReadOnlyRoutingList = $mockReadOnlyRoutingList } } @@ -677,9 +756,24 @@ try { Set-TargetResource @setTargetResourceParameters } | Should Throw 'HadrNotEnabled' - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer1Name } -Times 1 -Exactly - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer2Name } -Times 0 -Exactly - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer3Name } -Times 0 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer1Name + } -Times 1 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer2Name + } -Times 0 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer3Name + } -Times 0 -Exactly + 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 Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer3Name + } Assert-MockCalled -CommandName Import-SQLPSModule -Scope It -Times 1 -Exactly Assert-MockCalled -CommandName Join-SqlAvailabilityGroup -Scope It -Times 0 -Exactly Assert-MockCalled -CommandName New-SqlAvailabilityReplica -Scope It -Times 0 -Exactly @@ -697,9 +791,24 @@ try { Set-TargetResource @setTargetResourceParameters } | Should Throw - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer1Name } -Times 1 -Exactly - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer2Name } -Times 0 -Exactly - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer3Name } -Times 0 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer1Name + } -Times 1 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer2Name + } -Times 0 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer3Name + } -Times 0 -Exactly + 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 Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer3Name + } Assert-MockCalled -CommandName Import-SQLPSModule -Scope It -Times 1 -Exactly Assert-MockCalled -CommandName Join-SqlAvailabilityGroup -Scope It -Times 0 -Exactly Assert-MockCalled -CommandName New-SqlAvailabilityReplica -Scope It -Times 0 -Exactly @@ -713,9 +822,24 @@ try { Set-TargetResource @setTargetResourceParameters } | Should Not Throw - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer1Name } -Times 1 -Exactly - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer2Name } -Times 1 -Exactly - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer3Name } -Times 0 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer1Name + } -Times 1 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer2Name + } -Times 1 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer3Name + } -Times 0 -Exactly + Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer1Name + } + Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 1 -Exactly -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer2Name + } + Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer3Name + } Assert-MockCalled -CommandName Import-SQLPSModule -Scope It -Times 1 -Exactly Assert-MockCalled -CommandName Join-SqlAvailabilityGroup -Scope It -Times 1 -Exactly Assert-MockCalled -CommandName New-SqlAvailabilityReplica -Scope It -Times 1 -Exactly @@ -725,15 +849,30 @@ try Assert-MockCalled -CommandName Update-AvailabilityGroupReplica -Scope It -Times 0 -Exactly } - It 'Should throw the correct error (DatabaseMirroringEndpointNotFound) when the database mirroring endpoint is not absent' { + It 'Should throw the correct error (DatabaseMirroringEndpointNotFound) when the database mirroring endpoint is absent' { $mockDatabaseMirroringEndpoint = $false { Set-TargetResource @setTargetResourceParameters } | Should Throw 'DatabaseMirroringEndpointNotFound' - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer1Name } -Times 1 -Exactly - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer2Name } -Times 0 -Exactly - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer3Name } -Times 0 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer1Name + } -Times 1 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer2Name + } -Times 0 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer3Name + } -Times 0 -Exactly + 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 Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer3Name + } Assert-MockCalled -CommandName Import-SQLPSModule -Scope It -Times 1 -Exactly Assert-MockCalled -CommandName Join-SqlAvailabilityGroup -Scope It -Times 0 -Exactly Assert-MockCalled -CommandName New-SqlAvailabilityReplica -Scope It -Times 0 -Exactly @@ -749,9 +888,24 @@ try { Set-TargetResource @setTargetResourceParameters } | Should Not Throw - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer1Name } -Times 1 -Exactly - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer2Name } -Times 1 -Exactly - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer3Name } -Times 0 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer1Name + } -Times 1 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer2Name + } -Times 1 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer3Name + } -Times 0 -Exactly + Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer1Name + } + Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 1 -Exactly -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer2Name + } + Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer3Name + } Assert-MockCalled -CommandName Import-SQLPSModule -Scope It -Times 1 -Exactly Assert-MockCalled -CommandName Join-SqlAvailabilityGroup -Scope It -Times 1 -Exactly Assert-MockCalled -CommandName New-SqlAvailabilityReplica -Scope It -Times 1 -Exactly @@ -767,9 +921,24 @@ try { Set-TargetResource @setTargetResourceParameters } | Should Not Throw - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer1Name } -Times 1 -Exactly - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer2Name } -Times 1 -Exactly - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer3Name } -Times 1 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer1Name + } -Times 1 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer2Name + } -Times 0 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer3Name + } -Times 1 -Exactly + Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer1Name + } + Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 1 -Exactly -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer2Name + } + Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer3Name + } Assert-MockCalled -CommandName Import-SQLPSModule -Scope It -Times 1 -Exactly Assert-MockCalled -CommandName Join-SqlAvailabilityGroup -Scope It -Times 1 -Exactly Assert-MockCalled -CommandName New-SqlAvailabilityReplica -Scope It -Times 1 -Exactly @@ -785,9 +954,24 @@ try { Set-TargetResource @setTargetResourceParameters } | Should Throw 'CreateAvailabilityGroupReplicaFailed' - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer1Name } -Times 1 -Exactly - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer2Name } -Times 1 -Exactly - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer3Name } -Times 0 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer1Name + } -Times 1 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer2Name + } -Times 1 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer3Name + } -Times 0 -Exactly + Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer1Name + } + Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 1 -Exactly -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer2Name + } + Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer3Name + } Assert-MockCalled -CommandName Import-SQLPSModule -Scope It -Times 1 -Exactly Assert-MockCalled -CommandName Join-SqlAvailabilityGroup -Scope It -Times 0 -Exactly Assert-MockCalled -CommandName New-SqlAvailabilityReplica -Scope It -Times 1 -Exactly @@ -803,9 +987,24 @@ try { Set-TargetResource @setTargetResourceParameters } | Should Throw 'JoinAvailabilityGroupFailed' - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer1Name } -Times 1 -Exactly - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer2Name } -Times 1 -Exactly - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer3Name } -Times 0 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer1Name + } -Times 1 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer2Name + } -Times 1 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer3Name + } -Times 0 -Exactly + Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer1Name + } + Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 1 -Exactly -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer2Name + } + Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer3Name + } Assert-MockCalled -CommandName Import-SQLPSModule -Scope It -Times 1 -Exactly Assert-MockCalled -CommandName Join-SqlAvailabilityGroup -Scope It -Times 1 -Exactly Assert-MockCalled -CommandName New-SqlAvailabilityReplica -Scope It -Times 1 -Exactly @@ -821,9 +1020,24 @@ try { Set-TargetResource @setTargetResourceParameters } | Should Throw 'AvailabilityGroupNotFound' - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer1Name } -Times 1 -Exactly - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer2Name } -Times 1 -Exactly - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer3Name } -Times 0 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer1Name + } -Times 1 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer2Name + } -Times 1 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer3Name + } -Times 0 -Exactly + 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 Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer3Name + } Assert-MockCalled -CommandName Import-SQLPSModule -Scope It -Times 1 -Exactly Assert-MockCalled -CommandName Join-SqlAvailabilityGroup -Scope It -Times 0 -Exactly Assert-MockCalled -CommandName New-SqlAvailabilityReplica -Scope It -Times 0 -Exactly @@ -842,13 +1056,13 @@ try # Create a hash table to provide test properties and values for the update tests $mockTestProperties = @{ - AvailabilityMode = 'SynchronousCommit' - BackupPriority = 75 - ConnectionModeInPrimaryRole = 'AllowReadWriteConnections' + AvailabilityMode = 'SynchronousCommit' + BackupPriority = 75 + ConnectionModeInPrimaryRole = 'AllowReadWriteConnections' ConnectionModeInSecondaryRole = 'AllowReadIntentConnectionsOnly' - FailoverMode = 'Automatic' - ReadOnlyRoutingConnectionUrl = 'TCP://TestHost.domain.com:1433' - ReadOnlyRoutingList = @('Server1','Server2') + FailoverMode = 'Automatic' + ReadOnlyRoutingConnectionUrl = 'TCP://TestHost.domain.com:1433' + ReadOnlyRoutingList = @('Server1', 'Server2') } } @@ -857,21 +1071,21 @@ try $mockAlternateEndpointProtocol = $false $setTargetResourceParameters = @{ - Name = $mockSqlServer - AvailabilityGroupName = $mockAvailabilityGroupName - SQLServer = $mockSqlServer - SQLInstanceName = $mockSqlInstanceName - PrimaryReplicaSQLServer = $mockPrimaryReplicaSQLServer + Name = $mockSqlServer + AvailabilityGroupName = $mockAvailabilityGroupName + SQLServer = $mockSqlServer + SQLInstanceName = $mockSqlInstanceName + PrimaryReplicaSQLServer = $mockPrimaryReplicaSQLServer PrimaryReplicaSQLInstanceName = $mockPrimaryReplicaSQLInstanceName - Ensure = $mockEnsure - AvailabilityMode = $mockAvailabilityMode - BackupPriority = $mockBackupPriority - ConnectionModeInPrimaryRole = $mockConnectionModeInPrimaryRole + Ensure = $mockEnsure + AvailabilityMode = $mockAvailabilityMode + BackupPriority = $mockBackupPriority + ConnectionModeInPrimaryRole = $mockConnectionModeInPrimaryRole ConnectionModeInSecondaryRole = $mockConnectionModeInSecondaryRole - EndpointHostName = $mockEndpointHostName - FailoverMode = $mockFailoverMode - ReadOnlyRoutingConnectionUrl = $mockReadOnlyRoutingConnectionUrl - ReadOnlyRoutingList = $mockReadOnlyRoutingList + EndpointHostName = $mockEndpointHostName + FailoverMode = $mockFailoverMode + ReadOnlyRoutingConnectionUrl = $mockReadOnlyRoutingConnectionUrl + ReadOnlyRoutingList = $mockReadOnlyRoutingList } } @@ -881,9 +1095,24 @@ try { Set-TargetResource @setTargetResourceParameters } | Should Throw 'ReplicaNotFound' - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer1Name } -Times 1 -Exactly - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer2Name } -Times 1 -Exactly - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer3Name } -Times 0 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer1Name + } -Times 1 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer2Name + } -Times 0 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer3Name + } -Times 0 -Exactly + Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer1Name + } + Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 1 -Exactly -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer2Name + } + Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer3Name + } Assert-MockCalled -CommandName Import-SQLPSModule -Scope It -Times 1 -Exactly Assert-MockCalled -CommandName Join-SqlAvailabilityGroup -Scope It -Times 0 -Exactly Assert-MockCalled -CommandName New-SqlAvailabilityReplica -Scope It -Times 0 -Exactly @@ -903,9 +1132,24 @@ try { Set-TargetResource @setTargetResourceParameters } | Should Not Throw - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer1Name } -Times 1 -Exactly - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer2Name } -Times 1 -Exactly - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer3Name } -Times 0 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer1Name + } -Times 1 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer2Name + } -Times 0 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer3Name + } -Times 0 -Exactly + Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer1Name + } + Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 1 -Exactly -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer2Name + } + Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer3Name + } Assert-MockCalled -CommandName Import-SQLPSModule -Scope It -Times 1 -Exactly Assert-MockCalled -CommandName Join-SqlAvailabilityGroup -Scope It -Times 0 -Exactly Assert-MockCalled -CommandName New-SqlAvailabilityReplica -Scope It -Times 0 -Exactly @@ -922,9 +1166,24 @@ try { Set-TargetResource @setTargetResourceParameters } | Should Not Throw - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer1Name } -Times 1 -Exactly - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer2Name } -Times 1 -Exactly - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer3Name } -Times 0 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer1Name + } -Times 1 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer2Name + } -Times 0 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer3Name + } -Times 0 -Exactly + Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer1Name + } + Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 1 -Exactly -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer2Name + } + Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer3Name + } Assert-MockCalled -CommandName Import-SQLPSModule -Scope It -Times 1 -Exactly Assert-MockCalled -CommandName Join-SqlAvailabilityGroup -Scope It -Times 0 -Exactly Assert-MockCalled -CommandName New-SqlAvailabilityReplica -Scope It -Times 0 -Exactly @@ -942,9 +1201,24 @@ try { Set-TargetResource @setTargetResourceParameters } | Should Not Throw - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer1Name } -Times 1 -Exactly - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer2Name } -Times 1 -Exactly - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer3Name } -Times 0 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer1Name + } -Times 1 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer2Name + } -Times 0 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer3Name + } -Times 0 -Exactly + Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer1Name + } + Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 1 -Exactly -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer2Name + } + Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer3Name + } Assert-MockCalled -CommandName Import-SQLPSModule -Scope It -Times 1 -Exactly Assert-MockCalled -CommandName Join-SqlAvailabilityGroup -Scope It -Times 0 -Exactly Assert-MockCalled -CommandName New-SqlAvailabilityReplica -Scope It -Times 0 -Exactly @@ -962,9 +1236,24 @@ try { Set-TargetResource @setTargetResourceParameters } | Should Not Throw - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer1Name } -Times 1 -Exactly - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer2Name } -Times 1 -Exactly - Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { $SQLServer -eq $mockServer3Name } -Times 0 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer1Name + } -Times 1 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer2Name + } -Times 0 -Exactly + Assert-MockCalled -CommandName Connect-SQL -Scope It -ParameterFilter { + $SQLServer -eq $mockServer3Name + } -Times 0 -Exactly + Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer1Name + } + Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 1 -Exactly -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer2Name + } + Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Time 0 -Exactly -ParameterFilter { + $AvailabilityGroup.PrimaryReplicaServerName -eq $mockServer3Name + } Assert-MockCalled -CommandName Import-SQLPSModule -Scope It -Times 1 -Exactly Assert-MockCalled -CommandName Join-SqlAvailabilityGroup -Scope It -Times 0 -Exactly Assert-MockCalled -CommandName New-SqlAvailabilityReplica -Scope It -Times 0 -Exactly @@ -984,21 +1273,21 @@ try $mockDatabaseMirroringEndpoint = $true $testTargetResourceParameters = @{ - Name = $mockSqlServer - AvailabilityGroupName = $mockAvailabilityGroupName - SQLServer = $mockSqlServer - SQLInstanceName = $mockSqlInstanceName - PrimaryReplicaSQLServer = $mockPrimaryReplicaSQLServer + Name = $mockSqlServer + AvailabilityGroupName = $mockAvailabilityGroupName + SQLServer = $mockSqlServer + SQLInstanceName = $mockSqlInstanceName + PrimaryReplicaSQLServer = $mockPrimaryReplicaSQLServer PrimaryReplicaSQLInstanceName = $mockPrimaryReplicaSQLInstanceName - Ensure = $mockEnsure - AvailabilityMode = $mockAvailabilityMode - BackupPriority = $mockBackupPriority - ConnectionModeInPrimaryRole = $mockConnectionModeInPrimaryRole + Ensure = $mockEnsure + AvailabilityMode = $mockAvailabilityMode + BackupPriority = $mockBackupPriority + ConnectionModeInPrimaryRole = $mockConnectionModeInPrimaryRole ConnectionModeInSecondaryRole = $mockConnectionModeInSecondaryRole - EndpointHostName = $mockEndpointHostName - FailoverMode = $mockFailoverMode - ReadOnlyRoutingConnectionUrl = $mockReadOnlyRoutingConnectionUrl - ReadOnlyRoutingList = $mockReadOnlyRoutingList + EndpointHostName = $mockEndpointHostName + FailoverMode = $mockFailoverMode + ReadOnlyRoutingConnectionUrl = $mockReadOnlyRoutingConnectionUrl + ReadOnlyRoutingList = $mockReadOnlyRoutingList } Mock -CommandName Connect-SQL -MockWith $mockConnectSqlServer1 -Verifiable @@ -1031,13 +1320,13 @@ try BeforeAll { $propertiesToCheck = @{ - AvailabilityMode = 'SynchronousCommit' - BackupPriority = 42 - ConnectionModeInPrimaryRole = 'AllowReadWriteConnections' + AvailabilityMode = 'SynchronousCommit' + BackupPriority = 42 + ConnectionModeInPrimaryRole = 'AllowReadWriteConnections' ConnectionModeInSecondaryRole = 'AllowReadIntentConnectionsOnly' - FailoverMode = 'Automatic' - ReadOnlyRoutingConnectionUrl = 'WrongUrl' - ReadOnlyRoutingList = @('WrongServer') + FailoverMode = 'Automatic' + ReadOnlyRoutingConnectionUrl = 'WrongUrl' + ReadOnlyRoutingList = @('WrongServer') } }