Skip to content

Commit 4597f6d

Browse files
authored
Add integration test for Get-SqlDscPreferredModule command (#2251)
1 parent a3a74f0 commit 4597f6d

File tree

4 files changed

+273
-0
lines changed

4 files changed

+273
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
- Added integration tests for `Remove-SqlDscAudit` command to ensure it functions
1414
correctly in real environments
1515
[issue #2241](https://github.com/dsccommunity/SqlServerDsc/issues/2241).
16+
- Added integration tests for `Get-SqlDscPreferredModule` command to ensure it
17+
functions correctly in real environments
18+
[issue #2218](https://github.com/dsccommunity/SqlServerDsc/issues/2218).
1619
- Added integration tests for `Enable-SqlDscAudit` command to ensure command
1720
reliability [issue #2223](https://github.com/dsccommunity/SqlServerDsc/issues/2223).
1821
- Added integration tests for `Get-SqlDscAudit` command to ensure it functions

azure-pipelines.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ stages:
297297
'tests/Integration/Commands/New-SqlDscLogin.Integration.Tests.ps1'
298298
'tests/Integration/Commands/Get-SqlDscLogin.Integration.Tests.ps1'
299299
'tests/Integration/Commands/Get-SqlDscConfigurationOption.Integration.Tests.ps1'
300+
'tests/Integration/Commands/Get-SqlDscPreferredModule.Integration.Tests.ps1'
300301
'tests/Integration/Commands/Set-SqlDscConfigurationOption.Integration.Tests.ps1'
301302
'tests/Integration/Commands/Test-SqlDscConfigurationOption.Integration.Tests.ps1'
302303
'tests/Integration/Commands/Set-SqlDscStartupParameter.Integration.Tests.ps1'
Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
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+
Describe 'Get-SqlDscPreferredModule' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
33+
Context 'When using default parameters' {
34+
It 'Should return a module object when preferred modules are available' {
35+
$result = Get-SqlDscPreferredModule -ErrorAction 'Stop'
36+
37+
$result | Should -Not -BeNullOrEmpty
38+
$result | Should -BeOfType 'PSModuleInfo'
39+
$result.Name | Should -BeIn @('SqlServer', 'SQLPS')
40+
}
41+
42+
It 'Should return SqlServer module if available' {
43+
# Check if SqlServer module is available
44+
$sqlServerModule = Get-Module -Name 'SqlServer' -ListAvailable | Select-Object -First 1
45+
46+
if ($sqlServerModule) {
47+
$result = Get-SqlDscPreferredModule -ErrorAction 'Stop'
48+
49+
$result | Should -Not -BeNullOrEmpty
50+
$result.Name | Should -Be 'SqlServer'
51+
$result.Version | Should -Not -BeNullOrEmpty
52+
}
53+
else {
54+
Set-ItResult -Skipped -Because 'SqlServer module is not available in test environment'
55+
}
56+
}
57+
58+
It 'Should return the latest version when multiple versions are available' {
59+
$availableModules = Get-Module -Name 'SqlServer', 'SQLPS' -ListAvailable
60+
61+
if ($availableModules) {
62+
$result = Get-SqlDscPreferredModule -ErrorAction 'Stop'
63+
64+
$result | Should -Not -BeNullOrEmpty
65+
$result | Should -BeOfType 'PSModuleInfo'
66+
67+
# Verify it returns the latest version for the preferred module
68+
$sameNameModules = $availableModules | Where-Object { $_.Name -eq $result.Name }
69+
if ($sameNameModules.Count -gt 1) {
70+
$latestVersion = ($sameNameModules | Sort-Object Version -Descending | Select-Object -First 1).Version
71+
$result.Version | Should -Be $latestVersion
72+
}
73+
}
74+
else {
75+
Set-ItResult -Skipped -Because 'No preferred modules are available in test environment'
76+
}
77+
}
78+
}
79+
80+
Context 'When using the Name parameter' {
81+
It 'Should return the specified module when it exists' {
82+
# Test with SqlServer if available
83+
$sqlServerModule = Get-Module -Name 'SqlServer' -ListAvailable | Select-Object -First 1
84+
85+
if ($sqlServerModule) {
86+
$result = Get-SqlDscPreferredModule -Name @('SqlServer') -ErrorAction 'Stop'
87+
88+
$result | Should -Not -BeNullOrEmpty
89+
$result.Name | Should -Be 'SqlServer'
90+
}
91+
else {
92+
Set-ItResult -Skipped -Because 'SqlServer module is not available in test environment'
93+
}
94+
}
95+
96+
It 'Should return null when specified module does not exist' {
97+
$result = Get-SqlDscPreferredModule -Name @('NonExistentModule') -ErrorAction 'SilentlyContinue' -ErrorVariable errors
98+
99+
$result | Should -BeNullOrEmpty
100+
$errors | Should -HaveCount 1
101+
$errors[0].FullyQualifiedErrorId | Should -Be 'GSDPM0001,Get-SqlDscPreferredModule'
102+
}
103+
104+
It 'Should return the first available module from a list' {
105+
# Test with a list where first module doesn't exist but second does
106+
$sqlServerModule = Get-Module -Name 'SqlServer' -ListAvailable | Select-Object -First 1
107+
108+
if ($sqlServerModule) {
109+
$result = Get-SqlDscPreferredModule -Name @('NonExistentModule', 'SqlServer') -ErrorAction 'Stop'
110+
111+
$result | Should -Not -BeNullOrEmpty
112+
$result.Name | Should -Be 'SqlServer'
113+
}
114+
else {
115+
Set-ItResult -Skipped -Because 'SqlServer module is not available in test environment'
116+
}
117+
}
118+
}
119+
120+
Context 'When using the Refresh parameter' {
121+
It 'Should refresh PSModulePath and return a module' {
122+
$result = Get-SqlDscPreferredModule -Refresh -ErrorAction 'Stop'
123+
124+
$result | Should -Not -BeNullOrEmpty
125+
$result | Should -BeOfType 'PSModuleInfo'
126+
$result.Name | Should -BeIn @('SqlServer', 'SQLPS')
127+
}
128+
129+
It 'Should return the same result with and without Refresh' {
130+
$resultWithoutRefresh = Get-SqlDscPreferredModule -ErrorAction 'Stop'
131+
$resultWithRefresh = Get-SqlDscPreferredModule -Refresh -ErrorAction 'Stop'
132+
133+
$resultWithoutRefresh.Name | Should -Be $resultWithRefresh.Name
134+
$resultWithoutRefresh.Version | Should -Be $resultWithRefresh.Version
135+
}
136+
}
137+
138+
Context 'When using environment variables' {
139+
Context 'When SMODefaultModuleName is set' {
140+
BeforeAll {
141+
# Backup original environment variable
142+
$originalSMODefaultModuleName = $env:SMODefaultModuleName
143+
144+
# Set environment variable to SqlServer if available
145+
$sqlServerModule = Get-Module -Name 'SqlServer' -ListAvailable | Select-Object -First 1
146+
if ($sqlServerModule) {
147+
$env:SMODefaultModuleName = 'SqlServer'
148+
}
149+
}
150+
151+
AfterAll {
152+
# Restore original environment variable
153+
if ($originalSMODefaultModuleName) {
154+
$env:SMODefaultModuleName = $originalSMODefaultModuleName
155+
}
156+
else {
157+
Remove-Item -Path 'env:SMODefaultModuleName' -ErrorAction 'SilentlyContinue'
158+
}
159+
}
160+
161+
It 'Should use the module specified in SMODefaultModuleName' {
162+
$sqlServerModule = Get-Module -Name 'SqlServer' -ListAvailable | Select-Object -First 1
163+
164+
if ($sqlServerModule -and $env:SMODefaultModuleName -eq 'SqlServer') {
165+
$result = Get-SqlDscPreferredModule -ErrorAction 'Stop'
166+
167+
$result | Should -Not -BeNullOrEmpty
168+
$result.Name | Should -Be 'SqlServer'
169+
}
170+
else {
171+
Set-ItResult -Skipped -Because 'SqlServer module is not available or environment variable not set properly'
172+
}
173+
}
174+
}
175+
176+
Context 'When SMODefaultModuleVersion is set' {
177+
BeforeAll {
178+
# Backup original environment variable
179+
$originalSMODefaultModuleVersion = $env:SMODefaultModuleVersion
180+
181+
# Get available SqlServer module version if available
182+
$sqlServerModule = Get-Module -Name 'SqlServer' -ListAvailable | Select-Object -First 1
183+
if ($sqlServerModule) {
184+
$env:SMODefaultModuleVersion = $sqlServerModule.Version.ToString()
185+
}
186+
}
187+
188+
AfterAll {
189+
# Restore original environment variable
190+
if ($originalSMODefaultModuleVersion) {
191+
$env:SMODefaultModuleVersion = $originalSMODefaultModuleVersion
192+
}
193+
else {
194+
Remove-Item -Path 'env:SMODefaultModuleVersion' -ErrorAction 'SilentlyContinue'
195+
}
196+
}
197+
198+
It 'Should return the specific version specified in SMODefaultModuleVersion' {
199+
$sqlServerModule = Get-Module -Name 'SqlServer' -ListAvailable | Select-Object -First 1
200+
201+
if ($sqlServerModule -and $env:SMODefaultModuleVersion) {
202+
$result = Get-SqlDscPreferredModule -ErrorAction 'Stop'
203+
204+
$result | Should -Not -BeNullOrEmpty
205+
$result.Name | Should -Be 'SqlServer'
206+
$result.Version.ToString() | Should -Be $env:SMODefaultModuleVersion
207+
}
208+
else {
209+
Set-ItResult -Skipped -Because 'SqlServer module is not available or environment variable not set properly'
210+
}
211+
}
212+
213+
It 'Should throw an error when specified version does not exist' {
214+
# Backup original environment variable
215+
$originalSMODefaultModuleVersion = $env:SMODefaultModuleVersion
216+
217+
try {
218+
# Set to a non-existent version
219+
$env:SMODefaultModuleVersion = '999.999.999'
220+
221+
{ Get-SqlDscPreferredModule -ErrorAction 'Stop' } | Should -Throw -ErrorId 'GSDPM0001,Get-SqlDscPreferredModule'
222+
}
223+
finally {
224+
# Restore original environment variable
225+
if ($originalSMODefaultModuleVersion) {
226+
$env:SMODefaultModuleVersion = $originalSMODefaultModuleVersion
227+
}
228+
else {
229+
Remove-Item -Path 'env:SMODefaultModuleVersion' -ErrorAction 'SilentlyContinue'
230+
}
231+
}
232+
}
233+
}
234+
}
235+
236+
Context 'When validating error handling' {
237+
It 'Should throw a terminating error when no modules are found and ErrorAction is Stop' {
238+
{ Get-SqlDscPreferredModule -Name @('NonExistentModule1', 'NonExistentModule2') -ErrorAction 'Stop' } |
239+
Should -Throw -ErrorId 'GSDPM0001,Get-SqlDscPreferredModule'
240+
}
241+
242+
It 'Should write a non-terminating error when no modules are found and ErrorAction is Continue' {
243+
$result = Get-SqlDscPreferredModule -Name @('NonExistentModule1', 'NonExistentModule2') -ErrorAction 'SilentlyContinue' -ErrorVariable errors
244+
245+
$result | Should -BeNullOrEmpty
246+
$errors | Should -HaveCount 1
247+
$errors[0].FullyQualifiedErrorId | Should -Be 'GSDPM0001,Get-SqlDscPreferredModule'
248+
$errors[0].CategoryInfo.Category | Should -Be 'ObjectNotFound'
249+
}
250+
}
251+
252+
Context 'When validating output type' {
253+
It 'Should return PSModuleInfo type' {
254+
$result = Get-SqlDscPreferredModule -ErrorAction 'Stop'
255+
256+
$result | Should -BeOfType 'PSModuleInfo'
257+
$result.PSTypeNames | Should -Contain 'System.Management.Automation.PSModuleInfo'
258+
}
259+
260+
It 'Should have expected properties' {
261+
$result = Get-SqlDscPreferredModule -ErrorAction 'Stop'
262+
263+
$result.Name | Should -Not -BeNullOrEmpty
264+
$result.Version | Should -Not -BeNullOrEmpty
265+
$result.Path | Should -Not -BeNullOrEmpty
266+
}
267+
}
268+
}

tests/Integration/Commands/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ to each other. Dependencies are made to speed up the testing.**
4141
Command | Run order # | Depends on # | Use instance | Creates persistent objects
4242
--- | --- | --- | --- | ---
4343
Prerequisites | 0 | - | - | Sets up dependencies
44+
Get-SqlDscPreferredModule | 1 | 0 (Prerequisites) | - | -
4445
Save-SqlDscSqlServerMediaFile | 0 | - | - | Downloads SQL Server media files
4546
ConvertTo-SqlDscEditionName | 0 | - | - | -
4647
Import-SqlDscPreferredModule | 0 | - | - | -

0 commit comments

Comments
 (0)