From 23b977c9b30855ef250d922494ad87a4589d4665 Mon Sep 17 00:00:00 2001 From: Fiander <51764122+Fiander@users.noreply.github.com> Date: Sat, 9 Jan 2021 13:49:08 +0100 Subject: [PATCH] SqlAgDatabase: Typo in resource for fix for #1492 (#1666) --- CHANGELOG.md | 1 + .../DSC_SqlAGDatabase/DSC_SqlAGDatabase.psm1 | 2 +- tests/Unit/DSC_SqlAGDatabase.Tests.ps1 | 24 ++++++++++++------- tests/Unit/Stubs/SMO.cs | 1 + 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c18160e5..f8a160fa7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/source/DSCResources/DSC_SqlAGDatabase/DSC_SqlAGDatabase.psm1 b/source/DSCResources/DSC_SqlAGDatabase/DSC_SqlAGDatabase.psm1 index 61a099f5f..eb14fbd16 100644 --- a/source/DSCResources/DSC_SqlAGDatabase/DSC_SqlAGDatabase.psm1 +++ b/source/DSCResources/DSC_SqlAGDatabase/DSC_SqlAGDatabase.psm1 @@ -447,7 +447,7 @@ function Set-TargetResource #> if ( $primaryServerObject.Databases[$databaseToAddToAvailabilityGroup].CreateDate -gt $primaryServerObject.Databases[$databaseToAddToAvailabilityGroup].LastBackupDate) { - $needsBackup = $true + $backupNeeded = $true } } diff --git a/tests/Unit/DSC_SqlAGDatabase.Tests.ps1 b/tests/Unit/DSC_SqlAGDatabase.Tests.ps1 index a2953a0ad..38e105809 100644 --- a/tests/Unit/DSC_SqlAGDatabase.Tests.ps1 +++ b/tests/Unit/DSC_SqlAGDatabase.Tests.ps1 @@ -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 @@ -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 @@ -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' } @@ -2280,7 +2278,8 @@ 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 @@ -2288,8 +2287,8 @@ REVERT' 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' } @@ -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' { @@ -2730,6 +2733,8 @@ REVERT' Mock -CommandName Test-ActiveNode -MockWith { return -not $mockProcessOnlyOnActiveNode } -Verifiable + + } BeforeEach { @@ -2744,6 +2749,7 @@ REVERT' MatchDatabaseOwner = $false ProcessOnlyOnActiveNode = $false } + } Context 'When Ensure is Present' { diff --git a/tests/Unit/Stubs/SMO.cs b/tests/Unit/Stubs/SMO.cs index f2e56a65c..5e514c992 100644 --- a/tests/Unit/Stubs/SMO.cs +++ b/tests/Unit/Stubs/SMO.cs @@ -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;