diff --git a/CHANGELOG.md b/CHANGELOG.md index d13e31831..6bc3766dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `Get-LocalizedDataRecursive` - Added documentation how to generate stub modules for the unit tests. The documentation can be found in ['tests/Unit/Stubs`](https://github.com/dsccommunity/SqlServerDsc/tree/main/tests/Unit/Stubs). + - SqlRSSetup and SqlRS + - Removed the integration test when running against SQL Server 2019, + due to the URL to download the Reporting Services 2019 executable + no longer works. ### Added @@ -55,6 +59,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `Complete-SqlDscImage` - `Complete-SqlDscFailoverCluster` - `Initialize-SqlDscRebuildDatabase` + - `Import-SqlDscPreferredModule` - New GitHub issue templates for proposing new public commands, proposing an enhancement to an existing command, or having a problem with an existing command. @@ -121,6 +126,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 in AppVeyor to help maximize the time alloted be run. - The stubs in `SqlServerStub.psm1` are now based on the commands from the module SqlServer v22.0.49-preview. + - The module will now call `Import-SqlDscPreferredModule` when the module + is imported to make sure SqlServer (default preferred module) or SQLPS + is loaded into the session. This will make it possible for classes and + commands to use and return SQL types. If no module is found it will + output a warning to install any of the dependent modules. - `Install-SqlServerDsc` - No longer throws an exception when parameter `AgtSvcAccount` is not specified. - SqlAgReplica @@ -177,6 +187,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 when passed to the command ([issue #1837](https://github.com/dsccommunity/SqlServerDsc/issues/1837)). - Now returns a more clear error message when the status of a database instance is not `Online`. + - `Import-SQLPSModule` + - The function was changed to call public command `Import-SqlDscPreferredModule`. - SqlTraceFlag - The examples was updated to show that values should be passed as an array, even when there is only one value. diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 31d4d8ae5..a81aa747a 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -61,6 +61,9 @@ stages: pool: vmImage: 'windows-2022' timeoutInMinutes: 0 + variables: + # This sets environment variable $env:SqlServerDscCI. + SqlServerDscCI: true steps: - task: DownloadPipelineArtifact@2 displayName: 'Download Build Artifact' @@ -105,6 +108,9 @@ stages: pool: vmImage: 'windows-2019' timeoutInMinutes: 0 + variables: + # This sets environment variable $env:SqlServerDscCI. + SqlServerDscCI: true steps: - task: DownloadPipelineArtifact@2 displayName: 'Download Build Artifact' @@ -168,6 +174,8 @@ stages: variables: # This sets environment variable $env:CI. CI: true + # This sets environment variable $env:SqlServerDscCI. + SqlServerDscCI: true steps: - task: DownloadPipelineArtifact@2 displayName: 'Download Build Artifact' diff --git a/build.yaml b/build.yaml index bf67b43f2..997d42971 100644 --- a/build.yaml +++ b/build.yaml @@ -40,6 +40,7 @@ CopyPaths: - en-US - Modules Prefix: prefix.ps1 +Suffix: suffix.ps1 Encoding: UTF8 VersionedOutputDirectory: true BuiltModuleSubdirectory: builtModule diff --git a/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psd1 b/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psd1 index 8ac4f4973..199e48281 100644 --- a/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psd1 +++ b/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psd1 @@ -52,7 +52,6 @@ 'Get-ProtocolNameProperties' 'Get-ServerProtocolObject' 'Import-Assembly' - 'Set-PSModulePath' 'ConvertTo-ServerInstanceName' 'Get-FilePathMajorVersion' 'Test-FeatureFlag' @@ -75,4 +74,3 @@ } # End of PrivateData hashtable } - diff --git a/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1 b/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1 index 9ebb8f56f..878d873b4 100644 --- a/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1 +++ b/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1 @@ -880,112 +880,7 @@ function Import-SQLPSModule $Force ) - if ($Force.IsPresent) - { - Write-Verbose -Message $script:localizedData.ModuleForceRemoval -Verbose - Remove-Module -Name @('SqlServer', 'SQLPS', 'SQLASCmdlets') -Force -ErrorAction SilentlyContinue - } - - <# - Check if either of the modules are already loaded into the session. - Prefer to use the first one (in order found). - NOTE: There should actually only be either SqlServer or SQLPS loaded, - otherwise there can be problems with wrong assemblies being loaded. - #> - $loadedModuleName = (Get-Module -Name @('SqlServer', 'SQLPS') | Select-Object -First 1).Name - if ($loadedModuleName) - { - Write-Verbose -Message ($script:localizedData.PowerShellModuleAlreadyImported -f $loadedModuleName) -Verbose - return - } - - $availableModuleName = $null - - # Get the newest SqlServer module if more than one exist - $availableModule = Get-Module -FullyQualifiedName 'SqlServer' -ListAvailable | - Sort-Object -Property 'Version' -Descending | - Select-Object -First 1 -Property Name, Path, Version - - if ($availableModule) - { - $availableModuleName = $availableModule.Name - - Write-Verbose -Message ($script:localizedData.PreferredModuleFound -f $availableModuleName) -Verbose - } - else - { - Write-Verbose -Message ($script:localizedData.PreferredModuleNotFound) -Verbose - - <# - After installing SQL Server the current PowerShell session doesn't know about the new path - that was added for the SQLPS module. - This reloads PowerShell session environment variable PSModulePath to make sure it contains - all paths. - #> - Set-PSModulePath -Path ([System.Environment]::GetEnvironmentVariable('PSModulePath', 'Machine')) - - <# - Get the newest SQLPS module if more than one exist. - #> - $availableModule = Get-Module -FullyQualifiedName 'SQLPS' -ListAvailable | - Select-Object -Property Name, Path, @{ - Name = 'Version' - Expression = { - # Parse the build version number '120', '130' from the Path. - (Select-String -InputObject $_.Path -Pattern '\\([0-9]{3})\\' -List).Matches.Groups[1].Value - } - } | - Sort-Object -Property 'Version' -Descending | - Select-Object -First 1 - - if ($availableModule) - { - # This sets $availableModuleName to the Path of the module to be loaded. - $availableModuleName = Split-Path -Path $availableModule.Path -Parent - } - } - - if ($availableModuleName) - { - try - { - Write-Debug -Message ($script:localizedData.DebugMessagePushingLocation) - Push-Location - - <# - SQLPS has unapproved verbs, disable checking to ignore Warnings. - Suppressing verbose so all cmdlet is not listed. - #> - $importedModule = Import-Module -Name $availableModuleName -DisableNameChecking -Verbose:$false -Force:$Force -PassThru -ErrorAction Stop - - <# - SQLPS returns two entries, one with module type 'Script' and another with module type 'Manifest'. - Only return the object with module type 'Manifest'. - SqlServer only returns one object (of module type 'Script'), so no need to do anything for SqlServer module. - #> - if ($availableModuleName -ne 'SqlServer') - { - $importedModule = $importedModule | Where-Object -Property 'ModuleType' -EQ -Value 'Manifest' - } - - Write-Verbose -Message ($script:localizedData.ImportedPowerShellModule -f $importedModule.Name, $importedModule.Version, $importedModule.Path) -Verbose - } - catch - { - $errorMessage = $script:localizedData.FailedToImportPowerShellSqlModule -f $availableModuleName - New-InvalidOperationException -Message $errorMessage -ErrorRecord $_ - } - finally - { - Write-Debug -Message ($script:localizedData.DebugMessagePoppingLocation) - Pop-Location - } - } - else - { - $errorMessage = $script:localizedData.PowerShellSqlModuleNotFound - New-InvalidOperationException -Message $errorMessage - } + Import-SqlDscPreferredModule @PSBoundParameters } <# diff --git a/source/Modules/SqlServerDsc.Common/en-US/SqlServerDsc.Common.strings.psd1 b/source/Modules/SqlServerDsc.Common/en-US/SqlServerDsc.Common.strings.psd1 index 0b8448e5b..7a6be1fc4 100644 --- a/source/Modules/SqlServerDsc.Common/en-US/SqlServerDsc.Common.strings.psd1 +++ b/source/Modules/SqlServerDsc.Common/en-US/SqlServerDsc.Common.strings.psd1 @@ -16,15 +16,6 @@ ConvertFrom-StringData @' ConnectedToAnalysisServicesInstance = Connected to Analysis Services instance '{0}'. (SQLCOMMON0020) FailedToConnectToAnalysisServicesInstance = Failed to connect to Analysis Services instance '{0}'. (SQLCOMMON0021) SqlServerVersionIsInvalid = Could not get the SQL version for the instance '{0}'. (SQLCOMMON0022) - PreferredModuleFound = Preferred module {0} found. (SQLCOMMON0023) - PreferredModuleNotFound = Information: No preferred PowerShell module was found, trying to use the SQLPS module. (SQLCOMMON0024) - ImportedPowerShellModule = Importing PowerShell module '{0}' with version '{1}' from path '{2}'. (SQLCOMMON0025) - PowerShellModuleAlreadyImported = Found PowerShell module {0} already imported in the session. (SQLCOMMON0026) - ModuleForceRemoval = Forcibly removed the SQL PowerShell module from the session to import it fresh again. (SQLCOMMON0027) - DebugMessagePushingLocation = SQLPS module changes CWD to SQLSERVER:\ when loading, pushing location to pop it when module is loaded. (SQLCOMMON0028) - DebugMessagePoppingLocation = Popping location back to what it was before importing SQLPS module. (SQLCOMMON0029) - PowerShellSqlModuleNotFound = Neither PowerShell module SqlServer or SQLPS was found. Unable to run SQL Server cmdlets. (SQLCOMMON0030) - FailedToImportPowerShellSqlModule = Failed to import {0} module. (SQLCOMMON0031) GetSqlServerClusterResources = Getting cluster resource for SQL Server. (SQLCOMMON0032) GetSqlAgentClusterResource = Getting active cluster resource SQL Server Agent. (SQLCOMMON0033) BringClusterResourcesOffline = Bringing the SQL Server resources '{0}' offline. (SQLCOMMON0034) diff --git a/source/Modules/SqlServerDsc.Common/sv-SE/SqlServerDsc.Common.strings.psd1 b/source/Modules/SqlServerDsc.Common/sv-SE/SqlServerDsc.Common.strings.psd1 index 145f6502c..305aeae20 100644 --- a/source/Modules/SqlServerDsc.Common/sv-SE/SqlServerDsc.Common.strings.psd1 +++ b/source/Modules/SqlServerDsc.Common/sv-SE/SqlServerDsc.Common.strings.psd1 @@ -22,15 +22,6 @@ ConvertFrom-StringData @' ConnectedToAnalysisServicesInstance = Ansluten till Analysis Services instans '{0}'. (SQLCOMMON0020) FailedToConnectToAnalysisServicesInstance = Misslyckades att ansluta till Analysis Services instans '{0}'. (SQLCOMMON0021) SqlServerVersionIsInvalid = Kunde inte hämta SQL version för instansen '{0}'. (SQLCOMMON0022) - PreferredModuleFound = Föredragen modul SqlServer funnen. (SQLCOMMON0023) - PreferredModuleNotFound = Information: PowerShell modul SqlServer ej funnen, försöker att använda äldre SQLPS modul. (SQLCOMMON0024) - ImportedPowerShellModule = Importerade PowerShell modul '{0}' med version '{1}' från mapp '{2}'. (SQLCOMMON0025) - PowerShellModuleAlreadyImported = Fann att PowerShell modul {0} redan är importerad i sessionen. (SQLCOMMON0026) - ModuleForceRemoval = Tvingade bort den tidigare SQL PowerShell modulen från sessionen för att importera den fräsch igen. (SQLCOMMON0027) - DebugMessagePushingLocation = SQLPS modul ändrar nuvarande katalog till SQLSERVER:\ när modulen laddas, sparar nuvarande katalog så den kan återställas efter modulen laddats. (SQLCOMMON0028) - DebugMessagePoppingLocation = Återställer nuvarande katalog till vad den var innan modulen SQLPS importerades. (SQLCOMMON0029) - PowerShellSqlModuleNotFound = Varken PowerShell modulen SqlServer eller SQLPS kunde hittas. Kommer inte kunna köra SQL Server cmdlets. (SQLCOMMON0030) - FailedToImportPowerShellSqlModule = Misslyckades att importera {0} modulen. (SQLCOMMON0031) GetSqlServerClusterResources = Hämtar kluster resurser för SQL Server. (SQLCOMMON0032) GetSqlAgentClusterResource = Hämtar aktiva kluster resurser för SQL Server Agent. (SQLCOMMON0033) BringClusterResourcesOffline = Tar SQL Server resurser '{0}' offline. (SQLCOMMON0034) diff --git a/source/Public/Import-SqlDscPreferredModule.ps1 b/source/Public/Import-SqlDscPreferredModule.ps1 new file mode 100644 index 000000000..9d60dc7c0 --- /dev/null +++ b/source/Public/Import-SqlDscPreferredModule.ps1 @@ -0,0 +1,165 @@ +<# + .SYNOPSIS + Imports the module SqlServer (preferred) or SQLPS in a standardized way. + + .DESCRIPTION + Imports the module SqlServer (preferred) or SQLPS in a standardized way. + + .PARAMETER PreferredModule + Specifies the name of the preferred module. Defaults to 'SqlServer'. + + .PARAMETER Force + Forces the removal of the previous SQL module, to load the same or newer + version fresh. This is meant to make sure the newest version is used, with + the latest assemblies. + + .EXAMPLE + Import-SqlDscPreferredModule + + Imports the default preferred module (SqlServer) if it exist, otherwise + it will try to import the module SQLPS. + + .EXAMPLE + Import-SqlDscPreferredModule -Force + + Removes any already loaded module of the default preferred module (SqlServer) + and the module SQLPS, then it will forcibly import the default preferred + module if it exist, otherwise it will try to import the module SQLPS. + + .EXAMPLE + Import-SqlDscPreferredModule -PreferredModule 'OtherSqlModule' + + Imports the specified preferred module OtherSqlModule if it exist, otherwise + it will try to import the module SQLPS. +#> +function Import-SqlDscPreferredModule +{ + [CmdletBinding()] + param + ( + [Parameter()] + [System.String] + $PreferredModule = 'SqlServer', + + [Parameter()] + [System.Management.Automation.SwitchParameter] + $Force + ) + + if ($Force.IsPresent) + { + Write-Verbose -Message $script:localizedData.PreferredModule_ForceRemoval + + Remove-Module -Name @( + $PreferredModule, + 'SQLPS', + 'SQLASCmdlets' # cSpell: disable-line + ) -Force -ErrorAction 'SilentlyContinue' + } + else + { + <# + Check if either of the modules are already loaded into the session. + Prefer to use the first one (in order found). + NOTE: There should actually only be either SqlServer or SQLPS loaded, + otherwise there can be problems with wrong assemblies being loaded. + #> + $loadedModuleName = (Get-Module -Name @($PreferredModule, 'SQLPS') | Select-Object -First 1).Name + + if ($loadedModuleName) + { + Write-Verbose -Message ($script:localizedData.PreferredModule_AlreadyImported -f $loadedModuleName) + + return + } + } + + $availableModuleName = $null + + # Get the newest SqlServer module if more than one exist + $availableModule = Get-Module -FullyQualifiedName $PreferredModule -ListAvailable | + Sort-Object -Property 'Version' -Descending | + Select-Object -First 1 -Property 'Name', 'Path', 'Version' + + if ($availableModule) + { + $availableModuleName = $availableModule.Name + + Write-Verbose -Message ($script:localizedData.PreferredModule_ModuleFound -f $availableModuleName) + } + else + { + Write-Verbose -Message ($script:localizedData.PreferredModule_ModuleNotFound) + + <# + After installing SQL Server the current PowerShell session doesn't know + about the new path that was added for the SQLPS module. This reloads + PowerShell session environment variable PSModulePath to make sure it + contains all paths. + #> + Set-PSModulePath -Path ([System.Environment]::GetEnvironmentVariable('PSModulePath', 'Machine')) + + # Get the newest SQLPS module if more than one exist. + $availableModule = Get-Module -FullyQualifiedName 'SQLPS' -ListAvailable | + Select-Object -Property Name, Path, @{ + Name = 'Version' + Expression = { + # Parse the build version number '120', '130' from the Path. + (Select-String -InputObject $_.Path -Pattern '\\([0-9]{3})\\' -List).Matches.Groups[1].Value + } + } | + Sort-Object -Property 'Version' -Descending | + Select-Object -First 1 + + if ($availableModule) + { + # This sets $availableModuleName to the Path of the module to be loaded. + $availableModuleName = Split-Path -Path $availableModule.Path -Parent + } + } + + if ($availableModuleName) + { + try + { + Write-Debug -Message ($script:localizedData.PreferredModule_PushingLocation) + + Push-Location + + <# + SQLPS has unapproved verbs, disable checking to ignore Warnings. + Suppressing verbose so all cmdlet is not listed. + #> + $importedModule = Import-Module -Name $availableModuleName -DisableNameChecking -Verbose:$false -Force:$Force -PassThru -ErrorAction 'Stop' + + <# + SQLPS returns two entries, one with module type 'Script' and another with module type 'Manifest'. + Only return the object with module type 'Manifest'. + SqlServer only returns one object (of module type 'Script'), so no need to do anything for SqlServer module. + #> + if ($availableModuleName -ne $PreferredModule) + { + $importedModule = $importedModule | Where-Object -Property 'ModuleType' -EQ -Value 'Manifest' + } + + Write-Verbose -Message ($script:localizedData.PreferredModule_ImportedModule -f $importedModule.Name, $importedModule.Version, $importedModule.Path) + } + finally + { + Write-Debug -Message ($script:localizedData.PreferredModule_PoppingLocation) + + Pop-Location + } + } + else + { + $PSCmdlet.ThrowTerminatingError( + [System.Management.Automation.ErrorRecord]::new( + ($script:localizedData.PreferredModule_FailedFinding -f $PreferredModule), + 'ISDPM0001', # cspell: disable-line + [System.Management.Automation.ErrorCategory]::ObjectNotFound, + 'PreferredModule' + ) + ) + } +} diff --git a/source/en-US/SqlServerDsc.strings.psd1 b/source/en-US/SqlServerDsc.strings.psd1 index 3090a6451..34bf96c99 100644 --- a/source/en-US/SqlServerDsc.strings.psd1 +++ b/source/en-US/SqlServerDsc.strings.psd1 @@ -94,4 +94,14 @@ ConvertFrom-StringData @' ## Assert-SetupActionProperties InstallSqlServerProperties_ASServerModeInvalidValue = The value for ASServerMode is not valid for the setup action {0}. InstallSqlServerProperties_RsInstallModeInvalidValue = The only valid value for RsInstallMode is 'FilesOnlyMode' when using setup action {0}. + + ## Import-SqlDscPreferredModule + PreferredModule_ModuleFound = Preferred module {0} found. + PreferredModule_ModuleNotFound = Information: No preferred PowerShell module was found, trying to use the SQLPS module. + PreferredModule_ImportedModule = Imported PowerShell module '{0}' with version '{1}' from path '{2}'. + PreferredModule_AlreadyImported = Found PowerShell module {0} already imported in the session. + PreferredModule_ForceRemoval = Forcibly removed the SQL PowerShell module from the session to import it fresh again. + PreferredModule_PushingLocation = SQLPS module changes CWD to SQLServer:\ when loading, pushing location to pop it when module is loaded. + PreferredModule_PoppingLocation = Popping location back to what it was before importing SQLPS module. + PreferredModule_FailedFinding = Failed to find a dependent module. Unable to run SQL Server commands or use SQL Server types. Please install the {0} or SQLPS then try to import SqlServerDsc again. '@ diff --git a/source/suffix.ps1 b/source/suffix.ps1 new file mode 100644 index 000000000..2cd50e32a --- /dev/null +++ b/source/suffix.ps1 @@ -0,0 +1,26 @@ +<# + This check is made so that the real SqlServer or SQLPS module is not loaded into + the session when the CI runs unit tests of SqlServerDsc. It would conflict with + the stub types and stub commands used in the unit tests. This is a workaround + because we cannot set a specific module as a nested module in the module manifest, + the user must be able to choose to use either SQLPS or SqlServer. +#> +if (-not $env:SqlServerDscCI) +{ + try + { + <# + Import SQL commands and types into the session, so that types used + by commands can be parsed. + #> + Import-SqlDscPreferredModule -ErrorAction 'Stop' + } + catch + { + <# + It is not possible to throw the error from Import-SqlDscPreferredModule + since it will just fail the command Import-Module with an obscure error. + #> + Write-Warning -Message $_.Exception.Message + } +} diff --git a/tests/Integration/DSC_SqlRS.Integration.Tests.ps1 b/tests/Integration/DSC_SqlRS.Integration.Tests.ps1 index fa6a1fa69..aed1c31a4 100644 --- a/tests/Integration/DSC_SqlRS.Integration.Tests.ps1 +++ b/tests/Integration/DSC_SqlRS.Integration.Tests.ps1 @@ -64,7 +64,7 @@ AfterAll { Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force } -Describe "$($script:dscResourceName)_Integration" -Tag @('Integration_SQL2016', 'Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') { +Describe "$($script:dscResourceName)_Integration" -Tag @('Integration_SQL2016', 'Integration_SQL2017', <# 'Integration_SQL2019', #> 'Integration_SQL2022') { BeforeAll { $resourceId = "[$($script:dscResourceFriendlyName)]Integration_Test" } diff --git a/tests/Integration/DSC_SqlRSSetup.Integration.Tests.ps1 b/tests/Integration/DSC_SqlRSSetup.Integration.Tests.ps1 index babe4db9e..18ff9409b 100644 --- a/tests/Integration/DSC_SqlRSSetup.Integration.Tests.ps1 +++ b/tests/Integration/DSC_SqlRSSetup.Integration.Tests.ps1 @@ -102,7 +102,7 @@ AfterAll { Older versions of Reporting Services (eg. 2016) are integration tested in separate tests (part of resource SqlSetup). #> -Describe "$($script:dscResourceName)_Integration" -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') { +Describe "$($script:dscResourceName)_Integration" -Tag @('Integration_SQL2017', <# 'Integration_SQL2019', #> 'Integration_SQL2022') { BeforeAll { $resourceId = "[$($script:dscResourceFriendlyName)]Integration_Test" } diff --git a/tests/Unit/Classes/DatabasePermission.Tests.ps1 b/tests/Unit/Classes/DatabasePermission.Tests.ps1 index 2cbeeaf2e..ef95a4910 100644 --- a/tests/Unit/Classes/DatabasePermission.Tests.ps1 +++ b/tests/Unit/Classes/DatabasePermission.Tests.ps1 @@ -26,6 +26,8 @@ BeforeDiscovery { BeforeAll { $script:dscModuleName = 'SqlServerDsc' + $env:SqlServerDscCI = $true + Import-Module -Name $script:dscModuleName $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:dscModuleName @@ -40,6 +42,8 @@ AfterAll { # Unload the module being tested so that it doesn't impact any other tests. Get-Module -Name $script:dscModuleName -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'DatabasePermission' -Tag 'DatabasePermission' { diff --git a/tests/Unit/Classes/ServerPermission.Tests.ps1 b/tests/Unit/Classes/ServerPermission.Tests.ps1 index e11481a7e..3915c6a8e 100644 --- a/tests/Unit/Classes/ServerPermission.Tests.ps1 +++ b/tests/Unit/Classes/ServerPermission.Tests.ps1 @@ -26,6 +26,8 @@ BeforeDiscovery { BeforeAll { $script:dscModuleName = 'SqlServerDsc' + $env:SqlServerDscCI = $true + Import-Module -Name $script:dscModuleName $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:dscModuleName @@ -40,6 +42,8 @@ AfterAll { # Unload the module being tested so that it doesn't impact any other tests. Get-Module -Name $script:dscModuleName -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'ServerPermission' -Tag 'ServerPermission' { diff --git a/tests/Unit/Classes/SqlAudit.Tests.ps1 b/tests/Unit/Classes/SqlAudit.Tests.ps1 index c9cfa02d9..4b615db20 100644 --- a/tests/Unit/Classes/SqlAudit.Tests.ps1 +++ b/tests/Unit/Classes/SqlAudit.Tests.ps1 @@ -32,6 +32,8 @@ BeforeDiscovery { BeforeAll { $script:dscModuleName = 'SqlServerDsc' + $env:SqlServerDscCI = $true + Import-Module -Name $script:dscModuleName Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath '../../TestHelpers/CommonTestHelper.psm1') @@ -60,6 +62,8 @@ AfterAll { # Remove module common test helper. Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'SqlAudit' { diff --git a/tests/Unit/Classes/SqlDatabasePermission.Tests.ps1 b/tests/Unit/Classes/SqlDatabasePermission.Tests.ps1 index 3fd2edb24..279d6e9d2 100644 --- a/tests/Unit/Classes/SqlDatabasePermission.Tests.ps1 +++ b/tests/Unit/Classes/SqlDatabasePermission.Tests.ps1 @@ -32,6 +32,8 @@ BeforeDiscovery { BeforeAll { $script:dscModuleName = 'SqlServerDsc' + $env:SqlServerDscCI = $true + Import-Module -Name $script:dscModuleName Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath '../../TestHelpers/CommonTestHelper.psm1') @@ -60,6 +62,8 @@ AfterAll { # Remove module common test helper. Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'SqlDatabasePermission' { diff --git a/tests/Unit/Classes/SqlPermission.Tests.ps1 b/tests/Unit/Classes/SqlPermission.Tests.ps1 index c6b5cbc1e..eabb10e56 100644 --- a/tests/Unit/Classes/SqlPermission.Tests.ps1 +++ b/tests/Unit/Classes/SqlPermission.Tests.ps1 @@ -32,6 +32,8 @@ BeforeDiscovery { BeforeAll { $script:dscModuleName = 'SqlServerDsc' + $env:SqlServerDscCI = $true + Import-Module -Name $script:dscModuleName Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath '../../TestHelpers/CommonTestHelper.psm1') @@ -60,6 +62,8 @@ AfterAll { # Remove module common test helper. Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'SqlPermission' { diff --git a/tests/Unit/Classes/SqlResourceBase.Tests.ps1 b/tests/Unit/Classes/SqlResourceBase.Tests.ps1 index d46646ffd..348ac0efc 100644 --- a/tests/Unit/Classes/SqlResourceBase.Tests.ps1 +++ b/tests/Unit/Classes/SqlResourceBase.Tests.ps1 @@ -26,6 +26,8 @@ BeforeDiscovery { BeforeAll { $script:dscModuleName = 'SqlServerDsc' + $env:SqlServerDscCI = $true + Import-Module -Name $script:dscModuleName # Loading mocked classes @@ -43,6 +45,8 @@ AfterAll { # Unload the module being tested so that it doesn't impact any other tests. Get-Module -Name $script:dscModuleName -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'SqlResourceBase' { diff --git a/tests/Unit/DSC_SqlAGListener.Tests.ps1 b/tests/Unit/DSC_SqlAGListener.Tests.ps1 index 6327c1b29..35199f43b 100644 --- a/tests/Unit/DSC_SqlAGListener.Tests.ps1 +++ b/tests/Unit/DSC_SqlAGListener.Tests.ps1 @@ -35,6 +35,8 @@ BeforeAll { $script:dscModuleName = 'SqlServerDsc' $script:dscResourceName = 'DSC_SqlAGListener' + $env:SqlServerDscCI = $true + $script:testEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:dscModuleName ` -DSCResourceName $script:dscResourceName ` @@ -66,45 +68,9 @@ AfterAll { # Remove module common test helper. Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force -} -# $mockDynamicAvailabilityGroup = 'AG01' -# $mockDynamicListenerName = 'AGListener' -# $mockDynamicPortNumber = 5031 -# $mockDynamicIsDhcp = $true -# $script:mockMethodDropRan = $false - -# $mockConnectSql = { -# return New-Object -TypeName Object | -# Add-Member -MemberType ScriptProperty -Name AvailabilityGroups -Value { -# return @( -# @{ -# $mockDynamicAvailabilityGroup = New-Object -TypeName Object | -# Add-Member -MemberType ScriptProperty -Name AvailabilityGroupListeners -Value { -# @( -# @{ -# $mockDynamicListenerName = New-Object -TypeName Object | -# Add-Member -MemberType NoteProperty -Name PortNumber -Value $mockDynamicPortNumber -PassThru | -# Add-Member -MemberType ScriptProperty -Name AvailabilityGroupListenerIPAddresses -Value { -# return @( -# # TypeName: Microsoft.SqlServer.Management.Smo.AvailabilityGroupListenerIPAddressCollection -# (New-Object -TypeName Object | # TypeName: Microsoft.SqlServer.Management.Smo.AvailabilityGroupListenerIPAddress -# Add-Member -MemberType NoteProperty -Name IsDHCP -Value $mockDynamicIsDhcp -PassThru | -# Add-Member -MemberType NoteProperty -Name IPAddress -Value '192.168.0.1' -PassThru | -# Add-Member -MemberType NoteProperty -Name SubnetMask -Value '255.255.255.0' -PassThru -# ) -# ) -# } -PassThru | -# Add-Member -MemberType ScriptMethod -Name Drop -Value { -# $script:mockMethodDropRan = $true -# } -PassThru -Force -# } -# ) -# } -PassThru -Force -# } -# ) -# } -PassThru -Force -# } + Remove-Item -Path 'env:SqlServerDscCI' +} Describe 'SqlAGListener\Get-TargetResource' { BeforeAll { diff --git a/tests/Unit/DSC_SqlAGReplica.Tests.ps1 b/tests/Unit/DSC_SqlAGReplica.Tests.ps1 index e0f3a397e..5d7087511 100644 --- a/tests/Unit/DSC_SqlAGReplica.Tests.ps1 +++ b/tests/Unit/DSC_SqlAGReplica.Tests.ps1 @@ -35,6 +35,8 @@ BeforeAll { $script:dscModuleName = 'SqlServerDsc' $script:dscResourceName = 'DSC_SqlAGReplica' + $env:SqlServerDscCI = $true + $script:testEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:dscModuleName ` -DSCResourceName $script:dscResourceName ` @@ -69,6 +71,8 @@ AfterAll { # Remove module common test helper. Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'SqlAGReplica\Get-TargetResource' { diff --git a/tests/Unit/DSC_SqlAgentAlert.Tests.ps1 b/tests/Unit/DSC_SqlAgentAlert.Tests.ps1 index b4f6b1d17..2126f37db 100644 --- a/tests/Unit/DSC_SqlAgentAlert.Tests.ps1 +++ b/tests/Unit/DSC_SqlAgentAlert.Tests.ps1 @@ -33,6 +33,8 @@ BeforeAll { $script:dscModuleName = 'SqlServerDsc' $script:dscResourceName = 'DSC_SqlAgentAlert' + $env:SqlServerDscCI = $true + $script:testEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:dscModuleName ` -DSCResourceName $script:dscResourceName ` @@ -58,6 +60,8 @@ AfterAll { # Remove module common test helper. Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'DSC_SqlAgentAlert\Get-TargetResource' -Tag 'Get' { diff --git a/tests/Unit/DSC_SqlAgentFailsafe.Tests.ps1 b/tests/Unit/DSC_SqlAgentFailsafe.Tests.ps1 index 99898e833..9fdc3df73 100644 --- a/tests/Unit/DSC_SqlAgentFailsafe.Tests.ps1 +++ b/tests/Unit/DSC_SqlAgentFailsafe.Tests.ps1 @@ -33,6 +33,8 @@ BeforeAll { $script:dscModuleName = 'SqlServerDsc' $script:dscResourceName = 'DSC_SqlAgentFailsafe' + $env:SqlServerDscCI = $true + $script:testEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:dscModuleName ` -DSCResourceName $script:dscResourceName ` @@ -58,6 +60,8 @@ AfterAll { # Remove module common test helper. Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'DSC_SqlAgentFailsafe\Get-TargetResource' -Tag 'Get' { diff --git a/tests/Unit/DSC_SqlAgentOperator.Tests.ps1 b/tests/Unit/DSC_SqlAgentOperator.Tests.ps1 index 63c586996..3158d30de 100644 --- a/tests/Unit/DSC_SqlAgentOperator.Tests.ps1 +++ b/tests/Unit/DSC_SqlAgentOperator.Tests.ps1 @@ -33,6 +33,8 @@ BeforeAll { $script:dscModuleName = 'SqlServerDsc' $script:dscResourceName = 'DSC_SqlAgentOperator' + $env:SqlServerDscCI = $true + $script:testEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:dscModuleName ` -DSCResourceName $script:dscResourceName ` @@ -58,6 +60,8 @@ AfterAll { # Remove module common test helper. Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'DSC_SqlAgentOperator\Get-TargetResource' -Tag 'Get' { diff --git a/tests/Unit/DSC_SqlAlias.Tests.ps1 b/tests/Unit/DSC_SqlAlias.Tests.ps1 index 5a138f0a1..a2d9c1db9 100644 --- a/tests/Unit/DSC_SqlAlias.Tests.ps1 +++ b/tests/Unit/DSC_SqlAlias.Tests.ps1 @@ -33,6 +33,8 @@ BeforeAll { $script:dscModuleName = 'SqlServerDsc' $script:dscResourceName = 'DSC_SqlAlias' + $env:SqlServerDscCI = $true + $script:testEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:dscModuleName ` -DSCResourceName $script:dscResourceName ` @@ -47,7 +49,8 @@ BeforeAll { # Inject a stub in the module scope to support testing cross-plattform InModuleScope -ScriptBlock { - function script:Get-CimInstance { + function script:Get-CimInstance + { param ( $ClassName @@ -68,6 +71,8 @@ AfterAll { # Remove module common test helper. Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'SqlAlias\Get-TargetResource' { diff --git a/tests/Unit/DSC_SqlAlwaysOnService.Tests.ps1 b/tests/Unit/DSC_SqlAlwaysOnService.Tests.ps1 index 8cc925410..7a741f4a6 100644 --- a/tests/Unit/DSC_SqlAlwaysOnService.Tests.ps1 +++ b/tests/Unit/DSC_SqlAlwaysOnService.Tests.ps1 @@ -33,6 +33,8 @@ BeforeAll { $script:dscModuleName = 'SqlServerDsc' $script:dscResourceName = 'DSC_SqlAlwaysOnService' + $env:SqlServerDscCI = $true + $script:testEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:dscModuleName ` -DSCResourceName $script:dscResourceName ` @@ -64,6 +66,8 @@ AfterAll { # Remove module common test helper. Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'SqlAlwaysOnService\Get-TargetResource' { diff --git a/tests/Unit/DSC_SqlConfiguration.Tests.ps1 b/tests/Unit/DSC_SqlConfiguration.Tests.ps1 index ecb6817c4..c9296f25e 100644 --- a/tests/Unit/DSC_SqlConfiguration.Tests.ps1 +++ b/tests/Unit/DSC_SqlConfiguration.Tests.ps1 @@ -33,6 +33,8 @@ BeforeAll { $script:dscModuleName = 'SqlServerDsc' $script:dscResourceName = 'DSC_SqlConfiguration' + $env:SqlServerDscCI = $true + $script:testEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:dscModuleName ` -DSCResourceName $script:dscResourceName ` @@ -64,6 +66,8 @@ AfterAll { # Remove module common test helper. Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'SqlConfiguration\Get-TargetResource' { diff --git a/tests/Unit/DSC_SqlDatabase.Tests.ps1 b/tests/Unit/DSC_SqlDatabase.Tests.ps1 index 018a6a8f8..18c6d6747 100644 --- a/tests/Unit/DSC_SqlDatabase.Tests.ps1 +++ b/tests/Unit/DSC_SqlDatabase.Tests.ps1 @@ -33,6 +33,8 @@ BeforeAll { $script:dscModuleName = 'SqlServerDsc' $script:dscResourceName = 'DSC_SqlDatabase' + $env:SqlServerDscCI = $true + $script:testEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:dscModuleName ` -DSCResourceName $script:dscResourceName ` @@ -64,6 +66,8 @@ AfterAll { # Remove module common test helper. Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'SqlDatabase\Get-TargetResource' { diff --git a/tests/Unit/DSC_SqlDatabaseDefaultLocation.Tests.ps1 b/tests/Unit/DSC_SqlDatabaseDefaultLocation.Tests.ps1 index 9f486aa72..a6de856ba 100644 --- a/tests/Unit/DSC_SqlDatabaseDefaultLocation.Tests.ps1 +++ b/tests/Unit/DSC_SqlDatabaseDefaultLocation.Tests.ps1 @@ -33,6 +33,8 @@ BeforeAll { $script:dscModuleName = 'SqlServerDsc' $script:dscResourceName = 'DSC_SqlDatabaseDefaultLocation' + $env:SqlServerDscCI = $true + $script:testEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:dscModuleName ` -DSCResourceName $script:dscResourceName ` @@ -67,6 +69,8 @@ AfterAll { # Remove module common test helper. Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'SqlDatabaseDefaultLocation\Get-TargetResource' { diff --git a/tests/Unit/DSC_SqlDatabaseMail.Tests.ps1 b/tests/Unit/DSC_SqlDatabaseMail.Tests.ps1 index 437712735..e22404a98 100644 --- a/tests/Unit/DSC_SqlDatabaseMail.Tests.ps1 +++ b/tests/Unit/DSC_SqlDatabaseMail.Tests.ps1 @@ -34,14 +34,7 @@ BeforeAll { $script:dscModuleName = 'SqlServerDsc' $script:dscResourceName = 'DSC_SqlDatabaseMail' - try - { - Import-Module -Name DscResource.Test -Force -ErrorAction 'Stop' - } - catch [System.IO.FileNotFoundException] - { - throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -Tasks build" first.' - } + $env:SqlServerDscCI = $true $script:testEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:dscModuleName ` @@ -68,6 +61,8 @@ AfterAll { # Remove module common test helper. Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'DSC_SqlDatabaseMail\Get-TargetResource' -Tag 'Get' { diff --git a/tests/Unit/DSC_SqlDatabaseObjectPermission.Tests.ps1 b/tests/Unit/DSC_SqlDatabaseObjectPermission.Tests.ps1 index a95f2c683..eda951803 100644 --- a/tests/Unit/DSC_SqlDatabaseObjectPermission.Tests.ps1 +++ b/tests/Unit/DSC_SqlDatabaseObjectPermission.Tests.ps1 @@ -33,6 +33,8 @@ BeforeAll { $script:dscModuleName = 'SqlServerDsc' $script:dscResourceName = 'DSC_SqlDatabaseObjectPermission' + $env:SqlServerDscCI = $true + $script:testEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:dscModuleName ` -DSCResourceName $script:dscResourceName ` @@ -64,6 +66,8 @@ AfterAll { # Remove module common test helper. Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'SqlDatabaseObjectPermission\Get-TargetResource' -Tag 'Get' { diff --git a/tests/Unit/DSC_SqlDatabaseRole.Tests.ps1 b/tests/Unit/DSC_SqlDatabaseRole.Tests.ps1 index 183e04306..875035e79 100644 --- a/tests/Unit/DSC_SqlDatabaseRole.Tests.ps1 +++ b/tests/Unit/DSC_SqlDatabaseRole.Tests.ps1 @@ -33,6 +33,8 @@ BeforeAll { $script:dscModuleName = 'SqlServerDsc' $script:dscResourceName = 'DSC_SqlDatabaseRole' + $env:SqlServerDscCI = $true + $script:testEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:dscModuleName ` -DSCResourceName $script:dscResourceName ` @@ -64,6 +66,8 @@ AfterAll { # Remove module common test helper. Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'SqlDatabaseRole\Get-TargetResource' { diff --git a/tests/Unit/DSC_SqlDatabaseUser.Tests.ps1 b/tests/Unit/DSC_SqlDatabaseUser.Tests.ps1 index c06d16e4d..94b309b48 100644 --- a/tests/Unit/DSC_SqlDatabaseUser.Tests.ps1 +++ b/tests/Unit/DSC_SqlDatabaseUser.Tests.ps1 @@ -33,6 +33,8 @@ BeforeAll { $script:dscModuleName = 'SqlServerDsc' $script:dscResourceName = 'DSC_SqlDatabaseUser' + $env:SqlServerDscCI = $true + $script:testEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:dscModuleName ` -DSCResourceName $script:dscResourceName ` @@ -67,6 +69,8 @@ AfterAll { # Remove module common test helper. Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'SqlDatabaseUser\Get-TargetResource' -Tag 'Get' { diff --git a/tests/Unit/DSC_SqlEndpoint.Tests.ps1 b/tests/Unit/DSC_SqlEndpoint.Tests.ps1 index 5d523d7fc..1b0f031e2 100644 --- a/tests/Unit/DSC_SqlEndpoint.Tests.ps1 +++ b/tests/Unit/DSC_SqlEndpoint.Tests.ps1 @@ -33,6 +33,8 @@ BeforeAll { $script:dscModuleName = 'SqlServerDsc' $script:dscResourceName = 'DSC_SqlEndpoint' + $env:SqlServerDscCI = $true + $script:testEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:dscModuleName ` -DSCResourceName $script:dscResourceName ` @@ -67,6 +69,8 @@ AfterAll { # Remove module common test helper. Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'SqlEndpoint\Get-TargetResource' -Tag 'Get' { diff --git a/tests/Unit/DSC_SqlEndpointPermission.Tests.ps1 b/tests/Unit/DSC_SqlEndpointPermission.Tests.ps1 index 5ed61b9a1..8ef5c3801 100644 --- a/tests/Unit/DSC_SqlEndpointPermission.Tests.ps1 +++ b/tests/Unit/DSC_SqlEndpointPermission.Tests.ps1 @@ -33,6 +33,8 @@ BeforeAll { $script:dscModuleName = 'SqlServerDsc' $script:dscResourceName = 'DSC_SqlEndpointPermission' + $env:SqlServerDscCI = $true + $script:testEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:dscModuleName ` -DSCResourceName $script:dscResourceName ` @@ -64,6 +66,8 @@ AfterAll { # Remove module common test helper. Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'SqlEndpointPermission\Get-TargetResource' -Tag 'Get' { diff --git a/tests/Unit/DSC_SqlLogin.Tests.ps1 b/tests/Unit/DSC_SqlLogin.Tests.ps1 index d31eb05c0..c104f7beb 100644 --- a/tests/Unit/DSC_SqlLogin.Tests.ps1 +++ b/tests/Unit/DSC_SqlLogin.Tests.ps1 @@ -35,6 +35,8 @@ BeforeAll { $script:dscModuleName = 'SqlServerDsc' $script:dscResourceName = 'DSC_SqlLogin' + $env:SqlServerDscCI = $true + $script:testEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:dscModuleName ` -DSCResourceName $script:dscResourceName ` @@ -69,6 +71,8 @@ AfterAll { # Remove module common test helper. Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'SqlLogin\Get-TargetResource' -Tag 'Get' { diff --git a/tests/Unit/DSC_SqlMaxDop.Tests.ps1 b/tests/Unit/DSC_SqlMaxDop.Tests.ps1 index c7a43e94b..29f7e5304 100644 --- a/tests/Unit/DSC_SqlMaxDop.Tests.ps1 +++ b/tests/Unit/DSC_SqlMaxDop.Tests.ps1 @@ -33,6 +33,8 @@ BeforeAll { $script:dscModuleName = 'SqlServerDsc' $script:dscResourceName = 'DSC_SqlMaxDop' + $env:SqlServerDscCI = $true + $script:testEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:dscModuleName ` -DSCResourceName $script:dscResourceName ` @@ -67,6 +69,8 @@ AfterAll { # Remove module common test helper. Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'SqlMaxDop\Get-TargetResource' -Tag 'Get' { diff --git a/tests/Unit/DSC_SqlMemory.Tests.ps1 b/tests/Unit/DSC_SqlMemory.Tests.ps1 index 6a56900b5..3aca2dc6f 100644 --- a/tests/Unit/DSC_SqlMemory.Tests.ps1 +++ b/tests/Unit/DSC_SqlMemory.Tests.ps1 @@ -33,6 +33,8 @@ BeforeAll { $script:dscModuleName = 'SqlServerDsc' $script:dscResourceName = 'DSC_SqlMemory' + $env:SqlServerDscCI = $true + $script:testEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:dscModuleName ` -DSCResourceName $script:dscResourceName ` @@ -53,7 +55,8 @@ BeforeAll { # Inject a stub in the module scope to support testing cross-plattform InModuleScope -ScriptBlock { - function script:Get-CimInstance { + function script:Get-CimInstance + { param ( $ClassName @@ -77,6 +80,8 @@ AfterAll { # Remove module common test helper. Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'SqlMaxDop\Get-TargetResource' -Tag 'Get' { diff --git a/tests/Unit/DSC_SqlProtocol.Tests.ps1 b/tests/Unit/DSC_SqlProtocol.Tests.ps1 index 3ac9a2c2d..ff9a541d3 100644 --- a/tests/Unit/DSC_SqlProtocol.Tests.ps1 +++ b/tests/Unit/DSC_SqlProtocol.Tests.ps1 @@ -33,6 +33,8 @@ BeforeAll { $script:dscModuleName = 'SqlServerDsc' $script:dscResourceName = 'DSC_SqlProtocol' + $env:SqlServerDscCI = $true + $script:testEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:dscModuleName ` -DSCResourceName $script:dscResourceName ` @@ -58,6 +60,8 @@ AfterAll { # Remove module common test helper. Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'SqlProtocol\Get-TargetResource' -Tag 'Get' { diff --git a/tests/Unit/DSC_SqlProtocolTcpIp.Tests.ps1 b/tests/Unit/DSC_SqlProtocolTcpIp.Tests.ps1 index 5b381d37c..0fb1b2a6d 100644 --- a/tests/Unit/DSC_SqlProtocolTcpIp.Tests.ps1 +++ b/tests/Unit/DSC_SqlProtocolTcpIp.Tests.ps1 @@ -33,6 +33,8 @@ BeforeAll { $script:dscModuleName = 'SqlServerDsc' $script:dscResourceName = 'DSC_SqlProtocolTcpIp' + $env:SqlServerDscCI = $true + $script:testEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:dscModuleName ` -DSCResourceName $script:dscResourceName ` @@ -58,6 +60,8 @@ AfterAll { # Remove module common test helper. Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'SqlProtocolTcpIp\Get-TargetResource' -Tag 'Get' { diff --git a/tests/Unit/DSC_SqlRS.Tests.ps1 b/tests/Unit/DSC_SqlRS.Tests.ps1 index 9360ef3eb..88c68daad 100644 --- a/tests/Unit/DSC_SqlRS.Tests.ps1 +++ b/tests/Unit/DSC_SqlRS.Tests.ps1 @@ -33,6 +33,8 @@ BeforeAll { $script:dscModuleName = 'SqlServerDsc' $script:dscResourceName = 'DSC_SqlRS' + $env:SqlServerDscCI = $true + $script:testEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:dscModuleName ` -DSCResourceName $script:dscResourceName ` @@ -64,6 +66,8 @@ AfterAll { # Remove module common test helper. Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'SqlRS\Get-TargetResource' -Tag 'Get' { diff --git a/tests/Unit/DSC_SqlRSSetup.Tests.ps1 b/tests/Unit/DSC_SqlRSSetup.Tests.ps1 index 7930c21c2..4f862b487 100644 --- a/tests/Unit/DSC_SqlRSSetup.Tests.ps1 +++ b/tests/Unit/DSC_SqlRSSetup.Tests.ps1 @@ -35,6 +35,8 @@ BeforeAll { $script:dscModuleName = 'SqlServerDsc' $script:dscResourceName = 'DSC_SqlRSSetup' + $env:SqlServerDscCI = $true + $script:testEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:dscModuleName ` -DSCResourceName $script:dscResourceName ` @@ -55,6 +57,8 @@ AfterAll { # Unload the module being tested so that it doesn't impact any other tests. Get-Module -Name $script:dscResourceName -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'DSC_SqlRSSetup\Get-TargetResource' -Tag 'Get' { diff --git a/tests/Unit/DSC_SqlReplication.Tests.ps1 b/tests/Unit/DSC_SqlReplication.Tests.ps1 index b707440a2..8ba2a852a 100644 --- a/tests/Unit/DSC_SqlReplication.Tests.ps1 +++ b/tests/Unit/DSC_SqlReplication.Tests.ps1 @@ -35,6 +35,8 @@ BeforeAll { $script:dscModuleName = 'SqlServerDsc' $script:dscResourceName = 'DSC_SqlReplication' + $env:SqlServerDscCI = $true + $script:testEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:dscModuleName ` -DSCResourceName $script:dscResourceName ` @@ -69,6 +71,8 @@ AfterAll { # Remove module common test helper. Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'Helper functions' { diff --git a/tests/Unit/DSC_SqlRole.Tests.ps1 b/tests/Unit/DSC_SqlRole.Tests.ps1 index 59f8ff42c..530ed4911 100644 --- a/tests/Unit/DSC_SqlRole.Tests.ps1 +++ b/tests/Unit/DSC_SqlRole.Tests.ps1 @@ -33,6 +33,8 @@ BeforeAll { $script:dscModuleName = 'SqlServerDsc' $script:dscResourceName = 'DSC_SqlRole' + $env:SqlServerDscCI = $true + $script:testEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:dscModuleName ` -DSCResourceName $script:dscResourceName ` @@ -200,6 +202,8 @@ AfterAll { # Remove module common test helper. Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe "DSC_SqlRole\Get-TargetResource" -Tag 'Get' { diff --git a/tests/Unit/DSC_SqlScript.Tests.ps1 b/tests/Unit/DSC_SqlScript.Tests.ps1 index 66eb8cbab..68217aa3e 100644 --- a/tests/Unit/DSC_SqlScript.Tests.ps1 +++ b/tests/Unit/DSC_SqlScript.Tests.ps1 @@ -33,6 +33,8 @@ BeforeAll { $script:dscModuleName = 'SqlServerDsc' $script:dscResourceName = 'DSC_SqlScript' + $env:SqlServerDscCI = $true + $script:testEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:dscModuleName ` -DSCResourceName $script:dscResourceName ` @@ -67,6 +69,8 @@ AfterAll { # Remove module common test helper. Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'SqlScript\Get-TargetResource' -Tag 'Get' { diff --git a/tests/Unit/DSC_SqlScriptQuery.Tests.ps1 b/tests/Unit/DSC_SqlScriptQuery.Tests.ps1 index b5186e8f1..3a079e127 100644 --- a/tests/Unit/DSC_SqlScriptQuery.Tests.ps1 +++ b/tests/Unit/DSC_SqlScriptQuery.Tests.ps1 @@ -33,6 +33,8 @@ BeforeAll { $script:dscModuleName = 'SqlServerDsc' $script:dscResourceName = 'DSC_SqlScriptQuery' + $env:SqlServerDscCI = $true + $script:testEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:dscModuleName ` -DSCResourceName $script:dscResourceName ` @@ -67,6 +69,8 @@ AfterAll { # Remove module common test helper. Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'SqlScriptQuery\Get-TargetResource' -Tag 'Get' { diff --git a/tests/Unit/DSC_SqlSecureConnection.Tests.ps1 b/tests/Unit/DSC_SqlSecureConnection.Tests.ps1 index 64133a24e..679963db4 100644 --- a/tests/Unit/DSC_SqlSecureConnection.Tests.ps1 +++ b/tests/Unit/DSC_SqlSecureConnection.Tests.ps1 @@ -33,6 +33,8 @@ BeforeAll { $script:dscModuleName = 'SqlServerDsc' $script:dscResourceName = 'DSC_SqlSecureConnection' + $env:SqlServerDscCI = $true + $script:testEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:dscModuleName ` -DSCResourceName $script:dscResourceName ` @@ -67,6 +69,8 @@ AfterAll { # Remove module common test helper. Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'SqlSecureConnection\Get-TargetResource' -Tag 'Get' { diff --git a/tests/Unit/DSC_SqlServiceAccount.Tests.ps1 b/tests/Unit/DSC_SqlServiceAccount.Tests.ps1 index cb3906bca..06e29df9d 100644 --- a/tests/Unit/DSC_SqlServiceAccount.Tests.ps1 +++ b/tests/Unit/DSC_SqlServiceAccount.Tests.ps1 @@ -35,6 +35,8 @@ BeforeAll { $script:dscModuleName = 'SqlServerDsc' $script:dscResourceName = 'DSC_SqlServiceAccount' + $env:SqlServerDscCI = $true + $script:testEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:dscModuleName ` -DSCResourceName $script:dscResourceName ` @@ -69,6 +71,8 @@ AfterAll { # Remove module common test helper. Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'SqlServerServiceAccount\Get-TargetResource' -Tag 'Get' { diff --git a/tests/Unit/DSC_SqlSetup.Tests.ps1 b/tests/Unit/DSC_SqlSetup.Tests.ps1 index 342c18f68..b76e185b2 100644 --- a/tests/Unit/DSC_SqlSetup.Tests.ps1 +++ b/tests/Unit/DSC_SqlSetup.Tests.ps1 @@ -57,6 +57,8 @@ BeforeAll { $script:dscModuleName = 'SqlServerDsc' $script:dscResourceName = 'DSC_SqlSetup' + $env:SqlServerDscCI = $true + $script:testEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:dscModuleName ` -DSCResourceName $script:dscResourceName ` @@ -82,6 +84,8 @@ AfterAll { # Remove module common test helper. Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'SqlSetup\Get-TargetResource' -Tag 'Get' { diff --git a/tests/Unit/DSC_SqlTraceFlag.Tests.ps1 b/tests/Unit/DSC_SqlTraceFlag.Tests.ps1 index 3901ab018..e540ac543 100644 --- a/tests/Unit/DSC_SqlTraceFlag.Tests.ps1 +++ b/tests/Unit/DSC_SqlTraceFlag.Tests.ps1 @@ -33,6 +33,8 @@ BeforeAll { $script:dscModuleName = 'SqlServerDsc' $script:dscResourceName = 'DSC_SqlTraceFlag' + $env:SqlServerDscCI = $true + $script:testEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:dscModuleName ` -DSCResourceName $script:dscResourceName ` @@ -64,6 +66,8 @@ AfterAll { # Remove module common test helper. Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'DSC_SqlTraceFlag\Get-TargetResource' -Tag 'Get' { diff --git a/tests/Unit/DSC_SqlWaitForAG.Tests.ps1 b/tests/Unit/DSC_SqlWaitForAG.Tests.ps1 index 5e238b596..248bc695a 100644 --- a/tests/Unit/DSC_SqlWaitForAG.Tests.ps1 +++ b/tests/Unit/DSC_SqlWaitForAG.Tests.ps1 @@ -33,6 +33,8 @@ BeforeAll { $script:dscModuleName = 'SqlServerDsc' $script:dscResourceName = 'DSC_SqlWaitForAG' + $env:SqlServerDscCI = $true + $script:testEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:dscModuleName ` -DSCResourceName $script:dscResourceName ` @@ -67,6 +69,8 @@ AfterAll { # Remove module common test helper. Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'SqlWaitForAG\Get-TargetResource' -Tag 'Get' { diff --git a/tests/Unit/DSC_SqlWindowsFirewall.Tests.ps1 b/tests/Unit/DSC_SqlWindowsFirewall.Tests.ps1 index 2660a16fa..14dd84ffd 100644 --- a/tests/Unit/DSC_SqlWindowsFirewall.Tests.ps1 +++ b/tests/Unit/DSC_SqlWindowsFirewall.Tests.ps1 @@ -35,6 +35,8 @@ BeforeAll { $script:dscModuleName = 'SqlServerDsc' $script:dscResourceName = 'DSC_SqlWindowsFirewall' + $env:SqlServerDscCI = $true + $script:testEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:dscModuleName ` -DSCResourceName $script:dscResourceName ` @@ -69,6 +71,8 @@ AfterAll { # Remove module common test helper. Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'SqlWindowsFirewall\Get-TargetResource' -Tag 'Get' { diff --git a/tests/Unit/Private/Assert-SetupActionProperties.Tests.ps1 b/tests/Unit/Private/Assert-SetupActionProperties.Tests.ps1 index b04523b27..75160d0db 100644 --- a/tests/Unit/Private/Assert-SetupActionProperties.Tests.ps1 +++ b/tests/Unit/Private/Assert-SetupActionProperties.Tests.ps1 @@ -26,6 +26,8 @@ BeforeDiscovery { BeforeAll { $script:dscModuleName = 'SqlServerDsc' + $env:SqlServerDscCI = $true + Import-Module -Name $script:dscModuleName $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:dscModuleName @@ -40,6 +42,8 @@ AfterAll { # Unload the module being tested so that it doesn't impact any other tests. Get-Module -Name $script:dscModuleName -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'Assert-SetupActionProperties' -Tag 'Private' { diff --git a/tests/Unit/Private/Invoke-SetupAction.Tests.ps1 b/tests/Unit/Private/Invoke-SetupAction.Tests.ps1 index 91a9c585c..2d36af82b 100644 --- a/tests/Unit/Private/Invoke-SetupAction.Tests.ps1 +++ b/tests/Unit/Private/Invoke-SetupAction.Tests.ps1 @@ -27,6 +27,8 @@ BeforeDiscovery { BeforeAll { $script:dscModuleName = 'SqlServerDsc' + $env:SqlServerDscCI = $true + Import-Module -Name $script:dscModuleName $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:dscModuleName @@ -41,6 +43,8 @@ AfterAll { # Unload the module being tested so that it doesn't impact any other tests. Get-Module -Name $script:dscModuleName -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'Invoke-SetupAction' -Tag 'Public' { diff --git a/tests/Unit/Public/Add-SqlDscNode.Tests.ps1 b/tests/Unit/Public/Add-SqlDscNode.Tests.ps1 index 0312d1643..e7c6f7640 100644 --- a/tests/Unit/Public/Add-SqlDscNode.Tests.ps1 +++ b/tests/Unit/Public/Add-SqlDscNode.Tests.ps1 @@ -27,6 +27,8 @@ BeforeDiscovery { BeforeAll { $script:dscModuleName = 'SqlServerDsc' + $env:SqlServerDscCI = $true + Import-Module -Name $script:dscModuleName $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:dscModuleName @@ -41,6 +43,8 @@ AfterAll { # Unload the module being tested so that it doesn't impact any other tests. Get-Module -Name $script:dscModuleName -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'Add-SqlDscNode' -Tag 'Public' { diff --git a/tests/Unit/Public/Complete-SqlDscFailoverCluster.Tests.ps1 b/tests/Unit/Public/Complete-SqlDscFailoverCluster.Tests.ps1 index fdd25f318..81f0584e9 100644 --- a/tests/Unit/Public/Complete-SqlDscFailoverCluster.Tests.ps1 +++ b/tests/Unit/Public/Complete-SqlDscFailoverCluster.Tests.ps1 @@ -27,6 +27,8 @@ BeforeDiscovery { BeforeAll { $script:dscModuleName = 'SqlServerDsc' + $env:SqlServerDscCI = $true + Import-Module -Name $script:dscModuleName $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:dscModuleName @@ -41,6 +43,8 @@ AfterAll { # Unload the module being tested so that it doesn't impact any other tests. Get-Module -Name $script:dscModuleName -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'Complete-SqlDscFailoverCluster' -Tag 'Public' { diff --git a/tests/Unit/Public/Complete-SqlDscImage.Tests.ps1 b/tests/Unit/Public/Complete-SqlDscImage.Tests.ps1 index 01842327a..9fbd6ec8d 100644 --- a/tests/Unit/Public/Complete-SqlDscImage.Tests.ps1 +++ b/tests/Unit/Public/Complete-SqlDscImage.Tests.ps1 @@ -27,6 +27,8 @@ BeforeDiscovery { BeforeAll { $script:dscModuleName = 'SqlServerDsc' + $env:SqlServerDscCI = $true + Import-Module -Name $script:dscModuleName $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:dscModuleName @@ -41,6 +43,8 @@ AfterAll { # Unload the module being tested so that it doesn't impact any other tests. Get-Module -Name $script:dscModuleName -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'Complete-SqlDscImage' -Tag 'Public' { diff --git a/tests/Unit/Public/Connect-SqlDscDatabaseEngine.Tests.ps1 b/tests/Unit/Public/Connect-SqlDscDatabaseEngine.Tests.ps1 index ea2360776..95cef49fa 100644 --- a/tests/Unit/Public/Connect-SqlDscDatabaseEngine.Tests.ps1 +++ b/tests/Unit/Public/Connect-SqlDscDatabaseEngine.Tests.ps1 @@ -27,6 +27,8 @@ BeforeDiscovery { BeforeAll { $script:dscModuleName = 'SqlServerDsc' + $env:SqlServerDscCI = $true + Import-Module -Name $script:dscModuleName # Loading mocked classes @@ -44,6 +46,8 @@ AfterAll { # Unload the module being tested so that it doesn't impact any other tests. Get-Module -Name $script:dscModuleName -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'Connect-SqlDscDatabaseEngine' -Tag 'Public' { diff --git a/tests/Unit/Public/ConvertFrom-SqlDscDatabasePermission.Tests.ps1 b/tests/Unit/Public/ConvertFrom-SqlDscDatabasePermission.Tests.ps1 index ea83d9b01..aea0f3c8a 100644 --- a/tests/Unit/Public/ConvertFrom-SqlDscDatabasePermission.Tests.ps1 +++ b/tests/Unit/Public/ConvertFrom-SqlDscDatabasePermission.Tests.ps1 @@ -26,6 +26,8 @@ BeforeDiscovery { BeforeAll { $script:dscModuleName = 'SqlServerDsc' + $env:SqlServerDscCI = $true + Import-Module -Name $script:dscModuleName # Loading mocked classes @@ -43,6 +45,8 @@ AfterAll { # Unload the module being tested so that it doesn't impact any other tests. Get-Module -Name $script:dscModuleName -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'ConvertFrom-SqlDscDatabasePermission' -Tag 'Public' { diff --git a/tests/Unit/Public/ConvertFrom-SqlDscServerPermission.Tests.ps1 b/tests/Unit/Public/ConvertFrom-SqlDscServerPermission.Tests.ps1 index 7ddd88442..9a5cf77ec 100644 --- a/tests/Unit/Public/ConvertFrom-SqlDscServerPermission.Tests.ps1 +++ b/tests/Unit/Public/ConvertFrom-SqlDscServerPermission.Tests.ps1 @@ -26,6 +26,8 @@ BeforeDiscovery { BeforeAll { $script:dscModuleName = 'SqlServerDsc' + $env:SqlServerDscCI = $true + Import-Module -Name $script:dscModuleName # Loading mocked classes @@ -43,6 +45,8 @@ AfterAll { # Unload the module being tested so that it doesn't impact any other tests. Get-Module -Name $script:dscModuleName -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'ConvertFrom-SqlDscServerPermission' -Tag 'Public' { diff --git a/tests/Unit/Public/ConvertTo-SqlDscDatabasePermission.Tests.ps1 b/tests/Unit/Public/ConvertTo-SqlDscDatabasePermission.Tests.ps1 index 7c3e3b3e5..0bfaea1d4 100644 --- a/tests/Unit/Public/ConvertTo-SqlDscDatabasePermission.Tests.ps1 +++ b/tests/Unit/Public/ConvertTo-SqlDscDatabasePermission.Tests.ps1 @@ -26,6 +26,8 @@ BeforeDiscovery { BeforeAll { $script:dscModuleName = 'SqlServerDsc' + $env:SqlServerDscCI = $true + Import-Module -Name $script:dscModuleName # Loading mocked classes @@ -43,6 +45,8 @@ AfterAll { # Unload the module being tested so that it doesn't impact any other tests. Get-Module -Name $script:dscModuleName -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'ConvertTo-SqlDscDatabasePermission' -Tag 'Public' { diff --git a/tests/Unit/Public/ConvertTo-SqlDscServerPermission.Tests.ps1 b/tests/Unit/Public/ConvertTo-SqlDscServerPermission.Tests.ps1 index cf34ea490..3e8fde254 100644 --- a/tests/Unit/Public/ConvertTo-SqlDscServerPermission.Tests.ps1 +++ b/tests/Unit/Public/ConvertTo-SqlDscServerPermission.Tests.ps1 @@ -26,6 +26,8 @@ BeforeDiscovery { BeforeAll { $script:dscModuleName = 'SqlServerDsc' + $env:SqlServerDscCI = $true + Import-Module -Name $script:dscModuleName # Loading mocked classes @@ -43,6 +45,8 @@ AfterAll { # Unload the module being tested so that it doesn't impact any other tests. Get-Module -Name $script:dscModuleName -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'ConvertTo-SqlDscServerPermission' -Tag 'Public' { diff --git a/tests/Unit/Public/Disable-SqlDscAudit.Tests.ps1 b/tests/Unit/Public/Disable-SqlDscAudit.Tests.ps1 index fa171c110..095b15ef2 100644 --- a/tests/Unit/Public/Disable-SqlDscAudit.Tests.ps1 +++ b/tests/Unit/Public/Disable-SqlDscAudit.Tests.ps1 @@ -26,6 +26,8 @@ BeforeDiscovery { BeforeAll { $script:dscModuleName = 'SqlServerDsc' + $env:SqlServerDscCI = $true + Import-Module -Name $script:dscModuleName # Loading mocked classes @@ -43,6 +45,8 @@ AfterAll { # Unload the module being tested so that it doesn't impact any other tests. Get-Module -Name $script:dscModuleName -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'Disable-SqlDscAudit' -Tag 'Public' { diff --git a/tests/Unit/Public/Enable-SqlDscAudit.Tests.ps1 b/tests/Unit/Public/Enable-SqlDscAudit.Tests.ps1 index 026cbe25d..b68bff8e6 100644 --- a/tests/Unit/Public/Enable-SqlDscAudit.Tests.ps1 +++ b/tests/Unit/Public/Enable-SqlDscAudit.Tests.ps1 @@ -26,6 +26,8 @@ BeforeDiscovery { BeforeAll { $script:dscModuleName = 'SqlServerDsc' + $env:SqlServerDscCI = $true + Import-Module -Name $script:dscModuleName # Loading mocked classes @@ -43,6 +45,8 @@ AfterAll { # Unload the module being tested so that it doesn't impact any other tests. Get-Module -Name $script:dscModuleName -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'Enable-SqlDscAudit' -Tag 'Public' { diff --git a/tests/Unit/Public/Get-SqlDscAudit.Tests.ps1 b/tests/Unit/Public/Get-SqlDscAudit.Tests.ps1 index fe1b3bb44..20ddea99a 100644 --- a/tests/Unit/Public/Get-SqlDscAudit.Tests.ps1 +++ b/tests/Unit/Public/Get-SqlDscAudit.Tests.ps1 @@ -26,6 +26,8 @@ BeforeDiscovery { BeforeAll { $script:dscModuleName = 'SqlServerDsc' + $env:SqlServerDscCI = $true + Import-Module -Name $script:dscModuleName # Loading mocked classes @@ -43,6 +45,8 @@ AfterAll { # Unload the module being tested so that it doesn't impact any other tests. Get-Module -Name $script:dscModuleName -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'Get-SqlDscAudit' -Tag 'Public' { diff --git a/tests/Unit/Public/Get-SqlDscDatabasePermission.Tests.ps1 b/tests/Unit/Public/Get-SqlDscDatabasePermission.Tests.ps1 index 9ccc05e41..db4f3598e 100644 --- a/tests/Unit/Public/Get-SqlDscDatabasePermission.Tests.ps1 +++ b/tests/Unit/Public/Get-SqlDscDatabasePermission.Tests.ps1 @@ -26,6 +26,8 @@ BeforeDiscovery { BeforeAll { $script:dscModuleName = 'SqlServerDsc' + $env:SqlServerDscCI = $true + Import-Module -Name $script:dscModuleName # Loading mocked classes @@ -43,6 +45,8 @@ AfterAll { # Unload the module being tested so that it doesn't impact any other tests. Get-Module -Name $script:dscModuleName -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'Get-SqlDscDatabasePermission' -Tag 'Public' { diff --git a/tests/Unit/Public/Get-SqlDscServerPermission.Tests.ps1 b/tests/Unit/Public/Get-SqlDscServerPermission.Tests.ps1 index 077d0f7da..155884fe7 100644 --- a/tests/Unit/Public/Get-SqlDscServerPermission.Tests.ps1 +++ b/tests/Unit/Public/Get-SqlDscServerPermission.Tests.ps1 @@ -26,6 +26,8 @@ BeforeDiscovery { BeforeAll { $script:dscModuleName = 'SqlServerDsc' + $env:SqlServerDscCI = $true + Import-Module -Name $script:dscModuleName # Loading mocked classes @@ -43,6 +45,8 @@ AfterAll { # Unload the module being tested so that it doesn't impact any other tests. Get-Module -Name $script:dscModuleName -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'Get-SqlDscServerPermission' -Tag 'Public' { diff --git a/tests/Unit/Public/Import-SqlDscPreferredModule.Tests.ps1 b/tests/Unit/Public/Import-SqlDscPreferredModule.Tests.ps1 new file mode 100644 index 000000000..9a4596230 --- /dev/null +++ b/tests/Unit/Public/Import-SqlDscPreferredModule.Tests.ps1 @@ -0,0 +1,299 @@ +[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')] +[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '', Justification = 'because ConvertTo-SecureString is used to simplify the tests.')] +param () + +BeforeDiscovery { + try + { + if (-not (Get-Module -Name 'DscResource.Test')) + { + # Assumes dependencies has 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' 2>&1 4>&1 5>&1 6>&1 > $null + } + + # If the dependencies has 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:dscModuleName = 'SqlServerDsc' + + $env:SqlServerDscCI = $true + + Import-Module -Name $script:dscModuleName + + $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:dscModuleName + $PSDefaultParameterValues['Mock:ModuleName'] = $script:dscModuleName + $PSDefaultParameterValues['Should:ModuleName'] = $script:dscModuleName +} + +AfterAll { + $PSDefaultParameterValues.Remove('InModuleScope:ModuleName') + $PSDefaultParameterValues.Remove('Mock:ModuleName') + $PSDefaultParameterValues.Remove('Should:ModuleName') + + # Unload the module being tested so that it doesn't impact any other tests. + Get-Module -Name $script:dscModuleName -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' +} + +Describe 'Import-SqlDscPreferredModule' -Tag 'Public' { + It 'Should have the correct parameters in parameter set ' -ForEach @( + @{ + MockParameterSetName = '__AllParameterSets' + # cSpell: disable-next + MockExpectedParameters = '[[-PreferredModule] ] [-Force] []' + } + ) { + $result = (Get-Command -Name 'Import-SqlDscPreferredModule').ParameterSets | + Where-Object -FilterScript { + $_.Name -eq $mockParameterSetName + } | + Select-Object -Property @( + @{ + Name = 'ParameterSetName' + Expression = { $_.Name } + }, + @{ + Name = 'ParameterListAsString' + Expression = { $_.ToString() } + } + ) + + $result.ParameterSetName | Should -Be $MockParameterSetName + $result.ParameterListAsString | Should -Be $MockExpectedParameters + } + + BeforeAll { + <# + This is the path to the latest version of SQLPS, to test that only the + newest SQLPS module is returned. + #> + $sqlPsLatestModulePath = 'C:\Program Files (x86)\Microsoft SQL Server\130\Tools\PowerShell\Modules\SQLPS\Sqlps.ps1' + + <# + For SQLPS module this should be the root of the module. + The .psd1 file is parsed from the module full path in the code. + #> + $sqlPsExpectedModulePath = Split-Path -Path $sqlPsLatestModulePath -Parent + + + $mockImportModule = { + # Convert the single value array [String[]] to the expected string value [String]. + $moduleNameToImport = $Name[0] + + if ($moduleNameToImport -ne $mockExpectedModuleNameToImport) + { + throw ('Wrong module was loaded. Expected {0}, but was {1}.' -f $mockExpectedModuleNameToImport, $moduleNameToImport) + } + + switch ($moduleNameToImport) + { + 'SqlServer' + { + $importModuleResult = @{ + ModuleType = 'Script' + Version = '21.0.17279' + Name = $moduleNameToImport + Path = 'C:\Program Files\WindowsPowerShell\Modules\SqlServer\21.0.17279\SqlServer.psm1' + } + } + + $sqlPsExpectedModulePath + { + # Can not use $Name because that contain the path to the module manifest. + $importModuleResult = @( + @{ + ModuleType = 'Script' + Version = '0.0' + # Intentionally formatted to correctly mimic a real run. + Name = 'Sqlps' + Path = $sqlPsLatestModulePath + } + @{ + ModuleType = 'Manifest' + Version = '1.0' + # Intentionally formatted to correctly mimic a real run. + Name = 'sqlps' + Path = $sqlPsLatestModulePath + } + ) + } + } + + return $importModuleResult + } + + $mockGetModuleSqlServer = { + # Return an array to test so that the latest version is only imported. + return @( + [PSCustomObject] @{ + Name = 'SqlServer' + Version = [Version] '1.0' + } + + [PSCustomObject] @{ + Name = 'SqlServer' + Version = [Version] '2.0' + } + ) + } + + Mock -CommandName Set-PSModulePath + Mock -CommandName Push-Location + Mock -CommandName Pop-Location + } + + Context 'When module SqlServer is already loaded into the session' { + BeforeAll { + Mock -CommandName Import-Module -MockWith $mockImportModule + Mock -CommandName Get-Module -MockWith { + return @{ + Name = 'SqlServer' + } + } + } + + It 'Should use the already loaded module and not call Import-Module' { + { Import-SqlDscPreferredModule } | Should -Not -Throw + + Should -Invoke -CommandName Import-Module -Exactly -Times 0 -Scope It + } + } + + Context 'When module SQLPS is already loaded into the session' { + BeforeAll { + Mock -CommandName Import-Module -MockWith $mockImportModule + Mock -CommandName Get-Module -MockWith { + return @{ + Name = 'SQLPS' + } + } + } + + It 'Should use the already loaded module and not call Import-Module' { + { Import-SqlDscPreferredModule } | Should -Not -Throw + + Should -Invoke -CommandName Import-Module -Exactly -Times 0 -Scope It + } + } + + Context 'When module SqlServer exists, but not loaded into the session' { + BeforeAll { + Mock -CommandName Import-Module -MockWith $mockImportModule + Mock -CommandName Get-Module -ParameterFilter { + $PesterBoundParameters.ContainsKey('Name') + } + + Mock -CommandName Get-Module -MockWith $mockGetModuleSqlServer -ParameterFilter { + $FullyQualifiedName.Name -eq 'SqlServer' -and $ListAvailable -eq $true + } + + $mockExpectedModuleNameToImport = 'SqlServer' + } + + It 'Should import the SqlServer module without throwing' { + { Import-SqlDscPreferredModule } | Should -Not -Throw + + Should -Invoke -CommandName Get-Module -ParameterFilter { + $FullyQualifiedName.Name -eq 'SqlServer' -and $ListAvailable -eq $true + } -Exactly -Times 1 -Scope It + Should -Invoke -CommandName Push-Location -Exactly -Times 1 -Scope It + Should -Invoke -CommandName Pop-Location -Exactly -Times 1 -Scope It + Should -Invoke -CommandName Import-Module -Exactly -Times 1 -Scope It + } + } + + Context 'When only module SQLPS exists, but not loaded into the session, and using -Force' { + BeforeAll { + Mock -CommandName Import-Module -MockWith $mockImportModule + Mock -CommandName Remove-Module + Mock -CommandName Get-Module -ParameterFilter { + $PesterBoundParameters.ContainsKey('Name') + } + + $mockExpectedModuleNameToImport = $sqlPsExpectedModulePath + + Mock -CommandName Get-Module -MockWith { + # Return an array to test so that the latest version is only imported. + return @( + [PSCustomObject] @{ + Name = 'SQLPS' + # This is a path to an older version of SQL PS than $sqlPsLatestModulePath. + Path = 'C:\Program Files (x86)\Microsoft SQL Server\120\Tools\PowerShell\Modules\SQLPS\Sqlps.ps1' + } + + [PSCustomObject] @{ + Name = 'SQLPS' + Path = $sqlPsLatestModulePath + } + ) + } -ParameterFilter { + $FullyQualifiedName.Name -eq 'SQLPS' -and $ListAvailable -eq $true + } + + Mock -CommandName Get-Module -MockWith { + return $null + } -ParameterFilter { + $FullyQualifiedName.Name -eq 'SqlServer' -and $ListAvailable -eq $true + } + } + + It 'Should import the SqlServer module without throwing' { + { Import-SqlDscPreferredModule -Force } | Should -Not -Throw + + Should -Invoke -CommandName Get-Module -ParameterFilter { + $FullyQualifiedName.Name -eq 'SqlServer' -and $ListAvailable -eq $true + } -Exactly -Times 1 -Scope It + + Should -Invoke -CommandName Get-Module -ParameterFilter { + $FullyQualifiedName.Name -eq 'SQLPS' -and $ListAvailable -eq $true + } -Exactly -Times 1 -Scope It + + Should -Invoke -CommandName Push-Location -Exactly -Times 1 -Scope It + Should -Invoke -CommandName Pop-Location -Exactly -Times 1 -Scope It + Should -Invoke -CommandName Remove-Module -Exactly -Times 1 -Scope It + Should -Invoke -CommandName Import-Module -Exactly -Times 1 -Scope It + } + } + + Context 'When neither SqlServer or SQLPS exists' { + BeforeAll { + Mock -CommandName Import-Module + + $mockExpectedModuleNameToImport = $sqlPsExpectedModulePath + + Mock -CommandName Get-Module + } + + It 'Should throw the correct error message' { + $mockLocalizedString = InModuleScope -ScriptBlock { + $script:localizedData.PowerShellSqlModuleNotFound -f 'SqlServer' + } + + { Import-SqlDscPreferredModule } | Should -Throw -ExpectedMessage $mockLocalizedString + + Should -Invoke -CommandName Get-Module -ParameterFilter { + $FullyQualifiedName.Name -eq 'SqlServer' -and $ListAvailable -eq $true + } -Exactly -Times 1 -Scope It + + Should -Invoke -CommandName Get-Module -ParameterFilter { + $FullyQualifiedName.Name -eq 'SQLPS' -and $ListAvailable -eq $true + } -Exactly -Times 1 -Scope It + + Should -Invoke -CommandName Push-Location -Exactly -Times 0 -Scope It + Should -Invoke -CommandName Pop-Location -Exactly -Times 0 -Scope It + Should -Invoke -CommandName Import-Module -Exactly -Times 0 -Scope It + } + } +} diff --git a/tests/Unit/Public/Initialize-SqlDscRebuildDatabase.Tests.ps1 b/tests/Unit/Public/Initialize-SqlDscRebuildDatabase.Tests.ps1 index 217fd6952..bd2419013 100644 --- a/tests/Unit/Public/Initialize-SqlDscRebuildDatabase.Tests.ps1 +++ b/tests/Unit/Public/Initialize-SqlDscRebuildDatabase.Tests.ps1 @@ -27,6 +27,8 @@ BeforeDiscovery { BeforeAll { $script:dscModuleName = 'SqlServerDsc' + $env:SqlServerDscCI = $true + Import-Module -Name $script:dscModuleName $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:dscModuleName @@ -41,6 +43,8 @@ AfterAll { # Unload the module being tested so that it doesn't impact any other tests. Get-Module -Name $script:dscModuleName -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'Initialize-SqlDscRebuildDatabase' -Tag 'Public' { diff --git a/tests/Unit/Public/Install-SqlDscServer.Tests.ps1 b/tests/Unit/Public/Install-SqlDscServer.Tests.ps1 index bcfcb4924..3779a5480 100644 --- a/tests/Unit/Public/Install-SqlDscServer.Tests.ps1 +++ b/tests/Unit/Public/Install-SqlDscServer.Tests.ps1 @@ -27,6 +27,8 @@ BeforeDiscovery { BeforeAll { $script:dscModuleName = 'SqlServerDsc' + $env:SqlServerDscCI = $true + Import-Module -Name $script:dscModuleName $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:dscModuleName @@ -41,6 +43,8 @@ AfterAll { # Unload the module being tested so that it doesn't impact any other tests. Get-Module -Name $script:dscModuleName -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'Install-SqlDscServer' -Tag 'Public' { diff --git a/tests/Unit/Public/Invoke-SqlDscQuery.Tests.ps1 b/tests/Unit/Public/Invoke-SqlDscQuery.Tests.ps1 index 9533cca2e..f8a479c30 100644 --- a/tests/Unit/Public/Invoke-SqlDscQuery.Tests.ps1 +++ b/tests/Unit/Public/Invoke-SqlDscQuery.Tests.ps1 @@ -26,6 +26,8 @@ BeforeDiscovery { BeforeAll { $script:dscModuleName = 'SqlServerDsc' + $env:SqlServerDscCI = $true + Import-Module -Name $script:dscModuleName # Loading mocked classes @@ -43,6 +45,8 @@ AfterAll { # Unload the module being tested so that it doesn't impact any other tests. Get-Module -Name $script:dscModuleName -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'Invoke-SqlDscQuery' -Tag 'Public' { diff --git a/tests/Unit/Public/New-SqlDscAudit.Tests.ps1 b/tests/Unit/Public/New-SqlDscAudit.Tests.ps1 index 05447edff..01b561cfb 100644 --- a/tests/Unit/Public/New-SqlDscAudit.Tests.ps1 +++ b/tests/Unit/Public/New-SqlDscAudit.Tests.ps1 @@ -26,6 +26,8 @@ BeforeDiscovery { BeforeAll { $script:dscModuleName = 'SqlServerDsc' + $env:SqlServerDscCI = $true + Import-Module -Name $script:dscModuleName # Loading mocked classes @@ -43,6 +45,8 @@ AfterAll { # Unload the module being tested so that it doesn't impact any other tests. Get-Module -Name $script:dscModuleName -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'New-SqlDscAudit' -Tag 'Public' { diff --git a/tests/Unit/Public/Remove-SqlDscAudit.Tests.ps1 b/tests/Unit/Public/Remove-SqlDscAudit.Tests.ps1 index 127027636..20b1bb5d9 100644 --- a/tests/Unit/Public/Remove-SqlDscAudit.Tests.ps1 +++ b/tests/Unit/Public/Remove-SqlDscAudit.Tests.ps1 @@ -26,6 +26,8 @@ BeforeDiscovery { BeforeAll { $script:dscModuleName = 'SqlServerDsc' + $env:SqlServerDscCI = $true + Import-Module -Name $script:dscModuleName # Loading mocked classes @@ -43,6 +45,8 @@ AfterAll { # Unload the module being tested so that it doesn't impact any other tests. Get-Module -Name $script:dscModuleName -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'Remove-SqlDscAudit' -Tag 'Public' { diff --git a/tests/Unit/Public/Remove-SqlDscNode.Tests.ps1 b/tests/Unit/Public/Remove-SqlDscNode.Tests.ps1 index a17c00937..199e9cbe7 100644 --- a/tests/Unit/Public/Remove-SqlDscNode.Tests.ps1 +++ b/tests/Unit/Public/Remove-SqlDscNode.Tests.ps1 @@ -27,6 +27,8 @@ BeforeDiscovery { BeforeAll { $script:dscModuleName = 'SqlServerDsc' + $env:SqlServerDscCI = $true + Import-Module -Name $script:dscModuleName $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:dscModuleName @@ -41,6 +43,8 @@ AfterAll { # Unload the module being tested so that it doesn't impact any other tests. Get-Module -Name $script:dscModuleName -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'Remove-SqlDscNode' -Tag 'Public' { diff --git a/tests/Unit/Public/Repair-SqlDscServer.Tests.ps1 b/tests/Unit/Public/Repair-SqlDscServer.Tests.ps1 index 90a727eca..de70d452f 100644 --- a/tests/Unit/Public/Repair-SqlDscServer.Tests.ps1 +++ b/tests/Unit/Public/Repair-SqlDscServer.Tests.ps1 @@ -27,6 +27,8 @@ BeforeDiscovery { BeforeAll { $script:dscModuleName = 'SqlServerDsc' + $env:SqlServerDscCI = $true + Import-Module -Name $script:dscModuleName $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:dscModuleName @@ -41,6 +43,8 @@ AfterAll { # Unload the module being tested so that it doesn't impact any other tests. Get-Module -Name $script:dscModuleName -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'Repair-SqlDscServer' -Tag 'Public' { diff --git a/tests/Unit/Public/Set-SqlDscAudit.Tests.ps1 b/tests/Unit/Public/Set-SqlDscAudit.Tests.ps1 index eef5f1098..1420209d8 100644 --- a/tests/Unit/Public/Set-SqlDscAudit.Tests.ps1 +++ b/tests/Unit/Public/Set-SqlDscAudit.Tests.ps1 @@ -26,6 +26,8 @@ BeforeDiscovery { BeforeAll { $script:dscModuleName = 'SqlServerDsc' + $env:SqlServerDscCI = $true + Import-Module -Name $script:dscModuleName # Loading mocked classes @@ -43,6 +45,8 @@ AfterAll { # Unload the module being tested so that it doesn't impact any other tests. Get-Module -Name $script:dscModuleName -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'Set-SqlDscAudit' -Tag 'Public' { diff --git a/tests/Unit/Public/Set-SqlDscDatabasePermission.Tests.ps1 b/tests/Unit/Public/Set-SqlDscDatabasePermission.Tests.ps1 index 7d77f4d98..68427ae25 100644 --- a/tests/Unit/Public/Set-SqlDscDatabasePermission.Tests.ps1 +++ b/tests/Unit/Public/Set-SqlDscDatabasePermission.Tests.ps1 @@ -26,6 +26,8 @@ BeforeDiscovery { BeforeAll { $script:dscModuleName = 'SqlServerDsc' + $env:SqlServerDscCI = $true + Import-Module -Name $script:dscModuleName # Loading mocked classes @@ -43,6 +45,8 @@ AfterAll { # Unload the module being tested so that it doesn't impact any other tests. Get-Module -Name $script:dscModuleName -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'Set-SqlDscDatabasePermission' -Tag 'Public' { diff --git a/tests/Unit/Public/Set-SqlDscServerPermission.Tests.ps1 b/tests/Unit/Public/Set-SqlDscServerPermission.Tests.ps1 index 12aa3720b..d461d0195 100644 --- a/tests/Unit/Public/Set-SqlDscServerPermission.Tests.ps1 +++ b/tests/Unit/Public/Set-SqlDscServerPermission.Tests.ps1 @@ -26,6 +26,8 @@ BeforeDiscovery { BeforeAll { $script:dscModuleName = 'SqlServerDsc' + $env:SqlServerDscCI = $true + Import-Module -Name $script:dscModuleName # Loading mocked classes @@ -43,6 +45,8 @@ AfterAll { # Unload the module being tested so that it doesn't impact any other tests. Get-Module -Name $script:dscModuleName -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'Set-SqlDscServerPermission' -Tag 'Public' { diff --git a/tests/Unit/Public/Test-SqlDscIsDatabasePrincipal.Tests.ps1 b/tests/Unit/Public/Test-SqlDscIsDatabasePrincipal.Tests.ps1 index 49e798b90..664935002 100644 --- a/tests/Unit/Public/Test-SqlDscIsDatabasePrincipal.Tests.ps1 +++ b/tests/Unit/Public/Test-SqlDscIsDatabasePrincipal.Tests.ps1 @@ -26,6 +26,8 @@ BeforeDiscovery { BeforeAll { $script:dscModuleName = 'SqlServerDsc' + $env:SqlServerDscCI = $true + Import-Module -Name $script:dscModuleName # Loading mocked classes @@ -43,6 +45,8 @@ AfterAll { # Unload the module being tested so that it doesn't impact any other tests. Get-Module -Name $script:dscModuleName -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'Test-SqlDscIsDatabasePrincipal' -Tag 'Public' { diff --git a/tests/Unit/Public/Test-SqlDscIsLogin.Tests.ps1 b/tests/Unit/Public/Test-SqlDscIsLogin.Tests.ps1 index 105e1994c..d3329b38f 100644 --- a/tests/Unit/Public/Test-SqlDscIsLogin.Tests.ps1 +++ b/tests/Unit/Public/Test-SqlDscIsLogin.Tests.ps1 @@ -26,6 +26,8 @@ BeforeDiscovery { BeforeAll { $script:dscModuleName = 'SqlServerDsc' + $env:SqlServerDscCI = $true + Import-Module -Name $script:dscModuleName # Loading mocked classes @@ -43,6 +45,8 @@ AfterAll { # Unload the module being tested so that it doesn't impact any other tests. Get-Module -Name $script:dscModuleName -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'Test-SqlDscIsLogin' -Tag 'Public' { diff --git a/tests/Unit/Public/Uninstall-SqlDscServer.Tests.ps1 b/tests/Unit/Public/Uninstall-SqlDscServer.Tests.ps1 index aa6b8ff7b..122520127 100644 --- a/tests/Unit/Public/Uninstall-SqlDscServer.Tests.ps1 +++ b/tests/Unit/Public/Uninstall-SqlDscServer.Tests.ps1 @@ -27,6 +27,8 @@ BeforeDiscovery { BeforeAll { $script:dscModuleName = 'SqlServerDsc' + $env:SqlServerDscCI = $true + Import-Module -Name $script:dscModuleName $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:dscModuleName @@ -41,6 +43,8 @@ AfterAll { # Unload the module being tested so that it doesn't impact any other tests. Get-Module -Name $script:dscModuleName -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' } Describe 'Uninstall-SqlDscServer' -Tag 'Public' { diff --git a/tests/Unit/SqlServerDsc.Common.Tests.ps1 b/tests/Unit/SqlServerDsc.Common.Tests.ps1 index a64191b5b..a38d802b8 100644 --- a/tests/Unit/SqlServerDsc.Common.Tests.ps1 +++ b/tests/Unit/SqlServerDsc.Common.Tests.ps1 @@ -2223,287 +2223,11 @@ Describe 'SqlServerDsc.Common\Test-LoginEffectivePermissions' -Tag 'TestLoginEff Describe 'SqlServerDsc.Common\Import-SQLPSModule' -Tag 'ImportSQLPSModule' { BeforeAll { - <# - This is the path to the latest version of SQLPS, to test that only the - newest SQLPS module is returned. - #> - $sqlPsLatestModulePath = 'C:\Program Files (x86)\Microsoft SQL Server\130\Tools\PowerShell\Modules\SQLPS\Sqlps.ps1' - - <# - For SQLPS module this should be the root of the module. - The .psd1 file is parsed from the module full path in the code. - #> - $sqlPsExpectedModulePath = Split-Path -Path $sqlPsLatestModulePath -Parent - - - $mockImportModule = { - # Convert the single value array [String[]] to the expected string value [String]. - $moduleNameToImport = $Name[0] - - if ($moduleNameToImport -ne $mockExpectedModuleNameToImport) - { - throw ('Wrong module was loaded. Expected {0}, but was {1}.' -f $mockExpectedModuleNameToImport, $moduleNameToImport) - } - - switch ($moduleNameToImport) - { - 'SqlServer' - { - $importModuleResult = @{ - ModuleType = 'Script' - Version = '21.0.17279' - Name = $moduleNameToImport - Path = 'C:\Program Files\WindowsPowerShell\Modules\sqlserver\21.0.17279\SqlServer.psm1' - } - } - - $sqlPsExpectedModulePath - { - # Can not use $Name because that contain the path to the module manifest. - $importModuleResult = @( - @{ - ModuleType = 'Script' - Version = '0.0' - # Intentionally formatted to correctly mimic a real run. - Name = 'Sqlps' - Path = $sqlPsLatestModulePath - } - @{ - ModuleType = 'Manifest' - Version = '1.0' - # Intentionally formatted to correctly mimic a real run. - Name = 'sqlps' - Path = $sqlPsLatestModulePath - } - ) - } - } - - return $importModuleResult - } - - $mockGetModuleSqlServer = { - # Return an array to test so that the latest version is only imported. - return @( - New-Object -TypeName PSObject -Property @{ - Name = 'SqlServer' - Version = [Version] '1.0' - } - - New-Object -TypeName PSObject -Property @{ - Name = 'SqlServer' - Version = [Version] '2.0' - } - ) - } - - $mockGetModuleSqlPs = { - # Return an array to test so that the latest version is only imported. - return @( - New-Object -TypeName PSObject -Property @{ - Name = 'SQLPS' - # This is a path to an older version of SQL PS than $sqlPsLatestModulePath. - Path = 'C:\Program Files (x86)\Microsoft SQL Server\120\Tools\PowerShell\Modules\SQLPS\Sqlps.ps1' - } - - New-Object -TypeName PSObject -Property @{ - Name = 'SQLPS' - Path = $sqlPsLatestModulePath - } - ) - } - - $mockGetModule_SqlServer_ParameterFilter = { - $FullyQualifiedName.Name -eq 'SqlServer' -and $ListAvailable -eq $true - } - - $mockGetModule_SQLPS_ParameterFilter = { - $FullyQualifiedName.Name -eq 'SQLPS' -and $ListAvailable -eq $true - } - - Mock -CommandName Set-PSModulePath - Mock -CommandName Push-Location - Mock -CommandName Pop-Location - } - - - Context 'When module SqlServer is already loaded into the session' { - BeforeAll { - Mock -CommandName Import-Module -MockWith $mockImportModule - Mock -CommandName Get-Module -MockWith { - return @{ - Name = 'SqlServer' - } - } - } - - It 'Should use the already loaded module and not call Import-Module' { - { Import-SQLPSModule } | Should -Not -Throw - - Should -Invoke -CommandName Import-Module -Exactly -Times 0 -Scope It - } - } - - Context 'When module SQLPS is already loaded into the session' { - BeforeAll { - Mock -CommandName Import-Module -MockWith $mockImportModule - Mock -CommandName Get-Module -MockWith { - return @{ - Name = 'SQLPS' - } - } - } - - It 'Should use the already loaded module and not call Import-Module' { - { Import-SQLPSModule } | Should -Not -Throw - - Should -Invoke -CommandName Import-Module -Exactly -Times 0 -Scope It - } - } - - Context 'When module SqlServer exists, but not loaded into the session' { - BeforeAll { - Mock -CommandName Import-Module -MockWith $mockImportModule - Mock -CommandName Get-Module -ParameterFilter { - <# - Should not be called with a login type. - - Due to issue https://github.com/pester/Pester/issues/1542 - we cannot use `$PSBoundParameters.ContainsKey('Name') -eq $true`. - #> - $null -ne $Name - } - - Mock -CommandName Get-Module -MockWith $mockGetModuleSqlServer -ParameterFilter $mockGetModule_SqlServer_ParameterFilter - - $mockExpectedModuleNameToImport = 'SqlServer' - } - - It 'Should import the SqlServer module without throwing' { - { Import-SQLPSModule } | Should -Not -Throw - - Should -Invoke -CommandName Get-Module -ParameterFilter $mockGetModule_SqlServer_ParameterFilter -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Push-Location -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Pop-Location -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Import-Module -Exactly -Times 1 -Scope It - } - } - - Context 'When only module SQLPS exists, but not loaded into the session, and using -Force' { - BeforeAll { - Mock -CommandName Import-Module -MockWith $mockImportModule - Mock -CommandName Remove-Module - Mock -CommandName Get-Module -ParameterFilter { - <# - Should not be called with a login type. - - Due to issue https://github.com/pester/Pester/issues/1542 - we cannot use `$PSBoundParameters.ContainsKey('Name') -eq $true`. - #> - $null -ne $Name - } - - $mockExpectedModuleNameToImport = $sqlPsExpectedModulePath - - Mock -CommandName Get-Module -MockWith $mockGetModuleSqlPs -ParameterFilter $mockGetModule_SQLPS_ParameterFilter - Mock -CommandName Get-Module -MockWith { - return $null - } -ParameterFilter $mockGetModule_SqlServer_ParameterFilter - } - - It 'Should import the SqlServer module without throwing' { - { Import-SQLPSModule -Force } | Should -Not -Throw - - Should -Invoke -CommandName Get-Module -ParameterFilter $mockGetModule_SqlServer_ParameterFilter -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Get-Module -ParameterFilter $mockGetModule_SQLPS_ParameterFilter -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Push-Location -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Pop-Location -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Remove-Module -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Import-Module -Exactly -Times 1 -Scope It - } - } - - Context 'When neither SqlServer or SQLPS exists' { - BeforeAll { - Mock -CommandName Import-Module - - $mockExpectedModuleNameToImport = $sqlPsExpectedModulePath - - Mock -CommandName Get-Module - } - - It 'Should throw the correct error message' { - $mockLocalizedString = InModuleScope -ScriptBlock { - $script:localizedData.PowerShellSqlModuleNotFound - } - - $mockErrorRecord = Get-InvalidOperationRecord -Message ( - $mockLocalizedString - ) - - { Import-SQLPSModule } | Should -Throw -ExpectedMessage $mockErrorRecord.Exception.Message - - Should -Invoke -CommandName Get-Module -ParameterFilter $mockGetModule_SqlServer_ParameterFilter -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Get-Module -ParameterFilter $mockGetModule_SQLPS_ParameterFilter -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Push-Location -Exactly -Times 0 -Scope It - Should -Invoke -CommandName Pop-Location -Exactly -Times 0 -Scope It - Should -Invoke -CommandName Import-Module -Exactly -Times 0 -Scope It - } - } - - Context 'When Import-Module fails to load the module' { - BeforeAll { - $mockExpectedModuleNameToImport = 'SqlServer' - - Mock -CommandName Get-Module -MockWith $mockGetModuleSqlServer -ParameterFilter $mockGetModule_SqlServer_ParameterFilter - Mock -CommandName Import-Module -MockWith { - throw $errorMessage - } - } - - It 'Should throw the correct error message' { - $mockLocalizedString = InModuleScope -ScriptBlock { - $script:localizedData.FailedToImportPowerShellSqlModule - } - - $mockErrorRecord = Get-InvalidOperationRecord -Message ( - $mockLocalizedString -f $mockExpectedModuleNameToImport - ) - - { Import-SQLPSModule } | Should -Throw -ExpectedMessage ($mockErrorRecord.Exception.Message + '*') - - Should -Invoke -CommandName Get-Module -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Push-Location -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Pop-Location -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Import-Module -Exactly -Times 1 -Scope It - } + Mock -CommandName Import-SqlDscPreferredModule } - # This is to test the tests (so the mock throws correctly) - Context 'When mock Import-Module is called with wrong module name' { - BeforeAll { - $mockExpectedModuleNameToImport = 'UnknownModule' - - Mock -CommandName Import-Module -MockWith $mockImportModule - Mock -CommandName Get-Module -MockWith $mockGetModuleSqlServer -ParameterFilter $mockGetModule_SqlServer_ParameterFilter - } - - It 'Should throw the correct error message' { - $mockLocalizedString = InModuleScope -ScriptBlock { - $script:localizedData.FailedToImportPowerShellSqlModule - } - - $mockErrorRecord = Get-InvalidOperationRecord -Message ( - $mockLocalizedString -f 'SqlServer' - ) - - { Import-SQLPSModule } | Should -Throw -ExpectedMessage ($mockErrorRecord.Exception.Message + '*') - - Should -Invoke -CommandName Get-Module -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Push-Location -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Pop-Location -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Import-Module -Exactly -Times 1 -Scope It - } + It 'Should not throw an exception' { + { Import-SQLPSModule } | Should -Not -Throw } }