diff --git a/CHANGELOG.md b/CHANGELOG.md index 9124d8b4c..6289ce7b3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -75,6 +75,10 @@ - Added en-US localization ([issue #602](https://github.com/PowerShell/SqlServerDsc/issues/602)). - Removed ShouldProcess for the code, since it has no purpose in a DSC resource ([issue #242](https://github.com/PowerShell/SqlServerDsc/issues/242)). +- Changes to SqlServerReplication + - Added en-US localization ([issue #620](https://github.com/PowerShell/SqlServerDsc/issues/620)). + - Refactored Get-TargetResource slightly so it provide better verbose + messages. ## 12.4.0.0 diff --git a/DSCResources/MSFT_SqlServerReplication/MSFT_SqlServerReplication.psm1 b/DSCResources/MSFT_SqlServerReplication/MSFT_SqlServerReplication.psm1 index a4be6d8a9..548a266af 100644 --- a/DSCResources/MSFT_SqlServerReplication/MSFT_SqlServerReplication.psm1 +++ b/DSCResources/MSFT_SqlServerReplication/MSFT_SqlServerReplication.psm1 @@ -1,10 +1,22 @@ +$script:resourceModulePath = Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent +$script:modulesFolderPath = Join-Path -Path $script:resourceModulePath -ChildPath 'Modules' + +$script:localizationModulePath = Join-Path -Path $script:modulesFolderPath -ChildPath 'DscResource.LocalizationHelper' +Import-Module -Name (Join-Path -Path $script:localizationModulePath -ChildPath 'DscResource.LocalizationHelper.psm1') + +$script:resourceHelperModulePath = Join-Path -Path $script:modulesFolderPath -ChildPath 'DscResource.Common' +Import-Module -Name (Join-Path -Path $script:resourceHelperModulePath -ChildPath 'DscResource.Common.psm1') + +$script:localizedData = Get-LocalizedData -ResourceName 'MSFT_SqlServerReplication' + $dom = [AppDomain]::CreateDomain('SqlServerReplication') function Get-TargetResource { [CmdletBinding()] [OutputType([System.Collections.Hashtable])] - param( + param + ( [Parameter(Mandatory = $true)] [System.String] $InstanceName, @@ -44,7 +56,9 @@ function Get-TargetResource $UninstallWithForce = $true ) - $Ensure = 'Absent' + Write-Verbose -Message ( + $script:localizedData.GetCurrentState -f $InstanceName + ) $sqlMajorVersion = Get-SqlServerMajorVersion -InstanceName $InstanceName $localSqlName = Get-SqlLocalServerName -InstanceName $InstanceName @@ -52,31 +66,45 @@ function Get-TargetResource $localServerConnection = New-ServerConnection -SqlMajorVersion $sqlMajorVersion -SqlServerName $localSqlName $localReplicationServer = New-ReplicationServer -SqlMajorVersion $sqlMajorVersion -ServerConnection $localServerConnection + $currentEnsure = 'Present' + if ($localReplicationServer.IsDistributor -eq $true) { - $Ensure = 'Present' - $DistributorMode = 'Local' + $currentDistributorMode = 'Local' } elseif ($localReplicationServer.IsPublisher -eq $true) { - $Ensure = 'Present' - $DistributorMode = 'Remote' + $currentDistributorMode = 'Remote' + } + else + { + $currentEnsure = 'Absent' } - if ($Ensure -eq 'Present') + if ($currentEnsure -eq 'Present') + { + Write-Verbose -Message ( + $script:localizedData.DistributorMode -f $DistributorMode, $InstanceName + ) + + $currentDistributionDBName = $localReplicationServer.DistributionDatabase + $currentRemoteDistributor = $localReplicationServer.DistributionServer + $currentWorkingDirectory = $localReplicationServer.WorkingDirectory + } + else { - $DistributionDBName = $localReplicationServer.DistributionDatabase - $RemoteDistributor = $localReplicationServer.DistributionServer - $WorkingDirectory = $localReplicationServer.WorkingDirectory + Write-Verbose -Message ( + $script:localizedData.NoDistributorMode -f $InstanceName + ) } $returnValue = @{ InstanceName = $InstanceName - Ensure = $Ensure - DistributorMode = $DistributorMode - DistributionDBName = $DistributionDBName - RemoteDistributor = $RemoteDistributor - WorkingDirectory = $WorkingDirectory + Ensure = $currentEnsure + DistributorMode = $currentDistributorMode + DistributionDBName = $currentDistributionDBName + RemoteDistributor = $currentRemoteDistributor + WorkingDirectory = $currentWorkingDirectory } return $returnValue @@ -85,7 +113,8 @@ function Get-TargetResource function Set-TargetResource { [CmdletBinding()] - param( + param + ( [Parameter(Mandatory = $true)] [System.String] $InstanceName, @@ -127,7 +156,8 @@ function Set-TargetResource if (($DistributorMode -eq 'Remote') -and (-not $RemoteDistributor)) { - throw "RemoteDistributor parameter cannot be empty when DistributorMode = 'Remote'!" + $errorMessage = $script:localizedData.NoRemoteDistributor + New-InvalidArgumentException -ArgumentName 'RemoteDistributor' -Message $errorMessage } $sqlMajorVersion = Get-SqlServerMajorVersion -InstanceName $InstanceName @@ -140,7 +170,9 @@ function Set-TargetResource { if ($DistributorMode -eq 'Local' -and $localReplicationServer.IsDistributor -eq $false) { - Write-Verbose "Local distribution will be configured ..." + Write-Verbose -Message ( + $script:localizedData.ConfigureLocalDistributor + ) $distributionDB = New-DistributionDatabase ` -SqlMajorVersion $sqlMajorVersion ` @@ -163,7 +195,9 @@ function Set-TargetResource if ($DistributorMode -eq 'Remote' -and $localReplicationServer.IsPublisher -eq $false) { - Write-Verbose "Remote distribution will be configured ..." + Write-Verbose -Message ( + $script:localizedData.ConfigureRemoteDistributor + ) $remoteConnection = New-ServerConnection -SqlMajorVersion $sqlMajorVersion -SqlServerName $RemoteDistributor @@ -185,12 +219,17 @@ function Set-TargetResource { if ($localReplicationServer.IsDistributor -eq $true -or $localReplicationServer.IsPublisher -eq $true) { - Write-Verbose "Distribution will be removed ..." + Write-Verbose -Message ( + $script:localizedData.RemoveDistributor + ) + Uninstall-Distributor -ReplicationServer $localReplicationServer -UninstallWithForce $UninstallWithForce } else { - Write-Verbose "Distribution is not configured on this instance." + Write-Verbose -Message ( + $script:localizedData.NoDistributorMode -f $InstanceName + ) } } } @@ -199,7 +238,8 @@ function Test-TargetResource { [CmdletBinding()] [OutputType([System.Boolean])] - param( + param + ( [Parameter(Mandatory = $true)] [System.String] $InstanceName, @@ -239,6 +279,10 @@ function Test-TargetResource $UninstallWithForce = $true ) + Write-Verbose -Message ( + $script:localizedData.TestingConfiguration + ) + $result = $false $state = Get-TargetResource @PSBoundParameters @@ -259,7 +303,8 @@ function New-ServerConnection { [CmdletBinding()] [OutputType([System.Object])] - param( + param + ( [Parameter(Mandatory = $true)] [System.String] $SqlMajorVersion, @@ -279,7 +324,8 @@ function New-ReplicationServer { [CmdletBinding()] [OutputType([System.Object])] - param( + param + ( [Parameter(Mandatory = $true)] [System.String] $SqlMajorVersion, @@ -299,7 +345,8 @@ function New-DistributionDatabase { [CmdletBinding()] [OutputType([System.Object])] - param( + param + ( [Parameter(Mandatory = $true)] [System.String] $SqlMajorVersion, @@ -314,7 +361,11 @@ function New-DistributionDatabase ) $rmo = Get-RmoAssembly -SqlMajorVersion $SqlMajorVersion - Write-Verbose "Creating DistributionDatabase object $DistributionDBName" + + Write-Verbose -Message ( + $script:localizedData.CreateDistributionDatabase -f $DistributionDBName + ) + $distributionDB = New-Object $rmo.GetType('Microsoft.SqlServer.Replication.DistributionDatabase') $DistributionDBName, $ServerConnection return $distributionDB @@ -324,7 +375,8 @@ function New-DistributionPublisher { [CmdletBinding()] [OutputType([System.Object])] - param( + param + ( [Parameter(Mandatory = $true)] [System.String] $SqlMajorVersion, @@ -347,7 +399,8 @@ function New-DistributionPublisher function Install-RemoteDistributor { [CmdletBinding()] - param( + param + ( [Parameter(Mandatory = $true)] [System.Object] $ReplicationServer, @@ -361,14 +414,18 @@ function Install-RemoteDistributor $AdminLinkCredentials ) - Write-Verbose "Calling InstallDistributor with RemoteDistributor = $RemoteDistributor" + Write-Verbose -Message ( + $script:localizedData.InstallRemoteDistributor -f $RemoteDistributor + ) + $ReplicationServer.InstallDistributor($RemoteDistributor, $AdminLinkCredentials.Password) } function Install-LocalDistributor { [CmdletBinding()] - param( + param + ( [Parameter(Mandatory = $true)] [System.Object] $ReplicationServer, @@ -382,14 +439,18 @@ function Install-LocalDistributor $DistributionDB ) - Write-Verbose "Calling method InstallDistributor() with DistributionDB" + Write-Verbose -Message ( + $script:localizedData.InstallLocalDistributor + ) + $ReplicationServer.InstallDistributor($AdminLinkCredentials.Password, $DistributionDB) } function Uninstall-Distributor { [CmdletBinding()] - param( + param + ( [Parameter(Mandatory = $true)] [System.Object] $ReplicationServer, @@ -398,14 +459,19 @@ function Uninstall-Distributor [System.Boolean] $UninstallWithForce ) - Write-Verbose 'Calling method UninstallDistributor() on ReplicationServer object' + + Write-Verbose -Message ( + $script:localizedData.UninstallDistributor + ) + $ReplicationServer.UninstallDistributor($UninstallWithForce) } function Register-DistributorPublisher { [CmdletBinding()] - param( + param + ( [Parameter(Mandatory = $true)] [System.String] $SqlMajorVersion, @@ -431,7 +497,9 @@ function Register-DistributorPublisher $UseTrustedConnection ) - Write-Verbose "Creating DistributorPublisher $PublisherName on $($ServerConnection.ServerInstance)" + Write-Verbose -Message ( + $script:localizedData.CreateDistributorPublisher -f $PublisherName, $ServerConnection.ServerInstance + ) $distributorPublisher = New-DistributionPublisher ` -SqlMajorVersion $SqlMajorVersion ` @@ -448,14 +516,18 @@ function Get-ConnectionInfoAssembly { [CmdletBinding()] [OutputType([System.Reflection.Assembly])] - param( + param + ( [Parameter(Mandatory = $true)] [System.String] $SqlMajorVersion ) $connInfo = $dom.Load("Microsoft.SqlServer.ConnectionInfo, Version=$SqlMajorVersion.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91") - Write-Verbose "Loaded assembly: $($connInfo.FullName)" + + Write-Verbose -Message ( + $script:localizedData.LoadAssembly -f $connInfo.FullName + ) return $connInfo } @@ -464,14 +536,18 @@ function Get-RmoAssembly { [CmdletBinding()] [OutputType([System.Reflection.Assembly])] - param( + param + ( [Parameter(Mandatory = $true)] [System.String] $SqlMajorVersion ) $rmo = $dom.Load("Microsoft.SqlServer.Rmo, Version=$SqlMajorVersion.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91") - Write-Verbose "Loaded assembly: $($rmo.FullName)" + + Write-Verbose -Message ( + $script:localizedData.LoadAssembly -f $rmo.FullName + ) return $rmo } @@ -480,7 +556,8 @@ function Get-SqlServerMajorVersion { [CmdletBinding()] [OutputType([System.String])] - param( + param + ( [Parameter(Mandatory = $true)] [System.String] $InstanceName @@ -488,11 +565,14 @@ function Get-SqlServerMajorVersion $instanceId = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL").$InstanceName $sqlVersion = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$instanceId\Setup").Version + $sqlMajorVersion = $sqlVersion.Split(".")[0] if (-not $sqlMajorVersion) { - throw "Unable to detect version for sql server instance: $InstanceName!" + $errorMessage = $script:localizedData.FailedToDetectSqlVersion -f $InstanceName + New-InvalidResultException -Message $errorMessage } + return $sqlMajorVersion } @@ -500,13 +580,14 @@ function Get-SqlLocalServerName { [CmdletBinding()] [OutputType([System.String])] - param( + param + ( [Parameter(Mandatory = $true)] [System.String] $InstanceName ) - if ($InstanceName -eq "MSSQLSERVER") + if ($InstanceName -eq 'MSSQLSERVER') { return $env:COMPUTERNAME } diff --git a/DSCResources/MSFT_SqlServerReplication/en-US/MSFT_SqlServerReplication.strings.psd1 b/DSCResources/MSFT_SqlServerReplication/en-US/MSFT_SqlServerReplication.strings.psd1 new file mode 100644 index 000000000..81a4ec333 --- /dev/null +++ b/DSCResources/MSFT_SqlServerReplication/en-US/MSFT_SqlServerReplication.strings.psd1 @@ -0,0 +1,17 @@ +ConvertFrom-StringData @' + GetCurrentState = Get the current state of the server replication configuration for the instance '{0}'. + DistributorMode = The distributor mode is currently '{0}' for the instance '{1}'. + NoDistributorMode = There are currently no distributor mode set for the instance '{0}'. + NoRemoteDistributor = The parameter RemoteDistributor cannot be empty when DistributorMode is set to 'Remote'. + ConfigureLocalDistributor = The local distribution will be configured. + ConfigureRemoteDistributor = The remote distribution will be configured. + RemoveDistributor = The distribution will be removed. + TestingConfiguration = Determines if the distribution is configured in desired state. + CreateDistributionDatabase = Creating the distribution database object '{0}'. + InstallRemoteDistributor = Installing the remote distributor '{0}'. + InstallLocalDistributor = Installing the local distributor. + UninstallDistributor = Uninstalling the distributor. + 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}'. +'@ diff --git a/Tests/Unit/MSFT_SqlServerReplication.Tests.ps1 b/Tests/Unit/MSFT_SqlServerReplication.Tests.ps1 index cc7080d2f..2237aa797 100644 --- a/Tests/Unit/MSFT_SqlServerReplication.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlServerReplication.Tests.ps1 @@ -49,7 +49,7 @@ try -MockWith { return [pscustomobject]@{ MSSQLSERVER = 'MSSQL12.MSSQLSERVER'} } ` -ParameterFilter { $Path -eq 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL' } - It 'Should return corrent major version for default instance' { + It 'Should return correct major version for default instance' { Mock -CommandName Get-ItemProperty ` -MockWith { return [pscustomobject]@{ Version = '12.1.4100.1' } } ` @@ -64,7 +64,7 @@ try -MockWith { return [pscustomobject]@{ Version = '' } }` -ParameterFilter { $Path -eq 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL12.MSSQLSERVER\Setup' } - { Get-SqlServerMajorVersion -InstanceName 'MSSQLSERVER' } | Should -Throw "instance: MSSQLSERVER!" + { Get-SqlServerMajorVersion -InstanceName 'MSSQLSERVER' } | Should -Throw ($script:localizedData.FailedToDetectSqlVersion -f 'MSSQLSERVER') } } @@ -85,87 +85,89 @@ try $credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList @('AdminLink', $secpasswd) Describe 'The system is not in the desired state given Local distribution mode' { - - $testParameters = @{ - InstanceName = 'MSSQLSERVER' - AdminLinkCredentials = $credentials - DistributorMode = 'Local' - WorkingDirectory = 'C:\temp' - Ensure = 'Present' - } - - Mock -CommandName Get-SqlServerMajorVersion -MockWith { return '99' } - Mock -CommandName Get-SqlLocalServerName -MockWith { return 'SERVERNAME' } - Mock -CommandName New-ServerConnection -MockWith { - return [pscustomobject]@{ - ServerInstance = $SqlServerName - } - } - Mock -CommandName New-ReplicationServer -MockWith { - return [pscustomobject]@{ - IsDistributor = $false - IsPublisher = $false - DistributionDatabase = '' - DistributionServer = 'SERVERNAME' - WorkingDirectory = '' - } - } - Mock -CommandName New-DistributionDatabase -MockWith { return [pscustomobject]@{} } - Mock -CommandName Install-LocalDistributor -MockWith { } - Mock -CommandName Install-RemoteDistributor -MockWith { } - Mock -CommandName Register-DistributorPublisher -MockWith { } - Mock -CommandName Uninstall-Distributor -MockWith {} - - Context 'Get methot' { - $result = Get-TargetResource @testParameters - It 'Get method calls Get-SqlServerMajorVersion with InstanceName = MSSQLSERVER' { - Assert-MockCalled -CommandName Get-SqlServerMajorVersion -Times 1 ` - -ParameterFilter { $InstanceName -eq 'MSSQLSERVER' } - } - It 'Get method calls Get-SqlLocalServerName with $InstanceName = MSSQLSERVER' { - Assert-MockCalled -CommandName Get-SqlLocalServerName -Times 1 ` - -ParameterFilter { $InstanceName -eq 'MSSQLSERVER' } - } - It 'Get method calls New-ServerConnection with $SqlServerName = SERVERNAME' { - Assert-MockCalled -CommandName New-ServerConnection -Times 1 ` - -ParameterFilter { $SqlServerName -eq 'SERVERNAME' } - } - It 'Get method calls New-ReplicationServer with $ServerConnection.ServerInstance = SERVERNAME' { - Assert-MockCalled -CommandName New-ReplicationServer -Times 1 ` - -ParameterFilter { $ServerConnection.ServerInstance -eq 'SERVERNAME' } - } - It 'Get method doesnt call New-DistributionDatabase' { - Assert-MockCalled -CommandName New-DistributionDatabase -Times 0 - } - It 'Get method doesnt call Install-LocalDistributor' { - Assert-MockCalled -CommandName Install-LocalDistributor -Times 0 - } - It 'Get method doesnt call Install-RemoteDistributor' { - Assert-MockCalled -CommandName Install-RemoteDistributor -Times 0 - } - It 'Ger method doesnt call Register-DistributorPublisher' { - Assert-MockCalled -CommandName Register-DistributorPublisher -Times 0 - } - It 'Ger method doesnt call Uninstall-Distributor' { - Assert-MockCalled -CommandName Uninstall-Distributor -Times 0 - } - It 'Get method returns Ensure = Absent' { - $result.Ensure | Should -Be 'Absent' - } - It "Get method returns InstanceName = $($testParameters.InstanceName)" { - $result.InstanceName | Should -Be $testParameters.InstanceName - } - It "Get method returns DistributorMode = $($testParameters.DistributorMode)" { - $result.DistributorMode | Should -Be $testParameters.DistributorMode - } - It 'Get method returns DistributionDBName = distribution' { - $result.DistributionDBName | Should -Be 'distribution' - } - It 'Get method returns RemoteDistributor is empty' { - $result.RemoteDistributor | Should -Be '' - } - It 'Get method returns WorkingDirectory = C:\temp' { - $result.WorkingDirectory | Should -Be 'C:\temp' + BeforeAll { + $testParameters = @{ + InstanceName = 'MSSQLSERVER' + AdminLinkCredentials = $credentials + DistributorMode = 'Local' + WorkingDirectory = 'C:\Temp' + } + + Mock -CommandName Get-SqlServerMajorVersion -MockWith { return '99' } + Mock -CommandName Get-SqlLocalServerName -MockWith { return 'SERVERNAME' } + Mock -CommandName New-ServerConnection -MockWith { + return [pscustomobject]@{ + ServerInstance = $SqlServerName + } + } + Mock -CommandName New-ReplicationServer -MockWith { + return [pscustomobject]@{ + IsDistributor = $false + IsPublisher = $false + DistributionDatabase = '' + DistributionServer = 'SERVERNAME' + WorkingDirectory = '' + } + } + Mock -CommandName New-DistributionDatabase -MockWith { return [pscustomobject]@{} } + Mock -CommandName Install-LocalDistributor -MockWith { } + Mock -CommandName Install-RemoteDistributor -MockWith { } + Mock -CommandName Register-DistributorPublisher -MockWith { } + Mock -CommandName Uninstall-Distributor -MockWith {} + } + + Context 'The system is not in the desired state' { + Context 'Get method' { + $result = Get-TargetResource @testParameters + It 'Get method calls Get-SqlServerMajorVersion with InstanceName = MSSQLSERVER' { + Assert-MockCalled -CommandName Get-SqlServerMajorVersion -Times 1 ` + -ParameterFilter { $InstanceName -eq 'MSSQLSERVER' } + } + It 'Get method calls Get-SqlLocalServerName with $InstanceName = MSSQLSERVER' { + Assert-MockCalled -CommandName Get-SqlLocalServerName -Times 1 ` + -ParameterFilter { $InstanceName -eq 'MSSQLSERVER' } + } + It 'Get method calls New-ServerConnection with $SqlServerName = SERVERNAME' { + Assert-MockCalled -CommandName New-ServerConnection -Times 1 ` + -ParameterFilter { $SqlServerName -eq 'SERVERNAME' } + } + It 'Get method calls New-ReplicationServer with $ServerConnection.ServerInstance = SERVERNAME' { + Assert-MockCalled -CommandName New-ReplicationServer -Times 1 ` + -ParameterFilter { $ServerConnection.ServerInstance -eq 'SERVERNAME' } + } + It 'Get method does not call New-DistributionDatabase' { + Assert-MockCalled -CommandName New-DistributionDatabase -Times 0 + } + It 'Get method does not call Install-LocalDistributor' { + Assert-MockCalled -CommandName Install-LocalDistributor -Times 0 + } + It 'Get method does not call Install-RemoteDistributor' { + Assert-MockCalled -CommandName Install-RemoteDistributor -Times 0 + } + It 'Ger method does not call Register-DistributorPublisher' { + Assert-MockCalled -CommandName Register-DistributorPublisher -Times 0 + } + It 'Ger method does not call Uninstall-Distributor' { + Assert-MockCalled -CommandName Uninstall-Distributor -Times 0 + } + It 'Get method returns Ensure = Absent' { + $result.Ensure | Should -Be 'Absent' + } + It "Get method returns InstanceName = $($testParameters.InstanceName)" { + $result.InstanceName | Should -Be $testParameters.InstanceName + } + It 'Get method returns DistributorMode as $null' { + $result.DistributorMode | Should -BeNullOrEmpty + } + It 'Get method returns DistributionDBName as $null' { + $result.DistributionDBName | Should -BeNullOrEmpty + } + It 'Get method returns RemoteDistributor as $null' { + $result.RemoteDistributor | Should -BeNullOrEmpty + } + It 'Get method returns WorkingDirectory as $null' { + $result.WorkingDirectory | Should -BeNullOrEmpty + } } } @@ -205,10 +207,10 @@ try Assert-MockCalled -CommandName Register-DistributorPublisher -Times 1 ` -ParameterFilter { $PublisherName -eq 'SERVERNAME' } } - It 'Set method doesnt call Install-RemoteDistributor' { + It 'Set method does not call Install-RemoteDistributor' { Assert-MockCalled -CommandName Install-RemoteDistributor -Times 0 } - It 'Set method doesnt call Uninstall-Distributor' { + It 'Set method does not call Uninstall-Distributor' { Assert-MockCalled -CommandName Uninstall-Distributor -Times 0 } } @@ -247,56 +249,58 @@ try Mock -CommandName Register-DistributorPublisher -MockWith { } Mock -CommandName Uninstall-Distributor -MockWith {} - Context 'Get methot' { - $result = Get-TargetResource @testParameters - It 'Get method calls Get-SqlServerMajorVersion with $InstanceName = INSTANCENAME' { - Assert-MockCalled -CommandName Get-SqlServerMajorVersion -Times 1 ` - -ParameterFilter { $InstanceName -eq 'INSTANCENAME' } - } - It 'Get method calls Get-SqlLocalServerName with $InstanceName = INSTANCENAME' { - Assert-MockCalled -CommandName Get-SqlLocalServerName -Times 1 ` - -ParameterFilter { $InstanceName -eq 'INSTANCENAME' } - } - It 'Get method calls New-ServerConnection with $SqlServerName = SERVERNAME\INSTANCENAME' { - Assert-MockCalled -CommandName New-ServerConnection -Times 1 ` - -ParameterFilter { $SqlServerName -eq 'SERVERNAME\INSTANCENAME' } - } - It 'Get method calls New-ReplicationServer with $ServerConnection.ServerInstance = SERVERNAME\INSTANCENAME' { - Assert-MockCalled -CommandName New-ReplicationServer -Times 1 ` - -ParameterFilter { $ServerConnection.ServerInstance -eq 'SERVERNAME\INSTANCENAME' } - } - It 'Get method doesnt call New-DistributionDatabase' { - Assert-MockCalled -CommandName New-DistributionDatabase -Times 0 - } - It 'Get method doesnt call Install-LocalDistributor' { - Assert-MockCalled -CommandName Install-LocalDistributor -Times 0 - } - It 'Get method doesnt call Install-RemoteDistributor' { - Assert-MockCalled -CommandName Install-RemoteDistributor -Times 0 - } - It 'Ger method doesnt call Register-DistributorPublisher' { - Assert-MockCalled -CommandName Register-DistributorPublisher -Times 0 - } - It 'Ger method doesnt call Uninstall-Distributor' { - Assert-MockCalled -CommandName Uninstall-Distributor -Times 0 - } - It 'Get method returns Ensure = Absent' { - $result.Ensure | Should -Be 'Absent' - } - It "Get method returns InstanceName = $($testParameters.InstanceName)" { - $result.InstanceName | Should -Be $testParameters.InstanceName - } - It "Get method returns DistributorMode = $($testParameters.DistributorMode)" { - $result.DistributorMode | Should -Be $testParameters.DistributorMode - } - It 'Get method returns DistributionDBName = distribution' { - $result.DistributionDBName | Should -Be 'distribution' - } - It "Get method returns RemoteDistributor = $($testParameters.RemoteDistributor)" { - $result.RemoteDistributor | Should -Be $testParameters.RemoteDistributor - } - It 'Get method returns WorkingDirectory = C:\temp' { - $result.WorkingDirectory | Should -Be 'C:\temp' + Context 'The system is not in the desired state' { + Context 'Get method' { + $result = Get-TargetResource @testParameters + It 'Get method calls Get-SqlServerMajorVersion with $InstanceName = INSTANCENAME' { + Assert-MockCalled -CommandName Get-SqlServerMajorVersion -Times 1 ` + -ParameterFilter { $InstanceName -eq 'INSTANCENAME' } + } + It 'Get method calls Get-SqlLocalServerName with $InstanceName = INSTANCENAME' { + Assert-MockCalled -CommandName Get-SqlLocalServerName -Times 1 ` + -ParameterFilter { $InstanceName -eq 'INSTANCENAME' } + } + It 'Get method calls New-ServerConnection with $SqlServerName = SERVERNAME\INSTANCENAME' { + Assert-MockCalled -CommandName New-ServerConnection -Times 1 ` + -ParameterFilter { $SqlServerName -eq 'SERVERNAME\INSTANCENAME' } + } + It 'Get method calls New-ReplicationServer with $ServerConnection.ServerInstance = SERVERNAME\INSTANCENAME' { + Assert-MockCalled -CommandName New-ReplicationServer -Times 1 ` + -ParameterFilter { $ServerConnection.ServerInstance -eq 'SERVERNAME\INSTANCENAME' } + } + It 'Get method does not call New-DistributionDatabase' { + Assert-MockCalled -CommandName New-DistributionDatabase -Times 0 + } + It 'Get method does not call Install-LocalDistributor' { + Assert-MockCalled -CommandName Install-LocalDistributor -Times 0 + } + It 'Get method does not call Install-RemoteDistributor' { + Assert-MockCalled -CommandName Install-RemoteDistributor -Times 0 + } + It 'Ger method does not call Register-DistributorPublisher' { + Assert-MockCalled -CommandName Register-DistributorPublisher -Times 0 + } + It 'Ger method does not call Uninstall-Distributor' { + Assert-MockCalled -CommandName Uninstall-Distributor -Times 0 + } + It 'Get method returns Ensure = Absent' { + $result.Ensure | Should -Be 'Absent' + } + It "Get method returns InstanceName = $($testParameters.InstanceName)" { + $result.InstanceName | Should -Be $testParameters.InstanceName + } + It 'Get method returns DistributorMode as $null' { + $result.DistributorMode | Should -BeNullOrEmpty + } + It 'Get method returns DistributionDBName as $null' { + $result.DistributionDBName | Should -BeNullOrEmpty + } + It 'Get method returns RemoteDistributor as $null' { + $result.RemoteDistributor | Should -BeNullOrEmpty + } + It 'Get method returns WorkingDirectory as $null' { + $result.WorkingDirectory | Should -BeNullOrEmpty + } } } @@ -339,13 +343,23 @@ try Assert-MockCalled -CommandName Install-RemoteDistributor -Times 1 ` -ParameterFilter { $RemoteDistributor -eq $testParameters.RemoteDistributor } } - It 'Set method doesnt call Install-LocalDistributor' { + It 'Set method does not call Install-LocalDistributor' { Assert-MockCalled -CommandName Install-LocalDistributor -Times 0 } - It 'Set method doesnt call Uninstall-Distributor' { + It 'Set method does not call Uninstall-Distributor' { Assert-MockCalled -CommandName Uninstall-Distributor -Times 0 } } + + Context 'When calling the Set method with the ''Remote'' distributor mode, but does not provide the parameter RemoteDistributor' { + BeforeAll { + $testParameters.Remove('RemoteDistributor') + } + + It 'Should throw the correct errror' { + { Set-TargetResource @testParameters } | Should -Throw $script:localizedData.NoRemoteDistributor + } + } } Describe 'The system is in sync given Local distribution mode' { @@ -398,19 +412,19 @@ try Assert-MockCalled -CommandName New-ReplicationServer -Times 1 ` -ParameterFilter { $ServerConnection.ServerInstance -eq 'SERVERNAME' } } - It 'Get method doesnt call New-DistributionDatabase' { + It 'Get method does not call New-DistributionDatabase' { Assert-MockCalled -CommandName New-DistributionDatabase -Times 0 } - It 'Get method doesnt call Install-LocalDistributor' { + It 'Get method does not call Install-LocalDistributor' { Assert-MockCalled -CommandName Install-LocalDistributor -Times 0 } - It 'Get method doesnt call Install-RemoteDistributor' { + It 'Get method does not call Install-RemoteDistributor' { Assert-MockCalled -CommandName Install-RemoteDistributor -Times 0 } - It 'Ger method doesnt call Register-DistributorPublisher' { + It 'Ger method does not call Register-DistributorPublisher' { Assert-MockCalled -CommandName Register-DistributorPublisher -Times 0 } - It 'Ger method doesnt call Uninstall-Distributor' { + It 'Ger method does not call Uninstall-Distributor' { Assert-MockCalled -CommandName Uninstall-Distributor -Times 0 } It 'Get method returns Ensure = Present' { @@ -457,19 +471,19 @@ try Assert-MockCalled -CommandName New-ReplicationServer -Times 1 ` -ParameterFilter { $ServerConnection.ServerInstance -eq 'SERVERNAME' } } - It 'Set method doesnt call New-DistributionDatabase with $DistributionDBName = distribution' { + It 'Set method does not call New-DistributionDatabase with $DistributionDBName = distribution' { Assert-MockCalled -CommandName New-DistributionDatabase -Times 0 } - It 'Set method doesnt call Install-LocalDistributor' { + It 'Set method does not call Install-LocalDistributor' { Assert-MockCalled -CommandName Install-LocalDistributor -Times 0 } - It 'Set method doesnt call Install-RemoteDistributor' { + It 'Set method does not call Install-RemoteDistributor' { Assert-MockCalled -CommandName Install-RemoteDistributor -Times 0 } - It 'Set method doesnt call Register-DistributorPublisher' { + It 'Set method does not call Register-DistributorPublisher' { Assert-MockCalled -CommandName Register-DistributorPublisher -Times 0 } - It 'Set method doesnt call Uninstall-Distributor' { + It 'Set method does not call Uninstall-Distributor' { Assert-MockCalled -CommandName Uninstall-Distributor -Times 0 } } @@ -526,19 +540,19 @@ try Assert-MockCalled -CommandName New-ReplicationServer -Times 1 ` -ParameterFilter { $ServerConnection.ServerInstance -eq 'SERVERNAME\INSTANCENAME' } } - It 'Get method doesnt call New-DistributionDatabase' { + It 'Get method does not call New-DistributionDatabase' { Assert-MockCalled -CommandName New-DistributionDatabase -Times 0 } - It 'Get method doesnt call Install-LocalDistributor' { + It 'Get method does not call Install-LocalDistributor' { Assert-MockCalled -CommandName Install-LocalDistributor -Times 0 } - It 'Get method doesnt call Install-RemoteDistributor' { + It 'Get method does not call Install-RemoteDistributor' { Assert-MockCalled -CommandName Install-RemoteDistributor -Times 0 } - It 'Ger method doesnt call Register-DistributorPublisher' { + It 'Ger method does not call Register-DistributorPublisher' { Assert-MockCalled -CommandName Register-DistributorPublisher -Times 0 } - It 'Ger method doesnt call Uninstall-Distributor' { + It 'Ger method does not call Uninstall-Distributor' { Assert-MockCalled -CommandName Uninstall-Distributor -Times 0 } It 'Get method returns Ensure = Present' { @@ -585,19 +599,19 @@ try Assert-MockCalled -CommandName New-ReplicationServer -Times 1 ` -ParameterFilter { $ServerConnection.ServerInstance -eq 'SERVERNAME\INSTANCENAME' } } - It 'Set method doesnt call New-DistributionDatabase' { + It 'Set method does not call New-DistributionDatabase' { Assert-MockCalled -CommandName New-DistributionDatabase -Times 0 } - It 'Set method doesnt call Install-LocalDistributor' { + It 'Set method does not call Install-LocalDistributor' { Assert-MockCalled -CommandName Install-LocalDistributor -Times 0 } - It 'Set method doesnt call Install-RemoteDistributor' { + It 'Set method does not call Install-RemoteDistributor' { Assert-MockCalled -CommandName Install-RemoteDistributor -Times 0 } - It 'Set method doesnt call Register-DistributorPublisher' { + It 'Set method does not call Register-DistributorPublisher' { Assert-MockCalled -CommandName Register-DistributorPublisher -Times 0 } - It 'Set method doesnt call Uninstall-Distributor' { + It 'Set method does not call Uninstall-Distributor' { Assert-MockCalled -CommandName Uninstall-Distributor -Times 0 } } @@ -653,19 +667,19 @@ try Assert-MockCalled -CommandName New-ReplicationServer -Times 1 ` -ParameterFilter { $ServerConnection.ServerInstance -eq 'SERVERNAME' } } - It 'Get method doesnt call New-DistributionDatabase' { + It 'Get method does not call New-DistributionDatabase' { Assert-MockCalled -CommandName New-DistributionDatabase -Times 0 } - It 'Get method doesnt call Install-LocalDistributor' { + It 'Get method does not call Install-LocalDistributor' { Assert-MockCalled -CommandName Install-LocalDistributor -Times 0 } - It 'Get method doesnt call Install-RemoteDistributor' { + It 'Get method does not call Install-RemoteDistributor' { Assert-MockCalled -CommandName Install-RemoteDistributor -Times 0 } - It 'Ger method doesnt call Register-DistributorPublisher' { + It 'Ger method does not call Register-DistributorPublisher' { Assert-MockCalled -CommandName Register-DistributorPublisher -Times 0 } - It 'Ger method doesnt call Uninstall-Distributor' { + It 'Ger method does not call Uninstall-Distributor' { Assert-MockCalled -CommandName Uninstall-Distributor -Times 0 } It 'Get method returns Ensure = Present' { @@ -717,16 +731,16 @@ try Assert-MockCalled -CommandName Uninstall-Distributor -Times 1 ` -ParameterFilter { $ReplicationServer.DistributionServer -eq 'SERVERNAME' } } - It 'Set method doesnt call New-DistributionDatabase' { + It 'Set method does not call New-DistributionDatabase' { Assert-MockCalled -CommandName New-DistributionDatabase -Times 0 } - It 'Set method doesnt call Install-LocalDistributor' { + It 'Set method does not call Install-LocalDistributor' { Assert-MockCalled -CommandName Install-LocalDistributor -Times 0 } - It 'Set method doesnt call Install-RemoteDistributor' { + It 'Set method does not call Install-RemoteDistributor' { Assert-MockCalled -CommandName Install-RemoteDistributor -Times 0 } - It 'Set method doesnt call Register-DistributorPublisher' { + It 'Set method does not call Register-DistributorPublisher' { Assert-MockCalled -CommandName Register-DistributorPublisher -Times 0 } } @@ -783,19 +797,19 @@ try Assert-MockCalled -CommandName New-ReplicationServer -Times 1 ` -ParameterFilter { $ServerConnection.ServerInstance -eq 'SERVERNAME\INSTANCENAME' } } - It 'Get method doesnt call New-DistributionDatabase' { + It 'Get method does not call New-DistributionDatabase' { Assert-MockCalled -CommandName New-DistributionDatabase -Times 0 } - It 'Get method doesnt call Install-LocalDistributor' { + It 'Get method does not call Install-LocalDistributor' { Assert-MockCalled -CommandName Install-LocalDistributor -Times 0 } - It 'Get method doesnt call Install-RemoteDistributor' { + It 'Get method does not call Install-RemoteDistributor' { Assert-MockCalled -CommandName Install-RemoteDistributor -Times 0 } - It 'Ger method doesnt call Register-DistributorPublisher' { + It 'Ger method does not call Register-DistributorPublisher' { Assert-MockCalled -CommandName Register-DistributorPublisher -Times 0 } - It 'Ger method doesnt call Uninstall-Distributor' { + It 'Ger method does not call Uninstall-Distributor' { Assert-MockCalled -CommandName Uninstall-Distributor -Times 0 } It 'Get method returns Ensure = Present' { @@ -846,16 +860,16 @@ try Assert-MockCalled -CommandName Uninstall-Distributor -Times 1 ` -ParameterFilter { $ReplicationServer.DistributionServer -eq 'REMOTESERVER' } } - It 'Set method doesnt call New-DistributionDatabase' { + It 'Set method does not call New-DistributionDatabase' { Assert-MockCalled -CommandName New-DistributionDatabase -Times 0 } - It 'Set method doesnt call Install-LocalDistributor' { + It 'Set method does not call Install-LocalDistributor' { Assert-MockCalled -CommandName Install-LocalDistributor -Times 0 } - It 'Set method doesnt call Install-RemoteDistributor' { + It 'Set method does not call Install-RemoteDistributor' { Assert-MockCalled -CommandName Install-RemoteDistributor -Times 0 } - It 'Set method doesnt call Register-DistributorPublisher' { + It 'Set method does not call Register-DistributorPublisher' { Assert-MockCalled -CommandName Register-DistributorPublisher -Times 0 } } @@ -911,19 +925,19 @@ try Assert-MockCalled -CommandName New-ReplicationServer -Times 1 ` -ParameterFilter { $ServerConnection.ServerInstance -eq 'SERVERNAME' } } - It 'Get method doesnt call New-DistributionDatabase' { + It 'Get method does not call New-DistributionDatabase' { Assert-MockCalled -CommandName New-DistributionDatabase -Times 0 } - It 'Get method doesnt call Install-LocalDistributor' { + It 'Get method does not call Install-LocalDistributor' { Assert-MockCalled -CommandName Install-LocalDistributor -Times 0 } - It 'Get method doesnt call Install-RemoteDistributor' { + It 'Get method does not call Install-RemoteDistributor' { Assert-MockCalled -CommandName Install-RemoteDistributor -Times 0 } - It 'Ger method doesnt call Register-DistributorPublisher' { + It 'Ger method does not call Register-DistributorPublisher' { Assert-MockCalled -CommandName Register-DistributorPublisher -Times 0 } - It 'Ger method doesnt call Uninstall-Distributor' { + It 'Ger method does not call Uninstall-Distributor' { Assert-MockCalled -CommandName Uninstall-Distributor -Times 0 } It 'Get method returns Ensure = Absent' { @@ -932,17 +946,17 @@ try It "Get method returns InstanceName = $($testParameters.InstanceName)" { $result.InstanceName | Should -Be $testParameters.InstanceName } - It "Get method returns DistributorMode = $($testParameters.DistributorMode)" { - $result.DistributorMode | Should -Be $testParameters.DistributorMode + It 'Get method returns DistributorMode as $null' { + $result.DistributorMode | Should -BeNullOrEmpty } - It 'Get method returns DistributionDBName = distribution' { - $result.DistributionDBName | Should -Be 'distribution' + It 'Get method returns DistributionDBName as $null' { + $result.DistributionDBName | Should -BeNullOrEmpty } - It 'Get method returns RemoteDistributor is empty' { - $result.RemoteDistributor | Should -Be '' + It 'Get method returns RemoteDistributor as $null' { + $result.RemoteDistributor | Should -BeNullOrEmpty } - It "Get method returns WorkingDirectory = $($testParameters.WorkingDirectory)" { - $result.WorkingDirectory | Should -Be $testParameters.WorkingDirectory + It 'Get method returns WorkingDirectory as $null' { + $result.WorkingDirectory | Should -BeNullOrEmpty } } @@ -970,19 +984,19 @@ try Assert-MockCalled -CommandName New-ReplicationServer -Times 1 ` -ParameterFilter { $ServerConnection.ServerInstance -eq 'SERVERNAME' } } - It 'Set method doesnt call New-DistributionDatabase with $DistributionDBName = distribution' { + It 'Set method does not call New-DistributionDatabase with $DistributionDBName = distribution' { Assert-MockCalled -CommandName New-DistributionDatabase -Times 0 } - It 'Set method doesnt call Install-LocalDistributor' { + It 'Set method does not call Install-LocalDistributor' { Assert-MockCalled -CommandName Install-LocalDistributor -Times 0 } - It 'Set method doesnt call Install-RemoteDistributor' { + It 'Set method does not call Install-RemoteDistributor' { Assert-MockCalled -CommandName Install-RemoteDistributor -Times 0 } - It 'Set method doesnt call Register-DistributorPublisher' { + It 'Set method does not call Register-DistributorPublisher' { Assert-MockCalled -CommandName Register-DistributorPublisher -Times 0 } - It 'Set method doesnt call Uninstall-Distributor' { + It 'Set method does not call Uninstall-Distributor' { Assert-MockCalled -CommandName Uninstall-Distributor -Times 0 } }