|
| 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-SqlDscManagedComputerService' -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 | + # Starting the named instance SQL Server service prior to running tests. |
| 38 | + Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop' |
| 39 | + |
| 40 | + $script:mockInstanceName = 'DSCSQLTEST' |
| 41 | + $script:mockServerName = Get-ComputerName |
| 42 | + } |
| 43 | + |
| 44 | + AfterAll { |
| 45 | + # Stop the named instance SQL Server service to save memory on the build worker. |
| 46 | + Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop' |
| 47 | + } |
| 48 | + |
| 49 | + Context 'When using parameter set ByServerName' { |
| 50 | + Context 'When getting all services on the current managed computer' { |
| 51 | + It 'Should return all available services' { |
| 52 | + $result = Get-SqlDscManagedComputerService -ErrorAction 'Stop' |
| 53 | + |
| 54 | + $result | Should -Not -BeNullOrEmpty |
| 55 | + $result | Should -BeOfType ([Microsoft.SqlServer.Management.Smo.Wmi.Service]) |
| 56 | + |
| 57 | + # Should contain SQL Server related services |
| 58 | + $sqlServices = $result | Where-Object -FilterScript { $_.Name -like '*SQL*' } |
| 59 | + $sqlServices | Should -Not -BeNullOrEmpty |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + Context 'When getting all services on the specified managed computer' { |
| 64 | + It 'Should return all available services' { |
| 65 | + $result = Get-SqlDscManagedComputerService -ServerName $script:mockServerName -ErrorAction 'Stop' |
| 66 | + |
| 67 | + $result | Should -Not -BeNullOrEmpty |
| 68 | + $result | Should -BeOfType ([Microsoft.SqlServer.Management.Smo.Wmi.Service]) |
| 69 | + |
| 70 | + # Should contain SQL Server related services |
| 71 | + $sqlServices = $result | Where-Object -FilterScript { $_.Name -like '*SQL*' } |
| 72 | + $sqlServices | Should -Not -BeNullOrEmpty |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + Context 'When filtering by ServiceType' { |
| 77 | + It 'Should return only Database Engine services' { |
| 78 | + $result = Get-SqlDscManagedComputerService -ServerName $script:mockServerName -ServiceType 'DatabaseEngine' -ErrorAction 'Stop' |
| 79 | + |
| 80 | + $result | Should -Not -BeNullOrEmpty |
| 81 | + $result | Should -BeOfType ([Microsoft.SqlServer.Management.Smo.Wmi.Service]) |
| 82 | + |
| 83 | + # All returned services should be of type SqlServer |
| 84 | + foreach ($service in $result) |
| 85 | + { |
| 86 | + $service.Type | Should -Be 'SqlServer' |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + It 'Should return SQL Server Browser service when filtering by SQLServerBrowser' { |
| 91 | + $result = Get-SqlDscManagedComputerService -ServerName $script:mockServerName -ServiceType 'SQLServerBrowser' -ErrorAction 'Stop' |
| 92 | + |
| 93 | + if ($result) |
| 94 | + { |
| 95 | + $result | Should -BeOfType ([Microsoft.SqlServer.Management.Smo.Wmi.Service]) |
| 96 | + $result.Type | Should -Be 'SqlBrowser' |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + Context 'When filtering by InstanceName' { |
| 102 | + It 'Should return services for the specified instance' { |
| 103 | + $result = Get-SqlDscManagedComputerService -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -ErrorAction 'Stop' |
| 104 | + |
| 105 | + if ($result) |
| 106 | + { |
| 107 | + $result | Should -BeOfType ([Microsoft.SqlServer.Management.Smo.Wmi.Service]) |
| 108 | + |
| 109 | + # All returned services should contain the instance name |
| 110 | + foreach ($service in $result) |
| 111 | + { |
| 112 | + $service.Name | Should -Match ('\$' + $script:mockInstanceName + '$') |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + It 'Should return services for the default instance when filtering by MSSQLSERVER' { |
| 118 | + $result = Get-SqlDscManagedComputerService -ServerName $script:mockServerName -InstanceName 'MSSQLSERVER' -ErrorAction 'Stop' |
| 119 | + |
| 120 | + if ($result) |
| 121 | + { |
| 122 | + $result | Should -BeOfType ([Microsoft.SqlServer.Management.Smo.Wmi.Service]) |
| 123 | + |
| 124 | + # Should contain the default instance service |
| 125 | + $defaultInstanceService = $result | Where-Object -FilterScript { $_.Name -eq 'MSSQLSERVER' } |
| 126 | + $defaultInstanceService | Should -Not -BeNullOrEmpty |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + Context 'When using parameter set ByManagedComputerObject' { |
| 133 | + BeforeAll { |
| 134 | + $script:managedComputerObject = Get-SqlDscManagedComputer -ServerName $script:mockServerName -ErrorAction 'Stop' |
| 135 | + } |
| 136 | + |
| 137 | + Context 'When getting all services from managed computer object' { |
| 138 | + It 'Should return all available services' { |
| 139 | + $result = $script:managedComputerObject | Get-SqlDscManagedComputerService -ErrorAction 'Stop' |
| 140 | + |
| 141 | + $result | Should -Not -BeNullOrEmpty |
| 142 | + $result | Should -BeOfType ([Microsoft.SqlServer.Management.Smo.Wmi.Service]) |
| 143 | + |
| 144 | + # Should contain SQL Server related services |
| 145 | + $sqlServices = $result | Where-Object -FilterScript { $_.Name -like '*SQL*' } |
| 146 | + $sqlServices | Should -Not -BeNullOrEmpty |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + Context 'When filtering by ServiceType from managed computer object' { |
| 151 | + It 'Should return only Database Engine services' { |
| 152 | + $result = $script:managedComputerObject | Get-SqlDscManagedComputerService -ServiceType 'DatabaseEngine' -ErrorAction 'Stop' |
| 153 | + |
| 154 | + $result | Should -Not -BeNullOrEmpty |
| 155 | + $result | Should -BeOfType ([Microsoft.SqlServer.Management.Smo.Wmi.Service]) |
| 156 | + |
| 157 | + # All returned services should be of type SqlServer |
| 158 | + foreach ($service in $result) |
| 159 | + { |
| 160 | + $service.Type | Should -Be 'SqlServer' |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + Context 'When filtering by InstanceName from managed computer object' { |
| 166 | + It 'Should return services for the specified instance' { |
| 167 | + $result = $script:managedComputerObject | Get-SqlDscManagedComputerService -InstanceName $script:mockInstanceName -ErrorAction 'Stop' |
| 168 | + |
| 169 | + if ($result) |
| 170 | + { |
| 171 | + $result | Should -BeOfType ([Microsoft.SqlServer.Management.Smo.Wmi.Service]) |
| 172 | + |
| 173 | + # All returned services should contain the instance name |
| 174 | + foreach ($service in $result) |
| 175 | + { |
| 176 | + $service.Name | Should -Match ('\$' + $script:mockInstanceName + '$') |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + Context 'When validating SMO object properties' { |
| 184 | + It 'Should return objects with correct SMO properties' { |
| 185 | + $result = Get-SqlDscManagedComputerService -ServerName $script:mockServerName -ErrorAction 'Stop' |
| 186 | + |
| 187 | + $result | Should -Not -BeNullOrEmpty |
| 188 | + |
| 189 | + # Verify it's a proper SMO Service object |
| 190 | + $result | Should -BeOfType ([Microsoft.SqlServer.Management.Smo.Wmi.Service]) |
| 191 | + |
| 192 | + # Verify key properties exist for at least one service |
| 193 | + $firstService = $result | Select-Object -First 1 |
| 194 | + $firstService.Name | Should -Not -BeNullOrEmpty |
| 195 | + $firstService.Type | Should -Not -BeNullOrEmpty |
| 196 | + |
| 197 | + # Verify the service has access to its parent ManagedComputer |
| 198 | + $firstService.Parent | Should -Not -BeNullOrEmpty |
| 199 | + $firstService.Parent.Name | Should -Be $script:mockServerName |
| 200 | + } |
| 201 | + } |
| 202 | +} |
0 commit comments