|
| 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 'Get-SqlDscStartupParameter' -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 | + $script:mockInstanceName = 'DSCSQLTEST' |
| 38 | + $script:mockServerName = Get-ComputerName |
| 39 | + } |
| 40 | + |
| 41 | + Context 'When using parameter set ByServerName' { |
| 42 | + Context 'When getting startup parameters with default parameters' { |
| 43 | + It 'Should return a StartupParameters object for the test instance' { |
| 44 | + $result = Get-SqlDscStartupParameter -InstanceName $script:mockInstanceName -ErrorAction 'Stop' |
| 45 | + |
| 46 | + $result | Should -Not -BeNullOrEmpty |
| 47 | + $result | Should -BeOfType (InModuleScope -ModuleName $script:moduleName -ScriptBlock { [StartupParameters] }) |
| 48 | + $result.DataFilePath | Should -Not -BeNullOrEmpty |
| 49 | + $result.LogFilePath | Should -Not -BeNullOrEmpty |
| 50 | + $result.ErrorLogPath | Should -Not -BeNullOrEmpty |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + Context 'When getting startup parameters for a specific instance' { |
| 55 | + It 'Should return a StartupParameters object for the specified instance' { |
| 56 | + $result = Get-SqlDscStartupParameter -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -ErrorAction 'Stop' |
| 57 | + |
| 58 | + $result | Should -Not -BeNullOrEmpty |
| 59 | + $result | Should -BeOfType (InModuleScope -ModuleName $script:moduleName -ScriptBlock { [StartupParameters] }) |
| 60 | + $result.DataFilePath | Should -Not -BeNullOrEmpty |
| 61 | + $result.LogFilePath | Should -Not -BeNullOrEmpty |
| 62 | + $result.ErrorLogPath | Should -Not -BeNullOrEmpty |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + Context 'When getting startup parameters for a specific server name' { |
| 67 | + It 'Should return a StartupParameters object for the specified server' { |
| 68 | + $result = Get-SqlDscStartupParameter -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -ErrorAction 'Stop' |
| 69 | + |
| 70 | + $result | Should -Not -BeNullOrEmpty |
| 71 | + $result | Should -BeOfType (InModuleScope -ModuleName $script:moduleName -ScriptBlock { [StartupParameters] }) |
| 72 | + $result.DataFilePath | Should -Not -BeNullOrEmpty |
| 73 | + $result.LogFilePath | Should -Not -BeNullOrEmpty |
| 74 | + $result.ErrorLogPath | Should -Not -BeNullOrEmpty |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + Context 'When getting startup parameters for a non-existent instance' { |
| 79 | + It 'Should throw an error when the instance does not exist' { |
| 80 | + { |
| 81 | + Get-SqlDscStartupParameter -ServerName $script:mockServerName -InstanceName 'NonExistentInstance' -ErrorAction 'Stop' |
| 82 | + } | Should -Throw -ErrorId 'GSDSP0001,Get-SqlDscStartupParameter' |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + Context 'When using parameter set ByServiceObject' { |
| 88 | + BeforeAll { |
| 89 | + $script:serviceObject = Get-SqlDscManagedComputerService -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -ServiceType 'DatabaseEngine' -ErrorAction 'Stop' |
| 90 | + } |
| 91 | + |
| 92 | + Context 'When getting startup parameters using a service object' { |
| 93 | + It 'Should return a StartupParameters object for the service object' { |
| 94 | + $result = Get-SqlDscStartupParameter -ServiceObject $script:serviceObject -ErrorAction 'Stop' |
| 95 | + |
| 96 | + $result | Should -Not -BeNullOrEmpty |
| 97 | + $result | Should -BeOfType (InModuleScope -ModuleName $script:moduleName -ScriptBlock { [StartupParameters] }) |
| 98 | + $result.DataFilePath | Should -Not -BeNullOrEmpty |
| 99 | + $result.LogFilePath | Should -Not -BeNullOrEmpty |
| 100 | + $result.ErrorLogPath | Should -Not -BeNullOrEmpty |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + Context 'When getting startup parameters using pipeline input' { |
| 105 | + It 'Should accept ServiceObject from pipeline and return StartupParameters' { |
| 106 | + $result = $script:serviceObject | Get-SqlDscStartupParameter -ErrorAction 'Stop' |
| 107 | + |
| 108 | + $result | Should -Not -BeNullOrEmpty |
| 109 | + $result | Should -BeOfType (InModuleScope -ModuleName $script:moduleName -ScriptBlock { [StartupParameters] }) |
| 110 | + $result.DataFilePath | Should -Not -BeNullOrEmpty |
| 111 | + $result.LogFilePath | Should -Not -BeNullOrEmpty |
| 112 | + $result.ErrorLogPath | Should -Not -BeNullOrEmpty |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + Context 'When passing wrong service type' { |
| 117 | + BeforeAll { |
| 118 | + # Get a non-DatabaseEngine service for testing |
| 119 | + $script:wrongServiceObject = Get-SqlDscManagedComputerService -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -ServiceType 'SqlServerAgent' -ErrorAction 'Stop' |
| 120 | + } |
| 121 | + |
| 122 | + It 'Should throw an error when the service type is not DatabaseEngine' { |
| 123 | + { |
| 124 | + Get-SqlDscStartupParameter -ServiceObject $script:wrongServiceObject -ErrorAction 'Stop' |
| 125 | + } | Should -Throw |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + Context 'When validating output properties' { |
| 131 | + BeforeAll { |
| 132 | + $script:result = Get-SqlDscStartupParameter -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -ErrorAction 'Stop' |
| 133 | + } |
| 134 | + |
| 135 | + It 'Should return an object with expected DataFilePath property' { |
| 136 | + $script:result.DataFilePath | Should -Not -BeNullOrEmpty |
| 137 | + $script:result.DataFilePath | Should -BeOfType ([System.String]) |
| 138 | + $script:result.DataFilePath | Should -Match '\.mdf$' |
| 139 | + } |
| 140 | + |
| 141 | + It 'Should return an object with expected LogFilePath property' { |
| 142 | + $script:result.LogFilePath | Should -Not -BeNullOrEmpty |
| 143 | + $script:result.LogFilePath | Should -BeOfType ([System.String]) |
| 144 | + $script:result.LogFilePath | Should -Match '\.ldf$' |
| 145 | + } |
| 146 | + |
| 147 | + It 'Should return an object with expected ErrorLogPath property' { |
| 148 | + $script:result.ErrorLogPath | Should -Not -BeNullOrEmpty |
| 149 | + $script:result.ErrorLogPath | Should -BeOfType ([System.String]) |
| 150 | + $script:result.ErrorLogPath | Should -Match 'ERRORLOG$' |
| 151 | + } |
| 152 | + |
| 153 | + It 'Should return TraceFlag property as expected type' { |
| 154 | + # TraceFlag can be null, a single UInt32, or an array |
| 155 | + if ($null -ne $script:result.TraceFlag) { |
| 156 | + # Check if it's either a single UInt32 or UInt32 array |
| 157 | + ($script:result.TraceFlag -is [System.UInt32]) -or ($script:result.TraceFlag -is [System.UInt32[]]) | Should -BeTrue -Because 'TraceFlag can be a single value or array depending on how many flags are set' |
| 158 | + } else { |
| 159 | + $script:result.TraceFlag | Should -BeNullOrEmpty -Because 'TraceFlag can be empty/null if no trace flags are set' |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + It 'Should return InternalTraceFlag property as expected type' { |
| 164 | + # InternalTraceFlag can be null, a single UInt32, or an array |
| 165 | + if ($null -ne $script:result.InternalTraceFlag) { |
| 166 | + # Check if it's either a single UInt32 or UInt32 array |
| 167 | + ($script:result.InternalTraceFlag -is [System.UInt32]) -or ($script:result.InternalTraceFlag -is [System.UInt32[]]) | Should -BeTrue -Because 'InternalTraceFlag can be a single value or array depending on how many flags are set' |
| 168 | + } else { |
| 169 | + $script:result.InternalTraceFlag | Should -BeNullOrEmpty -Because 'InternalTraceFlag can be empty/null if no internal trace flags are set' |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + Context 'When comparing results from different parameter sets' { |
| 175 | + It 'Should return the same results for ByServerName and ByServiceObject parameter sets' { |
| 176 | + $resultByServerName = Get-SqlDscStartupParameter -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -ErrorAction 'Stop' |
| 177 | + |
| 178 | + $serviceObject = Get-SqlDscManagedComputerService -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -ServiceType 'DatabaseEngine' -ErrorAction 'Stop' |
| 179 | + $resultByServiceObject = Get-SqlDscStartupParameter -ServiceObject $serviceObject -ErrorAction 'Stop' |
| 180 | + |
| 181 | + $resultByServerName.DataFilePath | Should -Be $resultByServiceObject.DataFilePath |
| 182 | + $resultByServerName.LogFilePath | Should -Be $resultByServiceObject.LogFilePath |
| 183 | + $resultByServerName.ErrorLogPath | Should -Be $resultByServiceObject.ErrorLogPath |
| 184 | + $resultByServerName.TraceFlag | Should -Be $resultByServiceObject.TraceFlag |
| 185 | + $resultByServerName.InternalTraceFlag | Should -Be $resultByServiceObject.InternalTraceFlag |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments