Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xSQLServer: Using correct assemblies for module SqlServer #800

Merged
merged 10 commits into from
Sep 12, 2017
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,28 @@

## Unreleased

- Changes to xSQLServer
- Updated appveyor.yml so that integration tests run in order and so that
the SQLPS module folders are renamed to not disturb the units test, but
can be renamed back by the integration tests xSQLServerSetup so that the
integration tests can run successfully.
([issue #774](https://github.com/PowerShell/xFailOverCluster/issues/774)).
- Changes to xSQLServerHelper
- Changes to Connect-SQL and Import-SQLPSModule
- Now it correctly loads the correct assemblies when SqlServer module is
present (issue #649).
- Now SQLPS module will be correctly loaded (discovered) after installation
of SQL Server. Previously resources depending on SQLPS module could fail
because SQLPS was not found after installation because the PSModulePath
environment variable in the (LCM) PowerShell session did not contain the new
module path.
- Changes to xSQLServerSetup
- Fixed an issue with trailing slashes in the 'UpdateSource' Property ([issue #720](https://github.com/PowerShell/xSQLServer/issues/720)).
- Fixed so that the integration test renames back the SQLPS module folders if
they was renamed by AppVeyor (in the appveyor.yml file).
([issue #774](https://github.com/PowerShell/xFailOverCluster/issues/774)).
- Fixed so integration test does not write warnings when SQLPS module is loaded
([issue #798](https://github.com/PowerShell/xFailOverCluster/issues/798)).
- Changes to xSQLServerAlwaysOnAvailabilityGroup
- Change the check of the values entered as parameter for
BasicAvailabilityGroup. It is a boolean, hence it was not possible to
Expand Down
15 changes: 15 additions & 0 deletions Tests/Integration/MSFT_xSQLServerSetup.Integration.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ $TestEnvironment = Initialize-TestEnvironment `

#endregion

<#
Workaround for issue #774. In the appveyor.yml file the folder
C:\Program Files (x86)\Microsoft SQL Server\**\Tools\PowerShell\Modules
was renamed to
C:\Program Files (x86)\Microsoft SQL Server\**\Tools\PowerShell\Modules.old
here we rename back the folder to the correct name. Only the version need
for our tests are renamed.
#>
$sqlModulePath = Get-ChildItem -Path 'C:\Program Files (x86)\Microsoft SQL Server\**\Tools\PowerShell\*.old'
$sqlModulePath | ForEach-Object -Process {
$newFolderName = (Split-Path -Path $_ -Leaf) -replace '\.old'
Write-Verbose ('Renaming ''{0}'' to ''..\{1}''' -f $_, $newFolderName) -Verbose
Rename-Item $_ -NewName $newFolderName -Force
}

$mockInstanceName = 'DSCSQL2016'
$mockFeatures = 'SQLENGINE,CONN,BC,SDK'
$mockSqlCollation = 'Finnish_Swedish_CI_AS'
Expand Down
4 changes: 4 additions & 0 deletions Tests/Integration/MSFT_xSQLServerSetup.config.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# This is used to make sure the integration test run in the correct order.
[Microsoft.DscResourceKit.IntegrationTest(OrderNumber = 1)]
param()

configuration MSFT_xSQLServerSetup_InstallSqlEngineAsSystem_Config
{
param
Expand Down
1 change: 1 addition & 0 deletions Tests/Unit/xSQLServerHelper.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,7 @@ InModuleScope $script:moduleName {
Describe 'Testing Connect-SQL' -Tag ConnectSql {
BeforeEach {
Mock -CommandName New-InvalidOperationException -MockWith $mockThrowLocalizedMessage -Verifiable
Mock -CommandName Import-SQLPSModule
Mock -CommandName New-Object `
-MockWith $mockNewObject_MicrosoftDatabaseEngine `
-ParameterFilter $mockNewObject_MicrosoftDatabaseEngine_ParameterFilter `
Expand Down
28 changes: 23 additions & 5 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,30 @@ build: false

test_script:
- ps: |
Write-Verbose -Message 'Removing all SQL related PowerShell modules so they can not be used by the common tests.' -Verbose
Write-Verbose -Message 'This is a workaround because the AppVeyor build worker image contains SQL Server. More information in issue #239.' -Verbose
Write-Verbose -Message 'Modules that are being removed:' -Verbose
Get-Module -ListAvailable -Name 'sql*' | ForEach-Object -Process { Write-Verbose $_.Path -Verbose; Remove-Item $_.Path -Force -Verbose; }
# Workaround for issue #239 and issue #774.
Write-Verbose -Message '--- WORKAROUND FOR ISSUE #239 AND ISSUE #774 ---' -Verbose
$sqlModules = Get-Module -ListAvailable -Name 'sql*'
$sqlUniqueModulePath = Split-Path -Path (Split-Path $sqlModules.Path -Parent) -Parent | Sort-Object -Unique
$sqlUniqueModulePath | ForEach-Object -Process {
$newFolderName = '{0}.old' -f (Split-Path -Path $_ -Leaf)
Write-Verbose ('Renaming ''{0}'' to ''..\{1}''' -f $_, $newFolderName) -Verbose
Rename-Item $_ -NewName $newFolderName -Force
}

Invoke-AppveyorTestScriptTask -CodeCoverage -CodeCovIo -ExcludeTag @()
Write-Verbose -Message '' -Verbose

# Workaround for issue #798
Write-Verbose -Message '--- WORKAROUND FOR ISSUE #798 ---' -Verbose
$azureModules = Get-Module -ListAvailable -Name 'Azure*'
$azureUniqueModulePath = Split-Path -Path (Split-Path $azureModules.Path -Parent) -Parent | Sort-Object -Unique
$azureUniqueModulePath | ForEach-Object -Process {
$newFolderName = '{0}.old' -f (Split-Path -Path $_ -Leaf)
Write-Verbose ('Renaming ''{0}'' to ''..\{1}''' -f $_, $newFolderName) -Verbose
Rename-Item $_ -NewName $newFolderName -Force
}
Write-Verbose -Message '' -Verbose

Invoke-AppveyorTestScriptTask -CodeCoverage -CodeCovIo -ExcludeTag @() -RunTestInOrder

#---------------------------------#
# deployment configuration #
Expand Down
11 changes: 10 additions & 1 deletion xSQLServerHelper.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function Connect-SQL
$SetupCredential
)

$null = [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.Smo')
Import-SQLPSModule

if ($SQLInstanceName -eq 'MSSQLSERVER')
{
Expand Down Expand Up @@ -697,6 +697,15 @@ function Import-SQLPSModule
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.
#>
$env:PSModulePath = [System.Environment]::GetEnvironmentVariable('PSModulePath', 'Machine')

$module = (Get-Module -FullyQualifiedName 'SQLPS' -ListAvailable).Name
}

Expand Down