From c68733cdc2efd985e4cc7139e1bb80ffdfb96c0b Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Tue, 12 Sep 2017 17:54:30 +0200 Subject: [PATCH] xSQLServer: Using correct assemblies for module SqlServer (#800) - 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). - 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 so that the integration test renames back the SQLPS module folders if they was renamed by AppVeyor (in the appveyor.yml file). (issue #774). - Fixed so integration test does not write warnings when SQLPS module is loaded (issue #798). --- CHANGELOG.md | 20 +++++++++++++ ...MSFT_xSQLServerSetup.Integration.Tests.ps1 | 15 ++++++++++ .../MSFT_xSQLServerSetup.config.ps1 | 4 +++ Tests/Unit/xSQLServerHelper.Tests.ps1 | 1 + appveyor.yml | 28 +++++++++++++++---- xSQLServerHelper.psm1 | 11 +++++++- 6 files changed, 73 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c994b3753..d3363db23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Tests/Integration/MSFT_xSQLServerSetup.Integration.Tests.ps1 b/Tests/Integration/MSFT_xSQLServerSetup.Integration.Tests.ps1 index 693500803..d3c42f141 100644 --- a/Tests/Integration/MSFT_xSQLServerSetup.Integration.Tests.ps1 +++ b/Tests/Integration/MSFT_xSQLServerSetup.Integration.Tests.ps1 @@ -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' diff --git a/Tests/Integration/MSFT_xSQLServerSetup.config.ps1 b/Tests/Integration/MSFT_xSQLServerSetup.config.ps1 index 4ba5c9204..4ff1fe13c 100644 --- a/Tests/Integration/MSFT_xSQLServerSetup.config.ps1 +++ b/Tests/Integration/MSFT_xSQLServerSetup.config.ps1 @@ -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 diff --git a/Tests/Unit/xSQLServerHelper.Tests.ps1 b/Tests/Unit/xSQLServerHelper.Tests.ps1 index e7ea4fd0e..6da15cb0e 100644 --- a/Tests/Unit/xSQLServerHelper.Tests.ps1 +++ b/Tests/Unit/xSQLServerHelper.Tests.ps1 @@ -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 ` diff --git a/appveyor.yml b/appveyor.yml index ecd58f05d..bbd5098b9 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -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 # diff --git a/xSQLServerHelper.psm1 b/xSQLServerHelper.psm1 index 523e76ea3..fe2601a30 100644 --- a/xSQLServerHelper.psm1 +++ b/xSQLServerHelper.psm1 @@ -37,7 +37,7 @@ function Connect-SQL $SetupCredential ) - $null = [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.Smo') + Import-SQLPSModule if ($SQLInstanceName -eq 'MSSQLSERVER') { @@ -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 }