diff --git a/CHANGELOG.md b/CHANGELOG.md index 385931ee70..67b8cd725a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 clarity. The old names `*-SqlDscBIReportServer` and `*-SqlDscPBIReportServer` are available as aliases for backward compatibility ([issue #2071](https://github.com/dsccommunity/SqlServerDsc/issues/2071)). +- `SqlProtocol` + - Refactored to use the public command `Get-SqlDscServerProtocolName` instead + of the deprecated private function `Get-ProtocolNameProperties` + ([issue #2104](https://github.com/dsccommunity/SqlServerDsc/issues/2104)). + - Refactored to use the public command `Get-SqlDscServerProtocol` instead + of the deprecated private function `Get-ServerProtocolObject` + ([issue #2104](https://github.com/dsccommunity/SqlServerDsc/issues/2104)). +- `SqlProtocolTcpIp` + - Refactored to use the public command `Get-SqlDscServerProtocol` instead + of the deprecated private function `Get-ServerProtocolObject` + ([issue #2104](https://github.com/dsccommunity/SqlServerDsc/issues/2104)). - `SqlPermission` - Refactored to use the new object-based server permission commands (`Grant-SqlDscServerPermission`, `Deny-SqlDscServerPermission`, @@ -96,6 +107,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 `System.Management.Automation.SwitchParameter` to follow PowerShell best practices. ([issue #2190](https://github.com/dsccommunity/SqlServerDsc/issues/2190)). +### Removed + +- Removed deprecated private function `Get-ProtocolNameProperties` from the + SqlServerDsc.Common module. Use the public command `Get-SqlDscServerProtocolName` + instead ([issue #2104](https://github.com/dsccommunity/SqlServerDsc/issues/2104)). +- Removed deprecated private function `Get-ServerProtocolObject` from the + SqlServerDsc.Common module. Use the public command `Get-SqlDscServerProtocol` + instead ([issue #2104](https://github.com/dsccommunity/SqlServerDsc/issues/2104)). + ## [17.3.0] - 2025-12-01 ### Removed diff --git a/source/DSCResources/DSC_SqlProtocol/DSC_SqlProtocol.psm1 b/source/DSCResources/DSC_SqlProtocol/DSC_SqlProtocol.psm1 index 7f16a1a6d8..59fa04c788 100644 --- a/source/DSCResources/DSC_SqlProtocol/DSC_SqlProtocol.psm1 +++ b/source/DSCResources/DSC_SqlProtocol/DSC_SqlProtocol.psm1 @@ -80,7 +80,7 @@ function Get-TargetResource HasMultiIPAddresses = $false } - $protocolNameProperties = Get-ProtocolNameProperties -ProtocolName $ProtocolName + $protocolNameProperties = Get-SqlDscServerProtocolName -ProtocolName $ProtocolName # Getting the server protocol properties by using the computer name. $computerName = Get-ComputerName @@ -95,13 +95,13 @@ function Get-TargetResource Must connect to the local machine name because $ServerName can point to a cluster instance or availability group listener. #> - $getServerProtocolObjectParameters = @{ + $getSqlDscServerProtocolParameters = @{ ServerName = $computerName - Instance = $InstanceName + InstanceName = $InstanceName ProtocolName = $ProtocolName } - $serverProtocolProperties = Get-ServerProtocolObject @getServerProtocolObjectParameters + $serverProtocolProperties = Get-SqlDscServerProtocol @getSqlDscServerProtocolParameters if ($serverProtocolProperties) { @@ -229,12 +229,10 @@ function Set-TargetResource $RestartTimeout = 120 ) - $protocolNameProperties = Get-ProtocolNameProperties -ProtocolName $ProtocolName + $protocolNameProperties = Get-SqlDscServerProtocolName -ProtocolName $ProtocolName <# - Compare the current state against the desired state. Calling this will - also import the necessary module to later call Get-ServerProtocolObject - which uses the SMO class ManagedComputer. + Compare the current state against the desired state. #> $propertyState = Compare-TargetResourceState @PSBoundParameters @@ -254,13 +252,13 @@ function Set-TargetResource Must connect to the local machine name because $ServerName can point to a cluster instance or availability group listener. #> - $getServerProtocolObjectParameters = @{ + $getSqlDscServerProtocolParameters = @{ ServerName = $computerName - Instance = $InstanceName + InstanceName = $InstanceName ProtocolName = $ProtocolName } - $serverProtocolProperties = Get-ServerProtocolObject @getServerProtocolObjectParameters + $serverProtocolProperties = Get-SqlDscServerProtocol @getSqlDscServerProtocolParameters if ($serverProtocolProperties) { @@ -459,7 +457,7 @@ function Test-TargetResource $RestartTimeout = 120 ) - $protocolNameProperties = Get-ProtocolNameProperties -ProtocolName $ProtocolName + $protocolNameProperties = Get-SqlDscServerProtocolName -ProtocolName $ProtocolName Write-Verbose -Message ( $script:localizedData.TestDesiredState -f $protocolNameProperties.DisplayName, $InstanceName, $ServerName diff --git a/source/DSCResources/DSC_SqlProtocolTcpIp/DSC_SqlProtocolTcpIp.psm1 b/source/DSCResources/DSC_SqlProtocolTcpIp/DSC_SqlProtocolTcpIp.psm1 index 2418198c40..ac2b214b42 100644 --- a/source/DSCResources/DSC_SqlProtocolTcpIp/DSC_SqlProtocolTcpIp.psm1 +++ b/source/DSCResources/DSC_SqlProtocolTcpIp/DSC_SqlProtocolTcpIp.psm1 @@ -103,13 +103,13 @@ function Get-TargetResource Must connect to the local machine name because $ServerName can point to a cluster instance or availability group listener. #> - $getServerProtocolObjectParameters = @{ + $getSqlDscServerProtocolParameters = @{ ServerName = $computerName - Instance = $InstanceName + InstanceName = $InstanceName ProtocolName = 'TcpIp' } - $serverProtocolProperties = Get-ServerProtocolObject @getServerProtocolObjectParameters + $serverProtocolProperties = Get-SqlDscServerProtocol @getSqlDscServerProtocolParameters if ($serverProtocolProperties) { @@ -274,9 +274,7 @@ function Set-TargetResource $IpAddressGroup = Convert-IpAdressGroupCasing -IpAddressGroup $IpAddressGroup <# - Compare the current state against the desired state. Calling this will - also import the necessary module to later call Get-ServerProtocolObject - which uses the SMO class ManagedComputer. + Compare the current state against the desired state. #> $propertyState = Compare-TargetResourceState @PSBoundParameters @@ -296,13 +294,13 @@ function Set-TargetResource Must connect to the local machine name because $ServerName can point to a cluster instance or availability group listener. #> - $getServerProtocolObjectParameters = @{ + $getSqlDscServerProtocolParameters = @{ ServerName = $computerName - Instance = $InstanceName + InstanceName = $InstanceName ProtocolName = 'TcpIp' } - $serverProtocolProperties = Get-ServerProtocolObject @getServerProtocolObjectParameters + $serverProtocolProperties = Get-SqlDscServerProtocol @getSqlDscServerProtocolParameters if ($serverProtocolProperties) { diff --git a/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psd1 b/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psd1 index 69b14bb1b8..fb54758b7b 100644 --- a/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psd1 +++ b/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psd1 @@ -45,8 +45,6 @@ 'Find-ExceptionByNumber' 'Compare-ResourcePropertyState' 'Test-DscPropertyState' - 'Get-ProtocolNameProperties' - 'Get-ServerProtocolObject' 'Import-Assembly' 'ConvertTo-ServerInstanceName' 'Get-FilePathMajorVersion' diff --git a/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1 b/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1 index bf1f200c06..3249e35fab 100644 --- a/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1 +++ b/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1 @@ -1943,126 +1943,6 @@ function Find-ExceptionByNumber return $errorFound } -<# - .SYNOPSIS - Get static name properties of he specified protocol. - - .PARAMETER ProtocolName - Specifies the name of network protocol to return name properties for. - Possible values are 'TcpIp', 'NamedPipes', or 'ShareMemory'. - - .NOTES - The static values returned matches the values returned by the class - ServerProtocol. The property DisplayName could potentially be localized - while the property Name must be exactly like it is returned by the - class ServerProtocol, with the correct casing. - - The Get-ProtocolNameProperties function is deprecated and should be removed - in the future when existing code has moved to new public commands. -#> -function Get-ProtocolNameProperties -{ - [CmdletBinding()] - [OutputType([System.Collections.Hashtable])] - param - ( - [Parameter(Mandatory = $true)] - [ValidateSet('TcpIp', 'NamedPipes', 'SharedMemory')] - [System.String] - $ProtocolName - ) - - $protocolNameProperties = @{ } - - switch ($ProtocolName) - { - 'TcpIp' - { - $protocolNameProperties.DisplayName = 'TCP/IP' - $protocolNameProperties.Name = 'Tcp' - } - - 'NamedPipes' - { - $protocolNameProperties.DisplayName = 'Named Pipes' - $protocolNameProperties.Name = 'Np' - } - - 'SharedMemory' - { - $protocolNameProperties.DisplayName = 'Shared Memory' - $protocolNameProperties.Name = 'Sm' - } - } - - return $protocolNameProperties -} - -<# - .SYNOPSIS - Returns the ServerProtocol object for the specified SQL Server instance - and protocol name. - - .PARAMETER InstanceName - Specifies the name of the SQL Server instance to connect to. - - .PARAMETER ProtocolName - Specifies the name of network protocol to be configured. Possible values - are 'TcpIp', 'NamedPipes', or 'ShareMemory'. - - .PARAMETER ServerName - Specifies the host name of the SQL Server to connect to. - - .NOTES - The class Microsoft.SqlServer.Management.Smo.Wmi.ServerProtocol is - returned by this function. -#> -function Get-ServerProtocolObject -{ - [CmdletBinding()] - param - ( - [Parameter(Mandatory = $true)] - [System.String] - $InstanceName, - - [Parameter(Mandatory = $true)] - [ValidateSet('TcpIp', 'NamedPipes', 'SharedMemory')] - [System.String] - $ProtocolName, - - [Parameter(Mandatory = $true)] - [ValidateNotNullOrEmpty()] - [System.String] - $ServerName - ) - - $serverProtocolProperties = $null - - $newObjectParameters = @{ - TypeName = 'Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer' - ArgumentList = @($ServerName) - } - - $managedComputerObject = New-Object @newObjectParameters - - $serverInstance = $managedComputerObject.ServerInstances[$InstanceName] - - if ($serverInstance) - { - $protocolNameProperties = Get-ProtocolNameProperties -ProtocolName $ProtocolName - - $serverProtocolProperties = $serverInstance.ServerProtocols[$protocolNameProperties.Name] - } - else - { - $errorMessage = $script:localizedData.FailedToObtainServerInstance -f $InstanceName, $ServerName - New-InvalidOperationException -Message $errorMessage - } - - return $serverProtocolProperties -} - <# .SYNOPSIS Converts the combination of server name and instance name to diff --git a/source/Modules/SqlServerDsc.Common/en-US/SqlServerDsc.Common.strings.psd1 b/source/Modules/SqlServerDsc.Common/en-US/SqlServerDsc.Common.strings.psd1 index bc351cb592..558e95261d 100644 --- a/source/Modules/SqlServerDsc.Common/en-US/SqlServerDsc.Common.strings.psd1 +++ b/source/Modules/SqlServerDsc.Common/en-US/SqlServerDsc.Common.strings.psd1 @@ -43,7 +43,6 @@ ConvertFrom-StringData @' NotOwnerOfClusterResource = The node '{0}' is not the owner of the cluster resource '{1}'. The owner is '{2}' so no restart is needed. (SQLCOMMON0067) LoadedAssembly = Loaded the assembly '{0}'. (SQLCOMMON0068) FailedToLoadAssembly = Failed to load the assembly '{0}'. (SQLCOMMON0069) - FailedToObtainServerInstance = Failed to obtain a SQL Server instance with name '{0}' on server '{1}'. Ensure the SQL Server instance exists on the server and that the 'SQLServer' module references a version of the 'Microsoft.SqlServer.Management.Smo.Wmi' library that supports the version of the SQL Server instance. (SQLCOMMON0070) DatabaseEngineInstanceNotOnline = The SQL instance '{0}' was expected to have the status 'Online', but had status '{1}'. (SQLCOMMON0071) WaitForDatabaseEngineInstanceStatus = The SQL instance status is '{0}' expected '{1}', waiting {2} seconds. (SQLCOMMON0072) '@ diff --git a/source/Modules/SqlServerDsc.Common/sv-SE/SqlServerDsc.Common.strings.psd1 b/source/Modules/SqlServerDsc.Common/sv-SE/SqlServerDsc.Common.strings.psd1 index 5aaf6b8902..819b92a81a 100644 --- a/source/Modules/SqlServerDsc.Common/sv-SE/SqlServerDsc.Common.strings.psd1 +++ b/source/Modules/SqlServerDsc.Common/sv-SE/SqlServerDsc.Common.strings.psd1 @@ -49,7 +49,6 @@ ConvertFrom-StringData @' NotOwnerOfClusterResource = The node '{0}' is not the owner of the cluster resource '{1}'. The owner is '{2}' so no restart is needed. (SQLCOMMON0067) LoadedAssembly = Loaded the assembly '{0}'. (SQLCOMMON0068) FailedToLoadAssembly = Failed to load the assembly '{0}'. (SQLCOMMON0069) - FailedToObtainServerInstance = Failed to obtain a SQL Server instance with name '{0}' on server '{1}'. Ensure the SQL Server instance exists on the server and that the 'SQLServer' module references a version of the 'Microsoft.SqlServer.Management.Smo.Wmi' library that supports the version of the SQL Server instance. (SQLCOMMON0070) DatabaseEngineInstanceNotOnline = The SQL instance '{0}' was expected to have the status 'Online', but had status '{1}'. (SQLCOMMON0071) WaitForDatabaseEngineInstanceStatus = The SQL instance status is '{0}' expected '{1}', waiting {2} seconds. (SQLCOMMON0072) '@ diff --git a/tests/Unit/DSC_SqlProtocol.Tests.ps1 b/tests/Unit/DSC_SqlProtocol.Tests.ps1 index 287bda9d34..f4d6fc398b 100644 --- a/tests/Unit/DSC_SqlProtocol.Tests.ps1 +++ b/tests/Unit/DSC_SqlProtocol.Tests.ps1 @@ -43,6 +43,9 @@ BeforeAll { Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath '..\TestHelpers\CommonTestHelper.psm1') + # Load the SMO stub types + Add-Type -Path (Join-Path -Path $PSScriptRoot -ChildPath 'Stubs\SMO.cs') + $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:dscResourceName $PSDefaultParameterValues['Mock:ModuleName'] = $script:dscResourceName $PSDefaultParameterValues['Should:ModuleName'] = $script:dscResourceName @@ -86,9 +89,7 @@ Describe 'SqlProtocol\Get-TargetResource' -Tag 'Get' { Context 'When the system is not in the desired state' { Context 'When the SQL Server instance does not exist' { BeforeAll { - Mock -CommandName Get-ServerProtocolObject -MockWith { - return $null - } + Mock -CommandName Get-SqlDscServerProtocol } It 'Should return the correct values' { @@ -132,7 +133,7 @@ Describe 'SqlProtocol\Get-TargetResource' -Tag 'Get' { Context 'When the system is in the desired state' { Context 'When the desired protocol is TCP/IP' { BeforeAll { - Mock -CommandName Get-ServerProtocolObject -MockWith { + Mock -CommandName Get-SqlDscServerProtocol -MockWith { return @{ IsEnabled = $true HasMultiIPAddresses = $true @@ -178,7 +179,7 @@ Describe 'SqlProtocol\Get-TargetResource' -Tag 'Get' { BeforeAll { $mockPipeName = '\\.\pipe\$$\TESTCLU01A\MSSQL$SQL2014\sql\query' - Mock -CommandName Get-ServerProtocolObject -MockWith { + Mock -CommandName Get-SqlDscServerProtocol -MockWith { return @{ IsEnabled = $true HasMultiIPAddresses = $false @@ -223,7 +224,7 @@ Describe 'SqlProtocol\Get-TargetResource' -Tag 'Get' { Context 'When the desired protocol is Shared Memory' { BeforeAll { - Mock -CommandName Get-ServerProtocolObject -MockWith { + Mock -CommandName Get-SqlDscServerProtocol -MockWith { return @{ IsEnabled = $true HasMultiIPAddresses = $false @@ -260,7 +261,7 @@ Describe 'SqlProtocol\Get-TargetResource' -Tag 'Get' { Context 'When the restart service is requested' { BeforeAll { - Mock -CommandName Get-ServerProtocolObject -MockWith { + Mock -CommandName Get-SqlDscServerProtocol -MockWith { return @{ IsEnabled = $true HasMultiIPAddresses = $false @@ -735,9 +736,7 @@ Describe 'SqlProtocol\Set-TargetResource' -Tag 'Set' { ) } - Mock -CommandName Get-ServerProtocolObject -MockWith { - return $null - } + Mock -CommandName Get-SqlDscServerProtocol } It 'Should throw the correct error' { @@ -813,7 +812,7 @@ Describe 'SqlProtocol\Set-TargetResource' -Tag 'Set' { ) } - Mock -CommandName Get-ServerProtocolObject -MockWith { + Mock -CommandName Get-SqlDscServerProtocol -MockWith { return New-Object -TypeName PSObject | Add-Member -MemberType NoteProperty -Name 'IsEnabled' -Value $false -PassThru | Add-Member -MemberType ScriptProperty -Name 'ProtocolProperties' -Value { @@ -874,7 +873,7 @@ Describe 'SqlProtocol\Set-TargetResource' -Tag 'Set' { ) } - Mock -CommandName Get-ServerProtocolObject -MockWith { + Mock -CommandName Get-SqlDscServerProtocol -MockWith { return New-Object -TypeName PSObject | Add-Member -MemberType NoteProperty -Name 'IsEnabled' -Value $false -PassThru | Add-Member -MemberType ScriptProperty -Name 'ProtocolProperties' -Value { @@ -933,7 +932,7 @@ Describe 'SqlProtocol\Set-TargetResource' -Tag 'Set' { ) } - Mock -CommandName Get-ServerProtocolObject -MockWith { + Mock -CommandName Get-SqlDscServerProtocol -MockWith { return New-Object -TypeName PSObject | Add-Member -MemberType NoteProperty -Name 'IsEnabled' -Value $true -PassThru | Add-Member -MemberType ScriptProperty -Name 'ProtocolProperties' -Value { @@ -998,7 +997,7 @@ Describe 'SqlProtocol\Set-TargetResource' -Tag 'Set' { ) } - Mock -CommandName Get-ServerProtocolObject -MockWith { + Mock -CommandName Get-SqlDscServerProtocol -MockWith { return New-Object -TypeName PSObject | Add-Member -MemberType NoteProperty -Name 'IsEnabled' -Value $false -PassThru | Add-Member -MemberType ScriptProperty -Name 'ProtocolProperties' -Value { @@ -1059,7 +1058,7 @@ Describe 'SqlProtocol\Set-TargetResource' -Tag 'Set' { ) } - Mock -CommandName Get-ServerProtocolObject -MockWith { + Mock -CommandName Get-SqlDscServerProtocol -MockWith { return New-Object -TypeName PSObject | Add-Member -MemberType NoteProperty -Name 'IsEnabled' -Value $false -PassThru | Add-Member -MemberType ScriptProperty -Name 'ProtocolProperties' -Value { @@ -1128,7 +1127,7 @@ Describe 'SqlProtocol\Set-TargetResource' -Tag 'Set' { ) } - Mock -CommandName Get-ServerProtocolObject -MockWith { + Mock -CommandName Get-SqlDscServerProtocol -MockWith { return New-Object -TypeName PSObject | Add-Member -MemberType NoteProperty -Name 'IsEnabled' -Value $false -PassThru | Add-Member -MemberType ScriptProperty -Name 'ProtocolProperties' -Value { @@ -1186,7 +1185,7 @@ Describe 'SqlProtocol\Set-TargetResource' -Tag 'Set' { ) } - Mock -CommandName Get-ServerProtocolObject -MockWith { + Mock -CommandName Get-SqlDscServerProtocol -MockWith { return New-Object -TypeName PSObject | Add-Member -MemberType NoteProperty -Name 'IsEnabled' -Value $false -PassThru | Add-Member -MemberType ScriptProperty -Name 'ProtocolProperties' -Value { @@ -1243,7 +1242,7 @@ Describe 'SqlProtocol\Set-TargetResource' -Tag 'Set' { ) } - Mock -CommandName Get-ServerProtocolObject -MockWith { + Mock -CommandName Get-SqlDscServerProtocol -MockWith { return New-Object -TypeName PSObject | Add-Member -MemberType NoteProperty -Name 'IsEnabled' -Value $false -PassThru | Add-Member -MemberType ScriptProperty -Name 'ProtocolProperties' -Value { diff --git a/tests/Unit/DSC_SqlProtocolTcpIp.Tests.ps1 b/tests/Unit/DSC_SqlProtocolTcpIp.Tests.ps1 index 1a47fe0e48..6409b15307 100644 --- a/tests/Unit/DSC_SqlProtocolTcpIp.Tests.ps1 +++ b/tests/Unit/DSC_SqlProtocolTcpIp.Tests.ps1 @@ -43,6 +43,9 @@ BeforeAll { Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath '..\TestHelpers\CommonTestHelper.psm1') + # Load the SMO stub types + Add-Type -Path (Join-Path -Path $PSScriptRoot -ChildPath 'Stubs\SMO.cs') + $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:dscResourceName $PSDefaultParameterValues['Mock:ModuleName'] = $script:dscResourceName $PSDefaultParameterValues['Should:ModuleName'] = $script:dscResourceName @@ -76,9 +79,7 @@ Describe 'SqlProtocolTcpIp\Get-TargetResource' -Tag 'Get' { Context 'When the system is not in the desired state' { Context 'When the SQL Server instance does not exist' { BeforeAll { - Mock -CommandName Get-ServerProtocolObject -MockWith { - return $null - } + Mock -CommandName Get-SqlDscServerProtocol } It 'Should return the correct values' { @@ -117,7 +118,7 @@ Describe 'SqlProtocolTcpIp\Get-TargetResource' -Tag 'Get' { Context 'When the IP address group is missing' { BeforeAll { Mock -CommandName Write-Warning - Mock -CommandName Get-ServerProtocolObject -MockWith { + Mock -CommandName Get-SqlDscServerProtocol -MockWith { return @{ IPAddresses = @( [PSCustomObject] @{ @@ -152,7 +153,7 @@ Describe 'SqlProtocolTcpIp\Get-TargetResource' -Tag 'Get' { Context 'When the IP address group is IPAll' { Context 'When the IP address group is using dynamic port' { BeforeAll { - Mock -CommandName Get-ServerProtocolObject -MockWith { + Mock -CommandName Get-SqlDscServerProtocol -MockWith { return @{ IPAddresses = @{ Name = 'IPAll' @@ -201,7 +202,7 @@ Describe 'SqlProtocolTcpIp\Get-TargetResource' -Tag 'Get' { Context 'When the IP address group is using static TCP ports' { BeforeAll { - Mock -CommandName Get-ServerProtocolObject -MockWith { + Mock -CommandName Get-SqlDscServerProtocol -MockWith { return @{ IPAddresses = @{ Name = 'IPAll' @@ -252,7 +253,7 @@ Describe 'SqlProtocolTcpIp\Get-TargetResource' -Tag 'Get' { Context 'When the IP address group is IPx (where x is an available group number)' { Context 'When the IP address group is using dynamic port' { BeforeAll { - Mock -CommandName Get-ServerProtocolObject -MockWith { + Mock -CommandName Get-SqlDscServerProtocol -MockWith { return @{ IPAddresses = @{ Name = 'IP1' @@ -311,7 +312,7 @@ Describe 'SqlProtocolTcpIp\Get-TargetResource' -Tag 'Get' { Context 'When the IP address group is using static TCP ports' { BeforeAll { - Mock -CommandName Get-ServerProtocolObject -MockWith { + Mock -CommandName Get-SqlDscServerProtocol -MockWith { return @{ IPAddresses = @{ Name = 'IP1' @@ -959,9 +960,7 @@ Describe 'SqlProtocolTcpIp\Set-TargetResource' -Tag 'Set' { ) } - Mock -CommandName Get-ServerProtocolObject -MockWith { - return $null - } + Mock -CommandName Get-SqlDscServerProtocol } It 'Should throw the correct error' { @@ -991,7 +990,7 @@ Describe 'SqlProtocolTcpIp\Set-TargetResource' -Tag 'Set' { ) } - Mock -CommandName Get-ServerProtocolObject -MockWith { + Mock -CommandName Get-SqlDscServerProtocol -MockWith { return @{ IPAddresses = @( [PSCustomObject] @{ @@ -1066,7 +1065,7 @@ Describe 'SqlProtocolTcpIp\Set-TargetResource' -Tag 'Set' { ) } - Mock -CommandName Get-ServerProtocolObject -MockWith { + Mock -CommandName Get-SqlDscServerProtocol -MockWith { return New-Object -TypeName PSObject | Add-Member -MemberType NoteProperty -Name 'IPAddresses' -Value @{ Name = 'IPAll' @@ -1118,7 +1117,7 @@ Describe 'SqlProtocolTcpIp\Set-TargetResource' -Tag 'Set' { Addition evaluation is done in the mock to test if the object is set correctly. #> - $script:wasMethodAlterCalled | Should -BeTru + $script:wasMethodAlterCalled | Should -BeTrue } Should -Invoke -CommandName Restart-SqlService -Exactly -Times 1 -Scope It @@ -1139,7 +1138,7 @@ Describe 'SqlProtocolTcpIp\Set-TargetResource' -Tag 'Set' { ) } - Mock -CommandName Get-ServerProtocolObject -MockWith { + Mock -CommandName Get-SqlDscServerProtocol -MockWith { return New-Object -TypeName PSObject | Add-Member -MemberType NoteProperty -Name 'IPAddresses' -Value @{ Name = 'IPAll' @@ -1213,7 +1212,7 @@ Describe 'SqlProtocolTcpIp\Set-TargetResource' -Tag 'Set' { ) } - Mock -CommandName Get-ServerProtocolObject -MockWith { + Mock -CommandName Get-SqlDscServerProtocol -MockWith { return New-Object -TypeName PSObject | Add-Member -MemberType NoteProperty -Name 'IPAddresses' -Value @{ Name = 'IP1' @@ -1283,7 +1282,7 @@ Describe 'SqlProtocolTcpIp\Set-TargetResource' -Tag 'Set' { ) } - Mock -CommandName Get-ServerProtocolObject -MockWith { + Mock -CommandName Get-SqlDscServerProtocol -MockWith { return New-Object -TypeName PSObject | Add-Member -MemberType NoteProperty -Name 'IPAddresses' -Value @{ Name = 'IP1' @@ -1355,7 +1354,7 @@ Describe 'SqlProtocolTcpIp\Set-TargetResource' -Tag 'Set' { ) } - Mock -CommandName Get-ServerProtocolObject -MockWith { + Mock -CommandName Get-SqlDscServerProtocol -MockWith { return New-Object -TypeName PSObject | Add-Member -MemberType NoteProperty -Name 'IPAddresses' -Value @{ Name = 'IP1' @@ -1430,7 +1429,7 @@ Describe 'SqlProtocolTcpIp\Set-TargetResource' -Tag 'Set' { ) } - Mock -CommandName Get-ServerProtocolObject -MockWith { + Mock -CommandName Get-SqlDscServerProtocol -MockWith { return New-Object -TypeName PSObject | Add-Member -MemberType NoteProperty -Name 'IPAddresses' -Value @{ Name = 'IP1' diff --git a/tests/Unit/SqlServerDsc.Common.Tests.ps1 b/tests/Unit/SqlServerDsc.Common.Tests.ps1 index 9e76277c4e..be2ccdeaff 100644 --- a/tests/Unit/SqlServerDsc.Common.Tests.ps1 +++ b/tests/Unit/SqlServerDsc.Common.Tests.ps1 @@ -3172,110 +3172,6 @@ Describe 'SqlServerDsc.Common\Find-ExceptionByNumber' -Tag 'FindExceptionByNumbe } } -Describe 'SqlServerDsc.Common\Get-ProtocolNameProperties' -Tag 'GetProtocolNameProperties' { - It "Should return the correct values when the protocol is ''" -ForEach @( - @{ - ParameterValue = 'TcpIp' - DisplayName = 'TCP/IP' - Name = 'Tcp' - } - @{ - ParameterValue = 'NamedPipes' - DisplayName = 'Named Pipes' - Name = 'Np' - } - @{ - ParameterValue = 'SharedMemory' - DisplayName = 'Shared Memory' - Name = 'Sm' - } - ) { - $result = Get-ProtocolNameProperties -ProtocolName $ParameterValue - - $result.DisplayName | Should -Be $DisplayName - $result.Name | Should -Be $Name - } -} - -Describe 'SqlServerDsc.Common\Get-ServerProtocolObject' -Tag 'GetServerProtocolObject' { - BeforeAll { - $mockInstanceName = 'TestInstance' - - Mock -CommandName New-Object -MockWith { - return @{ - ServerInstances = @{ - $mockInstanceName = @{ - ServerProtocols = @{ - Tcp = @{ - IsEnabled = $true - HasMultiIPAddresses = $true - ProtocolProperties = @{ - ListenOnAllIPs = $true - KeepAlive = 30000 - } - } - } - } - } - } - } -ParameterFilter { - # Make sure to only mock the creation of the type we want to mock. - $TypeName -eq 'Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer' - } - } - - It 'Should return a ManagedComputer object with the correct values' { - $mockGetServerProtocolObjectParameters = @{ - ServerName = 'AnyServer' - Instance = $mockInstanceName - ProtocolName = 'TcpIp' - } - - $result = Get-ServerProtocolObject @mockGetServerProtocolObjectParameters - - $result.IsEnabled | Should -BeTrue - $result.HasMultiIPAddresses | Should -BeTrue - $result.ProtocolProperties.ListenOnAllIPs | Should -BeTrue - $result.ProtocolProperties.KeepAlive | Should -Be 30000 - } - - Context "When ManagedComputer object has an empty array, 'ServerInstances' value" { - BeforeAll { - $mockServerName = 'TestServerName' - $mockInstanceName = 'TestInstance' - - Mock -CommandName New-Object -MockWith { - return @{ - ServerInstances = @() - } - } -ParameterFilter { - # Make sure to only mock the creation of the type we want to mock. - $TypeName -eq 'Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer' - } - } - - It 'Should throw the correct error message' { - $mockGetServerProtocolObjectParameters = @{ - ServerName = $mockServerName - Instance = $mockInstanceName - ProtocolName = 'TcpIp' - } - - $mockLocalizedString = InModuleScope -ScriptBlock { - $script:localizedData.FailedToObtainServerInstance - } - - $mockErrorRecord = Get-InvalidOperationRecord -Message ( - $mockLocalizedString -f $mockInstanceName, $mockServerName - ) - - $mockErrorRecord.Exception.Message | Should -Not -BeNullOrEmpty - - { Get-ServerProtocolObject @mockGetServerProtocolObjectParameters } | Should -Throw -ExpectedMessage $mockErrorRecord.Exception.Message - } - } -} - Describe 'SqlServerDsc.Common\ConvertTo-ServerInstanceName' -Tag 'ConvertToServerInstanceName' { BeforeAll { $mockComputerName = Get-ComputerName