Skip to content

Commit

Permalink
SqlServerDsc.Common\Get-ServerProtocolObject: Updated to throw error/…
Browse files Browse the repository at this point in the history
…exception if no ServerInstances returned (#1651)

- SqlServerDsc.Common
  - Updated `Get-ServerProtocolObject`, helper function to ensure an exception is
    thrown if the specified instance cannot be obtained (issue #1628).
  • Loading branch information
SphenicPaul authored Dec 14, 2020
1 parent c073ddd commit f1b15bf
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixes ([issue #396](https://github.com/dsccommunity/SqlServerDsc/issues/396)).
Added three return values in Get-Target resource.
- SqlProtocol
- Changed KeepAlive Type from UInt16 to Int32 to reflect te actual WMI.ManagementObject
- Changed KeepAlive Type from UInt16 to Int32 to reflect the actual WMI.ManagementObject
Fixes #1645 ([issue #1645](https://github.com/dsccommunity/SqlServerDsc/issues/1645)).
- SqlServerDsc.Common
- Updated `Get-ServerProtocolObject`, helper function to ensure an exception is
thrown if the specified instance cannot be obtained ([issue #1628](https://github.com/dsccommunity/SqlServerDsc/issues/1628)).

## [15.0.0] - 2020-12-06

Expand Down
5 changes: 5 additions & 0 deletions source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -2333,6 +2333,11 @@ function Get-ServerProtocolObject

$serverProtocolProperties = $serverInstance.ServerProtocols[$protocolNameProperties.Name]
}
else
{
$errorMessage = $script:localizedData.FailedToObtainServerInstance -f $InstanceName, $ServerName
New-InvalidOperationException -Message $errorMessage
}

return $serverProtocolProperties
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ 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)
'@
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,5 @@ 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)
'@
29 changes: 29 additions & 0 deletions tests/Unit/SqlServerDsc.Common.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3431,6 +3431,35 @@ InModuleScope $script:subModuleName {
$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 = @()
}
}
}

It 'Should throw the correct error message' {
$mockGetServerProtocolObjectParameters = @{
ServerName = $mockServerName
Instance = $mockInstanceName
ProtocolName = 'TcpIp'
}

$mockErrorRecord = Get-InvalidOperationRecord -Message (
$script:localizedData.FailedToObtainServerInstance -f $mockInstanceName, $mockServerName
)

$mockErrorRecord.Exception.Message | Should -Not -BeNullOrEmpty

{ Get-ServerProtocolObject @mockGetServerProtocolObjectParameters } | Should -Throw -ExpectedMessage $mockErrorRecord.Exception.Message
}
}
}

Describe 'SqlServerDsc.Common\ConvertTo-ServerInstanceName' {
Expand Down

0 comments on commit f1b15bf

Please sign in to comment.