From 6372e2ab067efa65fc666d0efcff11fc766feb58 Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Sun, 23 Apr 2023 17:37:55 +0200 Subject: [PATCH] Create separate pipeline jobs for dbatools --- azure-pipelines.yml | 156 +++++++++++++++++- source/Public/Get-SqlDscPreferredModule.ps1 | 14 +- .../Public/Import-SqlDscPreferredModule.ps1 | 14 +- .../DSC_SqlSetup.Integration.Tests.ps1 | 2 +- tests/Integration/DSC_SqlSetup.config.ps1 | 110 +++++++----- 5 files changed, 249 insertions(+), 47 deletions(-) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index f1e2b561e..7feb26524 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -19,7 +19,7 @@ variables: sourceFolderName: source defaultBranch: main -# cSpell: ignore setvariable updatebuildnumber DSCSQLTEST hqrmtest quickconfig +# cSpell: ignore setvariable updatebuildnumber DSCSQLTEST hqrmtest quickconfig dbatools stages: - stage: Build jobs: @@ -173,7 +173,7 @@ stages: vmImage: $(JOB_VMIMAGE) timeoutInMinutes: 0 variables: - SMODefaultModuleName: 'dbatools' + SMODefaultModuleName: 'SqlServer' steps: - task: DownloadPipelineArtifact@2 displayName: 'Download Build Artifact' @@ -241,6 +241,94 @@ stages: testResultsFiles: '$(buildFolderName)/$(testResultFolderName)/NUnit*.xml' testRunTitle: 'Integration ($(TEST_CONFIGURATION) / $(JOB_VMIMAGE))' + - job: Test_Integration_dbatools + displayName: 'Integration (dbatools)' + 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: + SMODefaultModuleName: 'dbatools' + 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 + # 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' + '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: @@ -316,6 +404,70 @@ stages: testResultsFiles: '$(buildFolderName)/$(testResultFolderName)/NUnit*.xml' testRunTitle: 'Integration RS ($(TEST_CONFIGURATION) / $(JOB_VMIMAGE))' + - job: Test_Integration_RS_dbatools + displayName: 'Integration Reporting Services (dbatools)' + 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' + variables: + SKIP_DATABASE_ENGINE_DEFAULT_INSTANCE: true + SKIP_ANALYSIS_MULTI_INSTANCE: true + SKIP_ANALYSIS_TABULAR_INSTANCE: true + SMODefaultModuleName: 'dbatools' + pool: + vmImage: $(JOB_VMIMAGE) + timeoutInMinutes: 0 + 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 + # 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_SqlRSSetup.Integration.Tests.ps1' + # Group 3 + 'tests/Integration/DSC_SqlRS.Integration.Tests.ps1' + ) + name: test + displayName: 'Run Reporting Services Integration Test' + - task: PublishTestResults@2 + displayName: 'Publish Test Results' + condition: succeededOrFailed() + inputs: + testResultsFormat: 'NUnit' + testResultsFiles: '$(buildFolderName)/$(testResultFolderName)/NUnit*.xml' + testRunTitle: 'Integration RS (dbatools) ($(TEST_CONFIGURATION) / $(JOB_VMIMAGE))' + - job: Code_Coverage displayName: 'Publish Code Coverage' dependsOn: Test_Unit diff --git a/source/Public/Get-SqlDscPreferredModule.ps1 b/source/Public/Get-SqlDscPreferredModule.ps1 index d6ce2dab4..bf8b013a6 100644 --- a/source/Public/Get-SqlDscPreferredModule.ps1 +++ b/source/Public/Get-SqlDscPreferredModule.ps1 @@ -51,13 +51,25 @@ function Get-SqlDscPreferredModule ( [Parameter()] [System.String[]] - $Name = @('dbatools', 'SQLPS'), + $Name, [Parameter()] [System.Management.Automation.SwitchParameter] $Refresh ) + if (-not $PSBoundParameters.ContainsKey('Name')) + { + $Name = if ($env:SMODefaultModuleName) + { + @($env:SMODefaultModuleName, 'SQLPS') + } + else + { + @('SqlServer', 'SQLPS') + } + } + if ($Refresh.IsPresent) { # Only run on Windows that has Machine state. diff --git a/source/Public/Import-SqlDscPreferredModule.ps1 b/source/Public/Import-SqlDscPreferredModule.ps1 index f1140e3c6..93dc97c12 100644 --- a/source/Public/Import-SqlDscPreferredModule.ps1 +++ b/source/Public/Import-SqlDscPreferredModule.ps1 @@ -40,13 +40,25 @@ function Import-SqlDscPreferredModule ( [Parameter()] [System.String] - $PreferredModule = 'dbatools', + $PreferredModule, [Parameter()] [System.Management.Automation.SwitchParameter] $Force ) + if (-not $PSBoundParameters.ContainsKey('PreferredModule')) + { + $PreferredModule = if ($env:SMODefaultModuleName) + { + $env:SMODefaultModuleName + } + else + { + 'SqlServer' + } + } + if ($Force.IsPresent) { Write-Verbose -Message $script:localizedData.PreferredModule_ForceRemoval diff --git a/tests/Integration/DSC_SqlSetup.Integration.Tests.ps1 b/tests/Integration/DSC_SqlSetup.Integration.Tests.ps1 index fb633bdf6..5e27ad3a2 100644 --- a/tests/Integration/DSC_SqlSetup.Integration.Tests.ps1 +++ b/tests/Integration/DSC_SqlSetup.Integration.Tests.ps1 @@ -215,7 +215,7 @@ Describe "$($script:dscResourceName)_Integration" -Tag @('Integration_SQL2016', } Context ('When using configuration <_>') -ForEach @( - "$($script:dscResourceName)_InstallSqlServerModule_Config" + "$($script:dscResourceName)_InstallSMOModule_Config" ) { BeforeAll { $configurationName = $_ diff --git a/tests/Integration/DSC_SqlSetup.config.ps1 b/tests/Integration/DSC_SqlSetup.config.ps1 index 1e2438aba..05d35b55b 100644 --- a/tests/Integration/DSC_SqlSetup.config.ps1 +++ b/tests/Integration/DSC_SqlSetup.config.ps1 @@ -1,7 +1,6 @@ -#region HEADER -# Integration Test Config Template Version: 1.2.0 -#endregion - +<# + cSpell: ignore MSAS SNAC DREPLAY CTLR dbatools DSCSQLTEST DSCTABULAR DSCMULTI Hadr SQLAGENT SQLSERVERAGENT +#> $configFile = [System.IO.Path]::ChangeExtension($MyInvocation.MyCommand.Path, 'json') if (Test-Path -Path $configFile) { @@ -37,7 +36,8 @@ else # Features CONN, BC, SDK, SNAC_SDK, DREPLAY_CLT, DREPLAY_CTLR are no longer supported in 2022. SupportedFeatures = 'SQLENGINE,REPLICATION' - SqlServerModuleVersion = '2.0.0-preview7' + SqlServerModuleVersion = '22.0.59' + DbatoolsModuleVersion = '2.0.0-preview7' } } @@ -54,7 +54,8 @@ else SupportedFeatures = 'SQLENGINE,REPLICATION,CONN,BC,SDK' - SqlServerModuleVersion = '2.0.0-preview7' + SqlServerModuleVersion = '21.1.18256' + DbatoolsModuleVersion = '2.0.0-preview7' } } @@ -67,7 +68,8 @@ else SupportedFeatures = 'SQLENGINE,REPLICATION,CONN,BC,SDK' - SqlServerModuleVersion = '2.0.0-preview7' + SqlServerModuleVersion = '21.1.18256' + DbatoolsModuleVersion = '2.0.0-preview7' } } @@ -80,7 +82,8 @@ else SupportedFeatures = 'SQLENGINE,REPLICATION,CONN,BC,SDK' - SqlServerModuleVersion = '2.0.0-preview7' + SqlServerModuleVersion = '21.1.18256' + DbatoolsModuleVersion = '2.0.0-preview7' } } } @@ -89,13 +92,31 @@ else $env:IsoDriveLetter = $mockIsoMediaDriveLetter $env:IsoImagePath = Join-Path -Path $env:TEMP -ChildPath $versionSpecificData.IsoImageName + if ($env:SMODefaultModuleName -and $env:SMODefaultModuleName -eq 'SqlServer') + { + $SMOModuleName = $env:SMODefaultModuleName + $SMOModuleVersion = $versionSpecificData.SqlServerModuleVersion + } + elseif ($env:SMODefaultModuleName -and $env:SMODefaultModuleName -eq 'dbatools') + { + $SMOModuleName = $env:SMODefaultModuleName + $SMOModuleVersion = $versionSpecificData.DbatoolsModuleVersion + } + else + { + $SMOModuleName = 'SqlServer' + $SMOModuleVersion = $versionSpecificData.SqlServerModuleVersion + } + $ConfigurationData = @{ AllNodes = @( @{ NodeName = 'localhost' - SqlServerModuleVersion = $versionSpecificData.SqlServerModuleVersion - SqlServerModuleVersionIsPrerelease = $true + SMOModuleName = $SMOModuleName + + SMOModuleVersion = $SMOModuleVersion + SMOModuleVersionIsPrerelease = $true SqlServerInstanceIdPrefix = $versionSpecificData.SqlServerInstanceIdPrefix AnalysisServiceInstanceIdPrefix = $versionSpecificData.AnalysisServiceInstanceIdPrefix @@ -212,8 +233,6 @@ $SqlAgentServiceSecondaryCredential = New-Object ` -ArgumentList @($ConfigurationData.AllNodes.SqlAgentServiceSecondaryAccountUserName, (ConvertTo-SecureString -String $ConfigurationData.AllNodes.SqlAgentServiceSecondaryAccountPassword -AsPlainText -Force)) -Write-Verbose -Message ('Current set preferred module name (SMODefaultModuleName): {0}' -f ($env:SMODefaultModuleName | Out-String)) -Verbose - <# .SYNOPSIS Setting up the dependencies to test installing SQL Server instances. @@ -308,7 +327,7 @@ Configuration DSC_SqlSetup_CreateDependencies_Config that it does not conflict with the SqlServerStubs module that is used by unit tests. #> -Configuration DSC_SqlSetup_InstallSqlServerModule_Config +Configuration DSC_SqlSetup_InstallSMOModule_Config { Import-DscResource -ModuleName 'PSDscResources' -ModuleVersion '2.12.0.0' @@ -388,16 +407,20 @@ Configuration DSC_SqlSetup_InstallSqlServerModule_Config } } - Environment 'SetDbatoolsAsDefault' + # Only set the environment variable for the LCM user only if the pipeline has it configured. + if ($env:SMODefaultModuleName) { - Name = 'SMODefaultModuleName' - Value = 'dbatools' - Ensure = 'Present' - Path = $false - Target = @('Process', 'Machine') + Environment 'SetSMODefaultModuleName' + { + Name = 'SMODefaultModuleName' + Value = $env:SMODefaultModuleName + Ensure = 'Present' + Path = $false + Target = @('Process', 'Machine') + } } - Script 'InstallSqlServerModule' + Script 'InstallSMOModule' { DependsOn = @( '[Script]InstallPowerShellGet' @@ -408,17 +431,17 @@ Configuration DSC_SqlSetup_InstallSqlServerModule_Config Set-PSRepository -Name 'PSGallery' -InstallationPolicy 'Trusted' # Uninstall any existing SqlServer module, to make we only have the one we need. - Get-Module -Name 'dbatools' -ListAvailable | Uninstall-Module -ErrorAction 'Stop' + Get-Module -Name $using:Node.SMOModuleName -ListAvailable | Uninstall-Module -ErrorAction 'Stop' # Make sure we use TLS 1.2. [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 $installModuleParameters = @{ - Name = 'dbatools' + Name = $using:Node.SMOModuleName Scope = 'AllUsers' Force = $true - RequiredVersion = $Using:Node.SqlServerModuleVersion - AllowPrerelease = $Using:Node.SqlServerModuleVersionIsPrerelease + RequiredVersion = $Using:Node.SMOModuleVersion + AllowPrerelease = $Using:Node.SMOModuleVersionIsPrerelease AllowClobber = $true # Needed to handle existens of module SQLPS. PassThru = $true } @@ -427,20 +450,23 @@ Configuration DSC_SqlSetup_InstallSqlServerModule_Config $installedModule = Install-Module @installModuleParameters | Where-Object -FilterScript { <# - Need to filter out the right module since it also installs - the dependent module dbatools.library. + Need to filter out the right module since if dependencies are + also installed they will also be in the returned array. #> - $_.Name -eq 'dbatools' + $_.Name -eq $using:Node.SMOModuleName } - Write-Verbose -Message ('Installed dbatools module version {0}' -f $installedModule.Version) + Write-Verbose -Message ('Installed {0} module version {1}' -f $using:Node.SMOModuleName, $installedModule.Version) Write-Verbose -Message ('Current set preferred module name (SMODefaultModuleName): {0}' -f ($env:SMODefaultModuleName | Out-String)) - Set-DbatoolsConfig -Name Import.EncryptionMessageCheck -Value $false -PassThru | - Register-DbatoolsConfig -Verbose + if ($using:Node.SMOModuleName -eq 'dbatools') + { + Set-DbatoolsConfig -Name Import.EncryptionMessageCheck -Value $false -PassThru | + Register-DbatoolsConfig -Verbose - Write-Verbose -Message 'Disabled dbatools setting Import.EncryptionMessageCheck' + Write-Verbose -Message 'Disabled dbatools setting Import.EncryptionMessageCheck' + } } TestScript = { @@ -451,43 +477,43 @@ Configuration DSC_SqlSetup_InstallSqlServerModule_Config #> $getScriptResult = & ([ScriptBlock]::Create($GetScript)) - if ($getScriptResult.Result -eq $Using:Node.SqlServerModuleVersion) + if ($getScriptResult.Result -eq $Using:Node.SMOModuleVersion) { - Write-Verbose -Message ('The node already contain the module dbatools with version {0}.' -f $Using:Node.SqlServerModuleVersion) + Write-Verbose -Message ('The node already contain the module {0} with version {1}.' -f $using:Node.SMOModuleName, $Using:Node.SMOModuleVersion) return $true } - Write-Verbose -Message ('The module dbatools with version {0} is not installed.' -f $Using:Node.SqlServerModuleVersion) + Write-Verbose -Message ('The module {0} with version {1} is not installed.' -f $using:Node.SMOModuleName, $Using:Node.SMOModuleVersion) return $false } GetScript = { $moduleVersion = $null - $sqlServerModule = $null + $smoModule = $null # Forcibly import the required modules that is required for using prerelease modules. Import-Module -Name 'PackageManagement' -MinimumVersion '1.4.8.1' -Force Import-Module -Name 'PowerShellGet' -MinimumVersion '2.2.5' -Force - $sqlServerModule = Get-Module -Name 'dbatools' -ListAvailable | + $smoModule = Get-Module -Name $using:Node.SMOModuleName -ListAvailable | Sort-Object -Property Version -Descending | Select-Object -First 1 - if ($sqlServerModule) + if ($smoModule) { - $moduleVersion = $sqlServerModule.Version.ToString() + $moduleVersion = $smoModule.Version.ToString() - if ($sqlServerModule.PrivateData.PSData.Keys -contains 'Prerelease') + if ($smoModule.PrivateData.PSData.Keys -contains 'Prerelease') { - if (-not [System.String]::IsNullOrEmpty($sqlServerModule.PrivateData.PSData.Prerelease)) + if (-not [System.String]::IsNullOrEmpty($smoModule.PrivateData.PSData.Prerelease)) { - $moduleVersion = '{0}-{1}' -f $moduleVersion, $sqlServerModule.PrivateData.PSData.Prerelease + $moduleVersion = '{0}-{1}' -f $moduleVersion, $smoModule.PrivateData.PSData.Prerelease } } - Write-Verbose -Message ('Found dbatools module v{0}.' -f $moduleVersion) -Verbose + Write-Verbose -Message ('Found {0} module v{1}.' -f $using:Node.SMOModuleName, $moduleVersion) -Verbose } return @{