-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathImport-SqlDscPreferredModule.Integration.Tests.ps1
More file actions
188 lines (155 loc) · 7.52 KB
/
Import-SqlDscPreferredModule.Integration.Tests.ps1
File metadata and controls
188 lines (155 loc) · 7.52 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
[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 noop" first.'
}
}
BeforeAll {
$script:moduleName = 'SqlServerDsc'
Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop'
}
Describe 'Import-SqlDscPreferredModule' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
BeforeAll {
# Store original environment variable value to restore later
$script:originalSMODefaultModuleName = $env:SMODefaultModuleName
# Get list of initially loaded modules to restore session state
$script:initialModules = Get-Module | Where-Object { $_.Name -in @('SqlServer', 'SQLPS') }
}
AfterAll {
# Restore original environment variable
if ($null -ne $script:originalSMODefaultModuleName)
{
$env:SMODefaultModuleName = $script:originalSMODefaultModuleName
}
else
{
Remove-Item -Path 'env:SMODefaultModuleName' -ErrorAction 'SilentlyContinue'
}
# Clean up any modules that were imported during testing
$currentModules = Get-Module | Where-Object { $_.Name -in @('SqlServer', 'SQLPS') }
foreach ($module in $currentModules)
{
if ($module.Name -notin $script:initialModules.Name)
{
Remove-Module -Name $module.Name -Force -ErrorAction 'SilentlyContinue'
}
}
}
Context 'When importing the default preferred module' {
BeforeEach {
# Remove any SQL modules that might be loaded
Get-Module -Name @('SqlServer', 'SQLPS') | Remove-Module -Force -ErrorAction 'SilentlyContinue'
}
It 'Should import a module without throwing' {
{ Import-SqlDscPreferredModule -ErrorAction 'Stop' } | Should -Not -Throw
}
It 'Should import SqlServer module when available' {
Import-SqlDscPreferredModule -ErrorAction 'Stop'
$importedModule = Get-Module -Name @('SqlServer', 'SQLPS') | Select-Object -First 1
$importedModule | Should -Not -BeNullOrEmpty
$importedModule.Name | Should -BeIn @('SqlServer', 'SQLPS')
}
}
Context 'When using the Force parameter' {
BeforeEach {
# Remove any SQL modules that might be loaded
Get-Module -Name @('SqlServer', 'SQLPS') | Remove-Module -Force -ErrorAction 'SilentlyContinue'
}
It 'Should import a module without throwing when using Force' {
{ Import-SqlDscPreferredModule -Force -ErrorAction 'Stop' } | Should -Not -Throw
}
It 'Should reload module when Force is used' {
# First import
Import-SqlDscPreferredModule -ErrorAction 'Stop'
$firstImport = Get-Module -Name @('SqlServer', 'SQLPS') | Select-Object -First 1
# Force reimport
Import-SqlDscPreferredModule -Force -ErrorAction 'Stop'
$secondImport = Get-Module -Name @('SqlServer', 'SQLPS') | Select-Object -First 1
$secondImport | Should -Not -BeNullOrEmpty
$secondImport.Name | Should -Be $firstImport.Name
}
}
Context 'When specifying a preferred module name' {
BeforeEach {
# Remove any SQL modules that might be loaded
Get-Module -Name @('SqlServer', 'SQLPS') | Remove-Module -Force -ErrorAction 'SilentlyContinue'
}
It 'Should import SQLPS when specifically requested' {
# Skip if SQLPS is not available
$sqlpsModule = Get-Module -Name 'SQLPS' -ListAvailable
if (-not $sqlpsModule)
{
Set-ItResult -Skipped -Because 'SQLPS module is not available on this system'
return
}
{ Import-SqlDscPreferredModule -Name 'SQLPS' -ErrorAction 'Stop' } | Should -Not -Throw
$importedModule = Get-Module -Name 'SQLPS'
$importedModule | Should -Not -BeNullOrEmpty
$importedModule.Name | Should -Be 'SQLPS'
}
It 'Should import SqlServer when specifically requested' {
# Skip if SqlServer is not available
$sqlServerModule = Get-Module -Name 'SqlServer' -ListAvailable
if (-not $sqlServerModule)
{
Set-ItResult -Skipped -Because 'SqlServer module is not available on this system'
return
}
{ Import-SqlDscPreferredModule -Name 'SqlServer' -ErrorAction 'Stop' } | Should -Not -Throw
$importedModule = Get-Module -Name 'SqlServer'
$importedModule | Should -Not -BeNullOrEmpty
$importedModule.Name | Should -Be 'SqlServer'
}
}
Context 'When SMODefaultModuleName environment variable is set' {
BeforeEach {
# Remove any SQL modules that might be loaded
Get-Module -Name @('SqlServer', 'SQLPS') | Remove-Module -Force -ErrorAction 'SilentlyContinue'
}
AfterEach {
# Clean up environment variable after each test
Remove-Item -Path 'env:SMODefaultModuleName' -ErrorAction 'SilentlyContinue'
}
It 'Should respect SMODefaultModuleName environment variable when set to SqlServer' {
# Skip if SqlServer is not available
$sqlServerModule = Get-Module -Name 'SqlServer' -ListAvailable
if (-not $sqlServerModule)
{
Set-ItResult -Skipped -Because 'SqlServer module is not available on this system'
return
}
$env:SMODefaultModuleName = 'SqlServer'
{ Import-SqlDscPreferredModule -ErrorAction 'Stop' } | Should -Not -Throw
$importedModule = Get-Module -Name @('SqlServer', 'SQLPS') | Select-Object -First 1
$importedModule | Should -Not -BeNullOrEmpty
$importedModule.Name | Should -Be 'SqlServer'
}
}
Context 'When handling module availability' {
BeforeEach {
# Remove any SQL modules that might be loaded
Get-Module -Name @('SqlServer', 'SQLPS') | Remove-Module -Force -ErrorAction 'SilentlyContinue'
}
It 'Should handle the case when neither preferred module is available gracefully' {
# This test verifies error handling when no SQL modules are available
# We can't easily simulate this in CI where modules are installed, so we test with a non-existent module
{ Import-SqlDscPreferredModule -Name 'NonExistentSqlModule' -ErrorAction 'Stop' } | Should -Throw
}
}
}