Skip to content

Commit

Permalink
SqlServerDsc: Initial integration tests for commands (#2028)
Browse files Browse the repository at this point in the history
- SqlServerDsc
  - Initial integration tests for commands
  • Loading branch information
johlju authored May 11, 2024
1 parent c9de1bf commit a5c29fe
Show file tree
Hide file tree
Showing 26 changed files with 1,801 additions and 885 deletions.
32 changes: 32 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- SqlServerDsc
- Added build tasks to generate Wiki documentation for public commands.
- Initial integration tests for commands.
- SqlDatabaseMail
- Added the parameter `UseDefaultCredentials` to control use of the DatabaseEngine
service account for SMTP server authentication.
- New public commands
- `Save-SqlDscSqlServerMediaFile` - Downloads the content on the provided URL
and if it is an executable it will use the executable to download the
ISO image media.

### Fixed

Expand All @@ -35,6 +40,33 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- New method ToString() for making verbose output better.
- SqlAgDatabase
- Remove unused help file ([issue #1745](https://github.com/dsccommunity/SqlServerDsc/issues/1745)).
- `Install-SqlDscServer`
- No longer throws with duplicate parameter error if the parameter
`ErrorAction` is passed to the command.
- `Add-SqlDscNode`
- No longer throws with duplicate parameter error if the parameter
`ErrorAction` is passed to the command.
- `Complete-SqlDscFailoverCluster`
- No longer throws with duplicate parameter error if the parameter
`ErrorAction` is passed to the command.
- `Complete-SqlDscImage`
- No longer throws with duplicate parameter error if the parameter
`ErrorAction` is passed to the command.
- `Initialize-SqlDscRebuildDatabase`
- No longer throws with duplicate parameter error if the parameter
`ErrorAction` is passed to the command.
- `Remove-SqlDscNode`
- No longer throws with duplicate parameter error if the parameter
`ErrorAction` is passed to the command.
- `Repair-SqlDscServer`
- No longer throws with duplicate parameter error if the parameter
`ErrorAction` is passed to the command.
- `Uninstall-SqlDscServer`
- No longer throws with duplicate parameter error if the parameter
`ErrorAction` is passed to the command.
- Private functions
- `Invoke-SetupAction` no longer throws when secure strings is passed on
Windows PowerShell.

### Changed

Expand Down
10 changes: 9 additions & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,13 @@ stages:
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')
Expand All @@ -244,9 +251,10 @@ stages:
- powershell: |
./build.ps1 -Tasks test -CodeCoverageThreshold 0 -PesterTag $(TEST_CONFIGURATION) -PesterPath @(
# Run the integration tests in a specific group order.
# Group 0
'tests/Integration/Commands/Prerequisites.Integration.Tests.ps1'
# Group 1
'tests/Integration/Commands/Install-SqlDscServer.Integration.Tests.ps1'
# Group 2
#'tests/Integration/Commands/Install-SqlDscReportingServices.Integration.Tests.ps1'
)
name: test
Expand Down
4 changes: 2 additions & 2 deletions source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -569,9 +569,9 @@ function Connect-SQL
$sqlConnectionContext.Connect()

<#
The addition of the ConnetTimeout property to the ConnectionContext will force the
The addition of the ConnectTimeout property to the ConnectionContext will force the
Connect() method to block until successful. THe SMO object's Status property may not
report 'Online' immediately eventhough the Connect() was successful. The loop is to
report 'Online' immediately even though the Connect() was successful. The loop is to
ensure the SMO's Status property was been updated.
#>
$sleepInSeconds = 2
Expand Down
15 changes: 13 additions & 2 deletions source/Private/Invoke-SetupAction.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@
#>
function Invoke-SetupAction
{
# cSpell: ignore PBDMS Admini AZUREEXTENSION
# cSpell: ignore PBDMS Admini AZUREEXTENSION BSTR
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')]
[OutputType()]
param
Expand Down Expand Up @@ -1618,7 +1618,18 @@ function Invoke-SetupAction

{ $PSBoundParameters.$parameterName -is [System.Security.SecureString] }
{
$passwordClearText = $PSBoundParameters.$parameterName | ConvertFrom-SecureString -AsPlainText
$secureString = $PSBoundParameters.$parameterName

if ($PSVersionTable.PSVersion -ge '6.0')
{
$passwordClearText = $secureString | ConvertFrom-SecureString -AsPlainText
}
else
{
# Workaround to convert SecureString to plain text in Windows PowerShell.
$binaryStringPointer = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secureString)
$passwordClearText = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($binaryStringPointer)
}

$sensitiveValue += $passwordClearText

Expand Down
2 changes: 1 addition & 1 deletion source/Public/Add-SqlDscNode.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -252,5 +252,5 @@ function Add-SqlDscNode
$Force
)

Invoke-SetupAction -AddNode @PSBoundParameters -ErrorAction 'Stop'
Invoke-SetupAction -AddNode @PSBoundParameters
}
2 changes: 1 addition & 1 deletion source/Public/Complete-SqlDscFailoverCluster.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -306,5 +306,5 @@ function Complete-SqlDscFailoverCluster
$Force
)

Invoke-SetupAction -CompleteFailoverCluster @PSBoundParameters -ErrorAction 'Stop'
Invoke-SetupAction -CompleteFailoverCluster @PSBoundParameters
}
2 changes: 1 addition & 1 deletion source/Public/Complete-SqlDscImage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -375,5 +375,5 @@ function Complete-SqlDscImage
$Force
)

