|
| 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 | +# cSpell: ignore DSCSQLTEST |
| 33 | +Describe 'Initialize-SqlDscRebuildDatabase' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') { |
| 34 | + BeforeAll { |
| 35 | + Write-Verbose -Message ('Running integration test as user ''{0}''.' -f $env:UserName) -Verbose |
| 36 | + |
| 37 | + # Stop the SQL Server instance so we can rebuild the databases |
| 38 | + $serviceName = 'MSSQL$DSCSQLTEST' |
| 39 | + $sqlService = Get-Service -Name $serviceName -ErrorAction 'Stop' |
| 40 | + |
| 41 | + if ($sqlService.Status -eq 'Running') |
| 42 | + { |
| 43 | + Write-Verbose -Message "Stopping SQL Server service '$serviceName'..." -Verbose |
| 44 | + Stop-Service -Name $serviceName -Force -ErrorAction 'Stop' |
| 45 | + Start-Sleep -Seconds 5 |
| 46 | + } |
| 47 | + |
| 48 | + # Verify service is stopped |
| 49 | + $sqlService = Get-Service -Name $serviceName -ErrorAction 'Stop' |
| 50 | + if ($sqlService.Status -ne 'Stopped') |
| 51 | + { |
| 52 | + Write-Error -Message "Failed to stop SQL Server service '$serviceName'" |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + AfterAll { |
| 57 | + # Ensure SQL Server service is running after tests |
| 58 | + $serviceName = 'MSSQL$DSCSQLTEST' |
| 59 | + $sqlService = Get-Service -Name $serviceName -ErrorAction 'Stop' |
| 60 | + |
| 61 | + if ($sqlService.Status -ne 'Running') |
| 62 | + { |
| 63 | + Write-Verbose -Message "Starting SQL Server service '$serviceName'..." -Verbose |
| 64 | + Start-Service -Name $serviceName -ErrorAction 'Stop' |
| 65 | + Start-Sleep -Seconds 10 |
| 66 | + } |
| 67 | + |
| 68 | + # Verify service is running |
| 69 | + $sqlService = Get-Service -Name $serviceName -ErrorAction 'Stop' |
| 70 | + if ($sqlService.Status -ne 'Running') |
| 71 | + { |
| 72 | + Write-Error -Message "Failed to start SQL Server service '$serviceName'" |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + Context 'When rebuilding database on a named instance' { |
| 77 | + Context 'When specifying only mandatory parameters' { |
| 78 | + It 'Should run the rebuild command without throwing (using Force parameter)' { |
| 79 | + # Set splatting parameters for Initialize-SqlDscRebuildDatabase |
| 80 | + $rebuildSqlDscDatabaseParameters = @{ |
| 81 | + InstanceName = 'DSCSQLTEST' |
| 82 | + SqlSysAdminAccounts = @( |
| 83 | + ('{0}\SqlAdmin' -f (Get-ComputerName)) |
| 84 | + ) |
| 85 | + MediaPath = $env:IsoDrivePath |
| 86 | + Verbose = $true |
| 87 | + ErrorAction = 'Stop' |
| 88 | + Force = $true |
| 89 | + } |
| 90 | + |
| 91 | + $null = Initialize-SqlDscRebuildDatabase @rebuildSqlDscDatabaseParameters |
| 92 | + } |
| 93 | + |
| 94 | + It 'Should have the SQL Server service running after rebuild' { |
| 95 | + $serviceName = 'MSSQL$DSCSQLTEST' |
| 96 | + Start-Service -Name $serviceName -ErrorAction 'Stop' |
| 97 | + Start-Sleep -Seconds 10 |
| 98 | + |
| 99 | + $sqlService = Get-Service -Name $serviceName -ErrorAction 'Stop' |
| 100 | + $sqlService.Status | Should -Be 'Running' |
| 101 | + } |
| 102 | + |
| 103 | + It 'Should be able to connect to the instance after rebuild' { |
| 104 | + $computerName = Get-ComputerName |
| 105 | + $mockSqlAdministratorUserName = 'SqlAdmin' |
| 106 | + $mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force |
| 107 | + |
| 108 | + $mockSqlAdminCredential = [System.Management.Automation.PSCredential]::new( |
| 109 | + $mockSqlAdministratorUserName, |
| 110 | + $mockSqlAdministratorPassword |
| 111 | + ) |
| 112 | + |
| 113 | + $serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'DSCSQLTEST' -Credential $mockSqlAdminCredential -ErrorAction 'Stop' |
| 114 | + |
| 115 | + $serverObject | Should -Not -BeNullOrEmpty |
| 116 | + $serverObject.Name | Should -Match 'DSCSQLTEST' |
| 117 | + |
| 118 | + Disconnect-SqlDscDatabaseEngine -ServerObject $serverObject |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + Context 'When specifying optional TempDB parameters' { |
| 123 | + It 'Should run the rebuild command with custom TempDB file count' { |
| 124 | + # Set splatting parameters for Initialize-SqlDscRebuildDatabase |
| 125 | + $rebuildSqlDscDatabaseParameters = @{ |
| 126 | + InstanceName = 'DSCSQLTEST' |
| 127 | + SqlSysAdminAccounts = @( |
| 128 | + ('{0}\SqlAdmin' -f (Get-ComputerName)) |
| 129 | + ) |
| 130 | + MediaPath = $env:IsoDrivePath |
| 131 | + SqlTempDbFileCount = 8 |
| 132 | + Verbose = $true |
| 133 | + ErrorAction = 'Stop' |
| 134 | + Force = $true |
| 135 | + } |
| 136 | + |
| 137 | + $null = Initialize-SqlDscRebuildDatabase @rebuildSqlDscDatabaseParameters |
| 138 | + } |
| 139 | + |
| 140 | + It 'Should have the SQL Server service running after rebuild with TempDB parameters' { |
| 141 | + $serviceName = 'MSSQL$DSCSQLTEST' |
| 142 | + Start-Service -Name $serviceName -ErrorAction 'Stop' |
| 143 | + Start-Sleep -Seconds 10 |
| 144 | + |
| 145 | + $sqlService = Get-Service -Name $serviceName -ErrorAction 'Stop' |
| 146 | + $sqlService.Status | Should -Be 'Running' |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + Context 'When specifying optional SqlCollation parameter' { |
| 151 | + It 'Should run the rebuild command with custom collation' { |
| 152 | + # Set splatting parameters for Initialize-SqlDscRebuildDatabase |
| 153 | + $rebuildSqlDscDatabaseParameters = @{ |
| 154 | + InstanceName = 'DSCSQLTEST' |
| 155 | + SqlSysAdminAccounts = @( |
| 156 | + ('{0}\SqlAdmin' -f (Get-ComputerName)) |
| 157 | + ) |
| 158 | + MediaPath = $env:IsoDrivePath |
| 159 | + SqlCollation = 'SQL_Latin1_General_CP1_CI_AS' |
| 160 | + Verbose = $true |
| 161 | + ErrorAction = 'Stop' |
| 162 | + Force = $true |
| 163 | + } |
| 164 | + |
| 165 | + $null = Initialize-SqlDscRebuildDatabase @rebuildSqlDscDatabaseParameters |
| 166 | + } |
| 167 | + |
| 168 | + It 'Should have the SQL Server service running after rebuild with collation' { |
| 169 | + $serviceName = 'MSSQL$DSCSQLTEST' |
| 170 | + Start-Service -Name $serviceName -ErrorAction 'Stop' |
| 171 | + Start-Sleep -Seconds 10 |
| 172 | + |
| 173 | + $sqlService = Get-Service -Name $serviceName -ErrorAction 'Stop' |
| 174 | + $sqlService.Status | Should -Be 'Running' |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | +} |
0 commit comments