diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c78b3e75..091e2d2df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - SqlServerDsc + - Integration tests now also run using SQLPS. - Added a new build task `fastbuild` that can be used during development process when there are no need to generate documentation. - Added new public command: diff --git a/azure-pipelines.yml b/azure-pipelines.yml index ade6ce3e9..ba471b41b 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -277,8 +277,7 @@ stages: pwsh: false - powershell: | Import-Module -Name ./tests/TestHelpers/CommonTestHelper.psm1 - # Make sure to keep SQLPS so that Invoke-SqlCmd is available. - Remove-PowerShellModuleFromCI -Name @('SqlServer') + Remove-PowerShellModuleFromCI -Name @('SqlServer', 'SQLPS') Remove-Module -Name CommonTestHelper name: cleanCIWorker displayName: 'Clean CI worker' @@ -329,6 +328,97 @@ stages: testResultsFiles: '$(buildFolderName)/$(testResultFolderName)/NUnit*.xml' testRunTitle: 'Integration (dbatools) ($(TEST_CONFIGURATION) / $(JOB_VMIMAGE))' + - job: Test_Integration_SQLPS + displayName: 'Integration (SQLPS)' + dependsOn: Test_Integration + strategy: + matrix: + SQL2016_WIN2022: + JOB_VMIMAGE: 'windows-2022' + TEST_CONFIGURATION: 'Integration_SQL2016' + SQL2017_WIN2022: + JOB_VMIMAGE: 'windows-2022' + TEST_CONFIGURATION: 'Integration_SQL2017' + SQL2019_WIN2022: + JOB_VMIMAGE: 'windows-2022' + TEST_CONFIGURATION: 'Integration_SQL2019' + SQL2022_WIN2022: + JOB_VMIMAGE: 'windows-2022' + TEST_CONFIGURATION: 'Integration_SQL2022' + pool: + vmImage: $(JOB_VMIMAGE) + timeoutInMinutes: 0 + variables: + SkipInstallSMO: true + steps: + - task: DownloadPipelineArtifact@2 + displayName: 'Download Build Artifact' + inputs: + buildType: 'current' + artifactName: $(buildArtifactName) + targetPath: '$(Build.SourcesDirectory)/$(buildFolderName)' + - task: PowerShell@2 + name: configureWinRM + displayName: 'Configure WinRM' + inputs: + targetType: 'inline' + script: 'winrm quickconfig -quiet' + pwsh: false + - powershell: | + Import-Module -Name ./tests/TestHelpers/CommonTestHelper.psm1 + # Make sure to remove SQLPS so that a wrong version is not being imported prior to the SQL Server install. + Remove-PowerShellModuleFromCI -Name @('SqlServer', 'SQLPS') + Remove-Module -Name CommonTestHelper + name: cleanCIWorker + displayName: 'Clean CI worker' + - powershell: | + ./build.ps1 -Tasks test -CodeCoverageThreshold 0 -PesterTag $(TEST_CONFIGURATION) -PesterPath @( + # Run the integration tests in a specific group order. + # Group 1 + 'tests/Integration/DSC_SqlSetup.Integration.Tests.ps1' + # Group 2 + 'tests/Integration/DSC_SqlAgentAlert.Integration.Tests.ps1' + 'tests/Integration/DSC_SqlLogin.Integration.Tests.ps1' + 'tests/Integration/DSC_SqlEndpoint.Integration.Tests.ps1' + 'tests/Integration/DSC_SqlDatabaseMail.Integration.Tests.ps1' + 'tests/Integration/DSC_SqlRSSetup.Integration.Tests.ps1' + 'tests/Integration/DSC_SqlDatabaseDefaultLocation.Integration.Tests.ps1' + 'tests/Integration/DSC_SqlDatabase.Integration.Tests.ps1' + 'tests/Integration/DSC_SqlAlwaysOnService.Integration.Tests.ps1' + 'tests/Integration/DSC_SqlAgentOperator.Integration.Tests.ps1' + 'tests/Integration/DSC_SqlServiceAccount.Integration.Tests.ps1' + 'tests/Integration/DSC_SqlAgentFailsafe.Integration.Tests.ps1' + 'tests/Integration/DSC_SqlTraceFlag.Integration.Tests.ps1' + # Group 3 + 'tests/Integration/DSC_SqlRole.Integration.Tests.ps1' + 'tests/Integration/DSC_SqlRS_Default.Integration.Tests.ps1' + 'tests/Integration/DSC_SqlDatabaseUser.Integration.Tests.ps1' + ## Replication does not work with SQLPS and SQL Server 2022. + $(if ('$(TEST_CONFIGURATION)' -ne 'Integration_SQL2022') { 'tests/Integration/DSC_SqlReplication.Integration.Tests.ps1' }) + 'tests/Integration/DSC_SqlAudit.Integration.Tests.ps1' + # Group 4 + 'tests/Integration/DSC_SqlScript.Integration.Tests.ps1' + 'tests/Integration/DSC_SqlDatabasePermission.Integration.Tests.ps1' + 'tests/Integration/DSC_SqlPermission.Integration.Tests.ps1' + 'tests/Integration/DSC_SqlWindowsFirewall.Integration.Tests.ps1' + # Group 5 + 'tests/Integration/DSC_SqlSecureConnection.Integration.Tests.ps1' + 'tests/Integration/DSC_SqlScriptQuery.Integration.Tests.ps1' + 'tests/Integration/DSC_SqlProtocol.Integration.Tests.ps1' + # Group 6 (tests makes changes that could make SQL Server to loose connectivity) + 'tests/Integration/DSC_SqlProtocolTcpIp.Integration.Tests.ps1' + 'tests/Integration/DSC_SqlDatabaseObjectPermission.Integration.Tests.ps1' + ) + name: test + displayName: 'Run Integration Test' + - task: PublishTestResults@2 + displayName: 'Publish Test Results' + condition: succeededOrFailed() + inputs: + testResultsFormat: 'NUnit' + testResultsFiles: '$(buildFolderName)/$(testResultFolderName)/NUnit*.xml' + testRunTitle: 'Integration (dbatools) ($(TEST_CONFIGURATION) / $(JOB_VMIMAGE))' + - job: Test_Integration_RS displayName: 'Integration Reporting Services' strategy: diff --git a/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1 b/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1 index 9201acd1c..eee3acd4d 100644 --- a/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1 +++ b/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1 @@ -1949,8 +1949,41 @@ function Invoke-SqlScript $Encrypt ) + # Write-Verbose -Message ('Session PSModulePath: {0}' -f $env:PSModulePath) -Verbose + # Write-Verbose -Message ('Available modules: {0}' -f (Get-Module -Name @('SqlServer', 'SqlServerDsc', 'SQLPS') -ListAvailable | Out-String)) -Verbose + # Write-Verbose -Message ('All PSModulePath: {0}' -f (Get-PSModulePath -FromTarget 'Session', 'User', 'Machine')) -Verbose + + # Write-Verbose -Message ('Found module: {0}' -f (Get-SqlDscPreferredModule -ErrorAction 'Continue')) -Verbose + # Write-Verbose -Message ('Found module (refresh): {0}' -f (Get-SqlDscPreferredModule -Refresh -ErrorAction 'Continue')) -Verbose + + # Import the preferred module. Import-SqlDscPreferredModule + <# + If dbatools was imported as the preferred module and SqlServer is not available + then we need to import SQLPS because it will not always be dynamically imported + when Invoke-SqlCmd is called. + + Import-SqlDscPreferredModule must be called before Get-SqlDscPreferredModule + so that any module set using environment variable SMODefaultModuleName is + always imported first. + #> + if ((Get-SqlDscPreferredModule -Name @('SqlServer', 'dbatools')) -eq 'dbatools') + { + # Call Get-SqlDscPreferredModule to make sure we get the latest installed version of SQLPS. + Import-Module -Name (Get-SqlDscPreferredModule -Name 'SQLPS') + } + + Write-Verbose -Message ('Imported modules: {0}' -f (Get-Module | Out-String)) -Verbose + if ((Get-Module -Name 'SQLPS')) + { + Write-Verbose -Message ('DEBUG: All SQLPS commands: {0}' -f (Get-Command -Module 'SQLPS' -ErrorAction 'SilentlyContinue' | Out-String)) -Verbose + } + else + { + Write-Verbose -Message 'DEBUG: NO SQLPS IS LOADED' -Verbose + } + if ($PSCmdlet.ParameterSetName -eq 'File') { $null = $PSBoundParameters.Remove('Query') diff --git a/tests/Integration/DSC_SqlReplication.config.ps1 b/tests/Integration/DSC_SqlReplication.config.ps1 index 80d10f87f..28f421ca4 100644 --- a/tests/Integration/DSC_SqlReplication.config.ps1 +++ b/tests/Integration/DSC_SqlReplication.config.ps1 @@ -40,13 +40,13 @@ Configuration DSC_SqlReplication_Prerequisites_Config node $AllNodes.NodeName { - Service ('StopSqlServerInstance{0}' -f $Node.DefaultInstanceName) + Service ('StartSqlServerInstance{0}' -f $Node.DefaultInstanceName) { Name = $Node.DefaultInstanceName State = 'Running' } - Service ('StopSqlServerAgentForInstance{0}' -f $Node.DefaultInstanceName) + Service ('StartSqlServerAgentForInstance{0}' -f $Node.DefaultInstanceName) { Name = 'SQLSERVERAGENT' State = 'Running' diff --git a/tests/Integration/DSC_SqlSetup.Integration.Tests.ps1 b/tests/Integration/DSC_SqlSetup.Integration.Tests.ps1 index 5e27ad3a2..8fcb1e580 100644 --- a/tests/Integration/DSC_SqlSetup.Integration.Tests.ps1 +++ b/tests/Integration/DSC_SqlSetup.Integration.Tests.ps1 @@ -169,7 +169,7 @@ BeforeAll { } AfterAll { - Restore-TestEnvironment -TestEnvironment $script:testEnvironment + Restore-TestEnvironment -TestEnvironment $script:testEnvironment -KeepNewMachinePSModulePath Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force } @@ -214,7 +214,7 @@ Describe "$($script:dscResourceName)_Integration" -Tag @('Integration_SQL2016', } } - Context ('When using configuration <_>') -ForEach @( + Context ('When using configuration <_>') -Skip:($null -ne $env:SkipInstallSMO) -ForEach @( "$($script:dscResourceName)_InstallSMOModule_Config" ) { BeforeAll { diff --git a/tests/Integration/DSC_SqlSetup.config.ps1 b/tests/Integration/DSC_SqlSetup.config.ps1 index c746210c0..8f71fefca 100644 --- a/tests/Integration/DSC_SqlSetup.config.ps1 +++ b/tests/Integration/DSC_SqlSetup.config.ps1 @@ -198,6 +198,16 @@ else } } +# Do not use the feature flag AnalysisServicesConnection when running tests with SQLPS. +$featureFlag = if ((Get-Module -Name 'SqlServer', 'dbatools' -ListAvailable)) +{ + @('AnalysisServicesConnection') +} +else +{ + @() +} + <# Creating all the credential objects to save some repeating code. #> @@ -691,7 +701,7 @@ Configuration DSC_SqlSetup_InstallMultiDimensionalAnalysisServicesAsSystem_Confi { SqlSetup 'Integration_Test' { - FeatureFlag = @('AnalysisServicesConnection') + FeatureFlag = [System.String[]] $featureFlag InstanceName = $Node.AnalysisServicesMultiInstanceName Features = $Node.AnalysisServicesMultiFeatures @@ -745,7 +755,7 @@ Configuration DSC_SqlSetup_InstallTabularAnalysisServicesAsSystem_Config { SqlSetup 'Integration_Test' { - FeatureFlag = @('AnalysisServicesConnection') + FeatureFlag = [System.String[]] $featureFlag InstanceName = $Node.AnalysisServicesTabularInstanceName Features = $Node.AnalysisServicesTabularFeatures