-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathGet-SqlDscManagedComputerService.Integration.Tests.ps1
More file actions
202 lines (162 loc) · 8.69 KB
/
Get-SqlDscManagedComputerService.Integration.Tests.ps1
File metadata and controls
202 lines (162 loc) · 8.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification = 'Suppressing this rule because Script Analyzer does not understand Pester syntax.')]
param ()
BeforeDiscovery {
try
{
if (-not (Get-Module -Name 'DscResource.Test'))
{
# Assumes dependencies have been resolved, so if this module is not available, run 'noop' task.
if (-not (Get-Module -Name 'DscResource.Test' -ListAvailable))
{
# Redirect all streams to $null, except the error stream (stream 2)
& "$PSScriptRoot/../../../build.ps1" -Tasks 'noop' 3>&1 4>&1 5>&1 6>&1 > $null
}
# If the dependencies have not been resolved, this will throw an error.
Import-Module -Name 'DscResource.Test' -Force -ErrorAction 'Stop'
}
}
catch [System.IO.FileNotFoundException]
{
throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -ResolveDependency -Tasks build" first.'
}
}
BeforeAll {
$script:moduleName = 'SqlServerDsc'
Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop'
}
# cSpell: ignore DSCSQLTEST
Describe 'Get-SqlDscManagedComputerService' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
BeforeAll {
Write-Verbose -Message ('Running integration test as user ''{0}''.' -f $env:UserName) -Verbose
# Starting the named instance SQL Server service prior to running tests.
Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
$script:mockInstanceName = 'DSCSQLTEST'
$script:mockServerName = Get-ComputerName
}
AfterAll {
# Stop the named instance SQL Server service to save memory on the build worker.
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
}
Context 'When using parameter set ByServerName' {
Context 'When getting all services on the current managed computer' {
It 'Should return all available services' {
$result = Get-SqlDscManagedComputerService -ErrorAction 'Stop'
$result | Should -Not -BeNullOrEmpty
$result | Should -BeOfType ([Microsoft.SqlServer.Management.Smo.Wmi.Service])
# Should contain SQL Server related services
$sqlServices = $result | Where-Object -FilterScript { $_.Name -like '*SQL*' }
$sqlServices | Should -Not -BeNullOrEmpty
}
}
Context 'When getting all services on the specified managed computer' {
It 'Should return all available services' {
$result = Get-SqlDscManagedComputerService -ServerName $script:mockServerName -ErrorAction 'Stop'
$result | Should -Not -BeNullOrEmpty
$result | Should -BeOfType ([Microsoft.SqlServer.Management.Smo.Wmi.Service])
# Should contain SQL Server related services
$sqlServices = $result | Where-Object -FilterScript { $_.Name -like '*SQL*' }
$sqlServices | Should -Not -BeNullOrEmpty
}
}
Context 'When filtering by ServiceType' {
It 'Should return only Database Engine services' {
$result = Get-SqlDscManagedComputerService -ServerName $script:mockServerName -ServiceType 'DatabaseEngine' -ErrorAction 'Stop'
$result | Should -Not -BeNullOrEmpty
$result | Should -BeOfType ([Microsoft.SqlServer.Management.Smo.Wmi.Service])
# All returned services should be of type SqlServer
foreach ($service in $result)
{
$service.Type | Should -Be 'SqlServer'
}
}
It 'Should return SQL Server Browser service when filtering by SQLServerBrowser' {
$result = Get-SqlDscManagedComputerService -ServerName $script:mockServerName -ServiceType 'SQLServerBrowser' -ErrorAction 'Stop'
if ($result)
{
$result | Should -BeOfType ([Microsoft.SqlServer.Management.Smo.Wmi.Service])
$result.Type | Should -Be 'SqlBrowser'
}
}
}
Context 'When filtering by InstanceName' {
It 'Should return services for the specified instance' {
$result = Get-SqlDscManagedComputerService -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -ErrorAction 'Stop'
if ($result)
{
$result | Should -BeOfType ([Microsoft.SqlServer.Management.Smo.Wmi.Service])
# All returned services should contain the instance name
foreach ($service in $result)
{
$service.Name | Should -Match ('\$' + $script:mockInstanceName + '$')
}
}
}
It 'Should return services for the default instance when filtering by MSSQLSERVER' {
$result = Get-SqlDscManagedComputerService -ServerName $script:mockServerName -InstanceName 'MSSQLSERVER' -ErrorAction 'Stop'
if ($result)
{
$result | Should -BeOfType ([Microsoft.SqlServer.Management.Smo.Wmi.Service])
# Should contain the default instance service
$defaultInstanceService = $result | Where-Object -FilterScript { $_.Name -eq 'MSSQLSERVER' }
$defaultInstanceService | Should -Not -BeNullOrEmpty
}
}
}
}
Context 'When using parameter set ByManagedComputerObject' {
BeforeAll {
$script:managedComputerObject = Get-SqlDscManagedComputer -ServerName $script:mockServerName -ErrorAction 'Stop'
}
Context 'When getting all services from managed computer object' {
It 'Should return all available services' {
$result = $script:managedComputerObject | Get-SqlDscManagedComputerService -ErrorAction 'Stop'
$result | Should -Not -BeNullOrEmpty
$result | Should -BeOfType ([Microsoft.SqlServer.Management.Smo.Wmi.Service])
# Should contain SQL Server related services
$sqlServices = $result | Where-Object -FilterScript { $_.Name -like '*SQL*' }
$sqlServices | Should -Not -BeNullOrEmpty
}
}
Context 'When filtering by ServiceType from managed computer object' {
It 'Should return only Database Engine services' {
$result = $script:managedComputerObject | Get-SqlDscManagedComputerService -ServiceType 'DatabaseEngine' -ErrorAction 'Stop'
$result | Should -Not -BeNullOrEmpty
$result | Should -BeOfType ([Microsoft.SqlServer.Management.Smo.Wmi.Service])
# All returned services should be of type SqlServer
foreach ($service in $result)
{
$service.Type | Should -Be 'SqlServer'
}
}
}
Context 'When filtering by InstanceName from managed computer object' {
It 'Should return services for the specified instance' {
$result = $script:managedComputerObject | Get-SqlDscManagedComputerService -InstanceName $script:mockInstanceName -ErrorAction 'Stop'
if ($result)
{
$result | Should -BeOfType ([Microsoft.SqlServer.Management.Smo.Wmi.Service])
# All returned services should contain the instance name
foreach ($service in $result)
{
$service.Name | Should -Match ('\$' + $script:mockInstanceName + '$')
}
}
}
}
}
Context 'When validating SMO object properties' {
It 'Should return objects with correct SMO properties' {
$result = Get-SqlDscManagedComputerService -ServerName $script:mockServerName -ErrorAction 'Stop'
$result | Should -Not -BeNullOrEmpty
# Verify it's a proper SMO Service object
$result | Should -BeOfType ([Microsoft.SqlServer.Management.Smo.Wmi.Service])
# Verify key properties exist for at least one service
$firstService = $result | Select-Object -First 1
$firstService.Name | Should -Not -BeNullOrEmpty
$firstService.Type | Should -Not -BeNullOrEmpty
# Verify the service has access to its parent ManagedComputer
$firstService.Parent | Should -Not -BeNullOrEmpty
$firstService.Parent.Name | Should -Be $script:mockServerName
}
}
}