|
| 1 | +[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification = 'Suppressing this rule because Script Analyzer does not understand Pester syntax.')] |
| 2 | +[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '', Justification = 'because ConvertTo-SecureString is used to simplify the tests.')] |
| 3 | +param () |
| 4 | + |
| 5 | +BeforeDiscovery { |
| 6 | + try |
| 7 | + { |
| 8 | + if (-not (Get-Module -Name 'DscResource.Test')) |
| 9 | + { |
| 10 | + # Assumes dependencies have been resolved, so if this module is not available, run 'noop' task. |
| 11 | + if (-not (Get-Module -Name 'DscResource.Test' -ListAvailable)) |
| 12 | + { |
| 13 | + # Redirect all streams to $null, except the error stream (stream 2) |
| 14 | + & "$PSScriptRoot/../../../build.ps1" -Tasks 'noop' 3>&1 4>&1 5>&1 6>&1 > $null |
| 15 | + } |
| 16 | + |
| 17 | + # If the dependencies have not been resolved, this will throw an error. |
| 18 | + Import-Module -Name 'DscResource.Test' -Force -ErrorAction 'Stop' |
| 19 | + } |
| 20 | + } |
| 21 | + catch [System.IO.FileNotFoundException] |
| 22 | + { |
| 23 | + throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -ResolveDependency -Tasks noop" first.' |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +BeforeAll { |
| 28 | + $script:moduleName = 'SqlServerDsc' |
| 29 | + |
| 30 | + Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop' |
| 31 | +} |
| 32 | + |
| 33 | +# cSpell: ignore DSCSQLTEST |
| 34 | +Describe 'Initialize-SqlDscRebuildDatabase' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') { |
| 35 | + BeforeAll { |
| 36 | + Write-Verbose -Message ('Running integration test as user ''{0}''.' -f $env:UserName) -Verbose |
| 37 | + |
| 38 | + # Stop the SQL Server instance so we can rebuild the databases |
| 39 | + $serviceName = 'MSSQL$DSCSQLTEST' |
| 40 | + $sqlService = Get-Service -Name $serviceName -ErrorAction 'Stop' |
| 41 | + |
| 42 | + if ($sqlService.Status -eq 'Running') |
| 43 | + { |
| 44 | + Write-Verbose -Message "Stopping SQL Server service '$serviceName'..." -Verbose |
| 45 | + Stop-Service -Name $serviceName -Force -ErrorAction 'Stop' |
| 46 | + Start-Sleep -Seconds 5 |
| 47 | + } |
| 48 | + |
| 49 | + # Verify service is stopped |
| 50 | + $sqlService = Get-Service -Name $serviceName -ErrorAction 'Stop' |
| 51 | + if ($sqlService.Status -ne 'Stopped') |
| 52 | + { |
| 53 | + Write-Error -Message "Failed to stop SQL Server service '$serviceName'" |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + AfterAll { |
| 58 | + # Ensure SQL Server service is running after tests |
| 59 | + $serviceName = 'MSSQL$DSCSQLTEST' |
| 60 | + $sqlService = Get-Service -Name $serviceName -ErrorAction 'Stop' |
| 61 | + |
| 62 | + if ($sqlService.Status -ne 'Running') |
| 63 | + { |
| 64 | + Write-Verbose -Message "Starting SQL Server service '$serviceName'..." -Verbose |
| 65 | + Start-Service -Name $serviceName -ErrorAction 'Stop' |
| 66 | + Start-Sleep -Seconds 10 |
| 67 | + } |
| 68 | + |
| 69 | + # Verify service is running |
| 70 | + $sqlService = Get-Service -Name $serviceName -ErrorAction 'Stop' |
| 71 | + if ($sqlService.Status -ne 'Running') |
| 72 | + { |
| 73 | + Write-Error -Message "Failed to start SQL Server service '$serviceName'" |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + Context 'When rebuilding database on a named instance' { |
| 78 | + Context 'When specifying only mandatory parameters' { |
| 79 | + It 'Should run the rebuild command without throwing (using Force parameter)' { |
| 80 | + # Set splatting parameters for Initialize-SqlDscRebuildDatabase |
| 81 | + $saPwd = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force |
| 82 | + |
| 83 | + $rebuildSqlDscDatabaseParameters = @{ |
| 84 | + InstanceName = 'DSCSQLTEST' |
| 85 | + SAPwd = $saPwd |
| 86 | + SqlSysAdminAccounts = @( |
| 87 | + ('{0}\SqlAdmin' -f (Get-ComputerName)) |
| 88 | + ) |
| 89 | + MediaPath = $env:IsoDrivePath |
| 90 | + Verbose = $true |
| 91 | + ErrorAction = 'Stop' |
| 92 | + Force = $true |
| 93 | + } |
| 94 | + |
| 95 | + try |
| 96 | + { |
| 97 | + $null = Initialize-SqlDscRebuildDatabase @rebuildSqlDscDatabaseParameters |
| 98 | + } |
| 99 | + catch |
| 100 | + { |
| 101 | + # Output Summary.txt if it exists to help diagnose the failure |
| 102 | + $summaryFiles = Get-ChildItem -Path 'C:\Program Files\Microsoft SQL Server' -Filter 'Summary.txt' -Recurse -ErrorAction SilentlyContinue | |
| 103 | + Where-Object { $_.FullName -match '\\Setup Bootstrap\\Log\\' } | |
| 104 | + Sort-Object -Property LastWriteTime -Descending | |
| 105 | + Select-Object -First 1 |
| 106 | + |
| 107 | + if ($summaryFiles) |
| 108 | + { |
| 109 | + Write-Verbose "==== SQL Server Setup Summary.txt (from $($summaryFiles.FullName)) ====" -Verbose |
| 110 | + Get-Content -Path $summaryFiles.FullName | Write-Verbose -Verbose |
| 111 | + Write-Verbose "==== End of Summary.txt ====" -Verbose |
| 112 | + } |
| 113 | + else |
| 114 | + { |
| 115 | + Write-Verbose 'No Summary.txt file found.' -Verbose |
| 116 | + } |
| 117 | + |
| 118 | + # Re-throw the original error |
| 119 | + throw $_ |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + It 'Should have the SQL Server service running after rebuild' { |
| 124 | + $serviceName = 'MSSQL$DSCSQLTEST' |
| 125 | + Start-Service -Name $serviceName -ErrorAction 'Stop' |
| 126 | + Start-Sleep -Seconds 10 |
| 127 | + |
| 128 | + $sqlService = Get-Service -Name $serviceName -ErrorAction 'Stop' |
| 129 | + $sqlService.Status | Should -Be 'Running' |
| 130 | + } |
| 131 | + |
| 132 | + It 'Should be able to connect to the instance after rebuild' { |
| 133 | + $computerName = Get-ComputerName |
| 134 | + $mockSqlAdministratorUserName = 'SqlAdmin' |
| 135 | + $mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force |
| 136 | + |
| 137 | + $mockSqlAdminCredential = [System.Management.Automation.PSCredential]::new( |
| 138 | + $mockSqlAdministratorUserName, |
| 139 | + $mockSqlAdministratorPassword |
| 140 | + ) |
| 141 | + |
| 142 | + $serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'DSCSQLTEST' -Credential $mockSqlAdminCredential -ErrorAction 'Stop' |
| 143 | + |
| 144 | + $serverObject | Should -Not -BeNullOrEmpty |
| 145 | + $serverObject.Name | Should -Match 'DSCSQLTEST' |
| 146 | + |
| 147 | + Disconnect-SqlDscDatabaseEngine -ServerObject $serverObject |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + Context 'When specifying optional TempDB parameters' { |
| 152 | + It 'Should run the rebuild command with custom TempDB file count' { |
| 153 | + # Set splatting parameters for Initialize-SqlDscRebuildDatabase |
| 154 | + $saPwd = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force |
| 155 | + |
| 156 | + $rebuildSqlDscDatabaseParameters = @{ |
| 157 | + InstanceName = 'DSCSQLTEST' |
| 158 | + SAPwd = $saPwd |
| 159 | + SqlSysAdminAccounts = @( |
| 160 | + ('{0}\SqlAdmin' -f (Get-ComputerName)) |
| 161 | + ) |
| 162 | + MediaPath = $env:IsoDrivePath |
| 163 | + SqlTempDbFileCount = 8 |
| 164 | + Verbose = $true |
| 165 | + ErrorAction = 'Stop' |
| 166 | + Force = $true |
| 167 | + } |
| 168 | + |
| 169 | + try |
| 170 | + { |
| 171 | + $null = Initialize-SqlDscRebuildDatabase @rebuildSqlDscDatabaseParameters |
| 172 | + } |
| 173 | + catch |
| 174 | + { |
| 175 | + # Output Summary.txt if it exists to help diagnose the failure |
| 176 | + $summaryFiles = Get-ChildItem -Path 'C:\Program Files\Microsoft SQL Server' -Filter 'Summary.txt' -Recurse -ErrorAction SilentlyContinue | |
| 177 | + Where-Object { $_.FullName -match '\\Setup Bootstrap\\Log\\' } | |
| 178 | + Sort-Object -Property LastWriteTime -Descending | |
| 179 | + Select-Object -First 1 |
| 180 | + |
| 181 | + if ($summaryFiles) |
| 182 | + { |
| 183 | + Write-Verbose "==== SQL Server Setup Summary.txt (from $($summaryFiles.FullName)) ====" -Verbose |
| 184 | + Get-Content -Path $summaryFiles.FullName | Write-Verbose -Verbose |
| 185 | + Write-Verbose "==== End of Summary.txt ====" -Verbose |
| 186 | + } |
| 187 | + else |
| 188 | + { |
| 189 | + Write-Verbose 'No Summary.txt file found.' -Verbose |
| 190 | + } |
| 191 | + |
| 192 | + # Re-throw the original error |
| 193 | + throw $_ |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + It 'Should have the SQL Server service running after rebuild with TempDB parameters' { |
| 198 | + $serviceName = 'MSSQL$DSCSQLTEST' |
| 199 | + Start-Service -Name $serviceName -ErrorAction 'Stop' |
| 200 | + Start-Sleep -Seconds 10 |
| 201 | + |
| 202 | + $sqlService = Get-Service -Name $serviceName -ErrorAction 'Stop' |
| 203 | + $sqlService.Status | Should -Be 'Running' |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + Context 'When specifying optional SqlCollation parameter' { |
| 208 | + It 'Should run the rebuild command with custom collation' { |
| 209 | + # Set splatting parameters for Initialize-SqlDscRebuildDatabase |
| 210 | + $saPwd = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force |
| 211 | + |
| 212 | + $rebuildSqlDscDatabaseParameters = @{ |
| 213 | + InstanceName = 'DSCSQLTEST' |
| 214 | + SAPwd = $saPwd |
| 215 | + SqlSysAdminAccounts = @( |
| 216 | + ('{0}\SqlAdmin' -f (Get-ComputerName)) |
| 217 | + ) |
| 218 | + MediaPath = $env:IsoDrivePath |
| 219 | + SqlCollation = 'SQL_Latin1_General_CP1_CI_AS' |
| 220 | + Verbose = $true |
| 221 | + ErrorAction = 'Stop' |
| 222 | + Force = $true |
| 223 | + } |
| 224 | + |
| 225 | + try |
| 226 | + { |
| 227 | + $null = Initialize-SqlDscRebuildDatabase @rebuildSqlDscDatabaseParameters |
| 228 | + } |
| 229 | + catch |
| 230 | + { |
| 231 | + # Output Summary.txt if it exists to help diagnose the failure |
| 232 | + $summaryFiles = Get-ChildItem -Path 'C:\Program Files\Microsoft SQL Server' -Filter 'Summary.txt' -Recurse -ErrorAction SilentlyContinue | |
| 233 | + Where-Object { $_.FullName -match '\\Setup Bootstrap\\Log\\' } | |
| 234 | + Sort-Object -Property LastWriteTime -Descending | |
| 235 | + Select-Object -First 1 |
| 236 | + |
| 237 | + if ($summaryFiles) |
| 238 | + { |
| 239 | + Write-Verbose "==== SQL Server Setup Summary.txt (from $($summaryFiles.FullName)) ====" -Verbose |
| 240 | + Get-Content -Path $summaryFiles.FullName | Write-Verbose -Verbose |
| 241 | + Write-Verbose "==== End of Summary.txt ====" -Verbose |
| 242 | + } |
| 243 | + else |
| 244 | + { |
| 245 | + Write-Verbose 'No Summary.txt file found.' -Verbose |
| 246 | + } |
| 247 | + |
| 248 | + # Re-throw the original error |
| 249 | + throw $_ |
| 250 | + } |
| 251 | + } |
| 252 | + |
| 253 | + It 'Should have the SQL Server service running after rebuild with collation' { |
| 254 | + $serviceName = 'MSSQL$DSCSQLTEST' |
| 255 | + Start-Service -Name $serviceName -ErrorAction 'Stop' |
| 256 | + Start-Sleep -Seconds 10 |
| 257 | + |
| 258 | + $sqlService = Get-Service -Name $serviceName -ErrorAction 'Stop' |
| 259 | + $sqlService.Status | Should -Be 'Running' |
| 260 | + } |
| 261 | + } |
| 262 | + } |
| 263 | +} |
0 commit comments