Skip to content

Commit

Permalink
update SqlADReplica unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sidoran committed Apr 18, 2023
1 parent 9b01eb3 commit 176b7a0
Showing 1 changed file with 61 additions and 16 deletions.
77 changes: 61 additions & 16 deletions tests/Unit/DSC_SqlAGReplica.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ Describe 'SqlAGReplica\Get-TargetResource' {
$mockServerObject.NetName = 'Server1'
$mockServerObject.IsHadrEnabled = $true
$mockServerObject.ServiceName = 'MSSQLSERVER'
$mockServerObject.Version = @{
Major = 13
}

# Mock the availability group replicas
$mockAvailabilityGroupReplica1 = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityReplica
Expand All @@ -96,6 +99,7 @@ Describe 'SqlAGReplica\Get-TargetResource' {
$mockAvailabilityGroupReplica1.Name = 'Server1'
$mockAvailabilityGroupReplica1.ReadOnlyRoutingConnectionUrl = 'TCP://Server1.domain.com:1433'
$mockAvailabilityGroupReplica1.ReadOnlyRoutingList = @('Server1', 'Server2')
$mockAvailabilityGroupReplica1.SeedingMode = 'Manual'

$mockAvailabilityGroupReplica2 = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityReplica
$mockAvailabilityGroupReplica2.AvailabilityMode = 'AsynchronousCommit'
Expand All @@ -107,6 +111,7 @@ Describe 'SqlAGReplica\Get-TargetResource' {
$mockAvailabilityGroupReplica2.Name = 'Server2'
$mockAvailabilityGroupReplica2.ReadOnlyRoutingConnectionUrl = 'TCP://Server2.domain.com:1433'
$mockAvailabilityGroupReplica2.ReadOnlyRoutingList = @('Server1', 'Server2')
$mockAvailabilityGroupReplica2.SeedingMode = 'Manual'

$mockAvailabilityGroupReplica3 = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityReplica
$mockAvailabilityGroupReplica3.AvailabilityMode = 'AsynchronousCommit'
Expand All @@ -118,6 +123,7 @@ Describe 'SqlAGReplica\Get-TargetResource' {
$mockAvailabilityGroupReplica3.Name = 'Server3'
$mockAvailabilityGroupReplica3.ReadOnlyRoutingConnectionUrl = 'TCP://Server3.domain.com:1433'
$mockAvailabilityGroupReplica3.ReadOnlyRoutingList = @('Server1', 'Server2')
$mockAvailabilityGroupReplica3.SeedingMode = 'Manual'

# Mock the availability groups
$mockAvailabilityGroup1 = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityGroup
Expand Down Expand Up @@ -173,6 +179,7 @@ Describe 'SqlAGReplica\Get-TargetResource' {
$getTargetResourceResult.ServerName | Should -Be 'Server1'
$getTargetResourceResult.InstanceName | Should -Be 'MSSQLSERVER'
$getTargetResourceResult.EndpointHostName | Should -Be 'Server1'
$getTargetResourceResult.SeedingMode | Should -BeNullOrEmpty
}

Should -Invoke -CommandName Connect-SQL -Exactly -Times 1 -Scope It
Expand Down Expand Up @@ -206,6 +213,7 @@ Describe 'SqlAGReplica\Get-TargetResource' {
$getTargetResourceResult.ServerName | Should -Be 'Server1'
$getTargetResourceResult.InstanceName | Should -Be 'MSSQLSERVER'
$getTargetResourceResult.EndpointHostName | Should -Be 'Server1'
$getTargetResourceResult.SeedingMode | Should -Be 'Manual'
}

Should -Invoke -CommandName Connect-SQL -Exactly -Times 1 -Scope It
Expand All @@ -222,6 +230,9 @@ Describe 'SqlAGReplica\Set-TargetResource' {
$mockServerObject.NetName = 'Server1'
$mockServerObject.IsHadrEnabled = $true
$mockServerObject.ServiceName = 'MSSQLSERVER'
$mockServerObject.Version = @{
Major = 13
}

# Mock the availability group replicas
$mockAvailabilityGroupReplica1 = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityReplica
Expand All @@ -234,6 +245,7 @@ Describe 'SqlAGReplica\Set-TargetResource' {
$mockAvailabilityGroupReplica1.Name = 'Server1'
$mockAvailabilityGroupReplica1.ReadOnlyRoutingConnectionUrl = 'TCP://Server1.domain.com:1433'
$mockAvailabilityGroupReplica1.ReadOnlyRoutingList = @('Server1', 'Server2')
$mockAvailabilityGroupReplica1.SeedingMode = 'Manual'

$mockAvailabilityGroupReplica2 = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityReplica
$mockAvailabilityGroupReplica2.AvailabilityMode = 'AsynchronousCommit'
Expand All @@ -245,6 +257,7 @@ Describe 'SqlAGReplica\Set-TargetResource' {
$mockAvailabilityGroupReplica2.Name = 'Server2'
$mockAvailabilityGroupReplica2.ReadOnlyRoutingConnectionUrl = 'TCP://Server2.domain.com:1433'
$mockAvailabilityGroupReplica2.ReadOnlyRoutingList = @('Server1', 'Server2')
$mockAvailabilityGroupReplica2.SeedingMode = 'Manual'

$mockAvailabilityGroupReplica3 = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityReplica
$mockAvailabilityGroupReplica3.AvailabilityMode = 'AsynchronousCommit'
Expand All @@ -256,6 +269,7 @@ Describe 'SqlAGReplica\Set-TargetResource' {
$mockAvailabilityGroupReplica3.Name = 'Server3'
$mockAvailabilityGroupReplica3.ReadOnlyRoutingConnectionUrl = 'TCP://Server3.domain.com:1433'
$mockAvailabilityGroupReplica3.ReadOnlyRoutingList = @('Server1', 'Server2')
$mockAvailabilityGroupReplica3.SeedingMode = 'Manual'

# Mock the availability groups
$mockAvailabilityGroup1 = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityGroup
Expand Down Expand Up @@ -287,6 +301,9 @@ Describe 'SqlAGReplica\Set-TargetResource' {
$mockServerObject.NetName = 'Server2'
$mockServerObject.IsHadrEnabled = $true
$mockServerObject.ServiceName = 'MSSQLSERVER'
$mockServerObject.Version = @{
Major = 13
}

#region Mock the availability group replicas
$mockAvailabilityGroupReplica1 = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityReplica
Expand All @@ -299,6 +316,7 @@ Describe 'SqlAGReplica\Set-TargetResource' {
$mockAvailabilityGroupReplica1.Name = 'Server1'
$mockAvailabilityGroupReplica1.ReadOnlyRoutingConnectionUrl = 'TCP://Server1.domain.com:1433'
$mockAvailabilityGroupReplica1.ReadOnlyRoutingList = @('Server1', 'Server2')
$mockAvailabilityGroupReplica1.SeedingMode = 'Manual'

$mockAvailabilityGroupReplica2 = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityReplica
$mockAvailabilityGroupReplica2.AvailabilityMode = 'AsynchronousCommit'
Expand All @@ -310,6 +328,7 @@ Describe 'SqlAGReplica\Set-TargetResource' {
$mockAvailabilityGroupReplica2.Name = 'Server2'
$mockAvailabilityGroupReplica2.ReadOnlyRoutingConnectionUrl = 'TCP://Server2.domain.com:1433'
$mockAvailabilityGroupReplica2.ReadOnlyRoutingList = @('Server1', 'Server2')
$mockAvailabilityGroupReplica2.SeedingMode = 'Manual'

$mockAvailabilityGroupReplica3 = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityReplica
$mockAvailabilityGroupReplica3.AvailabilityMode = 'AsynchronousCommit'
Expand All @@ -321,6 +340,7 @@ Describe 'SqlAGReplica\Set-TargetResource' {
$mockAvailabilityGroupReplica3.Name = 'Server3'
$mockAvailabilityGroupReplica3.ReadOnlyRoutingConnectionUrl = 'TCP://Server3.domain.com:1433'
$mockAvailabilityGroupReplica3.ReadOnlyRoutingList = @('Server1', 'Server2')
$mockAvailabilityGroupReplica3.SeedingMode = 'Manual'
#endregion Mock the availability group replicas

# Mock the availability groups
Expand Down Expand Up @@ -369,6 +389,9 @@ Describe 'SqlAGReplica\Set-TargetResource' {
$mockServerObject.NetName = 'Server3'
$mockServerObject.IsHadrEnabled = $true
$mockServerObject.ServiceName = 'MSSQLSERVER'
$mockServerObject.Version = @{
Major = 13
}

#region Mock the availability group replicas
$mockAvailabilityGroupReplica1 = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityReplica
Expand All @@ -381,6 +404,7 @@ Describe 'SqlAGReplica\Set-TargetResource' {
$mockAvailabilityGroupReplica1.Name = 'Server1'
$mockAvailabilityGroupReplica1.ReadOnlyRoutingConnectionUrl = 'TCP://Server1.domain.com:1433'
$mockAvailabilityGroupReplica1.ReadOnlyRoutingList = @('Server1', 'Server2')
$mockAvailabilityGroupReplica1.SeedingMode = 'Manual'

$mockAvailabilityGroupReplica2 = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityReplica
$mockAvailabilityGroupReplica2.AvailabilityMode = 'AsynchronousCommit'
Expand All @@ -392,6 +416,7 @@ Describe 'SqlAGReplica\Set-TargetResource' {
$mockAvailabilityGroupReplica2.Name = 'Server2'
$mockAvailabilityGroupReplica2.ReadOnlyRoutingConnectionUrl = 'TCP://Server2.domain.com:1433'
$mockAvailabilityGroupReplica2.ReadOnlyRoutingList = @('Server1', 'Server2')
$mockAvailabilityGroupReplica2.SeedingMode = 'Manual'

$mockAvailabilityGroupReplica3 = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityReplica
$mockAvailabilityGroupReplica3.AvailabilityMode = 'AsynchronousCommit'
Expand All @@ -403,6 +428,7 @@ Describe 'SqlAGReplica\Set-TargetResource' {
$mockAvailabilityGroupReplica3.Name = 'Server3'
$mockAvailabilityGroupReplica3.ReadOnlyRoutingConnectionUrl = 'TCP://Server3.domain.com:1433'
$mockAvailabilityGroupReplica3.ReadOnlyRoutingList = @('Server1', 'Server2')
$mockAvailabilityGroupReplica3.SeedingMode = 'Manual'
#endregion Mock the availability group replicas

# Mock the availability groups
Expand Down Expand Up @@ -688,6 +714,7 @@ Describe 'SqlAGReplica\Set-TargetResource' {
FailoverMode = 'Manual'
ReadOnlyRoutingConnectionUrl = 'TCP://Server1.domain.com:1433'
ReadOnlyRoutingList = @('Server1', 'Server2')
SeedingMode = 'Manual'
}

{ Set-TargetResource @setTargetResourceParameters } | Should -Not -Throw
Expand Down Expand Up @@ -744,6 +771,7 @@ Describe 'SqlAGReplica\Set-TargetResource' {
FailoverMode = 'Manual'
ReadOnlyRoutingConnectionUrl = 'TCP://Server1.domain.com:1433'
ReadOnlyRoutingList = @('Server1', 'Server2')
SeedingMode = 'Manual'
}

{ Set-TargetResource @setTargetResourceParameters } | Should -Not -Throw
Expand Down Expand Up @@ -801,6 +829,7 @@ Describe 'SqlAGReplica\Set-TargetResource' {
FailoverMode = 'Manual'
ReadOnlyRoutingConnectionUrl = 'TCP://Server1.domain.com:1433'
ReadOnlyRoutingList = @('Server1', 'Server2')
SeedingMode = 'Manual'
}

{ Set-TargetResource @setTargetResourceParameters } | Should -Not -Throw
Expand Down Expand Up @@ -864,6 +893,7 @@ Describe 'SqlAGReplica\Set-TargetResource' {
FailoverMode = 'Manual'
ReadOnlyRoutingConnectionUrl = 'TCP://Server1.domain.com:1433'
ReadOnlyRoutingList = @('Server1', 'Server2')
SeedingMode = 'Manual'
}

$mockErrorRecord = Get-InvalidOperationRecord -Message (
Expand Down Expand Up @@ -932,6 +962,7 @@ Describe 'SqlAGReplica\Set-TargetResource' {
FailoverMode = 'Manual'
ReadOnlyRoutingConnectionUrl = 'TCP://Server1.domain.com:1433'
ReadOnlyRoutingList = @('Server1', 'Server2')
SeedingMode = 'Manual'
}

$mockErrorRecord = Get-InvalidOperationRecord -Message (
Expand Down Expand Up @@ -994,6 +1025,7 @@ Describe 'SqlAGReplica\Set-TargetResource' {
FailoverMode = 'Manual'
ReadOnlyRoutingConnectionUrl = 'TCP://Server1.domain.com:1433'
ReadOnlyRoutingList = @('Server1', 'Server2')
SeedingMode = 'Manual'
}

$mockErrorRecord = Get-ObjectNotFoundRecord -Message (
Expand Down Expand Up @@ -1058,6 +1090,7 @@ Describe 'SqlAGReplica\Set-TargetResource' {
FailoverMode = 'Manual'
ReadOnlyRoutingConnectionUrl = 'TCP://Server1.domain.com:1433'
ReadOnlyRoutingList = @('Server1', 'Server2')
SeedingMode = 'Manual'
}

$mockErrorRecord = Get-ObjectNotFoundRecord -Message (
Expand Down Expand Up @@ -1132,6 +1165,10 @@ Describe 'SqlAGReplica\Set-TargetResource' {
MockPropertyName = 'EndpointHostName'
MockPropertyValue = 'AnotherEndpointHostName'
}
@{
MockPropertyName = 'SeedingMode'
MockPropertyValue = 'Automatic'
}
) {
BeforeAll {
Mock -CommandName Remove-SqlAvailabilityReplica
Expand All @@ -1156,6 +1193,7 @@ Describe 'SqlAGReplica\Set-TargetResource' {
FailoverMode = 'Manual'
ReadOnlyRoutingConnectionUrl = 'TCP://Server1.domain.com:1433'
ReadOnlyRoutingList = @('Server1', 'Server2')
SeedingMode = 'Manual'
}

$setTargetResourceParameters.$MockPropertyName = $MockPropertyValue
Expand Down Expand Up @@ -1247,6 +1285,7 @@ Describe 'SqlAGReplica\Set-TargetResource' {
FailoverMode = 'Automatic'
ReadOnlyRoutingConnectionUrl = 'TCP://Server1.domain.com:1433'
ReadOnlyRoutingList = @('Server2', 'Server1')
SeedingMode = 'Manual'
}

{ Set-TargetResource @setTargetResourceParameters } | Should -Not -Throw
Expand Down Expand Up @@ -1279,6 +1318,7 @@ Describe 'SqlAGReplica\Set-TargetResource' {
FailoverMode = 'Automatic'
ReadOnlyRoutingConnectionUrl = 'TCP://Server1.domain.com:1433'
ReadOnlyRoutingList = @('Server1', 'Server2')
SeedingMode = 'Manual'
}

{ Set-TargetResource @setTargetResourceParameters } | Should -Not -Throw
Expand Down Expand Up @@ -1601,6 +1641,10 @@ Describe 'SqlAGReplica\Test-TargetResource' {
MockPropertyName = 'ReadOnlyRoutingList'
MockPropertyValue = @('Server2', 'Server1')
}
@{
MockPropertyName = 'SeedingMode'
MockPropertyValue = 'Automatic'
}
) {
BeforeAll {
Mock -CommandName Get-TargetResource -MockWith {
Expand All @@ -1620,6 +1664,7 @@ Describe 'SqlAGReplica\Test-TargetResource' {
FailoverMode = 'Manual'
ReadOnlyRoutingConnectionUrl = 'TCP://Server1.domain.com:1433'
ReadOnlyRoutingList = @('Server1', 'Server2')
SeedingMode = 'Manual'

# Read properties
EndpointPort = '5022'
Expand Down Expand Up @@ -1652,16 +1697,16 @@ Describe 'SqlAGReplica\Test-TargetResource' {
BeforeAll {
Mock -CommandName Get-TargetResource -MockWith {
@{
Name = 'Server1'
AvailabilityGroupName = 'AG_AllServers'
ServerName = 'Server1'
InstanceName = 'MSSQLSERVER'
Ensure = 'Present'
Name = 'Server1'
AvailabilityGroupName = 'AG_AllServers'
ServerName = 'Server1'
InstanceName = 'MSSQLSERVER'
Ensure = 'Present'

# Read properties
EndpointPort = '5022'
EndpointUrl = 'TCP://Server1:1433'
IsActiveNode = $true
EndpointPort = '5022'
EndpointUrl = 'TCP://Server1:1433'
IsActiveNode = $true
}
}
}
Expand All @@ -1687,16 +1732,16 @@ Describe 'SqlAGReplica\Test-TargetResource' {
BeforeAll {
Mock -CommandName Get-TargetResource -MockWith {
@{
Name = 'Server1'
AvailabilityGroupName = 'AG_AllServers'
ServerName = 'Server1'
InstanceName = 'MSSQLSERVER'
Ensure = 'Present'
Name = 'Server1'
AvailabilityGroupName = 'AG_AllServers'
ServerName = 'Server1'
InstanceName = 'MSSQLSERVER'
Ensure = 'Present'

# Read properties
EndpointPort = '5022'
EndpointUrl = 'UDP://Server1:5022'
IsActiveNode = $true
EndpointPort = '5022'
EndpointUrl = 'UDP://Server1:5022'
IsActiveNode = $true
}
}
}
Expand Down

0 comments on commit 176b7a0

Please sign in to comment.