Invoke-SetupAction -CompleteImage @PSBoundParameters -ErrorAction 'Stop'
Invoke-SetupAction -CompleteImage @PSBoundParameters
}
2 changes: 1 addition & 1 deletion source/Public/Initialize-SqlDscRebuildDatabase.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,5 @@ function Initialize-SqlDscRebuildDatabase
$Force
)

Invoke-SetupAction -RebuildDatabase @PSBoundParameters -ErrorAction 'Stop'
Invoke-SetupAction -RebuildDatabase @PSBoundParameters
}
2 changes: 1 addition & 1 deletion source/Public/Install-SqlDscServer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1139,5 +1139,5 @@ function Install-SqlDscServer
$Force
)

Invoke-SetupAction @PSBoundParameters -ErrorAction 'Stop'
Invoke-SetupAction @PSBoundParameters
}
2 changes: 1 addition & 1 deletion source/Public/Remove-SqlDscNode.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,5 @@ function Remove-SqlDscNode
$Force
)

Invoke-SetupAction -RemoveNode @PSBoundParameters -ErrorAction 'Stop'
Invoke-SetupAction -RemoveNode @PSBoundParameters
}
2 changes: 1 addition & 1 deletion source/Public/Repair-SqlDscServer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,5 @@ function Repair-SqlDscServer
$Force
)

Invoke-SetupAction -Repair @PSBoundParameters -ErrorAction 'Stop'
Invoke-SetupAction -Repair @PSBoundParameters
}
228 changes: 228 additions & 0 deletions source/Public/Save-SqlDscSqlServerMediaFile.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
<#
.SYNOPSIS
Downloads SQL Server media from a provided URL and saves the downloaded
media file to the specified file path
.DESCRIPTION
The Save-SqlDscSqlServerMediaFile function downloads SQL Server media from a
provided URL and saves the downloaded media file to the specified file path.
If the URL ends with ".exe", it is treated as an executable that downloads
an ISO. If it doesn't, it is treated as a direct link to an ISO.
The function also prints the SHA1 hash of the downloaded file.
.PARAMETER Url
The URL of the SQL Server media to download.
.PARAMETER DestinationPath
The file path where the downloaded media file should be saved.
.PARAMETER FileName
The file name of the downloaded media file. Defaults to media.iso if not
provided.
.PARAMETER Language
The language parameter specifies the language of the downloaded iso. This
parameter is only used when the provided URL is an executable file. Defaults
to 'en-US' if not provided.
.PARAMETER Quiet
Disables verbose progress output during the download process.
.PARAMETER Force
Forces the download of the media file even if the file already exists at the
specified destination path.
.EXAMPLE
Save-SqlDscSqlServerMediaFile -Url 'https://download.microsoft.com/download/c/c/9/cc9c6797-383c-4b24-8920-dc057c1de9d3/SQL2022-SSEI-Dev.exe' -DestinationPath 'C:\path\to\destination'
This downloads the SQL Server 2022 media and saves it to the specified destination path.
.EXAMPLE
Save-SqlDscSqlServerMediaFile -Url 'https://download.microsoft.com/download/d/a/2/da259851-b941-459d-989c-54a18a5d44dd/SQL2019-SSEI-Dev.exe' -DestinationPath 'C:\path\to\destination'
This downloads the SQL Server 2019 media and saves it to the specified destination path.
.EXAMPLE
Save-SqlDscSqlServerMediaFile -Url 'https://download.microsoft.com/download/E/F/2/EF23C21D-7860-4F05-88CE-39AA114B014B/SQLServer2017-x64-ENU.iso' -DestinationPath 'C:\path\to\destination'
This downloads the SQL Server 2017 media and saves it to the specified destination path.
.EXAMPLE
Save-SqlDscSqlServerMediaFile -Url 'https://download.microsoft.com/download/9/0/7/907AD35F-9F9C-43A5-9789-52470555DB90/ENU/SQLServer2016SP1-FullSlipstream-x64-ENU.iso' -DestinationPath 'C:\path\to\destination'
This downloads the SQL Server 2016 media and saves it to the specified destination path.
#>
function Save-SqlDscSqlServerMediaFile
{
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')]
[OutputType([System.IO.FileInfo])]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$Url,

[Parameter(Mandatory = $true)]
[System.IO.FileInfo]
[ValidateScript({ Test-Path $_ -PathType 'Container' })]
$DestinationPath,

[Parameter()]
[System.String]
$FileName = 'media.iso',

[Parameter()]
# Supported by SQL Server version 2019 and 2022.
[ValidateSet('zh-CN', 'zh-TW', 'en-US', 'fr-FR', 'de-DE', 'it-IT', 'ja-JP', 'ko-KR', 'pt-BR', 'ru-RU', 'es-ES')]
[System.String]
$Language = 'en-US',

[Parameter()]
[System.Management.Automation.SwitchParameter]
$Quiet,

[Parameter()]
[System.Management.Automation.SwitchParameter]
$Force
)

