From f1b15bf98379a6d57a75c6e0fcb27c976ef5dea4 Mon Sep 17 00:00:00 2001 From: "Paul J. Wilcox" Date: Mon, 14 Dec 2020 07:15:25 +0000 Subject: [PATCH] SqlServerDsc.Common\Get-ServerProtocolObject: Updated to throw error/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). --- CHANGELOG.md | 5 +++- .../SqlServerDsc.Common.psm1 | 5 ++++ .../en-US/SqlServerDsc.Common.strings.psd1 | 1 + .../sv-SE/SqlServerDsc.Common.strings.psd1 | 1 + tests/Unit/SqlServerDsc.Common.Tests.ps1 | 29 +++++++++++++++++++ 5 files changed, 40 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index abb5f15b0..1c5c6d2a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1 b/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1 index 187bd1f1a..cab2c8cc0 100644 --- a/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1 +++ b/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1 @@ -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 } 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 99b1afd3f..10f86f4ad 100644 --- a/source/Modules/SqlServerDsc.Common/en-US/SqlServerDsc.Common.strings.psd1 +++ b/source/Modules/SqlServerDsc.Common/en-US/SqlServerDsc.Common.strings.psd1 @@ -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) '@ 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 bdd63f519..c68dd6233 100644 --- a/source/Modules/SqlServerDsc.Common/sv-SE/SqlServerDsc.Common.strings.psd1 +++ b/source/Modules/SqlServerDsc.Common/sv-SE/SqlServerDsc.Common.strings.psd1 @@ -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) '@ diff --git a/tests/Unit/SqlServerDsc.Common.Tests.ps1 b/tests/Unit/SqlServerDsc.Common.Tests.ps1 index 1bae2b3e3..f17644fe0 100644 --- a/tests/Unit/SqlServerDsc.Common.Tests.ps1 +++ b/tests/Unit/SqlServerDsc.Common.Tests.ps1 @@ -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' {