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

SqlServerDsc: Unit tests run in PowerShell 7 #1524

Merged
merged 26 commits into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- A read only property `IsClustered` was added that can be used to determine
if the instance is clustered.
- Added the properties `NpEnabled` and `TcpEnabled` ([issue #1161](https://github.com/dsccommunity/SqlServerDsc/issues/1161)).
- SqlServerReplication
- Add integration tests ([issue #755](https://github.com/dsccommunity/SqlServerDsc/issues/755)
- SqlServerDsc.Common
- The helper function `Restart-SqlService` was improved to handle Failover
Clusters better. Now the SQL Server service will only be taken offline
Expand All @@ -41,6 +43,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Updated the CI pipeline to use latest version of the module ModuleBuilder.
- Changed to use the property `NuGetVersionV2` from GitVersion in the
CI pipeline.
- The unit tests now run on PowerShell 7 to optimize the total run time.
- SqlServerDsc.Common
- The helper function `Invoke-InstallationMediaCopy` was changed to
handle a breaking change in PowerShell 7 ([issue #1530](https://github.com/dsccommunity/SqlServerDsc/issues/1530)).
- SqlAlwaysOnService
- BREAKING CHANGE: The parameter `ServerName` is now non-mandatory and
defaults to `$env:COMPUTERNAME` ([issue #319](https://github.com/dsccommunity/SqlServerDsc/issues/319)).
Expand Down Expand Up @@ -161,6 +167,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- SqlServerPermission
- The parameter `ServerName` now throws when passing an empty string or
null value (part of [issue #319](https://github.com/dsccommunity/SqlServerDsc/issues/319)).
- SqlServerReplication
- Enhanced the exception handling so it shows the inner exception error
message that have the actual error that occurred.
- Corrected the examples.

## [13.5.0] - 2020-04-12

Expand Down
4 changes: 3 additions & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ stages:
inputs:
filePath: './build.ps1'
arguments: "-Tasks test -PesterScript 'tests/Unit'"
pwsh: false
pwsh: true
- task: PublishTestResults@2
displayName: 'Publish Test Results'
condition: succeededOrFailed()
Expand Down Expand Up @@ -158,6 +158,7 @@ stages:
'tests/Integration/DSC_SqlServerRole.Integration.Tests.ps1'
'tests/Integration/DSC_SqlRS.Integration.Tests.ps1'
'tests/Integration/DSC_SqlDatabaseUser.Integration.Tests.ps1'
'tests/Integration/DSC_SqlServerReplication.Integration.Tests.ps1'
# Group 4
'tests/Integration/DSC_SqlScript.Integration.Tests.ps1'
# Group 5
Expand Down Expand Up @@ -223,6 +224,7 @@ stages:
'tests/Integration/DSC_SqlServerRole.Integration.Tests.ps1'
'tests/Integration/DSC_SqlRS.Integration.Tests.ps1'
'tests/Integration/DSC_SqlDatabaseUser.Integration.Tests.ps1'
'tests/Integration/DSC_SqlServerReplication.Integration.Tests.ps1'
# Group 4
'tests/Integration/DSC_SqlScript.Integration.Tests.ps1'
# Group 5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ Import-Module -Name $script:resourceHelperModulePath

$script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US'

$dom = [AppDomain]::CreateDomain('SqlServerReplication')

function Get-TargetResource
{
[CmdletBinding()]
Expand Down Expand Up @@ -295,7 +293,6 @@ function Test-TargetResource
return $result
}

#region helper functions
function New-ServerConnection
{
[CmdletBinding()]
Expand Down Expand Up @@ -388,7 +385,17 @@ function New-DistributionPublisher
)

$rmo = Get-RmoAssembly -SqlMajorVersion $SqlMajorVersion
$distributorPublisher = New-object $rmo.GetType('Microsoft.SqlServer.Replication.DistributionPublisher') $PublisherName, $ServerConnection

try
{
$distributorPublisher = New-object $rmo.GetType('Microsoft.SqlServer.Replication.DistributionPublisher') $PublisherName, $ServerConnection
}
catch
{
$errorMessage = $script:localizedData.FailedInFunction -f 'New-DistributionPublisher'

New-InvalidOperationException -Message $errorMessage -ErrorRecord $_
}

return $distributorPublisher
}
Expand All @@ -415,7 +422,16 @@ function Install-RemoteDistributor
$script:localizedData.InstallRemoteDistributor -f $RemoteDistributor
)

$ReplicationServer.InstallDistributor($RemoteDistributor, $AdminLinkCredentials.Password)
try
{
$ReplicationServer.InstallDistributor($RemoteDistributor, $AdminLinkCredentials.Password)
}
catch
{
$errorMessage = $script:localizedData.FailedInFunction -f 'Install-RemoteDistributor'

New-InvalidOperationException -Message $errorMessage -ErrorRecord $_
}
}

function Install-LocalDistributor
Expand All @@ -440,7 +456,16 @@ function Install-LocalDistributor
$script:localizedData.InstallLocalDistributor
)

$ReplicationServer.InstallDistributor($AdminLinkCredentials.Password, $DistributionDB)
try
{
$ReplicationServer.InstallDistributor($AdminLinkCredentials.Password, $DistributionDB)
}
catch
{
$errorMessage = $script:localizedData.FailedInFunction -f 'Install-LocalDistributor'

New-InvalidOperationException -Message $errorMessage -ErrorRecord $_
}
}

function Uninstall-Distributor
Expand All @@ -461,7 +486,16 @@ function Uninstall-Distributor
$script:localizedData.UninstallDistributor
)

$ReplicationServer.UninstallDistributor($UninstallWithForce)
try
{
$ReplicationServer.UninstallDistributor($UninstallWithForce)
}
catch
{
$errorMessage = $script:localizedData.FailedInFunction -f 'Uninstall-Distributor'

New-InvalidOperationException -Message $errorMessage -ErrorRecord $_
}
}

function Register-DistributorPublisher
Expand Down Expand Up @@ -506,9 +540,48 @@ function Register-DistributorPublisher
$distributorPublisher.DistributionDatabase = $DistributionDBName
$distributorPublisher.WorkingDirectory = $WorkingDirectory
$distributorPublisher.PublisherSecurity.WindowsAuthentication = $UseTrustedConnection
$distributorPublisher.Create()

try
{
$distributorPublisher.Create()
}
catch
{
$errorMessage = $script:localizedData.FailedInFunction -f 'Register-DistributorPublisher'

New-InvalidOperationException -Message $errorMessage -ErrorRecord $_
}
}

<#
.SYNOPSIS
Returns a reference to the ConnectionInfo assembly.

.DESCRIPTION
Returns a reference to the ConnectionInfo assembly.

.PARAMETER SqlMajorVersion
Specifies the major version of the SQL Server instance, e.g. '14'.

.OUTPUTS
[System.Reflection.Assembly]

Returns a reference to the ConnectionInfo assembly.

.EXAMPLE
Get-ConnectionInfoAssembly -SqlMajorVersion '14'

.NOTES
This should normally work using Import-Module and New-Object instead of
using the method [System.Reflection.Assembly]::Load(). But due to a
missing assembly in the module SqlServer ('Microsoft.SqlServer.Rmo') we
cannot use this:

Import-Module SqlServer
$connectionInfo = New-Object -TypeName 'Microsoft.SqlServer.Management.Common.ServerConnection' -ArgumentList @('testclu01a\SQL2014')
# Missing assembly 'Microsoft.SqlServer.Rmo' in module SqlServer prevents this call from working.
$replication = New-Object -TypeName 'Microsoft.SqlServer.Replication.ReplicationServer' -ArgumentList @($connectionInfo)
#>
function Get-ConnectionInfoAssembly
{
[CmdletBinding()]
Expand All @@ -520,15 +593,53 @@ function Get-ConnectionInfoAssembly
$SqlMajorVersion
)

$connInfo = $dom.Load("Microsoft.SqlServer.ConnectionInfo, Version=$SqlMajorVersion.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91")
try
{
$connectionInfo = [System.Reflection.Assembly]::Load("Microsoft.SqlServer.ConnectionInfo, Version=$SqlMajorVersion.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91")

Write-Verbose -Message (
$script:localizedData.LoadAssembly -f $connInfo.FullName
)
Write-Verbose -Message (
$script:localizedData.LoadAssembly -f $connectionInfo.FullName
)
}
catch
{
$errorMessage = $script:localizedData.FailedInFunction -f 'Get-ConnectionInfoAssembly'

return $connInfo
New-InvalidOperationException -Message $errorMessage -ErrorRecord $_
}

return $connectionInfo
}

<#
.SYNOPSIS
Returns a reference to the RMO assembly.

.DESCRIPTION
Returns a reference to the RMO assembly.

.PARAMETER SqlMajorVersion
Specifies the major version of the SQL Server instance, e.g. '14'.

.OUTPUTS
[System.Reflection.Assembly]

Returns a reference to the RMO assembly.

.EXAMPLE
Get-RmoAssembly -SqlMajorVersion '14'

.NOTES
This should normally work using Import-Module and New-Object instead of
using the method [System.Reflection.Assembly]::Load(). But due to a
missing assembly in the module SqlServer ('Microsoft.SqlServer.Rmo') we
cannot use this:

Import-Module SqlServer
$connectionInfo = New-Object -TypeName 'Microsoft.SqlServer.Management.Common.ServerConnection' -ArgumentList @('testclu01a\SQL2014')
# Missing assembly 'Microsoft.SqlServer.Rmo' in module SqlServer prevents this call from working.
$replication = New-Object -TypeName 'Microsoft.SqlServer.Replication.ReplicationServer' -ArgumentList @($connectionInfo)
#>
function Get-RmoAssembly
{
[CmdletBinding()]
Expand All @@ -540,11 +651,20 @@ function Get-RmoAssembly
$SqlMajorVersion
)

$rmo = $dom.Load("Microsoft.SqlServer.Rmo, Version=$SqlMajorVersion.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91")
try
{
$rmo = [System.Reflection.Assembly]::Load("Microsoft.SqlServer.Rmo, Version=$SqlMajorVersion.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91")

Write-Verbose -Message (
$script:localizedData.LoadAssembly -f $rmo.FullName
)
Write-Verbose -Message (
$script:localizedData.LoadAssembly -f $rmo.FullName
)
}
catch
{
$errorMessage = $script:localizedData.FailedInFunction -f 'Get-RmoAssembly'

New-InvalidOperationException -Message $errorMessage -ErrorRecord $_
}

return $rmo
}
Expand All @@ -567,6 +687,7 @@ function Get-SqlServerMajorVersion
if (-not $sqlMajorVersion)
{
$errorMessage = $script:localizedData.FailedToDetectSqlVersion -f $InstanceName

New-InvalidResultException -Message $errorMessage
}

