-
Notifications
You must be signed in to change notification settings - Fork 227
Add integration test for Import-SqlDscPreferredModule command #2258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
188 changes: 188 additions & 0 deletions
188
tests/Integration/Commands/Import-SqlDscPreferredModule.Integration.Tests.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| [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 | ||
| } | ||
| } | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.