Skip to content

Commit

Permalink
SqlAgDatabase: Typo in resource for fix for #1492 (#1666)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fiander committed Jan 9, 2021
1 parent cb1917a commit 23b977c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
at all replicas of an availability group. When automatic seeding is found, it will use that.
- Lots of extra tests to check AutomaticSeeding.
- The parameter `BackupPath` is still needed just in case a database never has been backuped before.
- Fixed a typo.
- SqlMaxDop
- Fixes ([issue #396](https://github.com/dsccommunity/SqlServerDsc/issues/396)).
Added three return values in Get-Target resource.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ function Set-TargetResource
#>
if ( $primaryServerObject.Databases[$databaseToAddToAvailabilityGroup].CreateDate -gt $primaryServerObject.Databases[$databaseToAddToAvailabilityGroup].LastBackupDate)
{
$needsBackup = $true
$backupNeeded = $true
}
}

Expand Down
24 changes: 15 additions & 9 deletions tests/Unit/DSC_SqlAGDatabase.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1679,7 +1679,6 @@ REVERT'
$mockDatabaseEncryptionKeyObject = New-Object -TypeName Microsoft.SqlServer.Management.Smo.DatabaseEncryptionKey
$mockDatabaseEncryptionKeyObject.EncryptorName = 'TDE Cert'
$mockDatabaseEncryptionKeyObject.Thumbprint = $mockThumbprint1

#endregion Certificate Mocks

#region Database File Mocks
Expand Down Expand Up @@ -1834,7 +1833,6 @@ REVERT'

#endregion Invoke Query Mock


Mock -CommandName Get-PrimaryReplicaServerObject -MockWith { return $mockServerObject } -Verifiable -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server1' }
Mock -CommandName Get-PrimaryReplicaServerObject -MockWith { return $mockServer2Object } -Verifiable -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server2' }
Mock -CommandName Import-SQLPSModule -Verifiable
Expand Down Expand Up @@ -1873,7 +1871,7 @@ REVERT'
}

Context 'When Ensure is Present' {
It 'Should add the specified databases to the availability group' {
It 'Should add the specified databases to the availability group.' {
{ Set-TargetResource @mockSetTargetResourceParameters } | Should -Not -Throw

Assert-MockCalled -CommandName Add-SqlAvailabilityDatabase -Scope It -Times 1 -Exactly -ParameterFilter { $InputObject.PrimaryReplicaServerName -eq 'Server1' -and $InputObject.LocalReplicaRole -eq 'Primary' }
Expand Down Expand Up @@ -2280,16 +2278,17 @@ REVERT'
}

It 'Should add the specified databases to the availability group when the database has not been previously backed up' {
$mockServerObject.Databases['DB1'].LastBackupDate = 0
$mockServerObject.Databases['DB1'].CreateDate = '2020-10-20 10:00:00'
$mockServerObject.Databases['DB1'].LastBackupDate = '2020-10-10 10:00:00'

{ Set-TargetResource @mockSetTargetResourceParameters } | Should -Not -Throw

Assert-MockCalled -CommandName Add-SqlAvailabilityDatabase -Scope It -Times 1 -Exactly -ParameterFilter { $InputObject.PrimaryReplicaServerName -eq 'Server1' -and $InputObject.LocalReplicaRole -eq 'Primary' }
Assert-MockCalled -CommandName Add-SqlAvailabilityDatabase -Scope It -Times 1 -Exactly -ParameterFilter { $InputObject.PrimaryReplicaServerName -eq 'Server1' -and $InputObject.LocalReplicaRole -eq 'Secondary' }
Assert-MockCalled -CommandName Add-SqlAvailabilityDatabase -Scope It -Times 0 -Exactly -ParameterFilter { $InputObject.PrimaryReplicaServerName -eq 'Server2' -and $InputObject.LocalReplicaRole -eq 'Primary' }
Assert-MockCalled -CommandName Add-SqlAvailabilityDatabase -Scope It -Times 0 -Exactly -ParameterFilter { $InputObject.PrimaryReplicaServerName -eq 'Server2' -and $InputObject.LocalReplicaRole -eq 'Secondary' }
Assert-MockCalled -CommandName Backup-SqlDatabase -Scope It -Times 0 -Exactly -ParameterFilter { $BackupAction -eq 'Database' }
Assert-MockCalled -CommandName Backup-SqlDatabase -Scope It -Times 0 -Exactly -ParameterFilter { $BackupAction -eq 'Log' }
Assert-MockCalled -CommandName Backup-SqlDatabase -Scope It -Times 1 -Exactly -ParameterFilter { $BackupAction -eq 'Database' }
Assert-MockCalled -CommandName Backup-SqlDatabase -Scope It -Times 1 -Exactly -ParameterFilter { $BackupAction -eq 'Log' }
Assert-MockCalled -CommandName Connect-SQL -Scope It -Times 1 -Exactly -ParameterFilter { $ServerName -eq 'Server1' -and $InstanceName -eq 'MSSQLSERVER' }
Assert-MockCalled -CommandName Connect-SQL -Scope It -Times 1 -Exactly -ParameterFilter { $ServerName -eq 'Server1' }
Assert-MockCalled -CommandName Connect-SQL -Scope It -Times 3 -Exactly -ParameterFilter { $ServerName -eq 'Server2' }
Expand All @@ -2299,11 +2298,15 @@ REVERT'
Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' }
Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase
Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs
Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Full_*.bak' }
Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Log_*.trn' }
Assert-MockCalled -CommandName Remove-Item -Scope It -Times 0 -Exactly
Assert-MockCalled -CommandName Join-Path -Scope It -Times 1 -Exactly -ParameterFilter { $ChildPath -like '*_Full_*.bak' }
Assert-MockCalled -CommandName Join-Path -Scope It -Times 1 -Exactly -ParameterFilter { $ChildPath -like '*_Log_*.trn' }
Assert-MockCalled -CommandName Remove-Item -Scope It -Times 1 -Exactly
Assert-MockCalled -CommandName Remove-SqlAvailabilityDatabase -Scope It -Times 0 -Exactly
Assert-MockCalled -CommandName Test-ImpersonatePermissions -Scope It -Times 1 -Exactly

#reset so others will not trip. Fix for pester 5
$mockServerObject.Databases['DB1'].CreateDate = '2020-10-10 10:00:00'
$mockServerObject.Databases['DB1'].LastBackupDate = '2020-10-20 10:00:00'
}

It 'Should throw the correct error when it fails to add the database to the primary replica' {
Expand Down Expand Up @@ -2730,6 +2733,8 @@ REVERT'
Mock -CommandName Test-ActiveNode -MockWith {
return -not $mockProcessOnlyOnActiveNode
} -Verifiable


}

BeforeEach {
Expand All @@ -2744,6 +2749,7 @@ REVERT'
MatchDatabaseOwner = $false
ProcessOnlyOnActiveNode = $false
}

}

Context 'When Ensure is Present' {
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Stubs/SMO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,7 @@ public class Database
public string AvailabilityGroupName = "";
public Certificate[] Certificates;
public string ContainmentType = "None";
public DateTime CreateDate;
public DatabaseEncryptionKey DatabaseEncryptionKey;
public string DefaultFileStreamFileGroup;
public bool EncryptionEnabled = false;
Expand Down

0 comments on commit 23b977c

Please sign in to comment.