Expand All @@ -593,6 +714,5 @@ function Get-SqlLocalServerName
return "$($env:COMPUTERNAME)\$InstanceName"
}
}
#endregion

Export-ModuleMember -Function *-TargetResource
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ ConvertFrom-StringData @'
CreateDistributorPublisher = Creating the distributor publisher '{0}' on the instance '{1}'.
LoadAssembly = Loaded assembly: {0}
FailedToDetectSqlVersion = Unable to detect version for the SQL Server instance '{0}'.
FailedInFunction = The call to '{0}' failed.
'@
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ Configuration Example
SqlServerReplication 'distributor'
{
Ensure = 'Present'
InstanceName = 'MSSQLSERVER'
InstanceName = 'DISTRIBUTOR' # Or 'MSSQLSERVER' for default instance.
AdminLinkCredentials = $SqlAdministratorCredential
DistributorMode = 'Local'
DistributionDBName = 'MyDistribution'
WorkingDirectory = 'C:\Temp'

PsDscRunAsCredential = $SqlAdministratorCredential
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ Configuration Example
SqlServerReplication 'publisher'
{
Ensure = 'Present'
InstanceName = 'PUBLISHER'
InstanceName = 'PUBLISHER' # Or 'MSSQLSERVER' for default instance.
AdminLinkCredentials = $SqlAdministratorCredential
DistributorMode = 'Remote'
RemoteDistributor = 'distsqlsrv.company.local'
DistributionDBName = 'MyDistribution'
RemoteDistributor = 'distsqlsrv.company.local\DISTRIBUTOR'
WorkingDirectory = 'C:\Temp'

PsDscRunAsCredential = $SqlAdministratorCredential
Expand Down
9 changes: 7 additions & 2 deletions source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,17 @@ function Invoke-InstallationMediaCopy

Connect-UncPath -RemotePath $SourcePath -SourceCredential $SourceCredential

$SourcePath = $SourcePath.TrimEnd('/\')
<#
Create a destination folder so the media files aren't written
to the root of the Temp folder.
#>
$mediaDestinationFolder = Split-Path -Path $SourcePath -Leaf
if (-not $mediaDestinationFolder )
$serverName, $shareName, $leafs = ($SourcePath -replace '\\\\') -split '\\'
if ($leafs)
{
$mediaDestinationFolder = $leafs | Select-Object -Last 1
}
else
{
$mediaDestinationFolder = New-Guid | Select-Object -ExpandProperty Guid
}
Expand Down
Loading