if ($Force.IsPresent -and -not $Confirm)
{
$ConfirmPreference = 'None'
}

if ((Get-Item -Path "$DestinationPath/*.iso" -Force).Count -gt 0)
{
$auditAlreadyPresentMessage = $script:localizedData.SqlServerMediaFile_Save_InvalidDestinationFolder

$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
$auditAlreadyPresentMessage,
'SSDSSM0001', # cspell: disable-line
[System.Management.Automation.ErrorCategory]::InvalidOperation,
$DestinationPath
)
)
}

$destinationFilePath = Join-Path -Path $DestinationPath -ChildPath $FileName

if ((Test-Path -Path $destinationFilePath))
{
$verboseDescriptionMessage = $script:localizedData.SqlServerMediaFile_Save_ShouldProcessVerboseDescription -f $destinationFilePath
$verboseWarningMessage = $script:localizedData.qlServerMedia_Save_ShouldProcessVerboseWarning -f $destinationFilePath
$captionMessage = $script:localizedData.qlServerMedia_Save_ShouldProcessCaption

if ($PSCmdlet.ShouldProcess($verboseDescriptionMessage, $verboseWarningMessage, $captionMessage))
{
Remove-Item -Path $destinationFilePath -Force
}
else
{
return
}
}

Write-Verbose -Message ($script:localizedData.SqlServerMediaFile_Save_ShouldProcessVerboseDescription -f $destinationFilePath)

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$isExecutable = $false

if ($Url -match '\.exe$')
{
$isExecutable = $true

# Change the file extension of the destination file path to .exe
$destinationExecutableFilePath = [System.IO.Path]::ChangeExtension($destinationFilePath, 'exe')

$downloadedFilePath = $destinationExecutableFilePath
}
else
{
$downloadedFilePath = $destinationFilePath
}

if ($Quiet.IsPresent)
{
<#
By switching to 'SilentlyContinue' it removes the progress bar of
Invoke-WebRequest and should also theoretically increase the download
speed.
#>
$previousProgressPreference = $ProgressPreference
$ProgressPreference = 'SilentlyContinue'
}

# Download the URL content.
Invoke-WebRequest -Uri $Url -OutFile $downloadedFilePath | Out-Null

if ($Quiet.IsPresent)
{
# Revert the progress preference back to the previous value.
$ProgressPreference = $previousProgressPreference
}

if ($isExecutable)
{
Write-Verbose -Message $script:localizedData.SqlServerMediaFile_Save_IsExecutable

$executableArguments = @(
'/Quiet'
)

if ($VerbosePreference -eq 'SilentlyContinue')
{
$executableArguments += @(
'/HideProgressBar'
)
}
else
{
$executableArguments += @(
'/Verbose'
)
}

$executableArguments += @(
'/Action=Download',
"/Language=$Language",
'/MediaType=ISO',
"/MediaPath=$destinationPath"
)

$startProcessArgumentList = $executableArguments -join ' '

# Download ISO media using the downloaded executable.
Start-Process -FilePath $destinationExecutableFilePath -ArgumentList $startProcessArgumentList -Wait

Write-Verbose -Message $script:localizedData.SqlServerMediaFile_Save_RemovingExecutable

# Remove the downloaded executable.
Remove-Item -Path $destinationExecutableFilePath -Force

# Get all the iso files in the destination path and if there are more than one throw an error.
$isoFile = Get-Item -Path "$DestinationPath/*.iso" -Force

if ($isoFile.Count -gt 1)
{
$writeErrorParameters = @{
Message = $script:localizedData.SqlServerMediaFile_Save_MultipleFilesFoundAfterDownload
Category = 'InvalidOperation'
ErrorId = 'SSDSSM0002' # CSpell: disable-line
TargetObject = $ServiceType
}

Write-Error @writeErrorParameters
}

Write-Verbose -Message ($script:localizedData.SqlServerMediaFile_Save_RenamingFile -f $isoFile.Name, $FileName)

# Rename the iso file in the destination path.
Rename-Item -Path $isoFile.FullName -NewName $FileName -Force
}

return (Get-Item -Path $destinationFilePath)
}
Loading

0 comments on commit a5c29fe

Please sign in to comment.