|
| 1 | +[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification = 'Suppressing this rule because Script Analyzer does not understand Pester syntax.')] |
| 2 | +param () |
| 3 | + |
| 4 | +BeforeDiscovery { |
| 5 | + try |
| 6 | + { |
| 7 | + if (-not (Get-Module -Name 'DscResource.Test')) |
| 8 | + { |
| 9 | + # Assumes dependencies have been resolved, so if this module is not available, run 'noop' task. |
| 10 | + if (-not (Get-Module -Name 'DscResource.Test' -ListAvailable)) |
| 11 | + { |
| 12 | + # Redirect all streams to $null, except the error stream (stream 2) |
| 13 | + & "$PSScriptRoot/../../../build.ps1" -Tasks 'noop' 3>&1 4>&1 5>&1 6>&1 > $null |
| 14 | + } |
| 15 | + |
| 16 | + # If the dependencies have not been resolved, this will throw an error. |
| 17 | + Import-Module -Name 'DscResource.Test' -Force -ErrorAction 'Stop' |
| 18 | + } |
| 19 | + } |
| 20 | + catch [System.IO.FileNotFoundException] |
| 21 | + { |
| 22 | + throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -ResolveDependency -Tasks noop" first.' |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +BeforeAll { |
| 27 | + $script:moduleName = 'SqlServerDsc' |
| 28 | + |
| 29 | + Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop' |
| 30 | +} |
| 31 | + |
| 32 | +Describe 'New-SqlDscDatabaseSnapshot' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') { |
| 33 | + BeforeAll { |
| 34 | + $script:mockInstanceName = 'DSCSQLTEST' |
| 35 | + $script:mockComputerName = Get-ComputerName |
| 36 | + |
| 37 | + $mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception. |
| 38 | + $mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force |
| 39 | + |
| 40 | + $script:mockSqlAdminCredential = [System.Management.Automation.PSCredential]::new($mockSqlAdministratorUserName, $mockSqlAdministratorPassword) |
| 41 | + |
| 42 | + $script:serverObject = Connect-SqlDscDatabaseEngine -InstanceName $script:mockInstanceName -Credential $script:mockSqlAdminCredential -ErrorAction 'Stop' |
| 43 | + |
| 44 | + # Source database names - using the persistent database created by New-SqlDscDatabase integration tests |
| 45 | + $script:persistentSourceDatabase = 'SqlDscIntegrationTestDatabase_Persistent' |
| 46 | + |
| 47 | + # Snapshot names |
| 48 | + $script:testSnapshotName = 'SqlDscTestSnapshot_' + (Get-Random) |
| 49 | + $script:testSnapshotNameWithFileGroup = 'SqlDscTestSnapshotFG_' + (Get-Random) |
| 50 | + $script:testSnapshotNameFromDbObject = 'SqlDscTestSnapshotDbObj_' + (Get-Random) |
| 51 | + |
| 52 | + # Verify the persistent database exists before proceeding |
| 53 | + $sourceDb = Get-SqlDscDatabase -ServerObject $script:serverObject -Name $script:persistentSourceDatabase -ErrorAction 'SilentlyContinue' |
| 54 | + |
| 55 | + if (-not $sourceDb) |
| 56 | + { |
| 57 | + throw "The source database '$script:persistentSourceDatabase' does not exist. Please ensure New-SqlDscDatabase integration tests have run successfully." |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + AfterAll { |
| 62 | + # Clean up test snapshots |
| 63 | + $testSnapshotsToRemove = @( |
| 64 | + $script:testSnapshotName, |
| 65 | + $script:testSnapshotNameWithFileGroup, |
| 66 | + $script:testSnapshotNameFromDbObject |
| 67 | + ) |
| 68 | + |
| 69 | + foreach ($snapshotName in $testSnapshotsToRemove) |
| 70 | + { |
| 71 | + $existingSnapshot = Get-SqlDscDatabase -ServerObject $script:serverObject -Name $snapshotName -ErrorAction 'SilentlyContinue' |
| 72 | + |
| 73 | + if ($existingSnapshot) |
| 74 | + { |
| 75 | + $null = Remove-SqlDscDatabase -DatabaseObject $existingSnapshot -Force -ErrorAction 'Stop' |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject |
| 80 | + } |
| 81 | + |
| 82 | + Context 'When creating a database snapshot using ServerObject parameter set' { |
| 83 | + It 'Should create a database snapshot successfully with minimal parameters' { |
| 84 | + $result = New-SqlDscDatabaseSnapshot -ServerObject $script:serverObject -Name $script:testSnapshotName -DatabaseName $script:persistentSourceDatabase -Force -ErrorAction 'Stop' |
| 85 | + |
| 86 | + $result | Should -Not -BeNullOrEmpty |
| 87 | + $result.Name | Should -Be $script:testSnapshotName |
| 88 | + $result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.Database' |
| 89 | + $result.DatabaseSnapshotBaseName | Should -Be $script:persistentSourceDatabase |
| 90 | + |
| 91 | + # Verify the snapshot exists |
| 92 | + $createdSnapshot = Get-SqlDscDatabase -ServerObject $script:serverObject -Name $script:testSnapshotName -Refresh -ErrorAction 'Stop' |
| 93 | + $createdSnapshot | Should -Not -BeNullOrEmpty |
| 94 | + $createdSnapshot.DatabaseSnapshotBaseName | Should -Be $script:persistentSourceDatabase |
| 95 | + } |
| 96 | + |
| 97 | + It 'Should throw error when trying to create a snapshot that already exists' { |
| 98 | + { New-SqlDscDatabaseSnapshot -ServerObject $script:serverObject -Name $script:testSnapshotName -DatabaseName $script:persistentSourceDatabase -Force -ErrorAction 'Stop' } | |
| 99 | + Should -Throw |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + Context 'When creating a database snapshot using DatabaseObject parameter set' { |
| 104 | + BeforeAll { |
| 105 | + # Get the source database object |
| 106 | + $script:sourceDatabaseObject = Get-SqlDscDatabase -ServerObject $script:serverObject -Name $script:persistentSourceDatabase -ErrorAction 'Stop' |
| 107 | + } |
| 108 | + |
| 109 | + It 'Should create a database snapshot from DatabaseObject successfully' { |
| 110 | + $result = New-SqlDscDatabaseSnapshot -DatabaseObject $script:sourceDatabaseObject -Name $script:testSnapshotNameFromDbObject -Force -ErrorAction 'Stop' |
| 111 | + |
| 112 | + $result | Should -Not -BeNullOrEmpty |
| 113 | + $result.Name | Should -Be $script:testSnapshotNameFromDbObject |
| 114 | + $result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.Database' |
| 115 | + $result.DatabaseSnapshotBaseName | Should -Be $script:persistentSourceDatabase |
| 116 | + |
| 117 | + # Verify the snapshot exists |
| 118 | + $createdSnapshot = Get-SqlDscDatabase -ServerObject $script:serverObject -Name $script:testSnapshotNameFromDbObject -Refresh -ErrorAction 'Stop' |
| 119 | + $createdSnapshot | Should -Not -BeNullOrEmpty |
| 120 | + $createdSnapshot.DatabaseSnapshotBaseName | Should -Be $script:persistentSourceDatabase |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + Context 'When creating a database snapshot with custom file groups' { |
| 125 | + BeforeAll { |
| 126 | + # Get the default data directory from the server |
| 127 | + $script:dataDirectory = $script:serverObject.Settings.DefaultFile |
| 128 | + |
| 129 | + if (-not $script:dataDirectory) |
| 130 | + { |
| 131 | + $script:dataDirectory = $script:serverObject.Information.MasterDBPath |
| 132 | + } |
| 133 | + |
| 134 | + # Ensure the directory exists |
| 135 | + if (-not (Test-Path -Path $script:dataDirectory)) |
| 136 | + { |
| 137 | + $null = New-Item -Path $script:dataDirectory -ItemType Directory -Force |
| 138 | + } |
| 139 | + |
| 140 | + # Get the source database for file group creation |
| 141 | + $script:sourceDatabaseForFG = Get-SqlDscDatabase -ServerObject $script:serverObject -Name $script:persistentSourceDatabase -ErrorAction 'Stop' |
| 142 | + } |
| 143 | + |
| 144 | + It 'Should create a database snapshot with custom sparse file location' { |
| 145 | + # Create PRIMARY filegroup with sparse file using -AsSpec |
| 146 | + $sparseFilePath = Join-Path -Path $script:dataDirectory -ChildPath ($script:testSnapshotNameWithFileGroup + '_Sparse.ss') |
| 147 | + $dataFileSpec = New-SqlDscDataFile -Name ($script:testSnapshotNameWithFileGroup + '_Sparse') -FileName $sparseFilePath -AsSpec |
| 148 | + $primaryFileGroupSpec = New-SqlDscFileGroup -Name 'PRIMARY' -Files @($dataFileSpec) -AsSpec |
| 149 | + |
| 150 | + # Create snapshot with custom file group |
| 151 | + $result = New-SqlDscDatabaseSnapshot -ServerObject $script:serverObject -Name $script:testSnapshotNameWithFileGroup -DatabaseName $script:persistentSourceDatabase -FileGroup @($primaryFileGroupSpec) -Force -ErrorAction 'Stop' |
| 152 | + |
| 153 | + $result | Should -Not -BeNullOrEmpty |
| 154 | + $result.Name | Should -Be $script:testSnapshotNameWithFileGroup |
| 155 | + $result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.Database' |
| 156 | + $result.DatabaseSnapshotBaseName | Should -Be $script:persistentSourceDatabase |
| 157 | + |
| 158 | + # Verify the snapshot exists with correct file configuration |
| 159 | + $createdSnapshot = Get-SqlDscDatabase -ServerObject $script:serverObject -Name $script:testSnapshotNameWithFileGroup -Refresh -ErrorAction 'Stop' |
| 160 | + $createdSnapshot | Should -Not -BeNullOrEmpty |
| 161 | + $createdSnapshot.DatabaseSnapshotBaseName | Should -Be $script:persistentSourceDatabase |
| 162 | + |
| 163 | + # Verify the sparse file was created |
| 164 | + $createdSnapshot.FileGroups['PRIMARY'] | Should -Not -BeNullOrEmpty |
| 165 | + $createdSnapshot.FileGroups['PRIMARY'].Files.Count | Should -BeGreaterThan 0 |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + Context 'When using the Refresh parameter' { |
| 170 | + BeforeAll { |
| 171 | + $script:refreshTestSnapshotName = 'SqlDscTestSnapshotRefresh_' + (Get-Random) |
| 172 | + } |
| 173 | + |
| 174 | + AfterAll { |
| 175 | + # Clean up the refresh test snapshot |
| 176 | + $snapshotToRemove = Get-SqlDscDatabase -ServerObject $script:serverObject -Name $script:refreshTestSnapshotName -ErrorAction 'SilentlyContinue' |
| 177 | + if ($snapshotToRemove) |
| 178 | + { |
| 179 | + $null = Remove-SqlDscDatabase -DatabaseObject $snapshotToRemove -Force -ErrorAction 'Stop' |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + It 'Should refresh the database collection before creating snapshot' { |
| 184 | + $result = New-SqlDscDatabaseSnapshot -ServerObject $script:serverObject -Name $script:refreshTestSnapshotName -DatabaseName $script:persistentSourceDatabase -Refresh -Force -ErrorAction 'Stop' |
| 185 | + |
| 186 | + $result | Should -Not -BeNullOrEmpty |
| 187 | + $result.Name | Should -Be $script:refreshTestSnapshotName |
| 188 | + $result.DatabaseSnapshotBaseName | Should -Be $script:persistentSourceDatabase |
| 189 | + } |
| 190 | + } |
| 191 | +} |
0 commit comments